remove CONFIG_FIPS substitutions when building md5-non-fips.c
[libeap.git] / src / crypto / md5-non-fips.c
1 /*
2  * MD5 hash implementation and interface functions (non-FIPS allowed cases)
3  * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "md5.h"
19 #include "crypto.h"
20
21 #undef hmac_md5_vector_non_fips_allow
22 #undef hmac_md5_non_fips_allow
23
24 /**
25  * hmac_md5_vector_non_fips_allow - HMAC-MD5 over data vector (RFC 2104)
26  * @key: Key for HMAC operations
27  * @key_len: Length of the key in bytes
28  * @num_elem: Number of elements in the data vector
29  * @addr: Pointers to the data areas
30  * @len: Lengths of the data blocks
31  * @mac: Buffer for the hash (16 bytes)
32  * Returns: 0 on success, -1 on failure
33  */
34 int hmac_md5_vector_non_fips_allow(const u8 *key, size_t key_len,
35                                    size_t num_elem, const u8 *addr[],
36                                    const size_t *len, u8 *mac)
37 {
38         u8 k_pad[64]; /* padding - key XORd with ipad/opad */
39         u8 tk[16];
40         const u8 *_addr[6];
41         size_t i, _len[6];
42
43         if (num_elem > 5) {
44                 /*
45                  * Fixed limit on the number of fragments to avoid having to
46                  * allocate memory (which could fail).
47                  */
48                 return -1;
49         }
50
51         /* if key is longer than 64 bytes reset it to key = MD5(key) */
52         if (key_len > 64) {
53                 if (md5_vector_non_fips_allow(1, &key, &key_len, tk))
54                         return -1;
55                 key = tk;
56                 key_len = 16;
57         }
58
59         /* the HMAC_MD5 transform looks like:
60          *
61          * MD5(K XOR opad, MD5(K XOR ipad, text))
62          *
63          * where K is an n byte key
64          * ipad is the byte 0x36 repeated 64 times
65          * opad is the byte 0x5c repeated 64 times
66          * and text is the data being protected */
67
68         /* start out by storing key in ipad */
69         os_memset(k_pad, 0, sizeof(k_pad));
70         os_memcpy(k_pad, key, key_len);
71
72         /* XOR key with ipad values */
73         for (i = 0; i < 64; i++)
74                 k_pad[i] ^= 0x36;
75
76         /* perform inner MD5 */
77         _addr[0] = k_pad;
78         _len[0] = 64;
79         for (i = 0; i < num_elem; i++) {
80                 _addr[i + 1] = addr[i];
81                 _len[i + 1] = len[i];
82         }
83         if (md5_vector_non_fips_allow(1 + num_elem, _addr, _len, mac))
84                 return -1;
85
86         os_memset(k_pad, 0, sizeof(k_pad));
87         os_memcpy(k_pad, key, key_len);
88         /* XOR key with opad values */
89         for (i = 0; i < 64; i++)
90                 k_pad[i] ^= 0x5c;
91
92         /* perform outer MD5 */
93         _addr[0] = k_pad;
94         _len[0] = 64;
95         _addr[1] = mac;
96         _len[1] = MD5_MAC_LEN;
97         return md5_vector_non_fips_allow(2, _addr, _len, mac);
98 }
99
100
101 /**
102  * hmac_md5_non_fips_allow - HMAC-MD5 over data buffer (RFC 2104)
103  * @key: Key for HMAC operations
104  * @key_len: Length of the key in bytes
105  * @data: Pointers to the data area
106  * @data_len: Length of the data area
107  * @mac: Buffer for the hash (16 bytes)
108  * Returns: 0 on success, -1 on failure
109  */
110 int hmac_md5_non_fips_allow(const u8 *key, size_t key_len, const u8 *data,
111                             size_t data_len, u8 *mac)
112 {
113         return hmac_md5_vector_non_fips_allow(key, key_len, 1, &data,
114                                               &data_len, mac);
115 }