blob: 2def333b14d28f11af77d5ac32eae4aad78c0723 [file] [log] [blame]
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00001/* $OpenBSD: cipher.c,v 1.102 2016/08/03 05:41:57 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 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
djm@openbsd.org4706c1d2016-08-03 05:41:57 +000060struct sshcipher_ctx {
61 int plaintext;
62 int encrypt;
63 EVP_CIPHER_CTX *evp;
64 struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
65 struct aesctr_ctx ac_ctx; /* XXX union with evp? */
66 const struct sshcipher *cipher;
67};
68
Damien Miller86687062014-07-02 15:28:02 +100069struct sshcipher {
Damien Miller963f6b22002-02-19 15:21:23 +110070 char *name;
71 int number; /* for ssh1 only */
72 u_int block_size;
73 u_int key_len;
Damien Millerd522c682013-01-09 16:42:47 +110074 u_int iv_len; /* defaults to block_size */
75 u_int auth_len;
Damien Miller3710f272005-05-26 12:19:17 +100076 u_int discard_len;
Damien Miller0fde8ac2013-11-21 14:12:23 +110077 u_int flags;
78#define CFLAG_CBC (1<<0)
79#define CFLAG_CHACHAPOLY (1<<1)
Damien Miller1f0311c2014-05-15 14:24:09 +100080#define CFLAG_AESCTR (1<<2)
81#define CFLAG_NONE (1<<3)
82#ifdef WITH_OPENSSL
Ben Lindstrom6a246412002-06-06 19:48:16 +000083 const EVP_CIPHER *(*evptype)(void);
Damien Miller1f0311c2014-05-15 14:24:09 +100084#else
85 void *ignored;
86#endif
Damien Millerea111192013-04-23 19:24:32 +100087};
88
Damien Miller86687062014-07-02 15:28:02 +100089static const struct sshcipher ciphers[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +100090#ifdef WITH_SSH1
Damien Miller1d75abf2013-01-09 16:12:19 +110091 { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
92 { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
Damien Miller832b7442016-07-15 14:45:34 +100093# ifndef OPENSSL_NO_BF
Damien Miller1d75abf2013-01-09 16:12:19 +110094 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
Damien Miller832b7442016-07-15 14:45:34 +100095# endif /* OPENSSL_NO_BF */
Damien Miller86687062014-07-02 15:28:02 +100096#endif /* WITH_SSH1 */
Damien Miller1f0311c2014-05-15 14:24:09 +100097#ifdef WITH_OPENSSL
98 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
Damien Miller1d75abf2013-01-09 16:12:19 +110099 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
Damien Miller832b7442016-07-15 14:45:34 +1000100# ifndef OPENSSL_NO_BF
Damien Miller1d75abf2013-01-09 16:12:19 +1100101 { "blowfish-cbc",
102 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
Damien Miller832b7442016-07-15 14:45:34 +1000103# endif /* OPENSSL_NO_BF */
104# ifndef OPENSSL_NO_CAST
Damien Miller1d75abf2013-01-09 16:12:19 +1100105 { "cast128-cbc",
106 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
Damien Miller832b7442016-07-15 14:45:34 +1000107# endif /* OPENSSL_NO_CAST */
108# ifndef OPENSSL_NO_RC4
Damien Miller1d75abf2013-01-09 16:12:19 +1100109 { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
110 { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
111 { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
Damien Miller832b7442016-07-15 14:45:34 +1000112# endif /* OPENSSL_NO_RC4 */
Damien Miller1d75abf2013-01-09 16:12:19 +1100113 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
114 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
115 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
116 { "rijndael-cbc@lysator.liu.se",
117 SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
118 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
119 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
120 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
Damien Miller86687062014-07-02 15:28:02 +1000121# ifdef OPENSSL_HAVE_EVPGCM
Damien Miller1d75abf2013-01-09 16:12:19 +1100122 { "aes128-gcm@openssh.com",
123 SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
124 { "aes256-gcm@openssh.com",
125 SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
Damien Miller86687062014-07-02 15:28:02 +1000126# endif /* OPENSSL_HAVE_EVPGCM */
Damien Miller1f0311c2014-05-15 14:24:09 +1000127#else /* WITH_OPENSSL */
128 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
129 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
130 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
131 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
132#endif /* WITH_OPENSSL */
Damien Miller0fde8ac2013-11-21 14:12:23 +1100133 { "chacha20-poly1305@openssh.com",
134 SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
Damien Miller86687062014-07-02 15:28:02 +1000135
Damien Miller1d75abf2013-01-09 16:12:19 +1100136 { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137};
138
Damien Miller874d77b2000-10-14 16:23:11 +1100139/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller86687062014-07-02 15:28:02 +1000141/* Returns a comma-separated list of supported ciphers. */
Damien Millerea111192013-04-23 19:24:32 +1000142char *
Damien Miller0fde8ac2013-11-21 14:12:23 +1100143cipher_alg_list(char sep, int auth_only)
Damien Millerea111192013-04-23 19:24:32 +1000144{
Damien Miller86687062014-07-02 15:28:02 +1000145 char *tmp, *ret = NULL;
Damien Millerea111192013-04-23 19:24:32 +1000146 size_t nlen, rlen = 0;
Damien Miller86687062014-07-02 15:28:02 +1000147 const struct sshcipher *c;
Damien Millerea111192013-04-23 19:24:32 +1000148
149 for (c = ciphers; c->name != NULL; c++) {
150 if (c->number != SSH_CIPHER_SSH2)
151 continue;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100152 if (auth_only && c->auth_len == 0)
153 continue;
Damien Millerea111192013-04-23 19:24:32 +1000154 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100155 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000156 nlen = strlen(c->name);
Damien Miller86687062014-07-02 15:28:02 +1000157 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
158 free(ret);
159 return NULL;
160 }
161 ret = tmp;
Damien Millerea111192013-04-23 19:24:32 +1000162 memcpy(ret + rlen, c->name, nlen + 1);
163 rlen += nlen;
164 }
165 return ret;
166}
167
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000168u_int
Damien Miller86687062014-07-02 15:28:02 +1000169cipher_blocksize(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100170{
171 return (c->block_size);
172}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000173
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000174u_int
Damien Miller86687062014-07-02 15:28:02 +1000175cipher_keylen(const struct sshcipher *c)
Damien Miller963f6b22002-02-19 15:21:23 +1100176{
177 return (c->key_len);
178}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000179
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000180u_int
Damien Miller86687062014-07-02 15:28:02 +1000181cipher_seclen(const struct sshcipher *c)
Damien Miller76eea4a2014-01-26 09:37:25 +1100182{
183 if (strcmp("3des-cbc", c->name) == 0)
184 return 14;
185 return cipher_keylen(c);
186}
187
188u_int
Damien Miller86687062014-07-02 15:28:02 +1000189cipher_authlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100190{
191 return (c->auth_len);
192}
193
194u_int
Damien Miller86687062014-07-02 15:28:02 +1000195cipher_ivlen(const struct sshcipher *c)
Damien Miller1d75abf2013-01-09 16:12:19 +1100196{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100197 /*
198 * Default is cipher block size, except for chacha20+poly1305 that
199 * needs no IV. XXX make iv_len == -1 default?
200 */
201 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
202 c->iv_len : c->block_size;
Damien Miller1d75abf2013-01-09 16:12:19 +1100203}
204
205u_int
Damien Miller86687062014-07-02 15:28:02 +1000206cipher_get_number(const struct sshcipher *c)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000207{
208 return (c->number);
209}
Damien Miller963f6b22002-02-19 15:21:23 +1100210
Ben Lindstrom46c16222000-12-22 01:43:59 +0000211u_int
Damien Miller86687062014-07-02 15:28:02 +1000212cipher_is_cbc(const struct sshcipher *c)
Damien Miller13ae44c2009-01-28 16:38:41 +1100213{
Damien Miller0fde8ac2013-11-21 14:12:23 +1100214 return (c->flags & CFLAG_CBC) != 0;
Damien Miller13ae44c2009-01-28 16:38:41 +1100215}
216
217u_int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000218cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
219{
220 return cc->plaintext;
221}
222
223u_int
224cipher_ctx_get_number(struct sshcipher_ctx *cc)
225{
226 return cc->cipher->number;
227}
228
229u_int
Damien Miller874d77b2000-10-14 16:23:11 +1100230cipher_mask_ssh1(int client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000232 u_int mask = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100233 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
Damien Miller95def091999-11-25 00:26:21 +1100234 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller874d77b2000-10-14 16:23:11 +1100235 if (client) {
236 mask |= 1 << SSH_CIPHER_DES;
237 }
Damien Miller1383bd82000-04-06 12:32:37 +1000238 return mask;
239}
Damien Miller874d77b2000-10-14 16:23:11 +1100240
Damien Miller86687062014-07-02 15:28:02 +1000241const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100242cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000243{
Damien Miller86687062014-07-02 15:28:02 +1000244 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100245 for (c = ciphers; c->name != NULL; c++)
Darren Tucker660db782005-01-24 21:57:11 +1100246 if (strcmp(c->name, name) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100247 return c;
248 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000249}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Damien Miller86687062014-07-02 15:28:02 +1000251const struct sshcipher *
Damien Miller874d77b2000-10-14 16:23:11 +1100252cipher_by_number(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253{
Damien Miller86687062014-07-02 15:28:02 +1000254 const struct sshcipher *c;
Damien Miller874d77b2000-10-14 16:23:11 +1100255 for (c = ciphers; c->name != NULL; c++)
256 if (c->number == id)
257 return c;
258 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259}
260
Damien Miller78928792000-04-12 20:17:38 +1000261#define CIPHER_SEP ","
262int
263ciphers_valid(const char *names)
264{
Damien Miller86687062014-07-02 15:28:02 +1000265 const struct sshcipher *c;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000266 char *cipher_list, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000267 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000268
Damien Millerb1715dc2000-05-30 13:44:51 +1000269 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000270 return 0;
Damien Miller86687062014-07-02 15:28:02 +1000271 if ((cipher_list = cp = strdup(names)) == NULL)
272 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100273 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100274 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100275 c = cipher_by_name(p);
276 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000277 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000278 return 0;
279 }
280 }
Darren Tuckera627d422013-06-02 07:31:17 +1000281 free(cipher_list);
Damien Miller78928792000-04-12 20:17:38 +1000282 return 1;
283}
284
Damien Miller5428f641999-11-25 11:54:57 +1100285/*
286 * Parses the name of the cipher. Returns the number of the corresponding
287 * cipher, or -1 on error.
288 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289
290int
291cipher_number(const char *name)
292{
Damien Miller86687062014-07-02 15:28:02 +1000293 const struct sshcipher *c;
Damien Millerb1715dc2000-05-30 13:44:51 +1000294 if (name == NULL)
295 return -1;
Darren Tucker660db782005-01-24 21:57:11 +1100296 for (c = ciphers; c->name != NULL; c++)
297 if (strcasecmp(c->name, name) == 0)
298 return c->number;
299 return -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100300}
301
302char *
303cipher_name(int id)
304{
Damien Miller86687062014-07-02 15:28:02 +1000305 const struct sshcipher *c = cipher_by_number(id);
Damien Miller874d77b2000-10-14 16:23:11 +1100306 return (c==NULL) ? "<unknown>" : c->name;
307}
308
Damien Miller86687062014-07-02 15:28:02 +1000309const char *
310cipher_warning_message(const struct sshcipher_ctx *cc)
311{
312 if (cc == NULL || cc->cipher == NULL)
313 return NULL;
314 if (cc->cipher->number == SSH_CIPHER_DES)
315 return "use of DES is strongly discouraged due to "
316 "cryptographic weaknesses";
317 return NULL;
318}
319
320int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000321cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
Damien Miller21cf4e02002-02-19 15:26:42 +1100322 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000323 int do_encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100324{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000325 struct sshcipher_ctx *cc = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000326 int ret = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000327#ifdef WITH_OPENSSL
Damien Miller21cf4e02002-02-19 15:26:42 +1100328 const EVP_CIPHER *type;
329 int klen;
Damien Miller3710f272005-05-26 12:19:17 +1000330 u_char *junk, *discard;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000331#endif
332
333 *ccp = NULL;
334 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
335 return SSH_ERR_ALLOC_FAIL;
Damien Miller21cf4e02002-02-19 15:26:42 +1100336
337 if (cipher->number == SSH_CIPHER_DES) {
Damien Miller21cf4e02002-02-19 15:26:42 +1100338 if (keylen > 8)
339 keylen = 8;
340 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000341
Damien Miller21cf4e02002-02-19 15:26:42 +1100342 cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
Damien Miller1d75abf2013-01-09 16:12:19 +1100343 cc->encrypt = do_encrypt;
Damien Miller21cf4e02002-02-19 15:26:42 +1100344
Damien Miller86687062014-07-02 15:28:02 +1000345 if (keylen < cipher->key_len ||
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000346 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
347 ret = SSH_ERR_INVALID_ARGUMENT;
348 goto out;
349 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100350
Damien Miller86687062014-07-02 15:28:02 +1000351 cc->cipher = cipher;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100352 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000353 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
354 goto out;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100355 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000356#ifndef WITH_OPENSSL
357 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
358 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
359 aesctr_ivsetup(&cc->ac_ctx, iv);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000360 ret = 0;
361 goto out;
Damien Miller1f0311c2014-05-15 14:24:09 +1000362 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000363 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
364 ret = 0;
365 goto out;
366 }
367 ret = SSH_ERR_INVALID_ARGUMENT;
368 goto out;
369#else /* WITH_OPENSSL */
Damien Miller21cf4e02002-02-19 15:26:42 +1100370 type = (*cipher->evptype)();
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000371 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
372 ret = SSH_ERR_ALLOC_FAIL;
373 goto out;
374 }
375 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
Damien Miller86687062014-07-02 15:28:02 +1000376 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
377 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000378 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000379 }
Damien Miller1d75abf2013-01-09 16:12:19 +1100380 if (cipher_authlen(cipher) &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000381 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
Damien Miller86687062014-07-02 15:28:02 +1000382 -1, (u_char *)iv)) {
383 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000384 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000385 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000386 klen = EVP_CIPHER_CTX_key_length(cc->evp);
Damien Millereccb9de2005-06-17 12:59:34 +1000387 if (klen > 0 && keylen != (u_int)klen) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000388 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000389 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000390 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000391 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100392 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000393 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000394 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000395 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000396 }
Damien Miller3710f272005-05-26 12:19:17 +1000397
Damien Miller46d38de2005-07-17 17:02:09 +1000398 if (cipher->discard_len > 0) {
Damien Miller86687062014-07-02 15:28:02 +1000399 if ((junk = malloc(cipher->discard_len)) == NULL ||
400 (discard = malloc(cipher->discard_len)) == NULL) {
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000401 free(junk);
Damien Miller86687062014-07-02 15:28:02 +1000402 ret = SSH_ERR_ALLOC_FAIL;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000403 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000404 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000405 ret = EVP_Cipher(cc->evp, discard, junk, cipher->discard_len);
Damien Millera5103f42014-02-04 11:20:14 +1100406 explicit_bzero(discard, cipher->discard_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000407 free(junk);
408 free(discard);
Damien Miller86687062014-07-02 15:28:02 +1000409 if (ret != 1) {
410 ret = SSH_ERR_LIBCRYPTO_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000411 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000412 }
Damien Miller3710f272005-05-26 12:19:17 +1000413 }
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000414 ret = 0;
415#endif /* WITH_OPENSSL */
416 out:
417 if (ret == 0) {
418 /* success */
419 *ccp = cc;
420 } else {
421 if (cc != NULL) {
422#ifdef WITH_OPENSSL
423 if (cc->evp != NULL)
424 EVP_CIPHER_CTX_free(cc->evp);
425#endif /* WITH_OPENSSL */
426 explicit_bzero(cc, sizeof(*cc));
427 free(cc);
428 }
429 }
430 return ret;
Damien Miller874d77b2000-10-14 16:23:11 +1100431}
432
Damien Milleraf43a7a2012-12-12 10:46:31 +1100433/*
434 * cipher_crypt() operates as following:
435 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
436 * Theses bytes are treated as additional authenticated data for
437 * authenticated encryption modes.
438 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
Damien Miller1d75abf2013-01-09 16:12:19 +1100439 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
440 * This tag is written on encryption and verified on decryption.
Damien Milleraf43a7a2012-12-12 10:46:31 +1100441 * Both 'aadlen' and 'authlen' can be set to 0.
442 */
Damien Millerbcd00ab2013-12-07 10:41:55 +1100443int
Damien Miller86687062014-07-02 15:28:02 +1000444cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
445 const u_char *src, u_int len, u_int aadlen, u_int authlen)
Damien Miller874d77b2000-10-14 16:23:11 +1100446{
Damien Miller86687062014-07-02 15:28:02 +1000447 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
448 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
449 len, aadlen, authlen, cc->encrypt);
450 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000451#ifndef WITH_OPENSSL
452 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
453 if (aadlen)
454 memcpy(dest, src, aadlen);
455 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
456 dest + aadlen, len);
457 return 0;
458 }
459 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
460 memcpy(dest, src, aadlen + len);
461 return 0;
462 }
Damien Miller86687062014-07-02 15:28:02 +1000463 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1f0311c2014-05-15 14:24:09 +1000464#else
Damien Miller1d75abf2013-01-09 16:12:19 +1100465 if (authlen) {
466 u_char lastiv[1];
467
468 if (authlen != cipher_authlen(cc->cipher))
Damien Miller86687062014-07-02 15:28:02 +1000469 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller1d75abf2013-01-09 16:12:19 +1100470 /* increment IV */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000471 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Damien Miller1d75abf2013-01-09 16:12:19 +1100472 1, lastiv))
Damien Miller86687062014-07-02 15:28:02 +1000473 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100474 /* set tag on decyption */
475 if (!cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000476 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100477 authlen, (u_char *)src + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000478 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100479 }
480 if (aadlen) {
481 if (authlen &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000482 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000483 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Milleraf43a7a2012-12-12 10:46:31 +1100484 memcpy(dest, src, aadlen);
Damien Miller1d75abf2013-01-09 16:12:19 +1100485 }
Damien Miller874d77b2000-10-14 16:23:11 +1100486 if (len % cc->cipher->block_size)
Damien Miller86687062014-07-02 15:28:02 +1000487 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000488 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
Damien Milleraf43a7a2012-12-12 10:46:31 +1100489 len) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000490 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100491 if (authlen) {
492 /* compute tag (on encrypt) or verify tag (on decrypt) */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000493 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000494 return cc->encrypt ?
495 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
Damien Miller1d75abf2013-01-09 16:12:19 +1100496 if (cc->encrypt &&
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000497 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
Damien Miller1d75abf2013-01-09 16:12:19 +1100498 authlen, dest + aadlen + len))
Damien Miller86687062014-07-02 15:28:02 +1000499 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller1d75abf2013-01-09 16:12:19 +1100500 }
Damien Millerbcd00ab2013-12-07 10:41:55 +1100501 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000502#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100503}
504
Damien Miller0fde8ac2013-11-21 14:12:23 +1100505/* Extract the packet length, including any decryption necessary beforehand */
506int
Damien Miller86687062014-07-02 15:28:02 +1000507cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
Damien Miller0fde8ac2013-11-21 14:12:23 +1100508 const u_char *cp, u_int len)
509{
510 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
511 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
512 cp, len);
513 if (len < 4)
Damien Miller86687062014-07-02 15:28:02 +1000514 return SSH_ERR_MESSAGE_INCOMPLETE;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100515 *plenp = get_u32(cp);
516 return 0;
517}
518
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000519void
520cipher_free(struct sshcipher_ctx *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100521{
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000522 if (cc == NULL)
523 return;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100524 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
Damien Millera5103f42014-02-04 11:20:14 +1100525 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
Damien Miller1f0311c2014-05-15 14:24:09 +1000526 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
527 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
528#ifdef WITH_OPENSSL
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000529 if (cc->evp != NULL) {
530 EVP_CIPHER_CTX_free(cc->evp);
531 cc->evp = NULL;
532 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000533#endif
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000534 explicit_bzero(cc, sizeof(*cc));
535 free(cc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536}
537
Damien Miller5428f641999-11-25 11:54:57 +1100538/*
539 * Selects the cipher, and keys if by computing the MD5 checksum of the
540 * passphrase and using the resulting 16 bytes as the key.
541 */
Damien Miller86687062014-07-02 15:28:02 +1000542int
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000543cipher_set_key_string(struct sshcipher_ctx **ccp,
544 const struct sshcipher *cipher, const char *passphrase, int do_encrypt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000546 u_char digest[16];
Damien Miller86687062014-07-02 15:28:02 +1000547 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548
Damien Miller86687062014-07-02 15:28:02 +1000549 if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
550 passphrase, strlen(passphrase),
551 digest, sizeof(digest))) != 0)
552 goto out;
Damien Miller95def091999-11-25 00:26:21 +1100553
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000554 r = cipher_init(ccp, cipher, digest, 16, NULL, 0, do_encrypt);
Damien Miller86687062014-07-02 15:28:02 +1000555 out:
Damien Millera5103f42014-02-04 11:20:14 +1100556 explicit_bzero(digest, sizeof(digest));
Damien Miller86687062014-07-02 15:28:02 +1000557 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558}
Damien Miller21cf4e02002-02-19 15:26:42 +1100559
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000560/*
Damien Miller86687062014-07-02 15:28:02 +1000561 * Exports an IV from the sshcipher_ctx required to export the key
Ben Lindstrom212faca2002-03-22 01:39:44 +0000562 * state back from the unprivileged child to the privileged parent
563 * process.
564 */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000565int
Damien Miller86687062014-07-02 15:28:02 +1000566cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000567{
Damien Miller86687062014-07-02 15:28:02 +1000568 const struct sshcipher *c = cc->cipher;
Damien Miller1f0311c2014-05-15 14:24:09 +1000569 int ivlen = 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000570
571 if (c->number == SSH_CIPHER_3DES)
572 ivlen = 24;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100573 else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
574 ivlen = 0;
djm@openbsd.org540e8912015-01-14 10:29:45 +0000575 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
576 ivlen = sizeof(cc->ac_ctx.ctr);
Damien Miller1f0311c2014-05-15 14:24:09 +1000577#ifdef WITH_OPENSSL
Ben Lindstrom212faca2002-03-22 01:39:44 +0000578 else
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000579 ivlen = EVP_CIPHER_CTX_iv_length(cc->evp);
Damien Miller86687062014-07-02 15:28:02 +1000580#endif /* WITH_OPENSSL */
Ben Lindstrom212faca2002-03-22 01:39:44 +0000581 return (ivlen);
582}
583
Damien Miller86687062014-07-02 15:28:02 +1000584int
585cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000586{
Damien Miller86687062014-07-02 15:28:02 +1000587 const struct sshcipher *c = cc->cipher;
Damien Miller1f0311c2014-05-15 14:24:09 +1000588#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000589 int evplen;
Damien Miller1f0311c2014-05-15 14:24:09 +1000590#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000591
Damien Miller0fde8ac2013-11-21 14:12:23 +1100592 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
593 if (len != 0)
Damien Miller86687062014-07-02 15:28:02 +1000594 return SSH_ERR_INVALID_ARGUMENT;
595 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100596 }
djm@openbsd.org540e8912015-01-14 10:29:45 +0000597 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
598 if (len != sizeof(cc->ac_ctx.ctr))
599 return SSH_ERR_INVALID_ARGUMENT;
600 memcpy(iv, cc->ac_ctx.ctr, len);
601 return 0;
602 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000603 if ((cc->cipher->flags & CFLAG_NONE) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000604 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +1100605
Ben Lindstrom212faca2002-03-22 01:39:44 +0000606 switch (c->number) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000607#ifdef WITH_OPENSSL
Ben Lindstrom212faca2002-03-22 01:39:44 +0000608 case SSH_CIPHER_SSH2:
609 case SSH_CIPHER_DES:
610 case SSH_CIPHER_BLOWFISH:
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000611 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000612 if (evplen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000613 return 0;
614 else if (evplen < 0)
615 return SSH_ERR_LIBCRYPTO_ERROR;
616 if ((u_int)evplen != len)
617 return SSH_ERR_INVALID_ARGUMENT;
Darren Tucker55731712014-07-21 02:24:59 +1000618#ifndef OPENSSL_HAVE_EVPCTR
619 if (c->evptype == evp_aes_128_ctr)
Damien Miller315d2a42016-10-28 14:34:07 +1100620 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
Darren Tucker55731712014-07-21 02:24:59 +1000621 else
622#endif
Damien Millereae88742014-05-27 14:27:02 +1000623 if (cipher_authlen(c)) {
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000624 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Damien Miller86687062014-07-02 15:28:02 +1000625 len, iv))
626 return SSH_ERR_LIBCRYPTO_ERROR;
627 } else
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000628 memcpy(iv, cc->evp->iv, len);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000629 break;
Damien Miller86687062014-07-02 15:28:02 +1000630#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000631#ifdef WITH_SSH1
Damien Millera201bb32003-05-14 13:41:23 +1000632 case SSH_CIPHER_3DES:
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000633 return ssh1_3des_iv(cc->evp, 0, iv, 24);
Damien Miller86687062014-07-02 15:28:02 +1000634#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000635 default:
Damien Miller86687062014-07-02 15:28:02 +1000636 return SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000637 }
Damien Miller86687062014-07-02 15:28:02 +1000638 return 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000639}
640
Ben Lindstrom212faca2002-03-22 01:39:44 +0000641int
Damien Miller86687062014-07-02 15:28:02 +1000642cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
643{
644 const struct sshcipher *c = cc->cipher;
645#ifdef WITH_OPENSSL
646 int evplen = 0;
647#endif
648
649 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
650 return 0;
651 if ((cc->cipher->flags & CFLAG_NONE) != 0)
652 return 0;
653
654 switch (c->number) {
655#ifdef WITH_OPENSSL
656 case SSH_CIPHER_SSH2:
657 case SSH_CIPHER_DES:
658 case SSH_CIPHER_BLOWFISH:
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000659 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
Damien Miller86687062014-07-02 15:28:02 +1000660 if (evplen <= 0)
661 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller315d2a42016-10-28 14:34:07 +1100662#ifndef OPENSSL_HAVE_EVPCTR
663 /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
664 if (c->evptype == evp_aes_128_ctr)
665 ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
666 else
667#endif
Damien Miller86687062014-07-02 15:28:02 +1000668 if (cipher_authlen(c)) {
669 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000670 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
Damien Miller86687062014-07-02 15:28:02 +1000671 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
672 return SSH_ERR_LIBCRYPTO_ERROR;
673 } else
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000674 memcpy(cc->evp->iv, iv, evplen);
Damien Miller86687062014-07-02 15:28:02 +1000675 break;
676#endif
677#ifdef WITH_SSH1
678 case SSH_CIPHER_3DES:
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000679 return ssh1_3des_iv(cc->evp, 1, (u_char *)iv, 24);
Damien Miller86687062014-07-02 15:28:02 +1000680#endif
681 default:
682 return SSH_ERR_INVALID_ARGUMENT;
683 }
684 return 0;
685}
686
687#ifdef WITH_OPENSSL
djm@openbsd.org4706c1d2016-08-03 05:41:57 +0000688#define EVP_X_STATE(evp) (evp)->cipher_data
689#define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
Damien Miller86687062014-07-02 15:28:02 +1000690#endif
691
692int
693cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000694{
Damien Miller832b7442016-07-15 14:45:34 +1000695#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
Damien Miller86687062014-07-02 15:28:02 +1000696 const struct sshcipher *c = cc->cipher;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000697 int plen = 0;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000698
Damien Millerf0a8ded2013-02-12 11:00:34 +1100699 if (c->evptype == EVP_rc4) {
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000700 plen = EVP_X_STATE_LEN(cc->evp);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000701 if (dat == NULL)
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000702 return (plen);
703 memcpy(dat, EVP_X_STATE(cc->evp), plen);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000704 }
Ben Lindstrom212faca2002-03-22 01:39:44 +0000705 return (plen);
Damien Miller1f0311c2014-05-15 14:24:09 +1000706#else
Damien Miller86687062014-07-02 15:28:02 +1000707 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000708#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000709}
710
711void
Damien Miller86687062014-07-02 15:28:02 +1000712cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
Ben Lindstrom212faca2002-03-22 01:39:44 +0000713{
Damien Miller832b7442016-07-15 14:45:34 +1000714#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
Damien Miller86687062014-07-02 15:28:02 +1000715 const struct sshcipher *c = cc->cipher;
Ben Lindstrom212faca2002-03-22 01:39:44 +0000716 int plen;
717
Damien Millerf0a8ded2013-02-12 11:00:34 +1100718 if (c->evptype == EVP_rc4) {
Ben Lindstrom212faca2002-03-22 01:39:44 +0000719 plen = EVP_X_STATE_LEN(cc->evp);
720 memcpy(EVP_X_STATE(cc->evp), dat, plen);
721 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000722#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100723}