Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
lcx_mldsa.h
Go to the documentation of this file.
1/*****************************************************************************
2 * (c) 2026 Ledger SAS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
26#ifndef MLDSA_H
27#define MLDSA_H
28
29#include <stdint.h>
30#include <stddef.h>
31#include "cx_errors.h"
32
37#define MLDSA_N 256U
38#define MLDSA_Q 8380417
39#define MLDSA_D 13U
40#define MLDSA_SEEDBYTES 32U
41#define MLDSA_CRHBYTES 64U
42#define MLDSA_TRBYTES 64U
43#define MLDSA_RNDBYTES 32U
45#define MLDSA_POLYT1_PACKEDBYTES 320U
46#define MLDSA_POLYT0_PACKEDBYTES 416U
54#define MLDSA44_K 4U
55#define MLDSA44_L 4U
56#define MLDSA44_ETA 2U
57#define MLDSA44_TAU 39U
58#define MLDSA44_BETA 78U
59#define MLDSA44_GAMMA1 (1 << 17)
60#define MLDSA44_GAMMA2 ((MLDSA_Q - 1) / 88)
61#define MLDSA44_OMEGA 80U
63#define MLDSA44_CTILDEBYTES 32U
64#define MLDSA44_POLYETA_PACKEDBYTES 96U
65#define MLDSA44_POLYZ_PACKEDBYTES 576U
66#define MLDSA44_POLYW1_PACKEDBYTES 192U
68#define MLDSA44_POLYVECH_PACKEDBYTES (MLDSA44_OMEGA + MLDSA44_K)
70#define MLDSA44_PUBLICKEYBYTES \
71 (MLDSA_SEEDBYTES + MLDSA44_K * MLDSA_POLYT1_PACKEDBYTES)
73#define MLDSA44_SECRETKEYBYTES \
74 (2U * MLDSA_SEEDBYTES + MLDSA_TRBYTES + MLDSA44_L * MLDSA44_POLYETA_PACKEDBYTES \
75 + MLDSA44_K * MLDSA44_POLYETA_PACKEDBYTES \
76 + MLDSA44_K * MLDSA_POLYT0_PACKEDBYTES)
78#define MLDSA44_SIGBYTES \
79 (MLDSA44_CTILDEBYTES + MLDSA44_L * MLDSA44_POLYZ_PACKEDBYTES \
80 + MLDSA44_POLYVECH_PACKEDBYTES)
88#define MLDSA65_K 6U
89#define MLDSA65_L 5U
90#define MLDSA65_ETA 4U
91#define MLDSA65_TAU 49U
92#define MLDSA65_BETA 196U
93#define MLDSA65_GAMMA1 (1 << 19)
94#define MLDSA65_GAMMA2 ((MLDSA_Q - 1) / 32)
95#define MLDSA65_OMEGA 55U
97#define MLDSA65_CTILDEBYTES 48U
98#define MLDSA65_POLYETA_PACKEDBYTES 128U
99#define MLDSA65_POLYZ_PACKEDBYTES 640U
100#define MLDSA65_POLYW1_PACKEDBYTES 128U
102#define MLDSA65_POLYVECH_PACKEDBYTES (MLDSA65_OMEGA + MLDSA65_K)
104#define MLDSA65_PUBLICKEYBYTES \
105 (MLDSA_SEEDBYTES + MLDSA65_K * MLDSA_POLYT1_PACKEDBYTES)
107#define MLDSA65_SECRETKEYBYTES \
108 (2U * MLDSA_SEEDBYTES + MLDSA_TRBYTES + MLDSA65_L * MLDSA65_POLYETA_PACKEDBYTES \
109 + MLDSA65_K * MLDSA65_POLYETA_PACKEDBYTES \
110 + MLDSA65_K * MLDSA_POLYT0_PACKEDBYTES)
112#define MLDSA65_SIGBYTES \
113 (MLDSA65_CTILDEBYTES + MLDSA65_L * MLDSA65_POLYZ_PACKEDBYTES \
114 + MLDSA65_POLYVECH_PACKEDBYTES)
118#ifdef HAVE_MLDSA_87
123#define MLDSA87_K 8U
124#define MLDSA87_L 7U
125#define MLDSA87_ETA 2U
126#define MLDSA87_TAU 60U
127#define MLDSA87_BETA 120U
128#define MLDSA87_GAMMA1 (1 << 19)
129#define MLDSA87_GAMMA2 ((MLDSA_Q - 1) / 32)
130#define MLDSA87_OMEGA 75U
132#define MLDSA87_CTILDEBYTES 64U
133#define MLDSA87_POLYETA_PACKEDBYTES 96U
134#define MLDSA87_POLYZ_PACKEDBYTES 640U
135#define MLDSA87_POLYW1_PACKEDBYTES 128U
137#define MLDSA87_POLYVECH_PACKEDBYTES (MLDSA87_OMEGA + MLDSA87_K)
139#define MLDSA87_PUBLICKEYBYTES \
140 (MLDSA_SEEDBYTES + MLDSA87_K * MLDSA_POLYT1_PACKEDBYTES)
142#define MLDSA87_SECRETKEYBYTES \
143 (2U * MLDSA_SEEDBYTES + MLDSA_TRBYTES + MLDSA87_L * MLDSA87_POLYETA_PACKEDBYTES \
144 + MLDSA87_K * MLDSA87_POLYETA_PACKEDBYTES \
145 + MLDSA87_K * MLDSA_POLYT0_PACKEDBYTES)
147#define MLDSA87_SIGBYTES \
148 (MLDSA87_CTILDEBYTES + MLDSA87_L * MLDSA87_POLYZ_PACKEDBYTES \
149 + MLDSA87_POLYVECH_PACKEDBYTES)
152#endif /* HAVE_MLDSA_87 */
153
154// Maximum dimensions across supported parameter sets.
155#ifdef HAVE_MLDSA_87
156#define MLDSA_MAX_K MLDSA87_K
157#define MLDSA_MAX_L MLDSA87_L
158#define MLDSA_NUM_PARAM_SETS 3U
159#else
160#define MLDSA_MAX_K MLDSA65_K
161#define MLDSA_MAX_L MLDSA65_L
162#define MLDSA_NUM_PARAM_SETS 2U
163#endif
164
168typedef enum MLDSA_param_e {
171#ifdef HAVE_MLDSA_87
172 MLDSA_87,
173#endif
175
179typedef struct MLDSA_param_info_s {
180 uint8_t k;
181 uint8_t l;
182 uint8_t eta;
183 uint8_t tau;
184 uint16_t beta;
185 int32_t gamma1;
186 int32_t gamma2;
187 uint8_t omega;
188 uint8_t ctilde_bytes;
193 uint16_t pk_bytes;
194 uint16_t sk_bytes;
195 uint16_t sig_bytes;
197
202
216cx_err_t MLDSA_keygen(uint8_t *pk, size_t pk_len, uint8_t *sk, size_t sk_len, MLDSA_param_t param);
217
234cx_err_t MLDSA_sign(uint8_t *sig,
235 size_t sig_len,
236 size_t *sig_actual_len,
237 const uint8_t *msg,
238 size_t msg_len,
239 const uint8_t *ctx,
240 size_t ctx_len,
241 const uint8_t *sk,
242 size_t sk_len,
243 MLDSA_param_t param);
244
260cx_err_t MLDSA_verify(const uint8_t *sig,
261 size_t sig_len,
262 const uint8_t *msg,
263 size_t msg_len,
264 const uint8_t *ctx,
265 size_t ctx_len,
266 const uint8_t *pk,
267 size_t pk_len,
268 MLDSA_param_t param);
269
275#define MLDSA_PREHASH_OID_LEN 11U
276
289
313cx_err_t MLDSA_sign_prehash(uint8_t *sig,
314 size_t sig_len,
315 size_t *sig_actual_len,
316 const uint8_t *ph,
317 size_t ph_len,
318 const uint8_t *ctx,
319 size_t ctx_len,
320 const uint8_t *sk,
321 size_t sk_len,
322 MLDSA_prehash_t prehash_alg,
323 MLDSA_param_t param);
324
345cx_err_t MLDSA_verify_prehash(const uint8_t *sig,
346 size_t sig_len,
347 const uint8_t *ph,
348 size_t ph_len,
349 const uint8_t *ctx,
350 size_t ctx_len,
351 const uint8_t *pk,
352 size_t pk_len,
353 MLDSA_prehash_t prehash_alg,
354 MLDSA_param_t param);
355
356#endif /* MLDSA_H */
enum MLDSA_prehash_e MLDSA_prehash_t
Hash algorithm selector for HashML-DSA pre-hash signatures.
MLDSA_prehash_e
Hash algorithm selector for HashML-DSA pre-hash signatures.
Definition lcx_mldsa.h:280
@ MLDSA_PREHASH_SHA3_512
Definition lcx_mldsa.h:284
@ MLDSA_PREHASH_SHA512
Definition lcx_mldsa.h:282
@ MLDSA_PREHASH_SHA256
Definition lcx_mldsa.h:281
@ MLDSA_PREHASH_SHA3_256
Definition lcx_mldsa.h:283
@ MLDSA_PREHASH_SHAKE128
Definition lcx_mldsa.h:285
@ MLDSA_NUM_PREHASH_ALGS
Definition lcx_mldsa.h:287
@ MLDSA_PREHASH_SHAKE256
Definition lcx_mldsa.h:286
const MLDSA_param_info_t MLDSA_PARAM[MLDSA_NUM_PARAM_SETS]
Lookup table of ML-DSA parameter sets indexed by MLDSA_param_t.
enum MLDSA_param_e MLDSA_param_t
ML-DSA parameter set selector.
cx_err_t MLDSA_verify_prehash(const uint8_t *sig, size_t sig_len, const uint8_t *ph, size_t ph_len, const uint8_t *ctx, size_t ctx_len, const uint8_t *pk, size_t pk_len, MLDSA_prehash_t prehash_alg, MLDSA_param_t param)
HashML-DSA pre-hash signature verification (FIPS 204, Algorithm 5).
Definition cx_mldsa.c:1370
cx_err_t MLDSA_verify(const uint8_t *sig, size_t sig_len, const uint8_t *msg, size_t msg_len, const uint8_t *ctx, size_t ctx_len, const uint8_t *pk, size_t pk_len, MLDSA_param_t param)
ML-DSA signature verification.
Definition cx_mldsa.c:1315
cx_err_t MLDSA_keygen(uint8_t *pk, size_t pk_len, uint8_t *sk, size_t sk_len, MLDSA_param_t param)
Generates an ML-DSA key pair.
Definition cx_mldsa.c:1258
cx_err_t MLDSA_sign_prehash(uint8_t *sig, size_t sig_len, size_t *sig_actual_len, const uint8_t *ph, size_t ph_len, const uint8_t *ctx, size_t ctx_len, const uint8_t *sk, size_t sk_len, MLDSA_prehash_t prehash_alg, MLDSA_param_t param)
HashML-DSA pre-hash signature generation (FIPS 204, Algorithm 4).
Definition cx_mldsa.c:1338
#define MLDSA_NUM_PARAM_SETS
Definition lcx_mldsa.h:162
cx_err_t MLDSA_sign(uint8_t *sig, size_t sig_len, size_t *sig_actual_len, const uint8_t *msg, size_t msg_len, const uint8_t *ctx, size_t ctx_len, const uint8_t *sk, size_t sk_len, MLDSA_param_t param)
ML-DSA signature generation.
Definition cx_mldsa.c:1284
MLDSA_param_e
ML-DSA parameter set selector.
Definition lcx_mldsa.h:168
@ MLDSA_44
Definition lcx_mldsa.h:169
@ MLDSA_65
Definition lcx_mldsa.h:170
struct MLDSA_param_info_s MLDSA_param_info_t
ML-DSA parameter set descriptor holding all derived sizes.
ML-DSA parameter set descriptor holding all derived sizes.
Definition lcx_mldsa.h:179
uint16_t polyvech_packed_bytes
Definition lcx_mldsa.h:192
uint16_t polyz_packed_bytes
Definition lcx_mldsa.h:190
uint16_t polyeta_packed_bytes
Definition lcx_mldsa.h:189
uint16_t polyw1_packed_bytes
Definition lcx_mldsa.h:191