1 /* 2 * FIPS 186-2 PRF for internal crypto implementation 3 * Copyright (c) 2006-2007, 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 11 #include "common.h" 12 #include "sha1.h" 13 #include "sha1_i.h" 14 #include "crypto.h" 15 16 17 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen) 18 { 19 u8 xkey[64]; 20 u32 _t[5]; 21 int i, j, m, k; 22 u8 *xpos = x; 23 u32 carry; 24 struct SHA1Context ctx; 25 26 if (seed_len < sizeof(xkey)) 27 os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len); 28 else 29 seed_len = sizeof(xkey); 30 31 /* FIPS 186-2 + change notice 1 */ 32 33 os_memcpy(xkey, seed, seed_len); 34 SHA1Init(&ctx); 35 36 m = xlen / 40; 37 for (j = 0; j < m; j++) { 38 /* XSEED_j = 0 */ 39 for (i = 0; i < 2; i++) { 40 /* XVAL = (XKEY + XSEED_j) mod 2^b */ 41 42 /* w_i = G(t, XVAL) */ 43 os_memcpy(_t, ctx.state, 20); 44 SHA1Transform(_t, xkey); 45 _t[0] = host_to_be32(_t[0]); 46 _t[1] = host_to_be32(_t[1]); 47 _t[2] = host_to_be32(_t[2]); 48 _t[3] = host_to_be32(_t[3]); 49 _t[4] = host_to_be32(_t[4]); 50 os_memcpy(xpos, _t, 20); 51 52 /* XKEY = (1 + XKEY + w_i) mod 2^b */ 53 carry = 1; 54 for (k = 19; k >= 0; k--) { 55 carry += xkey[k] + xpos[k]; 56 xkey[k] = carry & 0xff; 57 carry >>= 8; 58 } 59 60 xpos += SHA1_MAC_LEN; 61 } 62 /* x_j = w_0|w_1 */ 63 } 64 65 return 0; 66 } 67