1 /* $OpenBSD: cipher.c,v 1.102 2016/08/03 05:41:57 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
14 * Copyright (c) 1999 Niels Provos. All rights reserved.
15 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
40 #include <sys/types.h>
52 #include "openbsd-compat/openssl-compat.h"
55 extern const EVP_CIPHER *evp_ssh1_bf(void);
56 extern const EVP_CIPHER *evp_ssh1_3des(void);
57 extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
60 struct sshcipher_ctx {
64 struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
65 struct aesctr_ctx ac_ctx; /* XXX union with evp? */
66 const struct sshcipher *cipher;
71 int number; /* for ssh1 only */
74 u_int iv_len; /* defaults to block_size */
78 #define CFLAG_CBC (1<<0)
79 #define CFLAG_CHACHAPOLY (1<<1)
80 #define CFLAG_AESCTR (1<<2)
81 #define CFLAG_NONE (1<<3)
83 const EVP_CIPHER *(*evptype)(void);
89 static const struct sshcipher ciphers[] = {
91 { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
92 { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
93 # ifndef OPENSSL_NO_BF
94 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
95 # endif /* OPENSSL_NO_BF */
96 #endif /* WITH_SSH1 */
98 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
99 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
100 # ifndef OPENSSL_NO_BF
102 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
103 # endif /* OPENSSL_NO_BF */
104 # ifndef OPENSSL_NO_CAST
106 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
107 # endif /* OPENSSL_NO_CAST */
108 # ifndef OPENSSL_NO_RC4
109 { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
110 { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
111 { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
112 # endif /* OPENSSL_NO_RC4 */
113 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
114 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
115 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
116 { "rijndael-cbc@lysator.liu.se",
117 SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
118 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
119 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
120 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
121 # ifdef OPENSSL_HAVE_EVPGCM
122 { "aes128-gcm@openssh.com",
123 SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
124 { "aes256-gcm@openssh.com",
125 SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
126 # endif /* OPENSSL_HAVE_EVPGCM */
127 #else /* WITH_OPENSSL */
128 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
129 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
130 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
131 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
132 #endif /* WITH_OPENSSL */
133 { "chacha20-poly1305@openssh.com",
134 SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
136 { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
141 /* Returns a comma-separated list of supported ciphers. */
143 cipher_alg_list(char sep, int auth_only)
145 char *tmp, *ret = NULL;
146 size_t nlen, rlen = 0;
147 const struct sshcipher *c;
149 for (c = ciphers; c->name != NULL; c++) {
150 if (c->number != SSH_CIPHER_SSH2)
152 if (auth_only && c->auth_len == 0)
156 nlen = strlen(c->name);
157 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
162 memcpy(ret + rlen, c->name, nlen + 1);
169 cipher_blocksize(const struct sshcipher *c)
171 return (c->block_size);
175 cipher_keylen(const struct sshcipher *c)
181 cipher_seclen(const struct sshcipher *c)
183 if (strcmp("3des-cbc", c->name) == 0)
185 return cipher_keylen(c);
189 cipher_authlen(const struct sshcipher *c)
191 return (c->auth_len);
195 cipher_ivlen(const struct sshcipher *c)
198 * Default is cipher block size, except for chacha20+poly1305 that
199 * needs no IV. XXX make iv_len == -1 default?
201 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
202 c->iv_len : c->block_size;
206 cipher_get_number(const struct sshcipher *c)
212 cipher_is_cbc(const struct sshcipher *c)
214 return (c->flags & CFLAG_CBC) != 0;
218 cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
220 return cc->plaintext;
224 cipher_ctx_get_number(struct sshcipher_ctx *cc)
226 return cc->cipher->number;
230 cipher_mask_ssh1(int client)
233 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
234 mask |= 1 << SSH_CIPHER_BLOWFISH;
236 mask |= 1 << SSH_CIPHER_DES;
241 const struct sshcipher *
242 cipher_by_name(const char *name)
244 const struct sshcipher *c;
245 for (c = ciphers; c->name != NULL; c++)
246 if (strcmp(c->name, name) == 0)
251 const struct sshcipher *
252 cipher_by_number(int id)
254 const struct sshcipher *c;
255 for (c = ciphers; c->name != NULL; c++)
261 #define CIPHER_SEP ","
263 ciphers_valid(const char *names)
265 const struct sshcipher *c;
266 char *cipher_list, *cp;
269 if (names == NULL || strcmp(names, "") == 0)
271 if ((cipher_list = cp = strdup(names)) == NULL)
273 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
274 (p = strsep(&cp, CIPHER_SEP))) {
275 c = cipher_by_name(p);
276 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
286 * Parses the name of the cipher. Returns the number of the corresponding
287 * cipher, or -1 on error.
291 cipher_number(const char *name)
293 const struct sshcipher *c;
296 for (c = ciphers; c->name != NULL; c++)
297 if (strcasecmp(c->name, name) == 0)
305 const struct sshcipher *c = cipher_by_number(id);
306 return (c==NULL) ? "<unknown>" : c->name;
310 cipher_warning_message(const struct sshcipher_ctx *cc)
312 if (cc == NULL || cc->cipher == NULL)
314 if (cc->cipher->number == SSH_CIPHER_DES)
315 return "use of DES is strongly discouraged due to "
316 "cryptographic weaknesses";
321 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
322 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
325 struct sshcipher_ctx *cc = NULL;
326 int ret = SSH_ERR_INTERNAL_ERROR;
328 const EVP_CIPHER *type;
330 u_char *junk, *discard;
334 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
335 return SSH_ERR_ALLOC_FAIL;
337 if (cipher->number == SSH_CIPHER_DES) {
342 cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
343 cc->encrypt = do_encrypt;
345 if (keylen < cipher->key_len ||
346 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
347 ret = SSH_ERR_INVALID_ARGUMENT;
352 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
353 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
357 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
358 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
359 aesctr_ivsetup(&cc->ac_ctx, iv);
363 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
367 ret = SSH_ERR_INVALID_ARGUMENT;
369 #else /* WITH_OPENSSL */
370 type = (*cipher->evptype)();
371 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
372 ret = SSH_ERR_ALLOC_FAIL;
375 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
376 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
377 ret = SSH_ERR_LIBCRYPTO_ERROR;
380 if (cipher_authlen(cipher) &&
381 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
383 ret = SSH_ERR_LIBCRYPTO_ERROR;
386 klen = EVP_CIPHER_CTX_key_length(cc->evp);
387 if (klen > 0 && keylen != (u_int)klen) {
388 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
389 ret = SSH_ERR_LIBCRYPTO_ERROR;
393 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
394 ret = SSH_ERR_LIBCRYPTO_ERROR;
398 if (cipher->discard_len > 0) {
399 if ((junk = malloc(cipher->discard_len)) == NULL ||
400 (discard = malloc(cipher->discard_len)) == NULL) {
402 ret = SSH_ERR_ALLOC_FAIL;
405 ret = EVP_Cipher(cc->evp, discard, junk, cipher->discard_len);
406 explicit_bzero(discard, cipher->discard_len);
410 ret = SSH_ERR_LIBCRYPTO_ERROR;
415 #endif /* WITH_OPENSSL */
424 EVP_CIPHER_CTX_free(cc->evp);
425 #endif /* WITH_OPENSSL */
426 explicit_bzero(cc, sizeof(*cc));
434 * cipher_crypt() operates as following:
435 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
436 * Theses bytes are treated as additional authenticated data for
437 * authenticated encryption modes.
438 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
439 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
440 * This tag is written on encryption and verified on decryption.
441 * Both 'aadlen' and 'authlen' can be set to 0.
444 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
445 const u_char *src, u_int len, u_int aadlen, u_int authlen)
447 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
448 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
449 len, aadlen, authlen, cc->encrypt);
452 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
454 memcpy(dest, src, aadlen);
455 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
459 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
460 memcpy(dest, src, aadlen + len);
463 return SSH_ERR_INVALID_ARGUMENT;
468 if (authlen != cipher_authlen(cc->cipher))
469 return SSH_ERR_INVALID_ARGUMENT;
471 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
473 return SSH_ERR_LIBCRYPTO_ERROR;
474 /* set tag on decyption */
476 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
477 authlen, (u_char *)src + aadlen + len))
478 return SSH_ERR_LIBCRYPTO_ERROR;
482 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
483 return SSH_ERR_LIBCRYPTO_ERROR;
484 memcpy(dest, src, aadlen);
486 if (len % cc->cipher->block_size)
487 return SSH_ERR_INVALID_ARGUMENT;
488 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
490 return SSH_ERR_LIBCRYPTO_ERROR;
492 /* compute tag (on encrypt) or verify tag (on decrypt) */
493 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
495 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
497 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
498 authlen, dest + aadlen + len))
499 return SSH_ERR_LIBCRYPTO_ERROR;
505 /* Extract the packet length, including any decryption necessary beforehand */
507 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
508 const u_char *cp, u_int len)
510 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
511 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
514 return SSH_ERR_MESSAGE_INCOMPLETE;
515 *plenp = get_u32(cp);
520 cipher_free(struct sshcipher_ctx *cc)
524 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
525 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
526 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
527 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
529 if (cc->evp != NULL) {
530 EVP_CIPHER_CTX_free(cc->evp);
534 explicit_bzero(cc, sizeof(*cc));
539 * Selects the cipher, and keys if by computing the MD5 checksum of the
540 * passphrase and using the resulting 16 bytes as the key.
543 cipher_set_key_string(struct sshcipher_ctx **ccp,
544 const struct sshcipher *cipher, const char *passphrase, int do_encrypt)
547 int r = SSH_ERR_INTERNAL_ERROR;
549 if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
550 passphrase, strlen(passphrase),
551 digest, sizeof(digest))) != 0)
554 r = cipher_init(ccp, cipher, digest, 16, NULL, 0, do_encrypt);
556 explicit_bzero(digest, sizeof(digest));
561 * Exports an IV from the sshcipher_ctx required to export the key
562 * state back from the unprivileged child to the privileged parent
566 cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
568 const struct sshcipher *c = cc->cipher;
571 if (c->number == SSH_CIPHER_3DES)
573 else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
575 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
576 ivlen = sizeof(cc->ac_ctx.ctr);
579 ivlen = EVP_CIPHER_CTX_iv_length(cc->evp);
580 #endif /* WITH_OPENSSL */
585 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
587 const struct sshcipher *c = cc->cipher;
592 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
594 return SSH_ERR_INVALID_ARGUMENT;
597 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
598 if (len != sizeof(cc->ac_ctx.ctr))
599 return SSH_ERR_INVALID_ARGUMENT;
600 memcpy(iv, cc->ac_ctx.ctr, len);
603 if ((cc->cipher->flags & CFLAG_NONE) != 0)
608 case SSH_CIPHER_SSH2:
610 case SSH_CIPHER_BLOWFISH:
611 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
615 return SSH_ERR_LIBCRYPTO_ERROR;
616 if ((u_int)evplen != len)
617 return SSH_ERR_INVALID_ARGUMENT;
618 #ifndef OPENSSL_HAVE_EVPCTR
619 if (c->evptype == evp_aes_128_ctr)
620 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
623 if (cipher_authlen(c)) {
624 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
626 return SSH_ERR_LIBCRYPTO_ERROR;
628 memcpy(iv, cc->evp->iv, len);
632 case SSH_CIPHER_3DES:
633 return ssh1_3des_iv(cc->evp, 0, iv, 24);
636 return SSH_ERR_INVALID_ARGUMENT;
642 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
644 const struct sshcipher *c = cc->cipher;
649 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
651 if ((cc->cipher->flags & CFLAG_NONE) != 0)
656 case SSH_CIPHER_SSH2:
658 case SSH_CIPHER_BLOWFISH:
659 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
661 return SSH_ERR_LIBCRYPTO_ERROR;
662 #ifndef OPENSSL_HAVE_EVPCTR
663 /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
664 if (c->evptype == evp_aes_128_ctr)
665 ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
668 if (cipher_authlen(c)) {
669 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
670 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
671 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
672 return SSH_ERR_LIBCRYPTO_ERROR;
674 memcpy(cc->evp->iv, iv, evplen);
678 case SSH_CIPHER_3DES:
679 return ssh1_3des_iv(cc->evp, 1, (u_char *)iv, 24);
682 return SSH_ERR_INVALID_ARGUMENT;
688 #define EVP_X_STATE(evp) (evp)->cipher_data
689 #define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
693 cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
695 #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
696 const struct sshcipher *c = cc->cipher;
699 if (c->evptype == EVP_rc4) {
700 plen = EVP_X_STATE_LEN(cc->evp);
703 memcpy(dat, EVP_X_STATE(cc->evp), plen);
712 cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
714 #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
715 const struct sshcipher *c = cc->cipher;
718 if (c->evptype == EVP_rc4) {
719 plen = EVP_X_STATE_LEN(cc->evp);
720 memcpy(EVP_X_STATE(cc->evp), dat, plen);