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