• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant / wrapper functions for libcrypto
3  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 #include <openssl/opensslv.h>
11 #include <openssl/err.h>
12 #include <openssl/des.h>
13 #include <openssl/aes.h>
14 #include <openssl/bn.h>
15 #include <openssl/evp.h>
16 #include <openssl/dh.h>
17 
18 #include "common.h"
19 #include "wpabuf.h"
20 #include "dh_group5.h"
21 #include "crypto.h"
22 
23 #if OPENSSL_VERSION_NUMBER < 0x00907000
24 #define DES_key_schedule des_key_schedule
25 #define DES_cblock des_cblock
26 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
27 #define DES_ecb_encrypt(input, output, ks, enc) \
28 	des_ecb_encrypt((input), (output), *(ks), (enc))
29 #endif /* openssl < 0.9.7 */
30 
get_group5_prime(void)31 static BIGNUM * get_group5_prime(void)
32 {
33 #if OPENSSL_VERSION_NUMBER < 0x00908000
34 	static const unsigned char RFC3526_PRIME_1536[] = {
35 		0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
36 		0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
37 		0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
38 		0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
39 		0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
40 		0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
41 		0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
42 		0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
43 		0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
44 		0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
45 		0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
46 		0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
47 		0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
48 		0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
49 		0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
50 		0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
51 	};
52         return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
53 #else /* openssl < 0.9.8 */
54 	return get_rfc3526_prime_1536(NULL);
55 #endif /* openssl < 0.9.8 */
56 }
57 
58 #if OPENSSL_VERSION_NUMBER < 0x00908000
59 #ifndef OPENSSL_NO_SHA256
60 #ifndef OPENSSL_FIPS
61 #define NO_SHA256_WRAPPER
62 #endif
63 #endif
64 
65 #endif /* openssl < 0.9.8 */
66 
67 #ifdef OPENSSL_NO_SHA256
68 #define NO_SHA256_WRAPPER
69 #endif
70 
openssl_digest_vector(const EVP_MD * type,int non_fips,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)71 static int openssl_digest_vector(const EVP_MD *type, int non_fips,
72 				 size_t num_elem, const u8 *addr[],
73 				 const size_t *len, u8 *mac)
74 {
75 	EVP_MD_CTX ctx;
76 	size_t i;
77 	unsigned int mac_len;
78 
79 	EVP_MD_CTX_init(&ctx);
80 #ifdef CONFIG_FIPS
81 #ifdef OPENSSL_FIPS
82 	if (non_fips)
83 		EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
84 #endif /* OPENSSL_FIPS */
85 #endif /* CONFIG_FIPS */
86 	if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
87 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
88 			   ERR_error_string(ERR_get_error(), NULL));
89 		return -1;
90 	}
91 	for (i = 0; i < num_elem; i++) {
92 		if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
93 			wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
94 				   "failed: %s",
95 				   ERR_error_string(ERR_get_error(), NULL));
96 			return -1;
97 		}
98 	}
99 	if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
100 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
101 			   ERR_error_string(ERR_get_error(), NULL));
102 		return -1;
103 	}
104 
105 	return 0;
106 }
107 
108 
md4_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)109 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
110 {
111 	return openssl_digest_vector(EVP_md4(), 0, num_elem, addr, len, mac);
112 }
113 
114 
des_encrypt(const u8 * clear,const u8 * key,u8 * cypher)115 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
116 {
117 	u8 pkey[8], next, tmp;
118 	int i;
119 	DES_key_schedule ks;
120 
121 	/* Add parity bits to the key */
122 	next = 0;
123 	for (i = 0; i < 7; i++) {
124 		tmp = key[i];
125 		pkey[i] = (tmp >> i) | next | 1;
126 		next = tmp << (7 - i);
127 	}
128 	pkey[i] = next | 1;
129 
130 	DES_set_key(&pkey, &ks);
131 	DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
132 			DES_ENCRYPT);
133 }
134 
135 
rc4_skip(const u8 * key,size_t keylen,size_t skip,u8 * data,size_t data_len)136 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
137 	     u8 *data, size_t data_len)
138 {
139 #ifdef OPENSSL_NO_RC4
140 	return -1;
141 #else /* OPENSSL_NO_RC4 */
142 	EVP_CIPHER_CTX ctx;
143 	int outl;
144 	int res = -1;
145 	unsigned char skip_buf[16];
146 
147 	EVP_CIPHER_CTX_init(&ctx);
148 	if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
149 	    !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
150 	    !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
151 	    !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
152 		goto out;
153 
154 	while (skip >= sizeof(skip_buf)) {
155 		size_t len = skip;
156 		if (len > sizeof(skip_buf))
157 			len = sizeof(skip_buf);
158 		if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
159 			goto out;
160 		skip -= len;
161 	}
162 
163 	if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
164 		res = 0;
165 
166 out:
167 	EVP_CIPHER_CTX_cleanup(&ctx);
168 	return res;
169 #endif /* OPENSSL_NO_RC4 */
170 }
171 
172 
md5_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)173 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
174 {
175 	return openssl_digest_vector(EVP_md5(), 0, num_elem, addr, len, mac);
176 }
177 
178 
179 #ifdef CONFIG_FIPS
md5_vector_non_fips_allow(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)180 int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
181 			      const size_t *len, u8 *mac)
182 {
183 	return openssl_digest_vector(EVP_md5(), 1, num_elem, addr, len, mac);
184 }
185 #endif /* CONFIG_FIPS */
186 
187 
sha1_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)188 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
189 {
190 	return openssl_digest_vector(EVP_sha1(), 0, num_elem, addr, len, mac);
191 }
192 
193 
194 #ifndef NO_SHA256_WRAPPER
sha256_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)195 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
196 		  u8 *mac)
197 {
198 	return openssl_digest_vector(EVP_sha256(), 0, num_elem, addr, len,
199 				     mac);
200 }
201 #endif /* NO_SHA256_WRAPPER */
202 
203 
aes_encrypt_init(const u8 * key,size_t len)204 void * aes_encrypt_init(const u8 *key, size_t len)
205 {
206 	AES_KEY *ak;
207 	ak = os_malloc(sizeof(*ak));
208 	if (ak == NULL)
209 		return NULL;
210 	if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
211 		os_free(ak);
212 		return NULL;
213 	}
214 	return ak;
215 }
216 
217 
aes_encrypt(void * ctx,const u8 * plain,u8 * crypt)218 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
219 {
220 	AES_encrypt(plain, crypt, ctx);
221 }
222 
223 
aes_encrypt_deinit(void * ctx)224 void aes_encrypt_deinit(void *ctx)
225 {
226 	os_free(ctx);
227 }
228 
229 
aes_decrypt_init(const u8 * key,size_t len)230 void * aes_decrypt_init(const u8 *key, size_t len)
231 {
232 	AES_KEY *ak;
233 	ak = os_malloc(sizeof(*ak));
234 	if (ak == NULL)
235 		return NULL;
236 	if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
237 		os_free(ak);
238 		return NULL;
239 	}
240 	return ak;
241 }
242 
243 
aes_decrypt(void * ctx,const u8 * crypt,u8 * plain)244 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
245 {
246 	AES_decrypt(crypt, plain, ctx);
247 }
248 
249 
aes_decrypt_deinit(void * ctx)250 void aes_decrypt_deinit(void *ctx)
251 {
252 	os_free(ctx);
253 }
254 
255 
crypto_mod_exp(const u8 * base,size_t base_len,const u8 * power,size_t power_len,const u8 * modulus,size_t modulus_len,u8 * result,size_t * result_len)256 int crypto_mod_exp(const u8 *base, size_t base_len,
257 		   const u8 *power, size_t power_len,
258 		   const u8 *modulus, size_t modulus_len,
259 		   u8 *result, size_t *result_len)
260 {
261 	BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
262 	int ret = -1;
263 	BN_CTX *ctx;
264 
265 	ctx = BN_CTX_new();
266 	if (ctx == NULL)
267 		return -1;
268 
269 	bn_base = BN_bin2bn(base, base_len, NULL);
270 	bn_exp = BN_bin2bn(power, power_len, NULL);
271 	bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
272 	bn_result = BN_new();
273 
274 	if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
275 	    bn_result == NULL)
276 		goto error;
277 
278 	if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
279 		goto error;
280 
281 	*result_len = BN_bn2bin(bn_result, result);
282 	ret = 0;
283 
284 error:
285 	BN_free(bn_base);
286 	BN_free(bn_exp);
287 	BN_free(bn_modulus);
288 	BN_free(bn_result);
289 	BN_CTX_free(ctx);
290 	return ret;
291 }
292 
293 
294 struct crypto_cipher {
295 	EVP_CIPHER_CTX enc;
296 	EVP_CIPHER_CTX dec;
297 };
298 
299 
crypto_cipher_init(enum crypto_cipher_alg alg,const u8 * iv,const u8 * key,size_t key_len)300 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
301 					  const u8 *iv, const u8 *key,
302 					  size_t key_len)
303 {
304 	struct crypto_cipher *ctx;
305 	const EVP_CIPHER *cipher;
306 
307 	ctx = os_zalloc(sizeof(*ctx));
308 	if (ctx == NULL)
309 		return NULL;
310 
311 	switch (alg) {
312 #ifndef OPENSSL_NO_RC4
313 	case CRYPTO_CIPHER_ALG_RC4:
314 		cipher = EVP_rc4();
315 		break;
316 #endif /* OPENSSL_NO_RC4 */
317 #ifndef OPENSSL_NO_AES
318 	case CRYPTO_CIPHER_ALG_AES:
319 		switch (key_len) {
320 		case 16:
321 			cipher = EVP_aes_128_cbc();
322 			break;
323 		case 24:
324 			cipher = EVP_aes_192_cbc();
325 			break;
326 		case 32:
327 			cipher = EVP_aes_256_cbc();
328 			break;
329 		default:
330 			os_free(ctx);
331 			return NULL;
332 		}
333 		break;
334 #endif /* OPENSSL_NO_AES */
335 #ifndef OPENSSL_NO_DES
336 	case CRYPTO_CIPHER_ALG_3DES:
337 		cipher = EVP_des_ede3_cbc();
338 		break;
339 	case CRYPTO_CIPHER_ALG_DES:
340 		cipher = EVP_des_cbc();
341 		break;
342 #endif /* OPENSSL_NO_DES */
343 #ifndef OPENSSL_NO_RC2
344 	case CRYPTO_CIPHER_ALG_RC2:
345 		cipher = EVP_rc2_ecb();
346 		break;
347 #endif /* OPENSSL_NO_RC2 */
348 	default:
349 		os_free(ctx);
350 		return NULL;
351 	}
352 
353 	EVP_CIPHER_CTX_init(&ctx->enc);
354 	EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
355 	if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
356 	    !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
357 	    !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
358 		EVP_CIPHER_CTX_cleanup(&ctx->enc);
359 		os_free(ctx);
360 		return NULL;
361 	}
362 
363 	EVP_CIPHER_CTX_init(&ctx->dec);
364 	EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
365 	if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
366 	    !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
367 	    !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
368 		EVP_CIPHER_CTX_cleanup(&ctx->enc);
369 		EVP_CIPHER_CTX_cleanup(&ctx->dec);
370 		os_free(ctx);
371 		return NULL;
372 	}
373 
374 	return ctx;
375 }
376 
377 
crypto_cipher_encrypt(struct crypto_cipher * ctx,const u8 * plain,u8 * crypt,size_t len)378 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
379 			  u8 *crypt, size_t len)
380 {
381 	int outl;
382 	if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
383 		return -1;
384 	return 0;
385 }
386 
387 
crypto_cipher_decrypt(struct crypto_cipher * ctx,const u8 * crypt,u8 * plain,size_t len)388 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
389 			  u8 *plain, size_t len)
390 {
391 	int outl;
392 	outl = len;
393 	if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
394 		return -1;
395 	return 0;
396 }
397 
398 
crypto_cipher_deinit(struct crypto_cipher * ctx)399 void crypto_cipher_deinit(struct crypto_cipher *ctx)
400 {
401 	EVP_CIPHER_CTX_cleanup(&ctx->enc);
402 	EVP_CIPHER_CTX_cleanup(&ctx->dec);
403 	os_free(ctx);
404 }
405 
406 
dh5_init(struct wpabuf ** priv,struct wpabuf ** publ)407 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
408 {
409 	DH *dh;
410 	struct wpabuf *pubkey = NULL, *privkey = NULL;
411 	size_t publen, privlen;
412 
413 	*priv = NULL;
414 	*publ = NULL;
415 
416 	dh = DH_new();
417 	if (dh == NULL)
418 		return NULL;
419 
420 	dh->g = BN_new();
421 	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
422 		goto err;
423 
424 	dh->p = get_group5_prime();
425 	if (dh->p == NULL)
426 		goto err;
427 
428 	if (DH_generate_key(dh) != 1)
429 		goto err;
430 
431 	publen = BN_num_bytes(dh->pub_key);
432 	pubkey = wpabuf_alloc(publen);
433 	if (pubkey == NULL)
434 		goto err;
435 	privlen = BN_num_bytes(dh->priv_key);
436 	privkey = wpabuf_alloc(privlen);
437 	if (privkey == NULL)
438 		goto err;
439 
440 	BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
441 	BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
442 
443 	*priv = privkey;
444 	*publ = pubkey;
445 	return dh;
446 
447 err:
448 	wpabuf_free(pubkey);
449 	wpabuf_free(privkey);
450 	DH_free(dh);
451 	return NULL;
452 }
453 
454 
dh5_derive_shared(void * ctx,const struct wpabuf * peer_public,const struct wpabuf * own_private)455 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
456 				  const struct wpabuf *own_private)
457 {
458 	BIGNUM *pub_key;
459 	struct wpabuf *res = NULL;
460 	size_t rlen;
461 	DH *dh = ctx;
462 	int keylen;
463 
464 	if (ctx == NULL)
465 		return NULL;
466 
467 	pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
468 			    NULL);
469 	if (pub_key == NULL)
470 		return NULL;
471 
472 	rlen = DH_size(dh);
473 	res = wpabuf_alloc(rlen);
474 	if (res == NULL)
475 		goto err;
476 
477 	keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
478 	if (keylen < 0)
479 		goto err;
480 	wpabuf_put(res, keylen);
481 	BN_free(pub_key);
482 
483 	return res;
484 
485 err:
486 	BN_free(pub_key);
487 	wpabuf_free(res);
488 	return NULL;
489 }
490 
491 
dh5_free(void * ctx)492 void dh5_free(void *ctx)
493 {
494 	DH *dh;
495 	if (ctx == NULL)
496 		return;
497 	dh = ctx;
498 	DH_free(dh);
499 }
500