xref: /freebsd/contrib/wpa/src/crypto/fips_prf_internal.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*
2  * FIPS 186-2 PRF for internal crypto implementation
3  * Copyright (c) 2006-2007, 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 
11 #include "common.h"
12 #include "sha1.h"
13 #include "sha1_i.h"
14 #include "crypto.h"
15 
16 
17 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
18 {
19 	u8 xkey[64];
20 	u32 t[5], _t[5];
21 	int i, j, m, k;
22 	u8 *xpos = x;
23 	u32 carry;
24 
25 	if (seed_len < sizeof(xkey))
26 		os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len);
27 	else
28 		seed_len = sizeof(xkey);
29 
30 	/* FIPS 186-2 + change notice 1 */
31 
32 	os_memcpy(xkey, seed, seed_len);
33 	t[0] = 0x67452301;
34 	t[1] = 0xEFCDAB89;
35 	t[2] = 0x98BADCFE;
36 	t[3] = 0x10325476;
37 	t[4] = 0xC3D2E1F0;
38 
39 	m = xlen / 40;
40 	for (j = 0; j < m; j++) {
41 		/* XSEED_j = 0 */
42 		for (i = 0; i < 2; i++) {
43 			/* XVAL = (XKEY + XSEED_j) mod 2^b */
44 
45 			/* w_i = G(t, XVAL) */
46 			os_memcpy(_t, t, 20);
47 			SHA1Transform(_t, xkey);
48 			_t[0] = host_to_be32(_t[0]);
49 			_t[1] = host_to_be32(_t[1]);
50 			_t[2] = host_to_be32(_t[2]);
51 			_t[3] = host_to_be32(_t[3]);
52 			_t[4] = host_to_be32(_t[4]);
53 			os_memcpy(xpos, _t, 20);
54 
55 			/* XKEY = (1 + XKEY + w_i) mod 2^b */
56 			carry = 1;
57 			for (k = 19; k >= 0; k--) {
58 				carry += xkey[k] + xpos[k];
59 				xkey[k] = carry & 0xff;
60 				carry >>= 8;
61 			}
62 
63 			xpos += SHA1_MAC_LEN;
64 		}
65 		/* x_j = w_0|w_1 */
66 	}
67 
68 	return 0;
69 }
70