Annotation of src/usr.bin/ssh/cipher.c, Revision 1.103
1.103 ! djm 1: /* $OpenBSD: cipher.c,v 1.102 2016/08/03 05:41:57 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 <string.h>
1.81 deraadt 41: #include <stdarg.h>
1.91 djm 42: #include <stdio.h>
1.80 stevesk 43:
1.99 djm 44: #include "cipher.h"
1.91 djm 45: #include "misc.h"
1.99 djm 46: #include "sshbuf.h"
47: #include "ssherr.h"
1.95 markus 48: #include "digest.h"
1.57 markus 49:
1.1 deraadt 50:
1.102 djm 51: struct sshcipher_ctx {
52: int plaintext;
53: int encrypt;
54: EVP_CIPHER_CTX *evp;
55: struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
56: struct aesctr_ctx ac_ctx; /* XXX union with evp? */
57: const struct sshcipher *cipher;
58: };
59:
1.99 djm 60: struct sshcipher {
1.51 markus 61: char *name;
62: int number; /* for ssh1 only */
63: u_int block_size;
64: u_int key_len;
1.85 markus 65: u_int iv_len; /* defaults to block_size */
66: u_int auth_len;
1.74 djm 67: u_int discard_len;
1.91 djm 68: u_int flags;
69: #define CFLAG_CBC (1<<0)
70: #define CFLAG_CHACHAPOLY (1<<1)
1.98 markus 71: #define CFLAG_AESCTR (1<<2)
72: #define CFLAG_NONE (1<<3)
73: #ifdef WITH_OPENSSL
1.56 markus 74: const EVP_CIPHER *(*evptype)(void);
1.98 markus 75: #else
76: void *ignored;
77: #endif
1.88 djm 78: };
79:
1.99 djm 80: static const struct sshcipher ciphers[] = {
1.98 markus 81: #ifdef WITH_OPENSSL
82: { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
1.85 markus 83: { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
84: { "blowfish-cbc",
85: SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
86: { "cast128-cbc",
87: SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
88: { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
89: { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
90: { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
91: { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
92: { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
93: { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
1.57 markus 94: { "rijndael-cbc@lysator.liu.se",
1.85 markus 95: SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
96: { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
97: { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
98: { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
99: { "aes128-gcm@openssh.com",
100: SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
101: { "aes256-gcm@openssh.com",
102: SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
1.98 markus 103: #else
104: { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
105: { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
106: { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
107: { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
108: #endif
1.91 djm 109: { "chacha20-poly1305@openssh.com",
110: SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
1.47 markus 111:
1.85 markus 112: { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
1.32 markus 113: };
114:
115: /*--*/
1.1 deraadt 116:
1.99 djm 117: /* Returns a comma-separated list of supported ciphers. */
1.88 djm 118: char *
1.91 djm 119: cipher_alg_list(char sep, int auth_only)
1.88 djm 120: {
1.99 djm 121: char *tmp, *ret = NULL;
1.88 djm 122: size_t nlen, rlen = 0;
1.99 djm 123: const struct sshcipher *c;
1.88 djm 124:
125: for (c = ciphers; c->name != NULL; c++) {
126: if (c->number != SSH_CIPHER_SSH2)
127: continue;
1.91 djm 128: if (auth_only && c->auth_len == 0)
129: continue;
1.88 djm 130: if (ret != NULL)
1.90 dtucker 131: ret[rlen++] = sep;
1.88 djm 132: nlen = strlen(c->name);
1.99 djm 133: if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
134: free(ret);
135: return NULL;
136: }
137: ret = tmp;
1.88 djm 138: memcpy(ret + rlen, c->name, nlen + 1);
139: rlen += nlen;
140: }
141: return ret;
142: }
143:
1.54 markus 144: u_int
1.99 djm 145: cipher_blocksize(const struct sshcipher *c)
1.51 markus 146: {
147: return (c->block_size);
148: }
1.60 deraadt 149:
1.54 markus 150: u_int
1.99 djm 151: cipher_keylen(const struct sshcipher *c)
1.51 markus 152: {
153: return (c->key_len);
1.94 dtucker 154: }
155:
156: u_int
1.99 djm 157: cipher_seclen(const struct sshcipher *c)
1.94 dtucker 158: {
159: if (strcmp("3des-cbc", c->name) == 0)
160: return 14;
161: return cipher_keylen(c);
1.51 markus 162: }
1.60 deraadt 163:
1.54 markus 164: u_int
1.99 djm 165: cipher_authlen(const struct sshcipher *c)
1.85 markus 166: {
167: return (c->auth_len);
168: }
169:
170: u_int
1.99 djm 171: cipher_ivlen(const struct sshcipher *c)
1.85 markus 172: {
1.91 djm 173: /*
174: * Default is cipher block size, except for chacha20+poly1305 that
175: * needs no IV. XXX make iv_len == -1 default?
176: */
177: return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
178: c->iv_len : c->block_size;
1.85 markus 179: }
180:
181: u_int
1.99 djm 182: cipher_get_number(const struct sshcipher *c)
1.53 markus 183: {
184: return (c->number);
1.82 markus 185: }
186:
187: u_int
1.99 djm 188: cipher_is_cbc(const struct sshcipher *c)
1.82 markus 189: {
1.91 djm 190: return (c->flags & CFLAG_CBC) != 0;
1.53 markus 191: }
1.51 markus 192:
1.41 markus 193: u_int
1.102 djm 194: cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
195: {
196: return cc->plaintext;
197: }
198:
199: u_int
200: cipher_ctx_get_number(struct sshcipher_ctx *cc)
201: {
202: return cc->cipher->number;
203: }
204:
205: u_int
1.34 markus 206: cipher_mask_ssh1(int client)
1.1 deraadt 207: {
1.41 markus 208: u_int mask = 0;
1.48 deraadt 209: mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
1.34 markus 210: mask |= 1 << SSH_CIPHER_BLOWFISH;
211: if (client) {
212: mask |= 1 << SSH_CIPHER_DES;
1.32 markus 213: }
214: return mask;
1.1 deraadt 215: }
216:
1.99 djm 217: const struct sshcipher *
1.32 markus 218: cipher_by_name(const char *name)
219: {
1.99 djm 220: const struct sshcipher *c;
1.32 markus 221: for (c = ciphers; c->name != NULL; c++)
1.73 djm 222: if (strcmp(c->name, name) == 0)
1.32 markus 223: return c;
224: return NULL;
225: }
226:
1.99 djm 227: const struct sshcipher *
1.32 markus 228: cipher_by_number(int id)
229: {
1.99 djm 230: const struct sshcipher *c;
1.32 markus 231: for (c = ciphers; c->name != NULL; c++)
232: if (c->number == id)
233: return c;
234: return NULL;
235: }
1.24 markus 236:
237: #define CIPHER_SEP ","
238: int
239: ciphers_valid(const char *names)
240: {
1.99 djm 241: const struct sshcipher *c;
1.69 avsm 242: char *cipher_list, *cp;
1.24 markus 243: char *p;
244:
1.27 markus 245: if (names == NULL || strcmp(names, "") == 0)
1.24 markus 246: return 0;
1.99 djm 247: if ((cipher_list = cp = strdup(names)) == NULL)
248: return 0;
1.32 markus 249: for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48 deraadt 250: (p = strsep(&cp, CIPHER_SEP))) {
1.32 markus 251: c = cipher_by_name(p);
252: if (c == NULL || c->number != SSH_CIPHER_SSH2) {
1.89 djm 253: free(cipher_list);
1.24 markus 254: return 0;
255: }
256: }
1.89 djm 257: free(cipher_list);
1.24 markus 258: return 1;
259: }
260:
1.18 markus 261: /*
262: * Parses the name of the cipher. Returns the number of the corresponding
263: * cipher, or -1 on error.
264: */
1.1 deraadt 265:
1.4 provos 266: int
267: cipher_number(const char *name)
1.1 deraadt 268: {
1.99 djm 269: const struct sshcipher *c;
1.27 markus 270: if (name == NULL)
271: return -1;
1.73 djm 272: for (c = ciphers; c->name != NULL; c++)
273: if (strcasecmp(c->name, name) == 0)
274: return c->number;
275: return -1;
1.1 deraadt 276: }
277:
1.32 markus 278: char *
279: cipher_name(int id)
1.1 deraadt 280: {
1.99 djm 281: const struct sshcipher *c = cipher_by_number(id);
1.32 markus 282: return (c==NULL) ? "<unknown>" : c->name;
1.1 deraadt 283: }
284:
1.99 djm 285: const char *
286: cipher_warning_message(const struct sshcipher_ctx *cc)
287: {
288: if (cc == NULL || cc->cipher == NULL)
289: return NULL;
290: if (cc->cipher->number == SSH_CIPHER_DES)
291: return "use of DES is strongly discouraged due to "
292: "cryptographic weaknesses";
293: return NULL;
294: }
295:
296: int
1.102 djm 297: cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
1.52 markus 298: const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.69 avsm 299: int do_encrypt)
1.16 markus 300: {
1.102 djm 301: struct sshcipher_ctx *cc = NULL;
302: int ret = SSH_ERR_INTERNAL_ERROR;
1.98 markus 303: #ifdef WITH_OPENSSL
1.52 markus 304: const EVP_CIPHER *type;
305: int klen;
1.74 djm 306: u_char *junk, *discard;
1.102 djm 307: #endif
308:
309: *ccp = NULL;
310: if ((cc = calloc(sizeof(*cc), 1)) == NULL)
311: return SSH_ERR_ALLOC_FAIL;
1.52 markus 312:
313: if (cipher->number == SSH_CIPHER_DES) {
314: if (keylen > 8)
315: keylen = 8;
316: }
1.102 djm 317:
1.52 markus 318: cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
1.85 markus 319: cc->encrypt = do_encrypt;
1.52 markus 320:
1.99 djm 321: if (keylen < cipher->key_len ||
1.102 djm 322: (iv != NULL && ivlen < cipher_ivlen(cipher))) {
323: ret = SSH_ERR_INVALID_ARGUMENT;
324: goto out;
325: }
1.99 djm 326:
1.32 markus 327: cc->cipher = cipher;
1.91 djm 328: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
1.102 djm 329: ret = chachapoly_init(&cc->cp_ctx, key, keylen);
330: goto out;
1.91 djm 331: }
1.98 markus 332: #ifndef WITH_OPENSSL
333: if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
334: aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
335: aesctr_ivsetup(&cc->ac_ctx, iv);
1.102 djm 336: ret = 0;
337: goto out;
338: }
339: if ((cc->cipher->flags & CFLAG_NONE) != 0) {
340: ret = 0;
341: goto out;
1.98 markus 342: }
1.102 djm 343: ret = SSH_ERR_INVALID_ARGUMENT;
344: goto out;
345: #else /* WITH_OPENSSL */
1.52 markus 346: type = (*cipher->evptype)();
1.102 djm 347: if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
348: ret = SSH_ERR_ALLOC_FAIL;
349: goto out;
350: }
351: if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
1.99 djm 352: (do_encrypt == CIPHER_ENCRYPT)) == 0) {
353: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 354: goto out;
1.99 djm 355: }
1.85 markus 356: if (cipher_authlen(cipher) &&
1.102 djm 357: !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
1.99 djm 358: -1, (u_char *)iv)) {
359: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 360: goto out;
1.99 djm 361: }
1.102 djm 362: klen = EVP_CIPHER_CTX_key_length(cc->evp);
1.76 djm 363: if (klen > 0 && keylen != (u_int)klen) {
1.102 djm 364: if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
1.99 djm 365: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 366: goto out;
1.99 djm 367: }
368: }
1.102 djm 369: if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
1.99 djm 370: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 371: goto out;
1.99 djm 372: }
1.74 djm 373:
1.77 djm 374: if (cipher->discard_len > 0) {
1.99 djm 375: if ((junk = malloc(cipher->discard_len)) == NULL ||
376: (discard = malloc(cipher->discard_len)) == NULL) {
1.101 mmcc 377: free(junk);
1.99 djm 378: ret = SSH_ERR_ALLOC_FAIL;
1.102 djm 379: goto out;
1.99 djm 380: }
1.102 djm 381: ret = EVP_Cipher(cc->evp, discard, junk, cipher->discard_len);
1.96 djm 382: explicit_bzero(discard, cipher->discard_len);
1.89 djm 383: free(junk);
384: free(discard);
1.99 djm 385: if (ret != 1) {
386: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.102 djm 387: goto out;
388: }
389: }
390: ret = 0;
391: #endif /* WITH_OPENSSL */
392: out:
393: if (ret == 0) {
394: /* success */
395: *ccp = cc;
396: } else {
397: if (cc != NULL) {
398: #ifdef WITH_OPENSSL
399: if (cc->evp != NULL)
400: EVP_CIPHER_CTX_free(cc->evp);
401: #endif /* WITH_OPENSSL */
402: explicit_bzero(cc, sizeof(*cc));
403: free(cc);
1.99 djm 404: }
1.74 djm 405: }
1.102 djm 406: return ret;
1.1 deraadt 407: }
1.21 markus 408:
1.83 markus 409: /*
410: * cipher_crypt() operates as following:
411: * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
412: * Theses bytes are treated as additional authenticated data for
413: * authenticated encryption modes.
414: * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85 markus 415: * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
416: * This tag is written on encryption and verified on decryption.
1.83 markus 417: * Both 'aadlen' and 'authlen' can be set to 0.
418: */
1.93 markus 419: int
1.99 djm 420: cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
421: const u_char *src, u_int len, u_int aadlen, u_int authlen)
1.32 markus 422: {
1.99 djm 423: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
424: return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
425: len, aadlen, authlen, cc->encrypt);
426: }
1.98 markus 427: #ifndef WITH_OPENSSL
428: if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
429: if (aadlen)
430: memcpy(dest, src, aadlen);
431: aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
432: dest + aadlen, len);
433: return 0;
434: }
435: if ((cc->cipher->flags & CFLAG_NONE) != 0) {
436: memcpy(dest, src, aadlen + len);
437: return 0;
438: }
1.99 djm 439: return SSH_ERR_INVALID_ARGUMENT;
1.98 markus 440: #else
1.85 markus 441: if (authlen) {
442: u_char lastiv[1];
443:
444: if (authlen != cipher_authlen(cc->cipher))
1.99 djm 445: return SSH_ERR_INVALID_ARGUMENT;
1.85 markus 446: /* increment IV */
1.102 djm 447: if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
1.85 markus 448: 1, lastiv))
1.99 djm 449: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 450: /* set tag on decyption */
451: if (!cc->encrypt &&
1.102 djm 452: !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
1.85 markus 453: authlen, (u_char *)src + aadlen + len))
1.99 djm 454: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 455: }
456: if (aadlen) {
457: if (authlen &&
1.102 djm 458: EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
1.99 djm 459: return SSH_ERR_LIBCRYPTO_ERROR;
1.83 markus 460: memcpy(dest, src, aadlen);
1.85 markus 461: }
1.32 markus 462: if (len % cc->cipher->block_size)
1.99 djm 463: return SSH_ERR_INVALID_ARGUMENT;
1.102 djm 464: if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
1.83 markus 465: len) < 0)
1.99 djm 466: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 467: if (authlen) {
468: /* compute tag (on encrypt) or verify tag (on decrypt) */
1.102 djm 469: if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
1.99 djm 470: return cc->encrypt ?
471: SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
1.85 markus 472: if (cc->encrypt &&
1.102 djm 473: !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
1.85 markus 474: authlen, dest + aadlen + len))
1.99 djm 475: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 476: }
1.93 markus 477: return 0;
1.98 markus 478: #endif
1.21 markus 479: }
480:
1.91 djm 481: /* Extract the packet length, including any decryption necessary beforehand */
482: int
1.99 djm 483: cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
1.91 djm 484: const u_char *cp, u_int len)
485: {
486: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
487: return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
488: cp, len);
489: if (len < 4)
1.99 djm 490: return SSH_ERR_MESSAGE_INCOMPLETE;
1.91 djm 491: *plenp = get_u32(cp);
492: return 0;
493: }
494:
1.102 djm 495: void
496: cipher_free(struct sshcipher_ctx *cc)
1.16 markus 497: {
1.102 djm 498: if (cc == NULL)
499: return;
1.91 djm 500: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.96 djm 501: explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
1.98 markus 502: else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
503: explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
504: #ifdef WITH_OPENSSL
1.102 djm 505: if (cc->evp != NULL) {
506: EVP_CIPHER_CTX_free(cc->evp);
507: cc->evp = NULL;
508: }
1.98 markus 509: #endif
1.102 djm 510: explicit_bzero(cc, sizeof(*cc));
511: free(cc);
1.16 markus 512: }
1.1 deraadt 513:
1.32 markus 514: /*
515: * Selects the cipher, and keys if by computing the MD5 checksum of the
516: * passphrase and using the resulting 16 bytes as the key.
517: */
1.99 djm 518: int
1.102 djm 519: cipher_set_key_string(struct sshcipher_ctx **ccp,
520: const struct sshcipher *cipher, const char *passphrase, int do_encrypt)
1.16 markus 521: {
1.41 markus 522: u_char digest[16];
1.99 djm 523: int r = SSH_ERR_INTERNAL_ERROR;
1.32 markus 524:
1.99 djm 525: if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
526: passphrase, strlen(passphrase),
527: digest, sizeof(digest))) != 0)
528: goto out;
1.16 markus 529:
1.102 djm 530: r = cipher_init(ccp, cipher, digest, 16, NULL, 0, do_encrypt);
1.99 djm 531: out:
1.96 djm 532: explicit_bzero(digest, sizeof(digest));
1.99 djm 533: return r;
1.52 markus 534: }
1.53 markus 535:
1.54 markus 536: /*
1.99 djm 537: * Exports an IV from the sshcipher_ctx required to export the key
1.53 markus 538: * state back from the unprivileged child to the privileged parent
539: * process.
540: */
541: int
1.99 djm 542: cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
1.53 markus 543: {
1.99 djm 544: const struct sshcipher *c = cc->cipher;
1.98 markus 545: int ivlen = 0;
1.53 markus 546:
547: if (c->number == SSH_CIPHER_3DES)
548: ivlen = 24;
1.91 djm 549: else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
550: ivlen = 0;
1.100 djm 551: else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
552: ivlen = sizeof(cc->ac_ctx.ctr);
1.98 markus 553: #ifdef WITH_OPENSSL
1.53 markus 554: else
1.102 djm 555: ivlen = EVP_CIPHER_CTX_iv_length(cc->evp);
1.99 djm 556: #endif /* WITH_OPENSSL */
1.53 markus 557: return (ivlen);
558: }
559:
1.99 djm 560: int
561: cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
1.53 markus 562: {
1.99 djm 563: const struct sshcipher *c = cc->cipher;
1.98 markus 564: #ifdef WITH_OPENSSL
1.99 djm 565: int evplen;
1.98 markus 566: #endif
1.53 markus 567:
1.91 djm 568: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
569: if (len != 0)
1.99 djm 570: return SSH_ERR_INVALID_ARGUMENT;
1.100 djm 571: return 0;
572: }
573: if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
574: if (len != sizeof(cc->ac_ctx.ctr))
575: return SSH_ERR_INVALID_ARGUMENT;
576: memcpy(iv, cc->ac_ctx.ctr, len);
1.99 djm 577: return 0;
1.91 djm 578: }
1.98 markus 579: if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99 djm 580: return 0;
1.91 djm 581:
1.53 markus 582: switch (c->number) {
1.98 markus 583: #ifdef WITH_OPENSSL
1.53 markus 584: case SSH_CIPHER_SSH2:
585: case SSH_CIPHER_DES:
586: case SSH_CIPHER_BLOWFISH:
1.102 djm 587: evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
1.99 djm 588: if (evplen == 0)
589: return 0;
590: else if (evplen < 0)
591: return SSH_ERR_LIBCRYPTO_ERROR;
1.76 djm 592: if ((u_int)evplen != len)
1.99 djm 593: return SSH_ERR_INVALID_ARGUMENT;
1.85 markus 594: if (cipher_authlen(c)) {
1.102 djm 595: if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
1.85 markus 596: len, iv))
1.99 djm 597: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 598: } else
1.102 djm 599: memcpy(iv, cc->evp->iv, len);
1.63 markus 600: break;
1.98 markus 601: #endif
1.53 markus 602: default:
1.99 djm 603: return SSH_ERR_INVALID_ARGUMENT;
1.53 markus 604: }
1.99 djm 605: return 0;
1.53 markus 606: }
607:
1.99 djm 608: int
609: cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
1.53 markus 610: {
1.99 djm 611: const struct sshcipher *c = cc->cipher;
1.98 markus 612: #ifdef WITH_OPENSSL
1.99 djm 613: int evplen = 0;
1.98 markus 614: #endif
1.91 djm 615:
616: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.99 djm 617: return 0;
1.98 markus 618: if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99 djm 619: return 0;
1.53 markus 620:
621: switch (c->number) {
1.98 markus 622: #ifdef WITH_OPENSSL
1.53 markus 623: case SSH_CIPHER_SSH2:
624: case SSH_CIPHER_DES:
625: case SSH_CIPHER_BLOWFISH:
1.102 djm 626: evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
1.99 djm 627: if (evplen <= 0)
628: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 629: if (cipher_authlen(c)) {
1.99 djm 630: /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
1.102 djm 631: if (!EVP_CIPHER_CTX_ctrl(cc->evp,
1.99 djm 632: EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
633: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 634: } else
1.102 djm 635: memcpy(cc->evp->iv, iv, evplen);
1.63 markus 636: break;
1.98 markus 637: #endif
1.53 markus 638: default:
1.99 djm 639: return SSH_ERR_INVALID_ARGUMENT;
1.53 markus 640: }
1.99 djm 641: return 0;
1.53 markus 642: }
643:
1.98 markus 644: #ifdef WITH_OPENSSL
1.102 djm 645: #define EVP_X_STATE(evp) (evp)->cipher_data
646: #define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
1.98 markus 647: #endif
1.53 markus 648:
649: int
1.99 djm 650: cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
1.53 markus 651: {
1.98 markus 652: #ifdef WITH_OPENSSL
1.99 djm 653: const struct sshcipher *c = cc->cipher;
1.59 markus 654: int plen = 0;
1.53 markus 655:
1.87 djm 656: if (c->evptype == EVP_rc4) {
1.59 markus 657: plen = EVP_X_STATE_LEN(cc->evp);
1.53 markus 658: if (dat == NULL)
1.59 markus 659: return (plen);
660: memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53 markus 661: }
662: return (plen);
1.98 markus 663: #else
1.99 djm 664: return 0;
1.98 markus 665: #endif
1.53 markus 666: }
667:
668: void
1.99 djm 669: cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
1.53 markus 670: {
1.98 markus 671: #ifdef WITH_OPENSSL
1.99 djm 672: const struct sshcipher *c = cc->cipher;
1.53 markus 673: int plen;
674:
1.87 djm 675: if (c->evptype == EVP_rc4) {
1.53 markus 676: plen = EVP_X_STATE_LEN(cc->evp);
677: memcpy(EVP_X_STATE(cc->evp), dat, plen);
678: }
1.98 markus 679: #endif
1.1 deraadt 680: }