blob: c3cd5dcf4405c64f0e1c41c37d2a64ef5b42181f [file] [log] [blame]
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +00001/* $OpenBSD: cipher.c,v 1.107 2017/05/07 23:12:57 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * 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".
Damien Miller4af51302000-04-16 11:18:38 +100012 *
Damien Millere4340be2000-09-16 13:29:08 +110013 *
14 * Copyright (c) 1999 Niels Provos. All rights reserved.
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 *
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.
25 *
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.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
Damien Millerd7834352006-08-05 12:39:39 +100040#include <sys/types.h>
41
Damien Millere3476ed2006-07-24 14:13:33 +100042#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100043#include <stdarg.h>
Damien Miller0fde8ac2013-11-21 14:12:23 +110044#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "cipher.h"
Damien Miller86687062014-07-02 15:28:02 +100047#include "misc.h"
48#include "sshbuf.h"
49#include "ssherr.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110050#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Darren Tucker55731712014-07-21 02:24:59 +100052#include "openbsd-compat/openssl-compat.h"
53
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
djm@openbsd.org4706c1d2016-08-03 05:41:57 +000055struct sshcipher_ctx {
56 int plaintext;
57 int encrypt;
58 EVP_CIPHER_CTX *evp;
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;
62};
63
Damien Miller86687062014-07-02 15:28:02 +100064struct sshcipher {
Damien Miller963f6b22002-02-19 15:21:23 +110065 char *name;
Damien Miller963f6b22002-02-19 15:21:23 +110066 u_int block_size;
67 u_int key_len;
Damien Millerd522c682013-01-09 16:42:47 +110068 u_int iv_len; /* defaults to block_size */
69 u_int auth_len;
Damien Miller0fde8ac2013-11-21 14:12:23 +110070 u_int flags;
71#define CFLAG_CBC (1<<0)
72#define CFLAG_CHACHAPOLY (1<<1)
Damien Miller1f0311c2014-05-15 14:24:09 +100073#define CFLAG_AESCTR (1<<2)
74#define CFLAG_NONE (1<<3)
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +000075#define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */
Damien Miller1f0311c2014-05-15 14:24:09 +100076#ifdef WITH_OPENSSL
Ben Lindstrom6a246412002-06-06 19:48:16 +000077 const EVP_CIPHER *(*evptype)(void);
Damien Miller1f0311c2014-05-15 14:24:09 +100078#else
79 void *ignored;
80#endif
Damien Millerea111192013-04-23 19:24:32 +100081};
82
Damien Miller86687062014-07-02 15:28:02 +100083static const struct sshcipher ciphers[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +100084#ifdef WITH_OPENSSL
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000085 { "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 },
Damien Miller1d75abf2013-01-09 16:12:19 +110089 { "rijndael-cbc@lysator.liu.se",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000090 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 },
Damien Miller86687062014-07-02 15:28:02 +100094# ifdef OPENSSL_HAVE_EVPGCM
Damien Miller1d75abf2013-01-09 16:12:19 +110095 { "aes128-gcm@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000096 16, 16, 12, 16, 0, EVP_aes_128_gcm },
Damien Miller1d75abf2013-01-09 16:12:19 +110097 { "aes256-gcm@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000098 16, 32, 12, 16, 0, EVP_aes_256_gcm },
Damien Miller86687062014-07-02 15:28:02 +100099# endif /* OPENSSL_HAVE_EVPGCM */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000100#else
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000101 { "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 },
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000104#endif
Damien Miller0fde8ac2013-11-21 14:12:23 +1100105 { "chacha20-poly1305@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000106 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
107 { "none", 8, 0, 0, 0, CFLAG_NONE, NULL },
Damien Miller86687062014-07-02 15:28:02 +1000108
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000109 { NULL, 0, 0, 0, 0, 0, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110};
111
Damien Miller874d77b2000-10-14 16:23:11 +1100112/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller86687062014-07-02 15:28:02 +1000114/* Returns a comma-separated list of supported ciphers. */
Damien Millerea111192013-04-23 19:24:32 +1000115char *
Damien Miller0fde8ac2013-11-21 14:12:23 +1100116cipher_alg_list(char sep, int auth_only)
Damien Millerea111192013-04-23 19:24:32 +1000117{
Damien Miller86687062014-07-02 15:28:02 +1000118 char *tmp, *ret = NULL;
Damien Millerea111192013-04-23 19:24:32 +1000119 size_t nlen, rlen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000120 const struct sshcipher *c;
Damien Millerea111192013-04-23 19:24:32 +1000121
122 for (c = ciphers; c->name != NULL; c++) {
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000123 if ((c->flags & CFLAG_INTERNAL) != 0)
Damien Millerea111192013-04-23 19:24:32 +1000124 continue;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100125 if (auth_only && c->auth_len == 0)
126 continue;
Damien Millerea111192013-04-23 19:24:32 +1000127 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100128 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000129 nlen = strlen(c->name);
Damien Miller86687062014-07-02 15:28:02 +1000130 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
131 free(ret);
132 return NULL;
133 }
134 ret = tmp;
Damien Millerea111192013-04-23 19:24:32 +1000135 memcpy(ret + rlen, c->name, nlen + 1);
136 rlen += nlen;
137 }
138 return ret;
139}
140
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000141u_int
Damien Miller86687062014-07-02 15:28:02 +1000142cipher_blocksize(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100143{
144 return (c->block_size);
145}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000146
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000147u_int
Damien Miller86687062014-07-02 15:28:02 +1000148cipher_keylen(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100149{
150 return (c->key_len);
151}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000152
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000153u_int
Damien Miller86687062014-07-02 15:28:02 +1000154cipher_seclen(const struct sshcipher *c)
Damien Miller76eea4a2014-01-26 09:37:25 +1100155{
156 if (strcmp("3des-cbc", c->name) == 0)
157 return 14;
158 return cipher_keylen(c);
159}
160
161u_int
Damien Miller86687062014-07-02 15:28:02 +1000162cipher_authlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100163{
164 return (c->auth_len);
165}
166
167u_int
Damien Miller86687062014-07-02 15:28:02 +1000168cipher_ivlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100169{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100170 /*
171 * Default is cipher block size, except for chacha20+poly1305 that
172 * needs no IV. XXX make iv_len == -1 default?
173 */
174 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
175 c->iv_len : c->block_size;
Damien Miller1d75abf2013-01-09 16:12:19 +1100176}
177
178u_int
Damien Miller86687062014-07-02 15:28:02 +1000179cipher_is_cbc(const struct sshcipher *c)
Damien Miller13ae44c2009-01-28 16:38:41 +1100180{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100181 return (c->flags & CFLAG_CBC) != 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100182}
183
184u_int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000185cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
186{
187 return cc->plaintext;
188}
189
Damien Miller86687062014-07-02 15:28:02 +1000190const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100191cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000192{
Damien Miller86687062014-07-02 15:28:02 +1000193 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100194 for (c = ciphers; c->name != NULL; c++)
Darren Tucker660db782005-01-24 21:57:11 +1100195 if (strcmp(c->name, name) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100196 return c;
197 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000198}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
Damien Miller78928792000-04-12 20:17:38 +1000200#define CIPHER_SEP ","
201int
202ciphers_valid(const char *names)
203{
Damien Miller86687062014-07-02 15:28:02 +1000204 const struct sshcipher *c;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000205 char *cipher_list, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000206 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000207
Damien Millerb1715dc2000-05-30 13:44:51 +1000208 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000209 return 0;
Damien Miller86687062014-07-02 15:28:02 +1000210 if ((cipher_list = cp = strdup(names)) == NULL)
211 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100212 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100213 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100214 c = cipher_by_name(p);
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000215 if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000216 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000217 return 0;
218 }
219 }
Darren Tuckera627d422013-06-02 07:31:17 +1000220 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000221 return 1;
222}
223
Damien Miller86687062014-07-02 15:28:02 +1000224const char *
225cipher_warning_message(const struct sshcipher_ctx *cc)
226{
227 if (cc == NULL || cc->cipher == NULL)
228 return NULL;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000229 /* XXX repurpose for CBC warning */
Damien Miller86687062014-07-02 15:28:02 +1000230 return NULL;
231}
232
233int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000234cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
Damien Miller21cf4e02002-02-19 15:26:42 +1100235 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000236 int do_encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100237{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000238 struct sshcipher_ctx *cc = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000239 int ret = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000240#ifdef WITH_OPENSSL
Damien Miller21cf4e02002-02-19 15:26:42 +1100241 const EVP_CIPHER *type;
242 int klen;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000243#endif
244
245 *ccp = NULL;
246 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
247 return SSH_ERR_ALLOC_FAIL;
Damien Miller21cf4e02002-02-19 15:26:42 +1100248
djm@openbsd.orge77e1562017-05-01 00:03:18 +0000249 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100250 cc->encrypt = do_encrypt;
Damien Miller21cf4e02002-02-19 15:26:42 +1100251
Damien Miller86687062014-07-02 15:28:02 +1000252 if (keylen < cipher->key_len ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000253 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
254 ret = SSH_ERR_INVALID_ARGUMENT;
255 goto out;
256 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100257
Damien Miller86687062014-07-02 15:28:02 +1000258 cc->cipher = cipher;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100259 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000260 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
261 goto out;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100262 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000263 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
264 ret = 0;
265 goto out;
266 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000267#ifndef WITH_OPENSSL
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);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000271 ret = 0;
272 goto out;
Damien Miller1f0311c2014-05-15 14:24:09 +1000273 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000274 ret = SSH_ERR_INVALID_ARGUMENT;
275 goto out;
276#else /* WITH_OPENSSL */
Damien Miller21cf4e02002-02-19 15:26:42 +1100277 type = (*cipher->evptype)();
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000278 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
279 ret = SSH_ERR_ALLOC_FAIL;
280 goto out;
281 }
282 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
Damien Miller86687062014-07-02 15:28:02 +1000283 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
284 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000285 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000286 }
Damien Miller1d75abf2013-01-09 16:12:19 +1100287 if (cipher_authlen(cipher) &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000288 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
Damien Miller86687062014-07-02 15:28:02 +1000289 -1, (u_char *)iv)) {
290 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000291 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000292 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000293 klen = EVP_CIPHER_CTX_key_length(cc->evp);
Damien Millereccb9de2005-06-17 12:59:34 +1000294 if (klen > 0 && keylen != (u_int)klen) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000295 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000296 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000297 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000298 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100299 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000300 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000301 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000302 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000303 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000304 ret = 0;
305#endif /* WITH_OPENSSL */
306 out:
307 if (ret == 0) {
308 /* success */
309 *ccp = cc;
310 } else {
311 if (cc != NULL) {
312#ifdef WITH_OPENSSL
313 if (cc->evp != NULL)
314 EVP_CIPHER_CTX_free(cc->evp);
315#endif /* WITH_OPENSSL */
316 explicit_bzero(cc, sizeof(*cc));
317 free(cc);
318 }
319 }
320 return ret;
Damien Miller874d77b2000-10-14 16:23:11 +1100321}
322
Damien Milleraf43a7a2012-12-12 10:46:31 +1100323/*
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'.
Damien Miller1d75abf2013-01-09 16:12:19 +1100329 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
330 * This tag is written on encryption and verified on decryption.
Damien Milleraf43a7a2012-12-12 10:46:31 +1100331 * Both 'aadlen' and 'authlen' can be set to 0.
332 */
Damien Millerbcd00ab2013-12-07 10:41:55 +1100333int
Damien Miller86687062014-07-02 15:28:02 +1000334cipher_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)
Damien Miller874d77b2000-10-14 16:23:11 +1100336{
Damien Miller86687062014-07-02 15:28:02 +1000337 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
338 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
339 len, aadlen, authlen, cc->encrypt);
340 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000341 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
342 memcpy(dest, src, aadlen + len);
343 return 0;
344 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000345#ifndef WITH_OPENSSL
346 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
347 if (aadlen)
348 memcpy(dest, src, aadlen);
349 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
350 dest + aadlen, len);
351 return 0;
352 }
Damien Miller86687062014-07-02 15:28:02 +1000353 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1f0311c2014-05-15 14:24:09 +1000354#else
Damien Miller1d75abf2013-01-09 16:12:19 +1100355 if (authlen) {
356 u_char lastiv[1];
357
358 if (authlen != cipher_authlen(cc->cipher))
Damien Miller86687062014-07-02 15:28:02 +1000359 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1d75abf2013-01-09 16:12:19 +1100360 /* increment IV */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000361 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Damien Miller1d75abf2013-01-09 16:12:19 +1100362 1, lastiv))
Damien Miller86687062014-07-02 15:28:02 +1000363 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100364 /* set tag on decyption */
365 if (!cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000366 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100367 authlen, (u_char *)src + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000368 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100369 }
370 if (aadlen) {
371 if (authlen &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000372 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000373 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100374 memcpy(dest, src, aadlen);
Damien Miller1d75abf2013-01-09 16:12:19 +1100375 }
Damien Miller874d77b2000-10-14 16:23:11 +1100376 if (len % cc->cipher->block_size)
Damien Miller86687062014-07-02 15:28:02 +1000377 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000378 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100379 len) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000380 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100381 if (authlen) {
382 /* compute tag (on encrypt) or verify tag (on decrypt) */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000383 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000384 return cc->encrypt ?
385 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
Damien Miller1d75abf2013-01-09 16:12:19 +1100386 if (cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000387 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100388 authlen, dest + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000389 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100390 }
Damien Millerbcd00ab2013-12-07 10:41:55 +1100391 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000392#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100393}
394
Damien Miller0fde8ac2013-11-21 14:12:23 +1100395/* Extract the packet length, including any decryption necessary beforehand */
396int
Damien Miller86687062014-07-02 15:28:02 +1000397cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100398 const u_char *cp, u_int len)
399{
400 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
401 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
402 cp, len);
403 if (len < 4)
Damien Miller86687062014-07-02 15:28:02 +1000404 return SSH_ERR_MESSAGE_INCOMPLETE;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100405 *plenp = get_u32(cp);
406 return 0;
407}
408
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000409void
410cipher_free(struct sshcipher_ctx *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100411{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000412 if (cc == NULL)
413 return;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100414 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
Damien Millera5103f42014-02-04 11:20:14 +1100415 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
Damien Miller1f0311c2014-05-15 14:24:09 +1000416 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
417 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
418#ifdef WITH_OPENSSL
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000419 if (cc->evp != NULL) {
420 EVP_CIPHER_CTX_free(cc->evp);
421 cc->evp = NULL;
422 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000423#endif
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000424 explicit_bzero(cc, sizeof(*cc));
425 free(cc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426}
427
Damien Miller5428f641999-11-25 11:54:57 +1100428/*
Damien Miller86687062014-07-02 15:28:02 +1000429 * Exports an IV from the sshcipher_ctx required to export the key
Ben Lindstrom212faca2002-03-22 01:39:44 +0000430 * state back from the unprivileged child to the privileged parent
431 * process.
432 */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000433int
Damien Miller86687062014-07-02 15:28:02 +1000434cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000435{
Damien Miller86687062014-07-02 15:28:02 +1000436 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000437
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000438 if ((c->flags & CFLAG_CHACHAPOLY) != 0)
439 return 0;
440 else if ((c->flags & CFLAG_AESCTR) != 0)
441 return sizeof(cc->ac_ctx.ctr);
Damien Miller1f0311c2014-05-15 14:24:09 +1000442#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000443 return EVP_CIPHER_CTX_iv_length(cc->evp);
444#else
445 return 0;
446#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000447}
448
Damien Miller86687062014-07-02 15:28:02 +1000449int
450cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000451{
Damien Miller86687062014-07-02 15:28:02 +1000452 const struct sshcipher *c = cc->cipher;
Damien Miller1f0311c2014-05-15 14:24:09 +1000453#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000454 int evplen;
Damien Miller1f0311c2014-05-15 14:24:09 +1000455#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000456
Damien Miller0fde8ac2013-11-21 14:12:23 +1100457 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
458 if (len != 0)
Damien Miller86687062014-07-02 15:28:02 +1000459 return SSH_ERR_INVALID_ARGUMENT;
460 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100461 }
djm@openbsd.org540e8912015-01-14 10:29:45 +0000462 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);
466 return 0;
467 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000468 if ((cc->cipher->flags & CFLAG_NONE) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000469 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100470
Damien Miller1f0311c2014-05-15 14:24:09 +1000471#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000472 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
473 if (evplen == 0)
474 return 0;
475 else if (evplen < 0)
476 return SSH_ERR_LIBCRYPTO_ERROR;
477 if ((u_int)evplen != len)
Damien Miller86687062014-07-02 15:28:02 +1000478 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000479#ifndef OPENSSL_HAVE_EVPCTR
480 if (c->evptype == evp_aes_128_ctr)
481 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
482 else
483#endif
484 if (cipher_authlen(c)) {
485 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
486 len, iv))
487 return SSH_ERR_LIBCRYPTO_ERROR;
488 } else
489 memcpy(iv, cc->evp->iv, len);
490#endif
Damien Miller86687062014-07-02 15:28:02 +1000491 return 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000492}
493
Ben Lindstrom212faca2002-03-22 01:39:44 +0000494int
Damien Miller86687062014-07-02 15:28:02 +1000495cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
496{
497 const struct sshcipher *c = cc->cipher;
498#ifdef WITH_OPENSSL
499 int evplen = 0;
500#endif
501
502 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
503 return 0;
504 if ((cc->cipher->flags & CFLAG_NONE) != 0)
505 return 0;
506
Damien Miller86687062014-07-02 15:28:02 +1000507#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000508 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
509 if (evplen <= 0)
510 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller315d2a42016-10-28 14:34:07 +1100511#ifndef OPENSSL_HAVE_EVPCTR
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000512 /* 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);
515 else
Damien Miller315d2a42016-10-28 14:34:07 +1100516#endif
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000517 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;
522 } else
523 memcpy(cc->evp->iv, iv, evplen);
Damien Miller86687062014-07-02 15:28:02 +1000524#endif
Damien Miller86687062014-07-02 15:28:02 +1000525 return 0;
526}
527
528#ifdef WITH_OPENSSL
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000529#define EVP_X_STATE(evp) (evp)->cipher_data
530#define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
Damien Miller86687062014-07-02 15:28:02 +1000531#endif
532
533int
534cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000535{
Damien Miller832b7442016-07-15 14:45:34 +1000536#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
Damien Miller86687062014-07-02 15:28:02 +1000537 const struct sshcipher *c = cc->cipher;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000538 int plen = 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000539
Damien Millerf0a8ded2013-02-12 11:00:34 +1100540 if (c->evptype == EVP_rc4) {
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000541 plen = EVP_X_STATE_LEN(cc->evp);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000542 if (dat == NULL)
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000543 return (plen);
544 memcpy(dat, EVP_X_STATE(cc->evp), plen);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000545 }
Ben Lindstrom212faca2002-03-22 01:39:44 +0000546 return (plen);
Damien Miller1f0311c2014-05-15 14:24:09 +1000547#else
Damien Miller86687062014-07-02 15:28:02 +1000548 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000549#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000550}
551
552void
Damien Miller86687062014-07-02 15:28:02 +1000553cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000554{
Damien Miller832b7442016-07-15 14:45:34 +1000555#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
Damien Miller86687062014-07-02 15:28:02 +1000556 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000557 int plen;
558
Damien Millerf0a8ded2013-02-12 11:00:34 +1100559 if (c->evptype == EVP_rc4) {
Ben Lindstrom212faca2002-03-22 01:39:44 +0000560 plen = EVP_X_STATE_LEN(cc->evp);
561 memcpy(EVP_X_STATE(cc->evp), dat, plen);
562 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000563#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100564}