~ubuntu-branches/ubuntu/jaunty/wpasupplicant/jaunty

« back to all changes in this revision

Viewing changes to sha1.c

  • Committer: Bazaar Package Importer
  • Author(s): Kel Modderman
  • Date: 2006-10-05 08:04:01 UTC
  • mfrom: (1.2.1 upstream) (2.1.14 edgy)
  • Revision ID: james.westby@ubuntu.com-20061005080401-myfwjtq7di70dyeo
* Update madwifi headers to latest SVN. (Closes: #388316)
* Remove failed attempt at action locking. [debian/functions.sh,
  debian/wpa_action.sh]
* Add hysteresis checking functions, to avoid "event loops" while
  using wpa-roam. [debian/functions.sh, debian/wpa_action.sh]
* Change of co-maintainer email address.
* Add ishex() function to functions.sh to determine wpa-psk value type in
  plaintext or hex. This effectively eliminates the need for the bogus and
  somewhat confusing wpa-passphrase contruct specific to our scripts and
  allows wpa-psk to work with either a 8 to 63 character long plaintext
  string or 64 character long hex string.
* Adjust README.modes to not refer to the redundant wpa-passphrase stuff.
* Add big fat NOTE about acceptable wpa-psk's to top of example gallery.
* Strip surrounding quotes from wpa-ssid if present, instead of just whining
  about them.
* Update email address in copyright blurb of functions.sh, ifupdown.sh and
  wpa_action.sh.  

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 * See README and COPYING for more details.
13
13
 */
14
14
 
15
 
#include <stdlib.h>
16
 
#include <stdio.h>
17
 
#include <string.h>
 
15
#include "includes.h"
18
16
 
19
17
#include "common.h"
20
18
#include "sha1.h"
21
19
#include "md5.h"
22
 
 
23
 
 
24
 
void sha1_mac(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
25
 
              u8 *mac)
26
 
{
27
 
        SHA1_CTX context;
28
 
        SHA1Init(&context);
29
 
        SHA1Update(&context, key, key_len);
30
 
        SHA1Update(&context, data, data_len);
31
 
        SHA1Update(&context, key, key_len);
32
 
        SHA1Final(mac, &context);
33
 
}
34
 
 
35
 
 
36
 
/* HMAC code is based on RFC 2104 */
 
20
#include "crypto.h"
 
21
 
 
22
 
 
23
/**
 
24
 * hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104)
 
25
 * @key: Key for HMAC operations
 
26
 * @key_len: Length of the key in bytes
 
27
 * @num_elem: Number of elements in the data vector
 
28
 * @addr: Pointers to the data areas
 
29
 * @len: Lengths of the data blocks
 
30
 * @mac: Buffer for the hash (20 bytes)
 
31
 */
37
32
void hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
38
33
                      const u8 *addr[], const size_t *len, u8 *mac)
39
34
{
40
 
        SHA1_CTX context;
41
 
        unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
42
 
        unsigned char k_opad[65]; /* outer padding - key XORd with opad */
 
35
        unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
43
36
        unsigned char tk[20];
44
 
        int i;
 
37
        const u8 *_addr[6];
 
38
        size_t _len[6], i;
 
39
 
 
40
        if (num_elem > 5) {
 
41
                /*
 
42
                 * Fixed limit on the number of fragments to avoid having to
 
43
                 * allocate memory (which could fail).
 
44
                 */
 
45
                return;
 
46
        }
45
47
 
46
48
        /* if key is longer than 64 bytes reset it to key = SHA1(key) */
47
49
        if (key_len > 64) {
48
 
                SHA1Init(&context);
49
 
                SHA1Update(&context, key, key_len);
50
 
                SHA1Final(tk, &context);
51
 
 
 
50
                sha1_vector(1, &key, &key_len, tk);
52
51
                key = tk;
53
52
                key_len = 20;
54
53
        }
62
61
         * opad is the byte 0x5c repeated 64 times
63
62
         * and text is the data being protected */
64
63
 
65
 
        /* start out by storing key in pads */
66
 
        memset(k_ipad, 0, sizeof(k_ipad));
67
 
        memset(k_opad, 0, sizeof(k_opad));
68
 
        memcpy(k_ipad, key, key_len);
69
 
        memcpy(k_opad, key, key_len);
70
 
 
71
 
        /* XOR key with ipad and opad values */
72
 
        for (i = 0; i < 64; i++) {
73
 
                k_ipad[i] ^= 0x36;
74
 
                k_opad[i] ^= 0x5c;
75
 
        }
 
64
        /* start out by storing key in ipad */
 
65
        memset(k_pad, 0, sizeof(k_pad));
 
66
        memcpy(k_pad, key, key_len);
 
67
        /* XOR key with ipad values */
 
68
        for (i = 0; i < 64; i++)
 
69
                k_pad[i] ^= 0x36;
76
70
 
77
71
        /* perform inner SHA1 */
78
 
        SHA1Init(&context);                   /* init context for 1st pass */
79
 
        SHA1Update(&context, k_ipad, 64);     /* start with inner pad */
80
 
        /* then text of datagram; all fragments */
 
72
        _addr[0] = k_pad;
 
73
        _len[0] = 64;
81
74
        for (i = 0; i < num_elem; i++) {
82
 
                SHA1Update(&context, addr[i], len[i]);
 
75
                _addr[i + 1] = addr[i];
 
76
                _len[i + 1] = len[i];
83
77
        }
84
 
        SHA1Final(mac, &context);             /* finish up 1st pass */
 
78
        sha1_vector(1 + num_elem, _addr, _len, mac);
 
79
 
 
80
        memset(k_pad, 0, sizeof(k_pad));
 
81
        memcpy(k_pad, key, key_len);
 
82
        /* XOR key with opad values */
 
83
        for (i = 0; i < 64; i++)
 
84
                k_pad[i] ^= 0x5c;
85
85
 
86
86
        /* perform outer SHA1 */
87
 
        SHA1Init(&context);                   /* init context for 2nd pass */
88
 
        SHA1Update(&context, k_opad, 64);     /* start with outer pad */
89
 
        SHA1Update(&context, mac, 20);        /* then results of 1st hash */
90
 
        SHA1Final(mac, &context);             /* finish up 2nd pass */
 
87
        _addr[0] = k_pad;
 
88
        _len[0] = 64;
 
89
        _addr[1] = mac;
 
90
        _len[1] = SHA1_MAC_LEN;
 
91
        sha1_vector(2, _addr, _len, mac);
91
92
}
92
93
 
93
94
 
 
95
/**
 
96
 * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
 
97
 * @key: Key for HMAC operations
 
98
 * @key_len: Length of the key in bytes
 
99
 * @data: Pointers to the data area
 
100
 * @data_len: Length of the data area
 
101
 * @mac: Buffer for the hash (20 bytes)
 
102
 */
94
103
void hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
95
104
               u8 *mac)
96
105
{
98
107
}
99
108
 
100
109
 
 
110
/**
 
111
 * sha1_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1)
 
112
 * @key: Key for PRF
 
113
 * @key_len: Length of the key in bytes
 
114
 * @label: A unique label for each purpose of the PRF
 
115
 * @data: Extra data to bind into the key
 
116
 * @data_len: Length of the data
 
117
 * @buf: Buffer for the generated pseudo-random key
 
118
 * @buf_len: Number of bytes of key to generate
 
119
 *
 
120
 * This function is used to derive new, cryptographically separate keys from a
 
121
 * given key (e.g., PMK in IEEE 802.11i).
 
122
 */
101
123
void sha1_prf(const u8 *key, size_t key_len, const char *label,
102
124
              const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
103
125
{
135
157
}
136
158
 
137
159
 
138
 
/* draft-cam-winget-eap-fast-00.txt */
 
160
/**
 
161
 * sha1_t_prf - EAP-FAST Pseudo-Random Function (T-PRF)
 
162
 * @key: Key for PRF
 
163
 * @key_len: Length of the key in bytes
 
164
 * @label: A unique label for each purpose of the PRF
 
165
 * @seed: Seed value to bind into the key
 
166
 * @seed_len: Length of the seed
 
167
 * @buf: Buffer for the generated pseudo-random key
 
168
 * @buf_len: Number of bytes of key to generate
 
169
 *
 
170
 * This function is used to derive new, cryptographically separate keys from a
 
171
 * given key for EAP-FAST. T-PRF is defined in
 
172
 * draft-cam-winget-eap-fast-02.txt, Appendix B.
 
173
 */
139
174
void sha1_t_prf(const u8 *key, size_t key_len, const char *label,
140
175
                const u8 *seed, size_t seed_len, u8 *buf, size_t buf_len)
141
176
{
177
212
}
178
213
 
179
214
 
180
 
/* RFC 2246 */
 
215
/**
 
216
 * tls_prf - Pseudo-Random Function for TLS (TLS-PRF, RFC 2246)
 
217
 * @secret: Key for PRF
 
218
 * @secret_len: Length of the key in bytes
 
219
 * @label: A unique label for each purpose of the PRF
 
220
 * @seed: Seed value to bind into the key
 
221
 * @seed_len: Length of the seed
 
222
 * @out: Buffer for the generated pseudo-random key
 
223
 * @outlen: Number of bytes of key to generate
 
224
 * Returns: 0 on success, -1 on failure.
 
225
 *
 
226
 * This function is used to derive new, cryptographically separate keys from a
 
227
 * given key in TLS. This PRF is defined in RFC 2246, Chapter 5.
 
228
 */
181
229
int tls_prf(const u8 *secret, size_t secret_len, const char *label,
182
230
            const u8 *seed, size_t seed_len, u8 *out, size_t outlen)
183
231
{
184
 
        size_t L_S1, L_S2;
 
232
        size_t L_S1, L_S2, i;
185
233
        const u8 *S1, *S2;
186
234
        u8 A_MD5[MD5_MAC_LEN], A_SHA1[SHA1_MAC_LEN];
187
235
        u8 P_MD5[MD5_MAC_LEN], P_SHA1[SHA1_MAC_LEN];
188
 
        int i, MD5_pos, SHA1_pos;
 
236
        int MD5_pos, SHA1_pos;
189
237
        const u8 *MD5_addr[3];
190
238
        size_t MD5_len[3];
191
239
        const unsigned char *SHA1_addr[3];
285
333
}
286
334
 
287
335
 
 
336
/**
 
337
 * pbkdf2_sha1 - SHA1-based key derivation function (PBKDF2) for IEEE 802.11i
 
338
 * @passphrase: ASCII passphrase
 
339
 * @ssid: SSID
 
340
 * @ssid_len: SSID length in bytes
 
341
 * @interations: Number of iterations to run
 
342
 * @buf: Buffer for the generated key
 
343
 * @buflen: Length of the buffer in bytes
 
344
 *
 
345
 * This function is used to derive PSK for WPA-PSK. For this protocol,
 
346
 * iterations is set to 4096 and buflen to 32. This function is described in
 
347
 * IEEE Std 802.11-2004, Clause H.4. The main construction is from PKCS#5 v2.0.
 
348
 */
288
349
void pbkdf2_sha1(const char *passphrase, const char *ssid, size_t ssid_len,
289
350
                 int iterations, u8 *buf, size_t buflen)
290
351
{
305
366
}
306
367
 
307
368
 
308
 
void sha1_transform(u8 *state, u8 data[64])
309
 
{
310
 
#ifdef EAP_TLS_FUNCS
311
 
        SHA_CTX context;
312
 
        memset(&context, 0, sizeof(context));
313
 
        memcpy(&context.h0, state, 5 * 4);
314
 
        SHA1_Transform(&context, data);
315
 
        memcpy(state, &context.h0, 5 * 4);
316
 
#else /* EAP_TLS_FUNCS */
317
 
        SHA1Transform((u32 *) state, data);
318
 
#endif /* EAP_TLS_FUNCS */
319
 
}
320
 
 
321
 
 
 
369
#ifdef INTERNAL_SHA1
 
370
 
 
371
typedef struct {
 
372
        u32 state[5];
 
373
        u32 count[2];
 
374
        unsigned char buffer[64];
 
375
} SHA1_CTX;
 
376
 
 
377
static void SHA1Init(SHA1_CTX *context);
 
378
static void SHA1Update(SHA1_CTX *context, const void *data, u32 len);
 
379
static void SHA1Final(unsigned char digest[20], SHA1_CTX* context);
 
380
static void SHA1Transform(u32 state[5], const unsigned char buffer[64]);
 
381
 
 
382
 
 
383
/**
 
384
 * sha1_vector - SHA-1 hash for data vector
 
385
 * @num_elem: Number of elements in the data vector
 
386
 * @addr: Pointers to the data areas
 
387
 * @len: Lengths of the data blocks
 
388
 * @mac: Buffer for the hash
 
389
 */
322
390
void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
323
391
                 u8 *mac)
324
392
{
325
393
        SHA1_CTX ctx;
326
 
        int i;
 
394
        size_t i;
327
395
 
328
396
        SHA1Init(&ctx);
329
397
        for (i = 0; i < num_elem; i++)
332
400
}
333
401
 
334
402
 
335
 
#ifndef EAP_TLS_FUNCS
 
403
int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
 
404
{
 
405
        u8 xkey[64];
 
406
        u32 t[5], _t[5];
 
407
        int i, j, m, k;
 
408
        u8 *xpos = x;
 
409
        u32 carry;
 
410
 
 
411
        if (seed_len > sizeof(xkey))
 
412
                seed_len = sizeof(xkey);
 
413
 
 
414
        /* FIPS 186-2 + change notice 1 */
 
415
 
 
416
        memcpy(xkey, seed, seed_len);
 
417
        memset(xkey + seed_len, 0, 64 - seed_len);
 
418
        t[0] = 0x67452301;
 
419
        t[1] = 0xEFCDAB89;
 
420
        t[2] = 0x98BADCFE;
 
421
        t[3] = 0x10325476;
 
422
        t[4] = 0xC3D2E1F0;
 
423
 
 
424
        m = xlen / 40;
 
425
        for (j = 0; j < m; j++) {
 
426
                /* XSEED_j = 0 */
 
427
                for (i = 0; i < 2; i++) {
 
428
                        /* XVAL = (XKEY + XSEED_j) mod 2^b */
 
429
 
 
430
                        /* w_i = G(t, XVAL) */
 
431
                        memcpy(_t, t, 20);
 
432
                        SHA1Transform(_t, xkey);
 
433
                        _t[0] = host_to_be32(_t[0]);
 
434
                        _t[1] = host_to_be32(_t[1]);
 
435
                        _t[2] = host_to_be32(_t[2]);
 
436
                        _t[3] = host_to_be32(_t[3]);
 
437
                        _t[4] = host_to_be32(_t[4]);
 
438
                        memcpy(xpos, _t, 20);
 
439
 
 
440
                        /* XKEY = (1 + XKEY + w_i) mod 2^b */
 
441
                        carry = 1;
 
442
                        for (k = 19; k >= 0; k--) {
 
443
                                carry += xkey[k] + xpos[k];
 
444
                                xkey[k] = carry & 0xff;
 
445
                                carry >>= 8;
 
446
                        }
 
447
 
 
448
                        xpos += SHA1_MAC_LEN;
 
449
                }
 
450
                /* x_j = w_0|w_1 */
 
451
        }
 
452
 
 
453
        return 0;
 
454
}
 
455
 
336
456
 
337
457
/* ===== start - public domain SHA1 implementation ===== */
338
458
 
462
582
 
463
583
/* Hash a single 512-bit block. This is the core of the algorithm. */
464
584
 
465
 
void SHA1Transform(u32 state[5], const unsigned char buffer[64])
 
585
static void SHA1Transform(u32 state[5], const unsigned char buffer[64])
466
586
{
467
587
        u32 a, b, c, d, e;
468
588
        typedef union {
520
640
 
521
641
/* SHA1Init - Initialize new context */
522
642
 
523
 
void SHA1Init(SHA1_CTX* context)
 
643
static void SHA1Init(SHA1_CTX* context)
524
644
{
525
645
        /* SHA1 initialization constants */
526
646
        context->state[0] = 0x67452301;
534
654
 
535
655
/* Run your data through this. */
536
656
 
537
 
void SHA1Update(SHA1_CTX* context, const void *_data, u32 len)
 
657
static void SHA1Update(SHA1_CTX* context, const void *_data, u32 len)
538
658
{
539
659
        u32 i, j;
540
660
        const unsigned char *data = _data;
564
684
 
565
685
/* Add padding and return the message digest. */
566
686
 
567
 
void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
 
687
static void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
568
688
{
569
689
        u32 i;
570
690
        unsigned char finalcount[8];
595
715
 
596
716
/* ===== end - public domain SHA1 implementation ===== */
597
717
 
598
 
#endif /* EAP_TLS_FUNCS */
599
 
 
600
 
 
601
 
#ifdef TEST_MAIN
602
 
 
603
 
#include "md5.c"
604
 
 
605
 
static int test_eap_fast(void)
606
 
{
607
 
        /* draft-cam-winget-eap-fast-01.txt */
608
 
        const u8 pac_key[] = {
609
 
                0x0B, 0x97, 0x39, 0x0F, 0x37, 0x51, 0x78, 0x09,
610
 
                0x81, 0x1E, 0xFD, 0x9C, 0x6E, 0x65, 0x94, 0x2B,
611
 
                0x63, 0x2C, 0xE9, 0x53, 0x89, 0x38, 0x08, 0xBA,
612
 
                0x36, 0x0B, 0x03, 0x7C, 0xD1, 0x85, 0xE4, 0x14
613
 
        };
614
 
        const u8 seed[] = {
615
 
                0x3F, 0xFB, 0x11, 0xC4, 0x6C, 0xBF, 0xA5, 0x7A,
616
 
                0x54, 0x40, 0xDA, 0xE8, 0x22, 0xD3, 0x11, 0xD3,
617
 
                0xF7, 0x6D, 0xE4, 0x1D, 0xD9, 0x33, 0xE5, 0x93,
618
 
                0x70, 0x97, 0xEB, 0xA9, 0xB3, 0x66, 0xF4, 0x2A,
619
 
                0x00, 0x00, 0x00, 0x02, 0x6A, 0x66, 0x43, 0x2A,
620
 
                0x8D, 0x14, 0x43, 0x2C, 0xEC, 0x58, 0x2D, 0x2F,
621
 
                0xC7, 0x9C, 0x33, 0x64, 0xBA, 0x04, 0xAD, 0x3A,
622
 
                0x52, 0x54, 0xD6, 0xA5, 0x79, 0xAD, 0x1E, 0x00
623
 
        };
624
 
        const u8 master_secret[] = {
625
 
                0x4A, 0x1A, 0x51, 0x2C, 0x01, 0x60, 0xBC, 0x02,
626
 
                0x3C, 0xCF, 0xBC, 0x83, 0x3F, 0x03, 0xBC, 0x64,
627
 
                0x88, 0xC1, 0x31, 0x2F, 0x0B, 0xA9, 0xA2, 0x77,
628
 
                0x16, 0xA8, 0xD8, 0xE8, 0xBD, 0xC9, 0xD2, 0x29,
629
 
                0x38, 0x4B, 0x7A, 0x85, 0xBE, 0x16, 0x4D, 0x27,
630
 
                0x33, 0xD5, 0x24, 0x79, 0x87, 0xB1, 0xC5, 0xA2  
631
 
        };
632
 
        const u8 key_block[] = {
633
 
                0x59, 0x59, 0xBE, 0x8E, 0x41, 0x3A, 0x77, 0x74,
634
 
                0x8B, 0xB2, 0xE5, 0xD3, 0x60, 0xAC, 0x4D, 0x35,
635
 
                0xDF, 0xFB, 0xC8, 0x1E, 0x9C, 0x24, 0x9C, 0x8B,
636
 
                0x0E, 0xC3, 0x1D, 0x72, 0xC8, 0x84, 0x9D, 0x57,
637
 
                0x48, 0x51, 0x2E, 0x45, 0x97, 0x6C, 0x88, 0x70,
638
 
                0xBE, 0x5F, 0x01, 0xD3, 0x64, 0xE7, 0x4C, 0xBB,
639
 
                0x11, 0x24, 0xE3, 0x49, 0xE2, 0x3B, 0xCD, 0xEF,
640
 
                0x7A, 0xB3, 0x05, 0x39, 0x5D, 0x64, 0x8A, 0x44,
641
 
                0x11, 0xB6, 0x69, 0x88, 0x34, 0x2E, 0x8E, 0x29,
642
 
                0xD6, 0x4B, 0x7D, 0x72, 0x17, 0x59, 0x28, 0x05,
643
 
                0xAF, 0xF9, 0xB7, 0xFF, 0x66, 0x6D, 0xA1, 0x96,
644
 
                0x8F, 0x0B, 0x5E, 0x06, 0x46, 0x7A, 0x44, 0x84,
645
 
                0x64, 0xC1, 0xC8, 0x0C, 0x96, 0x44, 0x09, 0x98,
646
 
                0xFF, 0x92, 0xA8, 0xB4, 0xC6, 0x42, 0x28, 0x71
647
 
        };
648
 
        const u8 sks[] = {
649
 
                0xD6, 0x4B, 0x7D, 0x72, 0x17, 0x59, 0x28, 0x05,
650
 
                0xAF, 0xF9, 0xB7, 0xFF, 0x66, 0x6D, 0xA1, 0x96,
651
 
                0x8F, 0x0B, 0x5E, 0x06, 0x46, 0x7A, 0x44, 0x84,
652
 
                0x64, 0xC1, 0xC8, 0x0C, 0x96, 0x44, 0x09, 0x98,
653
 
                0xFF, 0x92, 0xA8, 0xB4, 0xC6, 0x42, 0x28, 0x71
654
 
        };
655
 
        const u8 isk[] = {
656
 
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
657
 
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
658
 
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
659
 
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
660
 
        };
661
 
        const u8 imck[] = {
662
 
                0x16, 0x15, 0x3C, 0x3F, 0x21, 0x55, 0xEF, 0xD9,
663
 
                0x7F, 0x34, 0xAE, 0xC8, 0x1A, 0x4E, 0x66, 0x80,
664
 
                0x4C, 0xC3, 0x76, 0xF2, 0x8A, 0xA9, 0x6F, 0x96,
665
 
                0xC2, 0x54, 0x5F, 0x8C, 0xAB, 0x65, 0x02, 0xE1,
666
 
                0x18, 0x40, 0x7B, 0x56, 0xBE, 0xEA, 0xA7, 0xC5,
667
 
                0x76, 0x5D, 0x8F, 0x0B, 0xC5, 0x07, 0xC6, 0xB9,
668
 
                0x04, 0xD0, 0x69, 0x56, 0x72, 0x8B, 0x6B, 0xB8,
669
 
                0x15, 0xEC, 0x57, 0x7B
670
 
        };
671
 
        const u8 msk[] = {
672
 
                0x4D, 0x83, 0xA9, 0xBE, 0x6F, 0x8A, 0x74, 0xED,
673
 
                0x6A, 0x02, 0x66, 0x0A, 0x63, 0x4D, 0x2C, 0x33,
674
 
                0xC2, 0xDA, 0x60, 0x15, 0xC6, 0x37, 0x04, 0x51,
675
 
                0x90, 0x38, 0x63, 0xDA, 0x54, 0x3E, 0x14, 0xB9,
676
 
                0x27, 0x99, 0x18, 0x1E, 0x07, 0xBF, 0x0F, 0x5A,
677
 
                0x5E, 0x3C, 0x32, 0x93, 0x80, 0x8C, 0x6C, 0x49,
678
 
                0x67, 0xED, 0x24, 0xFE, 0x45, 0x40, 0xA0, 0x59,
679
 
                0x5E, 0x37, 0xC2, 0xE9, 0xD0, 0x5D, 0x0A, 0xE3
680
 
        };
681
 
        u8 tlv[] = {
682
 
                0x80, 0x0C, 0x00, 0x38, 0x00, 0x01, 0x01, 0x00,
683
 
                0xD8, 0x6A, 0x8C, 0x68, 0x3C, 0x32, 0x31, 0xA8,
684
 
                0x56, 0x63, 0xB6, 0x40, 0x21, 0xFE, 0x21, 0x14,
685
 
                0x4E, 0xE7, 0x54, 0x20, 0x79, 0x2D, 0x42, 0x62,
686
 
                0xC9, 0xBF, 0x53, 0x7F, 0x54, 0xFD, 0xAC, 0x58,
687
 
                0x43, 0x24, 0x6E, 0x30, 0x92, 0x17, 0x6D, 0xCF,
688
 
                0xE6, 0xE0, 0x69, 0xEB, 0x33, 0x61, 0x6A, 0xCC,
689
 
                0x05, 0xC5, 0x5B, 0xB7
690
 
        };
691
 
        const u8 compound_mac[] = {
692
 
                0x43, 0x24, 0x6E, 0x30, 0x92, 0x17, 0x6D, 0xCF,
693
 
                0xE6, 0xE0, 0x69, 0xEB, 0x33, 0x61, 0x6A, 0xCC,
694
 
                0x05, 0xC5, 0x5B, 0xB7
695
 
        };
696
 
        u8 buf[512];
697
 
        const u8 *simck, *cmk;
698
 
        int errors = 0;
699
 
 
700
 
        printf("EAP-FAST test cases\n");
701
 
 
702
 
        printf("- T-PRF (SHA1) test case / master_secret\n");
703
 
        sha1_t_prf(pac_key, sizeof(pac_key), "PAC to master secret label hash",
704
 
                   seed, sizeof(seed), buf, sizeof(master_secret));
705
 
        if (memcmp(master_secret, buf, sizeof(master_secret)) != 0) {
706
 
                printf("T-PRF test - FAILED!\n");
707
 
                errors++;
708
 
        }
709
 
 
710
 
        printf("- PRF (TLS, SHA1/MD5) test case / key_block\n");
711
 
        tls_prf(master_secret, sizeof(master_secret), "key expansion",
712
 
                seed, sizeof(seed), buf, sizeof(key_block));
713
 
        if (memcmp(key_block, buf, sizeof(key_block)) != 0) {
714
 
                printf("PRF test - FAILED!\n");
715
 
                errors++;
716
 
        }
717
 
 
718
 
        printf("- T-PRF (SHA1) test case / IMCK\n");
719
 
        sha1_t_prf(sks, sizeof(sks), "Inner Methods Compound Keys",
720
 
                   isk, sizeof(isk), buf, sizeof(imck));
721
 
        if (memcmp(imck, buf, sizeof(imck)) != 0) {
722
 
                printf("T-PRF test - FAILED!\n");
723
 
                errors++;
724
 
        }
725
 
 
726
 
        simck = imck;
727
 
        cmk = imck + 40;
728
 
 
729
 
        printf("- T-PRF (SHA1) test case / MSK\n");
730
 
        sha1_t_prf(simck, 40, "Session Key Generating Function",
731
 
                   "", 0, buf, sizeof(msk));
732
 
        if (memcmp(msk, buf, sizeof(msk)) != 0) {
733
 
                printf("T-PRF test - FAILED!\n");
734
 
                errors++;
735
 
        }
736
 
 
737
 
        printf("- Compound MAC test case\n");
738
 
        memset(tlv + sizeof(tlv) - 20, 0, 20);
739
 
        hmac_sha1(cmk, 20, tlv, sizeof(tlv), tlv + sizeof(tlv) - 20);
740
 
        if (memcmp(tlv + sizeof(tlv) - 20, compound_mac, sizeof(compound_mac))
741
 
            != 0) {
742
 
                printf("Compound MAC test - FAILED!\n");
743
 
                errors++;
744
 
        }
745
 
 
746
 
        return errors;
747
 
}
748
 
 
749
 
 
750
 
static u8 key0[] =
751
 
{
752
 
        0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
753
 
        0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
754
 
        0x0b, 0x0b, 0x0b, 0x0b
755
 
};
756
 
static u8 data0[] = "Hi There";
757
 
static u8 prf0[] =
758
 
{
759
 
        0xbc, 0xd4, 0xc6, 0x50, 0xb3, 0x0b, 0x96, 0x84,
760
 
        0x95, 0x18, 0x29, 0xe0, 0xd7, 0x5f, 0x9d, 0x54,
761
 
        0xb8, 0x62, 0x17, 0x5e, 0xd9, 0xf0, 0x06, 0x06,
762
 
        0xe1, 0x7d, 0x8d, 0xa3, 0x54, 0x02, 0xff, 0xee,
763
 
        0x75, 0xdf, 0x78, 0xc3, 0xd3, 0x1e, 0x0f, 0x88,
764
 
        0x9f, 0x01, 0x21, 0x20, 0xc0, 0x86, 0x2b, 0xeb,
765
 
        0x67, 0x75, 0x3e, 0x74, 0x39, 0xae, 0x24, 0x2e,
766
 
        0xdb, 0x83, 0x73, 0x69, 0x83, 0x56, 0xcf, 0x5a
767
 
};
768
 
 
769
 
static u8 key1[] = "Jefe";
770
 
static u8 data1[] = "what do ya want for nothing?";
771
 
static u8 prf1[] =
772
 
{
773
 
        0x51, 0xf4, 0xde, 0x5b, 0x33, 0xf2, 0x49, 0xad,
774
 
        0xf8, 0x1a, 0xeb, 0x71, 0x3a, 0x3c, 0x20, 0xf4,
775
 
        0xfe, 0x63, 0x14, 0x46, 0xfa, 0xbd, 0xfa, 0x58,
776
 
        0x24, 0x47, 0x59, 0xae, 0x58, 0xef, 0x90, 0x09,
777
 
        0xa9, 0x9a, 0xbf, 0x4e, 0xac, 0x2c, 0xa5, 0xfa,
778
 
        0x87, 0xe6, 0x92, 0xc4, 0x40, 0xeb, 0x40, 0x02,
779
 
        0x3e, 0x7b, 0xab, 0xb2, 0x06, 0xd6, 0x1d, 0xe7,
780
 
        0xb9, 0x2f, 0x41, 0x52, 0x90, 0x92, 0xb8, 0xfc
781
 
};
782
 
 
783
 
 
784
 
static u8 key2[] =
785
 
{
786
 
        0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
787
 
        0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
788
 
        0xaa, 0xaa, 0xaa, 0xaa
789
 
};
790
 
static u8 data2[] =
791
 
{
792
 
        0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
793
 
        0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
794
 
        0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
795
 
        0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
796
 
        0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
797
 
        0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
798
 
        0xdd, 0xdd
799
 
};
800
 
static u8 prf2[] =
801
 
{
802
 
        0xe1, 0xac, 0x54, 0x6e, 0xc4, 0xcb, 0x63, 0x6f,
803
 
        0x99, 0x76, 0x48, 0x7b, 0xe5, 0xc8, 0x6b, 0xe1,
804
 
        0x7a, 0x02, 0x52, 0xca, 0x5d, 0x8d, 0x8d, 0xf1,
805
 
        0x2c, 0xfb, 0x04, 0x73, 0x52, 0x52, 0x49, 0xce,
806
 
        0x9d, 0xd8, 0xd1, 0x77, 0xea, 0xd7, 0x10, 0xbc,
807
 
        0x9b, 0x59, 0x05, 0x47, 0x23, 0x91, 0x07, 0xae,
808
 
        0xf7, 0xb4, 0xab, 0xd4, 0x3d, 0x87, 0xf0, 0xa6,
809
 
        0x8f, 0x1c, 0xbd, 0x9e, 0x2b, 0x6f, 0x76, 0x07
810
 
};
811
 
 
812
 
 
813
 
struct passphrase_test {
814
 
        char *passphrase;
815
 
        char *ssid;
816
 
        char psk[32];
817
 
};
818
 
 
819
 
static struct passphrase_test passphrase_tests[] =
820
 
{
821
 
        {
822
 
                "password",
823
 
                "IEEE",
824
 
                {
825
 
                        0xf4, 0x2c, 0x6f, 0xc5, 0x2d, 0xf0, 0xeb, 0xef,
826
 
                        0x9e, 0xbb, 0x4b, 0x90, 0xb3, 0x8a, 0x5f, 0x90,
827
 
                        0x2e, 0x83, 0xfe, 0x1b, 0x13, 0x5a, 0x70, 0xe2,
828
 
                        0x3a, 0xed, 0x76, 0x2e, 0x97, 0x10, 0xa1, 0x2e
829
 
                }
830
 
        },
831
 
        {
832
 
                "ThisIsAPassword",
833
 
                "ThisIsASSID",
834
 
                {
835
 
                        0x0d, 0xc0, 0xd6, 0xeb, 0x90, 0x55, 0x5e, 0xd6,
836
 
                        0x41, 0x97, 0x56, 0xb9, 0xa1, 0x5e, 0xc3, 0xe3,
837
 
                        0x20, 0x9b, 0x63, 0xdf, 0x70, 0x7d, 0xd5, 0x08,
838
 
                        0xd1, 0x45, 0x81, 0xf8, 0x98, 0x27, 0x21, 0xaf
839
 
                }
840
 
        },
841
 
        {
842
 
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
843
 
                "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
844
 
                {
845
 
                        0xbe, 0xcb, 0x93, 0x86, 0x6b, 0xb8, 0xc3, 0x83,
846
 
                        0x2c, 0xb7, 0x77, 0xc2, 0xf5, 0x59, 0x80, 0x7c,
847
 
                        0x8c, 0x59, 0xaf, 0xcb, 0x6e, 0xae, 0x73, 0x48,
848
 
                        0x85, 0x00, 0x13, 0x00, 0xa9, 0x81, 0xcc, 0x62
849
 
                }
850
 
        },
851
 
};
852
 
 
853
 
#define NUM_PASSPHRASE_TESTS \
854
 
(sizeof(passphrase_tests) / sizeof(passphrase_tests[0]))
855
 
 
856
 
 
857
 
int main(int argc, char *argv[])
858
 
{
859
 
        u8 res[512];
860
 
        int ret = 0, i;
861
 
 
862
 
        printf("PRF-SHA1 test cases:\n");
863
 
 
864
 
        sha1_prf(key0, sizeof(key0), "prefix", data0, sizeof(data0) - 1,
865
 
                 res, sizeof(prf0));
866
 
        if (memcmp(res, prf0, sizeof(prf0)) == 0)
867
 
                printf("Test case 0 - OK\n");
868
 
        else {
869
 
                printf("Test case 0 - FAILED!\n");
870
 
                ret++;
871
 
        }
872
 
 
873
 
        sha1_prf(key1, sizeof(key1) - 1, "prefix", data1, sizeof(data1) - 1,
874
 
                 res, sizeof(prf1));
875
 
        if (memcmp(res, prf1, sizeof(prf1)) == 0)
876
 
                printf("Test case 1 - OK\n");
877
 
        else {
878
 
                printf("Test case 1 - FAILED!\n");
879
 
                ret++;
880
 
        }
881
 
 
882
 
        sha1_prf(key2, sizeof(key2), "prefix", data2, sizeof(data2),
883
 
                 res, sizeof(prf2));
884
 
        if (memcmp(res, prf2, sizeof(prf2)) == 0)
885
 
                printf("Test case 2 - OK\n");
886
 
        else {
887
 
                printf("Test case 2 - FAILED!\n");
888
 
                ret++;
889
 
        }
890
 
 
891
 
        ret += test_eap_fast();
892
 
 
893
 
        printf("PBKDF2-SHA1 Passphrase test cases:\n");
894
 
        for (i = 0; i < NUM_PASSPHRASE_TESTS; i++) {
895
 
                u8 psk[32];
896
 
                struct passphrase_test *test = &passphrase_tests[i];
897
 
                pbkdf2_sha1(test->passphrase,
898
 
                            test->ssid, strlen(test->ssid),
899
 
                            4096, psk, 32);
900
 
                if (memcmp(psk, test->psk, 32) == 0)
901
 
                        printf("Test case %d - OK\n", i);
902
 
                else {
903
 
                        printf("Test case %d - FAILED!\n", i);
904
 
                        ret++;
905
 
                }
906
 
        }
907
 
 
908
 
        return ret;
909
 
}
910
 
#endif /* TEST_MAIN */
 
718
#endif /* INTERNAL_SHA1 */