blob: 12c5988816cf4beaffb2d026b642a427146b2c0f [file] [log] [blame]
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001/* $OpenBSD: cipher.c,v 1.111 2018/02/23 15:58:37 markus 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
Darren Tuckercec33892018-04-19 09:53:14 +100085#ifndef OPENSSL_NO_DES
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000086 { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
Darren Tuckercec33892018-04-19 09:53:14 +100087#endif
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000088 { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
89 { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
90 { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
Damien Miller1d75abf2013-01-09 16:12:19 +110091 { "rijndael-cbc@lysator.liu.se",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000092 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
93 { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr },
94 { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr },
95 { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr },
Damien Miller86687062014-07-02 15:28:02 +100096# ifdef OPENSSL_HAVE_EVPGCM
Damien Miller1d75abf2013-01-09 16:12:19 +110097 { "aes128-gcm@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000098 16, 16, 12, 16, 0, EVP_aes_128_gcm },
Damien Miller1d75abf2013-01-09 16:12:19 +110099 { "aes256-gcm@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000100 16, 32, 12, 16, 0, EVP_aes_256_gcm },
Damien Miller86687062014-07-02 15:28:02 +1000101# endif /* OPENSSL_HAVE_EVPGCM */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000102#else
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000103 { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR, NULL },
104 { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR, NULL },
105 { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR, NULL },
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000106#endif
Damien Miller0fde8ac2013-11-21 14:12:23 +1100107 { "chacha20-poly1305@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000108 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
109 { "none", 8, 0, 0, 0, CFLAG_NONE, NULL },
Damien Miller86687062014-07-02 15:28:02 +1000110
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000111 { NULL, 0, 0, 0, 0, 0, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112};
113
Damien Miller874d77b2000-10-14 16:23:11 +1100114/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller86687062014-07-02 15:28:02 +1000116/* Returns a comma-separated list of supported ciphers. */
Damien Millerea111192013-04-23 19:24:32 +1000117char *
Damien Miller0fde8ac2013-11-21 14:12:23 +1100118cipher_alg_list(char sep, int auth_only)
Damien Millerea111192013-04-23 19:24:32 +1000119{
Damien Miller86687062014-07-02 15:28:02 +1000120 char *tmp, *ret = NULL;
Damien Millerea111192013-04-23 19:24:32 +1000121 size_t nlen, rlen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000122 const struct sshcipher *c;
Damien Millerea111192013-04-23 19:24:32 +1000123
124 for (c = ciphers; c->name != NULL; c++) {
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000125 if ((c->flags & CFLAG_INTERNAL) != 0)
Damien Millerea111192013-04-23 19:24:32 +1000126 continue;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100127 if (auth_only && c->auth_len == 0)
128 continue;
Damien Millerea111192013-04-23 19:24:32 +1000129 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100130 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000131 nlen = strlen(c->name);
Damien Miller86687062014-07-02 15:28:02 +1000132 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
133 free(ret);
134 return NULL;
135 }
136 ret = tmp;
Damien Millerea111192013-04-23 19:24:32 +1000137 memcpy(ret + rlen, c->name, nlen + 1);
138 rlen += nlen;
139 }
140 return ret;
141}
142
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000143u_int
Damien Miller86687062014-07-02 15:28:02 +1000144cipher_blocksize(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100145{
146 return (c->block_size);
147}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000148
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000149u_int
Damien Miller86687062014-07-02 15:28:02 +1000150cipher_keylen(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100151{
152 return (c->key_len);
153}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000154
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000155u_int
Damien Miller86687062014-07-02 15:28:02 +1000156cipher_seclen(const struct sshcipher *c)
Damien Miller76eea4a2014-01-26 09:37:25 +1100157{
158 if (strcmp("3des-cbc", c->name) == 0)
159 return 14;
160 return cipher_keylen(c);
161}
162
163u_int
Damien Miller86687062014-07-02 15:28:02 +1000164cipher_authlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100165{
166 return (c->auth_len);
167}
168
169u_int
Damien Miller86687062014-07-02 15:28:02 +1000170cipher_ivlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100171{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100172 /*
173 * Default is cipher block size, except for chacha20+poly1305 that
174 * needs no IV. XXX make iv_len == -1 default?
175 */
176 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
177 c->iv_len : c->block_size;
Damien Miller1d75abf2013-01-09 16:12:19 +1100178}
179
180u_int
Damien Miller86687062014-07-02 15:28:02 +1000181cipher_is_cbc(const struct sshcipher *c)
Damien Miller13ae44c2009-01-28 16:38:41 +1100182{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100183 return (c->flags & CFLAG_CBC) != 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100184}
185
186u_int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000187cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
188{
189 return cc->plaintext;
190}
191
Damien Miller86687062014-07-02 15:28:02 +1000192const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100193cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000194{
Damien Miller86687062014-07-02 15:28:02 +1000195 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100196 for (c = ciphers; c->name != NULL; c++)
Darren Tucker660db782005-01-24 21:57:11 +1100197 if (strcmp(c->name, name) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100198 return c;
199 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000200}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201
Damien Miller78928792000-04-12 20:17:38 +1000202#define CIPHER_SEP ","
203int
204ciphers_valid(const char *names)
205{
Damien Miller86687062014-07-02 15:28:02 +1000206 const struct sshcipher *c;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000207 char *cipher_list, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000208 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000209
Damien Millerb1715dc2000-05-30 13:44:51 +1000210 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000211 return 0;
Damien Miller86687062014-07-02 15:28:02 +1000212 if ((cipher_list = cp = strdup(names)) == NULL)
213 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100214 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100215 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100216 c = cipher_by_name(p);
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000217 if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000218 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000219 return 0;
220 }
221 }
Darren Tuckera627d422013-06-02 07:31:17 +1000222 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000223 return 1;
224}
225
Damien Miller86687062014-07-02 15:28:02 +1000226const char *
227cipher_warning_message(const struct sshcipher_ctx *cc)
228{
229 if (cc == NULL || cc->cipher == NULL)
230 return NULL;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000231 /* XXX repurpose for CBC warning */
Damien Miller86687062014-07-02 15:28:02 +1000232 return NULL;
233}
234
235int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000236cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
Damien Miller21cf4e02002-02-19 15:26:42 +1100237 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000238 int do_encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100239{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000240 struct sshcipher_ctx *cc = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000241 int ret = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000242#ifdef WITH_OPENSSL
Damien Miller21cf4e02002-02-19 15:26:42 +1100243 const EVP_CIPHER *type;
244 int klen;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000245#endif
246
247 *ccp = NULL;
248 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
249 return SSH_ERR_ALLOC_FAIL;
Damien Miller21cf4e02002-02-19 15:26:42 +1100250
djm@openbsd.orge77e1562017-05-01 00:03:18 +0000251 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100252 cc->encrypt = do_encrypt;
Damien Miller21cf4e02002-02-19 15:26:42 +1100253
Damien Miller86687062014-07-02 15:28:02 +1000254 if (keylen < cipher->key_len ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000255 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
256 ret = SSH_ERR_INVALID_ARGUMENT;
257 goto out;
258 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100259
Damien Miller86687062014-07-02 15:28:02 +1000260 cc->cipher = cipher;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100261 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000262 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
263 goto out;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100264 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000265 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
266 ret = 0;
267 goto out;
268 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000269#ifndef WITH_OPENSSL
270 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
271 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
272 aesctr_ivsetup(&cc->ac_ctx, iv);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000273 ret = 0;
274 goto out;
Damien Miller1f0311c2014-05-15 14:24:09 +1000275 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000276 ret = SSH_ERR_INVALID_ARGUMENT;
277 goto out;
278#else /* WITH_OPENSSL */
Damien Miller21cf4e02002-02-19 15:26:42 +1100279 type = (*cipher->evptype)();
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000280 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
281 ret = SSH_ERR_ALLOC_FAIL;
282 goto out;
283 }
284 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
Damien Miller86687062014-07-02 15:28:02 +1000285 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
286 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000287 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000288 }
Damien Miller1d75abf2013-01-09 16:12:19 +1100289 if (cipher_authlen(cipher) &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000290 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
Damien Miller86687062014-07-02 15:28:02 +1000291 -1, (u_char *)iv)) {
292 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000293 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000294 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000295 klen = EVP_CIPHER_CTX_key_length(cc->evp);
Damien Millereccb9de2005-06-17 12:59:34 +1000296 if (klen > 0 && keylen != (u_int)klen) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000297 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000298 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000299 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000300 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100301 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000302 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000303 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000304 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000305 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000306 ret = 0;
307#endif /* WITH_OPENSSL */
308 out:
309 if (ret == 0) {
310 /* success */
311 *ccp = cc;
312 } else {
313 if (cc != NULL) {
314#ifdef WITH_OPENSSL
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000315 EVP_CIPHER_CTX_free(cc->evp);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000316#endif /* WITH_OPENSSL */
317 explicit_bzero(cc, sizeof(*cc));
318 free(cc);
319 }
320 }
321 return ret;
Damien Miller874d77b2000-10-14 16:23:11 +1100322}
323
Damien Milleraf43a7a2012-12-12 10:46:31 +1100324/*
325 * cipher_crypt() operates as following:
326 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
327 * Theses bytes are treated as additional authenticated data for
328 * authenticated encryption modes.
329 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
Damien Miller1d75abf2013-01-09 16:12:19 +1100330 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
331 * This tag is written on encryption and verified on decryption.
Damien Milleraf43a7a2012-12-12 10:46:31 +1100332 * Both 'aadlen' and 'authlen' can be set to 0.
333 */
Damien Millerbcd00ab2013-12-07 10:41:55 +1100334int
Damien Miller86687062014-07-02 15:28:02 +1000335cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
336 const u_char *src, u_int len, u_int aadlen, u_int authlen)
Damien Miller874d77b2000-10-14 16:23:11 +1100337{
Damien Miller86687062014-07-02 15:28:02 +1000338 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
339 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
340 len, aadlen, authlen, cc->encrypt);
341 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000342 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
343 memcpy(dest, src, aadlen + len);
344 return 0;
345 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000346#ifndef WITH_OPENSSL
347 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
348 if (aadlen)
349 memcpy(dest, src, aadlen);
350 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
351 dest + aadlen, len);
352 return 0;
353 }
Damien Miller86687062014-07-02 15:28:02 +1000354 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1f0311c2014-05-15 14:24:09 +1000355#else
Damien Miller1d75abf2013-01-09 16:12:19 +1100356 if (authlen) {
357 u_char lastiv[1];
358
359 if (authlen != cipher_authlen(cc->cipher))
Damien Miller86687062014-07-02 15:28:02 +1000360 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1d75abf2013-01-09 16:12:19 +1100361 /* increment IV */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000362 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Damien Miller1d75abf2013-01-09 16:12:19 +1100363 1, lastiv))
Damien Miller86687062014-07-02 15:28:02 +1000364 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100365 /* set tag on decyption */
366 if (!cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000367 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100368 authlen, (u_char *)src + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000369 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100370 }
371 if (aadlen) {
372 if (authlen &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000373 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000374 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100375 memcpy(dest, src, aadlen);
Damien Miller1d75abf2013-01-09 16:12:19 +1100376 }
Damien Miller874d77b2000-10-14 16:23:11 +1100377 if (len % cc->cipher->block_size)
Damien Miller86687062014-07-02 15:28:02 +1000378 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000379 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100380 len) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000381 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100382 if (authlen) {
383 /* compute tag (on encrypt) or verify tag (on decrypt) */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000384 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000385 return cc->encrypt ?
386 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
Damien Miller1d75abf2013-01-09 16:12:19 +1100387 if (cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000388 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100389 authlen, dest + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000390 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100391 }
Damien Millerbcd00ab2013-12-07 10:41:55 +1100392 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000393#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100394}
395
Damien Miller0fde8ac2013-11-21 14:12:23 +1100396/* Extract the packet length, including any decryption necessary beforehand */
397int
Damien Miller86687062014-07-02 15:28:02 +1000398cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100399 const u_char *cp, u_int len)
400{
401 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
402 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
403 cp, len);
404 if (len < 4)
Damien Miller86687062014-07-02 15:28:02 +1000405 return SSH_ERR_MESSAGE_INCOMPLETE;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000406 *plenp = PEEK_U32(cp);
Damien Miller0fde8ac2013-11-21 14:12:23 +1100407 return 0;
408}
409
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000410void
411cipher_free(struct sshcipher_ctx *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100412{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000413 if (cc == NULL)
414 return;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100415 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
Damien Millera5103f42014-02-04 11:20:14 +1100416 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
Damien Miller1f0311c2014-05-15 14:24:09 +1000417 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
418 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
419#ifdef WITH_OPENSSL
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000420 EVP_CIPHER_CTX_free(cc->evp);
421 cc->evp = NULL;
Damien Miller1f0311c2014-05-15 14:24:09 +1000422#endif
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000423 explicit_bzero(cc, sizeof(*cc));
424 free(cc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425}
426
Damien Miller5428f641999-11-25 11:54:57 +1100427/*
Damien Miller86687062014-07-02 15:28:02 +1000428 * Exports an IV from the sshcipher_ctx required to export the key
Ben Lindstrom212faca2002-03-22 01:39:44 +0000429 * state back from the unprivileged child to the privileged parent
430 * process.
431 */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000432int
Damien Miller86687062014-07-02 15:28:02 +1000433cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000434{
Damien Miller86687062014-07-02 15:28:02 +1000435 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000436
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000437 if ((c->flags & CFLAG_CHACHAPOLY) != 0)
438 return 0;
439 else if ((c->flags & CFLAG_AESCTR) != 0)
440 return sizeof(cc->ac_ctx.ctr);
Damien Miller1f0311c2014-05-15 14:24:09 +1000441#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000442 return EVP_CIPHER_CTX_iv_length(cc->evp);
443#else
444 return 0;
445#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000446}
447
Damien Miller86687062014-07-02 15:28:02 +1000448int
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000449cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000450{
Damien Miller1f0311c2014-05-15 14:24:09 +1000451#ifdef WITH_OPENSSL
djm@openbsd.org@openbsd.org41bff4d2017-11-03 02:22:41 +0000452 const struct sshcipher *c = cc->cipher;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +0000453 int evplen;
Damien Miller1f0311c2014-05-15 14:24:09 +1000454#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000455
Damien Miller0fde8ac2013-11-21 14:12:23 +1100456 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
457 if (len != 0)
Damien Miller86687062014-07-02 15:28:02 +1000458 return SSH_ERR_INVALID_ARGUMENT;
459 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100460 }
djm@openbsd.org540e8912015-01-14 10:29:45 +0000461 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
462 if (len != sizeof(cc->ac_ctx.ctr))
463 return SSH_ERR_INVALID_ARGUMENT;
464 memcpy(iv, cc->ac_ctx.ctr, len);
465 return 0;
466 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000467 if ((cc->cipher->flags & CFLAG_NONE) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000468 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100469
Damien Miller1f0311c2014-05-15 14:24:09 +1000470#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000471 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
472 if (evplen == 0)
473 return 0;
474 else if (evplen < 0)
475 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000476 if ((size_t)evplen != len)
Damien Miller86687062014-07-02 15:28:02 +1000477 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000478#ifndef OPENSSL_HAVE_EVPCTR
479 if (c->evptype == evp_aes_128_ctr)
480 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
481 else
482#endif
483 if (cipher_authlen(c)) {
484 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
485 len, iv))
486 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000487 } else if (!EVP_CIPHER_CTX_get_iv(cc->evp, iv, len))
488 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000489#endif
Damien Miller86687062014-07-02 15:28:02 +1000490 return 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000491}
492
Ben Lindstrom212faca2002-03-22 01:39:44 +0000493int
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000494cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
Damien Miller86687062014-07-02 15:28:02 +1000495{
Damien Miller86687062014-07-02 15:28:02 +1000496#ifdef WITH_OPENSSL
djm@openbsd.org@openbsd.org41bff4d2017-11-03 02:22:41 +0000497 const struct sshcipher *c = cc->cipher;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +0000498 int evplen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000499#endif
500
501 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
502 return 0;
503 if ((cc->cipher->flags & CFLAG_NONE) != 0)
504 return 0;
505
Damien Miller86687062014-07-02 15:28:02 +1000506#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000507 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
508 if (evplen <= 0)
509 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000510 if ((size_t)evplen != len)
511 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller315d2a42016-10-28 14:34:07 +1100512#ifndef OPENSSL_HAVE_EVPCTR
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000513 /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
514 if (c->evptype == evp_aes_128_ctr)
515 ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
516 else
Damien Miller315d2a42016-10-28 14:34:07 +1100517#endif
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000518 if (cipher_authlen(c)) {
519 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
520 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
521 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
522 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000523 } else if (!EVP_CIPHER_CTX_set_iv(cc->evp, iv, evplen))
524 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller86687062014-07-02 15:28:02 +1000525#endif
Damien Miller86687062014-07-02 15:28:02 +1000526 return 0;
527}