Home | History | Annotate | Download | only in crypto
      1 /*
      2  * FIPS 186-2 PRF for internal crypto implementation
      3  * Copyright (c) 2006-2007, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This program is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License version 2 as
      7  * published by the Free Software Foundation.
      8  *
      9  * Alternatively, this software may be distributed under the terms of BSD
     10  * license.
     11  *
     12  * See README and COPYING for more details.
     13  */
     14 
     15 #include "includes.h"
     16 
     17 #include "common.h"
     18 #include "sha1.h"
     19 #include "sha1_i.h"
     20 #include "crypto.h"
     21 
     22 
     23 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
     24 {
     25 	u8 xkey[64];
     26 	u32 t[5], _t[5];
     27 	int i, j, m, k;
     28 	u8 *xpos = x;
     29 	u32 carry;
     30 
     31 	if (seed_len > sizeof(xkey))
     32 		seed_len = sizeof(xkey);
     33 
     34 	/* FIPS 186-2 + change notice 1 */
     35 
     36 	os_memcpy(xkey, seed, seed_len);
     37 	os_memset(xkey + seed_len, 0, 64 - seed_len);
     38 	t[0] = 0x67452301;
     39 	t[1] = 0xEFCDAB89;
     40 	t[2] = 0x98BADCFE;
     41 	t[3] = 0x10325476;
     42 	t[4] = 0xC3D2E1F0;
     43 
     44 	m = xlen / 40;
     45 	for (j = 0; j < m; j++) {
     46 		/* XSEED_j = 0 */
     47 		for (i = 0; i < 2; i++) {
     48 			/* XVAL = (XKEY + XSEED_j) mod 2^b */
     49 
     50 			/* w_i = G(t, XVAL) */
     51 			os_memcpy(_t, t, 20);
     52 			SHA1Transform(_t, xkey);
     53 			_t[0] = host_to_be32(_t[0]);
     54 			_t[1] = host_to_be32(_t[1]);
     55 			_t[2] = host_to_be32(_t[2]);
     56 			_t[3] = host_to_be32(_t[3]);
     57 			_t[4] = host_to_be32(_t[4]);
     58 			os_memcpy(xpos, _t, 20);
     59 
     60 			/* XKEY = (1 + XKEY + w_i) mod 2^b */
     61 			carry = 1;
     62 			for (k = 19; k >= 0; k--) {
     63 				carry += xkey[k] + xpos[k];
     64 				xkey[k] = carry & 0xff;
     65 				carry >>= 8;
     66 			}
     67 
     68 			xpos += SHA1_MAC_LEN;
     69 		}
     70 		/* x_j = w_0|w_1 */
     71 	}
     72 
     73 	return 0;
     74 }
     75