blob: a0f75a96ebabed913b9cc61330fdf39e1c5eea10 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <stdio.h>
16#include <string.h>
17
Adam Langleyd9e397b2015-01-22 14:27:53 -080018#include <openssl/crypto.h>
19#include <openssl/digest.h>
20#include <openssl/err.h>
21#include <openssl/hkdf.h>
22
David Benjamin4969cc92016-04-22 15:02:23 -040023#include "../test/test_util.h"
24
Adam Langleyd9e397b2015-01-22 14:27:53 -080025
26typedef struct {
27 const EVP_MD *(*md_func)(void);
28 const uint8_t ikm[80];
29 const size_t ikm_len;
30 const uint8_t salt[80];
31 const size_t salt_len;
32 const uint8_t info[80];
33 const size_t info_len;
David Benjamind316cba2016-06-02 16:17:39 -040034 const uint8_t prk[EVP_MAX_MD_SIZE];
35 const size_t prk_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -080036 const size_t out_len;
37 const uint8_t out[82];
38} hkdf_test_vector_t;
39
40/* These test vectors are from RFC 5869. */
41static const hkdf_test_vector_t kTests[] = {
42 {
43 EVP_sha256,
44 {
45 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
46 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
47 }, 22,
48 {
49 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
50 0x0c,
51 }, 13,
52 {
53 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
54 }, 10,
David Benjamind316cba2016-06-02 16:17:39 -040055 {
56 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, 0x0d, 0xdc, 0x3f, 0x0d,
57 0xc4, 0x7b, 0xba, 0x63, 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31,
58 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5,
59 }, 32,
Adam Langleyd9e397b2015-01-22 14:27:53 -080060 42, {
61 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64,
62 0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
63 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08,
64 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65
65 }
66 },
67 {
68 EVP_sha256,
69 {
70 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
71 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
72 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
73 0x24, 0x25, 0x26,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 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
93 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
94 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
95 }, 80,
David Benjamind316cba2016-06-02 16:17:39 -040096 {
97 0x06, 0xa6, 0xb8, 0x8c, 0x58, 0x53, 0x36, 0x1a, 0x06, 0x10, 0x4c, 0x9c,
98 0xeb, 0x35, 0xb4, 0x5c, 0xef, 0x76, 0x00, 0x14, 0x90, 0x46, 0x71, 0x01,
99 0x4a, 0x19, 0x3f, 0x40, 0xc1, 0x5f, 0xc2, 0x44,
100 }, 32,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800101 82, {
102 0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1, 0xc8, 0xe7, 0xf7, 0x8c,
103 0x59, 0x6a, 0x49, 0x34, 0x4f, 0x01, 0x2e, 0xda, 0x2d, 0x4e, 0xfa, 0xd8,
104 0xa0, 0x50, 0xcc, 0x4c, 0x19, 0xaf, 0xa9, 0x7c, 0x59, 0x04, 0x5a, 0x99,
105 0xca, 0xc7, 0x82, 0x72, 0x71, 0xcb, 0x41, 0xc6, 0x5e, 0x59, 0x0e, 0x09,
106 0xda, 0x32, 0x75, 0x60, 0x0c, 0x2f, 0x09, 0xb8, 0x36, 0x77, 0x93, 0xa9,
107 0xac, 0xa3, 0xdb, 0x71, 0xcc, 0x30, 0xc5, 0x81, 0x79, 0xec, 0x3e, 0x87,
108 0xc1, 0x4c, 0x01, 0xd5, 0xc1, 0xf3, 0x43, 0x4f, 0x1d, 0x87
109 }
110 },
111 {
112 EVP_sha256,
113 {
114 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
115 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
116 }, 22,
117 {
118 0,
119 }, 0,
120 {
121 0,
122 }, 0,
David Benjamind316cba2016-06-02 16:17:39 -0400123 {
124 0x19, 0xef, 0x24, 0xa3, 0x2c, 0x71, 0x7b, 0x16, 0x7f, 0x33, 0xa9, 0x1d,
125 0x6f, 0x64, 0x8b, 0xdf, 0x96, 0x59, 0x67, 0x76, 0xaf, 0xdb, 0x63, 0x77,
126 0xac, 0x43, 0x4c, 0x1c, 0x29, 0x3c, 0xcb, 0x04
127 }, 32,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800128 42, {
129 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f, 0x71, 0x5f, 0x80, 0x2a,
130 0x06, 0x3c, 0x5a, 0x31, 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
131 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d, 0x9d, 0x20, 0x13, 0x95,
132 0xfa, 0xa4, 0xb6, 0x1a, 0x96, 0xc8
133 }
134 },
135 {
136 EVP_sha1,
137 {
138 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
139 }, 11,
140 {
141 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
142 0x0c,
143 }, 13,
144 {
145 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
146 }, 10,
David Benjamind316cba2016-06-02 16:17:39 -0400147 {
148 0x9b, 0x6c, 0x18, 0xc4, 0x32, 0xa7, 0xbf, 0x8f, 0x0e, 0x71, 0xc8, 0xeb,
149 0x88, 0xf4, 0xb3, 0x0b, 0xaa, 0x2b, 0xa2, 0x43
150 }, 20,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151 42, {
152 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69, 0x33, 0x06, 0x8b, 0x56,
153 0xef, 0xa5, 0xad, 0x81, 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
154 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2, 0xc2, 0x2e, 0x42, 0x24,
155 0x78, 0xd3, 0x05, 0xf3, 0xf8, 0x96
156 }
157 },
158 {
159 EVP_sha1,
160 {
161 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
162 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
163 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
164 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
165 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
166 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
167 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
168 }, 80,
169 {
170 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
171 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
172 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
173 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
174 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
175 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
176 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf
177 }, 80,
178 {
179 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb,
180 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
181 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3,
182 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
183 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
184 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
185 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
186 }, 80,
David Benjamind316cba2016-06-02 16:17:39 -0400187 {
188 0x8a, 0xda, 0xe0, 0x9a, 0x2a, 0x30, 0x70, 0x59, 0x47, 0x8d, 0x30, 0x9b,
189 0x26, 0xc4, 0x11, 0x5a, 0x22, 0x4c, 0xfa, 0xf6,
190 }, 20,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800191 82, {
192 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7, 0xc9, 0xf1, 0x2c, 0xd5,
193 0x91, 0x2a, 0x06, 0xeb, 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d, 0x92, 0x19,
194 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe, 0x8f, 0xa3, 0xf1, 0xa4,
195 0xe5, 0xad, 0x79, 0xf3, 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2, 0x17, 0x3c,
196 0x48, 0x6e, 0xa3, 0x7c, 0xe3, 0xd3, 0x97, 0xed, 0x03, 0x4c, 0x7f, 0x9d,
197 0xfe, 0xb1, 0x5c, 0x5e, 0x92, 0x73, 0x36, 0xd0, 0x44, 0x1f, 0x4c, 0x43,
198 0x00, 0xe2, 0xcf, 0xf0, 0xd0, 0x90, 0x0b, 0x52, 0xd3, 0xb4
199 }
200 },
201 {
202 EVP_sha1,
203 {
204 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
205 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
206 }, 22,
207 {
208 0,
209 }, 0,
210 {
211 0,
212 }, 0,
David Benjamind316cba2016-06-02 16:17:39 -0400213 {
214 0xda, 0x8c, 0x8a, 0x73, 0xc7, 0xfa, 0x77, 0x28, 0x8e, 0xc6, 0xf5, 0xe7,
215 0xc2, 0x97, 0x78, 0x6a, 0xa0, 0xd3, 0x2d, 0x01,
216 }, 20,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800217 42, {
218 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61, 0xd1, 0xe5, 0x52, 0x98,
219 0xda, 0x9d, 0x05, 0x06, 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
220 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0, 0xea, 0x00, 0x03, 0x3d,
221 0xe0, 0x39, 0x84, 0xd3, 0x49, 0x18
222 }
223 },
224 {
225 EVP_sha1,
226 {
227 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
228 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
229 }, 22,
230 {
231 0,
232 }, 0,
233 {
234 0,
235 }, 0,
David Benjamind316cba2016-06-02 16:17:39 -0400236 {
237 0x2a, 0xdc, 0xca, 0xda, 0x18, 0x77, 0x9e, 0x7c, 0x20, 0x77, 0xad, 0x2e,
238 0xb1, 0x9d, 0x3f, 0x3e, 0x73, 0x13, 0x85, 0xdd,
239 }, 20,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 42, {
241 0x2c, 0x91, 0x11, 0x72, 0x04, 0xd7, 0x45, 0xf3, 0x50, 0x0d, 0x63, 0x6a,
242 0x62, 0xf6, 0x4f, 0x0a, 0xb3, 0xba, 0xe5, 0x48, 0xaa, 0x53, 0xd4, 0x23,
243 0xb0, 0xd1, 0xf2, 0x7e, 0xbb, 0xa6, 0xf5, 0xe5, 0x67, 0x3a, 0x08, 0x1d,
244 0x70, 0xcc, 0xe7, 0xac, 0xfc, 0x48
245 }
246 },
247};
248
249int main(void) {
David Benjamind316cba2016-06-02 16:17:39 -0400250 uint8_t buf[82], prk[EVP_MAX_MD_SIZE];
251 size_t i, prk_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800252
253 CRYPTO_library_init();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800254
255 for (i = 0; i < sizeof(kTests) / sizeof(kTests[0]); i++) {
256 const hkdf_test_vector_t *test = &kTests[i];
David Benjamind316cba2016-06-02 16:17:39 -0400257 if (!HKDF_extract(prk, &prk_len, test->md_func(), test->ikm, test->ikm_len,
258 test->salt, test->salt_len)) {
259 fprintf(stderr, "Call to HKDF_extract failed\n");
260 ERR_print_errors_fp(stderr);
261 return 1;
262 }
263 if (prk_len != test->prk_len ||
264 memcmp(prk, test->prk, test->prk_len) != 0) {
265 fprintf(stderr, "%zu: Resulting PRK does not match test vector\n", i);
266 return 1;
267 }
268 if (!HKDF_expand(buf, test->out_len, test->md_func(), prk, prk_len,
269 test->info, test->info_len)) {
270 fprintf(stderr, "Call to HKDF_expand failed\n");
271 ERR_print_errors_fp(stderr);
272 return 1;
273 }
274 if (memcmp(buf, test->out, test->out_len) != 0) {
275 fprintf(stderr,
276 "%zu: Resulting key material does not match test vector\n", i);
277 return 1;
278 }
279
Adam Langleyd9e397b2015-01-22 14:27:53 -0800280 if (!HKDF(buf, test->out_len, test->md_func(), test->ikm, test->ikm_len,
281 test->salt, test->salt_len, test->info, test->info_len)) {
282 fprintf(stderr, "Call to HKDF failed\n");
Adam Langleye9ada862015-05-11 17:20:37 -0700283 ERR_print_errors_fp(stderr);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800284 return 1;
285 }
286 if (memcmp(buf, test->out, test->out_len) != 0) {
David Benjamin9aaebef2016-04-22 15:02:23 -0400287 fprintf(stderr,
288 "%zu: Resulting key material does not match test vector\n", i);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800289 return 1;
290 }
291 }
292
293 printf("PASS\n");
294 ERR_free_strings();
295 return 0;
296}