2 * FIPS 186-2 PRF for libcrypto
3 * Copyright (c) 2004-2005, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 #include <openssl/sha.h>
16 static void sha1_transform(u32 *state, const u8 data[64])
19 os_memset(&context, 0, sizeof(context));
20 #if defined(OPENSSL_IS_BORINGSSL) && !defined(ANDROID)
21 context.h[0] = state[0];
22 context.h[1] = state[1];
23 context.h[2] = state[2];
24 context.h[3] = state[3];
25 context.h[4] = state[4];
26 SHA1_Transform(&context, data);
27 state[0] = context.h[0];
28 state[1] = context.h[1];
29 state[2] = context.h[2];
30 state[3] = context.h[3];
31 state[4] = context.h[4];
33 context.h0 = state[0];
34 context.h1 = state[1];
35 context.h2 = state[2];
36 context.h3 = state[3];
37 context.h4 = state[4];
38 SHA1_Transform(&context, data);
39 state[0] = context.h0;
40 state[1] = context.h1;
41 state[2] = context.h2;
42 state[3] = context.h3;
43 state[4] = context.h4;
48 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
56 if (seed_len < sizeof(xkey))
57 os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len);
59 seed_len = sizeof(xkey);
61 /* FIPS 186-2 + change notice 1 */
63 os_memcpy(xkey, seed, seed_len);
71 for (j = 0; j < m; j++) {
73 for (i = 0; i < 2; i++) {
74 /* XVAL = (XKEY + XSEED_j) mod 2^b */
76 /* w_i = G(t, XVAL) */
78 sha1_transform(_t, xkey);
79 WPA_PUT_BE32(xpos, _t[0]);
80 WPA_PUT_BE32(xpos + 4, _t[1]);
81 WPA_PUT_BE32(xpos + 8, _t[2]);
82 WPA_PUT_BE32(xpos + 12, _t[3]);
83 WPA_PUT_BE32(xpos + 16, _t[4]);
85 /* XKEY = (1 + XKEY + w_i) mod 2^b */
87 for (k = 19; k >= 0; k--) {
88 carry += xkey[k] + xpos[k];
89 xkey[k] = carry & 0xff;