summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbeck <>2025-05-19 07:53:00 +0000
committerbeck <>2025-05-19 07:53:00 +0000
commit41bd0848d38d40c872dd36e17a728b405acff4dc (patch)
tree181bb3be5116b982cf5bf212a96fd0a8c8753918
parent574636afc34a257a07ceb9fe84b926fa3c45fd04 (diff)
downloadopenbsd-41bd0848d38d40c872dd36e17a728b405acff4dc.tar.gz
openbsd-41bd0848d38d40c872dd36e17a728b405acff4dc.tar.bz2
openbsd-41bd0848d38d40c872dd36e17a728b405acff4dc.zip
Fix up MLKEM768_marshal_private_key to not use a passed in CBB
Even though this should remain internal, make it the same as the public key marshal function, and make the needed fallout changes in regress. This does not yet do the bikeshed of renaming the structure field in the regress ctx, that will wait until a follow on to convert 1024 in a similar manner ok tb@
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/mlkem/mlkem768.c64
-rw-r--r--src/lib/libcrypto/mlkem/mlkem_internal.h6
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c4
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem_tests.c4
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c21
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h4
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem_unittest.c4
7 files changed, 54 insertions, 53 deletions
diff --git a/src/lib/libcrypto/mlkem/mlkem768.c b/src/lib/libcrypto/mlkem/mlkem768.c
index b20545defc..82adea7b42 100644
--- a/src/lib/libcrypto/mlkem/mlkem768.c
+++ b/src/lib/libcrypto/mlkem/mlkem768.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: mlkem768.c,v 1.10 2025/05/19 07:40:17 beck Exp $ */ 1/* $OpenBSD: mlkem768.c,v 1.11 2025/05/19 07:53:00 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>
@@ -611,6 +611,19 @@ vector_encode(uint8_t *out, const vector *a, int bits)
611 } 611 }
612} 612}
613 613
614/* Encodes an entire vector as above, but adding it to a CBB */
615static int
616vector_encode_cbb(CBB *cbb, const vector *a, int bits)
617{
618 uint8_t *encoded_vector;
619
620 if (!CBB_add_space(cbb, &encoded_vector, kEncodedVectorSize))
621 return 0;
622 vector_encode(encoded_vector, a, bits);
623
624 return 1;
625}
626
614/* 627/*
615 * scalar_decode parses |DEGREE * bits| bits from |in| into |DEGREE| values in 628 * scalar_decode parses |DEGREE * bits| bits from |in| into |DEGREE| values in
616 * |out|. It returns one on success and zero if any parsed value is >= 629 * |out|. It returns one on success and zero if any parsed value is >=
@@ -850,16 +863,9 @@ LCRYPTO_ALIAS(MLKEM768_private_key_from_seed);
850static int 863static int
851mlkem_marshal_public_key(CBB *out, const struct public_key *pub) 864mlkem_marshal_public_key(CBB *out, const struct public_key *pub)
852{ 865{
853 uint8_t *vector_output; 866 if (!vector_encode_cbb(out, &pub->t, kLog2Prime))
854
855 if (!CBB_add_space(out, &vector_output, kEncodedVectorSize)) {
856 return 0; 867 return 0;
857 } 868 return CBB_add_bytes(out, pub->rho, sizeof(pub->rho));
858 vector_encode(vector_output, &pub->t, kLog2Prime);
859 if (!CBB_add_bytes(out, pub->rho, sizeof(pub->rho))) {
860 return 0;
861 }
862 return 1;
863} 869}
864 870
865int 871int
@@ -1117,27 +1123,37 @@ MLKEM768_parse_public_key(struct MLKEM768_public_key *public_key,
1117LCRYPTO_ALIAS(MLKEM768_parse_public_key); 1123LCRYPTO_ALIAS(MLKEM768_parse_public_key);
1118 1124
1119int 1125int
1120MLKEM768_marshal_private_key(CBB *out, 1126MLKEM768_marshal_private_key(const struct MLKEM768_private_key *private_key,
1121 const struct MLKEM768_private_key *private_key) 1127 uint8_t **out_private_key, size_t *out_private_key_len)
1122{ 1128{
1123 const struct private_key *const priv = private_key_768_from_external( 1129 const struct private_key *const priv = private_key_768_from_external(
1124 private_key); 1130 private_key);
1125 uint8_t *s_output; 1131 CBB cbb;
1132 int ret = 0;
1126 1133
1127 if (!CBB_add_space(out, &s_output, kEncodedVectorSize)) { 1134 if (!CBB_init(&cbb, MLKEM768_PRIVATE_KEY_BYTES))
1128 return 0; 1135 goto err;
1129 } 1136
1130 vector_encode(s_output, &priv->s, kLog2Prime); 1137 if (!vector_encode_cbb(&cbb, &priv->s, kLog2Prime))
1131 if (!mlkem_marshal_public_key(out, &priv->pub)) 1138 goto err;
1132 return 0; 1139 if (!mlkem_marshal_public_key(&cbb, &priv->pub))
1133 if (!CBB_add_bytes(out, priv->pub.public_key_hash, 1140 goto err;
1141 if (!CBB_add_bytes(&cbb, priv->pub.public_key_hash,
1134 sizeof(priv->pub.public_key_hash))) 1142 sizeof(priv->pub.public_key_hash)))
1135 return 0; 1143 goto err;
1136 if (!CBB_add_bytes(out, priv->fo_failure_secret, 1144 if (!CBB_add_bytes(&cbb, priv->fo_failure_secret,
1137 sizeof(priv->fo_failure_secret))) 1145 sizeof(priv->fo_failure_secret)))
1138 return 0; 1146 goto err;
1139 1147
1140 return 1; 1148 if (!CBB_finish(&cbb, out_private_key, out_private_key_len))
1149 goto err;
1150
1151 ret = 1;
1152
1153 err:
1154 CBB_cleanup(&cbb);
1155
1156 return ret;
1141} 1157}
1142 1158
1143int 1159int
diff --git a/src/lib/libcrypto/mlkem/mlkem_internal.h b/src/lib/libcrypto/mlkem/mlkem_internal.h
index 7a51197c36..aed051e980 100644
--- a/src/lib/libcrypto/mlkem/mlkem_internal.h
+++ b/src/lib/libcrypto/mlkem/mlkem_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: mlkem_internal.h,v 1.5 2025/05/19 06:47:40 beck Exp $ */ 1/* $OpenBSD: mlkem_internal.h,v 1.6 2025/05/19 07:53:00 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2023, Google Inc. 3 * Copyright (c) 2023, Google Inc.
4 * 4 *
@@ -57,8 +57,8 @@ int MLKEM768_generate_key_external_entropy(
57 * format for ML-KEM private keys. It returns one on success or zero on 57 * format for ML-KEM private keys. It returns one on success or zero on
58 * allocation error. 58 * allocation error.
59 */ 59 */
60int MLKEM768_marshal_private_key(CBB *out, 60int MLKEM768_marshal_private_key(const struct MLKEM768_private_key *private_key,
61 const struct MLKEM768_private_key *private_key); 61 uint8_t **out_private_key, size_t *out_private_key_len);
62 62
63/* 63/*
64 * MLKEM_encap_external_entropy behaves like |MLKEM_encap|, but uses 64 * MLKEM_encap_external_entropy behaves like |MLKEM_encap|, but uses
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c b/src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c
index a8495f55e3..e0fd9ca241 100644
--- a/src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c
+++ b/src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: mlkem_iteration_tests.c,v 1.3 2025/05/19 06:47:40 beck Exp $ */ 1/* $OpenBSD: mlkem_iteration_tests.c,v 1.4 2025/05/19 07:53:00 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>
@@ -185,7 +185,7 @@ main(void)
185 .priv = &priv768, 185 .priv = &priv768,
186 .pub = &pub768, 186 .pub = &pub768,
187 .encap_external_entropy = mlkem768_encap_external_entropy, 187 .encap_external_entropy = mlkem768_encap_external_entropy,
188 .encode_private_key = mlkem768_encode_private_key, 188 .encode_private_key = mlkem768_marshal_private_key,
189 .generate_key_external_entropy = 189 .generate_key_external_entropy =
190 mlkem768_generate_key_external_entropy, 190 mlkem768_generate_key_external_entropy,
191 .public_from_private = mlkem768_public_from_private, 191 .public_from_private = mlkem768_public_from_private,
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests.c b/src/regress/lib/libcrypto/mlkem/mlkem_tests.c
index a4e7208c76..84b71aebb0 100644
--- a/src/regress/lib/libcrypto/mlkem/mlkem_tests.c
+++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: mlkem_tests.c,v 1.4 2025/05/19 06:47:40 beck Exp $ */ 1/* $OpenBSD: mlkem_tests.c,v 1.5 2025/05/19 07:53:00 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2024 Google Inc. 3 * Copyright (c) 2024 Google Inc.
4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org>
@@ -662,7 +662,7 @@ mlkem_keygen_tests(const char *fn, size_t size, enum test_type test_type)
662 .generate_key_external_entropy = 662 .generate_key_external_entropy =
663 mlkem768_generate_key_external_entropy, 663 mlkem768_generate_key_external_entropy,
664 .encode_private_key = 664 .encode_private_key =
665 mlkem768_encode_private_key, 665 mlkem768_marshal_private_key,
666 }; 666 };
667 struct MLKEM1024_private_key private_key1024; 667 struct MLKEM1024_private_key private_key1024;
668 uint8_t encoded_public_key1024[MLKEM1024_PUBLIC_KEY_BYTES]; 668 uint8_t encoded_public_key1024[MLKEM1024_PUBLIC_KEY_BYTES];
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c
index 8677713c8e..5ec8c08585 100644
--- a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c
+++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: mlkem_tests_util.c,v 1.6 2025/05/19 06:47:40 beck Exp $ */ 1/* $OpenBSD: mlkem_tests_util.c,v 1.7 2025/05/19 07:53:00 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>
@@ -61,25 +61,10 @@ compare_data(const uint8_t *want, const uint8_t *got, size_t len, const char *ms
61} 61}
62 62
63int 63int
64mlkem768_encode_private_key(const void *private_key, uint8_t **out_buf, 64mlkem768_marshal_private_key(const void *private_key, uint8_t **out_buf,
65 size_t *out_len) 65 size_t *out_len)
66{ 66{
67 CBB cbb; 67 return MLKEM768_marshal_private_key(private_key, out_buf, out_len);
68 int ret = 0;
69
70 if (!CBB_init(&cbb, MLKEM768_PUBLIC_KEY_BYTES))
71 goto err;
72 if (!MLKEM768_marshal_private_key(&cbb, private_key))
73 goto err;
74 if (!CBB_finish(&cbb, out_buf, out_len))
75 goto err;
76
77 ret = 1;
78
79 err:
80 CBB_cleanup(&cbb);
81
82 return ret;
83} 68}
84 69
85int 70int
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h
index a3b255082f..5c2c400ea5 100644
--- a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h
+++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: mlkem_tests_util.h,v 1.5 2025/05/19 06:47:40 beck Exp $ */ 1/* $OpenBSD: mlkem_tests_util.h,v 1.6 2025/05/19 07:53:00 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2024 Bob Beck <beck@obtuse.com> 3 * Copyright (c) 2024 Bob Beck <beck@obtuse.com>
4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org>
@@ -30,7 +30,7 @@
30int compare_data(const uint8_t *want, const uint8_t *got, size_t len, 30int compare_data(const uint8_t *want, const uint8_t *got, size_t len,
31 const char *msg); 31 const char *msg);
32 32
33int mlkem768_encode_private_key(const void *priv, uint8_t **out_buf, 33int mlkem768_marshal_private_key(const void *priv, uint8_t **out_buf,
34 size_t *out_len); 34 size_t *out_len);
35int mlkem768_marshal_public_key(const void *pub, uint8_t **out_buf, 35int mlkem768_marshal_public_key(const void *pub, uint8_t **out_buf,
36 size_t *out_len); 36 size_t *out_len);
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c b/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c
index a1adc88569..ce1e797904 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.8 2025/05/19 06:47:40 beck Exp $ */ 1/* $OpenBSD: mlkem_unittest.c,v 1.9 2025/05/19 07:53:00 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>
@@ -205,7 +205,7 @@ mlkem768_unittest(void)
205 .generate_key = mlkem768_generate_key, 205 .generate_key = mlkem768_generate_key,
206 .parse_private_key = mlkem768_parse_private_key, 206 .parse_private_key = mlkem768_parse_private_key,
207 .parse_public_key = mlkem768_parse_public_key, 207 .parse_public_key = mlkem768_parse_public_key,
208 .encode_private_key = mlkem768_encode_private_key, 208 .encode_private_key = mlkem768_marshal_private_key,
209 .marshal_public_key = mlkem768_marshal_public_key, 209 .marshal_public_key = mlkem768_marshal_public_key,
210 .public_from_private = mlkem768_public_from_private, 210 .public_from_private = mlkem768_public_from_private,
211 }; 211 };