Skip to content

Commit e2f0e85

Browse files
committed
Composite Sigs: stream and one-shot APIs use full message
The stream and one-shot APIs for composite signatures now operate identically: the caller always provides the full input message. In addition, add the composite signature test cases from the IETF draft to verify the correct implementation. Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent a5badd6 commit e2f0e85

10 files changed

+1609
-205
lines changed

asn1/src/asym_key_dilithium_ed25519.c

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -253,37 +253,15 @@ public_key_dilithium_ed25519_get_data(const uint8_t **data_ptr,
253253
}
254254
}
255255

256-
static int lc_xof_authattr(const struct lc_hash *xof, const uint8_t *in,
257-
size_t inlen, uint8_t *digest, size_t digestlen,
258-
int authattrs_tag)
259-
{
260-
LC_HASH_CTX_ON_STACK(hash_ctx, xof);
261-
int ret;
262-
263-
CKINT(lc_hash_init(hash_ctx));
264-
if (authattrs_tag) {
265-
lc_hash_update(hash_ctx, &lc_pkcs7_authattr_tag,
266-
sizeof(lc_pkcs7_authattr_tag));
267-
}
268-
lc_hash_update(hash_ctx, in, inlen);
269-
CKINT(lc_hash_set_digestsize(hash_ctx, digestlen));
270-
lc_hash_final(hash_ctx, digest);
271-
272-
out:
273-
lc_hash_zero(hash_ctx);
274-
return ret;
275-
}
276-
277256
int public_key_verify_signature_dilithium_ed25519(
278257
const struct lc_public_key *pkey,
279258
const struct lc_public_key_signature *sig)
280259
{
281260
struct workspace {
282261
struct lc_dilithium_ed25519_pk dilithium_pk;
283262
struct lc_dilithium_ed25519_sig dilithium_sig;
284-
uint8_t ph_message[LC_SHA3_512_SIZE_DIGEST];
263+
struct lc_dilithium_ed25519_ctx sign_ctx;
285264
};
286-
const struct lc_hash *hash_algo;
287265
const uint8_t *dilithium_src, *ed25519_src, *data_ptr;
288266
size_t dilithium_src_len, ed25519_src_len, data_len;
289267
int ret, authattrs_tag;
@@ -322,17 +300,22 @@ int public_key_verify_signature_dilithium_ed25519(
322300

323301
printf_debug("Loaded composite signature of size %zu\n", sig->s_size);
324302

325-
CKINT(lc_x509_sig_type_to_hash(sig->pkey_algo, &hash_algo));
326-
/* XOF works as digest size of 64 bytes is same as XOF size */
327-
CKINT(lc_xof_authattr(hash_algo, data_ptr, data_len, ws->ph_message,
328-
sizeof(ws->ph_message), authattrs_tag));
329-
330303
/*
331304
* Verify the signature using Composite-ML-DSA
332305
*/
333-
CKINT(lc_dilithium_ed25519_verify(&ws->dilithium_sig, ws->ph_message,
334-
sizeof(ws->ph_message),
335-
&ws->dilithium_pk));
306+
LC_DILITHIUM_ED25519_SET_CTX(&ws->sign_ctx);
307+
CKINT(lc_dilithium_ed25519_verify_init(&ws->sign_ctx,
308+
&ws->dilithium_pk));
309+
if (authattrs_tag) {
310+
/* Add the authattr tag */
311+
CKINT(lc_dilithium_ed25519_verify_update(
312+
&ws->sign_ctx, &lc_pkcs7_authattr_tag,
313+
sizeof(lc_pkcs7_authattr_tag)));
314+
}
315+
CKINT(lc_dilithium_ed25519_verify_update(&ws->sign_ctx, data_ptr,
316+
data_len));
317+
CKINT(lc_dilithium_ed25519_verify_final(
318+
&ws->dilithium_sig, &ws->sign_ctx, &ws->dilithium_pk));
336319

337320
out:
338321
LC_RELEASE_MEM(ws);
@@ -346,10 +329,8 @@ int public_key_generate_signature_dilithium_ed25519(
346329
{
347330
#ifdef LC_X509_GENERATOR
348331
struct workspace {
349-
uint8_t ph_message[LC_SHA3_512_SIZE_DIGEST];
350332
struct lc_dilithium_ed25519_sig dilithium_ed25519_sig;
351333
};
352-
const struct lc_hash *hash_algo;
353334
struct lc_dilithium_ed25519_sk *dilithium_ed25519_sk =
354335
keys->sk.dilithium_ed25519_sk;
355336
size_t ml_dsa_siglen, ed25519_siglen, data_len;
@@ -361,15 +342,10 @@ int public_key_generate_signature_dilithium_ed25519(
361342
CKINT(public_key_dilithium_ed25519_get_data(&data_ptr, &data_len, NULL,
362343
sig));
363344

364-
CKINT(lc_x509_sig_type_to_hash(sig->pkey_algo, &hash_algo));
365-
/* XOF works as digest size of 64 bytes is same as XOF size */
366-
CKINT(lc_xof(hash_algo, data_ptr, data_len, ws->ph_message,
367-
sizeof(ws->ph_message)));
368-
369345
/* Sign the signature using Composite-ML-DSA */
370-
CKINT(lc_dilithium_ed25519_sign(&ws->dilithium_ed25519_sig,
371-
ws->ph_message, sizeof(ws->ph_message),
372-
dilithium_ed25519_sk, lc_seeded_rng));
346+
CKINT(lc_dilithium_ed25519_sign(&ws->dilithium_ed25519_sig, data_ptr,
347+
data_len, dilithium_ed25519_sk,
348+
lc_seeded_rng));
373349

374350
CKINT(lc_dilithium_ed25519_sig_ptr(&ml_dsa_ptr, &ml_dsa_siglen,
375351
&ed25519_ptr, &ed25519_siglen,

asn1/src/asym_key_dilithium_ed448.c

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -255,37 +255,15 @@ public_key_dilithium_ed448_get_data(const uint8_t **data_ptr, size_t *data_len,
255255
}
256256
}
257257

258-
static int lc_xof_authattr(const struct lc_hash *xof, const uint8_t *in,
259-
size_t inlen, uint8_t *digest, size_t digestlen,
260-
int authattrs_tag)
261-
{
262-
LC_HASH_CTX_ON_STACK(hash_ctx, xof);
263-
int ret;
264-
265-
CKINT(lc_hash_init(hash_ctx));
266-
if (authattrs_tag) {
267-
lc_hash_update(hash_ctx, &lc_pkcs7_authattr_tag,
268-
sizeof(lc_pkcs7_authattr_tag));
269-
}
270-
lc_hash_update(hash_ctx, in, inlen);
271-
CKINT(lc_hash_set_digestsize(hash_ctx, digestlen));
272-
lc_hash_final(hash_ctx, digest);
273-
274-
out:
275-
lc_hash_zero(hash_ctx);
276-
return ret;
277-
}
278-
279258
int public_key_verify_signature_dilithium_ed448(
280259
const struct lc_public_key *pkey,
281260
const struct lc_public_key_signature *sig)
282261
{
283262
struct workspace {
284263
struct lc_dilithium_ed448_pk dilithium_pk;
285264
struct lc_dilithium_ed448_sig dilithium_sig;
286-
uint8_t ph_message[LC_X509_COMP_ED448_MSG_SIZE];
265+
struct lc_dilithium_ed448_ctx sign_ctx;
287266
};
288-
const struct lc_hash *hash_algo;
289267
const uint8_t *dilithium_src, *ed448_src, *data_ptr;
290268
size_t dilithium_src_len, ed448_src_len, data_len;
291269
int ret, authattrs_tag;
@@ -324,17 +302,21 @@ int public_key_verify_signature_dilithium_ed448(
324302

325303
printf_debug("Loaded composite signature of size %zu\n", sig->s_size);
326304

327-
CKINT(lc_x509_sig_type_to_hash(sig->pkey_algo, &hash_algo));
328-
/* XOF works as digest size of 64 bytes is same as XOF size */
329-
CKINT(lc_xof_authattr(hash_algo, data_ptr, data_len, ws->ph_message,
330-
sizeof(ws->ph_message), authattrs_tag));
331-
332305
/*
333306
* Verify the signature using Composite-ML-DSA
334307
*/
335-
CKINT(lc_dilithium_ed448_verify(&ws->dilithium_sig, ws->ph_message,
336-
sizeof(ws->ph_message),
337-
&ws->dilithium_pk));
308+
LC_DILITHIUM_ED448_SET_CTX(&ws->sign_ctx);
309+
CKINT(lc_dilithium_ed448_verify_init(&ws->sign_ctx, &ws->dilithium_pk));
310+
if (authattrs_tag) {
311+
/* Add the authattr tag */
312+
CKINT(lc_dilithium_ed448_verify_update(
313+
&ws->sign_ctx, &lc_pkcs7_authattr_tag,
314+
sizeof(lc_pkcs7_authattr_tag)));
315+
}
316+
CKINT(lc_dilithium_ed448_verify_update(&ws->sign_ctx, data_ptr,
317+
data_len));
318+
CKINT(lc_dilithium_ed448_verify_final(&ws->dilithium_sig, &ws->sign_ctx,
319+
&ws->dilithium_pk));
338320

339321
out:
340322
LC_RELEASE_MEM(ws);
@@ -348,10 +330,8 @@ int public_key_generate_signature_dilithium_ed448(
348330
{
349331
#ifdef LC_X509_GENERATOR
350332
struct workspace {
351-
uint8_t ph_message[LC_X509_COMP_ED448_MSG_SIZE];
352333
struct lc_dilithium_ed448_sig dilithium_ed448_sig;
353334
};
354-
const struct lc_hash *hash_algo;
355335
struct lc_dilithium_ed448_sk *dilithium_ed448_sk =
356336
keys->sk.dilithium_ed448_sk;
357337
size_t ml_dsa_siglen, ed448_siglen, data_len;
@@ -363,15 +343,10 @@ int public_key_generate_signature_dilithium_ed448(
363343
CKINT(public_key_dilithium_ed448_get_data(&data_ptr, &data_len, NULL,
364344
sig));
365345

366-
CKINT(lc_x509_sig_type_to_hash(sig->pkey_algo, &hash_algo));
367-
/* XOF works as digest size of 64 bytes is same as XOF size */
368-
CKINT(lc_xof(hash_algo, data_ptr, data_len, ws->ph_message,
369-
sizeof(ws->ph_message)));
370-
371346
/* Sign the signature using Composite-ML-DSA */
372-
CKINT(lc_dilithium_ed448_sign(&ws->dilithium_ed448_sig, ws->ph_message,
373-
sizeof(ws->ph_message),
374-
dilithium_ed448_sk, lc_seeded_rng));
347+
CKINT(lc_dilithium_ed448_sign(&ws->dilithium_ed448_sig, data_ptr,
348+
data_len, dilithium_ed448_sk,
349+
lc_seeded_rng));
375350

376351
CKINT(lc_dilithium_ed448_sig_ptr(&ml_dsa_ptr, &ml_dsa_siglen,
377352
&ed448_ptr, &ed448_siglen,

asn1/src/base64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ int lc_base64_encode_len(size_t ilen, size_t *olen, enum lc_base64_flags flags);
5151
* @param [in] idata Buffer holding the base64 encoded data
5252
* @param [in] ilen Length of the Base64 data
5353
* @param [out] olen Length of the output data
54+
* @param [out] blank_chars Number of chars for CR and/or LF
5455
* @param [in] flags Flags to shape the operation
5556
*
5657
* @return 0 on success, < 0 on error

0 commit comments

Comments
 (0)