blob: 7d72084ff5c954d2a935ad2a0665e951b2691c1b [file] [log] [blame]
djm@openbsd.orge77e1562017-05-01 00:03:18 +00001/* $OpenBSD: cipher.c,v 1.105 2017/05/01 00:03:18 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 Miller3710f272005-05-26 12:19:17 +100070 u_int discard_len;
Damien Miller0fde8ac2013-11-21 14:12:23 +110071 u_int flags;
72#define CFLAG_CBC (1<<0)
73#define CFLAG_CHACHAPOLY (1<<1)
Damien Miller1f0311c2014-05-15 14:24:09 +100074#define CFLAG_AESCTR (1<<2)
75#define CFLAG_NONE (1<<3)
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +000076#define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */
Damien Miller1f0311c2014-05-15 14:24:09 +100077#ifdef WITH_OPENSSL
Ben Lindstrom6a246412002-06-06 19:48:16 +000078 const EVP_CIPHER *(*evptype)(void);
Damien Miller1f0311c2014-05-15 14:24:09 +100079#else
80 void *ignored;
81#endif
Damien Millerea111192013-04-23 19:24:32 +100082};
83
Damien Miller86687062014-07-02 15:28:02 +100084static const struct sshcipher ciphers[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +100085#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +000086 { "3des-cbc", 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
Damien Miller832b7442016-07-15 14:45:34 +100087# ifndef OPENSSL_NO_BF
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +000088 { "blowfish-cbc", 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
Damien Miller832b7442016-07-15 14:45:34 +100089# endif /* OPENSSL_NO_BF */
90# ifndef OPENSSL_NO_CAST
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +000091 { "cast128-cbc", 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
Damien Miller832b7442016-07-15 14:45:34 +100092# endif /* OPENSSL_NO_CAST */
93# ifndef OPENSSL_NO_RC4
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +000094 { "arcfour", 8, 16, 0, 0, 0, 0, EVP_rc4 },
95 { "arcfour128", 8, 16, 0, 0, 1536, 0, EVP_rc4 },
96 { "arcfour256", 8, 32, 0, 0, 1536, 0, EVP_rc4 },
Damien Miller832b7442016-07-15 14:45:34 +100097# endif /* OPENSSL_NO_RC4 */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +000098 { "aes128-cbc", 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
99 { "aes192-cbc", 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
100 { "aes256-cbc", 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
Damien Miller1d75abf2013-01-09 16:12:19 +1100101 { "rijndael-cbc@lysator.liu.se",
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000102 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
103 { "aes128-ctr", 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
104 { "aes192-ctr", 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
105 { "aes256-ctr", 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
Damien Miller86687062014-07-02 15:28:02 +1000106# ifdef OPENSSL_HAVE_EVPGCM
Damien Miller1d75abf2013-01-09 16:12:19 +1100107 { "aes128-gcm@openssh.com",
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000108 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
Damien Miller1d75abf2013-01-09 16:12:19 +1100109 { "aes256-gcm@openssh.com",
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000110 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
Damien Miller86687062014-07-02 15:28:02 +1000111# endif /* OPENSSL_HAVE_EVPGCM */
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000112#else
113 { "aes128-ctr", 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
114 { "aes192-ctr", 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
115 { "aes256-ctr", 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
116#endif
Damien Miller0fde8ac2013-11-21 14:12:23 +1100117 { "chacha20-poly1305@openssh.com",
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000118 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
119 { "none", 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
Damien Miller86687062014-07-02 15:28:02 +1000120
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000121 { NULL, 0, 0, 0, 0, 0, 0, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122};
123
Damien Miller874d77b2000-10-14 16:23:11 +1100124/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller86687062014-07-02 15:28:02 +1000126/* Returns a comma-separated list of supported ciphers. */
Damien Millerea111192013-04-23 19:24:32 +1000127char *
Damien Miller0fde8ac2013-11-21 14:12:23 +1100128cipher_alg_list(char sep, int auth_only)
Damien Millerea111192013-04-23 19:24:32 +1000129{
Damien Miller86687062014-07-02 15:28:02 +1000130 char *tmp, *ret = NULL;
Damien Millerea111192013-04-23 19:24:32 +1000131 size_t nlen, rlen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000132 const struct sshcipher *c;
Damien Millerea111192013-04-23 19:24:32 +1000133
134 for (c = ciphers; c->name != NULL; c++) {
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000135 if ((c->flags & CFLAG_INTERNAL) != 0)
Damien Millerea111192013-04-23 19:24:32 +1000136 continue;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100137 if (auth_only && c->auth_len == 0)
138 continue;
Damien Millerea111192013-04-23 19:24:32 +1000139 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100140 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000141 nlen = strlen(c->name);
Damien Miller86687062014-07-02 15:28:02 +1000142 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
143 free(ret);
144 return NULL;
145 }
146 ret = tmp;
Damien Millerea111192013-04-23 19:24:32 +1000147 memcpy(ret + rlen, c->name, nlen + 1);
148 rlen += nlen;
149 }
150 return ret;
151}
152
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000153u_int
Damien Miller86687062014-07-02 15:28:02 +1000154cipher_blocksize(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100155{
156 return (c->block_size);
157}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000158
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000159u_int
Damien Miller86687062014-07-02 15:28:02 +1000160cipher_keylen(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100161{
162 return (c->key_len);
163}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000164
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000165u_int
Damien Miller86687062014-07-02 15:28:02 +1000166cipher_seclen(const struct sshcipher *c)
Damien Miller76eea4a2014-01-26 09:37:25 +1100167{
168 if (strcmp("3des-cbc", c->name) == 0)
169 return 14;
170 return cipher_keylen(c);
171}
172
173u_int
Damien Miller86687062014-07-02 15:28:02 +1000174cipher_authlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100175{
176 return (c->auth_len);
177}
178
179u_int
Damien Miller86687062014-07-02 15:28:02 +1000180cipher_ivlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100181{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100182 /*
183 * Default is cipher block size, except for chacha20+poly1305 that
184 * needs no IV. XXX make iv_len == -1 default?
185 */
186 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
187 c->iv_len : c->block_size;
Damien Miller1d75abf2013-01-09 16:12:19 +1100188}
189
190u_int
Damien Miller86687062014-07-02 15:28:02 +1000191cipher_is_cbc(const struct sshcipher *c)
Damien Miller13ae44c2009-01-28 16:38:41 +1100192{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100193 return (c->flags & CFLAG_CBC) != 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100194}
195
196u_int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000197cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
198{
199 return cc->plaintext;
200}
201
Damien Miller86687062014-07-02 15:28:02 +1000202const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100203cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000204{
Damien Miller86687062014-07-02 15:28:02 +1000205 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100206 for (c = ciphers; c->name != NULL; c++)
Darren Tucker660db782005-01-24 21:57:11 +1100207 if (strcmp(c->name, name) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100208 return c;
209 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000210}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211
Damien Miller78928792000-04-12 20:17:38 +1000212#define CIPHER_SEP ","
213int
214ciphers_valid(const char *names)
215{
Damien Miller86687062014-07-02 15:28:02 +1000216 const struct sshcipher *c;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000217 char *cipher_list, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000218 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000219
Damien Millerb1715dc2000-05-30 13:44:51 +1000220 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000221 return 0;
Damien Miller86687062014-07-02 15:28:02 +1000222 if ((cipher_list = cp = strdup(names)) == NULL)
223 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100224 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100225 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100226 c = cipher_by_name(p);
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000227 if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000228 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000229 return 0;
230 }
231 }
Darren Tuckera627d422013-06-02 07:31:17 +1000232 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000233 return 1;
234}
235
Damien Miller86687062014-07-02 15:28:02 +1000236const char *
237cipher_warning_message(const struct sshcipher_ctx *cc)
238{
239 if (cc == NULL || cc->cipher == NULL)
240 return NULL;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000241 /* XXX repurpose for CBC warning */
Damien Miller86687062014-07-02 15:28:02 +1000242 return NULL;
243}
244
245int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000246cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
Damien Miller21cf4e02002-02-19 15:26:42 +1100247 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000248 int do_encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100249{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000250 struct sshcipher_ctx *cc = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000251 int ret = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000252#ifdef WITH_OPENSSL
Damien Miller21cf4e02002-02-19 15:26:42 +1100253 const EVP_CIPHER *type;
254 int klen;
Damien Miller3710f272005-05-26 12:19:17 +1000255 u_char *junk, *discard;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000256#endif
257
258 *ccp = NULL;
259 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
260 return SSH_ERR_ALLOC_FAIL;
Damien Miller21cf4e02002-02-19 15:26:42 +1100261
djm@openbsd.orge77e1562017-05-01 00:03:18 +0000262 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
Damien Miller1d75abf2013-01-09 16:12:19 +1100263 cc->encrypt = do_encrypt;
Damien Miller21cf4e02002-02-19 15:26:42 +1100264
Damien Miller86687062014-07-02 15:28:02 +1000265 if (keylen < cipher->key_len ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000266 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
267 ret = SSH_ERR_INVALID_ARGUMENT;
268 goto out;
269 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100270
Damien Miller86687062014-07-02 15:28:02 +1000271 cc->cipher = cipher;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100272 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000273 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
274 goto out;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100275 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000276 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
277 ret = 0;
278 goto out;
279 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000280#ifndef WITH_OPENSSL
281 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
282 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
283 aesctr_ivsetup(&cc->ac_ctx, iv);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000284 ret = 0;
285 goto out;
Damien Miller1f0311c2014-05-15 14:24:09 +1000286 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000287 ret = SSH_ERR_INVALID_ARGUMENT;
288 goto out;
289#else /* WITH_OPENSSL */
Damien Miller21cf4e02002-02-19 15:26:42 +1100290 type = (*cipher->evptype)();
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000291 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
292 ret = SSH_ERR_ALLOC_FAIL;
293 goto out;
294 }
295 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
Damien Miller86687062014-07-02 15:28:02 +1000296 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
297 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000298 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000299 }
Damien Miller1d75abf2013-01-09 16:12:19 +1100300 if (cipher_authlen(cipher) &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000301 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
Damien Miller86687062014-07-02 15:28:02 +1000302 -1, (u_char *)iv)) {
303 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 klen = EVP_CIPHER_CTX_key_length(cc->evp);
Damien Millereccb9de2005-06-17 12:59:34 +1000307 if (klen > 0 && keylen != (u_int)klen) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000308 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000309 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000310 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000311 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100312 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000313 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000314 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000315 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000316 }
Damien Miller3710f272005-05-26 12:19:17 +1000317
Damien Miller46d38de2005-07-17 17:02:09 +1000318 if (cipher->discard_len > 0) {
Damien Miller86687062014-07-02 15:28:02 +1000319 if ((junk = malloc(cipher->discard_len)) == NULL ||
320 (discard = malloc(cipher->discard_len)) == NULL) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000321 free(junk);
Damien Miller86687062014-07-02 15:28:02 +1000322 ret = SSH_ERR_ALLOC_FAIL;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000323 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000324 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000325 ret = EVP_Cipher(cc->evp, discard, junk, cipher->discard_len);
Damien Millera5103f42014-02-04 11:20:14 +1100326 explicit_bzero(discard, cipher->discard_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000327 free(junk);
328 free(discard);
Damien Miller86687062014-07-02 15:28:02 +1000329 if (ret != 1) {
330 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000331 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000332 }
Damien Miller3710f272005-05-26 12:19:17 +1000333 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000334 ret = 0;
335#endif /* WITH_OPENSSL */
336 out:
337 if (ret == 0) {
338 /* success */
339 *ccp = cc;
340 } else {
341 if (cc != NULL) {
342#ifdef WITH_OPENSSL
343 if (cc->evp != NULL)
344 EVP_CIPHER_CTX_free(cc->evp);
345#endif /* WITH_OPENSSL */
346 explicit_bzero(cc, sizeof(*cc));
347 free(cc);
348 }
349 }
350 return ret;
Damien Miller874d77b2000-10-14 16:23:11 +1100351}
352
Damien Milleraf43a7a2012-12-12 10:46:31 +1100353/*
354 * cipher_crypt() operates as following:
355 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
356 * Theses bytes are treated as additional authenticated data for
357 * authenticated encryption modes.
358 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
Damien Miller1d75abf2013-01-09 16:12:19 +1100359 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
360 * This tag is written on encryption and verified on decryption.
Damien Milleraf43a7a2012-12-12 10:46:31 +1100361 * Both 'aadlen' and 'authlen' can be set to 0.
362 */
Damien Millerbcd00ab2013-12-07 10:41:55 +1100363int
Damien Miller86687062014-07-02 15:28:02 +1000364cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
365 const u_char *src, u_int len, u_int aadlen, u_int authlen)
Damien Miller874d77b2000-10-14 16:23:11 +1100366{
Damien Miller86687062014-07-02 15:28:02 +1000367 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
368 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
369 len, aadlen, authlen, cc->encrypt);
370 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000371 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
372 memcpy(dest, src, aadlen + len);
373 return 0;
374 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000375#ifndef WITH_OPENSSL
376 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
377 if (aadlen)
378 memcpy(dest, src, aadlen);
379 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
380 dest + aadlen, len);
381 return 0;
382 }
Damien Miller86687062014-07-02 15:28:02 +1000383 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1f0311c2014-05-15 14:24:09 +1000384#else
Damien Miller1d75abf2013-01-09 16:12:19 +1100385 if (authlen) {
386 u_char lastiv[1];
387
388 if (authlen != cipher_authlen(cc->cipher))
Damien Miller86687062014-07-02 15:28:02 +1000389 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1d75abf2013-01-09 16:12:19 +1100390 /* increment IV */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000391 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Damien Miller1d75abf2013-01-09 16:12:19 +1100392 1, lastiv))
Damien Miller86687062014-07-02 15:28:02 +1000393 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100394 /* set tag on decyption */
395 if (!cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000396 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100397 authlen, (u_char *)src + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000398 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100399 }
400 if (aadlen) {
401 if (authlen &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000402 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000403 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100404 memcpy(dest, src, aadlen);
Damien Miller1d75abf2013-01-09 16:12:19 +1100405 }
Damien Miller874d77b2000-10-14 16:23:11 +1100406 if (len % cc->cipher->block_size)
Damien Miller86687062014-07-02 15:28:02 +1000407 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000408 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100409 len) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000410 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100411 if (authlen) {
412 /* compute tag (on encrypt) or verify tag (on decrypt) */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000413 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000414 return cc->encrypt ?
415 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
Damien Miller1d75abf2013-01-09 16:12:19 +1100416 if (cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000417 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100418 authlen, dest + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000419 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100420 }
Damien Millerbcd00ab2013-12-07 10:41:55 +1100421 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000422#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100423}
424
Damien Miller0fde8ac2013-11-21 14:12:23 +1100425/* Extract the packet length, including any decryption necessary beforehand */
426int
Damien Miller86687062014-07-02 15:28:02 +1000427cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100428 const u_char *cp, u_int len)
429{
430 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
431 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
432 cp, len);
433 if (len < 4)
Damien Miller86687062014-07-02 15:28:02 +1000434 return SSH_ERR_MESSAGE_INCOMPLETE;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100435 *plenp = get_u32(cp);
436 return 0;
437}
438
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000439void
440cipher_free(struct sshcipher_ctx *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100441{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000442 if (cc == NULL)
443 return;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100444 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
Damien Millera5103f42014-02-04 11:20:14 +1100445 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
Damien Miller1f0311c2014-05-15 14:24:09 +1000446 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
447 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
448#ifdef WITH_OPENSSL
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000449 if (cc->evp != NULL) {
450 EVP_CIPHER_CTX_free(cc->evp);
451 cc->evp = NULL;
452 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000453#endif
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000454 explicit_bzero(cc, sizeof(*cc));
455 free(cc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456}
457
Damien Miller5428f641999-11-25 11:54:57 +1100458/*
459 * Selects the cipher, and keys if by computing the MD5 checksum of the
460 * passphrase and using the resulting 16 bytes as the key.
461 */
Damien Miller86687062014-07-02 15:28:02 +1000462int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000463cipher_set_key_string(struct sshcipher_ctx **ccp,
464 const struct sshcipher *cipher, const char *passphrase, int do_encrypt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000466 u_char digest[16];
Damien Miller86687062014-07-02 15:28:02 +1000467 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller86687062014-07-02 15:28:02 +1000469 if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
470 passphrase, strlen(passphrase),
471 digest, sizeof(digest))) != 0)
472 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100473
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000474 r = cipher_init(ccp, cipher, digest, 16, NULL, 0, do_encrypt);
Damien Miller86687062014-07-02 15:28:02 +1000475 out:
Damien Millera5103f42014-02-04 11:20:14 +1100476 explicit_bzero(digest, sizeof(digest));
Damien Miller86687062014-07-02 15:28:02 +1000477 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478}
Damien Miller21cf4e02002-02-19 15:26:42 +1100479
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000480/*
Damien Miller86687062014-07-02 15:28:02 +1000481 * Exports an IV from the sshcipher_ctx required to export the key
Ben Lindstrom212faca2002-03-22 01:39:44 +0000482 * state back from the unprivileged child to the privileged parent
483 * process.
484 */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000485int
Damien Miller86687062014-07-02 15:28:02 +1000486cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000487{
Damien Miller86687062014-07-02 15:28:02 +1000488 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000489
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000490 if ((c->flags & CFLAG_CHACHAPOLY) != 0)
491 return 0;
492 else if ((c->flags & CFLAG_AESCTR) != 0)
493 return sizeof(cc->ac_ctx.ctr);
Damien Miller1f0311c2014-05-15 14:24:09 +1000494#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000495 return EVP_CIPHER_CTX_iv_length(cc->evp);
496#else
497 return 0;
498#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000499}
500
Damien Miller86687062014-07-02 15:28:02 +1000501int
502cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000503{
Damien Miller86687062014-07-02 15:28:02 +1000504 const struct sshcipher *c = cc->cipher;
Damien Miller1f0311c2014-05-15 14:24:09 +1000505#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000506 int evplen;
Damien Miller1f0311c2014-05-15 14:24:09 +1000507#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000508
Damien Miller0fde8ac2013-11-21 14:12:23 +1100509 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
510 if (len != 0)
Damien Miller86687062014-07-02 15:28:02 +1000511 return SSH_ERR_INVALID_ARGUMENT;
512 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100513 }
djm@openbsd.org540e8912015-01-14 10:29:45 +0000514 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
515 if (len != sizeof(cc->ac_ctx.ctr))
516 return SSH_ERR_INVALID_ARGUMENT;
517 memcpy(iv, cc->ac_ctx.ctr, len);
518 return 0;
519 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000520 if ((cc->cipher->flags & CFLAG_NONE) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000521 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100522
Damien Miller1f0311c2014-05-15 14:24:09 +1000523#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000524 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
525 if (evplen == 0)
526 return 0;
527 else if (evplen < 0)
528 return SSH_ERR_LIBCRYPTO_ERROR;
529 if ((u_int)evplen != len)
Damien Miller86687062014-07-02 15:28:02 +1000530 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000531#ifndef OPENSSL_HAVE_EVPCTR
532 if (c->evptype == evp_aes_128_ctr)
533 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
534 else
535#endif
536 if (cipher_authlen(c)) {
537 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
538 len, iv))
539 return SSH_ERR_LIBCRYPTO_ERROR;
540 } else
541 memcpy(iv, cc->evp->iv, len);
542#endif
Damien Miller86687062014-07-02 15:28:02 +1000543 return 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000544}
545
Ben Lindstrom212faca2002-03-22 01:39:44 +0000546int
Damien Miller86687062014-07-02 15:28:02 +1000547cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
548{
549 const struct sshcipher *c = cc->cipher;
550#ifdef WITH_OPENSSL
551 int evplen = 0;
552#endif
553
554 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
555 return 0;
556 if ((cc->cipher->flags & CFLAG_NONE) != 0)
557 return 0;
558
Damien Miller86687062014-07-02 15:28:02 +1000559#ifdef WITH_OPENSSL
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000560 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
561 if (evplen <= 0)
562 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller315d2a42016-10-28 14:34:07 +1100563#ifndef OPENSSL_HAVE_EVPCTR
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000564 /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
565 if (c->evptype == evp_aes_128_ctr)
566 ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
567 else
Damien Miller315d2a42016-10-28 14:34:07 +1100568#endif
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000569 if (cipher_authlen(c)) {
570 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
571 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
572 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
573 return SSH_ERR_LIBCRYPTO_ERROR;
574 } else
575 memcpy(cc->evp->iv, iv, evplen);
Damien Miller86687062014-07-02 15:28:02 +1000576#endif
Damien Miller86687062014-07-02 15:28:02 +1000577 return 0;
578}
579
580#ifdef WITH_OPENSSL
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000581#define EVP_X_STATE(evp) (evp)->cipher_data
582#define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
Damien Miller86687062014-07-02 15:28:02 +1000583#endif
584
585int
586cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000587{
Damien Miller832b7442016-07-15 14:45:34 +1000588#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
Damien Miller86687062014-07-02 15:28:02 +1000589 const struct sshcipher *c = cc->cipher;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000590 int plen = 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000591
Damien Millerf0a8ded2013-02-12 11:00:34 +1100592 if (c->evptype == EVP_rc4) {
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000593 plen = EVP_X_STATE_LEN(cc->evp);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000594 if (dat == NULL)
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000595 return (plen);
596 memcpy(dat, EVP_X_STATE(cc->evp), plen);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000597 }
Ben Lindstrom212faca2002-03-22 01:39:44 +0000598 return (plen);
Damien Miller1f0311c2014-05-15 14:24:09 +1000599#else
Damien Miller86687062014-07-02 15:28:02 +1000600 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000601#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000602}
603
604void
Damien Miller86687062014-07-02 15:28:02 +1000605cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000606{
Damien Miller832b7442016-07-15 14:45:34 +1000607#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
Damien Miller86687062014-07-02 15:28:02 +1000608 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000609 int plen;
610
Damien Millerf0a8ded2013-02-12 11:00:34 +1100611 if (c->evptype == EVP_rc4) {
Ben Lindstrom212faca2002-03-22 01:39:44 +0000612 plen = EVP_X_STATE_LEN(cc->evp);
613 memcpy(EVP_X_STATE(cc->evp), dat, plen);
614 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000615#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100616}