blob: 13847e5bd7b6575b0dd28ccf0cdf500efdad5881 [file] [log] [blame]
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001/* $OpenBSD: cipher.c,v 1.101 2015/12/10 17:08:40 mmcc 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 Miller1f0311c2014-05-15 14:24:09 +100054#ifdef WITH_SSH1
Damien Miller3a3261f2003-05-15 13:37:19 +100055extern const EVP_CIPHER *evp_ssh1_bf(void);
56extern const EVP_CIPHER *evp_ssh1_3des(void);
Damien Miller86687062014-07-02 15:28:02 +100057extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
Damien Miller1f0311c2014-05-15 14:24:09 +100058#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller86687062014-07-02 15:28:02 +100060struct sshcipher {
Damien Miller963f6b22002-02-19 15:21:23 +110061 char *name;
62 int number; /* for ssh1 only */
63 u_int block_size;
64 u_int key_len;
Damien Millerd522c682013-01-09 16:42:47 +110065 u_int iv_len; /* defaults to block_size */
66 u_int auth_len;
Damien Miller3710f272005-05-26 12:19:17 +100067 u_int discard_len;
Damien Miller0fde8ac2013-11-21 14:12:23 +110068 u_int flags;
69#define CFLAG_CBC (1<<0)
70#define CFLAG_CHACHAPOLY (1<<1)
Damien Miller1f0311c2014-05-15 14:24:09 +100071#define CFLAG_AESCTR (1<<2)
72#define CFLAG_NONE (1<<3)
73#ifdef WITH_OPENSSL
Ben Lindstrom6a246412002-06-06 19:48:16 +000074 const EVP_CIPHER *(*evptype)(void);
Damien Miller1f0311c2014-05-15 14:24:09 +100075#else
76 void *ignored;
77#endif
Damien Millerea111192013-04-23 19:24:32 +100078};
79
Damien Miller86687062014-07-02 15:28:02 +100080static const struct sshcipher ciphers[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +100081#ifdef WITH_SSH1
Damien Miller1d75abf2013-01-09 16:12:19 +110082 { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
83 { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
84 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
Damien Miller86687062014-07-02 15:28:02 +100085#endif /* WITH_SSH1 */
Damien Miller1f0311c2014-05-15 14:24:09 +100086#ifdef WITH_OPENSSL
87 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
Damien Miller1d75abf2013-01-09 16:12:19 +110088 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
89 { "blowfish-cbc",
90 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
91 { "cast128-cbc",
92 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
93 { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
94 { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
95 { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
96 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
97 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
98 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
99 { "rijndael-cbc@lysator.liu.se",
100 SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
101 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
102 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
103 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
Damien Miller86687062014-07-02 15:28:02 +1000104# ifdef OPENSSL_HAVE_EVPGCM
Damien Miller1d75abf2013-01-09 16:12:19 +1100105 { "aes128-gcm@openssh.com",
106 SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
107 { "aes256-gcm@openssh.com",
108 SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
Damien Miller86687062014-07-02 15:28:02 +1000109# endif /* OPENSSL_HAVE_EVPGCM */
Damien Miller1f0311c2014-05-15 14:24:09 +1000110#else /* WITH_OPENSSL */
111 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
112 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
113 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
114 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
115#endif /* WITH_OPENSSL */
Damien Miller0fde8ac2013-11-21 14:12:23 +1100116 { "chacha20-poly1305@openssh.com",
117 SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
Damien Miller86687062014-07-02 15:28:02 +1000118
Damien Miller1d75abf2013-01-09 16:12:19 +1100119 { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120};
121
Damien Miller874d77b2000-10-14 16:23:11 +1100122/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller86687062014-07-02 15:28:02 +1000124/* Returns a comma-separated list of supported ciphers. */
Damien Millerea111192013-04-23 19:24:32 +1000125char *
Damien Miller0fde8ac2013-11-21 14:12:23 +1100126cipher_alg_list(char sep, int auth_only)
Damien Millerea111192013-04-23 19:24:32 +1000127{
Damien Miller86687062014-07-02 15:28:02 +1000128 char *tmp, *ret = NULL;
Damien Millerea111192013-04-23 19:24:32 +1000129 size_t nlen, rlen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000130 const struct sshcipher *c;
Damien Millerea111192013-04-23 19:24:32 +1000131
132 for (c = ciphers; c->name != NULL; c++) {
133 if (c->number != SSH_CIPHER_SSH2)
134 continue;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100135 if (auth_only && c->auth_len == 0)
136 continue;
Damien Millerea111192013-04-23 19:24:32 +1000137 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100138 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000139 nlen = strlen(c->name);
Damien Miller86687062014-07-02 15:28:02 +1000140 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
141 free(ret);
142 return NULL;
143 }
144 ret = tmp;
Damien Millerea111192013-04-23 19:24:32 +1000145 memcpy(ret + rlen, c->name, nlen + 1);
146 rlen += nlen;
147 }
148 return ret;
149}
150
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000151u_int
Damien Miller86687062014-07-02 15:28:02 +1000152cipher_blocksize(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100153{
154 return (c->block_size);
155}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000156
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000157u_int
Damien Miller86687062014-07-02 15:28:02 +1000158cipher_keylen(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100159{
160 return (c->key_len);
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_seclen(const struct sshcipher *c)
Damien Miller76eea4a2014-01-26 09:37:25 +1100165{
166 if (strcmp("3des-cbc", c->name) == 0)
167 return 14;
168 return cipher_keylen(c);
169}
170
171u_int
Damien Miller86687062014-07-02 15:28:02 +1000172cipher_authlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100173{
174 return (c->auth_len);
175}
176
177u_int
Damien Miller86687062014-07-02 15:28:02 +1000178cipher_ivlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100179{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100180 /*
181 * Default is cipher block size, except for chacha20+poly1305 that
182 * needs no IV. XXX make iv_len == -1 default?
183 */
184 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
185 c->iv_len : c->block_size;
Damien Miller1d75abf2013-01-09 16:12:19 +1100186}
187
188u_int
Damien Miller86687062014-07-02 15:28:02 +1000189cipher_get_number(const struct sshcipher *c)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000190{
191 return (c->number);
192}
Damien Miller963f6b22002-02-19 15:21:23 +1100193
Ben Lindstrom46c16222000-12-22 01:43:59 +0000194u_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
Damien Miller874d77b2000-10-14 16:23:11 +1100201cipher_mask_ssh1(int client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000203 u_int mask = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100204 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
Damien Miller95def091999-11-25 00:26:21 +1100205 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller874d77b2000-10-14 16:23:11 +1100206 if (client) {
207 mask |= 1 << SSH_CIPHER_DES;
208 }
Damien Miller1383bd82000-04-06 12:32:37 +1000209 return mask;
210}
Damien Miller874d77b2000-10-14 16:23:11 +1100211
Damien Miller86687062014-07-02 15:28:02 +1000212const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100213cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000214{
Damien Miller86687062014-07-02 15:28:02 +1000215 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100216 for (c = ciphers; c->name != NULL; c++)
Darren Tucker660db782005-01-24 21:57:11 +1100217 if (strcmp(c->name, name) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100218 return c;
219 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000220}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller86687062014-07-02 15:28:02 +1000222const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100223cipher_by_number(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224{
Damien Miller86687062014-07-02 15:28:02 +1000225 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100226 for (c = ciphers; c->name != NULL; c++)
227 if (c->number == id)
228 return c;
229 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
Damien Miller78928792000-04-12 20:17:38 +1000232#define CIPHER_SEP ","
233int
234ciphers_valid(const char *names)
235{
Damien Miller86687062014-07-02 15:28:02 +1000236 const struct sshcipher *c;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000237 char *cipher_list, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000238 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000239
Damien Millerb1715dc2000-05-30 13:44:51 +1000240 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000241 return 0;
Damien Miller86687062014-07-02 15:28:02 +1000242 if ((cipher_list = cp = strdup(names)) == NULL)
243 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100244 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100245 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100246 c = cipher_by_name(p);
247 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000248 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000249 return 0;
250 }
251 }
Darren Tuckera627d422013-06-02 07:31:17 +1000252 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000253 return 1;
254}
255
Damien Miller5428f641999-11-25 11:54:57 +1100256/*
257 * Parses the name of the cipher. Returns the number of the corresponding
258 * cipher, or -1 on error.
259 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
261int
262cipher_number(const char *name)
263{
Damien Miller86687062014-07-02 15:28:02 +1000264 const struct sshcipher *c;
Damien Millerb1715dc2000-05-30 13:44:51 +1000265 if (name == NULL)
266 return -1;
Darren Tucker660db782005-01-24 21:57:11 +1100267 for (c = ciphers; c->name != NULL; c++)
268 if (strcasecmp(c->name, name) == 0)
269 return c->number;
270 return -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100271}
272
273char *
274cipher_name(int id)
275{
Damien Miller86687062014-07-02 15:28:02 +1000276 const struct sshcipher *c = cipher_by_number(id);
Damien Miller874d77b2000-10-14 16:23:11 +1100277 return (c==NULL) ? "<unknown>" : c->name;
278}
279
Damien Miller86687062014-07-02 15:28:02 +1000280const char *
281cipher_warning_message(const struct sshcipher_ctx *cc)
282{
283 if (cc == NULL || cc->cipher == NULL)
284 return NULL;
285 if (cc->cipher->number == SSH_CIPHER_DES)
286 return "use of DES is strongly discouraged due to "
287 "cryptographic weaknesses";
288 return NULL;
289}
290
291int
292cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
Damien Miller21cf4e02002-02-19 15:26:42 +1100293 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000294 int do_encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100295{
Damien Miller1f0311c2014-05-15 14:24:09 +1000296#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000297 int ret = SSH_ERR_INTERNAL_ERROR;
Damien Miller21cf4e02002-02-19 15:26:42 +1100298 const EVP_CIPHER *type;
299 int klen;
Damien Miller3710f272005-05-26 12:19:17 +1000300 u_char *junk, *discard;
Damien Miller21cf4e02002-02-19 15:26:42 +1100301
302 if (cipher->number == SSH_CIPHER_DES) {
Damien Miller21cf4e02002-02-19 15:26:42 +1100303 if (keylen > 8)
304 keylen = 8;
305 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000306#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100307 cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
Damien Miller1d75abf2013-01-09 16:12:19 +1100308 cc->encrypt = do_encrypt;
Damien Miller21cf4e02002-02-19 15:26:42 +1100309
Damien Miller86687062014-07-02 15:28:02 +1000310 if (keylen < cipher->key_len ||
311 (iv != NULL && ivlen < cipher_ivlen(cipher)))
312 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller21cf4e02002-02-19 15:26:42 +1100313
Damien Miller86687062014-07-02 15:28:02 +1000314 cc->cipher = cipher;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100315 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
Damien Miller86687062014-07-02 15:28:02 +1000316 return chachapoly_init(&cc->cp_ctx, key, keylen);
Damien Miller0fde8ac2013-11-21 14:12:23 +1100317 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000318#ifndef WITH_OPENSSL
319 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
320 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
321 aesctr_ivsetup(&cc->ac_ctx, iv);
Damien Miller86687062014-07-02 15:28:02 +1000322 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000323 }
324 if ((cc->cipher->flags & CFLAG_NONE) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000325 return 0;
326 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1f0311c2014-05-15 14:24:09 +1000327#else
Damien Miller21cf4e02002-02-19 15:26:42 +1100328 type = (*cipher->evptype)();
Damien Miller21cf4e02002-02-19 15:26:42 +1100329 EVP_CIPHER_CTX_init(&cc->evp);
330 if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
Damien Miller86687062014-07-02 15:28:02 +1000331 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
332 ret = SSH_ERR_LIBCRYPTO_ERROR;
333 goto bad;
334 }
Damien Miller1d75abf2013-01-09 16:12:19 +1100335 if (cipher_authlen(cipher) &&
336 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
Damien Miller86687062014-07-02 15:28:02 +1000337 -1, (u_char *)iv)) {
338 ret = SSH_ERR_LIBCRYPTO_ERROR;
339 goto bad;
340 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100341 klen = EVP_CIPHER_CTX_key_length(&cc->evp);
Damien Millereccb9de2005-06-17 12:59:34 +1000342 if (klen > 0 && keylen != (u_int)klen) {
Damien Miller86687062014-07-02 15:28:02 +1000343 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) {
344 ret = SSH_ERR_LIBCRYPTO_ERROR;
345 goto bad;
346 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100347 }
Damien Miller86687062014-07-02 15:28:02 +1000348 if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
349 ret = SSH_ERR_LIBCRYPTO_ERROR;
350 goto bad;
351 }
Damien Miller3710f272005-05-26 12:19:17 +1000352
Damien Miller46d38de2005-07-17 17:02:09 +1000353 if (cipher->discard_len > 0) {
Damien Miller86687062014-07-02 15:28:02 +1000354 if ((junk = malloc(cipher->discard_len)) == NULL ||
355 (discard = malloc(cipher->discard_len)) == NULL) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000356 free(junk);
Damien Miller86687062014-07-02 15:28:02 +1000357 ret = SSH_ERR_ALLOC_FAIL;
358 goto bad;
359 }
360 ret = EVP_Cipher(&cc->evp, discard, junk, cipher->discard_len);
Damien Millera5103f42014-02-04 11:20:14 +1100361 explicit_bzero(discard, cipher->discard_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000362 free(junk);
363 free(discard);
Damien Miller86687062014-07-02 15:28:02 +1000364 if (ret != 1) {
365 ret = SSH_ERR_LIBCRYPTO_ERROR;
366 bad:
367 EVP_CIPHER_CTX_cleanup(&cc->evp);
368 return ret;
369 }
Damien Miller3710f272005-05-26 12:19:17 +1000370 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000371#endif
Damien Miller86687062014-07-02 15:28:02 +1000372 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100373}
374
Damien Milleraf43a7a2012-12-12 10:46:31 +1100375/*
376 * cipher_crypt() operates as following:
377 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
378 * Theses bytes are treated as additional authenticated data for
379 * authenticated encryption modes.
380 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
Damien Miller1d75abf2013-01-09 16:12:19 +1100381 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
382 * This tag is written on encryption and verified on decryption.
Damien Milleraf43a7a2012-12-12 10:46:31 +1100383 * Both 'aadlen' and 'authlen' can be set to 0.
384 */
Damien Millerbcd00ab2013-12-07 10:41:55 +1100385int
Damien Miller86687062014-07-02 15:28:02 +1000386cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
387 const u_char *src, u_int len, u_int aadlen, u_int authlen)
Damien Miller874d77b2000-10-14 16:23:11 +1100388{
Damien Miller86687062014-07-02 15:28:02 +1000389 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
390 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
391 len, aadlen, authlen, cc->encrypt);
392 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000393#ifndef WITH_OPENSSL
394 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
395 if (aadlen)
396 memcpy(dest, src, aadlen);
397 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
398 dest + aadlen, len);
399 return 0;
400 }
401 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
402 memcpy(dest, src, aadlen + len);
403 return 0;
404 }
Damien Miller86687062014-07-02 15:28:02 +1000405 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1f0311c2014-05-15 14:24:09 +1000406#else
Damien Miller1d75abf2013-01-09 16:12:19 +1100407 if (authlen) {
408 u_char lastiv[1];
409
410 if (authlen != cipher_authlen(cc->cipher))
Damien Miller86687062014-07-02 15:28:02 +1000411 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1d75abf2013-01-09 16:12:19 +1100412 /* increment IV */
413 if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
414 1, lastiv))
Damien Miller86687062014-07-02 15:28:02 +1000415 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100416 /* set tag on decyption */
417 if (!cc->encrypt &&
418 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
419 authlen, (u_char *)src + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000420 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100421 }
422 if (aadlen) {
423 if (authlen &&
424 EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000425 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100426 memcpy(dest, src, aadlen);
Damien Miller1d75abf2013-01-09 16:12:19 +1100427 }
Damien Miller874d77b2000-10-14 16:23:11 +1100428 if (len % cc->cipher->block_size)
Damien Miller86687062014-07-02 15:28:02 +1000429 return SSH_ERR_INVALID_ARGUMENT;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100430 if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
431 len) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000432 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100433 if (authlen) {
434 /* compute tag (on encrypt) or verify tag (on decrypt) */
Damien Miller86687062014-07-02 15:28:02 +1000435 if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0)
436 return cc->encrypt ?
437 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
Damien Miller1d75abf2013-01-09 16:12:19 +1100438 if (cc->encrypt &&
439 !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
440 authlen, dest + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000441 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100442 }
Damien Millerbcd00ab2013-12-07 10:41:55 +1100443 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000444#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100445}
446
Damien Miller0fde8ac2013-11-21 14:12:23 +1100447/* Extract the packet length, including any decryption necessary beforehand */
448int
Damien Miller86687062014-07-02 15:28:02 +1000449cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100450 const u_char *cp, u_int len)
451{
452 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
453 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
454 cp, len);
455 if (len < 4)
Damien Miller86687062014-07-02 15:28:02 +1000456 return SSH_ERR_MESSAGE_INCOMPLETE;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100457 *plenp = get_u32(cp);
458 return 0;
459}
460
Damien Miller86687062014-07-02 15:28:02 +1000461int
462cipher_cleanup(struct sshcipher_ctx *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100463{
Damien Miller86687062014-07-02 15:28:02 +1000464 if (cc == NULL || cc->cipher == NULL)
465 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100466 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
Damien Millera5103f42014-02-04 11:20:14 +1100467 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
Damien Miller1f0311c2014-05-15 14:24:09 +1000468 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
469 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
470#ifdef WITH_OPENSSL
Damien Miller0fde8ac2013-11-21 14:12:23 +1100471 else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
Damien Miller86687062014-07-02 15:28:02 +1000472 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1f0311c2014-05-15 14:24:09 +1000473#endif
Damien Miller86687062014-07-02 15:28:02 +1000474 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
476
Damien Miller5428f641999-11-25 11:54:57 +1100477/*
478 * Selects the cipher, and keys if by computing the MD5 checksum of the
479 * passphrase and using the resulting 16 bytes as the key.
480 */
Damien Miller86687062014-07-02 15:28:02 +1000481int
482cipher_set_key_string(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000483 const char *passphrase, int do_encrypt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000485 u_char digest[16];
Damien Miller86687062014-07-02 15:28:02 +1000486 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
Damien Miller86687062014-07-02 15:28:02 +1000488 if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
489 passphrase, strlen(passphrase),
490 digest, sizeof(digest))) != 0)
491 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100492
Damien Miller86687062014-07-02 15:28:02 +1000493 r = cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
494 out:
Damien Millera5103f42014-02-04 11:20:14 +1100495 explicit_bzero(digest, sizeof(digest));
Damien Miller86687062014-07-02 15:28:02 +1000496 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497}
Damien Miller21cf4e02002-02-19 15:26:42 +1100498
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000499/*
Damien Miller86687062014-07-02 15:28:02 +1000500 * Exports an IV from the sshcipher_ctx required to export the key
Ben Lindstrom212faca2002-03-22 01:39:44 +0000501 * state back from the unprivileged child to the privileged parent
502 * process.
503 */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000504int
Damien Miller86687062014-07-02 15:28:02 +1000505cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000506{
Damien Miller86687062014-07-02 15:28:02 +1000507 const struct sshcipher *c = cc->cipher;
Damien Miller1f0311c2014-05-15 14:24:09 +1000508 int ivlen = 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000509
510 if (c->number == SSH_CIPHER_3DES)
511 ivlen = 24;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100512 else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
513 ivlen = 0;
djm@openbsd.org540e8912015-01-14 10:29:45 +0000514 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
515 ivlen = sizeof(cc->ac_ctx.ctr);
Damien Miller1f0311c2014-05-15 14:24:09 +1000516#ifdef WITH_OPENSSL
Ben Lindstrom212faca2002-03-22 01:39:44 +0000517 else
518 ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
Damien Miller86687062014-07-02 15:28:02 +1000519#endif /* WITH_OPENSSL */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000520 return (ivlen);
521}
522
Damien Miller86687062014-07-02 15:28:02 +1000523int
524cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000525{
Damien Miller86687062014-07-02 15:28:02 +1000526 const struct sshcipher *c = cc->cipher;
Damien Miller1f0311c2014-05-15 14:24:09 +1000527#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000528 int evplen;
Damien Miller1f0311c2014-05-15 14:24:09 +1000529#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000530
Damien Miller0fde8ac2013-11-21 14:12:23 +1100531 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
532 if (len != 0)
Damien Miller86687062014-07-02 15:28:02 +1000533 return SSH_ERR_INVALID_ARGUMENT;
534 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100535 }
djm@openbsd.org540e8912015-01-14 10:29:45 +0000536 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
537 if (len != sizeof(cc->ac_ctx.ctr))
538 return SSH_ERR_INVALID_ARGUMENT;
539 memcpy(iv, cc->ac_ctx.ctr, len);
540 return 0;
541 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000542 if ((cc->cipher->flags & CFLAG_NONE) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000543 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100544
Ben Lindstrom212faca2002-03-22 01:39:44 +0000545 switch (c->number) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000546#ifdef WITH_OPENSSL
Ben Lindstrom212faca2002-03-22 01:39:44 +0000547 case SSH_CIPHER_SSH2:
548 case SSH_CIPHER_DES:
549 case SSH_CIPHER_BLOWFISH:
550 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
551 if (evplen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000552 return 0;
553 else if (evplen < 0)
554 return SSH_ERR_LIBCRYPTO_ERROR;
555 if ((u_int)evplen != len)
556 return SSH_ERR_INVALID_ARGUMENT;
Darren Tucker55731712014-07-21 02:24:59 +1000557#ifndef OPENSSL_HAVE_EVPCTR
558 if (c->evptype == evp_aes_128_ctr)
559 ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
560 else
561#endif
Damien Millereae88742014-05-27 14:27:02 +1000562 if (cipher_authlen(c)) {
Damien Miller86687062014-07-02 15:28:02 +1000563 if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
564 len, iv))
565 return SSH_ERR_LIBCRYPTO_ERROR;
566 } else
567 memcpy(iv, cc->evp.iv, len);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000568 break;
Damien Miller86687062014-07-02 15:28:02 +1000569#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000570#ifdef WITH_SSH1
Damien Millera201bb32003-05-14 13:41:23 +1000571 case SSH_CIPHER_3DES:
Damien Miller86687062014-07-02 15:28:02 +1000572 return ssh1_3des_iv(&cc->evp, 0, iv, 24);
573#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000574 default:
Damien Miller86687062014-07-02 15:28:02 +1000575 return SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000576 }
Damien Miller86687062014-07-02 15:28:02 +1000577 return 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000578}
579
Ben Lindstrom212faca2002-03-22 01:39:44 +0000580int
Damien Miller86687062014-07-02 15:28:02 +1000581cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
582{
583 const struct sshcipher *c = cc->cipher;
584#ifdef WITH_OPENSSL
585 int evplen = 0;
586#endif
587
588 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
589 return 0;
590 if ((cc->cipher->flags & CFLAG_NONE) != 0)
591 return 0;
592
593 switch (c->number) {
594#ifdef WITH_OPENSSL
595 case SSH_CIPHER_SSH2:
596 case SSH_CIPHER_DES:
597 case SSH_CIPHER_BLOWFISH:
598 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
599 if (evplen <= 0)
600 return SSH_ERR_LIBCRYPTO_ERROR;
601 if (cipher_authlen(c)) {
602 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
603 if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
604 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
605 return SSH_ERR_LIBCRYPTO_ERROR;
606 } else
607 memcpy(cc->evp.iv, iv, evplen);
608 break;
609#endif
610#ifdef WITH_SSH1
611 case SSH_CIPHER_3DES:
612 return ssh1_3des_iv(&cc->evp, 1, (u_char *)iv, 24);
613#endif
614 default:
615 return SSH_ERR_INVALID_ARGUMENT;
616 }
617 return 0;
618}
619
620#ifdef WITH_OPENSSL
621#define EVP_X_STATE(evp) (evp).cipher_data
622#define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
623#endif
624
625int
626cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000627{
Damien Miller1f0311c2014-05-15 14:24:09 +1000628#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000629 const struct sshcipher *c = cc->cipher;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000630 int plen = 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000631
Damien Millerf0a8ded2013-02-12 11:00:34 +1100632 if (c->evptype == EVP_rc4) {
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000633 plen = EVP_X_STATE_LEN(cc->evp);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000634 if (dat == NULL)
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000635 return (plen);
636 memcpy(dat, EVP_X_STATE(cc->evp), plen);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000637 }
Ben Lindstrom212faca2002-03-22 01:39:44 +0000638 return (plen);
Damien Miller1f0311c2014-05-15 14:24:09 +1000639#else
Damien Miller86687062014-07-02 15:28:02 +1000640 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000641#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000642}
643
644void
Damien Miller86687062014-07-02 15:28:02 +1000645cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000646{
Damien Miller1f0311c2014-05-15 14:24:09 +1000647#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000648 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000649 int plen;
650
Damien Millerf0a8ded2013-02-12 11:00:34 +1100651 if (c->evptype == EVP_rc4) {
Ben Lindstrom212faca2002-03-22 01:39:44 +0000652 plen = EVP_X_STATE_LEN(cc->evp);
653 memcpy(EVP_X_STATE(cc->evp), dat, plen);
654 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000655#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100656}