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