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 static void sha1_transform(u32 *state, const u8 data[64]) 17 { 18 SHA_CTX context; 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]; 32 #else 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; 44 #endif 45 } 46 47 48 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 u8 *xpos = x; 54 u32 carry; 55 56 if (seed_len < sizeof(xkey)) 57 os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len); 58 else 59 seed_len = sizeof(xkey); 60 61 /* FIPS 186-2 + change notice 1 */ 62 63 os_memcpy(xkey, seed, seed_len); 64 t[0] = 0x67452301; 65 t[1] = 0xEFCDAB89; 66 t[2] = 0x98BADCFE; 67 t[3] = 0x10325476; 68 t[4] = 0xC3D2E1F0; 69 70 m = xlen / 40; 71 for (j = 0; j < m; j++) { 72 /* XSEED_j = 0 */ 73 for (i = 0; i < 2; i++) { 74 /* XVAL = (XKEY + XSEED_j) mod 2^b */ 75 76 /* w_i = G(t, XVAL) */ 77 os_memcpy(_t, t, 20); 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]); 84 85 /* XKEY = (1 + XKEY + w_i) mod 2^b */ 86 carry = 1; 87 for (k = 19; k >= 0; k--) { 88 carry += xkey[k] + xpos[k]; 89 xkey[k] = carry & 0xff; 90 carry >>= 8; 91 } 92 93 xpos += 20; 94 } 95 /* x_j = w_0|w_1 */ 96 } 97 98 return 0; 99 } 100