xref: /openbsd-src/usr.bin/ssh/cipher.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  *
6  * As far as I am concerned, the code I have written for this software
7  * can be used freely for any purpose.  Any derived versions of this
8  * software must be clearly marked as such, and if the derived work is
9  * incompatible with the protocol description in the RFC file, it must be
10  * called by a name other than "ssh" or "Secure Shell".
11  *
12  *
13  * Copyright (c) 1999 Niels Provos.  All rights reserved.
14  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include "includes.h"
38 RCSID("$OpenBSD: cipher.c,v 1.68 2004/01/23 19:26:33 hshoexer Exp $");
39 
40 #include "xmalloc.h"
41 #include "log.h"
42 #include "cipher.h"
43 
44 #include <openssl/md5.h>
45 
46 #if OPENSSL_VERSION_NUMBER < 0x00907000L
47 extern const EVP_CIPHER *evp_rijndael(void);
48 extern void ssh_rijndael_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
49 #endif
50 extern const EVP_CIPHER *evp_ssh1_bf(void);
51 extern const EVP_CIPHER *evp_ssh1_3des(void);
52 extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
53 extern const EVP_CIPHER *evp_aes_128_ctr(void);
54 extern void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
55 
56 struct Cipher {
57 	char	*name;
58 	int	number;		/* for ssh1 only */
59 	u_int	block_size;
60 	u_int	key_len;
61 	const EVP_CIPHER	*(*evptype)(void);
62 } ciphers[] = {
63 	{ "none", 		SSH_CIPHER_NONE, 8, 0, EVP_enc_null },
64 	{ "des", 		SSH_CIPHER_DES, 8, 8, EVP_des_cbc },
65 	{ "3des", 		SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des },
66 	{ "blowfish", 		SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf },
67 
68 	{ "3des-cbc", 		SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc },
69 	{ "blowfish-cbc", 	SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc },
70 	{ "cast128-cbc", 	SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc },
71 	{ "arcfour", 		SSH_CIPHER_SSH2, 8, 16, EVP_rc4 },
72 #if OPENSSL_VERSION_NUMBER < 0x00907000L
73 	{ "aes128-cbc", 	SSH_CIPHER_SSH2, 16, 16, evp_rijndael },
74 	{ "aes192-cbc", 	SSH_CIPHER_SSH2, 16, 24, evp_rijndael },
75 	{ "aes256-cbc", 	SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
76 	{ "rijndael-cbc@lysator.liu.se",
77 				SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
78 #else
79 	{ "aes128-cbc",		SSH_CIPHER_SSH2, 16, 16, EVP_aes_128_cbc },
80 	{ "aes192-cbc",		SSH_CIPHER_SSH2, 16, 24, EVP_aes_192_cbc },
81 	{ "aes256-cbc",		SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
82 	{ "rijndael-cbc@lysator.liu.se",
83 				SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
84 #endif
85 	{ "aes128-ctr", 	SSH_CIPHER_SSH2, 16, 16, evp_aes_128_ctr },
86 	{ "aes192-ctr", 	SSH_CIPHER_SSH2, 16, 24, evp_aes_128_ctr },
87 	{ "aes256-ctr", 	SSH_CIPHER_SSH2, 16, 32, evp_aes_128_ctr },
88 	{ "acss@openssh.org",	SSH_CIPHER_SSH2, 16, 5, EVP_acss },
89 
90 	{ NULL,			SSH_CIPHER_ILLEGAL, 0, 0, NULL }
91 };
92 
93 /*--*/
94 
95 u_int
96 cipher_blocksize(const Cipher *c)
97 {
98 	return (c->block_size);
99 }
100 
101 u_int
102 cipher_keylen(const Cipher *c)
103 {
104 	return (c->key_len);
105 }
106 
107 u_int
108 cipher_get_number(const Cipher *c)
109 {
110 	return (c->number);
111 }
112 
113 u_int
114 cipher_mask_ssh1(int client)
115 {
116 	u_int mask = 0;
117 	mask |= 1 << SSH_CIPHER_3DES;		/* Mandatory */
118 	mask |= 1 << SSH_CIPHER_BLOWFISH;
119 	if (client) {
120 		mask |= 1 << SSH_CIPHER_DES;
121 	}
122 	return mask;
123 }
124 
125 Cipher *
126 cipher_by_name(const char *name)
127 {
128 	Cipher *c;
129 	for (c = ciphers; c->name != NULL; c++)
130 		if (strcasecmp(c->name, name) == 0)
131 			return c;
132 	return NULL;
133 }
134 
135 Cipher *
136 cipher_by_number(int id)
137 {
138 	Cipher *c;
139 	for (c = ciphers; c->name != NULL; c++)
140 		if (c->number == id)
141 			return c;
142 	return NULL;
143 }
144 
145 #define	CIPHER_SEP	","
146 int
147 ciphers_valid(const char *names)
148 {
149 	Cipher *c;
150 	char *ciphers, *cp;
151 	char *p;
152 
153 	if (names == NULL || strcmp(names, "") == 0)
154 		return 0;
155 	ciphers = cp = xstrdup(names);
156 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
157 	    (p = strsep(&cp, CIPHER_SEP))) {
158 		c = cipher_by_name(p);
159 		if (c == NULL || c->number != SSH_CIPHER_SSH2) {
160 			debug("bad cipher %s [%s]", p, names);
161 			xfree(ciphers);
162 			return 0;
163 		} else {
164 			debug3("cipher ok: %s [%s]", p, names);
165 		}
166 	}
167 	debug3("ciphers ok: [%s]", names);
168 	xfree(ciphers);
169 	return 1;
170 }
171 
172 /*
173  * Parses the name of the cipher.  Returns the number of the corresponding
174  * cipher, or -1 on error.
175  */
176 
177 int
178 cipher_number(const char *name)
179 {
180 	Cipher *c;
181 	if (name == NULL)
182 		return -1;
183 	c = cipher_by_name(name);
184 	return (c==NULL) ? -1 : c->number;
185 }
186 
187 char *
188 cipher_name(int id)
189 {
190 	Cipher *c = cipher_by_number(id);
191 	return (c==NULL) ? "<unknown>" : c->name;
192 }
193 
194 void
195 cipher_init(CipherContext *cc, Cipher *cipher,
196     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
197     int encrypt)
198 {
199 	static int dowarn = 1;
200 	const EVP_CIPHER *type;
201 	int klen;
202 
203 	if (cipher->number == SSH_CIPHER_DES) {
204 		if (dowarn) {
205 			error("Warning: use of DES is strongly discouraged "
206 			    "due to cryptographic weaknesses");
207 			dowarn = 0;
208 		}
209 		if (keylen > 8)
210 			keylen = 8;
211 	}
212 	cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
213 
214 	if (keylen < cipher->key_len)
215 		fatal("cipher_init: key length %d is insufficient for %s.",
216 		    keylen, cipher->name);
217 	if (iv != NULL && ivlen < cipher->block_size)
218 		fatal("cipher_init: iv length %d is insufficient for %s.",
219 		    ivlen, cipher->name);
220 	cc->cipher = cipher;
221 
222 	type = (*cipher->evptype)();
223 
224 	EVP_CIPHER_CTX_init(&cc->evp);
225 	if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
226 	    (encrypt == CIPHER_ENCRYPT)) == 0)
227 		fatal("cipher_init: EVP_CipherInit failed for %s",
228 		    cipher->name);
229 	klen = EVP_CIPHER_CTX_key_length(&cc->evp);
230 	if (klen > 0 && keylen != klen) {
231 		debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
232 		if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
233 			fatal("cipher_init: set keylen failed (%d -> %d)",
234 			    klen, keylen);
235 	}
236 	if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
237 		fatal("cipher_init: EVP_CipherInit: set key failed for %s",
238 		    cipher->name);
239 }
240 
241 void
242 cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
243 {
244 	if (len % cc->cipher->block_size)
245 		fatal("cipher_encrypt: bad plaintext length %d", len);
246 	if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
247 		fatal("evp_crypt: EVP_Cipher failed");
248 }
249 
250 void
251 cipher_cleanup(CipherContext *cc)
252 {
253 	if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
254 		error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
255 }
256 
257 /*
258  * Selects the cipher, and keys if by computing the MD5 checksum of the
259  * passphrase and using the resulting 16 bytes as the key.
260  */
261 
262 void
263 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
264     const char *passphrase, int encrypt)
265 {
266 	MD5_CTX md;
267 	u_char digest[16];
268 
269 	MD5_Init(&md);
270 	MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
271 	MD5_Final(digest, &md);
272 
273 	cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt);
274 
275 	memset(digest, 0, sizeof(digest));
276 	memset(&md, 0, sizeof(md));
277 }
278 
279 /*
280  * Exports an IV from the CipherContext required to export the key
281  * state back from the unprivileged child to the privileged parent
282  * process.
283  */
284 
285 int
286 cipher_get_keyiv_len(const CipherContext *cc)
287 {
288 	Cipher *c = cc->cipher;
289 	int ivlen;
290 
291 	if (c->number == SSH_CIPHER_3DES)
292 		ivlen = 24;
293 	else
294 		ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
295 	return (ivlen);
296 }
297 
298 void
299 cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
300 {
301 	Cipher *c = cc->cipher;
302 	int evplen;
303 
304 	switch (c->number) {
305 	case SSH_CIPHER_SSH2:
306 	case SSH_CIPHER_DES:
307 	case SSH_CIPHER_BLOWFISH:
308 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
309 		if (evplen == 0)
310 			return;
311 		if (evplen != len)
312 			fatal("%s: wrong iv length %d != %d", __func__,
313 			    evplen, len);
314 #if OPENSSL_VERSION_NUMBER < 0x00907000L
315 		if (c->evptype == evp_rijndael)
316 			ssh_rijndael_iv(&cc->evp, 0, iv, len);
317 		else
318 #endif
319 		if (c->evptype == evp_aes_128_ctr)
320 			ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
321 		else
322 			memcpy(iv, cc->evp.iv, len);
323 		break;
324 	case SSH_CIPHER_3DES:
325 		ssh1_3des_iv(&cc->evp, 0, iv, 24);
326 		break;
327 	default:
328 		fatal("%s: bad cipher %d", __func__, c->number);
329 	}
330 }
331 
332 void
333 cipher_set_keyiv(CipherContext *cc, u_char *iv)
334 {
335 	Cipher *c = cc->cipher;
336 	int evplen = 0;
337 
338 	switch (c->number) {
339 	case SSH_CIPHER_SSH2:
340 	case SSH_CIPHER_DES:
341 	case SSH_CIPHER_BLOWFISH:
342 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
343 		if (evplen == 0)
344 			return;
345 #if OPENSSL_VERSION_NUMBER < 0x00907000L
346 		if (c->evptype == evp_rijndael)
347 			ssh_rijndael_iv(&cc->evp, 1, iv, evplen);
348 		else
349 #endif
350 		if (c->evptype == evp_aes_128_ctr)
351 			ssh_aes_ctr_iv(&cc->evp, 1, iv, evplen);
352 		else
353 			memcpy(cc->evp.iv, iv, evplen);
354 		break;
355 	case SSH_CIPHER_3DES:
356 		ssh1_3des_iv(&cc->evp, 1, iv, 24);
357 		break;
358 	default:
359 		fatal("%s: bad cipher %d", __func__, c->number);
360 	}
361 }
362 
363 #if OPENSSL_VERSION_NUMBER < 0x00907000L
364 #define EVP_X_STATE(evp)	&(evp).c
365 #define EVP_X_STATE_LEN(evp)	sizeof((evp).c)
366 #else
367 #define EVP_X_STATE(evp)	(evp).cipher_data
368 #define EVP_X_STATE_LEN(evp)	(evp).cipher->ctx_size
369 #endif
370 
371 int
372 cipher_get_keycontext(const CipherContext *cc, u_char *dat)
373 {
374 	Cipher *c = cc->cipher;
375 	int plen = 0;
376 
377 	if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
378 		plen = EVP_X_STATE_LEN(cc->evp);
379 		if (dat == NULL)
380 			return (plen);
381 		memcpy(dat, EVP_X_STATE(cc->evp), plen);
382 	}
383 	return (plen);
384 }
385 
386 void
387 cipher_set_keycontext(CipherContext *cc, u_char *dat)
388 {
389 	Cipher *c = cc->cipher;
390 	int plen;
391 
392 	if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
393 		plen = EVP_X_STATE_LEN(cc->evp);
394 		memcpy(EVP_X_STATE(cc->evp), dat, plen);
395 	}
396 }
397