Skip to content

Commit 8a3f87b

Browse files
committed
API doc: ML-DSA, ML-KEM
1 parent c71a4dd commit 8a3f87b

File tree

2 files changed

+775
-0
lines changed

2 files changed

+775
-0
lines changed
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
/*!
2+
\ingroup MLDSA
3+
4+
\brief This macro initializes a provided MlDsaKey structure.
5+
6+
MlDsaKey is an alias of dilithium_key. This macro maps to
7+
wc_dilithium_init_ex().
8+
9+
\return 0 Returned upon successfully initializing the key structure
10+
\return BAD_FUNC_ARGS Returned if the key pointer evaluates to NULL
11+
\return MEMORY_E Returned if memory allocation fails (when applicable)
12+
13+
\param key pointer to the MlDsaKey structure to initialize
14+
\param heap pointer to a heap identifier, for use with memory overrides
15+
\param devId ID to use with crypto callbacks or async hardware.
16+
Set to INVALID_DEVID (-2) if not used.
17+
18+
_Example_
19+
\code
20+
MlDsaKey key;
21+
wc_MlDsaKey_Init(&key, NULL, INVALID_DEVID);
22+
\endcode
23+
24+
\sa wc_MlDsaKey_Free
25+
\sa wc_MlDsaKey_SetParams
26+
*/
27+
#define wc_MlDsaKey_Init(key, heap, devId) wc_dilithium_init_ex(key, heap, devId)
28+
29+
/*!
30+
\ingroup MLDSA
31+
32+
\brief This macro sets the ML-DSA parameter set on an initialized MlDsaKey.
33+
34+
Supported parameter identifiers include:
35+
- WC_ML_DSA_44
36+
- WC_ML_DSA_65
37+
- WC_ML_DSA_87
38+
(Draft variants may also be available depending on build.)
39+
40+
This macro maps to wc_dilithium_set_level().
41+
42+
\return 0 Returned upon success
43+
\return BAD_FUNC_ARGS Returned if arguments are invalid (implementation dependent)
44+
45+
\param key pointer to the initialized MlDsaKey structure
46+
\param id parameter set identifier (e.g., WC_ML_DSA_44 / _65 / _87)
47+
48+
_Example_
49+
\code
50+
wc_MlDsaKey_SetParams(&key, WC_ML_DSA_65);
51+
\endcode
52+
53+
\sa wc_MlDsaKey_GetParams
54+
\sa wc_MlDsaKey_MakeKey
55+
*/
56+
#define wc_MlDsaKey_SetParams(key, id) wc_dilithium_set_level(key, id)
57+
58+
/*!
59+
\ingroup MLDSA
60+
61+
\brief This macro gets the ML-DSA parameter set configured in the key.
62+
63+
This macro maps to wc_dilithium_get_level().
64+
65+
\return 0 Returned upon success
66+
\return BAD_FUNC_ARGS Returned if arguments are invalid (implementation dependent)
67+
68+
\param key pointer to the initialized MlDsaKey structure
69+
\param id output pointer receiving the current parameter set identifier
70+
71+
\sa wc_MlDsaKey_SetParams
72+
*/
73+
#define wc_MlDsaKey_GetParams(key, id) wc_dilithium_get_level(key, id)
74+
75+
/*!
76+
\ingroup MLDSA
77+
78+
\brief This macro frees resources associated with an MlDsaKey structure.
79+
80+
This macro maps to wc_dilithium_free().
81+
82+
\return 0 Returned upon success (implementation dependent)
83+
\return BAD_FUNC_ARGS Returned if key is NULL (implementation dependent)
84+
85+
\param key pointer to the MlDsaKey structure to free
86+
87+
\sa wc_MlDsaKey_Init
88+
*/
89+
#define wc_MlDsaKey_Free(key) wc_dilithium_free(key)
90+
/*!
91+
\ingroup MLDSA
92+
93+
\brief This macro generates an ML-DSA public/private key pair.
94+
95+
The key must be initialized and have parameters set prior to calling.
96+
This macro maps to wc_dilithium_make_key().
97+
98+
\return 0 Returned upon success
99+
\return BAD_FUNC_ARGS Returned if key or rng is NULL
100+
\return RNG_FAILURE_E Returned if RNG fails (when applicable)
101+
102+
\param key pointer to the initialized MlDsaKey structure
103+
\param rng pointer to an initialized WC_RNG structure
104+
105+
\sa wc_MlDsaKey_Sign
106+
\sa wc_MlDsaKey_Verify
107+
*/
108+
#define wc_MlDsaKey_MakeKey(key, rng) wc_dilithium_make_key(key, rng)
109+
110+
/*!
111+
\ingroup MLDSA
112+
113+
\brief This macro exports the private key in raw (algorithm-specific) format.
114+
115+
This macro maps to wc_dilithium_export_private_only().
116+
117+
\return 0 Returned upon success
118+
\return BAD_FUNC_ARGS Returned if arguments are invalid (implementation dependent)
119+
\return BUFFER_E Returned if output buffer is too small (when applicable)
120+
121+
\param key pointer to the MlDsaKey structure containing a private key
122+
\param out output buffer for raw private key
123+
\param outLen in/out: on input, size of out; on output, bytes written (implementation dependent)
124+
125+
\sa wc_MlDsaKey_GetPrivLen
126+
\sa wc_MlDsaKey_ImportPrivRaw
127+
*/
128+
#define wc_MlDsaKey_ExportPrivRaw(key, out, outLen) \
129+
wc_dilithium_export_private_only(key, out, outLen)
130+
131+
/*!
132+
\ingroup MLDSA
133+
134+
\brief This macro imports the private key from raw (algorithm-specific) format.
135+
136+
This macro maps to wc_dilithium_import_private_only().
137+
138+
\return 0 Returned upon success
139+
\return BAD_FUNC_ARGS Returned if arguments are invalid (implementation dependent)
140+
\return BUFFER_E Returned if input length is invalid (when applicable)
141+
142+
\param key pointer to the MlDsaKey structure to receive the private key
143+
\param in input buffer containing raw private key
144+
\param inLen length of input in bytes
145+
146+
\sa wc_MlDsaKey_ExportPrivRaw
147+
*/
148+
#define wc_MlDsaKey_ImportPrivRaw(key, in, inLen) \
149+
wc_dilithium_import_private_only(in, inLen, key)
150+
151+
/*!
152+
\ingroup MLDSA
153+
154+
\brief This macro exports the public key in raw (algorithm-specific) format.
155+
156+
This macro maps to wc_dilithium_export_public().
157+
158+
\return 0 Returned upon success
159+
\return BAD_FUNC_ARGS Returned if arguments are invalid (implementation dependent)
160+
\return BUFFER_E Returned if output buffer is too small (when applicable)
161+
162+
\param key pointer to the MlDsaKey structure containing a public key
163+
\param out output buffer for raw public key
164+
\param outLen in/out: on input, size of out; on output, bytes written (implementation dependent)
165+
166+
\sa wc_MlDsaKey_GetPubLen
167+
\sa wc_MlDsaKey_ImportPubRaw
168+
*/
169+
#define wc_MlDsaKey_ExportPubRaw(key, out, outLen) \
170+
wc_dilithium_export_public(key, out, outLen)
171+
172+
/*!
173+
\ingroup MLDSA
174+
175+
\brief This macro imports the public key from raw (algorithm-specific) format.
176+
177+
This macro maps to wc_dilithium_import_public().
178+
179+
\return 0 Returned upon success
180+
\return BAD_FUNC_ARGS Returned if arguments are invalid (implementation dependent)
181+
\return BUFFER_E Returned if input length is invalid (when applicable)
182+
183+
\param key pointer to the MlDsaKey structure to receive the public key
184+
\param in input buffer containing raw public key
185+
\param inLen length of input in bytes
186+
187+
\sa wc_MlDsaKey_ExportPubRaw
188+
*/
189+
#define wc_MlDsaKey_ImportPubRaw(key, in, inLen) \
190+
wc_dilithium_import_public(in, inLen, key)
191+
/*!
192+
\ingroup MLDSA
193+
194+
\brief This macro signs a message using an ML-DSA private key.
195+
196+
This macro maps to wc_dilithium_sign_msg().
197+
Note the argument order: (msg,msgSz) are provided after (sig,sigSz).
198+
199+
\return 0 Returned upon success
200+
\return BAD_FUNC_ARGS Returned if arguments are invalid (implementation dependent)
201+
\return BUFFER_E Returned if the signature buffer is too small (when applicable)
202+
\return RNG_FAILURE_E Returned if RNG fails (when applicable)
203+
204+
\param key pointer to the MlDsaKey structure containing a private key
205+
\param sig output buffer for signature
206+
\param sigSz in/out: on input, size of sig; on output, signature length written
207+
\param msg pointer to message buffer to sign
208+
\param msgSz size of message in bytes
209+
\param rng pointer to an initialized WC_RNG structure
210+
211+
_Example_
212+
\code
213+
byte sigBuf[DILITHIUM_ML_DSA_44_SIG_SIZE];
214+
word32 sigSz = sizeof(sigBuf);
215+
216+
wc_MlDsaKey_Sign(&key, sigBuf, &sigSz, msg, msgSz, &rng);
217+
\endcode
218+
219+
\sa wc_MlDsaKey_Verify
220+
\sa wc_MlDsaKey_GetSigLen
221+
*/
222+
#define wc_MlDsaKey_Sign(key, sig, sigSz, msg, msgSz, rng) \
223+
wc_dilithium_sign_msg(msg, msgSz, sig, sigSz, key, rng)
224+
225+
/*!
226+
\ingroup MLDSA
227+
228+
\brief This macro verifies an ML-DSA signature using an ML-DSA public key.
229+
230+
This macro maps to wc_dilithium_verify_msg().
231+
232+
The verification result is written to \p res.
233+
234+
\return 0 Returned upon success (verification executed)
235+
\return BAD_FUNC_ARGS Returned if arguments are invalid (implementation dependent)
236+
237+
\param key pointer to the MlDsaKey structure containing a public key
238+
\param sig pointer to signature buffer
239+
\param sigSz size of signature in bytes
240+
\param msg pointer to message buffer to verify
241+
\param msgSz size of message in bytes
242+
\param res output: verification result (typically 1 = valid, 0 = invalid)
243+
244+
_Example_
245+
\code
246+
int verified = 0;
247+
wc_MlDsaKey_Verify(&pub, sigBuf, sigSz, msg, msgSz, &verified);
248+
if (verified != 1) {
249+
// invalid
250+
}
251+
\endcode
252+
253+
\sa wc_MlDsaKey_Sign
254+
*/
255+
#define wc_MlDsaKey_Verify(key, sig, sigSz, msg, msgSz, res) \
256+
wc_dilithium_verify_msg(sig, sigSz, msg, msgSz, res, key)
257+
/*!
258+
\ingroup MLDSA
259+
260+
\brief This macro exports the ML-DSA public key to DER format.
261+
262+
This macro maps to wc_Dilithium_PublicKeyToDer().
263+
264+
\return bytes Returned as the number of bytes written upon success (> 0)
265+
\return negative error code Returned upon failure
266+
267+
\param key pointer to MlDsaKey
268+
\param output output buffer for DER
269+
\param len size of output buffer in bytes
270+
\param withAlg non-zero to include AlgorithmIdentifier when supported
271+
272+
\sa wc_MlDsaKey_PrivateKeyToDer
273+
*/
274+
#define wc_MlDsaKey_PublicKeyToDer(key, output, len, withAlg) \
275+
wc_Dilithium_PublicKeyToDer(key, output, len, withAlg)
276+
277+
/*!
278+
\ingroup MLDSA
279+
280+
\brief This macro exports the ML-DSA private key to DER format.
281+
282+
This macro maps to wc_Dilithium_PrivateKeyToDer().
283+
284+
\return bytes Returned as the number of bytes written upon success (> 0)
285+
\return negative error code Returned upon failure
286+
287+
\param key pointer to MlDsaKey
288+
\param output output buffer for DER
289+
\param len size of output buffer in bytes
290+
291+
\sa wc_MlDsaKey_PublicKeyToDer
292+
*/
293+
#define wc_MlDsaKey_PrivateKeyToDer(key, output, len) \
294+
wc_Dilithium_PrivateKeyToDer(key, output, len)
295+
/*!
296+
\ingroup MLDSA
297+
298+
\brief This function returns the raw private key length in bytes for the configured parameter set.
299+
300+
\return 0 Returned upon success
301+
\return BAD_FUNC_ARGS Returned if key or len is NULL
302+
303+
\param key pointer to an initialized MlDsaKey structure
304+
\param len output pointer receiving private key length in bytes
305+
306+
\sa wc_MlDsaKey_ExportPrivRaw
307+
*/
308+
WOLFSSL_API int wc_MlDsaKey_GetPrivLen(MlDsaKey *key, int *len);
309+
310+
/*!
311+
\ingroup MLDSA
312+
313+
\brief This function returns the raw public key length in bytes for the configured parameter set.
314+
315+
\return 0 Returned upon success
316+
\return BAD_FUNC_ARGS Returned if key or len is NULL
317+
318+
\param key pointer to an initialized MlDsaKey structure
319+
\param len output pointer receiving public key length in bytes
320+
321+
\sa wc_MlDsaKey_ExportPubRaw
322+
*/
323+
WOLFSSL_API int wc_MlDsaKey_GetPubLen(MlDsaKey *key, int *len);
324+
325+
/*!
326+
\ingroup MLDSA
327+
328+
\brief This function returns the signature length in bytes for the configured parameter set.
329+
330+
\return 0 Returned upon success
331+
\return BAD_FUNC_ARGS Returned if key or len is NULL
332+
333+
\param key pointer to an initialized MlDsaKey structure
334+
\param len output pointer receiving signature length in bytes
335+
336+
\sa wc_MlDsaKey_Sign
337+
*/
338+
WOLFSSL_API int wc_MlDsaKey_GetSigLen(MlDsaKey *key, int *len);

0 commit comments

Comments
 (0)