blob: 82da6a2b5f8aa108017a75641e2bd798b1cb0cd1 [file] [log] [blame]
Greg Hartman9768ca42017-06-22 20:49:52 -07001/* $OpenBSD: cipher.c,v 1.102 2016/08/03 05:41:57 djm Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 *
7 * 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".
12 *
13 *
14 * Copyright (c) 1999 Niels Provos. All rights reserved.
15 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
16 *
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.
36 */
37
38#include "includes.h"
39
40#include <sys/types.h>
41
Greg Hartmanbd77cf72015-02-25 13:21:06 -080042#include <string.h>
43#include <stdarg.h>
Adam Langleyd0592972015-03-30 14:49:51 -070044#include <stdio.h>
Greg Hartmanbd77cf72015-02-25 13:21:06 -080045
Greg Hartmanbd77cf72015-02-25 13:21:06 -080046#include "cipher.h"
Adam Langleyd0592972015-03-30 14:49:51 -070047#include "misc.h"
48#include "sshbuf.h"
49#include "ssherr.h"
50#include "digest.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080051
Greg Hartmanbd77cf72015-02-25 13:21:06 -080052#include "openbsd-compat/openssl-compat.h"
53
Adam Langleyd0592972015-03-30 14:49:51 -070054#ifdef WITH_SSH1
Greg Hartmanbd77cf72015-02-25 13:21:06 -080055extern const EVP_CIPHER *evp_ssh1_bf(void);
56extern const EVP_CIPHER *evp_ssh1_3des(void);
Adam Langleyd0592972015-03-30 14:49:51 -070057extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
58#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -080059
Greg Hartman9768ca42017-06-22 20:49:52 -070060struct 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
Adam Langleyd0592972015-03-30 14:49:51 -070069struct sshcipher {
Greg Hartmanbd77cf72015-02-25 13:21:06 -080070 char *name;
71 int number; /* for ssh1 only */
72 u_int block_size;
73 u_int key_len;
Adam Langleyd0592972015-03-30 14:49:51 -070074 u_int iv_len; /* defaults to block_size */
75 u_int auth_len;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080076 u_int discard_len;
Adam Langleyd0592972015-03-30 14:49:51 -070077 u_int flags;
78#define CFLAG_CBC (1<<0)
79#define CFLAG_CHACHAPOLY (1<<1)
80#define CFLAG_AESCTR (1<<2)
81#define CFLAG_NONE (1<<3)
82#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -080083 const EVP_CIPHER *(*evptype)(void);
Adam Langleyd0592972015-03-30 14:49:51 -070084#else
85 void *ignored;
86#endif
87};
Greg Hartmanbd77cf72015-02-25 13:21:06 -080088
Adam Langleyd0592972015-03-30 14:49:51 -070089static const struct sshcipher ciphers[] = {
90#ifdef WITH_SSH1
91 { "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 },
Greg Hartman9768ca42017-06-22 20:49:52 -070093# ifndef OPENSSL_NO_BF
Adam Langleyd0592972015-03-30 14:49:51 -070094 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
Greg Hartman9768ca42017-06-22 20:49:52 -070095# endif /* OPENSSL_NO_BF */
Adam Langleyd0592972015-03-30 14:49:51 -070096#endif /* WITH_SSH1 */
97#ifdef WITH_OPENSSL
98 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
99#if !defined(ANDROID)
100 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
Greg Hartman9768ca42017-06-22 20:49:52 -0700101# endif /* ANDROID */
102# ifndef OPENSSL_NO_BF
Adam Langleyd0592972015-03-30 14:49:51 -0700103 { "blowfish-cbc",
104 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
Greg Hartman9768ca42017-06-22 20:49:52 -0700105# endif /* OPENSSL_NO_BF */
106# ifndef OPENSSL_NO_CAST
Adam Langleyd0592972015-03-30 14:49:51 -0700107 { "cast128-cbc",
108 SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
Greg Hartman9768ca42017-06-22 20:49:52 -0700109# endif /* OPENSSL_NO_CAST */
110# ifndef OPENSSL_NO_RC4
Adam Langleyd0592972015-03-30 14:49:51 -0700111 { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
112 { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
113 { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
Greg Hartman9768ca42017-06-22 20:49:52 -0700114# endif /* OPENSSL_NO_RC4 */
Adam Langleyd0592972015-03-30 14:49:51 -0700115 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
116 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
117 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800118 { "rijndael-cbc@lysator.liu.se",
Adam Langleyd0592972015-03-30 14:49:51 -0700119 SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
120 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
121#if !defined(ANDROID)
122 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800123#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700124 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
125# ifdef OPENSSL_HAVE_EVPGCM
126 { "aes128-gcm@openssh.com",
127 SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
128 { "aes256-gcm@openssh.com",
129 SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
130# endif /* OPENSSL_HAVE_EVPGCM */
131#else /* WITH_OPENSSL */
132 { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
133 { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
134 { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
135 { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
136#endif /* WITH_OPENSSL */
137 { "chacha20-poly1305@openssh.com",
138 SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
139
140 { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800141};
142
143/*--*/
144
Adam Langleyd0592972015-03-30 14:49:51 -0700145/* Returns a comma-separated list of supported ciphers. */
146char *
147cipher_alg_list(char sep, int auth_only)
148{
149 char *tmp, *ret = NULL;
150 size_t nlen, rlen = 0;
151 const struct sshcipher *c;
152
153 for (c = ciphers; c->name != NULL; c++) {
154 if (c->number != SSH_CIPHER_SSH2)
155 continue;
156 if (auth_only && c->auth_len == 0)
157 continue;
158 if (ret != NULL)
159 ret[rlen++] = sep;
160 nlen = strlen(c->name);
161 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
162 free(ret);
163 return NULL;
164 }
165 ret = tmp;
166 memcpy(ret + rlen, c->name, nlen + 1);
167 rlen += nlen;
168 }
169 return ret;
170}
171
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800172u_int
Adam Langleyd0592972015-03-30 14:49:51 -0700173cipher_blocksize(const struct sshcipher *c)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800174{
175 return (c->block_size);
176}
177
178u_int
Adam Langleyd0592972015-03-30 14:49:51 -0700179cipher_keylen(const struct sshcipher *c)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800180{
181 return (c->key_len);
182}
183
184u_int
Adam Langleyd0592972015-03-30 14:49:51 -0700185cipher_seclen(const struct sshcipher *c)
186{
187 if (strcmp("3des-cbc", c->name) == 0)
188 return 14;
189 return cipher_keylen(c);
190}
191
192u_int
193cipher_authlen(const struct sshcipher *c)
194{
195 return (c->auth_len);
196}
197
198u_int
199cipher_ivlen(const struct sshcipher *c)
200{
201 /*
202 * Default is cipher block size, except for chacha20+poly1305 that
203 * needs no IV. XXX make iv_len == -1 default?
204 */
205 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
206 c->iv_len : c->block_size;
207}
208
209u_int
210cipher_get_number(const struct sshcipher *c)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800211{
212 return (c->number);
213}
214
215u_int
Adam Langleyd0592972015-03-30 14:49:51 -0700216cipher_is_cbc(const struct sshcipher *c)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800217{
Adam Langleyd0592972015-03-30 14:49:51 -0700218 return (c->flags & CFLAG_CBC) != 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800219}
220
221u_int
Greg Hartman9768ca42017-06-22 20:49:52 -0700222cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
223{
224 return cc->plaintext;
225}
226
227u_int
228cipher_ctx_get_number(struct sshcipher_ctx *cc)
229{
230 return cc->cipher->number;
231}
232
233u_int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800234cipher_mask_ssh1(int client)
235{
236 u_int mask = 0;
237 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
238 mask |= 1 << SSH_CIPHER_BLOWFISH;
239 if (client) {
240 mask |= 1 << SSH_CIPHER_DES;
241 }
242 return mask;
243}
244
Adam Langleyd0592972015-03-30 14:49:51 -0700245const struct sshcipher *
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800246cipher_by_name(const char *name)
247{
Adam Langleyd0592972015-03-30 14:49:51 -0700248 const struct sshcipher *c;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800249 for (c = ciphers; c->name != NULL; c++)
250 if (strcmp(c->name, name) == 0)
251 return c;
252 return NULL;
253}
254
Adam Langleyd0592972015-03-30 14:49:51 -0700255const struct sshcipher *
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800256cipher_by_number(int id)
257{
Adam Langleyd0592972015-03-30 14:49:51 -0700258 const struct sshcipher *c;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800259 for (c = ciphers; c->name != NULL; c++)
260 if (c->number == id)
261 return c;
262 return NULL;
263}
264
265#define CIPHER_SEP ","
266int
267ciphers_valid(const char *names)
268{
Adam Langleyd0592972015-03-30 14:49:51 -0700269 const struct sshcipher *c;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800270 char *cipher_list, *cp;
271 char *p;
272
273 if (names == NULL || strcmp(names, "") == 0)
274 return 0;
Adam Langleyd0592972015-03-30 14:49:51 -0700275 if ((cipher_list = cp = strdup(names)) == NULL)
276 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800277 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
278 (p = strsep(&cp, CIPHER_SEP))) {
279 c = cipher_by_name(p);
280 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
Adam Langleyd0592972015-03-30 14:49:51 -0700281 free(cipher_list);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800282 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800283 }
284 }
Adam Langleyd0592972015-03-30 14:49:51 -0700285 free(cipher_list);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800286 return 1;
287}
288
289/*
290 * Parses the name of the cipher. Returns the number of the corresponding
291 * cipher, or -1 on error.
292 */
293
294int
295cipher_number(const char *name)
296{
Adam Langleyd0592972015-03-30 14:49:51 -0700297 const struct sshcipher *c;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800298 if (name == NULL)
299 return -1;
300 for (c = ciphers; c->name != NULL; c++)
301 if (strcasecmp(c->name, name) == 0)
302 return c->number;
303 return -1;
304}
305
306char *
307cipher_name(int id)
308{
Adam Langleyd0592972015-03-30 14:49:51 -0700309 const struct sshcipher *c = cipher_by_number(id);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800310 return (c==NULL) ? "<unknown>" : c->name;
311}
312
Adam Langleyd0592972015-03-30 14:49:51 -0700313const char *
314cipher_warning_message(const struct sshcipher_ctx *cc)
315{
316 if (cc == NULL || cc->cipher == NULL)
317 return NULL;
318 if (cc->cipher->number == SSH_CIPHER_DES)
319 return "use of DES is strongly discouraged due to "
320 "cryptographic weaknesses";
321 return NULL;
322}
323
324int
Greg Hartman9768ca42017-06-22 20:49:52 -0700325cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800326 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
327 int do_encrypt)
328{
Greg Hartman9768ca42017-06-22 20:49:52 -0700329 struct sshcipher_ctx *cc = NULL;
Adam Langleyd0592972015-03-30 14:49:51 -0700330 int ret = SSH_ERR_INTERNAL_ERROR;
Greg Hartman9768ca42017-06-22 20:49:52 -0700331#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800332 const EVP_CIPHER *type;
333 int klen;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800334 u_char *junk, *discard;
Greg Hartman9768ca42017-06-22 20:49:52 -0700335#endif
336
337 *ccp = NULL;
338 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
339 return SSH_ERR_ALLOC_FAIL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800340
341 if (cipher->number == SSH_CIPHER_DES) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800342 if (keylen > 8)
343 keylen = 8;
344 }
Greg Hartman9768ca42017-06-22 20:49:52 -0700345
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800346 cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
Adam Langleyd0592972015-03-30 14:49:51 -0700347 cc->encrypt = do_encrypt;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800348
Adam Langleyd0592972015-03-30 14:49:51 -0700349 if (keylen < cipher->key_len ||
Greg Hartman9768ca42017-06-22 20:49:52 -0700350 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
351 ret = SSH_ERR_INVALID_ARGUMENT;
352 goto out;
353 }
Adam Langleyd0592972015-03-30 14:49:51 -0700354
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800355 cc->cipher = cipher;
Adam Langleyd0592972015-03-30 14:49:51 -0700356 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
Greg Hartman9768ca42017-06-22 20:49:52 -0700357 ret = chachapoly_init(&cc->cp_ctx, key, keylen);
358 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800359 }
Adam Langleyd0592972015-03-30 14:49:51 -0700360#ifndef WITH_OPENSSL
361 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
362 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
363 aesctr_ivsetup(&cc->ac_ctx, iv);
Greg Hartman9768ca42017-06-22 20:49:52 -0700364 ret = 0;
365 goto out;
Adam Langleyd0592972015-03-30 14:49:51 -0700366 }
Greg Hartman9768ca42017-06-22 20:49:52 -0700367 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
368 ret = 0;
369 goto out;
370 }
371 ret = SSH_ERR_INVALID_ARGUMENT;
372 goto out;
373#else /* WITH_OPENSSL */
Adam Langleyd0592972015-03-30 14:49:51 -0700374 type = (*cipher->evptype)();
Greg Hartman9768ca42017-06-22 20:49:52 -0700375 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
376 ret = SSH_ERR_ALLOC_FAIL;
377 goto out;
378 }
379 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
Adam Langleyd0592972015-03-30 14:49:51 -0700380 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
381 ret = SSH_ERR_LIBCRYPTO_ERROR;
Greg Hartman9768ca42017-06-22 20:49:52 -0700382 goto out;
Adam Langleyd0592972015-03-30 14:49:51 -0700383 }
384 if (cipher_authlen(cipher) &&
Greg Hartman9768ca42017-06-22 20:49:52 -0700385 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
Adam Langleyd0592972015-03-30 14:49:51 -0700386 -1, (u_char *)iv)) {
387 ret = SSH_ERR_LIBCRYPTO_ERROR;
Greg Hartman9768ca42017-06-22 20:49:52 -0700388 goto out;
Adam Langleyd0592972015-03-30 14:49:51 -0700389 }
Greg Hartman9768ca42017-06-22 20:49:52 -0700390 klen = EVP_CIPHER_CTX_key_length(cc->evp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800391 if (klen > 0 && keylen != (u_int)klen) {
Greg Hartman9768ca42017-06-22 20:49:52 -0700392 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700393 ret = SSH_ERR_LIBCRYPTO_ERROR;
Greg Hartman9768ca42017-06-22 20:49:52 -0700394 goto out;
Adam Langleyd0592972015-03-30 14:49:51 -0700395 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800396 }
Greg Hartman9768ca42017-06-22 20:49:52 -0700397 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700398 ret = SSH_ERR_LIBCRYPTO_ERROR;
Greg Hartman9768ca42017-06-22 20:49:52 -0700399 goto out;
Adam Langleyd0592972015-03-30 14:49:51 -0700400 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800401
402 if (cipher->discard_len > 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700403 if ((junk = malloc(cipher->discard_len)) == NULL ||
404 (discard = malloc(cipher->discard_len)) == NULL) {
Greg Hartman9768ca42017-06-22 20:49:52 -0700405 free(junk);
Adam Langleyd0592972015-03-30 14:49:51 -0700406 ret = SSH_ERR_ALLOC_FAIL;
Greg Hartman9768ca42017-06-22 20:49:52 -0700407 goto out;
Adam Langleyd0592972015-03-30 14:49:51 -0700408 }
Greg Hartman9768ca42017-06-22 20:49:52 -0700409 ret = EVP_Cipher(cc->evp, discard, junk, cipher->discard_len);
Adam Langleyd0592972015-03-30 14:49:51 -0700410 explicit_bzero(discard, cipher->discard_len);
411 free(junk);
412 free(discard);
413 if (ret != 1) {
414 ret = SSH_ERR_LIBCRYPTO_ERROR;
Greg Hartman9768ca42017-06-22 20:49:52 -0700415 goto out;
Adam Langleyd0592972015-03-30 14:49:51 -0700416 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800417 }
Greg Hartman9768ca42017-06-22 20:49:52 -0700418 ret = 0;
419#endif /* WITH_OPENSSL */
420 out:
421 if (ret == 0) {
422 /* success */
423 *ccp = cc;
424 } else {
425 if (cc != NULL) {
426#ifdef WITH_OPENSSL
427 if (cc->evp != NULL)
428 EVP_CIPHER_CTX_free(cc->evp);
429#endif /* WITH_OPENSSL */
430 explicit_bzero(cc, sizeof(*cc));
431 free(cc);
432 }
433 }
434 return ret;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800435}
436
Adam Langleyd0592972015-03-30 14:49:51 -0700437/*
438 * cipher_crypt() operates as following:
439 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
440 * Theses bytes are treated as additional authenticated data for
441 * authenticated encryption modes.
442 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
443 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
444 * This tag is written on encryption and verified on decryption.
445 * Both 'aadlen' and 'authlen' can be set to 0.
446 */
447int
448cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
449 const u_char *src, u_int len, u_int aadlen, u_int authlen)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800450{
Adam Langleyd0592972015-03-30 14:49:51 -0700451 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
452 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
453 len, aadlen, authlen, cc->encrypt);
454 }
455#ifndef WITH_OPENSSL
456 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
457 if (aadlen)
458 memcpy(dest, src, aadlen);
459 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
460 dest + aadlen, len);
461 return 0;
462 }
463 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
464 memcpy(dest, src, aadlen + len);
465 return 0;
466 }
467 return SSH_ERR_INVALID_ARGUMENT;
468#else
469 if (authlen) {
470 u_char lastiv[1];
471
472 if (authlen != cipher_authlen(cc->cipher))
473 return SSH_ERR_INVALID_ARGUMENT;
474 /* increment IV */
Greg Hartman9768ca42017-06-22 20:49:52 -0700475 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Adam Langleyd0592972015-03-30 14:49:51 -0700476 1, lastiv))
477 return SSH_ERR_LIBCRYPTO_ERROR;
478 /* set tag on decyption */
479 if (!cc->encrypt &&
Greg Hartman9768ca42017-06-22 20:49:52 -0700480 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
Adam Langleyd0592972015-03-30 14:49:51 -0700481 authlen, (u_char *)src + aadlen + len))
482 return SSH_ERR_LIBCRYPTO_ERROR;
483 }
484 if (aadlen) {
485 if (authlen &&
Greg Hartman9768ca42017-06-22 20:49:52 -0700486 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
Adam Langleyd0592972015-03-30 14:49:51 -0700487 return SSH_ERR_LIBCRYPTO_ERROR;
488 memcpy(dest, src, aadlen);
489 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800490 if (len % cc->cipher->block_size)
Adam Langleyd0592972015-03-30 14:49:51 -0700491 return SSH_ERR_INVALID_ARGUMENT;
Greg Hartman9768ca42017-06-22 20:49:52 -0700492 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
Adam Langleyd0592972015-03-30 14:49:51 -0700493 len) < 0)
494 return SSH_ERR_LIBCRYPTO_ERROR;
495 if (authlen) {
496 /* compute tag (on encrypt) or verify tag (on decrypt) */
Greg Hartman9768ca42017-06-22 20:49:52 -0700497 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
Adam Langleyd0592972015-03-30 14:49:51 -0700498 return cc->encrypt ?
499 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
500 if (cc->encrypt &&
Greg Hartman9768ca42017-06-22 20:49:52 -0700501 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
Adam Langleyd0592972015-03-30 14:49:51 -0700502 authlen, dest + aadlen + len))
503 return SSH_ERR_LIBCRYPTO_ERROR;
504 }
505 return 0;
506#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800507}
508
Adam Langleyd0592972015-03-30 14:49:51 -0700509/* Extract the packet length, including any decryption necessary beforehand */
510int
511cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
512 const u_char *cp, u_int len)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800513{
Adam Langleyd0592972015-03-30 14:49:51 -0700514 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
515 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
516 cp, len);
517 if (len < 4)
518 return SSH_ERR_MESSAGE_INCOMPLETE;
519 *plenp = get_u32(cp);
520 return 0;
521}
522
Greg Hartman9768ca42017-06-22 20:49:52 -0700523void
524cipher_free(struct sshcipher_ctx *cc)
Adam Langleyd0592972015-03-30 14:49:51 -0700525{
Greg Hartman9768ca42017-06-22 20:49:52 -0700526 if (cc == NULL)
527 return;
Adam Langleyd0592972015-03-30 14:49:51 -0700528 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
529 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
530 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
531 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
532#ifdef WITH_OPENSSL
Greg Hartman9768ca42017-06-22 20:49:52 -0700533 if (cc->evp != NULL) {
534 EVP_CIPHER_CTX_free(cc->evp);
535 cc->evp = NULL;
536 }
Adam Langleyd0592972015-03-30 14:49:51 -0700537#endif
Greg Hartman9768ca42017-06-22 20:49:52 -0700538 explicit_bzero(cc, sizeof(*cc));
539 free(cc);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800540}
541
542/*
543 * Selects the cipher, and keys if by computing the MD5 checksum of the
544 * passphrase and using the resulting 16 bytes as the key.
545 */
Adam Langleyd0592972015-03-30 14:49:51 -0700546int
Greg Hartman9768ca42017-06-22 20:49:52 -0700547cipher_set_key_string(struct sshcipher_ctx **ccp,
548 const struct sshcipher *cipher, const char *passphrase, int do_encrypt)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800549{
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800550 u_char digest[16];
Adam Langleyd0592972015-03-30 14:49:51 -0700551 int r = SSH_ERR_INTERNAL_ERROR;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800552
Adam Langleyd0592972015-03-30 14:49:51 -0700553 if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
554 passphrase, strlen(passphrase),
555 digest, sizeof(digest))) != 0)
556 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800557
Greg Hartman9768ca42017-06-22 20:49:52 -0700558 r = cipher_init(ccp, cipher, digest, 16, NULL, 0, do_encrypt);
Adam Langleyd0592972015-03-30 14:49:51 -0700559 out:
560 explicit_bzero(digest, sizeof(digest));
561 return r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800562}
563
564/*
Adam Langleyd0592972015-03-30 14:49:51 -0700565 * Exports an IV from the sshcipher_ctx required to export the key
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800566 * state back from the unprivileged child to the privileged parent
567 * process.
568 */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800569int
Adam Langleyd0592972015-03-30 14:49:51 -0700570cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800571{
Adam Langleyd0592972015-03-30 14:49:51 -0700572 const struct sshcipher *c = cc->cipher;
573 int ivlen = 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800574
575 if (c->number == SSH_CIPHER_3DES)
576 ivlen = 24;
Adam Langleyd0592972015-03-30 14:49:51 -0700577 else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
578 ivlen = 0;
579 else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
580 ivlen = sizeof(cc->ac_ctx.ctr);
581#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800582 else
Greg Hartman9768ca42017-06-22 20:49:52 -0700583 ivlen = EVP_CIPHER_CTX_iv_length(cc->evp);
Adam Langleyd0592972015-03-30 14:49:51 -0700584#endif /* WITH_OPENSSL */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800585 return (ivlen);
586}
587
Adam Langleyd0592972015-03-30 14:49:51 -0700588int
589cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800590{
Adam Langleyd0592972015-03-30 14:49:51 -0700591 const struct sshcipher *c = cc->cipher;
592#ifdef WITH_OPENSSL
593 int evplen;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800594#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800595
Adam Langleyd0592972015-03-30 14:49:51 -0700596 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
597 if (len != 0)
598 return SSH_ERR_INVALID_ARGUMENT;
599 return 0;
600 }
601 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
602 if (len != sizeof(cc->ac_ctx.ctr))
603 return SSH_ERR_INVALID_ARGUMENT;
604 memcpy(iv, cc->ac_ctx.ctr, len);
605 return 0;
606 }
607 if ((cc->cipher->flags & CFLAG_NONE) != 0)
608 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800609
610 switch (c->number) {
Adam Langleyd0592972015-03-30 14:49:51 -0700611#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800612 case SSH_CIPHER_SSH2:
613 case SSH_CIPHER_DES:
614 case SSH_CIPHER_BLOWFISH:
Greg Hartman9768ca42017-06-22 20:49:52 -0700615 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800616 if (evplen == 0)
Adam Langleyd0592972015-03-30 14:49:51 -0700617 return 0;
618 else if (evplen < 0)
619 return SSH_ERR_LIBCRYPTO_ERROR;
620 if ((u_int)evplen != len)
621 return SSH_ERR_INVALID_ARGUMENT;
622#ifndef OPENSSL_HAVE_EVPCTR
623 if (c->evptype == evp_aes_128_ctr)
Greg Hartman9768ca42017-06-22 20:49:52 -0700624 ssh_aes_ctr_iv(cc->evp, 0, iv, len);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800625 else
626#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700627 if (cipher_authlen(c)) {
Greg Hartman9768ca42017-06-22 20:49:52 -0700628 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
Adam Langleyd0592972015-03-30 14:49:51 -0700629 len, iv))
630 return SSH_ERR_LIBCRYPTO_ERROR;
631 } else
Greg Hartman9768ca42017-06-22 20:49:52 -0700632 memcpy(iv, cc->evp->iv, len);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800633 break;
Adam Langleyd0592972015-03-30 14:49:51 -0700634#endif
635#ifdef WITH_SSH1
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800636 case SSH_CIPHER_3DES:
Greg Hartman9768ca42017-06-22 20:49:52 -0700637 return ssh1_3des_iv(cc->evp, 0, iv, 24);
Adam Langleyd0592972015-03-30 14:49:51 -0700638#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800639 default:
Adam Langleyd0592972015-03-30 14:49:51 -0700640 return SSH_ERR_INVALID_ARGUMENT;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800641 }
Adam Langleyd0592972015-03-30 14:49:51 -0700642 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800643}
644
Adam Langleyd0592972015-03-30 14:49:51 -0700645int
646cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
647{
648 const struct sshcipher *c = cc->cipher;
649#ifdef WITH_OPENSSL
650 int evplen = 0;
651#endif
652
653 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
654 return 0;
655 if ((cc->cipher->flags & CFLAG_NONE) != 0)
656 return 0;
657
658 switch (c->number) {
659#ifdef WITH_OPENSSL
660 case SSH_CIPHER_SSH2:
661 case SSH_CIPHER_DES:
662 case SSH_CIPHER_BLOWFISH:
Greg Hartman9768ca42017-06-22 20:49:52 -0700663 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
Adam Langleyd0592972015-03-30 14:49:51 -0700664 if (evplen <= 0)
665 return SSH_ERR_LIBCRYPTO_ERROR;
Greg Hartman9768ca42017-06-22 20:49:52 -0700666#ifndef OPENSSL_HAVE_EVPCTR
667 /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
668 if (c->evptype == evp_aes_128_ctr)
669 ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
670 else
671#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700672 if (cipher_authlen(c)) {
673 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
Greg Hartman9768ca42017-06-22 20:49:52 -0700674 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
Adam Langleyd0592972015-03-30 14:49:51 -0700675 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
676 return SSH_ERR_LIBCRYPTO_ERROR;
677 } else
Greg Hartman9768ca42017-06-22 20:49:52 -0700678 memcpy(cc->evp->iv, iv, evplen);
Adam Langleyd0592972015-03-30 14:49:51 -0700679 break;
680#endif
681#ifdef WITH_SSH1
682 case SSH_CIPHER_3DES:
Greg Hartman9768ca42017-06-22 20:49:52 -0700683 return ssh1_3des_iv(cc->evp, 1, (u_char *)iv, 24);
Adam Langleyd0592972015-03-30 14:49:51 -0700684#endif
685 default:
686 return SSH_ERR_INVALID_ARGUMENT;
687 }
688 return 0;
689}
690
691#ifdef WITH_OPENSSL
Greg Hartman9768ca42017-06-22 20:49:52 -0700692#define EVP_X_STATE(evp) (evp)->cipher_data
693#define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800694#endif
695
696int
Adam Langleyd0592972015-03-30 14:49:51 -0700697cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800698{
Greg Hartman9768ca42017-06-22 20:49:52 -0700699#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
Adam Langleyd0592972015-03-30 14:49:51 -0700700 const struct sshcipher *c = cc->cipher;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800701 int plen = 0;
702
Adam Langleyd0592972015-03-30 14:49:51 -0700703 if (c->evptype == EVP_rc4) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800704 plen = EVP_X_STATE_LEN(cc->evp);
705 if (dat == NULL)
706 return (plen);
707 memcpy(dat, EVP_X_STATE(cc->evp), plen);
708 }
709 return (plen);
Adam Langleyd0592972015-03-30 14:49:51 -0700710#else
711 return 0;
712#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800713}
714
715void
Adam Langleyd0592972015-03-30 14:49:51 -0700716cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800717{
Greg Hartman9768ca42017-06-22 20:49:52 -0700718#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
Adam Langleyd0592972015-03-30 14:49:51 -0700719 const struct sshcipher *c = cc->cipher;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800720 int plen;
721
Adam Langleyd0592972015-03-30 14:49:51 -0700722 if (c->evptype == EVP_rc4) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800723 plen = EVP_X_STATE_LEN(cc->evp);
724 memcpy(EVP_X_STATE(cc->evp), dat, plen);
725 }
Adam Langleyd0592972015-03-30 14:49:51 -0700726#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800727}