fips_prf_internal.c revision 1f69aa52ea2e0a73ac502565df8c666ee49cab6a
1/*
2 * FIPS 186-2 PRF for internal crypto implementation
3 * Copyright (c) 2006-2007, Jouni Malinen <j@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
23int 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		os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len);
33	else
34		seed_len = sizeof(xkey);
35
36	/* FIPS 186-2 + change notice 1 */
37
38	os_memcpy(xkey, seed, seed_len);
39	t[0] = 0x67452301;
40	t[1] = 0xEFCDAB89;
41	t[2] = 0x98BADCFE;
42	t[3] = 0x10325476;
43	t[4] = 0xC3D2E1F0;
44
45	m = xlen / 40;
46	for (j = 0; j < m; j++) {
47		/* XSEED_j = 0 */
48		for (i = 0; i < 2; i++) {
49			/* XVAL = (XKEY + XSEED_j) mod 2^b */
50
51			/* w_i = G(t, XVAL) */
52			os_memcpy(_t, t, 20);
53			SHA1Transform(_t, xkey);
54			_t[0] = host_to_be32(_t[0]);
55			_t[1] = host_to_be32(_t[1]);
56			_t[2] = host_to_be32(_t[2]);
57			_t[3] = host_to_be32(_t[3]);
58			_t[4] = host_to_be32(_t[4]);
59			os_memcpy(xpos, _t, 20);
60
61			/* XKEY = (1 + XKEY + w_i) mod 2^b */
62			carry = 1;
63			for (k = 19; k >= 0; k--) {
64				carry += xkey[k] + xpos[k];
65				xkey[k] = carry & 0xff;
66				carry >>= 8;
67			}
68
69			xpos += SHA1_MAC_LEN;
70		}
71		/* x_j = w_0|w_1 */
72	}
73
74	return 0;
75}
76