1 /* $OpenBSD: cipher.c,v 1.107 2017/05/07 23:12: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 struct sshcipher_ctx {
59 struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
60 struct aesctr_ctx ac_ctx; /* XXX union with evp? */
61 const struct sshcipher *cipher;
68 u_int iv_len; /* defaults to block_size */
71 #define CFLAG_CBC (1<<0)
72 #define CFLAG_CHACHAPOLY (1<<1)
73 #define CFLAG_AESCTR (1<<2)
74 #define CFLAG_NONE (1<<3)
75 #define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */
77 const EVP_CIPHER *(*evptype)(void);
83 static const struct sshcipher ciphers[] = {
85 { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
86 { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
87 { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
88 { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
89 { "rijndael-cbc@lysator.liu.se",
90 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
91 { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr },
92 { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr },
93 { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr },
94 # ifdef OPENSSL_HAVE_EVPGCM
95 { "aes128-gcm@openssh.com",
96 16, 16, 12, 16, 0, EVP_aes_128_gcm },
97 { "aes256-gcm@openssh.com",
98 16, 32, 12, 16, 0, EVP_aes_256_gcm },
99 # endif /* OPENSSL_HAVE_EVPGCM */
101 { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR, NULL },
102 { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR, NULL },
103 { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR, NULL },
105 { "chacha20-poly1305@openssh.com",
106 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
107 { "none", 8, 0, 0, 0, CFLAG_NONE, NULL },
109 { NULL, 0, 0, 0, 0, 0, NULL }
114 /* Returns a comma-separated list of supported ciphers. */
116 cipher_alg_list(char sep, int auth_only)
118 char *tmp, *ret = NULL;
119 size_t nlen, rlen = 0;
120 const struct sshcipher *c;
122 for (c = ciphers; c->name != NULL; c++) {
123 if ((c->flags & CFLAG_INTERNAL) != 0)
125 if (auth_only && c->auth_len == 0)
129 nlen = strlen(c->name);
130 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
135 memcpy(ret + rlen, c->name, nlen + 1);
142 cipher_blocksize(const struct sshcipher *c)
144 return (c->block_size);
148 cipher_keylen(const struct sshcipher *c)
154 cipher_seclen(const struct sshcipher *c)
156 if (strcmp("3des-cbc", c->name) == 0)
158 return cipher_keylen(c);
162 cipher_authlen(const struct sshcipher *c)
164 return (c->auth_len);
168 cipher_ivlen(const struct sshcipher *c)
171 * Default is cipher block size, except for chacha20+poly1305 that
172 * needs no IV. XXX make iv_len == -1 default?
174 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
175 c->iv_len : c->block_size;
179 cipher_is_cbc(const struct sshcipher *c)
181 return (c->flags & CFLAG_CBC) != 0;
185 cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
187 return cc->plaintext;
190 const struct sshcipher *
191 cipher_by_name(const char *name)
193 const struct sshcipher *c;
194 for (c = ciphers; c->name != NULL; c++)
195 if (strcmp(c->name, name) == 0)
200 #define CIPHER_SEP ","
202 ciphers_valid(const char *names)
204 const struct sshcipher *c;
205 char *cipher_list, *cp;
208 if (names == NULL || strcmp(names, "") == 0)
210 if ((cipher_list = cp = strdup(names)) == NULL)
212 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
213 (p = strsep(&cp, CIPHER_SEP))) {
214 c = cipher_by_name(p);
215 if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
225 cipher_warning_message(const struct sshcipher_ctx *cc)
227 if (cc == NULL || cc->cipher == NULL)
229 /* XXX repurpose for CBC warning */
234 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
235 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
238 struct sshcipher_ctx *cc = NULL;
239 int ret = SSH_ERR_INTERNAL_ERROR;
241 const EVP_CIPHER *type;
246 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
247 return SSH_ERR_ALLOC_FAIL;
249 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
250 cc->encrypt = do_encrypt;
252 if (keylen < cipher->key_len ||
253 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
254 ret = SSH_ERR_INVALID_ARGUMENT;
259 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
260 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
263 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
268 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
269 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
270 aesctr_ivsetup(&cc->ac_ctx, iv);
274 ret = SSH_ERR_INVALID_ARGUMENT;
276 #else /* WITH_OPENSSL */
277 type = (*cipher->evptype)();
278 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
279 ret = SSH_ERR_ALLOC_FAIL;
282 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
283 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
284 ret = SSH_ERR_LIBCRYPTO_ERROR;
287 if (cipher_authlen(cipher) &&
288 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
290 ret = SSH_ERR_LIBCRYPTO_ERROR;
293 klen = EVP_CIPHER_CTX_key_length(cc->evp);
294 if (klen > 0 && keylen != (u_int)klen) {
295 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
296 ret = SSH_ERR_LIBCRYPTO_ERROR;
300 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
301 ret = SSH_ERR_LIBCRYPTO_ERROR;
305 #endif /* WITH_OPENSSL */
314 EVP_CIPHER_CTX_free(cc->evp);
315 #endif /* WITH_OPENSSL */
316 explicit_bzero(cc, sizeof(*cc));
324 * cipher_crypt() operates as following:
325 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
326 * Theses bytes are treated as additional authenticated data for
327 * authenticated encryption modes.
328 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
329 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
330 * This tag is written on encryption and verified on decryption.
331 * Both 'aadlen' and 'authlen' can be set to 0.
334 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
335 const u_char *src, u_int len, u_int aadlen, u_int authlen)
337 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
338 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
339 len, aadlen, authlen, cc->encrypt);
341 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
342 memcpy(dest, src, aadlen + len);
346 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
348 memcpy(dest, src, aadlen);
349 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
353 return SSH_ERR_INVALID_ARGUMENT;
358 if (authlen != cipher_authlen(cc->cipher))
359 return SSH_ERR_INVALID_ARGUMENT;
361 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
363 return SSH_ERR_LIBCRYPTO_ERROR;
364 /* set tag on decyption */
366 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
367 authlen, (u_char *)src + aadlen + len))
368 return SSH_ERR_LIBCRYPTO_ERROR;
372 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
373 return SSH_ERR_LIBCRYPTO_ERROR;
374 memcpy(dest, src, aadlen);
376 if (len % cc->cipher->block_size)
377 return SSH_ERR_INVALID_ARGUMENT;
378 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
380 return SSH_ERR_LIBCRYPTO_ERROR;
382 /* compute tag (on encrypt) or verify tag (on decrypt) */
383 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
385 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
387 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
388 authlen, dest + aadlen + len))
389 return SSH_ERR_LIBCRYPTO_ERROR;
395 /* Extract the packet length, including any decryption necessary beforehand */
397 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
398 const u_char *cp, u_int len)
400 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
401 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
404 return SSH_ERR_MESSAGE_INCOMPLETE;
405 *plenp = get_u32(cp);
410 cipher_free(struct sshcipher_ctx *cc)
414 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
415 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
416 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
417 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
419 if (cc->evp != NULL) {
420 EVP_CIPHER_CTX_free(cc->evp);
424 explicit_bzero(cc, sizeof(*cc));
429 * Exports an IV from the sshcipher_ctx required to export the key
430 * state back from the unprivileged child to the privileged parent
434 cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
436 const struct sshcipher *c = cc->cipher;
438 if ((c->flags & CFLAG_CHACHAPOLY) != 0)
440 else if ((c->flags & CFLAG_AESCTR) != 0)
441 return sizeof(cc->ac_ctx.ctr);
443 return EVP_CIPHER_CTX_iv_length(cc->evp);
450 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
452 const struct sshcipher *c = cc->cipher;
457 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
459 return SSH_ERR_INVALID_ARGUMENT;
462 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
463 if (len != sizeof(cc->ac_ctx.ctr))
464 return SSH_ERR_INVALID_ARGUMENT;
465 memcpy(iv, cc->ac_ctx.ctr, len);
468 if ((cc->cipher->flags & CFLAG_NONE) != 0)
472 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
476 return SSH_ERR_LIBCRYPTO_ERROR;
477 if ((u_int)evplen != len)
478 return SSH_ERR_INVALID_ARGUMENT;
479 #ifndef OPENSSL_HAVE_EVPCTR
480 if (c->evptype == evp_aes_128_ctr)
481 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
484 if (cipher_authlen(c)) {
485 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
487 return SSH_ERR_LIBCRYPTO_ERROR;
489 memcpy(iv, cc->evp->iv, len);
495 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
497 const struct sshcipher *c = cc->cipher;
502 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
504 if ((cc->cipher->flags & CFLAG_NONE) != 0)
508 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
510 return SSH_ERR_LIBCRYPTO_ERROR;
511 #ifndef OPENSSL_HAVE_EVPCTR
512 /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
513 if (c->evptype == evp_aes_128_ctr)
514 ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
517 if (cipher_authlen(c)) {
518 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
519 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
520 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
521 return SSH_ERR_LIBCRYPTO_ERROR;
523 memcpy(cc->evp->iv, iv, evplen);
529 #define EVP_X_STATE(evp) (evp)->cipher_data
530 #define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
534 cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
536 #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
537 const struct sshcipher *c = cc->cipher;
540 if (c->evptype == EVP_rc4) {
541 plen = EVP_X_STATE_LEN(cc->evp);
544 memcpy(dat, EVP_X_STATE(cc->evp), plen);
553 cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
555 #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
556 const struct sshcipher *c = cc->cipher;
559 if (c->evptype == EVP_rc4) {
560 plen = EVP_X_STATE_LEN(cc->evp);
561 memcpy(EVP_X_STATE(cc->evp), dat, plen);