Annotation of src/usr.bin/ssh/cipher.c, Revision 1.85
1.85 ! markus 1: /* $OpenBSD: cipher.c,v 1.84 2012/12/12 16:46:10 naddy 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: { "acss@openssh.org",
! 86: SSH_CIPHER_SSH2, 16, 5, 0, 0, 0, 0, EVP_acss },
! 87: { "aes128-gcm@openssh.com",
! 88: SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
! 89: { "aes256-gcm@openssh.com",
! 90: SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
1.47 markus 91:
1.85 ! markus 92: { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
1.32 markus 93: };
94:
95: /*--*/
1.1 deraadt 96:
1.54 markus 97: u_int
1.66 jakob 98: cipher_blocksize(const Cipher *c)
1.51 markus 99: {
100: return (c->block_size);
101: }
1.60 deraadt 102:
1.54 markus 103: u_int
1.66 jakob 104: cipher_keylen(const Cipher *c)
1.51 markus 105: {
106: return (c->key_len);
107: }
1.60 deraadt 108:
1.54 markus 109: u_int
1.85 ! markus 110: cipher_authlen(const Cipher *c)
! 111: {
! 112: return (c->auth_len);
! 113: }
! 114:
! 115: u_int
! 116: cipher_ivlen(const Cipher *c)
! 117: {
! 118: return (c->iv_len ? c->iv_len : c->block_size);
! 119: }
! 120:
! 121: u_int
1.66 jakob 122: cipher_get_number(const Cipher *c)
1.53 markus 123: {
124: return (c->number);
1.82 markus 125: }
126:
127: u_int
128: cipher_is_cbc(const Cipher *c)
129: {
130: return (c->cbc_mode);
1.53 markus 131: }
1.51 markus 132:
1.41 markus 133: u_int
1.34 markus 134: cipher_mask_ssh1(int client)
1.1 deraadt 135: {
1.41 markus 136: u_int mask = 0;
1.48 deraadt 137: mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
1.34 markus 138: mask |= 1 << SSH_CIPHER_BLOWFISH;
139: if (client) {
140: mask |= 1 << SSH_CIPHER_DES;
1.32 markus 141: }
142: return mask;
1.1 deraadt 143: }
144:
1.32 markus 145: Cipher *
146: cipher_by_name(const char *name)
147: {
148: Cipher *c;
149: for (c = ciphers; c->name != NULL; c++)
1.73 djm 150: if (strcmp(c->name, name) == 0)
1.32 markus 151: return c;
152: return NULL;
153: }
154:
155: Cipher *
156: cipher_by_number(int id)
157: {
158: Cipher *c;
159: for (c = ciphers; c->name != NULL; c++)
160: if (c->number == id)
161: return c;
162: return NULL;
163: }
1.24 markus 164:
165: #define CIPHER_SEP ","
166: int
167: ciphers_valid(const char *names)
168: {
1.32 markus 169: Cipher *c;
1.69 avsm 170: char *cipher_list, *cp;
1.24 markus 171: char *p;
172:
1.27 markus 173: if (names == NULL || strcmp(names, "") == 0)
1.24 markus 174: return 0;
1.69 avsm 175: cipher_list = cp = xstrdup(names);
1.32 markus 176: for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48 deraadt 177: (p = strsep(&cp, CIPHER_SEP))) {
1.32 markus 178: c = cipher_by_name(p);
179: if (c == NULL || c->number != SSH_CIPHER_SSH2) {
180: debug("bad cipher %s [%s]", p, names);
1.69 avsm 181: xfree(cipher_list);
1.24 markus 182: return 0;
1.32 markus 183: } else {
1.36 markus 184: debug3("cipher ok: %s [%s]", p, names);
1.24 markus 185: }
186: }
1.36 markus 187: debug3("ciphers ok: [%s]", names);
1.69 avsm 188: xfree(cipher_list);
1.24 markus 189: return 1;
190: }
191:
1.18 markus 192: /*
193: * Parses the name of the cipher. Returns the number of the corresponding
194: * cipher, or -1 on error.
195: */
1.1 deraadt 196:
1.4 provos 197: int
198: cipher_number(const char *name)
1.1 deraadt 199: {
1.32 markus 200: Cipher *c;
1.27 markus 201: if (name == NULL)
202: return -1;
1.73 djm 203: for (c = ciphers; c->name != NULL; c++)
204: if (strcasecmp(c->name, name) == 0)
205: return c->number;
206: return -1;
1.1 deraadt 207: }
208:
1.32 markus 209: char *
210: cipher_name(int id)
1.1 deraadt 211: {
1.32 markus 212: Cipher *c = cipher_by_number(id);
213: return (c==NULL) ? "<unknown>" : c->name;
1.1 deraadt 214: }
215:
1.26 markus 216: void
1.52 markus 217: cipher_init(CipherContext *cc, Cipher *cipher,
218: const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69 avsm 219: int do_encrypt)
1.16 markus 220: {
1.52 markus 221: static int dowarn = 1;
222: const EVP_CIPHER *type;
223: int klen;
1.74 djm 224: u_char *junk, *discard;
1.52 markus 225:
226: if (cipher->number == SSH_CIPHER_DES) {
227: if (dowarn) {
228: error("Warning: use of DES is strongly discouraged "
229: "due to cryptographic weaknesses");
230: dowarn = 0;
231: }
232: if (keylen > 8)
233: keylen = 8;
234: }
235: cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
1.85 ! markus 236: cc->encrypt = do_encrypt;
1.52 markus 237:
1.32 markus 238: if (keylen < cipher->key_len)
239: fatal("cipher_init: key length %d is insufficient for %s.",
240: keylen, cipher->name);
1.85 ! markus 241: if (iv != NULL && ivlen < cipher_ivlen(cipher))
1.32 markus 242: fatal("cipher_init: iv length %d is insufficient for %s.",
243: ivlen, cipher->name);
244: cc->cipher = cipher;
1.52 markus 245:
246: type = (*cipher->evptype)();
247:
248: EVP_CIPHER_CTX_init(&cc->evp);
249: if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
1.69 avsm 250: (do_encrypt == CIPHER_ENCRYPT)) == 0)
1.52 markus 251: fatal("cipher_init: EVP_CipherInit failed for %s",
252: cipher->name);
1.85 ! markus 253: if (cipher_authlen(cipher) &&
! 254: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
! 255: -1, (u_char *)iv))
! 256: fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED failed for %s",
! 257: cipher->name);
1.52 markus 258: klen = EVP_CIPHER_CTX_key_length(&cc->evp);
1.76 djm 259: if (klen > 0 && keylen != (u_int)klen) {
1.62 markus 260: debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
1.52 markus 261: if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
262: fatal("cipher_init: set keylen failed (%d -> %d)",
263: klen, keylen);
264: }
265: if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
266: fatal("cipher_init: EVP_CipherInit: set key failed for %s",
267: cipher->name);
1.74 djm 268:
1.77 djm 269: if (cipher->discard_len > 0) {
1.74 djm 270: junk = xmalloc(cipher->discard_len);
271: discard = xmalloc(cipher->discard_len);
272: if (EVP_Cipher(&cc->evp, discard, junk,
273: cipher->discard_len) == 0)
274: fatal("evp_crypt: EVP_Cipher failed during discard");
275: memset(discard, 0, cipher->discard_len);
276: xfree(junk);
277: xfree(discard);
278: }
1.1 deraadt 279: }
1.21 markus 280:
1.83 markus 281: /*
282: * cipher_crypt() operates as following:
283: * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
284: * Theses bytes are treated as additional authenticated data for
285: * authenticated encryption modes.
286: * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85 ! markus 287: * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
! 288: * This tag is written on encryption and verified on decryption.
1.83 markus 289: * Both 'aadlen' and 'authlen' can be set to 0.
290: */
1.26 markus 291: void
1.83 markus 292: cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src,
1.85 ! markus 293: u_int len, u_int aadlen, u_int authlen)
1.32 markus 294: {
1.85 ! markus 295: if (authlen) {
! 296: u_char lastiv[1];
! 297:
! 298: if (authlen != cipher_authlen(cc->cipher))
! 299: fatal("%s: authlen mismatch %d", __func__, authlen);
! 300: /* increment IV */
! 301: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
! 302: 1, lastiv))
! 303: fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
! 304: /* set tag on decyption */
! 305: if (!cc->encrypt &&
! 306: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
! 307: authlen, (u_char *)src + aadlen + len))
! 308: fatal("%s: EVP_CTRL_GCM_SET_TAG", __func__);
! 309: }
! 310: if (aadlen) {
! 311: if (authlen &&
! 312: EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
! 313: fatal("%s: EVP_Cipher(aad) failed", __func__);
1.83 markus 314: memcpy(dest, src, aadlen);
1.85 ! markus 315: }
1.32 markus 316: if (len % cc->cipher->block_size)
1.83 markus 317: fatal("%s: bad plaintext length %d", __func__, len);
318: if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
319: len) < 0)
320: fatal("%s: EVP_Cipher failed", __func__);
1.85 ! markus 321: if (authlen) {
! 322: /* compute tag (on encrypt) or verify tag (on decrypt) */
! 323: if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0)
! 324: fatal("%s: EVP_Cipher(finish) failed", __func__);
! 325: if (cc->encrypt &&
! 326: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
! 327: authlen, dest + aadlen + len))
! 328: fatal("%s: EVP_CTRL_GCM_GET_TAG", __func__);
! 329: }
1.21 markus 330: }
331:
1.26 markus 332: void
1.51 markus 333: cipher_cleanup(CipherContext *cc)
1.16 markus 334: {
1.52 markus 335: if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
336: error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
1.16 markus 337: }
1.1 deraadt 338:
1.32 markus 339: /*
340: * Selects the cipher, and keys if by computing the MD5 checksum of the
341: * passphrase and using the resulting 16 bytes as the key.
342: */
1.1 deraadt 343:
1.26 markus 344: void
1.32 markus 345: cipher_set_key_string(CipherContext *cc, Cipher *cipher,
1.69 avsm 346: const char *passphrase, int do_encrypt)
1.16 markus 347: {
1.32 markus 348: MD5_CTX md;
1.41 markus 349: u_char digest[16];
1.32 markus 350:
351: MD5_Init(&md);
352: MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
353: MD5_Final(digest, &md);
1.16 markus 354:
1.69 avsm 355: cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
1.16 markus 356:
1.32 markus 357: memset(digest, 0, sizeof(digest));
358: memset(&md, 0, sizeof(md));
1.52 markus 359: }
1.53 markus 360:
1.54 markus 361: /*
1.53 markus 362: * Exports an IV from the CipherContext required to export the key
363: * state back from the unprivileged child to the privileged parent
364: * process.
365: */
366:
367: int
1.66 jakob 368: cipher_get_keyiv_len(const CipherContext *cc)
1.53 markus 369: {
370: Cipher *c = cc->cipher;
371: int ivlen;
372:
373: if (c->number == SSH_CIPHER_3DES)
374: ivlen = 24;
375: else
376: ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
377: return (ivlen);
378: }
379:
380: void
381: cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
382: {
383: Cipher *c = cc->cipher;
384: int evplen;
385:
386: switch (c->number) {
387: case SSH_CIPHER_SSH2:
388: case SSH_CIPHER_DES:
389: case SSH_CIPHER_BLOWFISH:
390: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.76 djm 391: if (evplen <= 0)
1.53 markus 392: return;
1.76 djm 393: if ((u_int)evplen != len)
1.58 markus 394: fatal("%s: wrong iv length %d != %d", __func__,
1.53 markus 395: evplen, len);
1.85 ! markus 396: if (cipher_authlen(c)) {
! 397: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
! 398: len, iv))
! 399: fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
! 400: } else
! 401: memcpy(iv, cc->evp.iv, len);
1.63 markus 402: break;
403: case SSH_CIPHER_3DES:
404: ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.53 markus 405: break;
406: default:
1.58 markus 407: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 408: }
409: }
410:
411: void
412: cipher_set_keyiv(CipherContext *cc, u_char *iv)
413: {
414: Cipher *c = cc->cipher;
415: int evplen = 0;
416:
417: switch (c->number) {
418: case SSH_CIPHER_SSH2:
419: case SSH_CIPHER_DES:
420: case SSH_CIPHER_BLOWFISH:
421: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
422: if (evplen == 0)
423: return;
1.85 ! markus 424: if (cipher_authlen(c)) {
! 425: if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
! 426: EVP_CTRL_GCM_SET_IV_FIXED, -1, iv))
! 427: fatal("%s: EVP_CTRL_GCM_SET_IV_FIXED failed",
! 428: __func__);
! 429: } else
! 430: memcpy(cc->evp.iv, iv, evplen);
1.63 markus 431: break;
432: case SSH_CIPHER_3DES:
433: ssh1_3des_iv(&cc->evp, 1, iv, 24);
1.53 markus 434: break;
435: default:
1.58 markus 436: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 437: }
438: }
439:
440: #define EVP_X_STATE(evp) (evp).cipher_data
441: #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
442:
443: int
1.66 jakob 444: cipher_get_keycontext(const CipherContext *cc, u_char *dat)
1.53 markus 445: {
446: Cipher *c = cc->cipher;
1.59 markus 447: int plen = 0;
1.53 markus 448:
1.67 hshoexer 449: if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
1.59 markus 450: plen = EVP_X_STATE_LEN(cc->evp);
1.53 markus 451: if (dat == NULL)
1.59 markus 452: return (plen);
453: memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53 markus 454: }
455: return (plen);
456: }
457:
458: void
459: cipher_set_keycontext(CipherContext *cc, u_char *dat)
460: {
461: Cipher *c = cc->cipher;
462: int plen;
463:
1.67 hshoexer 464: if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
1.53 markus 465: plen = EVP_X_STATE_LEN(cc->evp);
466: memcpy(EVP_X_STATE(cc->evp), dat, plen);
467: }
1.1 deraadt 468: }