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 * What kind of tripple DES are these 2 routines?
31 * Why is there a redundant initialization vector?
33 * If only iv3 was used, then, this would till effect have been
34 * outer-cbc. However, there is also a private iv1 == iv2 which
35 * perhaps makes differential analysis easier. On the other hand, the
36 * private iv1 probably makes the CRC-32 attack ineffective. This is a
37 * result of that there is no longer any known iv1 to use when
38 * choosing the X block.
41 SSH_3CBC_ENCRYPT(des_key_schedule ks1,
42 des_key_schedule ks2, des_cblock * iv2,
43 des_key_schedule ks3, des_cblock * iv3,
44 unsigned char *dest, unsigned char *src,
51 des_cbc_encrypt(src, dest, len, ks1, &iv1, DES_ENCRYPT);
52 memcpy(&iv1, dest + len - 8, 8);
54 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_DECRYPT);
55 memcpy(iv2, &iv1, 8); /* Note how iv1 == iv2 on entry and exit. */
57 des_cbc_encrypt(dest, dest, len, ks3, iv3, DES_ENCRYPT);
58 memcpy(iv3, dest + len - 8, 8);
62 SSH_3CBC_DECRYPT(des_key_schedule ks1,
63 des_key_schedule ks2, des_cblock * iv2,
64 des_key_schedule ks3, des_cblock * iv3,
65 unsigned char *dest, unsigned char *src,
72 des_cbc_encrypt(src, dest, len, ks3, iv3, DES_DECRYPT);
73 memcpy(iv3, src + len - 8, 8);
75 des_cbc_encrypt(dest, dest, len, ks2, iv2, DES_ENCRYPT);
76 memcpy(iv2, dest + len - 8, 8);
78 des_cbc_encrypt(dest, dest, len, ks1, &iv1, DES_DECRYPT);
79 /* memcpy(&iv1, iv2, 8); */
80 /* Note how iv1 == iv2 on entry and exit. */
84 * SSH uses a variation on Blowfish, all bytes must be swapped before
85 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
88 swap_bytes(const unsigned char *src, unsigned char *dst_, int n)
90 /* dst must be properly aligned. */
91 u_int32_t *dst = (u_int32_t *) dst_;
97 /* Process 8 bytes every lap. */
98 for (n = n / 8; n > 0; n--) {
114 * Names of all encryption algorithms.
115 * These must match the numbers defined in cipher.h.
117 static char *cipher_names[] =
134 * Returns a bit mask indicating which ciphers are supported by this
135 * implementation. The bit mask has the corresponding bit set of each
142 unsigned int mask = 0;
143 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
144 mask |= 1 << SSH_CIPHER_BLOWFISH;
145 mask |= 1 << SSH_CIPHER_BLOWFISH_CBC;
146 mask |= 1 << SSH_CIPHER_3DES_CBC;
147 mask |= 1 << SSH_CIPHER_ARCFOUR;
148 mask |= 1 << SSH_CIPHER_CAST128_CBC;
152 /* Returns the name of the cipher. */
155 cipher_name(int cipher)
157 if (cipher < 0 || cipher >= sizeof(cipher_names) / sizeof(cipher_names[0]) ||
158 cipher_names[cipher] == NULL)
159 fatal("cipher_name: bad cipher number: %d", cipher);
160 return cipher_names[cipher];
164 * Parses the name of the cipher. Returns the number of the corresponding
165 * cipher, or -1 on error.
169 cipher_number(const char *name)
172 for (i = 0; i < sizeof(cipher_names) / sizeof(cipher_names[0]); i++)
173 if (strcmp(cipher_names[i], name) == 0 &&
174 (cipher_mask() & (1 << i)))
180 * Selects the cipher, and keys if by computing the MD5 checksum of the
181 * passphrase and using the resulting 16 bytes as the key.
185 cipher_set_key_string(CipherContext *context, int cipher,
186 const char *passphrase, int for_encryption)
189 unsigned char digest[16];
192 MD5_Update(&md, (const unsigned char *) passphrase, strlen(passphrase));
193 MD5_Final(digest, &md);
195 cipher_set_key(context, cipher, digest, 16, for_encryption);
197 memset(digest, 0, sizeof(digest));
198 memset(&md, 0, sizeof(md));
201 /* Selects the cipher to use and sets the key. */
204 cipher_set_key(CipherContext *context, int cipher,
205 const unsigned char *key, int keylen, int for_encryption)
207 unsigned char padded[32];
209 /* Set cipher type. */
210 context->type = cipher;
212 /* Get 32 bytes of key data. Pad if necessary. (So that code
213 below does not need to worry about key size). */
214 memset(padded, 0, sizeof(padded));
215 memcpy(padded, key, keylen < sizeof(padded) ? keylen : sizeof(padded));
217 /* Initialize the initialization vector. */
219 case SSH_CIPHER_NONE:
221 * Has to stay for authfile saving of private key with no
226 case SSH_CIPHER_3DES:
228 * Note: the least significant bit of each byte of key is
229 * parity, and must be ignored by the implementation. 16
230 * bytes of key are used (first and last keys are the same).
233 error("Key length %d is insufficient for 3DES.", keylen);
234 des_set_key((void *) padded, context->u.des3.key1);
235 des_set_key((void *) (padded + 8), context->u.des3.key2);
237 des_set_key((void *) padded, context->u.des3.key3);
239 des_set_key((void *) (padded + 16), context->u.des3.key3);
240 memset(context->u.des3.iv2, 0, sizeof(context->u.des3.iv2));
241 memset(context->u.des3.iv3, 0, sizeof(context->u.des3.iv3));
244 case SSH_CIPHER_BLOWFISH:
246 error("Key length %d is insufficient for blowfish.", keylen);
247 BF_set_key(&context->u.bf.key, keylen, padded);
248 memset(context->u.bf.iv, 0, 8);
251 case SSH_CIPHER_3DES_CBC:
252 case SSH_CIPHER_BLOWFISH_CBC:
253 case SSH_CIPHER_ARCFOUR:
254 case SSH_CIPHER_CAST128_CBC:
255 fatal("cipher_set_key: illegal cipher: %s", cipher_name(cipher));
259 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
261 memset(padded, 0, sizeof(padded));
266 cipher_set_key_iv(CipherContext * context, int cipher,
267 const unsigned char *key, int keylen,
268 const unsigned char *iv, int ivlen)
270 /* Set cipher type. */
271 context->type = cipher;
273 /* Initialize the initialization vector. */
275 case SSH_CIPHER_NONE:
278 case SSH_CIPHER_3DES:
279 case SSH_CIPHER_BLOWFISH:
280 fatal("cipher_set_key_iv: illegal cipher: %s", cipher_name(cipher));
283 case SSH_CIPHER_3DES_CBC:
285 error("Key length %d is insufficient for 3des-cbc.", keylen);
286 des_set_key((void *) key, context->u.des3.key1);
287 des_set_key((void *) (key+8), context->u.des3.key2);
288 des_set_key((void *) (key+16), context->u.des3.key3);
290 error("IV length %d is insufficient for 3des-cbc.", ivlen);
291 memcpy(context->u.des3.iv3, (char *)iv, 8);
294 case SSH_CIPHER_BLOWFISH_CBC:
296 error("Key length %d is insufficient for blowfish.", keylen);
298 error("IV length %d is insufficient for blowfish.", ivlen);
299 BF_set_key(&context->u.bf.key, keylen, (unsigned char *)key);
300 memcpy(context->u.bf.iv, (char *)iv, 8);
303 case SSH_CIPHER_ARCFOUR:
305 error("Key length %d is insufficient for arcfour.", keylen);
306 RC4_set_key(&context->u.rc4, keylen, (unsigned char *)key);
309 case SSH_CIPHER_CAST128_CBC:
311 error("Key length %d is insufficient for cast128.", keylen);
313 error("IV length %d is insufficient for cast128.", ivlen);
314 CAST_set_key(&context->u.cast.key, keylen, (unsigned char *) key);
315 memcpy(context->u.cast.iv, (char *)iv, 8);
319 fatal("cipher_set_key: unknown cipher: %s", cipher_name(cipher));
323 /* Encrypts data using the cipher. */
326 cipher_encrypt(CipherContext *context, unsigned char *dest,
327 const unsigned char *src, unsigned int len)
330 fatal("cipher_encrypt: bad plaintext length %d", len);
332 switch (context->type) {
333 case SSH_CIPHER_NONE:
334 memcpy(dest, src, len);
337 case SSH_CIPHER_3DES:
338 SSH_3CBC_ENCRYPT(context->u.des3.key1,
339 context->u.des3.key2, &context->u.des3.iv2,
340 context->u.des3.key3, &context->u.des3.iv3,
341 dest, (unsigned char *) src, len);
344 case SSH_CIPHER_BLOWFISH:
345 swap_bytes(src, dest, len);
346 BF_cbc_encrypt(dest, dest, len,
347 &context->u.bf.key, context->u.bf.iv,
349 swap_bytes(dest, dest, len);
352 case SSH_CIPHER_BLOWFISH_CBC:
353 BF_cbc_encrypt((void *)src, dest, len,
354 &context->u.bf.key, context->u.bf.iv,
358 case SSH_CIPHER_3DES_CBC:
359 des_ede3_cbc_encrypt(src, dest, len,
360 context->u.des3.key1, context->u.des3.key2,
361 context->u.des3.key3, &context->u.des3.iv3, DES_ENCRYPT);
364 case SSH_CIPHER_ARCFOUR:
365 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
368 case SSH_CIPHER_CAST128_CBC:
369 CAST_cbc_encrypt(src, dest, len,
370 &context->u.cast.key, context->u.cast.iv, CAST_ENCRYPT);
374 fatal("cipher_encrypt: unknown cipher: %s", cipher_name(context->type));
378 /* Decrypts data using the cipher. */
381 cipher_decrypt(CipherContext *context, unsigned char *dest,
382 const unsigned char *src, unsigned int len)
385 fatal("cipher_decrypt: bad ciphertext length %d", len);
387 switch (context->type) {
388 case SSH_CIPHER_NONE:
389 memcpy(dest, src, len);
392 case SSH_CIPHER_3DES:
393 SSH_3CBC_DECRYPT(context->u.des3.key1,
394 context->u.des3.key2, &context->u.des3.iv2,
395 context->u.des3.key3, &context->u.des3.iv3,
396 dest, (unsigned char *) src, len);
399 case SSH_CIPHER_BLOWFISH:
400 swap_bytes(src, dest, len);
401 BF_cbc_encrypt((void *) dest, dest, len,
402 &context->u.bf.key, context->u.bf.iv,
404 swap_bytes(dest, dest, len);
407 case SSH_CIPHER_BLOWFISH_CBC:
408 BF_cbc_encrypt((void *) src, dest, len,
409 &context->u.bf.key, context->u.bf.iv,
413 case SSH_CIPHER_3DES_CBC:
414 des_ede3_cbc_encrypt(src, dest, len,
415 context->u.des3.key1, context->u.des3.key2,
416 context->u.des3.key3, &context->u.des3.iv3, DES_DECRYPT);
419 case SSH_CIPHER_ARCFOUR:
420 RC4(&context->u.rc4, len, (unsigned char *)src, dest);
423 case SSH_CIPHER_CAST128_CBC:
424 CAST_cbc_encrypt(src, dest, len,
425 &context->u.cast.key, context->u.cast.iv, CAST_DECRYPT);
429 fatal("cipher_decrypt: unknown cipher: %s", cipher_name(context->type));