Annotation of src/usr.bin/ssh/cipher.c, Revision 1.101
1.101 ! mmcc 1: /* $OpenBSD: cipher.c,v 1.100 2015/01/14 10:29:45 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.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) {
1.101 ! mmcc 350: free(junk);
1.99 djm 351: ret = SSH_ERR_ALLOC_FAIL;
352: goto bad;
353: }
354: ret = EVP_Cipher(&cc->evp, discard, junk, cipher->discard_len);
1.96 djm 355: explicit_bzero(discard, cipher->discard_len);
1.89 djm 356: free(junk);
357: free(discard);
1.99 djm 358: if (ret != 1) {
359: ret = SSH_ERR_LIBCRYPTO_ERROR;
360: bad:
361: EVP_CIPHER_CTX_cleanup(&cc->evp);
362: return ret;
363: }
1.74 djm 364: }
1.98 markus 365: #endif
1.99 djm 366: return 0;
1.1 deraadt 367: }
1.21 markus 368:
1.83 markus 369: /*
370: * cipher_crypt() operates as following:
371: * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
372: * Theses bytes are treated as additional authenticated data for
373: * authenticated encryption modes.
374: * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
1.85 markus 375: * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
376: * This tag is written on encryption and verified on decryption.
1.83 markus 377: * Both 'aadlen' and 'authlen' can be set to 0.
378: */
1.93 markus 379: int
1.99 djm 380: cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
381: const u_char *src, u_int len, u_int aadlen, u_int authlen)
1.32 markus 382: {
1.99 djm 383: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
384: return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
385: len, aadlen, authlen, cc->encrypt);
386: }
1.98 markus 387: #ifndef WITH_OPENSSL
388: if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
389: if (aadlen)
390: memcpy(dest, src, aadlen);
391: aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
392: dest + aadlen, len);
393: return 0;
394: }
395: if ((cc->cipher->flags & CFLAG_NONE) != 0) {
396: memcpy(dest, src, aadlen + len);
397: return 0;
398: }
1.99 djm 399: return SSH_ERR_INVALID_ARGUMENT;
1.98 markus 400: #else
1.85 markus 401: if (authlen) {
402: u_char lastiv[1];
403:
404: if (authlen != cipher_authlen(cc->cipher))
1.99 djm 405: return SSH_ERR_INVALID_ARGUMENT;
1.85 markus 406: /* increment IV */
407: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
408: 1, lastiv))
1.99 djm 409: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 410: /* set tag on decyption */
411: if (!cc->encrypt &&
412: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
413: authlen, (u_char *)src + aadlen + len))
1.99 djm 414: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 415: }
416: if (aadlen) {
417: if (authlen &&
418: EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
1.99 djm 419: return SSH_ERR_LIBCRYPTO_ERROR;
1.83 markus 420: memcpy(dest, src, aadlen);
1.85 markus 421: }
1.32 markus 422: if (len % cc->cipher->block_size)
1.99 djm 423: return SSH_ERR_INVALID_ARGUMENT;
1.83 markus 424: if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
425: len) < 0)
1.99 djm 426: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 427: if (authlen) {
428: /* compute tag (on encrypt) or verify tag (on decrypt) */
1.99 djm 429: if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0)
430: return cc->encrypt ?
431: SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
1.85 markus 432: if (cc->encrypt &&
433: !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
434: authlen, dest + aadlen + len))
1.99 djm 435: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 436: }
1.93 markus 437: return 0;
1.98 markus 438: #endif
1.21 markus 439: }
440:
1.91 djm 441: /* Extract the packet length, including any decryption necessary beforehand */
442: int
1.99 djm 443: cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
1.91 djm 444: const u_char *cp, u_int len)
445: {
446: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
447: return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
448: cp, len);
449: if (len < 4)
1.99 djm 450: return SSH_ERR_MESSAGE_INCOMPLETE;
1.91 djm 451: *plenp = get_u32(cp);
452: return 0;
453: }
454:
1.99 djm 455: int
456: cipher_cleanup(struct sshcipher_ctx *cc)
1.16 markus 457: {
1.99 djm 458: if (cc == NULL || cc->cipher == NULL)
459: return 0;
1.91 djm 460: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.96 djm 461: explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
1.98 markus 462: else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
463: explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
464: #ifdef WITH_OPENSSL
1.91 djm 465: else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
1.99 djm 466: return SSH_ERR_LIBCRYPTO_ERROR;
1.98 markus 467: #endif
1.99 djm 468: return 0;
1.16 markus 469: }
1.1 deraadt 470:
1.32 markus 471: /*
472: * Selects the cipher, and keys if by computing the MD5 checksum of the
473: * passphrase and using the resulting 16 bytes as the key.
474: */
1.99 djm 475: int
476: cipher_set_key_string(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
1.69 avsm 477: const char *passphrase, int do_encrypt)
1.16 markus 478: {
1.41 markus 479: u_char digest[16];
1.99 djm 480: int r = SSH_ERR_INTERNAL_ERROR;
1.32 markus 481:
1.99 djm 482: if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
483: passphrase, strlen(passphrase),
484: digest, sizeof(digest))) != 0)
485: goto out;
1.16 markus 486:
1.99 djm 487: r = cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
488: out:
1.96 djm 489: explicit_bzero(digest, sizeof(digest));
1.99 djm 490: return r;
1.52 markus 491: }
1.53 markus 492:
1.54 markus 493: /*
1.99 djm 494: * Exports an IV from the sshcipher_ctx required to export the key
1.53 markus 495: * state back from the unprivileged child to the privileged parent
496: * process.
497: */
498: int
1.99 djm 499: cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
1.53 markus 500: {
1.99 djm 501: const struct sshcipher *c = cc->cipher;
1.98 markus 502: int ivlen = 0;
1.53 markus 503:
504: if (c->number == SSH_CIPHER_3DES)
505: ivlen = 24;
1.91 djm 506: else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
507: ivlen = 0;
1.100 djm 508: else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
509: ivlen = sizeof(cc->ac_ctx.ctr);
1.98 markus 510: #ifdef WITH_OPENSSL
1.53 markus 511: else
512: ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.99 djm 513: #endif /* WITH_OPENSSL */
1.53 markus 514: return (ivlen);
515: }
516:
1.99 djm 517: int
518: cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
1.53 markus 519: {
1.99 djm 520: const struct sshcipher *c = cc->cipher;
1.98 markus 521: #ifdef WITH_OPENSSL
1.99 djm 522: int evplen;
1.98 markus 523: #endif
1.53 markus 524:
1.91 djm 525: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
526: if (len != 0)
1.99 djm 527: return SSH_ERR_INVALID_ARGUMENT;
1.100 djm 528: return 0;
529: }
530: if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
531: if (len != sizeof(cc->ac_ctx.ctr))
532: return SSH_ERR_INVALID_ARGUMENT;
533: memcpy(iv, cc->ac_ctx.ctr, len);
1.99 djm 534: return 0;
1.91 djm 535: }
1.98 markus 536: if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99 djm 537: return 0;
1.91 djm 538:
1.53 markus 539: switch (c->number) {
1.98 markus 540: #ifdef WITH_OPENSSL
1.53 markus 541: case SSH_CIPHER_SSH2:
542: case SSH_CIPHER_DES:
543: case SSH_CIPHER_BLOWFISH:
544: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.99 djm 545: if (evplen == 0)
546: return 0;
547: else if (evplen < 0)
548: return SSH_ERR_LIBCRYPTO_ERROR;
1.76 djm 549: if ((u_int)evplen != len)
1.99 djm 550: return SSH_ERR_INVALID_ARGUMENT;
1.85 markus 551: if (cipher_authlen(c)) {
552: if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
553: len, iv))
1.99 djm 554: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 555: } else
556: memcpy(iv, cc->evp.iv, len);
1.63 markus 557: break;
1.98 markus 558: #endif
559: #ifdef WITH_SSH1
1.63 markus 560: case SSH_CIPHER_3DES:
1.99 djm 561: return ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.98 markus 562: #endif
1.53 markus 563: default:
1.99 djm 564: return SSH_ERR_INVALID_ARGUMENT;
1.53 markus 565: }
1.99 djm 566: return 0;
1.53 markus 567: }
568:
1.99 djm 569: int
570: cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
1.53 markus 571: {
1.99 djm 572: const struct sshcipher *c = cc->cipher;
1.98 markus 573: #ifdef WITH_OPENSSL
1.99 djm 574: int evplen = 0;
1.98 markus 575: #endif
1.91 djm 576:
577: if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
1.99 djm 578: return 0;
1.98 markus 579: if ((cc->cipher->flags & CFLAG_NONE) != 0)
1.99 djm 580: return 0;
1.53 markus 581:
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:
587: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
1.99 djm 588: if (evplen <= 0)
589: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 590: if (cipher_authlen(c)) {
1.99 djm 591: /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
1.85 markus 592: if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
1.99 djm 593: EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
594: return SSH_ERR_LIBCRYPTO_ERROR;
1.85 markus 595: } else
596: memcpy(cc->evp.iv, iv, evplen);
1.63 markus 597: break;
1.98 markus 598: #endif
599: #ifdef WITH_SSH1
1.63 markus 600: case SSH_CIPHER_3DES:
1.99 djm 601: return ssh1_3des_iv(&cc->evp, 1, (u_char *)iv, 24);
1.98 markus 602: #endif
1.53 markus 603: default:
1.99 djm 604: return SSH_ERR_INVALID_ARGUMENT;
1.53 markus 605: }
1.99 djm 606: return 0;
1.53 markus 607: }
608:
1.98 markus 609: #ifdef WITH_OPENSSL
1.53 markus 610: #define EVP_X_STATE(evp) (evp).cipher_data
611: #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
1.98 markus 612: #endif
1.53 markus 613:
614: int
1.99 djm 615: cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
1.53 markus 616: {
1.98 markus 617: #ifdef WITH_OPENSSL
1.99 djm 618: const struct sshcipher *c = cc->cipher;
1.59 markus 619: int plen = 0;
1.53 markus 620:
1.87 djm 621: if (c->evptype == EVP_rc4) {
1.59 markus 622: plen = EVP_X_STATE_LEN(cc->evp);
1.53 markus 623: if (dat == NULL)
1.59 markus 624: return (plen);
625: memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53 markus 626: }
627: return (plen);
1.98 markus 628: #else
1.99 djm 629: return 0;
1.98 markus 630: #endif
1.53 markus 631: }
632:
633: void
1.99 djm 634: cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
1.53 markus 635: {
1.98 markus 636: #ifdef WITH_OPENSSL
1.99 djm 637: const struct sshcipher *c = cc->cipher;
1.53 markus 638: int plen;
639:
1.87 djm 640: if (c->evptype == EVP_rc4) {
1.53 markus 641: plen = EVP_X_STATE_LEN(cc->evp);
642: memcpy(EVP_X_STATE(cc->evp), dat, plen);
643: }
1.98 markus 644: #endif
1.1 deraadt 645: }