summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/mlkem/mlkem768_encap_tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem768_encap_tests.c')
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem768_encap_tests.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem768_encap_tests.c b/src/regress/lib/libcrypto/mlkem/mlkem768_encap_tests.c
new file mode 100644
index 0000000000..2f6ad896c6
--- /dev/null
+++ b/src/regress/lib/libcrypto/mlkem/mlkem768_encap_tests.c
@@ -0,0 +1,135 @@
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
30MlkemEncapFileTest(CBS *entropy, CBS *public_key, CBS *expected_ciphertext,
31 CBS *expected_shared_secret, int should_fail)
32{
33 uint8_t shared_secret[MLKEM_SHARED_SECRET_BYTES];
34 uint8_t ciphertext[MLKEM768_CIPHERTEXT_BYTES];
35 struct MLKEM768_public_key pub;
36 int parse_ok;
37
38 parse_ok = MLKEM768_parse_public_key(&pub, public_key);
39 if (!parse_ok) {
40 TEST(!should_fail, "parse_public_key");
41 return;
42 }
43 MLKEM768_encap(ciphertext, shared_secret, &pub);
44 TEST_DATAEQ(shared_secret, CBS_data(expected_shared_secret),
45 MLKEM_SHARED_SECRET_BYTES, "shared_secret");
46 TEST_DATAEQ(ciphertext, CBS_data(expected_ciphertext),
47 MLKEM768_CIPHERTEXT_BYTES, "shared_secret");
48}
49
50#define S_START 0
51#define S_COMMENT 1
52#define S_ENTROPY 2
53#define S_PUBLIC_KEY 3
54#define S_RESULT 4
55#define S_CIPHERTEXT 5
56#define S_SHARED_SECRET 6
57
58int
59main(int argc, char **argv)
60{
61 CBS entropy, public_key, ciphertext, shared_secret;
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 encap 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_ENTROPY;
84 break;
85 case S_ENTROPY:
86 if (strncmp(buf, "entropy: ", strlen("entropy: ")) != 0)
87 break;
88 grab_data(&entropy, buf, strlen("entropy: "));
89 p = CBS_data(&entropy);
90 state = S_PUBLIC_KEY;
91 break;
92 case S_PUBLIC_KEY:
93 if (strncmp(buf, "public_key: ",
94 strlen("public_key: ")) != 0)
95 break;
96 grab_data(&public_key, buf, strlen("public_key: "));
97 p = CBS_data(&public_key);
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_CIPHERTEXT;
107 break;
108 case S_CIPHERTEXT:
109 if (strncmp(buf, "ciphertext: ",
110 strlen("ciphertext: ")) != 0)
111 break;
112 grab_data(&ciphertext, buf, strlen("ciphertext: "));
113 state = S_RESULT;
114 break;
115 case S_SHARED_SECRET:
116 if (strncmp(buf, "shared_secret: ",
117 strlen("shared_secret: ")) != 0)
118 break;
119 grab_data(&shared_secret, buf,
120 strlen("shared_secret: "));
121 MlkemEncapFileTest(&entropy, &public_key, &ciphertext,
122 &shared_secret, should_fail);
123 free((void *)CBS_data(&ciphertext));
124 free((void *)CBS_data(&shared_secret));
125 free((void *)p);
126
127 test_number++;
128 state = S_START;
129 break;
130 }
131 }
132
133 free(buf);
134 exit(failure);
135}