xref: /freebsd/crypto/openssh/cipher.c (revision 9a0c3479e22feda1bdb2db4b97f9deb1b5fa6269)
1 /* $OpenBSD: cipher.c,v 1.87 2013/01/26 06:11:05 djm Exp $ */
2 /* $FreeBSD$ */
3 /*
4  * Author: Tatu Ylonen <ylo@cs.hut.fi>
5  * Copyright (c) 1995 Tatu Ylonen <ylo@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 
41 #include <sys/types.h>
42 
43 #include <openssl/md5.h>
44 
45 #include <string.h>
46 #include <stdarg.h>
47 
48 #include "xmalloc.h"
49 #include "log.h"
50 #include "cipher.h"
51 
52 /* compatibility with old or broken OpenSSL versions */
53 #include "openbsd-compat/openssl-compat.h"
54 
55 extern const EVP_CIPHER *evp_ssh1_bf(void);
56 extern const EVP_CIPHER *evp_ssh1_3des(void);
57 extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
58 
59 struct Cipher {
60 	char	*name;
61 	int	number;		/* for ssh1 only */
62 	u_int	block_size;
63 	u_int	key_len;
64 	u_int	iv_len;		/* defaults to block_size */
65 	u_int	auth_len;
66 	u_int	discard_len;
67 	u_int	cbc_mode;
68 	const EVP_CIPHER	*(*evptype)(void);
69 } ciphers[] = {
70 	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
71 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
72 	{ "3des",	SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
73 	{ "blowfish",	SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
74 
75 	{ "3des-cbc",	SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
76 	{ "blowfish-cbc",
77 			SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
78 	{ "cast128-cbc",
79 			SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
80 	{ "arcfour",	SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
81 	{ "arcfour128",	SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
82 	{ "arcfour256",	SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
83 	{ "aes128-cbc",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
84 	{ "aes192-cbc",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
85 	{ "aes256-cbc",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
86 	{ "rijndael-cbc@lysator.liu.se",
87 			SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
88 	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
89 	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
90 	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
91 #ifdef OPENSSL_HAVE_EVPGCM
92 	{ "aes128-gcm@openssh.com",
93 			SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
94 	{ "aes256-gcm@openssh.com",
95 			SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
96 #endif
97 	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
98 };
99 
100 /*--*/
101 
102 u_int
103 cipher_blocksize(const Cipher *c)
104 {
105 	return (c->block_size);
106 }
107 
108 u_int
109 cipher_keylen(const Cipher *c)
110 {
111 	return (c->key_len);
112 }
113 
114 u_int
115 cipher_authlen(const Cipher *c)
116 {
117 	return (c->auth_len);
118 }
119 
120 u_int
121 cipher_ivlen(const Cipher *c)
122 {
123 	return (c->iv_len ? c->iv_len : c->block_size);
124 }
125 
126 u_int
127 cipher_get_number(const Cipher *c)
128 {
129 	return (c->number);
130 }
131 
132 u_int
133 cipher_is_cbc(const Cipher *c)
134 {
135 	return (c->cbc_mode);
136 }
137 
138 u_int
139 cipher_mask_ssh1(int client)
140 {
141 	u_int mask = 0;
142 	mask |= 1 << SSH_CIPHER_3DES;		/* Mandatory */
143 	mask |= 1 << SSH_CIPHER_BLOWFISH;
144 	if (client) {
145 		mask |= 1 << SSH_CIPHER_DES;
146 	}
147 	return mask;
148 }
149 
150 Cipher *
151 cipher_by_name(const char *name)
152 {
153 	Cipher *c;
154 	for (c = ciphers; c->name != NULL; c++)
155 		if (strcmp(c->name, name) == 0)
156 			return c;
157 	return NULL;
158 }
159 
160 Cipher *
161 cipher_by_number(int id)
162 {
163 	Cipher *c;
164 	for (c = ciphers; c->name != NULL; c++)
165 		if (c->number == id)
166 			return c;
167 	return NULL;
168 }
169 
170 #define	CIPHER_SEP	","
171 int
172 ciphers_valid(const char *names)
173 {
174 	Cipher *c;
175 	char *cipher_list, *cp;
176 	char *p;
177 
178 	if (names == NULL || strcmp(names, "") == 0)
179 		return 0;
180 	cipher_list = cp = xstrdup(names);
181 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
182 	    (p = strsep(&cp, CIPHER_SEP))) {
183 		c = cipher_by_name(p);
184 #ifdef NONE_CIPHER_ENABLED
185 		if (c == NULL || (c->number != SSH_CIPHER_SSH2 &&
186 		    c->number != SSH_CIPHER_NONE)) {
187 #else
188 		if (c == NULL || (c->number != SSH_CIPHER_SSH2)) {
189 #endif
190 			debug("bad cipher %s [%s]", p, names);
191 			xfree(cipher_list);
192 			return 0;
193 		} else {
194 			debug3("cipher ok: %s [%s]", p, names);
195 		}
196 	}
197 	debug3("ciphers ok: [%s]", names);
198 	xfree(cipher_list);
199 	return 1;
200 }
201 
202 /*
203  * Parses the name of the cipher.  Returns the number of the corresponding
204  * cipher, or -1 on error.
205  */
206 
207 int
208 cipher_number(const char *name)
209 {
210 	Cipher *c;
211 	if (name == NULL)
212 		return -1;
213 	for (c = ciphers; c->name != NULL; c++)
214 		if (strcasecmp(c->name, name) == 0)
215 			return c->number;
216 	return -1;
217 }
218 
219 char *
220 cipher_name(int id)
221 {
222 	Cipher *c = cipher_by_number(id);
223 	return (c==NULL) ? "<unknown>" : c->name;
224 }
225 
226 void
227 cipher_init(CipherContext *cc, Cipher *cipher,
228     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
229     int do_encrypt)
230 {
231 	static int dowarn = 1;
232 #ifdef SSH_OLD_EVP
233 	EVP_CIPHER *type;
234 #else
235 	const EVP_CIPHER *type;
236 	int klen;
237 #endif
238 	u_char *junk, *discard;
239 
240 	if (cipher->number == SSH_CIPHER_DES) {
241 		if (dowarn) {
242 			error("Warning: use of DES is strongly discouraged "
243 			    "due to cryptographic weaknesses");
244 			dowarn = 0;
245 		}
246 		if (keylen > 8)
247 			keylen = 8;
248 	}
249 	cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
250 	cc->encrypt = do_encrypt;
251 
252 	if (keylen < cipher->key_len)
253 		fatal("cipher_init: key length %d is insufficient for %s.",
254 		    keylen, cipher->name);
255 	if (iv != NULL && ivlen < cipher_ivlen(cipher))
256 		fatal("cipher_init: iv length %d is insufficient for %s.",
257 		    ivlen, cipher->name);
258 	cc->cipher = cipher;
259 
260 	type = (*cipher->evptype)();
261 
262 	EVP_CIPHER_CTX_init(&cc->evp);
263 #ifdef SSH_OLD_EVP
264 	if (type->key_len > 0 && type->key_len != keylen) {
265 		debug("cipher_init: set keylen (%d -> %d)",
266 		    type->key_len, keylen);
267 		type->key_len = keylen;
268 	}
269 	EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv,
270 	    (do_encrypt == CIPHER_ENCRYPT));
271 #else
272 	if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
273 	    (do_encrypt == CIPHER_ENCRYPT)) == 0)
274 		fatal("cipher_init: EVP_CipherInit failed for %s",
275 		    cipher->name);
276 	if (cipher_authlen(cipher) &&
277 	    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
278 	    -1, (u_char *)iv))
279 		fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED failed for %s",
280 		    cipher->name);
281 	klen = EVP_CIPHER_CTX_key_length(&cc->evp);
282 	if (klen > 0 && keylen != (u_int)klen) {
283 		debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
284 		if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
285 			fatal("cipher_init: set keylen failed (%d -> %d)",
286 			    klen, keylen);
287 	}
288 	if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
289 		fatal("cipher_init: EVP_CipherInit: set key failed for %s",
290 		    cipher->name);
291 #endif
292 
293 	if (cipher->discard_len > 0) {
294 		junk = xmalloc(cipher->discard_len);
295 		discard = xmalloc(cipher->discard_len);
296 		if (EVP_Cipher(&cc->evp, discard, junk,
297 		    cipher->discard_len) == 0)
298 			fatal("evp_crypt: EVP_Cipher failed during discard");
299 		memset(discard, 0, cipher->discard_len);
300 		xfree(junk);
301 		xfree(discard);
302 	}
303 }
304 
305 /*
306  * cipher_crypt() operates as following:
307  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
308  * Theses bytes are treated as additional authenticated data for
309  * authenticated encryption modes.
310  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
311  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
312  * This tag is written on encryption and verified on decryption.
313  * Both 'aadlen' and 'authlen' can be set to 0.
314  */
315 void
316 cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src,
317     u_int len, u_int aadlen, u_int authlen)
318 {
319 	if (authlen) {
320 		u_char lastiv[1];
321 
322 		if (authlen != cipher_authlen(cc->cipher))
323 			fatal("%s: authlen mismatch %d", __func__, authlen);
324 		/* increment IV */
325 		if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
326 		    1, lastiv))
327 			fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
328 		/* set tag on decyption */
329 		if (!cc->encrypt &&
330 		    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
331 		    authlen, (u_char *)src + aadlen + len))
332 			fatal("%s: EVP_CTRL_GCM_SET_TAG", __func__);
333 	}
334 	if (aadlen) {
335 		if (authlen &&
336 		    EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
337 			fatal("%s: EVP_Cipher(aad) failed", __func__);
338 		memcpy(dest, src, aadlen);
339 	}
340 	if (len % cc->cipher->block_size)
341 		fatal("%s: bad plaintext length %d", __func__, len);
342 	if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
343 	    len) < 0)
344 		fatal("%s: EVP_Cipher failed", __func__);
345 	if (authlen) {
346 		/* compute tag (on encrypt) or verify tag (on decrypt) */
347 		if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) {
348 			if (cc->encrypt)
349 				fatal("%s: EVP_Cipher(final) failed", __func__);
350 			else
351 				fatal("Decryption integrity check failed");
352 		}
353 		if (cc->encrypt &&
354 		    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
355 		    authlen, dest + aadlen + len))
356 			fatal("%s: EVP_CTRL_GCM_GET_TAG", __func__);
357 	}
358 }
359 
360 void
361 cipher_cleanup(CipherContext *cc)
362 {
363 	if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
364 		error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
365 }
366 
367 /*
368  * Selects the cipher, and keys if by computing the MD5 checksum of the
369  * passphrase and using the resulting 16 bytes as the key.
370  */
371 
372 void
373 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
374     const char *passphrase, int do_encrypt)
375 {
376 	MD5_CTX md;
377 	u_char digest[16];
378 
379 	MD5_Init(&md);
380 	MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
381 	MD5_Final(digest, &md);
382 
383 	cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
384 
385 	memset(digest, 0, sizeof(digest));
386 	memset(&md, 0, sizeof(md));
387 }
388 
389 /*
390  * Exports an IV from the CipherContext required to export the key
391  * state back from the unprivileged child to the privileged parent
392  * process.
393  */
394 
395 int
396 cipher_get_keyiv_len(const CipherContext *cc)
397 {
398 	Cipher *c = cc->cipher;
399 	int ivlen;
400 
401 	if (c->number == SSH_CIPHER_3DES)
402 		ivlen = 24;
403 	else
404 		ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
405 	return (ivlen);
406 }
407 
408 void
409 cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
410 {
411 	Cipher *c = cc->cipher;
412 	int evplen;
413 
414 	switch (c->number) {
415 #ifdef	NONE_CIPHER_ENABLED
416 	case SSH_CIPHER_NONE:
417 #endif
418 	case SSH_CIPHER_SSH2:
419 	case SSH_CIPHER_DES:
420 	case SSH_CIPHER_BLOWFISH:
421 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
422 		if (evplen <= 0)
423 			return;
424 		if ((u_int)evplen != len)
425 			fatal("%s: wrong iv length %d != %d", __func__,
426 			    evplen, len);
427 #ifdef USE_BUILTIN_RIJNDAEL
428 		if (c->evptype == evp_rijndael)
429 			ssh_rijndael_iv(&cc->evp, 0, iv, len);
430 		else
431 #endif
432 #ifndef OPENSSL_HAVE_EVPCTR
433 		if (c->evptype == evp_aes_128_ctr)
434 			ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
435 		else
436 #endif
437 		memcpy(iv, cc->evp.iv, len);
438 		break;
439 	case SSH_CIPHER_3DES:
440 		ssh1_3des_iv(&cc->evp, 0, iv, 24);
441 		break;
442 	default:
443 		fatal("%s: bad cipher %d", __func__, c->number);
444 	}
445 }
446 
447 void
448 cipher_set_keyiv(CipherContext *cc, u_char *iv)
449 {
450 	Cipher *c = cc->cipher;
451 	int evplen = 0;
452 
453 	switch (c->number) {
454 #ifdef	NONE_CIPHER_ENABLED
455 	case SSH_CIPHER_NONE:
456 #endif
457 	case SSH_CIPHER_SSH2:
458 	case SSH_CIPHER_DES:
459 	case SSH_CIPHER_BLOWFISH:
460 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
461 		if (evplen == 0)
462 			return;
463 #ifdef USE_BUILTIN_RIJNDAEL
464 		if (c->evptype == evp_rijndael)
465 			ssh_rijndael_iv(&cc->evp, 1, iv, evplen);
466 		else
467 #endif
468 #ifndef OPENSSL_HAVE_EVPCTR
469 		if (c->evptype == evp_aes_128_ctr)
470 			ssh_aes_ctr_iv(&cc->evp, 1, iv, evplen);
471 		else
472 #endif
473 		memcpy(cc->evp.iv, iv, evplen);
474 		break;
475 	case SSH_CIPHER_3DES:
476 		ssh1_3des_iv(&cc->evp, 1, iv, 24);
477 		break;
478 	default:
479 		fatal("%s: bad cipher %d", __func__, c->number);
480 	}
481 }
482 
483 int
484 cipher_get_keycontext(const CipherContext *cc, u_char *dat)
485 {
486 	Cipher *c = cc->cipher;
487 	int plen = 0;
488 
489 	if (c->evptype == EVP_rc4) {
490 		plen = EVP_X_STATE_LEN(cc->evp);
491 		if (dat == NULL)
492 			return (plen);
493 		memcpy(dat, EVP_X_STATE(cc->evp), plen);
494 	}
495 	return (plen);
496 }
497 
498 void
499 cipher_set_keycontext(CipherContext *cc, u_char *dat)
500 {
501 	Cipher *c = cc->cipher;
502 	int plen;
503 
504 	if (c->evptype == EVP_rc4) {
505 		plen = EVP_X_STATE_LEN(cc->evp);
506 		memcpy(EVP_X_STATE(cc->evp), dat, plen);
507 	}
508 }
509