Line data Source code
1 : /*
2 : * FIPS 186-2 PRF for libcrypto
3 : * Copyright (c) 2004-2005, 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/sha.h>
11 :
12 : #include "common.h"
13 : #include "crypto.h"
14 :
15 :
16 2178 : static void sha1_transform(u32 *state, const u8 data[64])
17 : {
18 : SHA_CTX context;
19 2178 : 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];
32 : #else
33 2178 : context.h0 = state[0];
34 2178 : context.h1 = state[1];
35 2178 : context.h2 = state[2];
36 2178 : context.h3 = state[3];
37 2178 : context.h4 = state[4];
38 2178 : SHA1_Transform(&context, data);
39 2178 : state[0] = context.h0;
40 2178 : state[1] = context.h1;
41 2178 : state[2] = context.h2;
42 2178 : state[3] = context.h3;
43 2178 : state[4] = context.h4;
44 : #endif
45 2178 : }
46 :
47 :
48 273 : int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
49 : {
50 : u8 xkey[64];
51 : u32 t[5], _t[5];
52 : int i, j, m, k;
53 273 : u8 *xpos = x;
54 : u32 carry;
55 :
56 273 : if (seed_len < sizeof(xkey))
57 273 : os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len);
58 : else
59 0 : seed_len = sizeof(xkey);
60 :
61 : /* FIPS 186-2 + change notice 1 */
62 :
63 273 : os_memcpy(xkey, seed, seed_len);
64 273 : t[0] = 0x67452301;
65 273 : t[1] = 0xEFCDAB89;
66 273 : t[2] = 0x98BADCFE;
67 273 : t[3] = 0x10325476;
68 273 : t[4] = 0xC3D2E1F0;
69 :
70 273 : m = xlen / 40;
71 1362 : for (j = 0; j < m; j++) {
72 : /* XSEED_j = 0 */
73 3267 : for (i = 0; i < 2; i++) {
74 : /* XVAL = (XKEY + XSEED_j) mod 2^b */
75 :
76 : /* w_i = G(t, XVAL) */
77 2178 : os_memcpy(_t, t, 20);
78 2178 : sha1_transform(_t, xkey);
79 2178 : WPA_PUT_BE32(xpos, _t[0]);
80 2178 : WPA_PUT_BE32(xpos + 4, _t[1]);
81 2178 : WPA_PUT_BE32(xpos + 8, _t[2]);
82 2178 : WPA_PUT_BE32(xpos + 12, _t[3]);
83 2178 : WPA_PUT_BE32(xpos + 16, _t[4]);
84 :
85 : /* XKEY = (1 + XKEY + w_i) mod 2^b */
86 2178 : carry = 1;
87 45738 : for (k = 19; k >= 0; k--) {
88 43560 : carry += xkey[k] + xpos[k];
89 43560 : xkey[k] = carry & 0xff;
90 43560 : carry >>= 8;
91 : }
92 :
93 2178 : xpos += 20;
94 : }
95 : /* x_j = w_0|w_1 */
96 : }
97 :
98 273 : return 0;
99 : }
|