The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
hmac_tests.c
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /** Tests for the internal hmac functions
18  *
19  * @file src/lib/util/hmac_tests.c
20  *
21  * @copyright 2021 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22  */
23 
24 #include <freeradius-devel/util/acutest.h>
25 #include <freeradius-devel/util/acutest_helpers.h>
26 #include <freeradius-devel/util/md5.h>
27 #include <freeradius-devel/util/sha1.h>
28 
29 /*
30 Test Vectors (Trailing '\0' of a character string not included in test):
31 
32  key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
33  key_len = 16 bytes
34  data = "Hi There"
35  data_len = 8 bytes
36  digest = 0x9294727a3638bb1c13f48ef8158bfc9d
37 
38  key = "Jefe"
39  data = "what do ya want for nothing?"
40  data_len = 28 bytes
41  digest = 0x750c783e6ab0b503eaa86e310a5db738
42 
43  key = 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
44 
45  key_len 16 bytes
46  data = 0xDDDDDDDDDDDDDDDDDDDD...
47  ..DDDDDDDDDDDDDDDDDDDD...
48  ..DDDDDDDDDDDDDDDDDDDD...
49  ..DDDDDDDDDDDDDDDDDDDD...
50  ..DDDDDDDDDDDDDDDDDDDD
51  data_len = 50 bytes
52  digest = 0x56be34521d144c88dbb8c733f0e8b3f6
53 */
54 static void test_hmac_md5(void)
55 {
56  uint8_t digest[16];
57  uint8_t const *key;
58  int key_len;
59  uint8_t const *text;
60  int text_len;
61 
62  /*
63  * Test 1
64  */
65  key = (uint8_t[]){
66  0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
67  0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
68  0x00
69  };
70  key_len = strlen((char const *)key);
71 
72  text = (uint8_t const *)"Hi There";
73  text_len = strlen((char const *)text);
74 
75  fr_hmac_md5(digest, text, text_len, key, key_len);
76 
77  TEST_CHECK_RET(memcmp(digest,
78  (uint8_t[]){
79  0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
80  0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d
81  },
82  sizeof(digest)), 0);
83 
84  /*
85  * Test 2
86  */
87  key = (uint8_t const *)"Jefe";
88  key_len = strlen((char const *)key);
89 
90  text = (uint8_t const *)"what do ya want for nothing?";
91  text_len = strlen((char const *)text);
92 
93  fr_hmac_md5(digest, text, text_len, key, key_len);
94 
95  TEST_CHECK_RET(memcmp(digest,
96  (uint8_t[]){
97  0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
98  0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38
99  },
100  sizeof(digest)), 0);
101 
102  /*
103  * Test 3
104  */
105  key = (uint8_t[]){
106  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
107  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
108  0x00
109  };
110  key_len = strlen((char const *)key);
111 
112  text = (uint8_t[]){
113  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
114  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
115  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
116  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
117  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
118  0x00
119  };
120  text_len = strlen((char const *)text);
121 
122  fr_hmac_md5(digest, text, text_len, key, key_len);
123 
124  TEST_CHECK_RET(memcmp(digest,
125  (uint8_t[]){
126  0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
127  0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6
128  },
129  sizeof(digest)), 0);
130 }
131 
132 /*
133 Test Vectors (Trailing '\0' of a character string not included in test):
134 
135  key = "Jefe"
136  data = "what do ya want for nothing?"
137  data_len = 28 bytes
138  digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
139 
140  key = 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
141 
142  key_len 16 bytes
143  data = 0xDDDDDDDDDDDDDDDDDDDD...
144  ..DDDDDDDDDDDDDDDDDDDD...
145  ..DDDDDDDDDDDDDDDDDDDD...
146  ..DDDDDDDDDDDDDDDDDDDD...
147  ..DDDDDDDDDDDDDDDDDDDD
148  data_len = 50 bytes
149  digest = 0xd730594d167e35d5956fd8003d0db3d3f46dc7bb
150 */
151 static void test_hmac_sha1(void)
152 {
153 
154  uint8_t digest[20];
155  uint8_t const *key;
156  int key_len;
157  uint8_t const *text;
158  int text_len;
159 
160  /*
161  * Test 1
162  */
163  key = (uint8_t const *)"Jefe";
164  key_len = strlen((char const *)key);
165 
166  text = (uint8_t const *)"what do ya want for nothing?";
167  text_len = strlen((char const *)text);
168 
169  fr_hmac_sha1(digest, text, text_len, key, key_len);
170 
171  TEST_CHECK_RET(memcmp(digest,
172  (uint8_t[]){
173  0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb, 0x2f, 0xa2, 0xd2, 0x74,
174  0x16, 0xd5, 0xf1, 0x84, 0xdf, 0x9c, 0x25, 0x9a, 0x7c, 0x79
175  },
176  sizeof(digest)), 0);
177 
178  /*
179  * Test 2
180  */
181  key = (uint8_t[]){
182  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
183  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
184  0x00
185  };
186  key_len = strlen((char const *)key);
187 
188  text = (uint8_t[]){
189  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
190  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
191  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
192  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
193  0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
194  0x00
195  };
196  text_len = strlen((char const *)text);
197 
198  fr_hmac_sha1(digest, text, text_len, key, key_len);
199 
200  TEST_CHECK_RET(memcmp(digest,
201  (uint8_t[]){
202  0xd7, 0x30, 0x59, 0x4d, 0x16, 0x7e, 0x35, 0xd5, 0x95, 0x6f,
203  0xd8, 0x00, 0x3d, 0x0d, 0xb3, 0xd3, 0xf4, 0x6d, 0xc7, 0xbb
204  },
205  sizeof(digest)), 0);
206 }
207 
209  /*
210  * Allocation and management
211  */
212  { "hmac-md5", test_hmac_md5 },
213  { "hmac-sha1", test_hmac_sha1 },
214 
215  { NULL }
216 };
TEST_CHECK_RET((int) count,(int) 3)
int fr_hmac_md5(uint8_t digest[MD5_DIGEST_LENGTH], uint8_t const *in, size_t inlen, uint8_t const *key, size_t key_len)
Calculate HMAC using internal MD5 implementation.
Definition: hmac_md5.c:119
int fr_hmac_sha1(uint8_t digest[static SHA1_DIGEST_LENGTH], uint8_t const *in, size_t inlen, uint8_t const *key, size_t key_len)
Calculate HMAC using internal SHA1 implementation.
Definition: hmac_sha1.c:124
TEST_LIST
Definition: hmac_tests.c:208
static void test_hmac_md5(void)
Definition: hmac_tests.c:54
static void test_hmac_sha1(void)
Definition: hmac_tests.c:151
unsigned char uint8_t
Definition: merged_model.c:30