xref: /openssh-portable/cipher.c (revision 03e93c75)
1 /* $OpenBSD: cipher.c,v 1.118 2020/12/21 11:09:32 dtucker Exp $ */
2 /*
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 
42 #include <string.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 
46 #include "cipher.h"
47 #include "misc.h"
48 #include "sshbuf.h"
49 #include "ssherr.h"
50 #include "digest.h"
51 
52 #include "openbsd-compat/openssl-compat.h"
53 
54 #ifndef WITH_OPENSSL
55 #define EVP_CIPHER_CTX void
56 #endif
57 
58 struct sshcipher_ctx {
59 	int	plaintext;
60 	int	encrypt;
61 	EVP_CIPHER_CTX *evp;
62 	struct chachapoly_ctx *cp_ctx;
63 	struct aesctr_ctx ac_ctx; /* XXX union with evp? */
64 	const struct sshcipher *cipher;
65 };
66 
67 struct sshcipher {
68 	char	*name;
69 	u_int	block_size;
70 	u_int	key_len;
71 	u_int	iv_len;		/* defaults to block_size */
72 	u_int	auth_len;
73 	u_int	flags;
74 #define CFLAG_CBC		(1<<0)
75 #define CFLAG_CHACHAPOLY	(1<<1)
76 #define CFLAG_AESCTR		(1<<2)
77 #define CFLAG_NONE		(1<<3)
78 #define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
79 #ifdef WITH_OPENSSL
80 	const EVP_CIPHER	*(*evptype)(void);
81 #else
82 	void	*ignored;
83 #endif
84 };
85 
86 static const struct sshcipher ciphers[] = {
87 #ifdef WITH_OPENSSL
88 #ifndef OPENSSL_NO_DES
89 	{ "3des-cbc",		8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
90 #endif
91 	{ "aes128-cbc",		16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
92 	{ "aes192-cbc",		16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
93 	{ "aes256-cbc",		16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
94 	{ "aes128-ctr",		16, 16, 0, 0, 0, EVP_aes_128_ctr },
95 	{ "aes192-ctr",		16, 24, 0, 0, 0, EVP_aes_192_ctr },
96 	{ "aes256-ctr",		16, 32, 0, 0, 0, EVP_aes_256_ctr },
97 # ifdef OPENSSL_HAVE_EVPGCM
98 	{ "aes128-gcm@openssh.com",
99 				16, 16, 12, 16, 0, EVP_aes_128_gcm },
100 	{ "aes256-gcm@openssh.com",
101 				16, 32, 12, 16, 0, EVP_aes_256_gcm },
102 # endif /* OPENSSL_HAVE_EVPGCM */
103 #else
104 	{ "aes128-ctr",		16, 16, 0, 0, CFLAG_AESCTR, NULL },
105 	{ "aes192-ctr",		16, 24, 0, 0, CFLAG_AESCTR, NULL },
106 	{ "aes256-ctr",		16, 32, 0, 0, CFLAG_AESCTR, NULL },
107 #endif
108 	{ "chacha20-poly1305@openssh.com",
109 				8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
110 	{ "none",		8, 0, 0, 0, CFLAG_NONE, NULL },
111 
112 	{ NULL,			0, 0, 0, 0, 0, NULL }
113 };
114 
115 /*--*/
116 
117 /* Returns a comma-separated list of supported ciphers. */
118 char *
cipher_alg_list(char sep,int auth_only)119 cipher_alg_list(char sep, int auth_only)
120 {
121 	char *tmp, *ret = NULL;
122 	size_t nlen, rlen = 0;
123 	const struct sshcipher *c;
124 
125 	for (c = ciphers; c->name != NULL; c++) {
126 		if ((c->flags & CFLAG_INTERNAL) != 0)
127 			continue;
128 		if (auth_only && c->auth_len == 0)
129 			continue;
130 		if (ret != NULL)
131 			ret[rlen++] = sep;
132 		nlen = strlen(c->name);
133 		if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
134 			free(ret);
135 			return NULL;
136 		}
137 		ret = tmp;
138 		memcpy(ret + rlen, c->name, nlen + 1);
139 		rlen += nlen;
140 	}
141 	return ret;
142 }
143 
144 const char *
compression_alg_list(int compression)145 compression_alg_list(int compression)
146 {
147 #ifdef WITH_ZLIB
148 	return compression ? "zlib@openssh.com,zlib,none" :
149 	    "none,zlib@openssh.com,zlib";
150 #else
151 	return "none";
152 #endif
153 }
154 
155 u_int
cipher_blocksize(const struct sshcipher * c)156 cipher_blocksize(const struct sshcipher *c)
157 {
158 	return (c->block_size);
159 }
160 
161 u_int
cipher_keylen(const struct sshcipher * c)162 cipher_keylen(const struct sshcipher *c)
163 {
164 	return (c->key_len);
165 }
166 
167 u_int
cipher_seclen(const struct sshcipher * c)168 cipher_seclen(const struct sshcipher *c)
169 {
170 	if (strcmp("3des-cbc", c->name) == 0)
171 		return 14;
172 	return cipher_keylen(c);
173 }
174 
175 u_int
cipher_authlen(const struct sshcipher * c)176 cipher_authlen(const struct sshcipher *c)
177 {
178 	return (c->auth_len);
179 }
180 
181 u_int
cipher_ivlen(const struct sshcipher * c)182 cipher_ivlen(const struct sshcipher *c)
183 {
184 	/*
185 	 * Default is cipher block size, except for chacha20+poly1305 that
186 	 * needs no IV. XXX make iv_len == -1 default?
187 	 */
188 	return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
189 	    c->iv_len : c->block_size;
190 }
191 
192 u_int
cipher_is_cbc(const struct sshcipher * c)193 cipher_is_cbc(const struct sshcipher *c)
194 {
195 	return (c->flags & CFLAG_CBC) != 0;
196 }
197 
198 u_int
cipher_ctx_is_plaintext(struct sshcipher_ctx * cc)199 cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
200 {
201 	return cc->plaintext;
202 }
203 
204 const struct sshcipher *
cipher_by_name(const char * name)205 cipher_by_name(const char *name)
206 {
207 	const struct sshcipher *c;
208 	for (c = ciphers; c->name != NULL; c++)
209 		if (strcmp(c->name, name) == 0)
210 			return c;
211 	return NULL;
212 }
213 
214 #define	CIPHER_SEP	","
215 int
ciphers_valid(const char * names)216 ciphers_valid(const char *names)
217 {
218 	const struct sshcipher *c;
219 	char *cipher_list, *cp;
220 	char *p;
221 
222 	if (names == NULL || strcmp(names, "") == 0)
223 		return 0;
224 	if ((cipher_list = cp = strdup(names)) == NULL)
225 		return 0;
226 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
227 	    (p = strsep(&cp, CIPHER_SEP))) {
228 		c = cipher_by_name(p);
229 		if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
230 			free(cipher_list);
231 			return 0;
232 		}
233 	}
234 	free(cipher_list);
235 	return 1;
236 }
237 
238 const char *
cipher_warning_message(const struct sshcipher_ctx * cc)239 cipher_warning_message(const struct sshcipher_ctx *cc)
240 {
241 	if (cc == NULL || cc->cipher == NULL)
242 		return NULL;
243 	/* XXX repurpose for CBC warning */
244 	return NULL;
245 }
246 
247 int
cipher_init(struct sshcipher_ctx ** ccp,const struct sshcipher * cipher,const u_char * key,u_int keylen,const u_char * iv,u_int ivlen,int do_encrypt)248 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
249     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
250     int do_encrypt)
251 {
252 	struct sshcipher_ctx *cc = NULL;
253 	int ret = SSH_ERR_INTERNAL_ERROR;
254 #ifdef WITH_OPENSSL
255 	const EVP_CIPHER *type;
256 	int klen;
257 #endif
258 
259 	*ccp = NULL;
260 	if ((cc = calloc(sizeof(*cc), 1)) == NULL)
261 		return SSH_ERR_ALLOC_FAIL;
262 
263 	cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
264 	cc->encrypt = do_encrypt;
265 
266 	if (keylen < cipher->key_len ||
267 	    (iv != NULL && ivlen < cipher_ivlen(cipher))) {
268 		ret = SSH_ERR_INVALID_ARGUMENT;
269 		goto out;
270 	}
271 
272 	cc->cipher = cipher;
273 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
274 		cc->cp_ctx = chachapoly_new(key, keylen);
275 		ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT;
276 		goto out;
277 	}
278 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
279 		ret = 0;
280 		goto out;
281 	}
282 #ifndef WITH_OPENSSL
283 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
284 		aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
285 		aesctr_ivsetup(&cc->ac_ctx, iv);
286 		ret = 0;
287 		goto out;
288 	}
289 	ret = SSH_ERR_INVALID_ARGUMENT;
290 	goto out;
291 #else /* WITH_OPENSSL */
292 	type = (*cipher->evptype)();
293 	if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
294 		ret = SSH_ERR_ALLOC_FAIL;
295 		goto out;
296 	}
297 	if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
298 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
299 		ret = SSH_ERR_LIBCRYPTO_ERROR;
300 		goto out;
301 	}
302 	if (cipher_authlen(cipher) &&
303 	    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
304 	    -1, (u_char *)iv)) {
305 		ret = SSH_ERR_LIBCRYPTO_ERROR;
306 		goto out;
307 	}
308 	klen = EVP_CIPHER_CTX_key_length(cc->evp);
309 	if (klen > 0 && keylen != (u_int)klen) {
310 		if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
311 			ret = SSH_ERR_LIBCRYPTO_ERROR;
312 			goto out;
313 		}
314 	}
315 	if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
316 		ret = SSH_ERR_LIBCRYPTO_ERROR;
317 		goto out;
318 	}
319 	ret = 0;
320 #endif /* WITH_OPENSSL */
321  out:
322 	if (ret == 0) {
323 		/* success */
324 		*ccp = cc;
325 	} else {
326 		if (cc != NULL) {
327 #ifdef WITH_OPENSSL
328 			EVP_CIPHER_CTX_free(cc->evp);
329 #endif /* WITH_OPENSSL */
330 			freezero(cc, sizeof(*cc));
331 		}
332 	}
333 	return ret;
334 }
335 
336 /*
337  * cipher_crypt() operates as following:
338  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
339  * These bytes are treated as additional authenticated data for
340  * authenticated encryption modes.
341  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
342  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
343  * This tag is written on encryption and verified on decryption.
344  * Both 'aadlen' and 'authlen' can be set to 0.
345  */
346 int
cipher_crypt(struct sshcipher_ctx * cc,u_int seqnr,u_char * dest,const u_char * src,u_int len,u_int aadlen,u_int authlen)347 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
348    const u_char *src, u_int len, u_int aadlen, u_int authlen)
349 {
350 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
351 		return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src,
352 		    len, aadlen, authlen, cc->encrypt);
353 	}
354 	if ((cc->cipher->flags & CFLAG_NONE) != 0) {
355 		memcpy(dest, src, aadlen + len);
356 		return 0;
357 	}
358 #ifndef WITH_OPENSSL
359 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
360 		if (aadlen)
361 			memcpy(dest, src, aadlen);
362 		aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
363 		    dest + aadlen, len);
364 		return 0;
365 	}
366 	return SSH_ERR_INVALID_ARGUMENT;
367 #else
368 	if (authlen) {
369 		u_char lastiv[1];
370 
371 		if (authlen != cipher_authlen(cc->cipher))
372 			return SSH_ERR_INVALID_ARGUMENT;
373 		/* increment IV */
374 		if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
375 		    1, lastiv))
376 			return SSH_ERR_LIBCRYPTO_ERROR;
377 		/* set tag on decyption */
378 		if (!cc->encrypt &&
379 		    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
380 		    authlen, (u_char *)src + aadlen + len))
381 			return SSH_ERR_LIBCRYPTO_ERROR;
382 	}
383 	if (aadlen) {
384 		if (authlen &&
385 		    EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
386 			return SSH_ERR_LIBCRYPTO_ERROR;
387 		memcpy(dest, src, aadlen);
388 	}
389 	if (len % cc->cipher->block_size)
390 		return SSH_ERR_INVALID_ARGUMENT;
391 	if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
392 	    len) < 0)
393 		return SSH_ERR_LIBCRYPTO_ERROR;
394 	if (authlen) {
395 		/* compute tag (on encrypt) or verify tag (on decrypt) */
396 		if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
397 			return cc->encrypt ?
398 			    SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
399 		if (cc->encrypt &&
400 		    !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
401 		    authlen, dest + aadlen + len))
402 			return SSH_ERR_LIBCRYPTO_ERROR;
403 	}
404 	return 0;
405 #endif
406 }
407 
408 /* Extract the packet length, including any decryption necessary beforehand */
409 int
cipher_get_length(struct sshcipher_ctx * cc,u_int * plenp,u_int seqnr,const u_char * cp,u_int len)410 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
411     const u_char *cp, u_int len)
412 {
413 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
414 		return chachapoly_get_length(cc->cp_ctx, plenp, seqnr,
415 		    cp, len);
416 	if (len < 4)
417 		return SSH_ERR_MESSAGE_INCOMPLETE;
418 	*plenp = PEEK_U32(cp);
419 	return 0;
420 }
421 
422 void
cipher_free(struct sshcipher_ctx * cc)423 cipher_free(struct sshcipher_ctx *cc)
424 {
425 	if (cc == NULL)
426 		return;
427 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
428 		chachapoly_free(cc->cp_ctx);
429 		cc->cp_ctx = NULL;
430 	} else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
431 		explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
432 #ifdef WITH_OPENSSL
433 	EVP_CIPHER_CTX_free(cc->evp);
434 	cc->evp = NULL;
435 #endif
436 	freezero(cc, sizeof(*cc));
437 }
438 
439 /*
440  * Exports an IV from the sshcipher_ctx required to export the key
441  * state back from the unprivileged child to the privileged parent
442  * process.
443  */
444 int
cipher_get_keyiv_len(const struct sshcipher_ctx * cc)445 cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
446 {
447 	const struct sshcipher *c = cc->cipher;
448 
449 	if ((c->flags & CFLAG_CHACHAPOLY) != 0)
450 		return 0;
451 	else if ((c->flags & CFLAG_AESCTR) != 0)
452 		return sizeof(cc->ac_ctx.ctr);
453 #ifdef WITH_OPENSSL
454 	return EVP_CIPHER_CTX_iv_length(cc->evp);
455 #else
456 	return 0;
457 #endif
458 }
459 
460 int
cipher_get_keyiv(struct sshcipher_ctx * cc,u_char * iv,size_t len)461 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
462 {
463 #ifdef WITH_OPENSSL
464 	const struct sshcipher *c = cc->cipher;
465 	int evplen;
466 #endif
467 
468 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
469 		if (len != 0)
470 			return SSH_ERR_INVALID_ARGUMENT;
471 		return 0;
472 	}
473 	if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
474 		if (len != sizeof(cc->ac_ctx.ctr))
475 			return SSH_ERR_INVALID_ARGUMENT;
476 		memcpy(iv, cc->ac_ctx.ctr, len);
477 		return 0;
478 	}
479 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
480 		return 0;
481 
482 #ifdef WITH_OPENSSL
483 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
484 	if (evplen == 0)
485 		return 0;
486 	else if (evplen < 0)
487 		return SSH_ERR_LIBCRYPTO_ERROR;
488 	if ((size_t)evplen != len)
489 		return SSH_ERR_INVALID_ARGUMENT;
490 #ifndef OPENSSL_HAVE_EVPCTR
491 	if (c->evptype == evp_aes_128_ctr)
492 		ssh_aes_ctr_iv(cc->evp, 0, iv, len);
493 	else
494 #endif
495 	if (cipher_authlen(c)) {
496 		if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
497 		   len, iv))
498 		       return SSH_ERR_LIBCRYPTO_ERROR;
499 	} else if (!EVP_CIPHER_CTX_get_iv(cc->evp, iv, len))
500 	       return SSH_ERR_LIBCRYPTO_ERROR;
501 #endif
502 	return 0;
503 }
504 
505 int
cipher_set_keyiv(struct sshcipher_ctx * cc,const u_char * iv,size_t len)506 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
507 {
508 #ifdef WITH_OPENSSL
509 	const struct sshcipher *c = cc->cipher;
510 	int evplen = 0;
511 #endif
512 
513 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
514 		return 0;
515 	if ((cc->cipher->flags & CFLAG_NONE) != 0)
516 		return 0;
517 
518 #ifdef WITH_OPENSSL
519 	evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
520 	if (evplen <= 0)
521 		return SSH_ERR_LIBCRYPTO_ERROR;
522 	if ((size_t)evplen != len)
523 		return SSH_ERR_INVALID_ARGUMENT;
524 #ifndef OPENSSL_HAVE_EVPCTR
525 	/* XXX iv arg is const, but ssh_aes_ctr_iv isn't */
526 	if (c->evptype == evp_aes_128_ctr)
527 		ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen);
528 	else
529 #endif
530 	if (cipher_authlen(c)) {
531 		/* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
532 		if (!EVP_CIPHER_CTX_ctrl(cc->evp,
533 		    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
534 			return SSH_ERR_LIBCRYPTO_ERROR;
535 	} else if (!EVP_CIPHER_CTX_set_iv(cc->evp, iv, evplen))
536 		return SSH_ERR_LIBCRYPTO_ERROR;
537 #endif
538 	return 0;
539 }
540