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