1 /* $OpenBSD: cipher.c,v 1.82 2009/01/26 09:58:15 markus 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 <openssl/md5.h>
41 
42 #include <string.h>
43 #include <stdarg.h>
44 
45 #include "xmalloc.h"
46 #include "log.h"
47 #include "cipher.h"
48 
49 __RCSID("$MirOS: src/usr.bin/ssh/cipher.c,v 1.13 2010/09/21 21:24:36 tg Exp $");
50 
51 extern const EVP_CIPHER *evp_ssh1_bf(void);
52 extern const EVP_CIPHER *evp_ssh1_3des(void);
53 extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
54 extern const EVP_CIPHER *evp_aes_128_ctr(void);
55 extern void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
56 
57 struct Cipher {
58 	const char *name;
59 	int	number;		/* for ssh1 only */
60 	u_int	block_size;
61 	u_int	key_len;
62 	u_int	discard_len;
63 	u_int	cbc_mode;
64 	const EVP_CIPHER	*(*evptype)(void);
65 } ciphers[] = {
66 	{ "none",		SSH_CIPHER_NONE, 8, 0, 0, 0, EVP_enc_null },
67 	{ "des",		SSH_CIPHER_DES, 8, 8, 0, 1, EVP_des_cbc },
68 	{ "3des",		SSH_CIPHER_3DES, 8, 16, 0, 1, evp_ssh1_3des },
69 	{ "blowfish",		SSH_CIPHER_BLOWFISH, 8, 32, 0, 1, evp_ssh1_bf },
70 
71 	{ "3des-cbc",		SSH_CIPHER_SSH2, 8, 24, 0, 1, EVP_des_ede3_cbc },
72 	{ "blowfish-cbc",	SSH_CIPHER_SSH2, 8, 16, 0, 1, EVP_bf_cbc },
73 	{ "cast128-cbc",	SSH_CIPHER_SSH2, 8, 16, 0, 1, EVP_cast5_cbc },
74 	{ "arcfour",		SSH_CIPHER_SSH2, 8, 16, 0, 0, EVP_rc4 },
75 	{ "arcfour128",		SSH_CIPHER_SSH2, 8, 16, 1536, 0, EVP_rc4 },
76 	{ "arcfour256",		SSH_CIPHER_SSH2, 8, 32, 1536, 0, EVP_rc4 },
77 	{ "aes128-cbc",		SSH_CIPHER_SSH2, 16, 16, 0, 1, EVP_aes_128_cbc },
78 	{ "aes192-cbc",		SSH_CIPHER_SSH2, 16, 24, 0, 1, EVP_aes_192_cbc },
79 	{ "aes256-cbc",		SSH_CIPHER_SSH2, 16, 32, 0, 1, EVP_aes_256_cbc },
80 	{ "rijndael-cbc@lysator.liu.se",
81 				SSH_CIPHER_SSH2, 16, 32, 0, 1, EVP_aes_256_cbc },
82 	{ "aes128-ctr",		SSH_CIPHER_SSH2, 16, 16, 0, 0, evp_aes_128_ctr },
83 	{ "aes192-ctr",		SSH_CIPHER_SSH2, 16, 24, 0, 0, evp_aes_128_ctr },
84 	{ "aes256-ctr",		SSH_CIPHER_SSH2, 16, 32, 0, 0, evp_aes_128_ctr },
85 	{ "acss@openssh.org",	SSH_CIPHER_SSH2, 16, 5, 0, 0, EVP_acss },
86 
87 	{ NULL,			SSH_CIPHER_INVALID, 0, 0, 0, 0, NULL }
88 };
89 
90 /*--*/
91 
92 u_int
cipher_blocksize(const Cipher * c)93 cipher_blocksize(const Cipher *c)
94 {
95 	return (c->block_size);
96 }
97 
98 u_int
cipher_keylen(const Cipher * c)99 cipher_keylen(const Cipher *c)
100 {
101 	return (c->key_len);
102 }
103 
104 u_int
cipher_get_number(const Cipher * c)105 cipher_get_number(const Cipher *c)
106 {
107 	return (c->number);
108 }
109 
110 u_int
cipher_is_cbc(const Cipher * c)111 cipher_is_cbc(const Cipher *c)
112 {
113 	return (c->cbc_mode);
114 }
115 
116 u_int
cipher_mask_ssh1(int client)117 cipher_mask_ssh1(int client)
118 {
119 	u_int mask = 0;
120 	mask |= 1 << SSH_CIPHER_3DES;		/* Mandatory */
121 	mask |= 1 << SSH_CIPHER_BLOWFISH;
122 	if (client) {
123 		mask |= 1 << SSH_CIPHER_DES;
124 	}
125 	return mask;
126 }
127 
128 Cipher *
cipher_by_name(const char * name)129 cipher_by_name(const char *name)
130 {
131 	Cipher *c;
132 	for (c = ciphers; c->name != NULL; c++)
133 		if (strcmp(c->name, name) == 0)
134 			return c;
135 	return NULL;
136 }
137 
138 Cipher *
cipher_by_number(int id)139 cipher_by_number(int id)
140 {
141 	Cipher *c;
142 	for (c = ciphers; c->name != NULL; c++)
143 		if (c->number == id)
144 			return c;
145 	return NULL;
146 }
147 
148 #define	CIPHER_SEP	","
149 int
ciphers_valid(const char * names)150 ciphers_valid(const char *names)
151 {
152 	Cipher *c;
153 	char *cipher_list, *cp;
154 	char *p;
155 
156 	if (names == NULL || strcmp(names, "") == 0)
157 		return 0;
158 	cipher_list = cp = xstrdup(names);
159 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
160 	    (p = strsep(&cp, CIPHER_SEP))) {
161 		c = cipher_by_name(p);
162 		if (c == NULL || c->number != SSH_CIPHER_SSH2) {
163 			debug("bad cipher %s [%s]", p, names);
164 			xfree(cipher_list);
165 			return 0;
166 		} else {
167 			debug3("cipher ok: %s [%s]", p, names);
168 		}
169 	}
170 	debug3("ciphers ok: [%s]", names);
171 	xfree(cipher_list);
172 	return 1;
173 }
174 
175 /*
176  * Parses the name of the cipher.  Returns the number of the corresponding
177  * cipher, or -1 on error.
178  */
179 
180 int
cipher_number(const char * name)181 cipher_number(const char *name)
182 {
183 	Cipher *c;
184 	if (name == NULL)
185 		return -1;
186 	for (c = ciphers; c->name != NULL; c++)
187 		if (strcasecmp(c->name, name) == 0)
188 			return c->number;
189 	return -1;
190 }
191 
192 const char *
cipher_name(int id)193 cipher_name(int id)
194 {
195 	Cipher *c = cipher_by_number(id);
196 	return (c==NULL) ? "<unknown>" : c->name;
197 }
198 
199 void
cipher_init(CipherContext * cc,Cipher * cipher,const u_char * key,u_int keylen,const u_char * iv,u_int ivlen,int do_encrypt)200 cipher_init(CipherContext *cc, Cipher *cipher,
201     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
202     int do_encrypt)
203 {
204 	static int dowarn = 1;
205 	const EVP_CIPHER *type;
206 	int klen;
207 	u_char *junk, *discard;
208 
209 	u_char *pushbuf, *pushbufptr;
210 	size_t pushbuflen;
211 
212 	pushbuflen = 2 * sizeof(void *) + 1;
213 	if (key && keylen)
214 		pushbuflen += keylen;
215 	if (iv && ivlen)
216 		pushbuflen += ivlen;
217 
218 	pushbufptr = pushbuf = xmalloc(pushbuflen);
219 	memcpy(pushbufptr, &cc, sizeof(void *));
220 	pushbufptr += sizeof(void *);
221 	memcpy(pushbufptr, &cipher, sizeof(void *));
222 	pushbufptr += sizeof(void *);
223 	*pushbufptr++ = do_encrypt;
224 	if (key && keylen) {
225 		memcpy(pushbufptr, key, keylen);
226 		pushbufptr += keylen;
227 	}
228 	if (iv && ivlen) {
229 		memcpy(pushbufptr, iv, ivlen);
230 		pushbufptr += ivlen;
231 	}
232 	arc4random_pushb_fast(pushbuf, pushbuflen);
233 	xfree(pushbuf);
234 
235 	if (cipher->number == SSH_CIPHER_DES) {
236 		if (dowarn) {
237 			error("Warning: use of DES is strongly discouraged "
238 			    "due to cryptographic weaknesses");
239 			dowarn = 0;
240 		}
241 		if (keylen > 8)
242 			keylen = 8;
243 	}
244 	cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
245 
246 	if (keylen < cipher->key_len)
247 		fatal("cipher_init: key length %d is insufficient for %s.",
248 		    keylen, cipher->name);
249 	if (iv != NULL && ivlen < cipher->block_size)
250 		fatal("cipher_init: iv length %d is insufficient for %s.",
251 		    ivlen, cipher->name);
252 	cc->cipher = cipher;
253 
254 	type = (*cipher->evptype)();
255 
256 	EVP_CIPHER_CTX_init(&cc->evp);
257 	if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
258 	    (do_encrypt == CIPHER_ENCRYPT)) == 0)
259 		fatal("cipher_init: EVP_CipherInit failed for %s",
260 		    cipher->name);
261 	klen = EVP_CIPHER_CTX_key_length(&cc->evp);
262 	if (klen > 0 && keylen != (u_int)klen) {
263 		debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
264 		if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
265 			fatal("cipher_init: set keylen failed (%d -> %d)",
266 			    klen, keylen);
267 	}
268 	if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
269 		fatal("cipher_init: EVP_CipherInit: set key failed for %s",
270 		    cipher->name);
271 
272 	if (cipher->discard_len > 0) {
273 		junk = xmalloc(cipher->discard_len);
274 		discard = xmalloc(cipher->discard_len);
275 		if (EVP_Cipher(&cc->evp, discard, junk,
276 		    cipher->discard_len) == 0)
277 			fatal("evp_crypt: EVP_Cipher failed during discard");
278 		memset(discard, 0, cipher->discard_len);
279 		xfree(junk);
280 		xfree(discard);
281 	}
282 }
283 
284 void
cipher_crypt(CipherContext * cc,u_char * dest,const u_char * src,u_int len)285 cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
286 {
287 	if (len % cc->cipher->block_size)
288 		fatal("cipher_encrypt: bad plaintext length %d", len);
289 	if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
290 		fatal("evp_crypt: EVP_Cipher failed");
291 }
292 
293 void
cipher_cleanup(CipherContext * cc)294 cipher_cleanup(CipherContext *cc)
295 {
296 	if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
297 		error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
298 }
299 
300 /*
301  * Selects the cipher, and keys if by computing the MD5 checksum of the
302  * passphrase and using the resulting 16 bytes as the key.
303  */
304 
305 void
cipher_set_key_string(CipherContext * cc,Cipher * cipher,const char * passphrase,int do_encrypt)306 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
307     const char *passphrase, int do_encrypt)
308 {
309 	MD5_CTX md;
310 	u_char digest[16];
311 
312 	MD5_Init(&md);
313 	MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
314 	MD5_Final(digest, &md);
315 
316 	cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
317 
318 	memset(digest, 0, sizeof(digest));
319 	memset(&md, 0, sizeof(md));
320 }
321 
322 /*
323  * Exports an IV from the CipherContext required to export the key
324  * state back from the unprivileged child to the privileged parent
325  * process.
326  */
327 
328 int
cipher_get_keyiv_len(const CipherContext * cc)329 cipher_get_keyiv_len(const CipherContext *cc)
330 {
331 	Cipher *c = cc->cipher;
332 	int ivlen;
333 
334 	if (c->number == SSH_CIPHER_3DES)
335 		ivlen = 24;
336 	else
337 		ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
338 	return (ivlen);
339 }
340 
341 void
cipher_get_keyiv(CipherContext * cc,u_char * iv,u_int len)342 cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
343 {
344 	Cipher *c = cc->cipher;
345 	int evplen;
346 
347 	switch (c->number) {
348 	case SSH_CIPHER_SSH2:
349 	case SSH_CIPHER_DES:
350 	case SSH_CIPHER_BLOWFISH:
351 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
352 		if (evplen <= 0)
353 			return;
354 		if ((u_int)evplen != len)
355 			fatal("%s: wrong iv length %d != %d", __func__,
356 			    evplen, len);
357 		if (c->evptype == evp_aes_128_ctr)
358 			ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
359 		else
360 			memcpy(iv, cc->evp.iv, len);
361 		break;
362 	case SSH_CIPHER_3DES:
363 		ssh1_3des_iv(&cc->evp, 0, iv, 24);
364 		break;
365 	default:
366 		fatal("%s: bad cipher %d", __func__, c->number);
367 	}
368 }
369 
370 void
cipher_set_keyiv(CipherContext * cc,u_char * iv)371 cipher_set_keyiv(CipherContext *cc, u_char *iv)
372 {
373 	Cipher *c = cc->cipher;
374 	int evplen = 0;
375 
376 	switch (c->number) {
377 	case SSH_CIPHER_SSH2:
378 	case SSH_CIPHER_DES:
379 	case SSH_CIPHER_BLOWFISH:
380 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
381 		if (evplen == 0)
382 			return;
383 		if (c->evptype == evp_aes_128_ctr)
384 			ssh_aes_ctr_iv(&cc->evp, 1, iv, evplen);
385 		else
386 			memcpy(cc->evp.iv, iv, evplen);
387 		break;
388 	case SSH_CIPHER_3DES:
389 		ssh1_3des_iv(&cc->evp, 1, iv, 24);
390 		break;
391 	default:
392 		fatal("%s: bad cipher %d", __func__, c->number);
393 	}
394 }
395 
396 #define EVP_X_STATE(evp)	(evp).cipher_data
397 #define EVP_X_STATE_LEN(evp)	(evp).cipher->ctx_size
398 
399 int
cipher_get_keycontext(const CipherContext * cc,u_char * dat)400 cipher_get_keycontext(const CipherContext *cc, u_char *dat)
401 {
402 	Cipher *c = cc->cipher;
403 	int plen = 0;
404 
405 	if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
406 		plen = EVP_X_STATE_LEN(cc->evp);
407 		if (dat == NULL)
408 			return (plen);
409 		memcpy(dat, EVP_X_STATE(cc->evp), plen);
410 	}
411 	return (plen);
412 }
413 
414 void
cipher_set_keycontext(CipherContext * cc,u_char * dat)415 cipher_set_keycontext(CipherContext *cc, u_char *dat)
416 {
417 	Cipher *c = cc->cipher;
418 	int plen;
419 
420 	if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
421 		plen = EVP_X_STATE_LEN(cc->evp);
422 		memcpy(EVP_X_STATE(cc->evp), dat, plen);
423 	}
424 }
425