5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Wed Apr 19 17:41:39 1995 ylo
21 #include <openssl/md5.h>
24 * This is used by SSH1:
26 * What kind of triple DES are these 2 routines?
28 * Why is there a redundant initialization vector?
30 * If only iv3 was used, then, this would till effect have been
31 * outer-cbc. However, there is also a private iv1 == iv2 which
32 * perhaps makes differential analysis easier. On the other hand, the
33 * private iv1 probably makes the CRC-32 attack ineffective. This is a
34 * result of that there is no longer any known iv1 to use when
35 * choosing the X block.
38 SSH_3CBC_ENCRYPT(des_key_schedule ks1,
39 des_key_schedule ks2, des_cblock * iv2,
40 des_key_schedule ks3, des_cblock * iv3,
41 unsigned char *dest, unsigned char *src,
48 des_cbc_encrypt(src, dest, len, ks1, &iv1, DES_ENCRYPT);
49 memcpy(&iv1, dest + len - 8, 8);
51 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_DECRYPT);
52 memcpy(iv2, &iv1, 8); /* Note how iv1 == iv2 on entry and exit. */
54 des_cbc_encrypt(dest, dest, len, ks3, iv3, DES_ENCRYPT);
55 memcpy(iv3, dest + len - 8, 8);
59 SSH_3CBC_DECRYPT(des_key_schedule ks1,
60 des_key_schedule ks2, des_cblock * iv2,
61 des_key_schedule ks3, des_cblock * iv3,
62 unsigned char *dest, unsigned char *src,
69 des_cbc_encrypt(src, dest, len, ks3, iv3, DES_DECRYPT);
70 memcpy(iv3, src + len - 8, 8);
72 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_ENCRYPT);
73 memcpy(iv2, dest + len - 8, 8);
75 des_cbc_encrypt(dest, dest, len, ks1, &iv1, DES_DECRYPT);
76 /* memcpy(&iv1, iv2, 8); */
77 /* Note how iv1 == iv2 on entry and exit. */
81 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
82 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
85 swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
87 /* dst must be properly aligned. */
88 u_int32_t *dst = (u_int32_t *) dst_;
94 /* Process 8 bytes every lap. */
95 for (n = n / 8; n > 0; n--) {
111 * Names of all encryption algorithms.
112 * These must match the numbers defined in cipher.h.
114 static char *cipher_names[] =
131 * Returns a bit mask indicating which ciphers are supported by this
132 * implementation. The bit mask has the corresponding bit set of each
139 unsigned int mask = 0;
140 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
141 mask |= 1 << SSH_CIPHER_BLOWFISH;
147 unsigned int mask = 0;
148 mask |= 1 << SSH_CIPHER_BLOWFISH_CBC;
149 mask |= 1 << SSH_CIPHER_3DES_CBC;
150 mask |= 1 << SSH_CIPHER_ARCFOUR;
151 mask |= 1 << SSH_CIPHER_CAST128_CBC;
157 return cipher_mask1() | cipher_mask2();
160 /* Returns the name of the cipher. */
163 cipher_name(int cipher)
165 if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]) ||
166 cipher_names[cipher] == NULL)
167 fatal("cipher_name: bad cipher name: %d", cipher);
168 return cipher_names[cipher];
171 /* Returns 1 if the name of the ciphers are valid. */
173 #define CIPHER_SEP ","
175 ciphers_valid(const char *names)
181 if (strcmp(names, "") == 0)
183 ciphers = xstrdup(names);
184 for ((p = strtok(ciphers, CIPHER_SEP)); p; (p = strtok(NULL, CIPHER_SEP))) {
185 i = cipher_number(p);
186 if (i == -1 || !(cipher_mask2() & (1 << i))) {
196 * Parses the name of the cipher. Returns the number of the corresponding
197 * cipher, or -1 on error.
201 cipher_number(const char *name)
204 for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++)
205 if (strcmp(cipher_names[i], name) == 0 &&
206 (cipher_mask() & (1 << i)))
212 * Selects the cipher, and keys if by computing the MD5 checksum of the
213 * passphrase and using the resulting 16 bytes as the key.
217 cipher_set_key_string(CipherContext *context, int cipher, const char *passphrase)
220 unsigned char digest[16];
223 MD5_Update(&md, (const unsigned char *) passphrase, strlen(passphrase));
224 MD5_Final(digest, &md);
226 cipher_set_key(context, cipher, digest, 16);
228 memset(digest, 0, sizeof(digest));
229 memset(&md, 0, sizeof(md));
232 /* Selects the cipher to use and sets the key. */
235 cipher_set_key(CipherContext *context, int cipher, const unsigned char *key,
238 unsigned char padded[32];
240 /* Set cipher type. */
241 context->type = cipher;
243 /* Get 32 bytes of key data. Pad if necessary. (So that code
244 below does not need to worry about key size). */
245 memset(padded, 0, sizeof(padded));
246 memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
248 /* Initialize the initialization vector. */
250 case SSH_CIPHER_NONE:
252 * Has to stay for authfile saving of private key with no
257 case SSH_CIPHER_3DES:
259 * Note: the least significant bit of each byte of key is
260 * parity, and must be ignored by the implementation. 16
261 * bytes of key are used (first and last keys are the same).
264 error("Key length %d is insufficient for 3DES.", keylen);
265 des_set_key((void *) padded, context->u.des3.key1);
266 des_set_key((void *) (padded + 8), context->u.des3.key2);
268 des_set_key((void *) padded, context->u.des3.key3);
270 des_set_key((void *) (padded + 16), context->u.des3.key3);
271 memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
272 memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
275 case SSH_CIPHER_BLOWFISH:
277 error("Key length %d is insufficient for blowfish.", keylen);
278 BF_set_key(&context->u.bf.key, keylen, padded);
279 memset(context->u.bf.iv, 0, 8);
282 case SSH_CIPHER_3DES_CBC:
283 case SSH_CIPHER_BLOWFISH_CBC:
284 case SSH_CIPHER_ARCFOUR:
285 case SSH_CIPHER_CAST128_CBC:
286 fatal("cipher_set_key: illegal cipher: %s", cipher_name(cipher));
290 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
292 memset(padded, 0, sizeof(padded));
296 cipher_set_key_iv(CipherContext * context, int cipher,
297 const unsigned char *key, int keylen,
298 const unsigned char *iv, int ivlen)
300 /* Set cipher type. */
301 context->type = cipher;
303 /* Initialize the initialization vector. */
305 case SSH_CIPHER_NONE:
308 case SSH_CIPHER_3DES:
309 case SSH_CIPHER_BLOWFISH:
310 fatal("cipher_set_key_iv: illegal cipher: %s", cipher_name(cipher));
313 case SSH_CIPHER_3DES_CBC:
315 error("Key length %d is insufficient for 3des-cbc.", keylen);
316 des_set_key((void *) key, context->u.des3.key1);
317 des_set_key((void *) (key+8), context->u.des3.key2);
318 des_set_key((void *) (key+16), context->u.des3.key3);
320 error("IV length %d is insufficient for 3des-cbc.", ivlen);
321 memcpy(context->u.des3.iv3, (char *)iv, 8);
324 case SSH_CIPHER_BLOWFISH_CBC:
326 error("Key length %d is insufficient for blowfish.", keylen);
328 error("IV length %d is insufficient for blowfish.", ivlen);
329 BF_set_key(&context->u.bf.key, keylen, (unsigned char *)key);
330 memcpy(context->u.bf.iv, (char *)iv, 8);
333 case SSH_CIPHER_ARCFOUR:
335 error("Key length %d is insufficient for arcfour.", keylen);
336 RC4_set_key(&context->u.rc4, keylen, (unsigned char *)key);
339 case SSH_CIPHER_CAST128_CBC:
341 error("Key length %d is insufficient for cast128.", keylen);
343 error("IV length %d is insufficient for cast128.", ivlen);
344 CAST_set_key(&context->u.cast.key, keylen, (unsigned char *) key);
345 memcpy(context->u.cast.iv, (char *)iv, 8);
349 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
353 /* Encrypts data using the cipher. */
356 cipher_encrypt(CipherContext *context, unsigned char *dest,
357 const unsigned char *src, unsigned int len)
360 fatal("cipher_encrypt: bad plaintext length %d", len);
362 switch (context->type) {
363 case SSH_CIPHER_NONE:
364 memcpy(dest, src, len);
367 case SSH_CIPHER_3DES:
368 SSH_3CBC_ENCRYPT(context->u.des3.key1,
369 context->u.des3.key2, &context->u.des3.iv2,
370 context->u.des3.key3, &context->u.des3.iv3,
371 dest, (unsigned char *) src, len);
374 case SSH_CIPHER_BLOWFISH:
375 swap_bytes(src, dest, len);
376 BF_cbc_encrypt(dest, dest, len,
377 &context->u.bf.key, context->u.bf.iv,
379 swap_bytes(dest, dest, len);
382 case SSH_CIPHER_BLOWFISH_CBC:
383 BF_cbc_encrypt((void *)src, dest, len,
384 &context->u.bf.key, context->u.bf.iv,
388 case SSH_CIPHER_3DES_CBC:
389 des_ede3_cbc_encrypt(src, dest, len,
390 context->u.des3.key1, context->u.des3.key2,
391 context->u.des3.key3, &context->u.des3.iv3, DES_ENCRYPT);
394 case SSH_CIPHER_ARCFOUR:
395 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
398 case SSH_CIPHER_CAST128_CBC:
399 CAST_cbc_encrypt(src, dest, len,
400 &context->u.cast.key, context->u.cast.iv, CAST_ENCRYPT);
404 fatal("cipher_encrypt: unknown cipher: %s", cipher_name(context->type));
408 /* Decrypts data using the cipher. */
411 cipher_decrypt(CipherContext *context, unsigned char *dest,
412 const unsigned char *src, unsigned int len)
415 fatal("cipher_decrypt: bad ciphertext length %d", len);
417 switch (context->type) {
418 case SSH_CIPHER_NONE:
419 memcpy(dest, src, len);
422 case SSH_CIPHER_3DES:
423 SSH_3CBC_DECRYPT(context->u.des3.key1,
424 context->u.des3.key2, &context->u.des3.iv2,
425 context->u.des3.key3, &context->u.des3.iv3,
426 dest, (unsigned char *) src, len);
429 case SSH_CIPHER_BLOWFISH:
430 swap_bytes(src, dest, len);
431 BF_cbc_encrypt((void *) dest, dest, len,
432 &context->u.bf.key, context->u.bf.iv,
434 swap_bytes(dest, dest, len);
437 case SSH_CIPHER_BLOWFISH_CBC:
438 BF_cbc_encrypt((void *) src, dest, len,
439 &context->u.bf.key, context->u.bf.iv,
443 case SSH_CIPHER_3DES_CBC:
444 des_ede3_cbc_encrypt(src, dest, len,
445 context->u.des3.key1, context->u.des3.key2,
446 context->u.des3.key3, &context->u.des3.iv3, DES_DECRYPT);
449 case SSH_CIPHER_ARCFOUR:
450 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
453 case SSH_CIPHER_CAST128_CBC:
454 CAST_cbc_encrypt(src, dest, len,
455 &context->u.cast.key, context->u.cast.iv, CAST_DECRYPT);
459 fatal("cipher_decrypt: unknown cipher: %s", cipher_name(context->type));