Annotation of src/usr.bin/ssh/cipher.c, Revision 1.87
1.87 ! djm 1: /* $OpenBSD: cipher.c,v 1.86 2013/01/12 11:22:04 djm Exp $ */
1.1 deraadt 2: /*
1.30 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
1.26 markus 6: *
1.30 deraadt 7: * As far as I am concerned, the code I have written for this software
8: * can be used freely for any purpose. Any derived versions of this
9: * software must be clearly marked as such, and if the derived work is
10: * incompatible with the protocol description in the RFC file, it must be
11: * called by a name other than "ssh" or "Secure Shell".
1.26 markus 12: *
13: *
1.30 deraadt 14: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.46 markus 15: * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
1.26 markus 16: *
1.30 deraadt 17: * Redistribution and use in source and binary forms, with or without
18: * modification, are permitted provided that the following conditions
19: * are met:
20: * 1. Redistributions of source code must retain the above copyright
21: * notice, this list of conditions and the following disclaimer.
22: * 2. Redistributions in binary form must reproduce the above copyright
23: * notice, this list of conditions and the following disclaimer in the
24: * documentation and/or other materials provided with the distribution.
1.26 markus 25: *
1.30 deraadt 26: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.17 deraadt 36: */
1.1 deraadt 37:
1.81 deraadt 38: #include <sys/types.h>
1.1 deraadt 39:
1.80 stevesk 40: #include <openssl/md5.h>
41:
42: #include <string.h>
1.81 deraadt 43: #include <stdarg.h>
1.80 stevesk 44:
1.24 markus 45: #include "xmalloc.h"
1.42 markus 46: #include "log.h"
47: #include "cipher.h"
1.57 markus 48:
1.64 markus 49: extern const EVP_CIPHER *evp_ssh1_bf(void);
50: extern const EVP_CIPHER *evp_ssh1_3des(void);
51: extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
1.1 deraadt 52:
1.51 markus 53: struct Cipher {
54: char *name;
55: int number; /* for ssh1 only */
56: u_int block_size;
57: u_int key_len;
1.85 markus 58: u_int iv_len; /* defaults to block_size */
59: u_int auth_len;
1.74 djm 60: u_int discard_len;
1.82 markus 61: u_int cbc_mode;
1.56 markus 62: const EVP_CIPHER *(*evptype)(void);
1.52 markus 63: } ciphers[] = {
1.85 markus 64: { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
65: { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
66: { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
67: { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
68:
69: { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
70: { "blowfish-cbc",
71: SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
72: { "cast128-cbc",
73: SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
74: { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
75: { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
76: { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
77: { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
78: { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
79: { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
1.57 markus 80: { "rijndael-cbc@lysator.liu.se",
1.85 markus 81: SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
82: { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
83: { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
84: { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
85: { "aes128-gcm@openssh.com",
86: SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
87: { "aes256-gcm@openssh.com",
88: SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
1.47 markus 89:
1.85 markus 90: { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
1.32 markus 91: };
92:
93: /*--*/
1.1 deraadt 94:
1.54 markus 95: u_int
1.66 jakob 96: cipher_blocksize(const Cipher *c)
1.51 markus 97: {
98: return (c->block_size);
99: }
1.60 deraadt 100:
1.54 markus 101: u_int
1.66 jakob 102: cipher_keylen(const Cipher *c)
1.51 markus 103: {
104: return (c->key_len);
105: }
1.60 deraadt 106:
1.54 markus 107: u_int
1.85 markus 108: cipher_authlen(const Cipher *c)
109: {
110: return (c->auth_len);
111: }
112:
113: u_int
114: cipher_ivlen(const Cipher *c)
115: {
116: return (c->iv_len ? c->iv_len : c->block_size);
117: }
118:
119: u_int
1.66 jakob 120: cipher_get_number(const Cipher *c)
1.53 markus 121: {
122: return (c->number);
1.82 markus 123: }
124:
125: u_int
126: cipher_is_cbc(const Cipher *c)
127: {
128: return (c->cbc_mode);
1.53 markus 129: }
1.51 markus 130:
1.41 markus 131: u_int
1.34 markus 132: cipher_mask_ssh1(int client)
1.1 deraadt 133: {
1.41 markus 134: u_int mask = 0;
1.48 deraadt 135: mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
1.34 markus 136: mask |= 1 << SSH_CIPHER_BLOWFISH;
137: if (client) {
138: mask |= 1 << SSH_CIPHER_DES;
1.32 markus 139: }
140: return mask;
1.1 deraadt 141: }
142:
1.32 markus 143: Cipher *
144: cipher_by_name(const char *name)
145: {
146: Cipher *c;
147: for (c = ciphers; c->name != NULL; c++)
1.73 djm 148: if (strcmp(c->name, name) == 0)
1.32 markus 149: return c;
150: return NULL;
151: }
152:
153: Cipher *
154: cipher_by_number(int id)
155: {
156: Cipher *c;
157: for (c = ciphers; c->name != NULL; c++)
158: if (c->number == id)
159: return c;
160: return NULL;
161: }
1.24 markus 162:
163: #define CIPHER_SEP ","
164: int
165: ciphers_valid(const char *names)
166: {
1.32 markus 167: Cipher *c;
1.69 avsm 168: char *cipher_list, *cp;
1.24 markus 169: char *p;
170:
1.27 markus 171: if (names == NULL || strcmp(names, "") == 0)
1.24 markus 172: return 0;
1.69 avsm 173: cipher_list = cp = xstrdup(names);
1.32 markus 174: for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48 deraadt 175: (p = strsep(&cp, CIPHER_SEP))) {
1.32 markus 176: c = cipher_by_name(p);
177: if (c == NULL || c->number != SSH_CIPHER_SSH2) {
178: debug("bad cipher %s [%s]", p, names);
1.69 avsm 179: xfree(cipher_list);
1.24 markus 180: return 0;
1.32 markus 181: } else {
1.36 markus 182: debug3("cipher ok: %s [%s]", p, names);
1.24 markus 183: }
184: }
1.36 markus 185: debug3("ciphers ok: [%s]", names);
1.69 avsm 186: xfree(cipher_list);
1.24 markus 187: return 1;
188: }
189:
1.18 markus 190: /*
191: * Parses the name of the cipher. Returns the number of the corresponding
192: * cipher, or -1 on error.
193: */
1.1 deraadt 194:
1.4 provos 195: int
196: cipher_number(const char *name)
1.1 deraadt 197: {
1.32 markus 198: Cipher *c;
1.27 markus 199: if (name == NULL)
200: return -1;
1.73 djm 201: for (c = ciphers; c->name != NULL; c++)
202: if (strcasecmp(c->name, name) == 0)
203: return c->number;
204: return -1;
1.1 deraadt 205: }
206:
1.32 markus 207: char *
208: cipher_name(int id)
1.1 deraadt 209: {
1.32 markus 210: Cipher *c = cipher_by_number(id);
211: return (c==NULL) ? "<unknown>" : c->name;
1.1 deraadt 212: }
213:
1.26 markus 214: void
1.52 markus 215: cipher_init(CipherContext *cc, Cipher *cipher,
216: const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69 avsm 217: int do_encrypt)
1.16 markus 218: {
1.52 markus 219: static int dowarn = 1;
220: const EVP_CIPHER *type;
221: int klen;
1.74 djm 222: u_char *junk, *discard;
1.52 markus 223:
224: if (cipher->number == SSH_CIPHER_DES) {
225: if (dowarn) {
226: error("Warning: use of DES is strongly discouraged "
227: "due to cryptographic weaknesses");
228: dowarn = 0;
229: }
230: if (keylen > 8)
231: keylen = 8;
232: }
233: cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
1.85 markus 234: cc->encrypt = do_encrypt;
1.52 markus 235:
1.32 markus 236: if (keylen < cipher->key_len)
237: fatal("cipher_init: key length %d is insufficient for %s.",
238: keylen, cipher->name);
1.85 markus 239: if (iv != NULL && ivlen < cipher_ivlen(cipher))
1.32 markus 240: fatal("cipher_init: iv length %d is insufficient for %s.",
241: ivlen, cipher->name);
242: cc->cipher = cipher;
1.52 markus 243:
244: type = (*cipher->evptype)();
245:
246: EVP_CIPHER_CTX_init(&cc->evp);
247: if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
1.69 avsm 248: (do_encrypt == CIPHER_ENCRYPT)) == 0)
1.52 markus 249: fatal("cipher_init: EVP_CipherInit failed for %s",
250: cipher->name);
1.85 markus 251: if (cipher_authlen(cipher) &&
252: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
253: -1, (u_char *)iv))
254: fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED failed for %s",
255: cipher->name);
1.52 markus 256: klen = EVP_CIPHER_CTX_key_length(&cc->evp);
1.76 djm 257: if (klen > 0 && keylen != (u_int)klen) {
1.62 markus 258: debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
1.52 markus 259: if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
260: fatal("cipher_init: set keylen failed (%d -> %d)",
261: klen, keylen);
262: }
263: if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
264: fatal("cipher_init: EVP_CipherInit: set key failed for %s",
265: cipher->name);
1.74 djm 266:
1.77 djm 267: if (cipher->discard_len > 0) {
1.74 djm 268: junk = xmalloc(cipher->discard_len);
269: discard = xmalloc(cipher->discard_len);
270: if (EVP_Cipher(&cc->evp, discard, junk,
271: cipher->discard_len) == 0)
272: fatal("evp_crypt: EVP_Cipher failed during discard");
273: memset(discard, 0, cipher->discard_len);
274: xfree(junk);
275: xfree(discard);
276: }
1.1 deraadt 277: }
1.21 markus 278:
1.83 markus 279: /*
280: * cipher_crypt() operates as following:
281: * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
282: * Theses bytes are treated as additional authenticated data for
283: * authenticated encryption modes.
284: * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85 markus 285: * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
286: * This tag is written on encryption and verified on decryption.
1.83 markus 287: * Both 'aadlen' and 'authlen' can be set to 0.
288: */
1.26 markus 289: void
1.83 markus 290: cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src,
1.85 markus 291: u_int len, u_int aadlen, u_int authlen)
1.32 markus 292: {
1.85 markus 293: if (authlen) {
294: u_char lastiv[1];
295:
296: if (authlen != cipher_authlen(cc->cipher))
297: fatal("%s: authlen mismatch %d", __func__, authlen);
298: /* increment IV */
299: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
300: 1, lastiv))
301: fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
302: /* set tag on decyption */
303: if (!cc->encrypt &&
304: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
305: authlen, (u_char *)src + aadlen + len))
306: fatal("%s: EVP_CTRL_GCM_SET_TAG", __func__);
307: }
308: if (aadlen) {
309: if (authlen &&
310: EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
311: fatal("%s: EVP_Cipher(aad) failed", __func__);
1.83 markus 312: memcpy(dest, src, aadlen);
1.85 markus 313: }
1.32 markus 314: if (len % cc->cipher->block_size)
1.83 markus 315: fatal("%s: bad plaintext length %d", __func__, len);
316: if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
317: len) < 0)
318: fatal("%s: EVP_Cipher failed", __func__);
1.85 markus 319: if (authlen) {
320: /* compute tag (on encrypt) or verify tag (on decrypt) */
1.86 djm 321: if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) {
322: if (cc->encrypt)
323: fatal("%s: EVP_Cipher(final) failed", __func__);
324: else
325: fatal("Decryption integrity check failed");
326: }
1.85 markus 327: if (cc->encrypt &&
328: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
329: authlen, dest + aadlen + len))
330: fatal("%s: EVP_CTRL_GCM_GET_TAG", __func__);
331: }
1.21 markus 332: }
333:
1.26 markus 334: void
1.51 markus 335: cipher_cleanup(CipherContext *cc)
1.16 markus 336: {
1.52 markus 337: if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
338: error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
1.16 markus 339: }
1.1 deraadt 340:
1.32 markus 341: /*
342: * Selects the cipher, and keys if by computing the MD5 checksum of the
343: * passphrase and using the resulting 16 bytes as the key.
344: */
1.1 deraadt 345:
1.26 markus 346: void
1.32 markus 347: cipher_set_key_string(CipherContext *cc, Cipher *cipher,
1.69 avsm 348: const char *passphrase, int do_encrypt)
1.16 markus 349: {
1.32 markus 350: MD5_CTX md;
1.41 markus 351: u_char digest[16];
1.32 markus 352:
353: MD5_Init(&md);
354: MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
355: MD5_Final(digest, &md);
1.16 markus 356:
1.69 avsm 357: cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
1.16 markus 358:
1.32 markus 359: memset(digest, 0, sizeof(digest));
360: memset(&md, 0, sizeof(md));
1.52 markus 361: }
1.53 markus 362:
1.54 markus 363: /*
1.53 markus 364: * Exports an IV from the CipherContext required to export the key
365: * state back from the unprivileged child to the privileged parent
366: * process.
367: */
368:
369: int
1.66 jakob 370: cipher_get_keyiv_len(const CipherContext *cc)
1.53 markus 371: {
372: Cipher *c = cc->cipher;
373: int ivlen;
374:
375: if (c->number == SSH_CIPHER_3DES)
376: ivlen = 24;
377: else
378: ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
379: return (ivlen);
380: }
381:
382: void
383: cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
384: {
385: Cipher *c = cc->cipher;
386: int evplen;
387:
388: switch (c->number) {
389: case SSH_CIPHER_SSH2:
390: case SSH_CIPHER_DES:
391: case SSH_CIPHER_BLOWFISH:
392: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.76 djm 393: if (evplen <= 0)
1.53 markus 394: return;
1.76 djm 395: if ((u_int)evplen != len)
1.58 markus 396: fatal("%s: wrong iv length %d != %d", __func__,
1.53 markus 397: evplen, len);
1.85 markus 398: if (cipher_authlen(c)) {
399: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
400: len, iv))
401: fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
402: } else
403: memcpy(iv, cc->evp.iv, len);
1.63 markus 404: break;
405: case SSH_CIPHER_3DES:
406: ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.53 markus 407: break;
408: default:
1.58 markus 409: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 410: }
411: }
412:
413: void
414: cipher_set_keyiv(CipherContext *cc, u_char *iv)
415: {
416: Cipher *c = cc->cipher;
417: int evplen = 0;
418:
419: switch (c->number) {
420: case SSH_CIPHER_SSH2:
421: case SSH_CIPHER_DES:
422: case SSH_CIPHER_BLOWFISH:
423: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
424: if (evplen == 0)
425: return;
1.85 markus 426: if (cipher_authlen(c)) {
427: if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
428: EVP_CTRL_GCM_SET_IV_FIXED, -1, iv))
429: fatal("%s: EVP_CTRL_GCM_SET_IV_FIXED failed",
430: __func__);
431: } else
432: memcpy(cc->evp.iv, iv, evplen);
1.63 markus 433: break;
434: case SSH_CIPHER_3DES:
435: ssh1_3des_iv(&cc->evp, 1, iv, 24);
1.53 markus 436: break;
437: default:
1.58 markus 438: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 439: }
440: }
441:
442: #define EVP_X_STATE(evp) (evp).cipher_data
443: #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
444:
445: int
1.66 jakob 446: cipher_get_keycontext(const CipherContext *cc, u_char *dat)
1.53 markus 447: {
448: Cipher *c = cc->cipher;
1.59 markus 449: int plen = 0;
1.53 markus 450:
1.87 ! djm 451: if (c->evptype == EVP_rc4) {
1.59 markus 452: plen = EVP_X_STATE_LEN(cc->evp);
1.53 markus 453: if (dat == NULL)
1.59 markus 454: return (plen);
455: memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53 markus 456: }
457: return (plen);
458: }
459:
460: void
461: cipher_set_keycontext(CipherContext *cc, u_char *dat)
462: {
463: Cipher *c = cc->cipher;
464: int plen;
465:
1.87 ! djm 466: if (c->evptype == EVP_rc4) {
1.53 markus 467: plen = EVP_X_STATE_LEN(cc->evp);
468: memcpy(EVP_X_STATE(cc->evp), dat, plen);
469: }
1.1 deraadt 470: }