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>
46 #include <freeradius-devel/autoconf.h>
52 #ifdef HAVE_SYS_TYPES_H
53 #include <sys/types.h>
60 #ifdef HAVE_INTTYPES_H
64 #include <freeradius-devel/sha1.h>
67 * we do it in 8-bit chunks, because we have to keep the numbers
68 * in network byte order (i.e. MSB)
70 * make it a structure so that we can do structure assignments.
72 typedef struct onesixty {
76 static void onesixty_add_mod(onesixty *sum, onesixty *a, onesixty *b)
82 for(i=19; i>=0; i--) {
83 /* for(i=0; i<20; i++) { */
84 s = a->p[i] + b->p[i] + carry;
91 * run the FIPS-186-2 PRF on the given Master Key (160 bits)
92 * in order to derive 1280 bits (160 bytes) of keying data from
95 * Given that in EAP-SIM, this is coming from a 64-bit Kc it seems
96 * like an awful lot of "randomness" to pull out.. (MCR)
99 void fips186_2prf(uint8_t mk[20], uint8_t finalkey[160])
103 onesixty xval, xkey, w_0, w_1, sum, one;
110 * Step 3: For j = 0 to 3 do
112 * b. w_0 = SHA1(XVAL)
113 * c. XKEY = (1 + XKEY + w_0) mod 2^160
115 * e. w_1 = SHA1(XVAL)
116 * f. XKEY = (1 + XKEY + w_1) mod 2^160
120 memcpy(&xkey, mk, sizeof(xkey));
122 /* make the value 1 */
123 memset(&one, 0, sizeof(one));
132 /* b. w_0 = SHA1(XVAL) */
135 memset(zeros, 0, sizeof(zeros));
136 memcpy(zeros, xval.p, 20);
137 SHA1Transform(context.state, zeros);
138 SHA1FinalNoLen(w_0.p, &context);
140 /* c. XKEY = (1 + XKEY + w_0) mod 2^160 */
141 onesixty_add_mod(&sum, &xkey, &w_0);
142 onesixty_add_mod(&xkey, &sum, &one);
147 /* e. w_1 = SHA1(XVAL) */
150 memset(zeros, 0, sizeof(zeros));
151 memcpy(zeros, xval.p, 20);
152 SHA1Transform(context.state, zeros);
153 SHA1FinalNoLen(w_1.p, &context);
155 /* f. XKEY = (1 + XKEY + w_1) mod 2^160 */
156 onesixty_add_mod(&sum, &xkey, &w_1);
157 onesixty_add_mod(&xkey, &sum, &one);
159 /* now store it away */
170 * from http://csrc.nist.gov/CryptoToolkit/dss/Examples-1024bit.pdf
174 * XKEY= bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6
175 * XSEED= 00000000 00000000 00000000 00000000 00000000
178 * The first loop through step 3.2 provides:
180 * XVAL= bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6
182 * Using the routine in Appendix 3.3, Constructing The Function G From SHA-1,
183 * in step 3.2.b of the Change Notice algorithm for computing values of x
186 * w[0]= 2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614
189 * The following value is the updated XKEY value from step 3.2.c:
191 * XKEY= dd734ee0 bd0bcd3b adbaeb27 dd1eaa59 76803ecb
193 * The second loop through step 3.2 provides:
195 * XVAL= dd734ee0 bd0bcd3b adbaeb27 dd1eaa59 76803ecb
197 * Using the routine in Appendix 3.3, Constructing The Function G From SHA-1,
198 * in step 3.2.b of the Change Notice algorithm for computing values of x
201 * w[1]= 3c6c18ba cb0f6c55 babb1378 8e20d737 a3275116
203 * The following value is the updated XKEY value from step 3.2.c:
206 * XKEY= 19df679b 881b3991 6875fea0 6b3f8191 19a78fe2
208 * Step 3.3 provides the following values:
210 * w[0] || w[1]= 2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614
211 * 3c6c18ba cb0f6c55 babb1378 8e20d737 a3275116
219 uint8_t mk[20]={ 0xbd, 0x02, 0x9b, 0xbe, 0x7f, 0x51, 0x96, 0x0b,
220 0xcf, 0x9e, 0xdb, 0x2b, 0x61, 0xf0, 0x6f, 0x0f,
221 0xeb, 0x5a, 0x38, 0xb6 };
223 main(int argc, char *argv[])
225 uint8_t finalkey[160];
228 fips186_2prf(mk, finalkey);
230 printf("Input was: |");
232 for (i = 0; i < 20; i++) {
239 printf("%02x", mk[i]);
242 printf("|\nOutput was: ");
244 for (i = 0; i < 160; i++) {
257 printf("%02x", finalkey[i]);