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
22 #include <openssl/md5.h>
29 * This is used by SSH1:
31 * What kind of triple DES are these 2 routines?
33 * Why is there a redundant initialization vector?
35 * If only iv3 was used, then, this would till effect have been
36 * outer-cbc. However, there is also a private iv1 == iv2 which
37 * perhaps makes differential analysis easier. On the other hand, the
38 * private iv1 probably makes the CRC-32 attack ineffective. This is a
39 * result of that there is no longer any known iv1 to use when
40 * choosing the X block.
43 SSH_3CBC_ENCRYPT(des_key_schedule ks1,
44 des_key_schedule ks2, des_cblock * iv2,
45 des_key_schedule ks3, des_cblock * iv3,
46 unsigned char *dest, unsigned char *src,
53 des_cbc_encrypt(src, dest, len, ks1, &iv1, DES_ENCRYPT);
54 memcpy(&iv1, dest + len - 8, 8);
56 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_DECRYPT);
57 memcpy(iv2, &iv1, 8); /* Note how iv1 == iv2 on entry and exit. */
59 des_cbc_encrypt(dest, dest, len, ks3, iv3, DES_ENCRYPT);
60 memcpy(iv3, dest + len - 8, 8);
64 SSH_3CBC_DECRYPT(des_key_schedule ks1,
65 des_key_schedule ks2, des_cblock * iv2,
66 des_key_schedule ks3, des_cblock * iv3,
67 unsigned char *dest, unsigned char *src,
74 des_cbc_encrypt(src, dest, len, ks3, iv3, DES_DECRYPT);
75 memcpy(iv3, src + len - 8, 8);
77 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_ENCRYPT);
78 memcpy(iv2, dest + len - 8, 8);
80 des_cbc_encrypt(dest, dest, len, ks1, &iv1, DES_DECRYPT);
81 /* memcpy(&iv1, iv2, 8); */
82 /* Note how iv1 == iv2 on entry and exit. */
86 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
87 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
90 swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
92 /* dst must be properly aligned. */
93 u_int32_t *dst = (u_int32_t *) dst_;
99 /* Process 8 bytes every lap. */
100 for (n = n / 8; n > 0; n--) {
116 * Names of all encryption algorithms.
117 * These must match the numbers defined in cipher.h.
119 static char *cipher_names[] =
136 * Returns a bit mask indicating which ciphers are supported by this
137 * implementation. The bit mask has the corresponding bit set of each
144 unsigned int mask = 0;
145 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
146 mask |= 1 << SSH_CIPHER_BLOWFISH;
152 unsigned int mask = 0;
153 mask |= 1 << SSH_CIPHER_BLOWFISH_CBC;
154 mask |= 1 << SSH_CIPHER_3DES_CBC;
155 mask |= 1 << SSH_CIPHER_ARCFOUR;
156 mask |= 1 << SSH_CIPHER_CAST128_CBC;
162 return cipher_mask1() | cipher_mask2();
165 /* Returns the name of the cipher. */
168 cipher_name(int cipher)
170 if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]) ||
171 cipher_names[cipher] == NULL)
172 fatal("cipher_name: bad cipher name: %d", cipher);
173 return cipher_names[cipher];
176 /* Returns 1 if the name of the ciphers are valid. */
178 #define CIPHER_SEP ","
180 ciphers_valid(const char *names)
186 if (strcmp(names, "") == 0)
188 ciphers = xstrdup(names);
189 for ((p = strtok(ciphers, CIPHER_SEP)); p; (p = strtok(NULL, CIPHER_SEP))) {
190 i = cipher_number(p);
191 if (i == -1 || !(cipher_mask2() & (1 << i))) {
201 * Parses the name of the cipher. Returns the number of the corresponding
202 * cipher, or -1 on error.
206 cipher_number(const char *name)
209 for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++)
210 if (strcmp(cipher_names[i], name) == 0 &&
211 (cipher_mask() & (1 << i)))
217 * Selects the cipher, and keys if by computing the MD5 checksum of the
218 * passphrase and using the resulting 16 bytes as the key.
222 cipher_set_key_string(CipherContext *context, int cipher, const char *passphrase)
225 unsigned char digest[16];
228 MD5_Update(&md, (const unsigned char *) passphrase, strlen(passphrase));
229 MD5_Final(digest, &md);
231 cipher_set_key(context, cipher, digest, 16);
233 memset(digest, 0, sizeof(digest));
234 memset(&md, 0, sizeof(md));
237 /* Selects the cipher to use and sets the key. */
240 cipher_set_key(CipherContext *context, int cipher, const unsigned char *key,
243 unsigned char padded[32];
245 /* Set cipher type. */
246 context->type = cipher;
248 /* Get 32 bytes of key data. Pad if necessary. (So that code
249 below does not need to worry about key size). */
250 memset(padded, 0, sizeof(padded));
251 memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
253 /* Initialize the initialization vector. */
255 case SSH_CIPHER_NONE:
257 * Has to stay for authfile saving of private key with no
262 case SSH_CIPHER_3DES:
264 * Note: the least significant bit of each byte of key is
265 * parity, and must be ignored by the implementation. 16
266 * bytes of key are used (first and last keys are the same).
269 error("Key length %d is insufficient for 3DES.", keylen);
270 des_set_key((void *) padded, context->u.des3.key1);
271 des_set_key((void *) (padded + 8), context->u.des3.key2);
273 des_set_key((void *) padded, context->u.des3.key3);
275 des_set_key((void *) (padded + 16), context->u.des3.key3);
276 memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
277 memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
280 case SSH_CIPHER_BLOWFISH:
282 error("Key length %d is insufficient for blowfish.", keylen);
283 BF_set_key(&context->u.bf.key, keylen, padded);
284 memset(context->u.bf.iv, 0, 8);
287 case SSH_CIPHER_3DES_CBC:
288 case SSH_CIPHER_BLOWFISH_CBC:
289 case SSH_CIPHER_ARCFOUR:
290 case SSH_CIPHER_CAST128_CBC:
291 fatal("cipher_set_key: illegal cipher: %s", cipher_name(cipher));
295 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
297 memset(padded, 0, sizeof(padded));
301 cipher_set_key_iv(CipherContext * context, int cipher,
302 const unsigned char *key, int keylen,
303 const unsigned char *iv, int ivlen)
305 /* Set cipher type. */
306 context->type = cipher;
308 /* Initialize the initialization vector. */
310 case SSH_CIPHER_NONE:
313 case SSH_CIPHER_3DES:
314 case SSH_CIPHER_BLOWFISH:
315 fatal("cipher_set_key_iv: illegal cipher: %s", cipher_name(cipher));
318 case SSH_CIPHER_3DES_CBC:
320 error("Key length %d is insufficient for 3des-cbc.", keylen);
321 des_set_key((void *) key, context->u.des3.key1);
322 des_set_key((void *) (key+8), context->u.des3.key2);
323 des_set_key((void *) (key+16), context->u.des3.key3);
325 error("IV length %d is insufficient for 3des-cbc.", ivlen);
326 memcpy(context->u.des3.iv3, (char *)iv, 8);
329 case SSH_CIPHER_BLOWFISH_CBC:
331 error("Key length %d is insufficient for blowfish.", keylen);
333 error("IV length %d is insufficient for blowfish.", ivlen);
334 BF_set_key(&context->u.bf.key, keylen, (unsigned char *)key);
335 memcpy(context->u.bf.iv, (char *)iv, 8);
338 case SSH_CIPHER_ARCFOUR:
340 error("Key length %d is insufficient for arcfour.", keylen);
341 RC4_set_key(&context->u.rc4, keylen, (unsigned char *)key);
344 case SSH_CIPHER_CAST128_CBC:
346 error("Key length %d is insufficient for cast128.", keylen);
348 error("IV length %d is insufficient for cast128.", ivlen);
349 CAST_set_key(&context->u.cast.key, keylen, (unsigned char *) key);
350 memcpy(context->u.cast.iv, (char *)iv, 8);
354 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
358 /* Encrypts data using the cipher. */
361 cipher_encrypt(CipherContext *context, unsigned char *dest,
362 const unsigned char *src, unsigned int len)
365 fatal("cipher_encrypt: bad plaintext length %d", len);
367 switch (context->type) {
368 case SSH_CIPHER_NONE:
369 memcpy(dest, src, len);
372 case SSH_CIPHER_3DES:
373 SSH_3CBC_ENCRYPT(context->u.des3.key1,
374 context->u.des3.key2, &context->u.des3.iv2,
375 context->u.des3.key3, &context->u.des3.iv3,
376 dest, (unsigned char *) src, len);
379 case SSH_CIPHER_BLOWFISH:
380 swap_bytes(src, dest, len);
381 BF_cbc_encrypt(dest, dest, len,
382 &context->u.bf.key, context->u.bf.iv,
384 swap_bytes(dest, dest, len);
387 case SSH_CIPHER_BLOWFISH_CBC:
388 BF_cbc_encrypt((void *)src, dest, len,
389 &context->u.bf.key, context->u.bf.iv,
393 case SSH_CIPHER_3DES_CBC:
394 des_ede3_cbc_encrypt(src, dest, len,
395 context->u.des3.key1, context->u.des3.key2,
396 context->u.des3.key3, &context->u.des3.iv3, DES_ENCRYPT);
399 case SSH_CIPHER_ARCFOUR:
400 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
403 case SSH_CIPHER_CAST128_CBC:
404 CAST_cbc_encrypt(src, dest, len,
405 &context->u.cast.key, context->u.cast.iv, CAST_ENCRYPT);
409 fatal("cipher_encrypt: unknown cipher: %s", cipher_name(context->type));
413 /* Decrypts data using the cipher. */
416 cipher_decrypt(CipherContext *context, unsigned char *dest,
417 const unsigned char *src, unsigned int len)
420 fatal("cipher_decrypt: bad ciphertext length %d", len);
422 switch (context->type) {
423 case SSH_CIPHER_NONE:
424 memcpy(dest, src, len);
427 case SSH_CIPHER_3DES:
428 SSH_3CBC_DECRYPT(context->u.des3.key1,
429 context->u.des3.key2, &context->u.des3.iv2,
430 context->u.des3.key3, &context->u.des3.iv3,
431 dest, (unsigned char *) src, len);
434 case SSH_CIPHER_BLOWFISH:
435 swap_bytes(src, dest, len);
436 BF_cbc_encrypt((void *) dest, dest, len,
437 &context->u.bf.key, context->u.bf.iv,
439 swap_bytes(dest, dest, len);
442 case SSH_CIPHER_BLOWFISH_CBC:
443 BF_cbc_encrypt((void *) src, dest, len,
444 &context->u.bf.key, context->u.bf.iv,
448 case SSH_CIPHER_3DES_CBC:
449 des_ede3_cbc_encrypt(src, dest, len,
450 context->u.des3.key1, context->u.des3.key2,
451 context->u.des3.key3, &context->u.des3.iv3, DES_DECRYPT);
454 case SSH_CIPHER_ARCFOUR:
455 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
458 case SSH_CIPHER_CAST128_CBC:
459 CAST_cbc_encrypt(src, dest, len,
460 &context->u.cast.key, context->u.cast.iv, CAST_DECRYPT);
464 fatal("cipher_decrypt: unknown cipher: %s", cipher_name(context->type));