summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/aead/aeadtest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libcrypto/aead/aeadtest.c')
-rw-r--r--src/regress/lib/libcrypto/aead/aeadtest.c368
1 files changed, 0 insertions, 368 deletions
diff --git a/src/regress/lib/libcrypto/aead/aeadtest.c b/src/regress/lib/libcrypto/aead/aeadtest.c
deleted file mode 100644
index 4f0ab9fa3a..0000000000
--- a/src/regress/lib/libcrypto/aead/aeadtest.c
+++ /dev/null
@@ -1,368 +0,0 @@
1/* $OpenBSD: aeadtest.c,v 1.11 2018/07/17 17:06:49 tb Exp $ */
2/* ====================================================================
3 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 */
50
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <stdint.h>
55
56#include <openssl/evp.h>
57
58/* This program tests an AEAD against a series of test vectors from a file. The
59 * test vector file consists of key-value lines where the key and value are
60 * separated by a colon and optional whitespace. The keys are listed in
61 * NAMES, below. The values are hex-encoded data.
62 *
63 * After a number of key-value lines, a blank line indicates the end of the
64 * test case.
65 *
66 * For example, here's a valid test case:
67 *
68 * AEAD: chacha20-poly1305
69 * KEY: bcb2639bf989c6251b29bf38d39a9bdce7c55f4b2ac12a39c8a37b5d0a5cc2b5
70 * NONCE: 1e8b4c510f5ca083
71 * IN: 8c8419bc27
72 * AD: 34ab88c265
73 * CT: 1a7c2f33f5
74 * TAG: 2875c659d0f2808de3a40027feff91a4
75 */
76
77#define BUF_MAX 1024
78
79/* These are the different types of line that are found in the input file. */
80enum {
81 AEAD = 0, /* name of the AEAD algorithm. */
82 KEY, /* hex encoded key. */
83 NONCE, /* hex encoded nonce. */
84 IN, /* hex encoded plaintext. */
85 AD, /* hex encoded additional data. */
86 CT, /* hex encoded ciphertext (not including the
87 * authenticator, which is next. */
88 TAG, /* hex encoded authenticator. */
89 NUM_TYPES
90};
91
92static const char NAMES[NUM_TYPES][6] = {
93 "AEAD",
94 "KEY",
95 "NONCE",
96 "IN",
97 "AD",
98 "CT",
99 "TAG",
100};
101
102static unsigned char
103hex_digit(char h)
104{
105 if (h >= '0' && h <= '9')
106 return h - '0';
107 else if (h >= 'a' && h <= 'f')
108 return h - 'a' + 10;
109 else if (h >= 'A' && h <= 'F')
110 return h - 'A' + 10;
111 else
112 return 16;
113}
114
115static int
116aead_from_name(const EVP_AEAD **aead, const char *name)
117{
118 *aead = NULL;
119
120 if (strcmp(name, "aes-128-gcm") == 0) {
121#ifndef OPENSSL_NO_AES
122 *aead = EVP_aead_aes_128_gcm();
123#else
124 fprintf(stderr, "No AES support.\n");
125#endif
126 } else if (strcmp(name, "aes-256-gcm") == 0) {
127#ifndef OPENSSL_NO_AES
128 *aead = EVP_aead_aes_256_gcm();
129#else
130 fprintf(stderr, "No AES support.\n");
131#endif
132 } else if (strcmp(name, "chacha20-poly1305") == 0) {
133#if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
134 *aead = EVP_aead_chacha20_poly1305();
135#else
136 fprintf(stderr, "No chacha20-poly1305 support.\n");
137#endif
138 } else {
139 fprintf(stderr, "Unknown AEAD: %s\n", name);
140 return -1;
141 }
142
143 if (*aead == NULL)
144 return 0;
145
146 return 1;
147}
148
149static int
150run_test_case(const EVP_AEAD* aead, unsigned char bufs[NUM_TYPES][BUF_MAX],
151 const unsigned int lengths[NUM_TYPES], unsigned int line_no)
152{
153 EVP_AEAD_CTX ctx;
154 unsigned char out[BUF_MAX + EVP_AEAD_MAX_TAG_LENGTH], out2[BUF_MAX];
155 size_t out_len, out_len2;
156
157 if (!EVP_AEAD_CTX_init(&ctx, aead, bufs[KEY], lengths[KEY],
158 lengths[TAG], NULL)) {
159 fprintf(stderr, "Failed to init AEAD on line %u\n", line_no);
160 return 0;
161 }
162
163 if (!EVP_AEAD_CTX_seal(&ctx, out, &out_len, sizeof(out), bufs[NONCE],
164 lengths[NONCE], bufs[IN], lengths[IN], bufs[AD], lengths[AD])) {
165 fprintf(stderr, "Failed to run AEAD on line %u\n", line_no);
166 return 0;
167 }
168
169 if (out_len != lengths[CT] + lengths[TAG]) {
170 fprintf(stderr, "Bad output length on line %u: %zu vs %u\n",
171 line_no, out_len, (unsigned)(lengths[CT] + lengths[TAG]));
172 return 0;
173 }
174
175 if (memcmp(out, bufs[CT], lengths[CT]) != 0) {
176 fprintf(stderr, "Bad output on line %u\n", line_no);
177 return 0;
178 }
179
180 if (memcmp(out + lengths[CT], bufs[TAG], lengths[TAG]) != 0) {
181 fprintf(stderr, "Bad tag on line %u\n", line_no);
182 return 0;
183 }
184
185 if (!EVP_AEAD_CTX_open(&ctx, out2, &out_len2, lengths[IN], bufs[NONCE],
186 lengths[NONCE], out, out_len, bufs[AD], lengths[AD])) {
187 fprintf(stderr, "Failed to decrypt on line %u\n", line_no);
188 return 0;
189 }
190
191 if (out_len2 != lengths[IN]) {
192 fprintf(stderr, "Bad decrypt on line %u: %zu\n",
193 line_no, out_len2);
194 return 0;
195 }
196
197 if (memcmp(out2, bufs[IN], out_len2) != 0) {
198 fprintf(stderr, "Plaintext mismatch on line %u\n", line_no);
199 return 0;
200 }
201
202 out[0] ^= 0x80;
203 if (EVP_AEAD_CTX_open(&ctx, out2, &out_len2, lengths[IN], bufs[NONCE],
204 lengths[NONCE], out, out_len, bufs[AD], lengths[AD])) {
205 fprintf(stderr, "Decrypted bad data on line %u\n", line_no);
206 return 0;
207 }
208
209 EVP_AEAD_CTX_cleanup(&ctx);
210 return 1;
211}
212
213int
214main(int argc, char **argv)
215{
216 FILE *f;
217 const EVP_AEAD *aead = NULL;
218 unsigned int line_no = 0, num_tests = 0, j;
219
220 unsigned char bufs[NUM_TYPES][BUF_MAX];
221 unsigned int lengths[NUM_TYPES];
222
223 if (argc != 2) {
224 fprintf(stderr, "%s <test file.txt>\n", argv[0]);
225 return 1;
226 }
227
228 f = fopen(argv[1], "r");
229 if (f == NULL) {
230 perror("failed to open input");
231 return 1;
232 }
233
234 for (j = 0; j < NUM_TYPES; j++)
235 lengths[j] = 0;
236
237 for (;;) {
238 char line[4096];
239 unsigned int i, type_len = 0;
240
241 unsigned char *buf = NULL;
242 unsigned int *buf_len = NULL;
243
244 if (!fgets(line, sizeof(line), f))
245 break;
246
247 line_no++;
248 if (line[0] == '#')
249 continue;
250
251 if (line[0] == '\n' || line[0] == 0) {
252 /* Run a test, if possible. */
253 char any_values_set = 0;
254 for (j = 0; j < NUM_TYPES; j++) {
255 if (lengths[j] != 0) {
256 any_values_set = 1;
257 break;
258 }
259 }
260
261 if (!any_values_set)
262 continue;
263
264 switch (aead_from_name(&aead, bufs[AEAD])) {
265 case 0:
266 fprintf(stderr, "Skipping test...\n");
267 continue;
268 case -1:
269 fprintf(stderr, "Aborting...\n");
270 return 4;
271 }
272
273 if (!run_test_case(aead, bufs, lengths, line_no))
274 return 4;
275
276 for (j = 0; j < NUM_TYPES; j++)
277 lengths[j] = 0;
278
279 num_tests++;
280 continue;
281 }
282
283 /* Each line looks like:
284 * TYPE: 0123abc
285 * Where "TYPE" is the type of the data on the line,
286 * e.g. "KEY". */
287 for (i = 0; line[i] != 0 && line[i] != '\n'; i++) {
288 if (line[i] == ':') {
289 type_len = i;
290 break;
291 }
292 }
293 i++;
294
295 if (type_len == 0) {
296 fprintf(stderr, "Parse error on line %u\n", line_no);
297 return 3;
298 }
299
300 /* After the colon, there's optional whitespace. */
301 for (; line[i] != 0 && line[i] != '\n'; i++) {
302 if (line[i] != ' ' && line[i] != '\t')
303 break;
304 }
305
306 line[type_len] = 0;
307 for (j = 0; j < NUM_TYPES; j++) {
308 if (strcmp(line, NAMES[j]) != 0)
309 continue;
310 if (lengths[j] != 0) {
311 fprintf(stderr, "Duplicate value on line %u\n",
312 line_no);
313 return 3;
314 }
315 buf = bufs[j];
316 buf_len = &lengths[j];
317 break;
318 }
319
320 if (buf == NULL) {
321 fprintf(stderr, "Unknown line type on line %u\n",
322 line_no);
323 return 3;
324 }
325
326 if (j == AEAD) {
327 *buf_len = strlcpy(buf, line + i, BUF_MAX);
328 for (j = 0; j < BUF_MAX; j++) {
329 if (buf[j] == '\n')
330 buf[j] = '\0';
331 }
332 continue;
333 }
334
335 for (j = 0; line[i] != 0 && line[i] != '\n'; i++) {
336 unsigned char v, v2;
337 v = hex_digit(line[i++]);
338 if (line[i] == 0 || line[i] == '\n') {
339 fprintf(stderr, "Odd-length hex data on "
340 "line %u\n", line_no);
341 return 3;
342 }
343 v2 = hex_digit(line[i]);
344 if (v > 15 || v2 > 15) {
345 fprintf(stderr, "Invalid hex char on line %u\n",
346 line_no);
347 return 3;
348 }
349 v <<= 4;
350 v |= v2;
351
352 if (j == BUF_MAX) {
353 fprintf(stderr, "Too much hex data on line %u "
354 "(max is %u bytes)\n",
355 line_no, (unsigned) BUF_MAX);
356 return 3;
357 }
358 buf[j++] = v;
359 *buf_len = *buf_len + 1;
360 }
361 }
362
363 printf("Completed %u test cases\n", num_tests);
364 printf("PASS\n");
365 fclose(f);
366
367 return 0;
368}