Skip to content

Commit ec358a2

Browse files
committed
HashML-DSA: REplace enum by define to pick pre-hash function
Currently we have an enum for the 12 different pre-hash functions for HashML-DSA. This leads to problems in multi-level builds as we must only define the enum once. Currently we work around this by guarding the enum definition with a pre-processor conditional. However, there is also a (theoretical) concern about the type of the enum being implementation-defined in C90: #537 (comment) It seems cleaner to not use an enum here, but instead use #defines avoiding all the above problems. This commit implements that change. We also eliminate the camel case hashAlg - that was inconsistent with the remaining code base from the start. Resolves #591 Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 44ab709 commit ec358a2

File tree

9 files changed

+143
-128
lines changed

9 files changed

+143
-128
lines changed

mldsa/mldsa_native.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,18 @@
163163
#undef MLD_API_MUST_CHECK_RETURN_VALUE
164164
#undef MLD_API_NAMESPACE
165165
#undef MLD_H
166-
#undef MLD_PRE_HASH_ENUM
166+
#undef MLD_PREHASH_SHA2_224
167+
#undef MLD_PREHASH_SHA2_256
168+
#undef MLD_PREHASH_SHA2_384
169+
#undef MLD_PREHASH_SHA2_512
170+
#undef MLD_PREHASH_SHA2_512_224
171+
#undef MLD_PREHASH_SHA2_512_256
172+
#undef MLD_PREHASH_SHA3_224
173+
#undef MLD_PREHASH_SHA3_256
174+
#undef MLD_PREHASH_SHA3_384
175+
#undef MLD_PREHASH_SHA3_512
176+
#undef MLD_PREHASH_SHAKE_128
177+
#undef MLD_PREHASH_SHAKE_256
167178
#undef crypto_sign
168179
#undef crypto_sign_keypair
169180
#undef crypto_sign_open
@@ -284,6 +295,18 @@
284295
#undef mld_power2round
285296
#undef mld_use_hint
286297
/* mldsa/src/sign.h */
298+
#undef MLD_PREHASH_SHA2_224
299+
#undef MLD_PREHASH_SHA2_256
300+
#undef MLD_PREHASH_SHA2_384
301+
#undef MLD_PREHASH_SHA2_512
302+
#undef MLD_PREHASH_SHA2_512_224
303+
#undef MLD_PREHASH_SHA2_512_256
304+
#undef MLD_PREHASH_SHA3_224
305+
#undef MLD_PREHASH_SHA3_256
306+
#undef MLD_PREHASH_SHA3_384
307+
#undef MLD_PREHASH_SHA3_512
308+
#undef MLD_PREHASH_SHAKE_128
309+
#undef MLD_PREHASH_SHAKE_256
287310
#undef MLD_SIGN_H
288311
#undef crypto_sign
289312
#undef crypto_sign_keypair

mldsa/mldsa_native.h

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -422,27 +422,20 @@ int MLD_API_NAMESPACE(open)(
422422
const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]);
423423

424424
/*************************************************
425-
* Hash algorithm enumeration for pre-hash functions
425+
* Hash algorithm constants for pre-hash functions
426426
**************************************************/
427-
#ifndef MLD_PRE_HASH_ENUM
428-
#define MLD_PRE_HASH_ENUM
429-
typedef enum
430-
{
431-
MLD_SHA2_224,
432-
MLD_SHA2_256,
433-
MLD_SHA2_384,
434-
MLD_SHA2_512,
435-
MLD_SHA2_512_224,
436-
MLD_SHA2_512_256,
437-
MLD_SHA3_224,
438-
MLD_SHA3_256,
439-
MLD_SHA3_384,
440-
MLD_SHA3_512,
441-
MLD_SHAKE_128,
442-
MLD_SHAKE_256
443-
} mld_hash_alg_t;
444-
445-
#endif /* !MLD_PRE_HASH_ENUM */
427+
#define MLD_PREHASH_SHA2_224 1
428+
#define MLD_PREHASH_SHA2_256 2
429+
#define MLD_PREHASH_SHA2_384 3
430+
#define MLD_PREHASH_SHA2_512 4
431+
#define MLD_PREHASH_SHA2_512_224 5
432+
#define MLD_PREHASH_SHA2_512_256 6
433+
#define MLD_PREHASH_SHA3_224 7
434+
#define MLD_PREHASH_SHA3_256 8
435+
#define MLD_PREHASH_SHA3_384 9
436+
#define MLD_PREHASH_SHA3_512 10
437+
#define MLD_PREHASH_SHAKE_128 11
438+
#define MLD_PREHASH_SHAKE_256 12
446439

447440
/*************************************************
448441
* Name: crypto_sign_signature_pre_hash_internal
@@ -462,12 +455,13 @@ typedef enum
462455
* random seed
463456
* - const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]:
464457
* bit-packed secret key
465-
* - mld_hash_alg_t hashAlg: hash algorithm enumeration
458+
* - int hashalg: hash algorithm constant (one of MLD_PREHASH_*)
466459
*
467-
* The supported hash functions are: "SHA2-224", "SHA2-256", "SHA2-384",
468-
* "SHA2-512", "SHA2-512/224", "SHA2-512/256",
469-
* "SHA3-224", "SHA3-256", "SHA3-384",
470-
* "SHA3-512", "SHAKE-128", "SHAKE-256"
460+
* Supported hash algorithm constants:
461+
* MLD_PREHASH_SHA2_224, MLD_PREHASH_SHA2_256, MLD_PREHASH_SHA2_384,
462+
* MLD_PREHASH_SHA2_512, MLD_PREHASH_SHA2_512_224, MLD_PREHASH_SHA2_512_256,
463+
* MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384,
464+
* MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256
471465
*
472466
* Warning: This is an unstable API that may change in the future. If you need
473467
* a stable API use crypto_sign_signature_pre_hash_shake256.
@@ -481,7 +475,7 @@ int MLD_API_NAMESPACE(signature_pre_hash_internal)(
481475
const uint8_t *ph, size_t phlen, const uint8_t *ctx, size_t ctxlen,
482476
const uint8_t rnd[MLDSA_RNDBYTES],
483477
const uint8_t sk[MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)],
484-
mld_hash_alg_t hashAlg);
478+
int hashalg);
485479

486480
/*************************************************
487481
* Name: crypto_sign_verify_pre_hash_internal
@@ -498,12 +492,13 @@ int MLD_API_NAMESPACE(signature_pre_hash_internal)(
498492
* - size_t ctxlen: length of context string
499493
* - const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)]:
500494
* bit-packed public key
501-
* - mld_hash_alg_t hashAlg: hash algorithm enumeration
495+
* - int hashalg: hash algorithm constant (one of MLD_PREHASH_*)
502496
*
503-
* The supported hash functions are: "SHA2-224", "SHA2-256", "SHA2-384",
504-
* "SHA2-512", "SHA2-512/224", "SHA2-512/256",
505-
* "SHA3-224", "SHA3-256", "SHA3-384",
506-
* "SHA3-512", "SHAKE-128", "SHAKE-256"
497+
* Supported hash algorithm constants:
498+
* MLD_PREHASH_SHA2_224, MLD_PREHASH_SHA2_256, MLD_PREHASH_SHA2_384,
499+
* MLD_PREHASH_SHA2_512, MLD_PREHASH_SHA2_512_224, MLD_PREHASH_SHA2_512_256,
500+
* MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384,
501+
* MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256
507502
*
508503
* Warning: This is an unstable API that may change in the future. If you need
509504
* a stable API use crypto_sign_verify_pre_hash_shake256.
@@ -515,7 +510,7 @@ int MLD_API_NAMESPACE(verify_pre_hash_internal)(
515510
const uint8_t *sig, size_t siglen, const uint8_t *ph, size_t phlen,
516511
const uint8_t *ctx, size_t ctxlen,
517512
const uint8_t pk[MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)],
518-
mld_hash_alg_t hashAlg);
513+
int hashalg);
519514

520515
/*************************************************
521516
* Name: crypto_sign_signature_pre_hash_shake256

mldsa/src/prehash.c

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,91 +15,90 @@
1515
* Description: Returns the OID of a given SHA-2/SHA-3 hash function.
1616
*
1717
* Arguments: - uint8_t oid[11]: pointer to output oid
18-
* - mld_hash_alg_t hashAlg: hash algorithm enumeration
18+
* - int hashalg: hash algorithm constant (MLD_PREHASH_*)
1919
*
20-
**************************************************/
21-
static void mld_get_hash_oid(uint8_t oid[MLD_PRE_HASH_OID_LEN],
22-
mld_hash_alg_t hashAlg)
20+
***************************************************/
21+
static void mld_get_hash_oid(uint8_t oid[MLD_PRE_HASH_OID_LEN], int hashalg)
2322
{
2423
unsigned int i;
2524
static const struct
2625
{
27-
mld_hash_alg_t alg;
26+
int alg;
2827
uint8_t oid[MLD_PRE_HASH_OID_LEN];
2928
} oid_map[] = {
30-
{MLD_SHA2_224,
29+
{MLD_PREHASH_SHA2_224,
3130
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}},
32-
{MLD_SHA2_256,
31+
{MLD_PREHASH_SHA2_256,
3332
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}},
34-
{MLD_SHA2_384,
33+
{MLD_PREHASH_SHA2_384,
3534
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02}},
36-
{MLD_SHA2_512,
35+
{MLD_PREHASH_SHA2_512,
3736
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03}},
38-
{MLD_SHA2_512_224,
37+
{MLD_PREHASH_SHA2_512_224,
3938
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05}},
40-
{MLD_SHA2_512_256,
39+
{MLD_PREHASH_SHA2_512_256,
4140
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x06}},
42-
{MLD_SHA3_224,
41+
{MLD_PREHASH_SHA3_224,
4342
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x07}},
44-
{MLD_SHA3_256,
43+
{MLD_PREHASH_SHA3_256,
4544
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08}},
46-
{MLD_SHA3_384,
45+
{MLD_PREHASH_SHA3_384,
4746
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09}},
48-
{MLD_SHA3_512,
47+
{MLD_PREHASH_SHA3_512,
4948
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0A}},
50-
{MLD_SHAKE_128,
49+
{MLD_PREHASH_SHAKE_128,
5150
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0B}},
52-
{MLD_SHAKE_256,
51+
{MLD_PREHASH_SHAKE_256,
5352
{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0C}}};
5453

5554
for (i = 0; i < sizeof(oid_map) / sizeof(oid_map[0]); i++)
5655
__loop__(
5756
invariant(i <= sizeof(oid_map) / sizeof(oid_map[0]))
5857
)
5958
{
60-
if (oid_map[i].alg == hashAlg)
59+
if (oid_map[i].alg == hashalg)
6160
{
6261
mld_memcpy(oid, oid_map[i].oid, MLD_PRE_HASH_OID_LEN);
6362
return;
6463
}
6564
}
6665
}
6766

68-
int mld_validate_hash_length(mld_hash_alg_t hashAlg, size_t len)
67+
int mld_validate_hash_length(int hashalg, size_t len)
6968
{
70-
switch (hashAlg)
69+
switch (hashalg)
7170
{
72-
case MLD_SHA2_224:
71+
case MLD_PREHASH_SHA2_224:
7372
return (len == 224 / 8) ? 0 : -1;
74-
case MLD_SHA2_256:
73+
case MLD_PREHASH_SHA2_256:
7574
return (len == 256 / 8) ? 0 : -1;
76-
case MLD_SHA2_384:
75+
case MLD_PREHASH_SHA2_384:
7776
return (len == 384 / 8) ? 0 : -1;
78-
case MLD_SHA2_512:
77+
case MLD_PREHASH_SHA2_512:
7978
return (len == 512 / 8) ? 0 : -1;
80-
case MLD_SHA2_512_224:
79+
case MLD_PREHASH_SHA2_512_224:
8180
return (len == 224 / 8) ? 0 : -1;
82-
case MLD_SHA2_512_256:
81+
case MLD_PREHASH_SHA2_512_256:
8382
return (len == 256 / 8) ? 0 : -1;
84-
case MLD_SHA3_224:
83+
case MLD_PREHASH_SHA3_224:
8584
return (len == 224 / 8) ? 0 : -1;
86-
case MLD_SHA3_256:
85+
case MLD_PREHASH_SHA3_256:
8786
return (len == 256 / 8) ? 0 : -1;
88-
case MLD_SHA3_384:
87+
case MLD_PREHASH_SHA3_384:
8988
return (len == 384 / 8) ? 0 : -1;
90-
case MLD_SHA3_512:
89+
case MLD_PREHASH_SHA3_512:
9190
return (len == 512 / 8) ? 0 : -1;
92-
case MLD_SHAKE_128:
91+
case MLD_PREHASH_SHAKE_128:
9392
return (len == 256 / 8) ? 0 : -1;
94-
case MLD_SHAKE_256:
93+
case MLD_PREHASH_SHAKE_256:
9594
return (len == 512 / 8) ? 0 : -1;
9695
}
9796
return -1;
9897
}
9998

10099
size_t mld_format_pre_hash_message(
101100
uint8_t fmsg[MLD_PRE_HASH_MAX_FORMATTED_MESSAGE_BYTES], const uint8_t *ph,
102-
size_t phlen, const uint8_t *ctx, size_t ctxlen, mld_hash_alg_t hashAlg)
101+
size_t phlen, const uint8_t *ctx, size_t ctxlen, int hashalg)
103102
{
104103
/* Format: 0x01 || ctxlen (1 byte) || ctx || oid (11 bytes) || ph */
105104
fmsg[0] = 1;
@@ -112,7 +111,7 @@ size_t mld_format_pre_hash_message(
112111
}
113112

114113
/* Write OID */
115-
mld_get_hash_oid(fmsg + 2 + ctxlen, hashAlg);
114+
mld_get_hash_oid(fmsg + 2 + ctxlen, hashalg);
116115

117116
/* Copy pre-hash */
118117
mld_memcpy(fmsg + 2 + ctxlen + MLD_PRE_HASH_OID_LEN, ph, phlen);

mldsa/src/prehash.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
* Description: Validates that the given hash length matches the expected
2323
* length for the given hash algorithm.
2424
*
25-
* Arguments: - mld_hash_alg_t hashAlg: hash algorithm enumeration
25+
* Arguments: - int hashalg: hash algorithm constant (MLD_PREHASH_*)
2626
* - size_t len: Hash length to be checked
2727
*
2828
* Returns 0 if hash algorithm is known and the hash length matches
2929
* and -1 otherwise.
3030
**************************************************/
3131
MLD_MUST_CHECK_RETURN_VALUE
3232
MLD_INTERNAL_API
33-
int mld_validate_hash_length(mld_hash_alg_t hashAlg, size_t len);
33+
int mld_validate_hash_length(int hashalg, size_t len);
3434

3535
#define mld_format_pre_hash_message MLD_NAMESPACE(format_pre_hash_message)
3636
/*************************************************
@@ -45,13 +45,13 @@ int mld_validate_hash_length(mld_hash_alg_t hashAlg, size_t len);
4545
* - size_t phlen: length of pre-hashed message
4646
* - const uint8_t *ctx: pointer to context string (may be NULL)
4747
* - size_t ctxlen: length of context string
48-
* - mld_hash_alg_t hashAlg: hash algorithm enumeration
48+
* - int hashalg: hash algorithm constant (MLD_PREHASH_*)
4949
*
5050
* Returns the total length of the formatted message (2 + ctxlen + 11 + phlen).
5151
**************************************************/
5252
MLD_INTERNAL_API
5353
size_t mld_format_pre_hash_message(
5454
uint8_t fmsg[MLD_PRE_HASH_MAX_FORMATTED_MESSAGE_BYTES], const uint8_t *ph,
55-
size_t phlen, const uint8_t *ctx, size_t ctxlen, mld_hash_alg_t hashAlg);
55+
size_t phlen, const uint8_t *ctx, size_t ctxlen, int hashalg);
5656

5757
#endif /* !MLD_PREHASH_H */

mldsa/src/sign.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ MLD_EXTERNAL_API
887887
int crypto_sign_signature_pre_hash_internal(
888888
uint8_t sig[CRYPTO_BYTES], size_t *siglen, const uint8_t *ph, size_t phlen,
889889
const uint8_t *ctx, size_t ctxlen, const uint8_t rnd[MLDSA_RNDBYTES],
890-
const uint8_t sk[CRYPTO_SECRETKEYBYTES], mld_hash_alg_t hashAlg)
890+
const uint8_t sk[CRYPTO_SECRETKEYBYTES], int hashalg)
891891
{
892892
MLD_ALIGN uint8_t fmsg[MLD_PRE_HASH_MAX_FORMATTED_MESSAGE_BYTES];
893893
size_t fmsg_len;
@@ -899,13 +899,13 @@ int crypto_sign_signature_pre_hash_internal(
899899
return -1;
900900
}
901901

902-
if (mld_validate_hash_length(hashAlg, phlen))
902+
if (mld_validate_hash_length(hashalg, phlen))
903903
{
904904
*siglen = 0;
905905
return -1;
906906
}
907907

908-
fmsg_len = mld_format_pre_hash_message(fmsg, ph, phlen, ctx, ctxlen, hashAlg);
908+
fmsg_len = mld_format_pre_hash_message(fmsg, ph, phlen, ctx, ctxlen, hashalg);
909909

910910
result = crypto_sign_signature_internal(sig, siglen, fmsg, fmsg_len, NULL, 0,
911911
rnd, sk, 0);
@@ -919,7 +919,7 @@ MLD_EXTERNAL_API
919919
int crypto_sign_verify_pre_hash_internal(
920920
const uint8_t *sig, size_t siglen, const uint8_t *ph, size_t phlen,
921921
const uint8_t *ctx, size_t ctxlen, const uint8_t pk[CRYPTO_PUBLICKEYBYTES],
922-
mld_hash_alg_t hashAlg)
922+
int hashalg)
923923
{
924924
MLD_ALIGN uint8_t fmsg[MLD_PRE_HASH_MAX_FORMATTED_MESSAGE_BYTES];
925925
size_t fmsg_len;
@@ -930,12 +930,12 @@ int crypto_sign_verify_pre_hash_internal(
930930
return -1;
931931
}
932932

933-
if (mld_validate_hash_length(hashAlg, phlen))
933+
if (mld_validate_hash_length(hashalg, phlen))
934934
{
935935
return -1;
936936
}
937937

938-
fmsg_len = mld_format_pre_hash_message(fmsg, ph, phlen, ctx, ctxlen, hashAlg);
938+
fmsg_len = mld_format_pre_hash_message(fmsg, ph, phlen, ctx, ctxlen, hashalg);
939939

940940
result =
941941
crypto_sign_verify_internal(sig, siglen, fmsg, fmsg_len, NULL, 0, pk, 0);
@@ -955,7 +955,7 @@ int crypto_sign_signature_pre_hash_shake256(
955955
int result;
956956
mld_shake256(ph, sizeof(ph), m, mlen);
957957
result = crypto_sign_signature_pre_hash_internal(
958-
sig, siglen, ph, sizeof(ph), ctx, ctxlen, rnd, sk, MLD_SHAKE_256);
958+
sig, siglen, ph, sizeof(ph), ctx, ctxlen, rnd, sk, MLD_PREHASH_SHAKE_256);
959959
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
960960
mld_zeroize(ph, sizeof(ph));
961961
return result;
@@ -970,8 +970,8 @@ int crypto_sign_verify_pre_hash_shake256(
970970
MLD_ALIGN uint8_t ph[64];
971971
int result;
972972
mld_shake256(ph, sizeof(ph), m, mlen);
973-
result = crypto_sign_verify_pre_hash_internal(sig, siglen, ph, sizeof(ph),
974-
ctx, ctxlen, pk, MLD_SHAKE_256);
973+
result = crypto_sign_verify_pre_hash_internal(
974+
sig, siglen, ph, sizeof(ph), ctx, ctxlen, pk, MLD_PREHASH_SHAKE_256);
975975
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
976976
mld_zeroize(ph, sizeof(ph));
977977
return result;

0 commit comments

Comments
 (0)