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