$search
00001 /* 00002 * FIPS 186-2 PRF for libcrypto 00003 * Copyright (c) 2004-2005, 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 #include <openssl/sha.h> 00017 00018 #include "common.h" 00019 #include "crypto.h" 00020 00021 00022 static void sha1_transform(u8 *state, const u8 data[64]) 00023 { 00024 SHA_CTX context; 00025 os_memset(&context, 0, sizeof(context)); 00026 os_memcpy(&context.h0, state, 5 * 4); 00027 SHA1_Transform(&context, data); 00028 os_memcpy(state, &context.h0, 5 * 4); 00029 } 00030 00031 00032 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen) 00033 { 00034 u8 xkey[64]; 00035 u32 t[5], _t[5]; 00036 int i, j, m, k; 00037 u8 *xpos = x; 00038 u32 carry; 00039 00040 if (seed_len > sizeof(xkey)) 00041 seed_len = sizeof(xkey); 00042 00043 /* FIPS 186-2 + change notice 1 */ 00044 00045 os_memcpy(xkey, seed, seed_len); 00046 os_memset(xkey + seed_len, 0, 64 - seed_len); 00047 t[0] = 0x67452301; 00048 t[1] = 0xEFCDAB89; 00049 t[2] = 0x98BADCFE; 00050 t[3] = 0x10325476; 00051 t[4] = 0xC3D2E1F0; 00052 00053 m = xlen / 40; 00054 for (j = 0; j < m; j++) { 00055 /* XSEED_j = 0 */ 00056 for (i = 0; i < 2; i++) { 00057 /* XVAL = (XKEY + XSEED_j) mod 2^b */ 00058 00059 /* w_i = G(t, XVAL) */ 00060 os_memcpy(_t, t, 20); 00061 sha1_transform((u8 *) _t, xkey); 00062 _t[0] = host_to_be32(_t[0]); 00063 _t[1] = host_to_be32(_t[1]); 00064 _t[2] = host_to_be32(_t[2]); 00065 _t[3] = host_to_be32(_t[3]); 00066 _t[4] = host_to_be32(_t[4]); 00067 os_memcpy(xpos, _t, 20); 00068 00069 /* XKEY = (1 + XKEY + w_i) mod 2^b */ 00070 carry = 1; 00071 for (k = 19; k >= 0; k--) { 00072 carry += xkey[k] + xpos[k]; 00073 xkey[k] = carry & 0xff; 00074 carry >>= 8; 00075 } 00076 00077 xpos += 20; 00078 } 00079 /* x_j = w_0|w_1 */ 00080 } 00081 00082 return 0; 00083 }