cipher.c revision 1.18 1 /* $NetBSD: cipher.c,v 1.18 2020/05/28 17:05:49 christos Exp $ */
2 /* $OpenBSD: cipher.c,v 1.117 2020/04/03 04:27:03 djm Exp $ */
3 /*
4 * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
6 * All rights reserved
7 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Niels Provos. All rights reserved.
16 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include "includes.h"
40 __RCSID("$NetBSD: cipher.c,v 1.18 2020/05/28 17:05:49 christos Exp $");
41 #include <sys/types.h>
42
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46
47 #include "cipher.h"
48 #include "misc.h"
49 #include "sshbuf.h"
50 #include "ssherr.h"
51 #include "digest.h"
52
53 #ifndef WITH_OPENSSL
54 #define EVP_CIPHER_CTX void
55 #endif
56
57 struct sshcipher_ctx {
58 int plaintext;
59 int encrypt;
60 EVP_CIPHER_CTX *evp;
61 struct chachapoly_ctx *cp_ctx;
62 struct aesctr_ctx ac_ctx; /* XXX union with evp? */
63 const struct sshcipher *cipher;
64 };
65
66 struct sshcipher {
67 const char *name;
68 u_int block_size;
69 u_int key_len;
70 u_int iv_len; /* defaults to block_size */
71 u_int auth_len;
72 u_int flags;
73 #define CFLAG_CBC (1<<0)
74 #define CFLAG_CHACHAPOLY (1<<1)
75 #define CFLAG_AESCTR (1<<2)
76 #define CFLAG_NONE (1<<3)
77 #define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */
78 #ifdef WITH_OPENSSL
79 const EVP_CIPHER *(*evptype)(void);
80 #else
81 void *ignored;
82 #endif
83 };
84
85 static const struct sshcipher ciphers[] = {
86 #ifdef WITH_OPENSSL
87 { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
88 { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
89 { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
90 { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
91 { "rijndael-cbc (at) lysator.liu.se",
92 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
93 { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr },
94 { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr },
95 { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr },
96 { "aes128-gcm (at) openssh.com",
97 16, 16, 12, 16, 0, EVP_aes_128_gcm },
98 { "aes256-gcm (at) openssh.com",
99 16, 32, 12, 16, 0, EVP_aes_256_gcm },
100 #else
101 { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR, NULL },
102 { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR, NULL },
103 { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR, NULL },
104 #endif
105 { "chacha20-poly1305 (at) openssh.com",
106 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
107 { "none", 8, 0, 0, 0, CFLAG_NONE, NULL },
108
109 { NULL, 0, 0, 0, 0, 0, NULL }
110 };
111
112 /*--*/
113
114 /* Returns a comma-separated list of supported ciphers. */
115 char *
116 cipher_alg_list(char sep, int auth_only)
117 {
118 char *tmp, *ret = NULL;
119 size_t nlen, rlen = 0;
120 const struct sshcipher *c;
121
122 for (c = ciphers; c->name != NULL; c++) {
123 if ((c->flags & CFLAG_INTERNAL) != 0)
124 continue;
125 if (auth_only && c->auth_len == 0)
126 continue;
127 if (ret != NULL)
128 ret[rlen++] = sep;
129 nlen = strlen(c->name);
130 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
131 free(ret);
132 return NULL;
133 }
134 ret = tmp;
135 memcpy(ret + rlen, c->name, nlen + 1);
136 rlen += nlen;
137 }
138 return ret;
139 }
140
141 const char *
142 compression_alg_list(int compression)
143 {
144 #ifdef WITH_ZLIB
145 return compression ? "zlib (at) openssh.com,zlib,none" :
146 "none,zlib (at) openssh.com,zlib";
147 #else
148 return "none";
149 #endif
150 }
151
152 u_int
153 cipher_blocksize(const struct sshcipher *c)
154 {
155 return (c->block_size);
156 }
157
158 u_int
159 cipher_keylen(const struct sshcipher *c)
160 {
161 return (c->key_len);
162 }
163
164 u_int
165 cipher_seclen(const struct sshcipher *c)
166 {
167 if (strcmp("3des-cbc", c->name) == 0)
168 return 14;
169 return cipher_keylen(c);
170 }
171
172 u_int
173 cipher_authlen(const struct sshcipher *c)
174 {
175 return (c->auth_len);
176 }
177
178 u_int
179 cipher_ivlen(const struct sshcipher *c)
180 {
181 /*
182 * Default is cipher block size, except for chacha20+poly1305 that
183 * needs no IV. XXX make iv_len == -1 default?
184 */
185 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
186 c->iv_len : c->block_size;
187 }
188
189 u_int
190 cipher_is_cbc(const struct sshcipher *c)
191 {
192 return (c->flags & CFLAG_CBC) != 0;
193 }
194
195 u_int
196 cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
197 {
198 return cc->plaintext;
199 }
200
201 const struct sshcipher *
202 cipher_by_name(const char *name)
203 {
204 const struct sshcipher *c;
205 for (c = ciphers; c->name != NULL; c++)
206 if (strcmp(c->name, name) == 0)
207 return c;
208 return NULL;
209 }
210
211 #define CIPHER_SEP ","
212 int
213 ciphers_valid(const char *names)
214 {
215 const struct sshcipher *c;
216 char *cipher_list, *cp;
217 char *p;
218
219 if (names == NULL || strcmp(names, "") == 0)
220 return 0;
221 if ((cipher_list = cp = strdup(names)) == NULL)
222 return 0;
223 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
224 (p = strsep(&cp, CIPHER_SEP))) {
225 c = cipher_by_name(p);
226 if (c == NULL || (c->flags & (CFLAG_INTERNAL|CFLAG_NONE)) != 0) {
227 free(cipher_list);
228 return 0;
229 }
230 }
231 free(cipher_list);
232 return 1;
233 }
234
235 const char *
236 cipher_warning_message(const struct sshcipher_ctx *cc)
237 {
238 if (cc == NULL || cc->cipher == NULL)
239 return NULL;
240 /* XXX repurpose for CBC warning */
241 return NULL;
242 }
243
244 int
245 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
246 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
247 int do_encrypt)
248 {
249 struct sshcipher_ctx *cc = NULL;
250 int ret = SSH_ERR_INTERNAL_ERROR;
251 #ifdef WITH_OPENSSL
252 const EVP_CIPHER *type;
253 int klen;
254 #endif
255
256 *ccp = NULL;
257 if ((cc = calloc(sizeof(*cc), 1)) == NULL)
258 return SSH_ERR_ALLOC_FAIL;
259
260 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
261 cc->encrypt = do_encrypt;
262
263 if (keylen < cipher->key_len ||
264 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
265 ret = SSH_ERR_INVALID_ARGUMENT;
266 goto out;
267 }
268
269 cc->cipher = cipher;
270 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
271 cc->cp_ctx = chachapoly_new(key, keylen);
272 ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT;
273 goto out;
274 }
275 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
276 ret = 0;
277 goto out;
278 }
279 #ifndef WITH_OPENSSL
280 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
281 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
282 aesctr_ivsetup(&cc->ac_ctx, iv);
283 ret = 0;
284 goto out;
285 }
286 ret = SSH_ERR_INVALID_ARGUMENT;
287 goto out;
288 #else /* WITH_OPENSSL */
289 type = (*cipher->evptype)();
290 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
291 ret = SSH_ERR_ALLOC_FAIL;
292 goto out;
293 }
294 if (EVP_CipherInit(cc->evp, type, NULL, (const u_char *)iv,
295 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
296 ret = SSH_ERR_LIBCRYPTO_ERROR;
297 goto out;
298 }
299 if (cipher_authlen(cipher) &&
300 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
301 -1, __UNCONST(iv))) {
302 ret = SSH_ERR_LIBCRYPTO_ERROR;
303 goto out;
304 }
305 klen = EVP_CIPHER_CTX_key_length(cc->evp);
306 if (klen > 0 && keylen != (u_int)klen) {
307 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
308 ret = SSH_ERR_LIBCRYPTO_ERROR;
309 goto out;
310 }
311 }
312 /* in OpenSSL 1.1.0, EVP_CipherInit clears all previous setups;
313 use EVP_CipherInit_ex for augmenting */
314 if (EVP_CipherInit_ex(cc->evp, NULL, NULL, __UNCONST(key), NULL, -1) == 0) {
315 ret = SSH_ERR_LIBCRYPTO_ERROR;
316 goto out;
317 }
318 ret = 0;
319 #endif /* WITH_OPENSSL */
320 out:
321 if (ret == 0) {
322 /* success */
323 *ccp = cc;
324 } else {
325 if (cc != NULL) {
326 #ifdef WITH_OPENSSL
327 EVP_CIPHER_CTX_free(cc->evp);
328 #endif /* WITH_OPENSSL */
329 freezero(cc, sizeof(*cc));
330 }
331 }
332 return ret;
333 }
334
335 /*
336 * cipher_crypt() operates as following:
337 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
338 * These bytes are treated as additional authenticated data for
339 * authenticated encryption modes.
340 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
341 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
342 * This tag is written on encryption and verified on decryption.
343 * Both 'aadlen' and 'authlen' can be set to 0.
344 */
345 int
346 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
347 const u_char *src, u_int len, u_int aadlen, u_int authlen)
348 {
349 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
350 return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src,
351 len, aadlen, authlen, cc->encrypt);
352 }
353 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
354 memcpy(dest, src, aadlen + len);
355 return 0;
356 }
357 #ifndef WITH_OPENSSL
358 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
359 if (aadlen)
360 memcpy(dest, src, aadlen);
361 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
362 dest + aadlen, len);
363 return 0;
364 }
365 return SSH_ERR_INVALID_ARGUMENT;
366 #else
367 if (authlen) {
368 u_char lastiv[1];
369
370 if (authlen != cipher_authlen(cc->cipher))
371 return SSH_ERR_INVALID_ARGUMENT;
372 /* increment IV */
373 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
374 1, lastiv))
375 return SSH_ERR_LIBCRYPTO_ERROR;
376 /* set tag on decyption */
377 if (!cc->encrypt &&
378 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
379 authlen, __UNCONST(src + aadlen + len)))
380 return SSH_ERR_LIBCRYPTO_ERROR;
381 }
382 if (aadlen) {
383 if (authlen &&
384 EVP_Cipher(cc->evp, NULL, (const u_char *)src, aadlen) < 0)
385 return SSH_ERR_LIBCRYPTO_ERROR;
386 memcpy(dest, src, aadlen);
387 }
388 if (len % cc->cipher->block_size)
389 return SSH_ERR_INVALID_ARGUMENT;
390 if (EVP_Cipher(cc->evp, dest + aadlen, (const u_char *)src + aadlen,
391 len) < 0)
392 return SSH_ERR_LIBCRYPTO_ERROR;
393 if (authlen) {
394 /* compute tag (on encrypt) or verify tag (on decrypt) */
395 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
396 return cc->encrypt ?
397 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
398 if (cc->encrypt &&
399 !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
400 authlen, dest + aadlen + len))
401 return SSH_ERR_LIBCRYPTO_ERROR;
402 }
403 return 0;
404 #endif
405 }
406
407 /* Extract the packet length, including any decryption necessary beforehand */
408 int
409 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
410 const u_char *cp, u_int len)
411 {
412 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
413 return chachapoly_get_length(cc->cp_ctx, plenp, seqnr,
414 cp, len);
415 if (len < 4)
416 return SSH_ERR_MESSAGE_INCOMPLETE;
417 *plenp = PEEK_U32(cp);
418 return 0;
419 }
420
421 void
422 cipher_free(struct sshcipher_ctx *cc)
423 {
424 if (cc == NULL)
425 return;
426 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
427 chachapoly_free(cc->cp_ctx);
428 cc->cp_ctx = NULL;
429 } else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
430 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
431 #ifdef WITH_OPENSSL
432 EVP_CIPHER_CTX_free(cc->evp);
433 cc->evp = NULL;
434 #endif
435 freezero(cc, sizeof(*cc));
436 }
437
438 /*
439 * Exports an IV from the sshcipher_ctx required to export the key
440 * state back from the unprivileged child to the privileged parent
441 * process.
442 */
443 int
444 cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
445 {
446 const struct sshcipher *c = cc->cipher;
447
448 if ((c->flags & CFLAG_CHACHAPOLY) != 0)
449 return 0;
450 else if ((c->flags & CFLAG_AESCTR) != 0)
451 return sizeof(cc->ac_ctx.ctr);
452 #ifdef WITH_OPENSSL
453 return EVP_CIPHER_CTX_iv_length(cc->evp);
454 #else
455 return 0;
456 #endif
457 }
458
459 int
460 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
461 {
462 #ifdef WITH_OPENSSL
463 const struct sshcipher *c = cc->cipher;
464 int evplen;
465 #endif
466
467 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
468 if (len != 0)
469 return SSH_ERR_INVALID_ARGUMENT;
470 return 0;
471 }
472 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
473 if (len != sizeof(cc->ac_ctx.ctr))
474 return SSH_ERR_INVALID_ARGUMENT;
475 memcpy(iv, cc->ac_ctx.ctr, len);
476 return 0;
477 }
478 if ((cc->cipher->flags & CFLAG_NONE) != 0)
479 return 0;
480
481 #ifdef WITH_OPENSSL
482 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
483 if (evplen == 0)
484 return 0;
485 else if (evplen < 0)
486 return SSH_ERR_LIBCRYPTO_ERROR;
487 if ((size_t)evplen != len)
488 return SSH_ERR_INVALID_ARGUMENT;
489 if (cipher_authlen(c)) {
490 if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
491 len, iv))
492 return SSH_ERR_LIBCRYPTO_ERROR;
493 } else
494 memcpy(iv, EVP_CIPHER_CTX_iv(cc->evp), len);
495 #endif
496 return 0;
497 }
498
499 int
500 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
501 {
502 #ifdef WITH_OPENSSL
503 const struct sshcipher *c = cc->cipher;
504 int evplen = 0;
505 #endif
506
507 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
508 return 0;
509 if ((cc->cipher->flags & CFLAG_NONE) != 0)
510 return 0;
511
512 #ifdef WITH_OPENSSL
513 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
514 if (evplen <= 0)
515 return SSH_ERR_LIBCRYPTO_ERROR;
516 if ((size_t)evplen != len)
517 return SSH_ERR_INVALID_ARGUMENT;
518 if (cipher_authlen(c)) {
519 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
520 if (!EVP_CIPHER_CTX_ctrl(cc->evp,
521 EVP_CTRL_GCM_SET_IV_FIXED, -1, __UNCONST(iv)))
522 return SSH_ERR_LIBCRYPTO_ERROR;
523 } else
524 memcpy(EVP_CIPHER_CTX_iv_noconst(cc->evp), iv, evplen);
525 #endif
526 return 0;
527 }
528
529