2 * fips186prf.c An implementation of the FIPS-186-2 SHA1-based PRF.
4 * The development of the EAP/SIM support was funded by Internet Foundation
5 * Austria (http://www.nic.at/ipa).
7 * This code was written from scratch by Michael Richardson, and it is
8 * dual licensed under both GPL and BSD.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. Neither the name of The NetBSD Foundation nor the names of its
39 * contributors may be used to endorse or promote products derived
40 * from this software without specific prior written permission.
42 * Copyright 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
43 * Copyright 2006 The FreeRADIUS server project
47 #include <freeradius-devel/ident.h>
50 #include <freeradius-devel/autoconf.h>
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
64 #ifdef HAVE_INTTYPES_H
68 #include <freeradius-devel/sha1.h>
71 * we do it in 8-bit chunks, because we have to keep the numbers
72 * in network byte order (i.e. MSB)
74 * make it a structure so that we can do structure assignments.
76 typedef struct onesixty {
80 static void onesixty_add_mod(onesixty *sum, onesixty *a, onesixty *b)
86 for(i=19; i>=0; i--) {
87 /* for(i=0; i<20; i++) { */
88 s = a->p[i] + b->p[i] + carry;
95 * run the FIPS-186-2 PRF on the given Master Key (160 bits)
96 * in order to derive 1280 bits (160 bytes) of keying data from
99 * Given that in EAP-SIM, this is coming from a 64-bit Kc it seems
100 * like an awful lot of "randomness" to pull out.. (MCR)
103 void fips186_2prf(uint8_t mk[20], uint8_t finalkey[160])
107 onesixty xval, xkey, w_0, w_1, sum, one;
114 * Step 3: For j = 0 to 3 do
116 * b. w_0 = SHA1(XVAL)
117 * c. XKEY = (1 + XKEY + w_0) mod 2^160
119 * e. w_1 = SHA1(XVAL)
120 * f. XKEY = (1 + XKEY + w_1) mod 2^160
124 memcpy(&xkey, mk, sizeof(xkey));
126 /* make the value 1 */
127 memset(&one, 0, sizeof(one));
136 /* b. w_0 = SHA1(XVAL) */
137 fr_SHA1Init(&context);
139 memset(zeros, 0, sizeof(zeros));
140 memcpy(zeros, xval.p, 20);
141 fr_SHA1Transform(context.state, zeros);
142 fr_fr_SHA1FinalNoLen(w_0.p, &context);
144 /* c. XKEY = (1 + XKEY + w_0) mod 2^160 */
145 onesixty_add_mod(&sum, &xkey, &w_0);
146 onesixty_add_mod(&xkey, &sum, &one);
151 /* e. w_1 = SHA1(XVAL) */
152 fr_SHA1Init(&context);
154 memset(zeros, 0, sizeof(zeros));
155 memcpy(zeros, xval.p, 20);
156 fr_SHA1Transform(context.state, zeros);
157 fr_fr_SHA1FinalNoLen(w_1.p, &context);
159 /* f. XKEY = (1 + XKEY + w_1) mod 2^160 */
160 onesixty_add_mod(&sum, &xkey, &w_1);
161 onesixty_add_mod(&xkey, &sum, &one);
163 /* now store it away */
174 * from http://csrc.nist.gov/CryptoToolkit/dss/Examples-1024bit.pdf
178 * XKEY= bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6
179 * XSEED= 00000000 00000000 00000000 00000000 00000000
182 * The first loop through step 3.2 provides:
184 * XVAL= bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6
186 * Using the routine in Appendix 3.3, Constructing The Function G From SHA-1,
187 * in step 3.2.b of the Change Notice algorithm for computing values of x
190 * w[0]= 2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614
193 * The following value is the updated XKEY value from step 3.2.c:
195 * XKEY= dd734ee0 bd0bcd3b adbaeb27 dd1eaa59 76803ecb
197 * The second loop through step 3.2 provides:
199 * XVAL= dd734ee0 bd0bcd3b adbaeb27 dd1eaa59 76803ecb
201 * Using the routine in Appendix 3.3, Constructing The Function G From SHA-1,
202 * in step 3.2.b of the Change Notice algorithm for computing values of x
205 * w[1]= 3c6c18ba cb0f6c55 babb1378 8e20d737 a3275116
207 * The following value is the updated XKEY value from step 3.2.c:
210 * XKEY= 19df679b 881b3991 6875fea0 6b3f8191 19a78fe2
212 * Step 3.3 provides the following values:
214 * w[0] || w[1]= 2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614
215 * 3c6c18ba cb0f6c55 babb1378 8e20d737 a3275116
223 uint8_t mk[20]={ 0xbd, 0x02, 0x9b, 0xbe, 0x7f, 0x51, 0x96, 0x0b,
224 0xcf, 0x9e, 0xdb, 0x2b, 0x61, 0xf0, 0x6f, 0x0f,
225 0xeb, 0x5a, 0x38, 0xb6 };
227 main(int argc, char *argv[])
229 uint8_t finalkey[160];
232 fips186_2prf(mk, finalkey);
234 printf("Input was: |");
236 for (i = 0; i < 20; i++) {
243 printf("%02x", mk[i]);
246 printf("|\nOutput was: ");
248 for (i = 0; i < 160; i++) {
261 printf("%02x", finalkey[i]);