1e28a4053SRui Paulo /*
2e28a4053SRui Paulo * FIPS 186-2 PRF for libcrypto
3e28a4053SRui Paulo * Copyright (c) 2004-2005, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
7e28a4053SRui Paulo */
8e28a4053SRui Paulo
9e28a4053SRui Paulo #include "includes.h"
10*a90b9d01SCy Schubert #include <openssl/opensslv.h>
11*a90b9d01SCy Schubert
12*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
13*a90b9d01SCy Schubert
14*a90b9d01SCy Schubert /* OpenSSL 3.0 has deprecated the low-level SHA1 functions and does not
15*a90b9d01SCy Schubert * include an upper layer interface that could be used to use the
16*a90b9d01SCy Schubert * SHA1_Transform() function. Use the internal SHA-1 implementation instead
17*a90b9d01SCy Schubert * as a workaround. */
18*a90b9d01SCy Schubert #include "sha1-internal.c"
19*a90b9d01SCy Schubert #include "fips_prf_internal.c"
20*a90b9d01SCy Schubert
21*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
22*a90b9d01SCy Schubert
23e28a4053SRui Paulo #include <openssl/sha.h>
24e28a4053SRui Paulo
25e28a4053SRui Paulo #include "common.h"
26e28a4053SRui Paulo #include "crypto.h"
27e28a4053SRui Paulo
28e28a4053SRui Paulo
sha1_transform(u32 * state,const u8 data[64])29325151a3SRui Paulo static void sha1_transform(u32 *state, const u8 data[64])
30e28a4053SRui Paulo {
31e28a4053SRui Paulo SHA_CTX context;
32e28a4053SRui Paulo os_memset(&context, 0, sizeof(context));
33780fb4a2SCy Schubert #if defined(OPENSSL_IS_BORINGSSL) && !defined(ANDROID)
34780fb4a2SCy Schubert context.h[0] = state[0];
35780fb4a2SCy Schubert context.h[1] = state[1];
36780fb4a2SCy Schubert context.h[2] = state[2];
37780fb4a2SCy Schubert context.h[3] = state[3];
38780fb4a2SCy Schubert context.h[4] = state[4];
39780fb4a2SCy Schubert SHA1_Transform(&context, data);
40780fb4a2SCy Schubert state[0] = context.h[0];
41780fb4a2SCy Schubert state[1] = context.h[1];
42780fb4a2SCy Schubert state[2] = context.h[2];
43780fb4a2SCy Schubert state[3] = context.h[3];
44780fb4a2SCy Schubert state[4] = context.h[4];
45780fb4a2SCy Schubert #else
46325151a3SRui Paulo context.h0 = state[0];
47325151a3SRui Paulo context.h1 = state[1];
48325151a3SRui Paulo context.h2 = state[2];
49325151a3SRui Paulo context.h3 = state[3];
50325151a3SRui Paulo context.h4 = state[4];
51e28a4053SRui Paulo SHA1_Transform(&context, data);
52325151a3SRui Paulo state[0] = context.h0;
53325151a3SRui Paulo state[1] = context.h1;
54325151a3SRui Paulo state[2] = context.h2;
55325151a3SRui Paulo state[3] = context.h3;
56325151a3SRui Paulo state[4] = context.h4;
57780fb4a2SCy Schubert #endif
58e28a4053SRui Paulo }
59e28a4053SRui Paulo
60e28a4053SRui Paulo
fips186_2_prf(const u8 * seed,size_t seed_len,u8 * x,size_t xlen)61e28a4053SRui Paulo int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
62e28a4053SRui Paulo {
63e28a4053SRui Paulo u8 xkey[64];
64e28a4053SRui Paulo u32 t[5], _t[5];
65e28a4053SRui Paulo int i, j, m, k;
66e28a4053SRui Paulo u8 *xpos = x;
67e28a4053SRui Paulo u32 carry;
68e28a4053SRui Paulo
69f05cddf9SRui Paulo if (seed_len < sizeof(xkey))
70f05cddf9SRui Paulo os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len);
71f05cddf9SRui Paulo else
72e28a4053SRui Paulo seed_len = sizeof(xkey);
73e28a4053SRui Paulo
74e28a4053SRui Paulo /* FIPS 186-2 + change notice 1 */
75e28a4053SRui Paulo
76e28a4053SRui Paulo os_memcpy(xkey, seed, seed_len);
77e28a4053SRui Paulo t[0] = 0x67452301;
78e28a4053SRui Paulo t[1] = 0xEFCDAB89;
79e28a4053SRui Paulo t[2] = 0x98BADCFE;
80e28a4053SRui Paulo t[3] = 0x10325476;
81e28a4053SRui Paulo t[4] = 0xC3D2E1F0;
82e28a4053SRui Paulo
83e28a4053SRui Paulo m = xlen / 40;
84e28a4053SRui Paulo for (j = 0; j < m; j++) {
85e28a4053SRui Paulo /* XSEED_j = 0 */
86e28a4053SRui Paulo for (i = 0; i < 2; i++) {
87e28a4053SRui Paulo /* XVAL = (XKEY + XSEED_j) mod 2^b */
88e28a4053SRui Paulo
89e28a4053SRui Paulo /* w_i = G(t, XVAL) */
90e28a4053SRui Paulo os_memcpy(_t, t, 20);
91325151a3SRui Paulo sha1_transform(_t, xkey);
92780fb4a2SCy Schubert WPA_PUT_BE32(xpos, _t[0]);
93780fb4a2SCy Schubert WPA_PUT_BE32(xpos + 4, _t[1]);
94780fb4a2SCy Schubert WPA_PUT_BE32(xpos + 8, _t[2]);
95780fb4a2SCy Schubert WPA_PUT_BE32(xpos + 12, _t[3]);
96780fb4a2SCy Schubert WPA_PUT_BE32(xpos + 16, _t[4]);
97e28a4053SRui Paulo
98e28a4053SRui Paulo /* XKEY = (1 + XKEY + w_i) mod 2^b */
99e28a4053SRui Paulo carry = 1;
100e28a4053SRui Paulo for (k = 19; k >= 0; k--) {
101e28a4053SRui Paulo carry += xkey[k] + xpos[k];
102e28a4053SRui Paulo xkey[k] = carry & 0xff;
103e28a4053SRui Paulo carry >>= 8;
104e28a4053SRui Paulo }
105e28a4053SRui Paulo
106e28a4053SRui Paulo xpos += 20;
107e28a4053SRui Paulo }
108e28a4053SRui Paulo /* x_j = w_0|w_1 */
109e28a4053SRui Paulo }
110e28a4053SRui Paulo
111e28a4053SRui Paulo return 0;
112e28a4053SRui Paulo }
113*a90b9d01SCy Schubert
114*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
115