a5a7e58d5ef76624704a2ffd431507bc4bc9bb3f
[freeradius.git] / src / modules / rlm_eap / libeap / fips186prf.c
1 /*
2  * fips186prf.c    An implementation of the FIPS-186-2 SHA1-based PRF.
3  *
4  * The development of the EAP/SIM support was funded by Internet Foundation
5  * Austria (http://www.nic.at/ipa).
6  *
7  * This code was written from scratch by Michael Richardson, and it is
8  * dual licensed under both GPL and BSD.
9  *
10  * Version:     $Id$
11  *
12  * GPL notice:
13  *
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.
18  *
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.
23  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  *
28  * BSD notice:
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
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.
41  *
42  * Copyright 2003  Michael Richardson <mcr@sandelman.ottawa.on.ca>
43  *
44  */
45
46 #include "autoconf.h"
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #ifdef HAVE_SYS_TYPES_H
53 #include <sys/types.h>
54 #endif
55
56 #ifdef HAVE_STDINT_H
57 #include <stdint.h>
58 #endif
59
60 #ifdef HAVE_INTTYPES_H
61 #include <inttypes.h>
62 #endif
63
64 #include <rad_assert.h>
65
66 #include "sha1.h"
67
68 /*
69  * we do it in 8-bit chunks, because we have to keep the numbers
70  * in network byte order (i.e. MSB)
71  *
72  * make it a structure so that we can do structure assignments.
73  */
74 typedef struct onesixty {
75         uint8_t p[20];
76 } onesixty;
77
78 static void onesixty_add_mod(onesixty *sum, onesixty *a, onesixty *b)
79 {
80         uint32_t s;
81         int i, carry;
82
83         carry = 0;
84         for(i=19; i>=0; i--) {
85 /*      for(i=0; i<20; i++) {  */
86                 s = a->p[i] + b->p[i] + carry;
87                 sum->p[i] = s & 0xff;
88                 carry = s >> 8;
89         }
90 }
91
92 /*
93  * run the FIPS-186-2 PRF on the given Master Key (160 bits)
94  * in order to derive 1280 bits (160 bytes) of keying data from
95  * it.
96  *
97  * Given that in EAP-SIM, this is coming from a 64-bit Kc it seems
98  * like an awful lot of "randomness" to pull out.. (MCR)
99  *
100  */
101 void fips186_2prf(uint8_t mk[20], uint8_t finalkey[160])
102 {
103         SHA1_CTX context;
104         int j;
105         onesixty xval, xkey, w_0, w_1, sum, one;
106         uint8_t *f;
107         char zeros[64];
108
109         /*
110          * let XKEY := MK,
111          *
112          * Step 3: For j = 0 to 3 do
113          *   a. XVAL = XKEY
114          *   b. w_0 = SHA1(XVAL)
115          *   c. XKEY = (1 + XKEY + w_0) mod 2^160
116          *   d. XVAL = XKEY
117          *   e. w_1 = SHA1(XVAL)
118          *   f. XKEY = (1 + XKEY + w_1) mod 2^160
119          * 3.3 x_j = w_0|w_1
120          *
121          */
122         memcpy(&xkey, mk, sizeof(xkey));
123
124         /* make the value 1 */
125         memset(&one,  0, sizeof(one));
126         one.p[19]=1;
127
128         f=finalkey;
129
130         for(j=0; j<4; j++) {
131                 /*   a. XVAL = XKEY  */
132                 xval = xkey;
133
134                 /*   b. w_0 = SHA1(XVAL)  */
135                 SHA1Init(&context);
136
137                 memset(zeros, 0, sizeof(zeros));
138                 memcpy(zeros, xval.p, 20);
139                 SHA1Transform(context.state, zeros);
140                 SHA1FinalNoLen(w_0.p, &context);
141
142                 /*   c. XKEY = (1 + XKEY + w_0) mod 2^160 */
143                 onesixty_add_mod(&sum,  &xkey, &w_0);
144                 onesixty_add_mod(&xkey, &sum,  &one);
145
146                 /*   d. XVAL = XKEY  */
147                 xval = xkey;
148
149                 /*   e. w_1 = SHA1(XVAL)  */
150                 SHA1Init(&context);
151
152                 memset(zeros, 0, sizeof(zeros));
153                 memcpy(zeros, xval.p, 20);
154                 SHA1Transform(context.state, zeros);
155                 SHA1FinalNoLen(w_1.p, &context);
156
157                 /*   f. XKEY = (1 + XKEY + w_1) mod 2^160 */
158                 onesixty_add_mod(&sum,  &xkey, &w_1);
159                 onesixty_add_mod(&xkey, &sum,  &one);
160
161                 /* now store it away */
162                 memcpy(f, &w_0, 20);
163                 f += 20;
164
165                 memcpy(f, &w_1, 20);
166                 f += 20;
167         }
168 }
169
170 /*
171  * test vectors
172  * from http://csrc.nist.gov/CryptoToolkit/dss/Examples-1024bit.pdf
173  *
174  * page 5
175  *
176  * XKEY=     bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6
177  * XSEED=    00000000 00000000 00000000 00000000 00000000
178  *
179  *
180  * The first loop through step 3.2 provides:
181  *
182  * XVAL=     bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6
183  *
184  * Using the routine in Appendix 3.3, Constructing The Function G From SHA-1,
185  * in step 3.2.b of the Change Notice algorithm for computing values of x
186  * provides:
187  *
188  * w[0]=     2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614
189  *
190  *
191  * The following value is the updated XKEY value from step 3.2.c:
192  *
193  * XKEY=     dd734ee0 bd0bcd3b adbaeb27 dd1eaa59 76803ecb
194  *
195  * The second loop through step 3.2 provides:
196  *
197  * XVAL=     dd734ee0 bd0bcd3b adbaeb27 dd1eaa59 76803ecb
198  *
199  * Using the routine in Appendix 3.3, Constructing The Function G From SHA-1,
200  * in step 3.2.b of the Change Notice algorithm for computing values of x
201  * provides:
202  *
203  * w[1]=     3c6c18ba cb0f6c55 babb1378 8e20d737 a3275116
204  *
205  * The following value is the updated XKEY value from step 3.2.c:
206  *
207  *
208  * XKEY=     19df679b 881b3991 6875fea0 6b3f8191 19a78fe2
209  *
210  * Step 3.3 provides the following values:
211  *
212  * w[0] || w[1]=  2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614
213  *                3c6c18ba cb0f6c55 babb1378 8e20d737 a3275116
214  *
215  */
216
217 #ifdef TEST_CASE
218
219 #include <assert.h>
220
221 uint8_t mk[20]={ 0xbd, 0x02, 0x9b, 0xbe, 0x7f, 0x51, 0x96, 0x0b,
222                   0xcf, 0x9e, 0xdb, 0x2b, 0x61, 0xf0, 0x6f, 0x0f,
223                   0xeb, 0x5a, 0x38, 0xb6 };
224
225 main(int argc, char *argv[])
226 {
227         uint8_t finalkey[160];
228         int i, j, k;
229
230         fips186_2prf(mk, finalkey);
231
232         printf("Input was: |");
233         j=0;
234         for (i = 0; i < 20; i++) {
235                 if(j==4) {
236                         printf("_");
237                         j=0;
238                 }
239                 j++;
240
241                 printf("%02x", mk[i]);
242         }
243
244         printf("|\nOutput was: ");
245         j=0; k=0;
246         for (i = 0; i < 160; i++) {
247                 if(k==20) {
248                         printf("\n            ");
249                         k=0;
250                         j=0;
251                 }
252                 if(j==4) {
253                         printf("_");
254                         j=0;
255                 }
256                 k++;
257                 j++;
258
259                 printf("%02x", finalkey[i]);
260         }
261         printf("\n");
262 }
263 #endif
264
265
266
267 /*
268  * $Log$
269  * Revision 1.3  2004-02-26 19:04:30  aland
270  *      perl -i -npe "s/[ \t]+$//g" `find src -name "*.[ch]" -print`
271  *
272  *      Whitespace changes only, from a fresh checkout.
273  *
274  *      For bug # 13
275  *
276  * Revision 1.2  2003/11/06 15:37:24  aland
277  *      Update includes to work a little better
278  *
279  * Revision 1.1  2003/10/29 02:49:19  mcr
280  *      initial commit of eap-sim
281  *
282  *
283  * Local Variables:
284  * c-style: bsd
285  * End:
286  */