More search and replace changes to clean up the code.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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  * Copyright 2006  The FreeRADIUS server project
44  *
45  */
46
47 #include <freeradius-devel/ident.h>
48 RCSID("$Id$")
49
50 #include <freeradius-devel/autoconf.h>
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif
59
60 #ifdef HAVE_STDINT_H
61 #include <stdint.h>
62 #endif
63
64 #ifdef HAVE_INTTYPES_H
65 #include <inttypes.h>
66 #endif
67
68 #include <freeradius-devel/sha1.h>
69
70 /*
71  * we do it in 8-bit chunks, because we have to keep the numbers
72  * in network byte order (i.e. MSB)
73  *
74  * make it a structure so that we can do structure assignments.
75  */
76 typedef struct onesixty {
77         uint8_t p[20];
78 } onesixty;
79
80 static void onesixty_add_mod(onesixty *sum, onesixty *a, onesixty *b)
81 {
82         uint32_t s;
83         int i, carry;
84
85         carry = 0;
86         for(i=19; i>=0; i--) {
87 /*      for(i=0; i<20; i++) {  */
88                 s = a->p[i] + b->p[i] + carry;
89                 sum->p[i] = s & 0xff;
90                 carry = s >> 8;
91         }
92 }
93
94 /*
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
97  * it.
98  *
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)
101  *
102  */
103 void fips186_2prf(uint8_t mk[20], uint8_t finalkey[160])
104 {
105         fr_SHA1_CTX context;
106         int j;
107         onesixty xval, xkey, w_0, w_1, sum, one;
108         uint8_t *f;
109         uint8_t zeros[64];
110
111         /*
112          * let XKEY := MK,
113          *
114          * Step 3: For j = 0 to 3 do
115          *   a. XVAL = XKEY
116          *   b. w_0 = SHA1(XVAL)
117          *   c. XKEY = (1 + XKEY + w_0) mod 2^160
118          *   d. XVAL = XKEY
119          *   e. w_1 = SHA1(XVAL)
120          *   f. XKEY = (1 + XKEY + w_1) mod 2^160
121          * 3.3 x_j = w_0|w_1
122          *
123          */
124         memcpy(&xkey, mk, sizeof(xkey));
125
126         /* make the value 1 */
127         memset(&one,  0, sizeof(one));
128         one.p[19]=1;
129
130         f=finalkey;
131
132         for(j=0; j<4; j++) {
133                 /*   a. XVAL = XKEY  */
134                 xval = xkey;
135
136                 /*   b. w_0 = SHA1(XVAL)  */
137                 fr_SHA1Init(&context);
138
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);
143
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);
147
148                 /*   d. XVAL = XKEY  */
149                 xval = xkey;
150
151                 /*   e. w_1 = SHA1(XVAL)  */
152                 fr_SHA1Init(&context);
153
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);
158
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);
162
163                 /* now store it away */
164                 memcpy(f, &w_0, 20);
165                 f += 20;
166
167                 memcpy(f, &w_1, 20);
168                 f += 20;
169         }
170 }
171
172 /*
173  * test vectors
174  * from http://csrc.nist.gov/CryptoToolkit/dss/Examples-1024bit.pdf
175  *
176  * page 5
177  *
178  * XKEY=     bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6
179  * XSEED=    00000000 00000000 00000000 00000000 00000000
180  *
181  *
182  * The first loop through step 3.2 provides:
183  *
184  * XVAL=     bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6
185  *
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
188  * provides:
189  *
190  * w[0]=     2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614
191  *
192  *
193  * The following value is the updated XKEY value from step 3.2.c:
194  *
195  * XKEY=     dd734ee0 bd0bcd3b adbaeb27 dd1eaa59 76803ecb
196  *
197  * The second loop through step 3.2 provides:
198  *
199  * XVAL=     dd734ee0 bd0bcd3b adbaeb27 dd1eaa59 76803ecb
200  *
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
203  * provides:
204  *
205  * w[1]=     3c6c18ba cb0f6c55 babb1378 8e20d737 a3275116
206  *
207  * The following value is the updated XKEY value from step 3.2.c:
208  *
209  *
210  * XKEY=     19df679b 881b3991 6875fea0 6b3f8191 19a78fe2
211  *
212  * Step 3.3 provides the following values:
213  *
214  * w[0] || w[1]=  2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614
215  *                3c6c18ba cb0f6c55 babb1378 8e20d737 a3275116
216  *
217  */
218
219 #ifdef TEST_CASE
220
221 #include <assert.h>
222
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 };
226
227 main(int argc, char *argv[])
228 {
229         uint8_t finalkey[160];
230         int i, j, k;
231
232         fips186_2prf(mk, finalkey);
233
234         printf("Input was: |");
235         j=0;
236         for (i = 0; i < 20; i++) {
237                 if(j==4) {
238                         printf("_");
239                         j=0;
240                 }
241                 j++;
242
243                 printf("%02x", mk[i]);
244         }
245
246         printf("|\nOutput was: ");
247         j=0; k=0;
248         for (i = 0; i < 160; i++) {
249                 if(k==20) {
250                         printf("\n            ");
251                         k=0;
252                         j=0;
253                 }
254                 if(j==4) {
255                         printf("_");
256                         j=0;
257                 }
258                 k++;
259                 j++;
260
261                 printf("%02x", finalkey[i]);
262         }
263         printf("\n");
264 }
265 #endif