xref: /freebsd/contrib/wpa/src/crypto/fips_prf_wolfssl.c (revision 67350cb56a69468c118bd4ccf6e361b7ebfa9eb4)
1*85732ac8SCy Schubert /*
2*85732ac8SCy Schubert  * FIPS 186-2 PRF for libcrypto
3*85732ac8SCy Schubert  * Copyright (c) 2004-2017, Jouni Malinen <j@w1.fi>
4*85732ac8SCy Schubert  *
5*85732ac8SCy Schubert  * This software may be distributed under the terms of the BSD license.
6*85732ac8SCy Schubert  * See README for more details.
7*85732ac8SCy Schubert  */
8*85732ac8SCy Schubert 
9*85732ac8SCy Schubert #include "includes.h"
10*85732ac8SCy Schubert #include <wolfssl/options.h>
11*85732ac8SCy Schubert #include <wolfssl/wolfcrypt/sha.h>
12*85732ac8SCy Schubert 
13*85732ac8SCy Schubert #include "common.h"
14*85732ac8SCy Schubert #include "crypto.h"
15*85732ac8SCy Schubert 
16*85732ac8SCy Schubert 
sha1_transform(u32 * state,const u8 data[64])17*85732ac8SCy Schubert static void sha1_transform(u32 *state, const u8 data[64])
18*85732ac8SCy Schubert {
19*85732ac8SCy Schubert 	wc_Sha sha;
20*85732ac8SCy Schubert 
21*85732ac8SCy Schubert 	os_memset(&sha, 0, sizeof(sha));
22*85732ac8SCy Schubert 	sha.digest[0] = state[0];
23*85732ac8SCy Schubert 	sha.digest[1] = state[1];
24*85732ac8SCy Schubert 	sha.digest[2] = state[2];
25*85732ac8SCy Schubert 	sha.digest[3] = state[3];
26*85732ac8SCy Schubert 	sha.digest[4] = state[4];
27*85732ac8SCy Schubert 	wc_ShaUpdate(&sha, data, 64);
28*85732ac8SCy Schubert 	state[0] = sha.digest[0];
29*85732ac8SCy Schubert 	state[1] = sha.digest[1];
30*85732ac8SCy Schubert 	state[2] = sha.digest[2];
31*85732ac8SCy Schubert 	state[3] = sha.digest[3];
32*85732ac8SCy Schubert 	state[4] = sha.digest[4];
33*85732ac8SCy Schubert }
34*85732ac8SCy Schubert 
35*85732ac8SCy Schubert 
fips186_2_prf(const u8 * seed,size_t seed_len,u8 * x,size_t xlen)36*85732ac8SCy Schubert int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
37*85732ac8SCy Schubert {
38*85732ac8SCy Schubert 	u8 xkey[64];
39*85732ac8SCy Schubert 	u32 t[5], _t[5];
40*85732ac8SCy Schubert 	int i, j, m, k;
41*85732ac8SCy Schubert 	u8 *xpos = x;
42*85732ac8SCy Schubert 	u32 carry;
43*85732ac8SCy Schubert 
44*85732ac8SCy Schubert 	if (seed_len < sizeof(xkey))
45*85732ac8SCy Schubert 		os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len);
46*85732ac8SCy Schubert 	else
47*85732ac8SCy Schubert 		seed_len = sizeof(xkey);
48*85732ac8SCy Schubert 
49*85732ac8SCy Schubert 	/* FIPS 186-2 + change notice 1 */
50*85732ac8SCy Schubert 
51*85732ac8SCy Schubert 	os_memcpy(xkey, seed, seed_len);
52*85732ac8SCy Schubert 	t[0] = 0x67452301;
53*85732ac8SCy Schubert 	t[1] = 0xEFCDAB89;
54*85732ac8SCy Schubert 	t[2] = 0x98BADCFE;
55*85732ac8SCy Schubert 	t[3] = 0x10325476;
56*85732ac8SCy Schubert 	t[4] = 0xC3D2E1F0;
57*85732ac8SCy Schubert 
58*85732ac8SCy Schubert 	m = xlen / 40;
59*85732ac8SCy Schubert 	for (j = 0; j < m; j++) {
60*85732ac8SCy Schubert 		/* XSEED_j = 0 */
61*85732ac8SCy Schubert 		for (i = 0; i < 2; i++) {
62*85732ac8SCy Schubert 			/* XVAL = (XKEY + XSEED_j) mod 2^b */
63*85732ac8SCy Schubert 
64*85732ac8SCy Schubert 			/* w_i = G(t, XVAL) */
65*85732ac8SCy Schubert 			os_memcpy(_t, t, 20);
66*85732ac8SCy Schubert 			sha1_transform(_t, xkey);
67*85732ac8SCy Schubert 			WPA_PUT_BE32(xpos, _t[0]);
68*85732ac8SCy Schubert 			WPA_PUT_BE32(xpos + 4, _t[1]);
69*85732ac8SCy Schubert 			WPA_PUT_BE32(xpos + 8, _t[2]);
70*85732ac8SCy Schubert 			WPA_PUT_BE32(xpos + 12, _t[3]);
71*85732ac8SCy Schubert 			WPA_PUT_BE32(xpos + 16, _t[4]);
72*85732ac8SCy Schubert 
73*85732ac8SCy Schubert 			/* XKEY = (1 + XKEY + w_i) mod 2^b */
74*85732ac8SCy Schubert 			carry = 1;
75*85732ac8SCy Schubert 			for (k = 19; k >= 0; k--) {
76*85732ac8SCy Schubert 				carry += xkey[k] + xpos[k];
77*85732ac8SCy Schubert 				xkey[k] = carry & 0xff;
78*85732ac8SCy Schubert 				carry >>= 8;
79*85732ac8SCy Schubert 			}
80*85732ac8SCy Schubert 
81*85732ac8SCy Schubert 			xpos += 20;
82*85732ac8SCy Schubert 		}
83*85732ac8SCy Schubert 		/* x_j = w_0|w_1 */
84*85732ac8SCy Schubert 	}
85*85732ac8SCy Schubert 
86*85732ac8SCy Schubert 	return 0;
87*85732ac8SCy Schubert }
88