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