blob: 820bc6ace3e8c80e79a8ac3f617ab74f9542b4d7 [file] [log] [blame]
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +00001/* $OpenBSD: cipher.c,v 1.114 2020/01/23 10:24:29 dtucker 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
djm@openbsd.org670104b2019-09-06 05:23:55 +000054#ifndef WITH_OPENSSL
55#define EVP_CIPHER_CTX void
56#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
djm@openbsd.org4706c1d2016-08-03 05:41:57 +000058struct sshcipher_ctx {
59 int plaintext;
60 int encrypt;
61 EVP_CIPHER_CTX *evp;
62 struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
63 struct aesctr_ctx ac_ctx; /* XXX union with evp? */
64 const struct sshcipher *cipher;
65};
66
Damien Miller86687062014-07-02 15:28:02 +100067struct sshcipher {
Damien Miller963f6b22002-02-19 15:21:23 +110068 char *name;
Damien Miller963f6b22002-02-19 15:21:23 +110069 u_int block_size;
70 u_int key_len;
Damien Millerd522c682013-01-09 16:42:47 +110071 u_int iv_len; /* defaults to block_size */
72 u_int auth_len;
Damien Miller0fde8ac2013-11-21 14:12:23 +110073 u_int flags;
74#define CFLAG_CBC (1<<0)
75#define CFLAG_CHACHAPOLY (1<<1)
Damien Miller1f0311c2014-05-15 14:24:09 +100076#define CFLAG_AESCTR (1<<2)
77#define CFLAG_NONE (1<<3)
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +000078#define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */
Damien Miller1f0311c2014-05-15 14:24:09 +100079#ifdef WITH_OPENSSL
Ben Lindstrom6a246412002-06-06 19:48:16 +000080 const EVP_CIPHER *(*evptype)(void);
Damien Miller1f0311c2014-05-15 14:24:09 +100081#else
82 void *ignored;
83#endif
Damien Millerea111192013-04-23 19:24:32 +100084};
85
Damien Miller86687062014-07-02 15:28:02 +100086static const struct sshcipher ciphers[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +100087#ifdef WITH_OPENSSL
Darren Tuckercec33892018-04-19 09:53:14 +100088#ifndef OPENSSL_NO_DES
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000089 { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
Darren Tuckercec33892018-04-19 09:53:14 +100090#endif
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000091 { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
92 { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
93 { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
Damien Miller1d75abf2013-01-09 16:12:19 +110094 { "rijndael-cbc@lysator.liu.se",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +000095 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
96 { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr },
97 { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr },
98 { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr },
Damien Miller86687062014-07-02 15:28:02 +100099# ifdef OPENSSL_HAVE_EVPGCM
Damien Miller1d75abf2013-01-09 16:12:19 +1100100 { "aes128-gcm@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000101 16, 16, 12, 16, 0, EVP_aes_128_gcm },
Damien Miller1d75abf2013-01-09 16:12:19 +1100102 { "aes256-gcm@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000103 16, 32, 12, 16, 0, EVP_aes_256_gcm },
Damien Miller86687062014-07-02 15:28:02 +1000104# endif /* OPENSSL_HAVE_EVPGCM */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000105#else
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000106 { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR, NULL },
107 { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR, NULL },
108 { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR, NULL },
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000109#endif
Damien Miller0fde8ac2013-11-21 14:12:23 +1100110 { "chacha20-poly1305@openssh.com",
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000111 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
112 { "none", 8, 0, 0, 0, CFLAG_NONE, NULL },
Damien Miller86687062014-07-02 15:28:02 +1000113
djm@openbsd.orgacaf34f2017-05-07 23:12:57 +0000114 { NULL, 0, 0, 0, 0, 0, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115};
116
Damien Miller874d77b2000-10-14 16:23:11 +1100117/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Damien Miller86687062014-07-02 15:28:02 +1000119/* Returns a comma-separated list of supported ciphers. */
Damien Millerea111192013-04-23 19:24:32 +1000120char *
Damien Miller0fde8ac2013-11-21 14:12:23 +1100121cipher_alg_list(char sep, int auth_only)
Damien Millerea111192013-04-23 19:24:32 +1000122{
Damien Miller86687062014-07-02 15:28:02 +1000123 char *tmp, *ret = NULL;
Damien Millerea111192013-04-23 19:24:32 +1000124 size_t nlen, rlen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000125 const struct sshcipher *c;
Damien Millerea111192013-04-23 19:24:32 +1000126
127 for (c = ciphers; c->name != NULL; c++) {
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000128 if ((c->flags & CFLAG_INTERNAL) != 0)
Damien Millerea111192013-04-23 19:24:32 +1000129 continue;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100130 if (auth_only && c->auth_len == 0)
131 continue;
Damien Millerea111192013-04-23 19:24:32 +1000132 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100133 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000134 nlen = strlen(c->name);
Damien Miller86687062014-07-02 15:28:02 +1000135 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
136 free(ret);
137 return NULL;
138 }
139 ret = tmp;
Damien Millerea111192013-04-23 19:24:32 +1000140 memcpy(ret + rlen, c->name, nlen + 1);
141 rlen += nlen;
142 }
143 return ret;
144}
145
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000146const char *
147compression_alg_list(int compression)
148{
149#ifdef WITH_ZLIB
150 return compression ? "zlib@openssh.com,zlib,none" :
151 "none,zlib@openssh.com,zlib";
152#else
153 return "none";
154#endif
155}
156
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000157u_int
Damien Miller86687062014-07-02 15:28:02 +1000158cipher_blocksize(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100159{
160 return (c->block_size);
161}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000162
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000163u_int
Damien Miller86687062014-07-02 15:28:02 +1000164cipher_keylen(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100165{
166 return (c->key_len);
167}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000168
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000169u_int
Damien Miller86687062014-07-02 15:28:02 +1000170cipher_seclen(const struct sshcipher *c)
Damien Miller76eea4a2014-01-26 09:37:25 +1100171{
172 if (strcmp("3des-cbc", c->name) == 0)
173 return 14;
174 return cipher_keylen(c);
175}
176
177u_int
Damien Miller86687062014-07-02 15:28:02 +1000178cipher_authlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100179{
180 return (c->auth_len);
181}
182
183u_int
Damien Miller86687062014-07-02 15:28:02 +1000184cipher_ivlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100185{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100186 /*
187 * Default is cipher block size, except for chacha20+poly1305 that
188 * needs no IV. XXX make iv_len == -1 default?
189 */
190 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
191 c->iv_len : c->block_size;
Damien Miller1d75abf2013-01-09 16:12:19 +1100192}
193
194u_int
Damien Miller86687062014-07-02 15:28:02 +1000195cipher_is_cbc(const struct sshcipher *c)
Damien Miller13ae44c2009-01-28 16:38:41 +1100196{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100197 return (c->flags & CFLAG_CBC) != 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100198}
199
200u_int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000201cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
202{
203 return cc->plaintext;
204}
205
Damien Miller86687062014-07-02 15:28:02 +1000206const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100207cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000208{
Damien Miller86687062014-07-02 15:28:02 +1000209 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100210 for (c = ciphers; c->name != NULL; c++)
Darren Tucker660db782005-01-24 21:57:11 +1100211 if (strcmp(c->name, name) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100212 return c;
213 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000214}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller78928792000-04-12 20:17:38 +1000216#define CIPHER_SEP ","
217int
218ciphers_valid(const char *names)
219{
Damien Miller86687062014-07-02 15:28:02 +1000220 const struct sshcipher *c;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000221 char *cipher_list, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000222 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000223
Damien Millerb1715dc2000-05-30 13:44:51 +1000224 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000225 return 0;
Damien Miller86687062014-07-02 15:28:02 +1000226 if ((cipher_list = cp = strdup(names)) == NULL)
227 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100228 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100229 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100230 c = cipher_by_name(p);
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000231 if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000232 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000233 return 0;
234 }
235 }
Darren Tuckera627d422013-06-02 07:31:17 +1000236 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000237 return 1;
238}
239
Damien Miller86687062014-07-02 15:28:02 +1000240const char *
241cipher_warning_message(const struct sshcipher_ctx *cc)
242{
243 if (cc == NULL || cc->cipher == NULL)
244 return NULL;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000245 /* XXX repurpose for CBC warning */
Damien Miller86687062014-07-02 15:28:02 +1000246 return NULL;
247}
248
249int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000250cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
Damien Miller21cf4e02002-02-19 15:26:42 +1100251 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000252 int do_encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100253{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000254 struct sshcipher_ctx *cc = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000255 int ret = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000256#ifdef WITH_OPENSSL
Damien Miller21cf4e02002-02-19 15:26:42 +1100257 const EVP_CIPHER *type;
258 int klen;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000259#endif
260
261 *ccp = NULL;
262 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
263 return SSH_ERR_ALLOC_FAIL;
Damien Miller21cf4e02002-02-19 15:26:42 +1100264
djm@openbsd.orge77e1562017-05-01 00:03:18 +0000265 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100266 cc->encrypt = do_encrypt;
Damien Miller21cf4e02002-02-19 15:26:42 +1100267
Damien Miller86687062014-07-02 15:28:02 +1000268 if (keylen < cipher->key_len ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000269 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
270 ret = SSH_ERR_INVALID_ARGUMENT;
271 goto out;
272 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100273
Damien Miller86687062014-07-02 15:28:02 +1000274 cc->cipher = cipher;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100275 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000276 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
277 goto out;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100278 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000279 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
280 ret = 0;
281 goto out;
282 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000283#ifndef WITH_OPENSSL
284 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
285 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
286 aesctr_ivsetup(&cc->ac_ctx, iv);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000287 ret = 0;
288 goto out;
Damien Miller1f0311c2014-05-15 14:24:09 +1000289 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000290 ret = SSH_ERR_INVALID_ARGUMENT;
291 goto out;
292#else /* WITH_OPENSSL */
Damien Miller21cf4e02002-02-19 15:26:42 +1100293 type = (*cipher->evptype)();
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000294 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
295 ret = SSH_ERR_ALLOC_FAIL;
296 goto out;
297 }
298 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
Damien Miller86687062014-07-02 15:28:02 +1000299 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
300 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000301 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000302 }
Damien Miller1d75abf2013-01-09 16:12:19 +1100303 if (cipher_authlen(cipher) &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000304 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
Damien Miller86687062014-07-02 15:28:02 +1000305 -1, (u_char *)iv)) {
306 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000307 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000308 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000309 klen = EVP_CIPHER_CTX_key_length(cc->evp);
Damien Millereccb9de2005-06-17 12:59:34 +1000310 if (klen > 0 && keylen != (u_int)klen) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000311 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000312 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000313 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000314 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100315 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000316 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000317 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000318 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000319 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000320 ret = 0;
321#endif /* WITH_OPENSSL */
322 out:
323 if (ret == 0) {
324 /* success */
325 *ccp = cc;
326 } else {
327 if (cc != NULL) {
328#ifdef WITH_OPENSSL
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000329 EVP_CIPHER_CTX_free(cc->evp);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000330#endif /* WITH_OPENSSL */
331 explicit_bzero(cc, sizeof(*cc));
332 free(cc);
333 }
334 }
335 return ret;
Damien Miller874d77b2000-10-14 16:23:11 +1100336}
337
Damien Milleraf43a7a2012-12-12 10:46:31 +1100338/*
339 * cipher_crypt() operates as following:
340 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
341 * Theses bytes are treated as additional authenticated data for
342 * authenticated encryption modes.
343 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
Damien Miller1d75abf2013-01-09 16:12:19 +1100344 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
345 * This tag is written on encryption and verified on decryption.
Damien Milleraf43a7a2012-12-12 10:46:31 +1100346 * Both 'aadlen' and 'authlen' can be set to 0.
347 */
Damien Millerbcd00ab2013-12-07 10:41:55 +1100348int
Damien Miller86687062014-07-02 15:28:02 +1000349cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
350 const u_char *src, u_int len, u_int aadlen, u_int authlen)
Damien Miller874d77b2000-10-14 16:23:11 +1100351{
Damien Miller86687062014-07-02 15:28:02 +1000352 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
353 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
354 len, aadlen, authlen, cc->encrypt);
355 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000356 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
357 memcpy(dest, src, aadlen + len);
358 return 0;
359 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000360#ifndef WITH_OPENSSL
361 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
362 if (aadlen)
363 memcpy(dest, src, aadlen);
364 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
365 dest + aadlen, len);
366 return 0;
367 }
Damien Miller86687062014-07-02 15:28:02 +1000368 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1f0311c2014-05-15 14:24:09 +1000369#else
Damien Miller1d75abf2013-01-09 16:12:19 +1100370 if (authlen) {
371 u_char lastiv[1];
372
373 if (authlen != cipher_authlen(cc->cipher))
Damien Miller86687062014-07-02 15:28:02 +1000374 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1d75abf2013-01-09 16:12:19 +1100375 /* increment IV */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000376 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Damien Miller1d75abf2013-01-09 16:12:19 +1100377 1, lastiv))
Damien Miller86687062014-07-02 15:28:02 +1000378 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100379 /* set tag on decyption */
380 if (!cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000381 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100382 authlen, (u_char *)src + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000383 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100384 }
385 if (aadlen) {
386 if (authlen &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000387 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000388 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100389 memcpy(dest, src, aadlen);
Damien Miller1d75abf2013-01-09 16:12:19 +1100390 }
Damien Miller874d77b2000-10-14 16:23:11 +1100391 if (len % cc->cipher->block_size)
Damien Miller86687062014-07-02 15:28:02 +1000392 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000393 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100394 len) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000395 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100396 if (authlen) {
397 /* compute tag (on encrypt) or verify tag (on decrypt) */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000398 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000399 return cc->encrypt ?
400 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
Damien Miller1d75abf2013-01-09 16:12:19 +1100401 if (cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000402 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100403 authlen, dest + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000404 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100405 }
Damien Millerbcd00ab2013-12-07 10:41:55 +1100406 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000407#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100408}
409
Damien Miller0fde8ac2013-11-21 14:12:23 +1100410/* Extract the packet length, including any decryption necessary beforehand */
411int
Damien Miller86687062014-07-02 15:28:02 +1000412cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100413 const u_char *cp, u_int len)
414{
415 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
416 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
417 cp, len);
418 if (len < 4)
Damien Miller86687062014-07-02 15:28:02 +1000419 return SSH_ERR_MESSAGE_INCOMPLETE;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000420 *plenp = PEEK_U32(cp);
Damien Miller0fde8ac2013-11-21 14:12:23 +1100421 return 0;
422}
423
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000424void
425cipher_free(struct sshcipher_ctx *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100426{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000427 if (cc == NULL)
428 return;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100429 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
Damien Millera5103f42014-02-04 11:20:14 +1100430 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
Damien Miller1f0311c2014-05-15 14:24:09 +1000431 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
432 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
433#ifdef WITH_OPENSSL
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000434 EVP_CIPHER_CTX_free(cc->evp);
435 cc->evp = NULL;
Damien Miller1f0311c2014-05-15 14:24:09 +1000436#endif
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000437 explicit_bzero(cc, sizeof(*cc));
438 free(cc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439}
440
Damien Miller5428f641999-11-25 11:54:57 +1100441/*
Damien Miller86687062014-07-02 15:28:02 +1000442 * Exports an IV from the sshcipher_ctx required to export the key
Ben Lindstrom212faca2002-03-22 01:39:44 +0000443 * state back from the unprivileged child to the privileged parent
444 * process.
445 */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000446int
Damien Miller86687062014-07-02 15:28:02 +1000447cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000448{
Damien Miller86687062014-07-02 15:28:02 +1000449 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000450
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000451 if ((c->flags & CFLAG_CHACHAPOLY) != 0)
452 return 0;
453 else if ((c->flags & CFLAG_AESCTR) != 0)
454 return sizeof(cc->ac_ctx.ctr);
Damien Miller1f0311c2014-05-15 14:24:09 +1000455#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000456 return EVP_CIPHER_CTX_iv_length(cc->evp);
457#else
458 return 0;
459#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000460}
461
Damien Miller86687062014-07-02 15:28:02 +1000462int
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000463cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000464{
Damien Miller1f0311c2014-05-15 14:24:09 +1000465#ifdef WITH_OPENSSL
djm@openbsd.org@openbsd.org41bff4d2017-11-03 02:22:41 +0000466 const struct sshcipher *c = cc->cipher;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +0000467 int evplen;
Damien Miller1f0311c2014-05-15 14:24:09 +1000468#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000469
Damien Miller0fde8ac2013-11-21 14:12:23 +1100470 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
471 if (len != 0)
Damien Miller86687062014-07-02 15:28:02 +1000472 return SSH_ERR_INVALID_ARGUMENT;
473 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100474 }
djm@openbsd.org540e8912015-01-14 10:29:45 +0000475 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
476 if (len != sizeof(cc->ac_ctx.ctr))
477 return SSH_ERR_INVALID_ARGUMENT;
478 memcpy(iv, cc->ac_ctx.ctr, len);
479 return 0;
480 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000481 if ((cc->cipher->flags & CFLAG_NONE) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000482 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100483
Damien Miller1f0311c2014-05-15 14:24:09 +1000484#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000485 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
486 if (evplen == 0)
487 return 0;
488 else if (evplen < 0)
489 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000490 if ((size_t)evplen != len)
Damien Miller86687062014-07-02 15:28:02 +1000491 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000492#ifndef OPENSSL_HAVE_EVPCTR
493 if (c->evptype == evp_aes_128_ctr)
494 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
495 else
496#endif
497 if (cipher_authlen(c)) {
498 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
499 len, iv))
500 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000501 } else if (!EVP_CIPHER_CTX_get_iv(cc->evp, iv, len))
502 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000503#endif
Damien Miller86687062014-07-02 15:28:02 +1000504 return 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000505}
506
Ben Lindstrom212faca2002-03-22 01:39:44 +0000507int
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000508cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
Damien Miller86687062014-07-02 15:28:02 +1000509{
Damien Miller86687062014-07-02 15:28:02 +1000510#ifdef WITH_OPENSSL
djm@openbsd.org@openbsd.org41bff4d2017-11-03 02:22:41 +0000511 const struct sshcipher *c = cc->cipher;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +0000512 int evplen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000513#endif
514
515 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
516 return 0;
517 if ((cc->cipher->flags & CFLAG_NONE) != 0)
518 return 0;
519
Damien Miller86687062014-07-02 15:28:02 +1000520#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000521 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
522 if (evplen <= 0)
523 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000524 if ((size_t)evplen != len)
525 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller315d2a42016-10-28 14:34:07 +1100526#ifndef OPENSSL_HAVE_EVPCTR
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000527 /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
528 if (c->evptype == evp_aes_128_ctr)
529 ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
530 else
Damien Miller315d2a42016-10-28 14:34:07 +1100531#endif
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000532 if (cipher_authlen(c)) {
533 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
534 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
535 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
536 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000537 } else if (!EVP_CIPHER_CTX_set_iv(cc->evp, iv, evplen))
538 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller86687062014-07-02 15:28:02 +1000539#endif
Damien Miller86687062014-07-02 15:28:02 +1000540 return 0;
541}