$search
00001 /* 00002 * FIPS 186-2 PRF for internal crypto implementation 00003 * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation. 00008 * 00009 * Alternatively, this software may be distributed under the terms of BSD 00010 * license. 00011 * 00012 * See README and COPYING for more details. 00013 */ 00014 00015 #include "includes.h" 00016 00017 #include "common.h" 00018 #include "sha1.h" 00019 #include "sha1_i.h" 00020 #include "crypto.h" 00021 00022 00023 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen) 00024 { 00025 u8 xkey[64]; 00026 u32 t[5], _t[5]; 00027 int i, j, m, k; 00028 u8 *xpos = x; 00029 u32 carry; 00030 00031 if (seed_len > sizeof(xkey)) 00032 seed_len = sizeof(xkey); 00033 00034 /* FIPS 186-2 + change notice 1 */ 00035 00036 os_memcpy(xkey, seed, seed_len); 00037 os_memset(xkey + seed_len, 0, 64 - seed_len); 00038 t[0] = 0x67452301; 00039 t[1] = 0xEFCDAB89; 00040 t[2] = 0x98BADCFE; 00041 t[3] = 0x10325476; 00042 t[4] = 0xC3D2E1F0; 00043 00044 m = xlen / 40; 00045 for (j = 0; j < m; j++) { 00046 /* XSEED_j = 0 */ 00047 for (i = 0; i < 2; i++) { 00048 /* XVAL = (XKEY + XSEED_j) mod 2^b */ 00049 00050 /* w_i = G(t, XVAL) */ 00051 os_memcpy(_t, t, 20); 00052 SHA1Transform(_t, xkey); 00053 _t[0] = host_to_be32(_t[0]); 00054 _t[1] = host_to_be32(_t[1]); 00055 _t[2] = host_to_be32(_t[2]); 00056 _t[3] = host_to_be32(_t[3]); 00057 _t[4] = host_to_be32(_t[4]); 00058 os_memcpy(xpos, _t, 20); 00059 00060 /* XKEY = (1 + XKEY + w_i) mod 2^b */ 00061 carry = 1; 00062 for (k = 19; k >= 0; k--) { 00063 carry += xkey[k] + xpos[k]; 00064 xkey[k] = carry & 0xff; 00065 carry >>= 8; 00066 } 00067 00068 xpos += SHA1_MAC_LEN; 00069 } 00070 /* x_j = w_0|w_1 */ 00071 } 00072 00073 return 0; 00074 }