/src/boringssl/crypto/mldsa/mldsa.cc
Line | Count | Source |
1 | | // Copyright 2024 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/mldsa.h> |
16 | | |
17 | | #include "../fipsmodule/bcm_interface.h" |
18 | | |
19 | | |
20 | | using namespace bssl; |
21 | | |
22 | | int MLDSA65_generate_key( |
23 | | uint8_t out_encoded_public_key[MLDSA65_PUBLIC_KEY_BYTES], |
24 | | uint8_t out_seed[MLDSA_SEED_BYTES], |
25 | 0 | struct MLDSA65_private_key *out_private_key) { |
26 | 0 | return bcm_success(BCM_mldsa65_generate_key(out_encoded_public_key, out_seed, |
27 | 0 | out_private_key)); |
28 | 0 | } |
29 | | |
30 | | int MLDSA65_private_key_from_seed(struct MLDSA65_private_key *out_private_key, |
31 | 0 | const uint8_t *seed, size_t seed_len) { |
32 | 0 | if (seed_len != MLDSA_SEED_BYTES) { |
33 | 0 | return 0; |
34 | 0 | } |
35 | 0 | return bcm_success(BCM_mldsa65_private_key_from_seed(out_private_key, seed)); |
36 | 0 | } |
37 | | |
38 | | int MLDSA65_public_from_private(struct MLDSA65_public_key *out_public_key, |
39 | 0 | const struct MLDSA65_private_key *private_key) { |
40 | 0 | return bcm_success( |
41 | 0 | BCM_mldsa65_public_from_private(out_public_key, private_key)); |
42 | 0 | } |
43 | | |
44 | | int MLDSA65_sign(uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES], |
45 | | const struct MLDSA65_private_key *private_key, |
46 | | const uint8_t *msg, size_t msg_len, const uint8_t *context, |
47 | 0 | size_t context_len) { |
48 | 0 | if (context_len > 255) { |
49 | 0 | return 0; |
50 | 0 | } |
51 | 0 | return bcm_success(BCM_mldsa65_sign(out_encoded_signature, private_key, msg, |
52 | 0 | msg_len, context, context_len)); |
53 | 0 | } |
54 | | |
55 | | int MLDSA65_verify(const struct MLDSA65_public_key *public_key, |
56 | | const uint8_t *signature, size_t signature_len, |
57 | | const uint8_t *msg, size_t msg_len, const uint8_t *context, |
58 | 0 | size_t context_len) { |
59 | 0 | if (context_len > 255 || signature_len != MLDSA65_SIGNATURE_BYTES) { |
60 | 0 | return 0; |
61 | 0 | } |
62 | 0 | return bcm_success(BCM_mldsa65_verify(public_key, signature, msg, msg_len, |
63 | 0 | context, context_len)); |
64 | 0 | } |
65 | | |
66 | | int MLDSA65_prehash_init(struct MLDSA65_prehash *out_state, |
67 | | const struct MLDSA65_public_key *public_key, |
68 | 0 | const uint8_t *context, size_t context_len) { |
69 | 0 | if (context_len > 255) { |
70 | 0 | return 0; |
71 | 0 | } |
72 | 0 | BCM_mldsa65_prehash_init(out_state, public_key, context, context_len); |
73 | 0 | return 1; |
74 | 0 | } |
75 | | |
76 | | void MLDSA65_prehash_update(struct MLDSA65_prehash *inout_state, |
77 | 0 | const uint8_t *msg, size_t msg_len) { |
78 | 0 | BCM_mldsa65_prehash_update(inout_state, msg, msg_len); |
79 | 0 | } |
80 | | |
81 | | void MLDSA65_prehash_finalize(uint8_t out_msg_rep[MLDSA_MU_BYTES], |
82 | 0 | struct MLDSA65_prehash *inout_state) { |
83 | 0 | BCM_mldsa65_prehash_finalize(out_msg_rep, inout_state); |
84 | 0 | } |
85 | | |
86 | | int MLDSA65_sign_message_representative( |
87 | | uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES], |
88 | | const struct MLDSA65_private_key *private_key, |
89 | 0 | const uint8_t msg_rep[MLDSA_MU_BYTES]) { |
90 | 0 | return bcm_success(BCM_mldsa65_sign_message_representative( |
91 | 0 | out_encoded_signature, private_key, msg_rep)); |
92 | 0 | } |
93 | | |
94 | | int MLDSA65_verify_message_representative( |
95 | | const struct MLDSA65_public_key *public_key, |
96 | | const uint8_t *signature, size_t signature_len, |
97 | 0 | const uint8_t msg_rep[MLDSA_MU_BYTES]) { |
98 | 0 | if (signature_len != MLDSA65_SIGNATURE_BYTES) { |
99 | 0 | return 0; |
100 | 0 | } |
101 | 0 | return bcm_success(BCM_mldsa65_verify_message_representative( |
102 | 0 | public_key, signature, msg_rep)); |
103 | 0 | } |
104 | | |
105 | | int MLDSA65_marshal_public_key(CBB *out, |
106 | 0 | const struct MLDSA65_public_key *public_key) { |
107 | 0 | return bcm_success(BCM_mldsa65_marshal_public_key(out, public_key)); |
108 | 0 | } |
109 | | |
110 | 3 | int MLDSA65_parse_public_key(struct MLDSA65_public_key *public_key, CBS *in) { |
111 | 3 | return bcm_success(BCM_mldsa65_parse_public_key(public_key, in)); |
112 | 3 | } |
113 | | |
114 | | int MLDSA87_generate_key( |
115 | | uint8_t out_encoded_public_key[MLDSA87_PUBLIC_KEY_BYTES], |
116 | | uint8_t out_seed[MLDSA_SEED_BYTES], |
117 | 0 | struct MLDSA87_private_key *out_private_key) { |
118 | 0 | return bcm_success(BCM_mldsa87_generate_key(out_encoded_public_key, out_seed, |
119 | 0 | out_private_key)); |
120 | 0 | } |
121 | | |
122 | | int MLDSA87_private_key_from_seed(struct MLDSA87_private_key *out_private_key, |
123 | 0 | const uint8_t *seed, size_t seed_len) { |
124 | 0 | if (seed_len != MLDSA_SEED_BYTES) { |
125 | 0 | return 0; |
126 | 0 | } |
127 | 0 | return bcm_success(BCM_mldsa87_private_key_from_seed(out_private_key, seed)); |
128 | 0 | } |
129 | | |
130 | | int MLDSA87_public_from_private(struct MLDSA87_public_key *out_public_key, |
131 | 0 | const struct MLDSA87_private_key *private_key) { |
132 | 0 | return bcm_success( |
133 | 0 | BCM_mldsa87_public_from_private(out_public_key, private_key)); |
134 | 0 | } |
135 | | |
136 | | int MLDSA87_sign(uint8_t out_encoded_signature[MLDSA87_SIGNATURE_BYTES], |
137 | | const struct MLDSA87_private_key *private_key, |
138 | | const uint8_t *msg, size_t msg_len, const uint8_t *context, |
139 | 0 | size_t context_len) { |
140 | 0 | if (context_len > 255) { |
141 | 0 | return 0; |
142 | 0 | } |
143 | 0 | return bcm_success(BCM_mldsa87_sign(out_encoded_signature, private_key, msg, |
144 | 0 | msg_len, context, context_len)); |
145 | 0 | } |
146 | | |
147 | | int MLDSA87_verify(const struct MLDSA87_public_key *public_key, |
148 | | const uint8_t *signature, size_t signature_len, |
149 | | const uint8_t *msg, size_t msg_len, const uint8_t *context, |
150 | 0 | size_t context_len) { |
151 | 0 | if (context_len > 255 || signature_len != MLDSA87_SIGNATURE_BYTES) { |
152 | 0 | return 0; |
153 | 0 | } |
154 | 0 | return bcm_success(BCM_mldsa87_verify(public_key, signature, msg, msg_len, |
155 | 0 | context, context_len)); |
156 | 0 | } |
157 | | |
158 | | int MLDSA87_prehash_init(struct MLDSA87_prehash *out_state, |
159 | | const struct MLDSA87_public_key *public_key, |
160 | 0 | const uint8_t *context, size_t context_len) { |
161 | 0 | if (context_len > 255) { |
162 | 0 | return 0; |
163 | 0 | } |
164 | 0 | BCM_mldsa87_prehash_init(out_state, public_key, context, context_len); |
165 | 0 | return 1; |
166 | 0 | } |
167 | | |
168 | | void MLDSA87_prehash_update(struct MLDSA87_prehash *inout_state, |
169 | 0 | const uint8_t *msg, size_t msg_len) { |
170 | 0 | BCM_mldsa87_prehash_update(inout_state, msg, msg_len); |
171 | 0 | } |
172 | | |
173 | | void MLDSA87_prehash_finalize(uint8_t out_msg_rep[MLDSA_MU_BYTES], |
174 | 0 | struct MLDSA87_prehash *inout_state) { |
175 | 0 | BCM_mldsa87_prehash_finalize(out_msg_rep, inout_state); |
176 | 0 | } |
177 | | |
178 | | int MLDSA87_sign_message_representative( |
179 | | uint8_t out_encoded_signature[MLDSA87_SIGNATURE_BYTES], |
180 | | const struct MLDSA87_private_key *private_key, |
181 | 0 | const uint8_t msg_rep[MLDSA_MU_BYTES]) { |
182 | 0 | return bcm_success(BCM_mldsa87_sign_message_representative( |
183 | 0 | out_encoded_signature, private_key, msg_rep)); |
184 | 0 | } |
185 | | |
186 | | int MLDSA87_verify_message_representative( |
187 | | const struct MLDSA87_public_key *public_key, |
188 | | const uint8_t *signature, size_t signature_len, |
189 | 0 | const uint8_t msg_rep[MLDSA_MU_BYTES]) { |
190 | 0 | if (signature_len != MLDSA87_SIGNATURE_BYTES) { |
191 | 0 | return 0; |
192 | 0 | } |
193 | 0 | return bcm_success(BCM_mldsa87_verify_message_representative( |
194 | 0 | public_key, signature, msg_rep)); |
195 | 0 | } |
196 | | |
197 | | int MLDSA87_marshal_public_key(CBB *out, |
198 | 0 | const struct MLDSA87_public_key *public_key) { |
199 | 0 | return bcm_success(BCM_mldsa87_marshal_public_key(out, public_key)); |
200 | 0 | } |
201 | | |
202 | 2 | int MLDSA87_parse_public_key(struct MLDSA87_public_key *public_key, CBS *in) { |
203 | 2 | return bcm_success(BCM_mldsa87_parse_public_key(public_key, in)); |
204 | 2 | } |
205 | | |
206 | | int MLDSA44_generate_key( |
207 | | uint8_t out_encoded_public_key[MLDSA44_PUBLIC_KEY_BYTES], |
208 | | uint8_t out_seed[MLDSA_SEED_BYTES], |
209 | 0 | struct MLDSA44_private_key *out_private_key) { |
210 | 0 | return bcm_success(BCM_mldsa44_generate_key(out_encoded_public_key, out_seed, |
211 | 0 | out_private_key)); |
212 | 0 | } |
213 | | |
214 | | int MLDSA44_private_key_from_seed(struct MLDSA44_private_key *out_private_key, |
215 | 0 | const uint8_t *seed, size_t seed_len) { |
216 | 0 | if (seed_len != MLDSA_SEED_BYTES) { |
217 | 0 | return 0; |
218 | 0 | } |
219 | 0 | return bcm_success(BCM_mldsa44_private_key_from_seed(out_private_key, seed)); |
220 | 0 | } |
221 | | |
222 | | int MLDSA44_public_from_private(struct MLDSA44_public_key *out_public_key, |
223 | 0 | const struct MLDSA44_private_key *private_key) { |
224 | 0 | return bcm_success( |
225 | 0 | BCM_mldsa44_public_from_private(out_public_key, private_key)); |
226 | 0 | } |
227 | | |
228 | | int MLDSA44_sign(uint8_t out_encoded_signature[MLDSA44_SIGNATURE_BYTES], |
229 | | const struct MLDSA44_private_key *private_key, |
230 | | const uint8_t *msg, size_t msg_len, const uint8_t *context, |
231 | 0 | size_t context_len) { |
232 | 0 | if (context_len > 255) { |
233 | 0 | return 0; |
234 | 0 | } |
235 | 0 | return bcm_success(BCM_mldsa44_sign(out_encoded_signature, private_key, msg, |
236 | 0 | msg_len, context, context_len)); |
237 | 0 | } |
238 | | |
239 | | int MLDSA44_verify(const struct MLDSA44_public_key *public_key, |
240 | | const uint8_t *signature, size_t signature_len, |
241 | | const uint8_t *msg, size_t msg_len, const uint8_t *context, |
242 | 0 | size_t context_len) { |
243 | 0 | if (context_len > 255 || signature_len != MLDSA44_SIGNATURE_BYTES) { |
244 | 0 | return 0; |
245 | 0 | } |
246 | 0 | return bcm_success(BCM_mldsa44_verify(public_key, signature, msg, msg_len, |
247 | 0 | context, context_len)); |
248 | 0 | } |
249 | | |
250 | | int MLDSA44_prehash_init(struct MLDSA44_prehash *out_state, |
251 | | const struct MLDSA44_public_key *public_key, |
252 | 0 | const uint8_t *context, size_t context_len) { |
253 | 0 | if (context_len > 255) { |
254 | 0 | return 0; |
255 | 0 | } |
256 | 0 | BCM_mldsa44_prehash_init(out_state, public_key, context, context_len); |
257 | 0 | return 1; |
258 | 0 | } |
259 | | |
260 | | void MLDSA44_prehash_update(struct MLDSA44_prehash *inout_state, |
261 | 0 | const uint8_t *msg, size_t msg_len) { |
262 | 0 | BCM_mldsa44_prehash_update(inout_state, msg, msg_len); |
263 | 0 | } |
264 | | |
265 | | void MLDSA44_prehash_finalize(uint8_t out_msg_rep[MLDSA_MU_BYTES], |
266 | 0 | struct MLDSA44_prehash *inout_state) { |
267 | 0 | BCM_mldsa44_prehash_finalize(out_msg_rep, inout_state); |
268 | 0 | } |
269 | | |
270 | | int MLDSA44_sign_message_representative( |
271 | | uint8_t out_encoded_signature[MLDSA44_SIGNATURE_BYTES], |
272 | | const struct MLDSA44_private_key *private_key, |
273 | 0 | const uint8_t msg_rep[MLDSA_MU_BYTES]) { |
274 | 0 | return bcm_success(BCM_mldsa44_sign_message_representative( |
275 | 0 | out_encoded_signature, private_key, msg_rep)); |
276 | 0 | } |
277 | | |
278 | | int MLDSA44_verify_message_representative( |
279 | | const struct MLDSA44_public_key *public_key, |
280 | | const uint8_t *signature, size_t signature_len, |
281 | 0 | const uint8_t msg_rep[MLDSA_MU_BYTES]) { |
282 | 0 | if (signature_len != MLDSA44_SIGNATURE_BYTES) { |
283 | 0 | return 0; |
284 | 0 | } |
285 | 0 | return bcm_success(BCM_mldsa44_verify_message_representative( |
286 | 0 | public_key, signature, msg_rep)); |
287 | 0 | } |
288 | | |
289 | | int MLDSA44_marshal_public_key(CBB *out, |
290 | 0 | const struct MLDSA44_public_key *public_key) { |
291 | 0 | return bcm_success(BCM_mldsa44_marshal_public_key(out, public_key)); |
292 | 0 | } |
293 | | |
294 | 2 | int MLDSA44_parse_public_key(struct MLDSA44_public_key *public_key, CBS *in) { |
295 | 2 | return bcm_success(BCM_mldsa44_parse_public_key(public_key, in)); |
296 | 2 | } |