summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c
diff options
context:
space:
mode:
authorbeck <>2025-05-19 06:47:40 +0000
committerbeck <>2025-05-19 06:47:40 +0000
commit5a25995ae14e36a5cea71734d202bac849f02727 (patch)
treec0473c2a6778acc8c1c57a2e73ed27b1d35552a6 /src/regress/lib/libcrypto/mlkem/mlkem_unittest.c
parentc2782d1f52bceb30584b71c11631e00eacebcc4e (diff)
downloadopenbsd-5a25995ae14e36a5cea71734d202bac849f02727.tar.gz
openbsd-5a25995ae14e36a5cea71734d202bac849f02727.tar.bz2
openbsd-5a25995ae14e36a5cea71734d202bac849f02727.zip
API changes for ML-KEM
- Get rid of CBB/CBS usage in public api - Make void functions return int that can fail if malloc fails. Along with some fallout and resulting bikeshedding in the regress tests. ok jsing@, tb@
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem_unittest.c')
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem_unittest.c44
1 files changed, 20 insertions, 24 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c b/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c
index 597297b8cc..a1adc88569 100644
--- a/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c
+++ b/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: mlkem_unittest.c,v 1.7 2025/05/03 08:34:55 tb Exp $ */ 1/* $OpenBSD: mlkem_unittest.c,v 1.8 2025/05/19 06:47:40 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2024 Google Inc. 3 * Copyright (c) 2024 Google Inc.
4 * Copyright (c) 2024 Bob Beck <beck@obtuse.com> 4 * Copyright (c) 2024 Bob Beck <beck@obtuse.com>
@@ -42,7 +42,7 @@ struct unittest_ctx {
42 mlkem_parse_private_key_fn parse_private_key; 42 mlkem_parse_private_key_fn parse_private_key;
43 mlkem_parse_public_key_fn parse_public_key; 43 mlkem_parse_public_key_fn parse_public_key;
44 mlkem_encode_private_key_fn encode_private_key; 44 mlkem_encode_private_key_fn encode_private_key;
45 mlkem_encode_public_key_fn encode_public_key; 45 mlkem_marshal_public_key_fn marshal_public_key;
46 mlkem_public_from_private_fn public_from_private; 46 mlkem_public_from_private_fn public_from_private;
47}; 47};
48 48
@@ -54,36 +54,32 @@ MlKemUnitTest(struct unittest_ctx *ctx)
54 uint8_t first_two_bytes[2]; 54 uint8_t first_two_bytes[2];
55 uint8_t *encoded_private_key = NULL, *tmp_buf = NULL; 55 uint8_t *encoded_private_key = NULL, *tmp_buf = NULL;
56 size_t encoded_private_key_len, tmp_buf_len; 56 size_t encoded_private_key_len, tmp_buf_len;
57 CBS cbs;
58 int failed = 0; 57 int failed = 0;
59 58
60 ctx->generate_key(ctx->encoded_public_key, NULL, ctx->priv); 59 if (!ctx->generate_key(ctx->encoded_public_key, NULL, ctx->priv)) {
60 warnx("generate_key failed");
61 failed |= 1;
62 }
61 63
62 memcpy(first_two_bytes, ctx->encoded_public_key, sizeof(first_two_bytes)); 64 memcpy(first_two_bytes, ctx->encoded_public_key, sizeof(first_two_bytes));
63 memset(ctx->encoded_public_key, 0xff, sizeof(first_two_bytes)); 65 memset(ctx->encoded_public_key, 0xff, sizeof(first_two_bytes));
64 66
65 CBS_init(&cbs, ctx->encoded_public_key, ctx->encoded_public_key_len);
66
67 /* Parsing should fail because the first coefficient is >= kPrime. */ 67 /* Parsing should fail because the first coefficient is >= kPrime. */
68 if (ctx->parse_public_key(ctx->pub, &cbs)) { 68 if (ctx->parse_public_key(ctx->pub, ctx->encoded_public_key,
69 ctx->encoded_public_key_len)) {
69 warnx("parse_public_key should have failed"); 70 warnx("parse_public_key should have failed");
70 failed |= 1; 71 failed |= 1;
71 } 72 }
72 73
73 memcpy(ctx->encoded_public_key, first_two_bytes, sizeof(first_two_bytes)); 74 memcpy(ctx->encoded_public_key, first_two_bytes, sizeof(first_two_bytes));
74 CBS_init(&cbs, ctx->encoded_public_key, ctx->encoded_public_key_len); 75 if (!ctx->parse_public_key(ctx->pub, ctx->encoded_public_key,
75 if (!ctx->parse_public_key(ctx->pub, &cbs)) { 76 ctx->encoded_public_key_len)) {
76 warnx("MLKEM768_parse_public_key"); 77 warnx("MLKEM768_parse_public_key");
77 failed |= 1; 78 failed |= 1;
78 } 79 }
79 80
80 if (CBS_len(&cbs) != 0u) { 81 if (!ctx->marshal_public_key(ctx->pub, &tmp_buf, &tmp_buf_len)) {
81 warnx("CBS_len must be 0"); 82 warnx("marshal_public_key");
82 failed |= 1;
83 }
84
85 if (!ctx->encode_public_key(ctx->pub, &tmp_buf, &tmp_buf_len)) {
86 warnx("encode_public_key");
87 failed |= 1; 83 failed |= 1;
88 } 84 }
89 if (ctx->encoded_public_key_len != tmp_buf_len) { 85 if (ctx->encoded_public_key_len != tmp_buf_len) {
@@ -100,8 +96,8 @@ MlKemUnitTest(struct unittest_ctx *ctx)
100 tmp_buf = NULL; 96 tmp_buf = NULL;
101 97
102 ctx->public_from_private(ctx->pub2, ctx->priv); 98 ctx->public_from_private(ctx->pub2, ctx->priv);
103 if (!ctx->encode_public_key(ctx->pub2, &tmp_buf, &tmp_buf_len)) { 99 if (!ctx->marshal_public_key(ctx->pub2, &tmp_buf, &tmp_buf_len)) {
104 warnx("encode_public_key"); 100 warnx("marshal_public_key");
105 failed |= 1; 101 failed |= 1;
106 } 102 }
107 if (ctx->encoded_public_key_len != tmp_buf_len) { 103 if (ctx->encoded_public_key_len != tmp_buf_len) {
@@ -125,18 +121,18 @@ MlKemUnitTest(struct unittest_ctx *ctx)
125 121
126 memcpy(first_two_bytes, encoded_private_key, sizeof(first_two_bytes)); 122 memcpy(first_two_bytes, encoded_private_key, sizeof(first_two_bytes));
127 memset(encoded_private_key, 0xff, sizeof(first_two_bytes)); 123 memset(encoded_private_key, 0xff, sizeof(first_two_bytes));
128 CBS_init(&cbs, encoded_private_key, encoded_private_key_len);
129 124
130 /* Parsing should fail because the first coefficient is >= kPrime. */ 125 /* Parsing should fail because the first coefficient is >= kPrime. */
131 if (ctx->parse_private_key(ctx->priv2, &cbs)) { 126 if (ctx->parse_private_key(ctx->priv2, encoded_private_key,
127 encoded_private_key_len)) {
132 warnx("MLKEM768_parse_private_key should have failed"); 128 warnx("MLKEM768_parse_private_key should have failed");
133 failed |= 1; 129 failed |= 1;
134 } 130 }
135 131
136 memcpy(encoded_private_key, first_two_bytes, sizeof(first_two_bytes)); 132 memcpy(encoded_private_key, first_two_bytes, sizeof(first_two_bytes));
137 CBS_init(&cbs, encoded_private_key, encoded_private_key_len);
138 133
139 if (!ctx->parse_private_key(ctx->priv2, &cbs)) { 134 if (!ctx->parse_private_key(ctx->priv2, encoded_private_key,
135 encoded_private_key_len)) {
140 warnx("MLKEM768_parse_private_key"); 136 warnx("MLKEM768_parse_private_key");
141 failed |= 1; 137 failed |= 1;
142 } 138 }
@@ -210,7 +206,7 @@ mlkem768_unittest(void)
210 .parse_private_key = mlkem768_parse_private_key, 206 .parse_private_key = mlkem768_parse_private_key,
211 .parse_public_key = mlkem768_parse_public_key, 207 .parse_public_key = mlkem768_parse_public_key,
212 .encode_private_key = mlkem768_encode_private_key, 208 .encode_private_key = mlkem768_encode_private_key,
213 .encode_public_key = mlkem768_encode_public_key, 209 .marshal_public_key = mlkem768_marshal_public_key,
214 .public_from_private = mlkem768_public_from_private, 210 .public_from_private = mlkem768_public_from_private,
215 }; 211 };
216 212
@@ -239,7 +235,7 @@ mlkem1024_unittest(void)
239 .parse_private_key = mlkem1024_parse_private_key, 235 .parse_private_key = mlkem1024_parse_private_key,
240 .parse_public_key = mlkem1024_parse_public_key, 236 .parse_public_key = mlkem1024_parse_public_key,
241 .encode_private_key = mlkem1024_encode_private_key, 237 .encode_private_key = mlkem1024_encode_private_key,
242 .encode_public_key = mlkem1024_encode_public_key, 238 .marshal_public_key = mlkem1024_marshal_public_key,
243 .public_from_private = mlkem1024_public_from_private, 239 .public_from_private = mlkem1024_public_from_private,
244 }; 240 };
245 241