summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/mlkem/mlkem1024_decap_tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem1024_decap_tests.c')
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem1024_decap_tests.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem1024_decap_tests.c b/src/regress/lib/libcrypto/mlkem/mlkem1024_decap_tests.c
new file mode 100644
index 0000000000..816bc154ab
--- /dev/null
+++ b/src/regress/lib/libcrypto/mlkem/mlkem1024_decap_tests.c
@@ -0,0 +1,128 @@
1/* Copyright (c) 2024, Google Inc.
2 * Copyright (c) 2024, Bob Beck <beck@obtuse.com>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
15
16#include <err.h>
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20
21#include <openssl/evp.h>
22
23#include "bytestring.h"
24#include "mlkem.h"
25#include "mlkem_internal.h"
26#include "sha3_internal.h"
27#include "mlkem_tests_util.h"
28
29static void
30MlkemDecapFileTest(CBS *c, CBS *k, CBS *dk, int should_fail)
31{
32 uint8_t shared_secret[MLKEM_SHARED_SECRET_BYTES];
33 struct MLKEM1024_private_key priv;
34 int parse_ok, decap_ok;
35
36 parse_ok = MLKEM1024_parse_private_key(&priv, dk);
37 if (!parse_ok) {
38 TEST(!should_fail, "parse_private_key");
39 return;
40 }
41 decap_ok = MLKEM1024_decap(shared_secret, CBS_data(c), CBS_len(c),
42 &priv);
43 if (!decap_ok) {
44 TEST(!should_fail, "decap");
45 return;
46 }
47 TEST_DATAEQ(shared_secret, CBS_data(k),
48 MLKEM_SHARED_SECRET_BYTES, "shared_secret");
49}
50
51#define S_START 0
52#define S_COMMENT 1
53#define S_PRIVATE_KEY 2
54#define S_CIPHERTEXT 3
55#define S_RESULT 4
56#define S_SHARED_SECRET 5
57
58int
59main(int argc, char **argv)
60{
61 CBS ciphertext, shared_secret, private_key;
62 const uint8_t *p = NULL;
63 int should_fail = 0;
64 char *buf;
65 FILE *fp;
66 int state;
67
68 fprintf(stderr, "Testing decap test vectors in %s\n", argv[1]);
69 TEST((fp = fopen(argv[1], "r")) == NULL, "can't open test file");
70 MALLOC(buf, 16*1024);
71 state = S_COMMENT;
72 test_number = 1;
73 while (fgets(buf, 16*1024, fp) != NULL) {
74 switch (state) {
75 case S_START:
76 if (strcmp(buf, "\n") != 0)
77 break;
78 state = S_COMMENT;
79 break;
80 case S_COMMENT:
81 if (strncmp(buf, "#", 1) != 0)
82 break;
83 state = S_PRIVATE_KEY;
84 break;
85 case S_PRIVATE_KEY:
86 if (strncmp(buf, "private_key: ",
87 strlen("private_key: ")) != 0)
88 break;
89 grab_data(&private_key, buf, strlen("private_key: "));
90 p = CBS_data(&private_key);
91 state = S_CIPHERTEXT;
92 break;
93 case S_CIPHERTEXT:
94 if (strncmp(buf, "ciphertext: ",
95 strlen("ciphertext: ")) != 0)
96 break;
97 grab_data(&ciphertext, buf, strlen("ciphertext: "));
98 state = S_RESULT;
99 break;
100 case S_RESULT:
101 if (strncmp(buf, "result: pass",
102 strlen("result: pass")) != 0)
103 should_fail = 1;
104 else
105 should_fail = 0;
106 state = S_SHARED_SECRET;
107 break;
108 case S_SHARED_SECRET:
109 if (strncmp(buf, "shared_secret: ",
110 strlen("shared_secret: ")) != 0)
111 break;
112 grab_data(&shared_secret, buf,
113 strlen("shared_secret: "));
114 MlkemDecapFileTest(&ciphertext, &shared_secret,
115 &private_key, should_fail);
116 free((void *)CBS_data(&ciphertext));
117 free((void *)CBS_data(&shared_secret));
118 free((void *)p);
119
120 test_number++;
121 state = S_START;
122 break;
123 }
124 }
125
126 free(buf);
127 exit(failure);
128}