blob: 25f98ba8ebfec2a7a80c921578493d1991f787c9 [file] [log] [blame]
djm@openbsd.org670104b2019-09-06 05:23:55 +00001/* $OpenBSD: cipher.c,v 1.113 2019/09/06 05:23:55 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
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
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000146u_int
Damien Miller86687062014-07-02 15:28:02 +1000147cipher_blocksize(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100148{
149 return (c->block_size);
150}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000151
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000152u_int
Damien Miller86687062014-07-02 15:28:02 +1000153cipher_keylen(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100154{
155 return (c->key_len);
156}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000157
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000158u_int
Damien Miller86687062014-07-02 15:28:02 +1000159cipher_seclen(const struct sshcipher *c)
Damien Miller76eea4a2014-01-26 09:37:25 +1100160{
161 if (strcmp("3des-cbc", c->name) == 0)
162 return 14;
163 return cipher_keylen(c);
164}
165
166u_int
Damien Miller86687062014-07-02 15:28:02 +1000167cipher_authlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100168{
169 return (c->auth_len);
170}
171
172u_int
Damien Miller86687062014-07-02 15:28:02 +1000173cipher_ivlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100174{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100175 /*
176 * Default is cipher block size, except for chacha20+poly1305 that
177 * needs no IV. XXX make iv_len == -1 default?
178 */
179 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
180 c->iv_len : c->block_size;
Damien Miller1d75abf2013-01-09 16:12:19 +1100181}
182
183u_int
Damien Miller86687062014-07-02 15:28:02 +1000184cipher_is_cbc(const struct sshcipher *c)
Damien Miller13ae44c2009-01-28 16:38:41 +1100185{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100186 return (c->flags & CFLAG_CBC) != 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100187}
188
189u_int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000190cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
191{
192 return cc->plaintext;
193}
194
Damien Miller86687062014-07-02 15:28:02 +1000195const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100196cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000197{
Damien Miller86687062014-07-02 15:28:02 +1000198 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100199 for (c = ciphers; c->name != NULL; c++)
Darren Tucker660db782005-01-24 21:57:11 +1100200 if (strcmp(c->name, name) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100201 return c;
202 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000203}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Miller78928792000-04-12 20:17:38 +1000205#define CIPHER_SEP ","
206int
207ciphers_valid(const char *names)
208{
Damien Miller86687062014-07-02 15:28:02 +1000209 const struct sshcipher *c;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000210 char *cipher_list, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000211 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000212
Damien Millerb1715dc2000-05-30 13:44:51 +1000213 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000214 return 0;
Damien Miller86687062014-07-02 15:28:02 +1000215 if ((cipher_list = cp = strdup(names)) == NULL)
216 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100217 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100218 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100219 c = cipher_by_name(p);
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000220 if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000221 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000222 return 0;
223 }
224 }
Darren Tuckera627d422013-06-02 07:31:17 +1000225 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000226 return 1;
227}
228
Damien Miller86687062014-07-02 15:28:02 +1000229const char *
230cipher_warning_message(const struct sshcipher_ctx *cc)
231{
232 if (cc == NULL || cc->cipher == NULL)
233 return NULL;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000234 /* XXX repurpose for CBC warning */
Damien Miller86687062014-07-02 15:28:02 +1000235 return NULL;
236}
237
238int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000239cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
Damien Miller21cf4e02002-02-19 15:26:42 +1100240 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000241 int do_encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100242{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000243 struct sshcipher_ctx *cc = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000244 int ret = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000245#ifdef WITH_OPENSSL
Damien Miller21cf4e02002-02-19 15:26:42 +1100246 const EVP_CIPHER *type;
247 int klen;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000248#endif
249
250 *ccp = NULL;
251 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
252 return SSH_ERR_ALLOC_FAIL;
Damien Miller21cf4e02002-02-19 15:26:42 +1100253
djm@openbsd.orge77e1562017-05-01 00:03:18 +0000254 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100255 cc->encrypt = do_encrypt;
Damien Miller21cf4e02002-02-19 15:26:42 +1100256
Damien Miller86687062014-07-02 15:28:02 +1000257 if (keylen < cipher->key_len ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000258 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
259 ret = SSH_ERR_INVALID_ARGUMENT;
260 goto out;
261 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100262
Damien Miller86687062014-07-02 15:28:02 +1000263 cc->cipher = cipher;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100264 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000265 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
266 goto out;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100267 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000268 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
269 ret = 0;
270 goto out;
271 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000272#ifndef WITH_OPENSSL
273 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
274 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
275 aesctr_ivsetup(&cc->ac_ctx, iv);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000276 ret = 0;
277 goto out;
Damien Miller1f0311c2014-05-15 14:24:09 +1000278 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000279 ret = SSH_ERR_INVALID_ARGUMENT;
280 goto out;
281#else /* WITH_OPENSSL */
Damien Miller21cf4e02002-02-19 15:26:42 +1100282 type = (*cipher->evptype)();
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000283 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
284 ret = SSH_ERR_ALLOC_FAIL;
285 goto out;
286 }
287 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
Damien Miller86687062014-07-02 15:28:02 +1000288 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
289 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000290 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000291 }
Damien Miller1d75abf2013-01-09 16:12:19 +1100292 if (cipher_authlen(cipher) &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000293 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
Damien Miller86687062014-07-02 15:28:02 +1000294 -1, (u_char *)iv)) {
295 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000296 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000297 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000298 klen = EVP_CIPHER_CTX_key_length(cc->evp);
Damien Millereccb9de2005-06-17 12:59:34 +1000299 if (klen > 0 && keylen != (u_int)klen) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000300 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 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 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100304 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000305 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000306 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 ret = 0;
310#endif /* WITH_OPENSSL */
311 out:
312 if (ret == 0) {
313 /* success */
314 *ccp = cc;
315 } else {
316 if (cc != NULL) {
317#ifdef WITH_OPENSSL
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000318 EVP_CIPHER_CTX_free(cc->evp);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000319#endif /* WITH_OPENSSL */
320 explicit_bzero(cc, sizeof(*cc));
321 free(cc);
322 }
323 }
324 return ret;
Damien Miller874d77b2000-10-14 16:23:11 +1100325}
326
Damien Milleraf43a7a2012-12-12 10:46:31 +1100327/*
328 * cipher_crypt() operates as following:
329 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
330 * Theses bytes are treated as additional authenticated data for
331 * authenticated encryption modes.
332 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
Damien Miller1d75abf2013-01-09 16:12:19 +1100333 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
334 * This tag is written on encryption and verified on decryption.
Damien Milleraf43a7a2012-12-12 10:46:31 +1100335 * Both 'aadlen' and 'authlen' can be set to 0.
336 */
Damien Millerbcd00ab2013-12-07 10:41:55 +1100337int
Damien Miller86687062014-07-02 15:28:02 +1000338cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
339 const u_char *src, u_int len, u_int aadlen, u_int authlen)
Damien Miller874d77b2000-10-14 16:23:11 +1100340{
Damien Miller86687062014-07-02 15:28:02 +1000341 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
342 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
343 len, aadlen, authlen, cc->encrypt);
344 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000345 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
346 memcpy(dest, src, aadlen + len);
347 return 0;
348 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000349#ifndef WITH_OPENSSL
350 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
351 if (aadlen)
352 memcpy(dest, src, aadlen);
353 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
354 dest + aadlen, len);
355 return 0;
356 }
Damien Miller86687062014-07-02 15:28:02 +1000357 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1f0311c2014-05-15 14:24:09 +1000358#else
Damien Miller1d75abf2013-01-09 16:12:19 +1100359 if (authlen) {
360 u_char lastiv[1];
361
362 if (authlen != cipher_authlen(cc->cipher))
Damien Miller86687062014-07-02 15:28:02 +1000363 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1d75abf2013-01-09 16:12:19 +1100364 /* increment IV */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000365 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Damien Miller1d75abf2013-01-09 16:12:19 +1100366 1, lastiv))
Damien Miller86687062014-07-02 15:28:02 +1000367 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100368 /* set tag on decyption */
369 if (!cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000370 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100371 authlen, (u_char *)src + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000372 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100373 }
374 if (aadlen) {
375 if (authlen &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000376 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000377 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100378 memcpy(dest, src, aadlen);
Damien Miller1d75abf2013-01-09 16:12:19 +1100379 }
Damien Miller874d77b2000-10-14 16:23:11 +1100380 if (len % cc->cipher->block_size)
Damien Miller86687062014-07-02 15:28:02 +1000381 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000382 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100383 len) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000384 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100385 if (authlen) {
386 /* compute tag (on encrypt) or verify tag (on decrypt) */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000387 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000388 return cc->encrypt ?
389 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
Damien Miller1d75abf2013-01-09 16:12:19 +1100390 if (cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000391 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100392 authlen, dest + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000393 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100394 }
Damien Millerbcd00ab2013-12-07 10:41:55 +1100395 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000396#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100397}
398
Damien Miller0fde8ac2013-11-21 14:12:23 +1100399/* Extract the packet length, including any decryption necessary beforehand */
400int
Damien Miller86687062014-07-02 15:28:02 +1000401cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100402 const u_char *cp, u_int len)
403{
404 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
405 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
406 cp, len);
407 if (len < 4)
Damien Miller86687062014-07-02 15:28:02 +1000408 return SSH_ERR_MESSAGE_INCOMPLETE;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000409 *plenp = PEEK_U32(cp);
Damien Miller0fde8ac2013-11-21 14:12:23 +1100410 return 0;
411}
412
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000413void
414cipher_free(struct sshcipher_ctx *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100415{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000416 if (cc == NULL)
417 return;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100418 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
Damien Millera5103f42014-02-04 11:20:14 +1100419 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
Damien Miller1f0311c2014-05-15 14:24:09 +1000420 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
421 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
422#ifdef WITH_OPENSSL
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000423 EVP_CIPHER_CTX_free(cc->evp);
424 cc->evp = NULL;
Damien Miller1f0311c2014-05-15 14:24:09 +1000425#endif
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000426 explicit_bzero(cc, sizeof(*cc));
427 free(cc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428}
429
Damien Miller5428f641999-11-25 11:54:57 +1100430/*
Damien Miller86687062014-07-02 15:28:02 +1000431 * Exports an IV from the sshcipher_ctx required to export the key
Ben Lindstrom212faca2002-03-22 01:39:44 +0000432 * state back from the unprivileged child to the privileged parent
433 * process.
434 */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000435int
Damien Miller86687062014-07-02 15:28:02 +1000436cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000437{
Damien Miller86687062014-07-02 15:28:02 +1000438 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000439
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000440 if ((c->flags & CFLAG_CHACHAPOLY) != 0)
441 return 0;
442 else if ((c->flags & CFLAG_AESCTR) != 0)
443 return sizeof(cc->ac_ctx.ctr);
Damien Miller1f0311c2014-05-15 14:24:09 +1000444#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000445 return EVP_CIPHER_CTX_iv_length(cc->evp);
446#else
447 return 0;
448#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000449}
450
Damien Miller86687062014-07-02 15:28:02 +1000451int
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000452cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000453{
Damien Miller1f0311c2014-05-15 14:24:09 +1000454#ifdef WITH_OPENSSL
djm@openbsd.org@openbsd.org41bff4d2017-11-03 02:22:41 +0000455 const struct sshcipher *c = cc->cipher;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +0000456 int evplen;
Damien Miller1f0311c2014-05-15 14:24:09 +1000457#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000458
Damien Miller0fde8ac2013-11-21 14:12:23 +1100459 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
460 if (len != 0)
Damien Miller86687062014-07-02 15:28:02 +1000461 return SSH_ERR_INVALID_ARGUMENT;
462 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100463 }
djm@openbsd.org540e8912015-01-14 10:29:45 +0000464 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
465 if (len != sizeof(cc->ac_ctx.ctr))
466 return SSH_ERR_INVALID_ARGUMENT;
467 memcpy(iv, cc->ac_ctx.ctr, len);
468 return 0;
469 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000470 if ((cc->cipher->flags & CFLAG_NONE) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000471 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100472
Damien Miller1f0311c2014-05-15 14:24:09 +1000473#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000474 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
475 if (evplen == 0)
476 return 0;
477 else if (evplen < 0)
478 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000479 if ((size_t)evplen != len)
Damien Miller86687062014-07-02 15:28:02 +1000480 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000481#ifndef OPENSSL_HAVE_EVPCTR
482 if (c->evptype == evp_aes_128_ctr)
483 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
484 else
485#endif
486 if (cipher_authlen(c)) {
487 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
488 len, iv))
489 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000490 } else if (!EVP_CIPHER_CTX_get_iv(cc->evp, iv, len))
491 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000492#endif
Damien Miller86687062014-07-02 15:28:02 +1000493 return 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000494}
495
Ben Lindstrom212faca2002-03-22 01:39:44 +0000496int
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000497cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
Damien Miller86687062014-07-02 15:28:02 +1000498{
Damien Miller86687062014-07-02 15:28:02 +1000499#ifdef WITH_OPENSSL
djm@openbsd.org@openbsd.org41bff4d2017-11-03 02:22:41 +0000500 const struct sshcipher *c = cc->cipher;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +0000501 int evplen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000502#endif
503
504 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
505 return 0;
506 if ((cc->cipher->flags & CFLAG_NONE) != 0)
507 return 0;
508
Damien Miller86687062014-07-02 15:28:02 +1000509#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000510 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
511 if (evplen <= 0)
512 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000513 if ((size_t)evplen != len)
514 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller315d2a42016-10-28 14:34:07 +1100515#ifndef OPENSSL_HAVE_EVPCTR
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000516 /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
517 if (c->evptype == evp_aes_128_ctr)
518 ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
519 else
Damien Miller315d2a42016-10-28 14:34:07 +1100520#endif
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000521 if (cipher_authlen(c)) {
522 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
523 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
524 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
525 return SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000526 } else if (!EVP_CIPHER_CTX_set_iv(cc->evp, iv, evplen))
527 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller86687062014-07-02 15:28:02 +1000528#endif
Damien Miller86687062014-07-02 15:28:02 +1000529 return 0;
530}