1 /*
2  * WPA Supplicant / wrapper functions for libcrypto
3  * Copyright (c) 2004-2012, 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 #include <openssl/hmac.h>
18 #include <openssl/rand.h>
19 #ifdef CONFIG_OPENSSL_CMAC
20 #include <openssl/cmac.h>
21 #endif /* CONFIG_OPENSSL_CMAC */
22 
23 #include "common.h"
24 #include "wpabuf.h"
25 #include "dh_group5.h"
26 #include "crypto.h"
27 
28 #if OPENSSL_VERSION_NUMBER < 0x00907000
29 #define DES_key_schedule des_key_schedule
30 #define DES_cblock des_cblock
31 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
32 #define DES_ecb_encrypt(input, output, ks, enc) \
33 	des_ecb_encrypt((input), (output), *(ks), (enc))
34 #endif /* openssl < 0.9.7 */
35 
36 static BIGNUM * get_group5_prime(void)
37 {
38 #if OPENSSL_VERSION_NUMBER < 0x00908000
39 	static const unsigned char RFC3526_PRIME_1536[] = {
40 		0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
41 		0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
42 		0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
43 		0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
44 		0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
45 		0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
46 		0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
47 		0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
48 		0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
49 		0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
50 		0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
51 		0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
52 		0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
53 		0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
54 		0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
55 		0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
56 	};
57         return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
58 #else /* openssl < 0.9.8 */
59 	return get_rfc3526_prime_1536(NULL);
60 #endif /* openssl < 0.9.8 */
61 }
62 
63 #if OPENSSL_VERSION_NUMBER < 0x00908000
64 #ifndef OPENSSL_NO_SHA256
65 #ifndef OPENSSL_FIPS
66 #define NO_SHA256_WRAPPER
67 #endif
68 #endif
69 
70 #endif /* openssl < 0.9.8 */
71 
72 #ifdef OPENSSL_NO_SHA256
73 #define NO_SHA256_WRAPPER
74 #endif
75 
76 static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
77 				 const u8 *addr[], const size_t *len, u8 *mac)
78 {
79 	EVP_MD_CTX ctx;
80 	size_t i;
81 	unsigned int mac_len;
82 
83 	EVP_MD_CTX_init(&ctx);
84 	if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
85 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
86 			   ERR_error_string(ERR_get_error(), NULL));
87 		return -1;
88 	}
89 	for (i = 0; i < num_elem; i++) {
90 		if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
91 			wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
92 				   "failed: %s",
93 				   ERR_error_string(ERR_get_error(), NULL));
94 			return -1;
95 		}
96 	}
97 	if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
98 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
99 			   ERR_error_string(ERR_get_error(), NULL));
100 		return -1;
101 	}
102 
103 	return 0;
104 }
105 
106 
107 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
108 {
109 	return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
110 }
111 
112 
113 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
114 {
115 	u8 pkey[8], next, tmp;
116 	int i;
117 	DES_key_schedule ks;
118 
119 	/* Add parity bits to the key */
120 	next = 0;
121 	for (i = 0; i < 7; i++) {
122 		tmp = key[i];
123 		pkey[i] = (tmp >> i) | next | 1;
124 		next = tmp << (7 - i);
125 	}
126 	pkey[i] = next | 1;
127 
128 	DES_set_key(&pkey, &ks);
129 	DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
130 			DES_ENCRYPT);
131 }
132 
133 
134 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
135 	     u8 *data, size_t data_len)
136 {
137 #ifdef OPENSSL_NO_RC4
138 	return -1;
139 #else /* OPENSSL_NO_RC4 */
140 	EVP_CIPHER_CTX ctx;
141 	int outl;
142 	int res = -1;
143 	unsigned char skip_buf[16];
144 
145 	EVP_CIPHER_CTX_init(&ctx);
146 	if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
147 	    !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
148 	    !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
149 	    !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
150 		goto out;
151 
152 	while (skip >= sizeof(skip_buf)) {
153 		size_t len = skip;
154 		if (len > sizeof(skip_buf))
155 			len = sizeof(skip_buf);
156 		if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
157 			goto out;
158 		skip -= len;
159 	}
160 
161 	if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
162 		res = 0;
163 
164 out:
165 	EVP_CIPHER_CTX_cleanup(&ctx);
166 	return res;
167 #endif /* OPENSSL_NO_RC4 */
168 }
169 
170 
171 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
172 {
173 	return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
174 }
175 
176 
177 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
178 {
179 	return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
180 }
181 
182 
183 #ifndef NO_SHA256_WRAPPER
184 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
185 		  u8 *mac)
186 {
187 	return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
188 }
189 #endif /* NO_SHA256_WRAPPER */
190 
191 
192 static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
193 {
194 	switch (keylen) {
195 	case 16:
196 		return EVP_aes_128_ecb();
197 	case 24:
198 		return EVP_aes_192_ecb();
199 	case 32:
200 		return EVP_aes_256_ecb();
201 	}
202 
203 	return NULL;
204 }
205 
206 
207 void * aes_encrypt_init(const u8 *key, size_t len)
208 {
209 	EVP_CIPHER_CTX *ctx;
210 	const EVP_CIPHER *type;
211 
212 	type = aes_get_evp_cipher(len);
213 	if (type == NULL)
214 		return NULL;
215 
216 	ctx = os_malloc(sizeof(*ctx));
217 	if (ctx == NULL)
218 		return NULL;
219 	EVP_CIPHER_CTX_init(ctx);
220 	if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
221 		os_free(ctx);
222 		return NULL;
223 	}
224 	EVP_CIPHER_CTX_set_padding(ctx, 0);
225 	return ctx;
226 }
227 
228 
229 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
230 {
231 	EVP_CIPHER_CTX *c = ctx;
232 	int clen = 16;
233 	if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
234 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
235 			   ERR_error_string(ERR_get_error(), NULL));
236 	}
237 }
238 
239 
240 void aes_encrypt_deinit(void *ctx)
241 {
242 	EVP_CIPHER_CTX *c = ctx;
243 	u8 buf[16];
244 	int len = sizeof(buf);
245 	if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
246 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
247 			   "%s", ERR_error_string(ERR_get_error(), NULL));
248 	}
249 	if (len != 0) {
250 		wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
251 			   "in AES encrypt", len);
252 	}
253 	EVP_CIPHER_CTX_cleanup(c);
254 	os_free(c);
255 }
256 
257 
258 void * aes_decrypt_init(const u8 *key, size_t len)
259 {
260 	EVP_CIPHER_CTX *ctx;
261 	const EVP_CIPHER *type;
262 
263 	type = aes_get_evp_cipher(len);
264 	if (type == NULL)
265 		return NULL;
266 
267 	ctx = os_malloc(sizeof(*ctx));
268 	if (ctx == NULL)
269 		return NULL;
270 	EVP_CIPHER_CTX_init(ctx);
271 	if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
272 		os_free(ctx);
273 		return NULL;
274 	}
275 	EVP_CIPHER_CTX_set_padding(ctx, 0);
276 	return ctx;
277 }
278 
279 
280 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
281 {
282 	EVP_CIPHER_CTX *c = ctx;
283 	int plen = 16;
284 	if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
285 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
286 			   ERR_error_string(ERR_get_error(), NULL));
287 	}
288 }
289 
290 
291 void aes_decrypt_deinit(void *ctx)
292 {
293 	EVP_CIPHER_CTX *c = ctx;
294 	u8 buf[16];
295 	int len = sizeof(buf);
296 	if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
297 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
298 			   "%s", ERR_error_string(ERR_get_error(), NULL));
299 	}
300 	if (len != 0) {
301 		wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
302 			   "in AES decrypt", len);
303 	}
304 	EVP_CIPHER_CTX_cleanup(c);
305 	os_free(ctx);
306 }
307 
308 
309 int crypto_mod_exp(const u8 *base, size_t base_len,
310 		   const u8 *power, size_t power_len,
311 		   const u8 *modulus, size_t modulus_len,
312 		   u8 *result, size_t *result_len)
313 {
314 	BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
315 	int ret = -1;
316 	BN_CTX *ctx;
317 
318 	ctx = BN_CTX_new();
319 	if (ctx == NULL)
320 		return -1;
321 
322 	bn_base = BN_bin2bn(base, base_len, NULL);
323 	bn_exp = BN_bin2bn(power, power_len, NULL);
324 	bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
325 	bn_result = BN_new();
326 
327 	if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
328 	    bn_result == NULL)
329 		goto error;
330 
331 	if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
332 		goto error;
333 
334 	*result_len = BN_bn2bin(bn_result, result);
335 	ret = 0;
336 
337 error:
338 	BN_free(bn_base);
339 	BN_free(bn_exp);
340 	BN_free(bn_modulus);
341 	BN_free(bn_result);
342 	BN_CTX_free(ctx);
343 	return ret;
344 }
345 
346 
347 struct crypto_cipher {
348 	EVP_CIPHER_CTX enc;
349 	EVP_CIPHER_CTX dec;
350 };
351 
352 
353 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
354 					  const u8 *iv, const u8 *key,
355 					  size_t key_len)
356 {
357 	struct crypto_cipher *ctx;
358 	const EVP_CIPHER *cipher;
359 
360 	ctx = os_zalloc(sizeof(*ctx));
361 	if (ctx == NULL)
362 		return NULL;
363 
364 	switch (alg) {
365 #ifndef OPENSSL_NO_RC4
366 	case CRYPTO_CIPHER_ALG_RC4:
367 		cipher = EVP_rc4();
368 		break;
369 #endif /* OPENSSL_NO_RC4 */
370 #ifndef OPENSSL_NO_AES
371 	case CRYPTO_CIPHER_ALG_AES:
372 		switch (key_len) {
373 		case 16:
374 			cipher = EVP_aes_128_cbc();
375 			break;
376 		case 24:
377 			cipher = EVP_aes_192_cbc();
378 			break;
379 		case 32:
380 			cipher = EVP_aes_256_cbc();
381 			break;
382 		default:
383 			os_free(ctx);
384 			return NULL;
385 		}
386 		break;
387 #endif /* OPENSSL_NO_AES */
388 #ifndef OPENSSL_NO_DES
389 	case CRYPTO_CIPHER_ALG_3DES:
390 		cipher = EVP_des_ede3_cbc();
391 		break;
392 	case CRYPTO_CIPHER_ALG_DES:
393 		cipher = EVP_des_cbc();
394 		break;
395 #endif /* OPENSSL_NO_DES */
396 #ifndef OPENSSL_NO_RC2
397 	case CRYPTO_CIPHER_ALG_RC2:
398 		cipher = EVP_rc2_ecb();
399 		break;
400 #endif /* OPENSSL_NO_RC2 */
401 	default:
402 		os_free(ctx);
403 		return NULL;
404 	}
405 
406 	EVP_CIPHER_CTX_init(&ctx->enc);
407 	EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
408 	if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
409 	    !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
410 	    !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
411 		EVP_CIPHER_CTX_cleanup(&ctx->enc);
412 		os_free(ctx);
413 		return NULL;
414 	}
415 
416 	EVP_CIPHER_CTX_init(&ctx->dec);
417 	EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
418 	if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
419 	    !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
420 	    !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
421 		EVP_CIPHER_CTX_cleanup(&ctx->enc);
422 		EVP_CIPHER_CTX_cleanup(&ctx->dec);
423 		os_free(ctx);
424 		return NULL;
425 	}
426 
427 	return ctx;
428 }
429 
430 
431 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
432 			  u8 *crypt, size_t len)
433 {
434 	int outl;
435 	if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
436 		return -1;
437 	return 0;
438 }
439 
440 
441 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
442 			  u8 *plain, size_t len)
443 {
444 	int outl;
445 	outl = len;
446 	if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
447 		return -1;
448 	return 0;
449 }
450 
451 
452 void crypto_cipher_deinit(struct crypto_cipher *ctx)
453 {
454 	EVP_CIPHER_CTX_cleanup(&ctx->enc);
455 	EVP_CIPHER_CTX_cleanup(&ctx->dec);
456 	os_free(ctx);
457 }
458 
459 
460 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
461 {
462 	DH *dh;
463 	struct wpabuf *pubkey = NULL, *privkey = NULL;
464 	size_t publen, privlen;
465 
466 	*priv = NULL;
467 	*publ = NULL;
468 
469 	dh = DH_new();
470 	if (dh == NULL)
471 		return NULL;
472 
473 	dh->g = BN_new();
474 	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
475 		goto err;
476 
477 	dh->p = get_group5_prime();
478 	if (dh->p == NULL)
479 		goto err;
480 
481 	if (DH_generate_key(dh) != 1)
482 		goto err;
483 
484 	publen = BN_num_bytes(dh->pub_key);
485 	pubkey = wpabuf_alloc(publen);
486 	if (pubkey == NULL)
487 		goto err;
488 	privlen = BN_num_bytes(dh->priv_key);
489 	privkey = wpabuf_alloc(privlen);
490 	if (privkey == NULL)
491 		goto err;
492 
493 	BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
494 	BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
495 
496 	*priv = privkey;
497 	*publ = pubkey;
498 	return dh;
499 
500 err:
501 	wpabuf_free(pubkey);
502 	wpabuf_free(privkey);
503 	DH_free(dh);
504 	return NULL;
505 }
506 
507 
508 void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
509 {
510 	DH *dh;
511 
512 	dh = DH_new();
513 	if (dh == NULL)
514 		return NULL;
515 
516 	dh->g = BN_new();
517 	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
518 		goto err;
519 
520 	dh->p = get_group5_prime();
521 	if (dh->p == NULL)
522 		goto err;
523 
524 	dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
525 	if (dh->priv_key == NULL)
526 		goto err;
527 
528 	dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
529 	if (dh->pub_key == NULL)
530 		goto err;
531 
532 	if (DH_generate_key(dh) != 1)
533 		goto err;
534 
535 	return dh;
536 
537 err:
538 	DH_free(dh);
539 	return NULL;
540 }
541 
542 
543 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
544 				  const struct wpabuf *own_private)
545 {
546 	BIGNUM *pub_key;
547 	struct wpabuf *res = NULL;
548 	size_t rlen;
549 	DH *dh = ctx;
550 	int keylen;
551 
552 	if (ctx == NULL)
553 		return NULL;
554 
555 	pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
556 			    NULL);
557 	if (pub_key == NULL)
558 		return NULL;
559 
560 	rlen = DH_size(dh);
561 	res = wpabuf_alloc(rlen);
562 	if (res == NULL)
563 		goto err;
564 
565 	keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
566 	if (keylen < 0)
567 		goto err;
568 	wpabuf_put(res, keylen);
569 	BN_free(pub_key);
570 
571 	return res;
572 
573 err:
574 	BN_free(pub_key);
575 	wpabuf_free(res);
576 	return NULL;
577 }
578 
579 
580 void dh5_free(void *ctx)
581 {
582 	DH *dh;
583 	if (ctx == NULL)
584 		return;
585 	dh = ctx;
586 	DH_free(dh);
587 }
588 
589 
590 struct crypto_hash {
591 	HMAC_CTX ctx;
592 };
593 
594 
595 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
596 				      size_t key_len)
597 {
598 	struct crypto_hash *ctx;
599 	const EVP_MD *md;
600 
601 	switch (alg) {
602 #ifndef OPENSSL_NO_MD5
603 	case CRYPTO_HASH_ALG_HMAC_MD5:
604 		md = EVP_md5();
605 		break;
606 #endif /* OPENSSL_NO_MD5 */
607 #ifndef OPENSSL_NO_SHA
608 	case CRYPTO_HASH_ALG_HMAC_SHA1:
609 		md = EVP_sha1();
610 		break;
611 #endif /* OPENSSL_NO_SHA */
612 #ifndef OPENSSL_NO_SHA256
613 #ifdef CONFIG_SHA256
614 	case CRYPTO_HASH_ALG_HMAC_SHA256:
615 		md = EVP_sha256();
616 		break;
617 #endif /* CONFIG_SHA256 */
618 #endif /* OPENSSL_NO_SHA256 */
619 	default:
620 		return NULL;
621 	}
622 
623 	ctx = os_zalloc(sizeof(*ctx));
624 	if (ctx == NULL)
625 		return NULL;
626 	HMAC_CTX_init(&ctx->ctx);
627 
628 #if OPENSSL_VERSION_NUMBER < 0x00909000
629 	HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
630 #else /* openssl < 0.9.9 */
631 	if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
632 		os_free(ctx);
633 		return NULL;
634 	}
635 #endif /* openssl < 0.9.9 */
636 
637 	return ctx;
638 }
639 
640 
641 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
642 {
643 	if (ctx == NULL)
644 		return;
645 	HMAC_Update(&ctx->ctx, data, len);
646 }
647 
648 
649 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
650 {
651 	unsigned int mdlen;
652 	int res;
653 
654 	if (ctx == NULL)
655 		return -2;
656 
657 	if (mac == NULL || len == NULL) {
658 		os_free(ctx);
659 		return 0;
660 	}
661 
662 	mdlen = *len;
663 #if OPENSSL_VERSION_NUMBER < 0x00909000
664 	HMAC_Final(&ctx->ctx, mac, &mdlen);
665 	res = 1;
666 #else /* openssl < 0.9.9 */
667 	res = HMAC_Final(&ctx->ctx, mac, &mdlen);
668 #endif /* openssl < 0.9.9 */
669 	HMAC_CTX_cleanup(&ctx->ctx);
670 	os_free(ctx);
671 
672 	if (res == 1) {
673 		*len = mdlen;
674 		return 0;
675 	}
676 
677 	return -1;
678 }
679 
680 
681 int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
682 		int iterations, u8 *buf, size_t buflen)
683 {
684 #if OPENSSL_VERSION_NUMBER < 0x00908000
685 	if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
686 				   (unsigned char *) ssid,
687 				   ssid_len, 4096, buflen, buf) != 1)
688 		return -1;
689 #else /* openssl < 0.9.8 */
690 	if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
691 				   ssid_len, 4096, buflen, buf) != 1)
692 		return -1;
693 #endif /* openssl < 0.9.8 */
694 	return 0;
695 }
696 
697 
698 int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
699 		     const u8 *addr[], const size_t *len, u8 *mac)
700 {
701 	HMAC_CTX ctx;
702 	size_t i;
703 	unsigned int mdlen;
704 	int res;
705 
706 	HMAC_CTX_init(&ctx);
707 #if OPENSSL_VERSION_NUMBER < 0x00909000
708 	HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL);
709 #else /* openssl < 0.9.9 */
710 	if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
711 		return -1;
712 #endif /* openssl < 0.9.9 */
713 
714 	for (i = 0; i < num_elem; i++)
715 		HMAC_Update(&ctx, addr[i], len[i]);
716 
717 	mdlen = 20;
718 #if OPENSSL_VERSION_NUMBER < 0x00909000
719 	HMAC_Final(&ctx, mac, &mdlen);
720 	res = 1;
721 #else /* openssl < 0.9.9 */
722 	res = HMAC_Final(&ctx, mac, &mdlen);
723 #endif /* openssl < 0.9.9 */
724 	HMAC_CTX_cleanup(&ctx);
725 
726 	return res == 1 ? 0 : -1;
727 }
728 
729 
730 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
731 	       u8 *mac)
732 {
733 	return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
734 }
735 
736 
737 #ifdef CONFIG_SHA256
738 
739 int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
740 		       const u8 *addr[], const size_t *len, u8 *mac)
741 {
742 	HMAC_CTX ctx;
743 	size_t i;
744 	unsigned int mdlen;
745 	int res;
746 
747 	HMAC_CTX_init(&ctx);
748 #if OPENSSL_VERSION_NUMBER < 0x00909000
749 	HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
750 #else /* openssl < 0.9.9 */
751 	if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
752 		return -1;
753 #endif /* openssl < 0.9.9 */
754 
755 	for (i = 0; i < num_elem; i++)
756 		HMAC_Update(&ctx, addr[i], len[i]);
757 
758 	mdlen = 32;
759 #if OPENSSL_VERSION_NUMBER < 0x00909000
760 	HMAC_Final(&ctx, mac, &mdlen);
761 	res = 1;
762 #else /* openssl < 0.9.9 */
763 	res = HMAC_Final(&ctx, mac, &mdlen);
764 #endif /* openssl < 0.9.9 */
765 	HMAC_CTX_cleanup(&ctx);
766 
767 	return res == 1 ? 0 : -1;
768 }
769 
770 
771 int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
772 		size_t data_len, u8 *mac)
773 {
774 	return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
775 }
776 
777 #endif /* CONFIG_SHA256 */
778 
779 
780 int crypto_get_random(void *buf, size_t len)
781 {
782 	if (RAND_bytes(buf, len) != 1)
783 		return -1;
784 	return 0;
785 }
786 
787 
788 #ifdef CONFIG_OPENSSL_CMAC
789 int omac1_aes_128_vector(const u8 *key, size_t num_elem,
790 			 const u8 *addr[], const size_t *len, u8 *mac)
791 {
792 	CMAC_CTX *ctx;
793 	int ret = -1;
794 	size_t outlen, i;
795 
796 	ctx = CMAC_CTX_new();
797 	if (ctx == NULL)
798 		return -1;
799 
800 	if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
801 		goto fail;
802 	for (i = 0; i < num_elem; i++) {
803 		if (!CMAC_Update(ctx, addr[i], len[i]))
804 			goto fail;
805 	}
806 	if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
807 		goto fail;
808 
809 	ret = 0;
810 fail:
811 	CMAC_CTX_free(ctx);
812 	return ret;
813 }
814 
815 
816 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
817 {
818 	return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
819 }
820 #endif /* CONFIG_OPENSSL_CMAC */
821