pq_crypto 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/SECURITY.md +7 -0
  4. data/ext/pqcrypto/pqcrypto_version.h +1 -1
  5. data/ext/pqcrypto/vendor/.vendored +4 -4
  6. data/ext/pqcrypto/vendor/mldsa-native/README.md +23 -10
  7. data/ext/pqcrypto/vendor/mldsa-native/mldsa/README.md +23 -0
  8. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native.c +114 -58
  9. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native.h +498 -461
  10. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native_asm.S +145 -85
  11. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native_config.h +456 -422
  12. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/cbmc.h +47 -25
  13. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/common.h +26 -14
  14. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/ct.h +56 -81
  15. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/debug.h +17 -24
  16. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202.c +33 -40
  17. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202.h +67 -87
  18. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202x4.c +19 -14
  19. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202x4.h +13 -5
  20. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.c +84 -10
  21. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.h +10 -5
  22. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/auto.h +6 -0
  23. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/fips202_native_aarch64.h +22 -15
  24. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_scalar_aarch64_asm.S +376 -0
  25. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_v84a_aarch64_asm.S +204 -0
  26. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x2_v84a_aarch64_asm.S +259 -0
  27. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_scalar_hybrid_aarch64_asm.S +1077 -0
  28. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_v84a_scalar_hybrid_aarch64_asm.S +987 -0
  29. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccakf1600_round_constants.c +16 -10
  30. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_scalar.h +2 -1
  31. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_v84a.h +1 -1
  32. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x2_v84a.h +4 -2
  33. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_scalar.h +2 -2
  34. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_v84a_scalar.h +1 -1
  35. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/api.h +60 -0
  36. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/mve.h +48 -0
  37. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/fips202_native_armv81m.h +18 -1
  38. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.S +658 -582
  39. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.c +5 -100
  40. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccakf1600_round_constants.c +26 -25
  41. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/state_extract_bytes_x4_mve.S +334 -0
  42. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/state_xor_bytes_x4_mve.S +355 -0
  43. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/auto.h +8 -3
  44. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/{xkcp.h → keccak_f1600_x4_avx2.h} +11 -8
  45. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/fips202_native_x86_64.h +44 -0
  46. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/keccak_f1600_x4_avx2_asm.S +454 -0
  47. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/keccakf1600_constants.c +52 -0
  48. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/meta.h +37 -28
  49. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/aarch64_zetas.c +213 -196
  50. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/arith_native_aarch64.h +248 -64
  51. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/intt_aarch64_asm.S +753 -0
  52. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l4_aarch64_asm.S +129 -0
  53. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l5_aarch64_asm.S +145 -0
  54. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l7_aarch64_asm.S +177 -0
  55. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/ntt_aarch64_asm.S +653 -0
  56. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/pointwise_montgomery_aarch64_asm.S +84 -0
  57. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_caddq_aarch64_asm.S +53 -0
  58. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_chknorm_aarch64_asm.S +55 -0
  59. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_32_aarch64_asm.S +86 -0
  60. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_88_aarch64_asm.S +86 -0
  61. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_32_aarch64_asm.S +103 -0
  62. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_88_aarch64_asm.S +111 -0
  63. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_17_aarch64_asm.S +75 -0
  64. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_19_aarch64_asm.S +72 -0
  65. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_table.c +23 -11
  66. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_aarch64_asm.S +189 -0
  67. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta2_aarch64_asm.S +137 -0
  68. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta4_aarch64_asm.S +130 -0
  69. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta_table.c +520 -516
  70. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_table.c +34 -33
  71. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/api.h +202 -242
  72. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/meta.h +25 -17
  73. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/arith_native_x86_64.h +112 -28
  74. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/consts.c +1 -1
  75. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/consts.h +1 -1
  76. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/intt_avx2_asm.S +2311 -0
  77. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/ntt_avx2_asm.S +2383 -0
  78. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/nttunpack_avx2_asm.S +238 -0
  79. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l4_avx2_asm.S +139 -0
  80. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l5_avx2_asm.S +155 -0
  81. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l7_avx2_asm.S +187 -0
  82. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_avx2_asm.S +130 -0
  83. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_caddq_avx2_asm.S +190 -0
  84. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_decompose_32_avx2.c +6 -4
  85. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_decompose_88_avx2.c +6 -4
  86. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_use_hint_32_avx2.c +9 -8
  87. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_use_hint_88_avx2.c +10 -9
  88. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/polyz_unpack_17_avx2.c +8 -5
  89. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/polyz_unpack_19_avx2.c +8 -5
  90. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_eta2_avx2.c +6 -4
  91. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_eta4_avx2.c +6 -4
  92. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_table.c +130 -129
  93. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/packing.c +109 -180
  94. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/packing.h +169 -150
  95. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly.c +56 -40
  96. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly.h +149 -164
  97. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly_kl.c +52 -57
  98. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly_kl.h +132 -167
  99. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec.c +57 -424
  100. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec.h +167 -474
  101. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec_lazy.c +308 -0
  102. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec_lazy.h +653 -0
  103. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/reduce.h +22 -29
  104. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/rounding.h +37 -43
  105. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sign.c +511 -367
  106. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sign.h +456 -417
  107. data/lib/pq_crypto/version.rb +1 -1
  108. data/script/vendor_libs.rb +3 -3
  109. metadata +41 -35
  110. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_scalar_asm.S +0 -376
  111. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_v84a_asm.S +0 -204
  112. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x2_v84a_asm.S +0 -259
  113. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_scalar_hybrid_asm.S +0 -1077
  114. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_v84a_scalar_hybrid_asm.S +0 -987
  115. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/KeccakP_1600_times4_SIMD256.c +0 -488
  116. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/KeccakP_1600_times4_SIMD256.h +0 -16
  117. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/intt.S +0 -753
  118. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l4.S +0 -129
  119. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l5.S +0 -145
  120. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/mld_polyvecl_pointwise_acc_montgomery_l7.S +0 -177
  121. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/ntt.S +0 -653
  122. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/pointwise_montgomery.S +0 -79
  123. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_caddq_asm.S +0 -53
  124. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_chknorm_asm.S +0 -55
  125. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_32_asm.S +0 -85
  126. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_decompose_88_asm.S +0 -85
  127. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_32_asm.S +0 -102
  128. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/poly_use_hint_88_asm.S +0 -110
  129. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_17_asm.S +0 -72
  130. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/polyz_unpack_19_asm.S +0 -69
  131. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_asm.S +0 -189
  132. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta2_asm.S +0 -135
  133. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/rej_uniform_eta4_asm.S +0 -128
  134. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/intt.S +0 -2311
  135. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/ntt.S +0 -2383
  136. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/nttunpack.S +0 -239
  137. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise.S +0 -131
  138. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l4.S +0 -139
  139. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l5.S +0 -155
  140. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/pointwise_acc_l7.S +0 -187
  141. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_caddq_avx2.c +0 -61
@@ -71,9 +71,7 @@
71
71
  #define mld_sign_pk_from_sk \
72
72
  MLD_NAMESPACE_KL(pk_from_sk) MLD_CONTEXT_PARAMETERS_2
73
73
 
74
- /*************************************************
75
- * Hash algorithm constants for domain separation
76
- **************************************************/
74
+ /* Hash algorithm constants for domain separation */
77
75
  #define MLD_PREHASH_NONE 0
78
76
  #define MLD_PREHASH_SHA2_224 1
79
77
  #define MLD_PREHASH_SHA2_256 2
@@ -88,27 +86,35 @@
88
86
  #define MLD_PREHASH_SHAKE_128 11
89
87
  #define MLD_PREHASH_SHAKE_256 12
90
88
 
91
- /*************************************************
92
- * Name: mld_sign_keypair_internal
93
- *
94
- * Description: Generates public and private key. Internal API.
95
- * When MLD_CONFIG_KEYGEN_PCT is set, performs a Pairwise
96
- * Consistency Test (PCT) as required by FIPS 140-3 IG.
97
- *
98
- * Arguments: - uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]: output public key
99
- * - uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]: output private key
100
- * - const uint8_t seed[MLDSA_SEEDBYTES]: input random seed
101
- *
102
- * Returns: - 0: Success
103
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
104
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
105
- * - MLD_ERR_RNG_FAIL: Random number generation failed.
106
- * - MLD_ERR_FAIL: Other kinds of failure, incl. PCT failure
107
- * if MLD_CONFIG_KEYGEN_PCT is enabled.
108
- *
109
- * Specification: Implements @[FIPS204 Algorithm 6 (ML-DSA.KeyGen_internal)]
110
- *
111
- **************************************************/
89
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
90
+ /**
91
+ * Generate a public-private key pair from a seed.
92
+ *
93
+ * When MLD_CONFIG_KEYGEN_PCT is set, performs a Pairwise Consistency Test
94
+ * (PCT) as required by FIPS 140-3 IG.
95
+ *
96
+ * @spec{Implements @[FIPS204 Algorithm 6 (ML-DSA.KeyGen_internal)].}
97
+ *
98
+ * @param[out] pk Output public key.
99
+ * @param[out] sk Output private key.
100
+ * @param[in] seed Input random seed.
101
+ * @param context Application context. Only present when
102
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
103
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
104
+ *
105
+ * @retval 0 Success.
106
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was
107
+ * used and an allocation via
108
+ * MLD_CUSTOM_ALLOC returned NULL.
109
+ * @retval MLD_ERR_RNG_FAIL Random number generation failed.
110
+ * @retval MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED The PCT's signing step exhausted
111
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS
112
+ * iterations. Only possible when
113
+ * MLD_CONFIG_KEYGEN_PCT is enabled.
114
+ * @retval MLD_ERR_FAIL Other kinds of failure, including
115
+ * PCT failure if
116
+ * MLD_CONFIG_KEYGEN_PCT is enabled.
117
+ */
112
118
  MLD_MUST_CHECK_RETURN_VALUE
113
119
  MLD_EXTERNAL_API
114
120
  int mld_sign_keypair_internal(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
@@ -121,30 +127,37 @@ __contract__(
121
127
  requires(memory_no_alias(seed, MLDSA_SEEDBYTES))
122
128
  assigns(object_whole(pk))
123
129
  assigns(object_whole(sk))
124
- ensures(return_value == 0 || return_value == MLD_ERR_FAIL ||
125
- return_value == MLD_ERR_OUT_OF_MEMORY || return_value == MLD_ERR_RNG_FAIL)
130
+ ensures(return_value == 0 || MLD_ANY_ERROR(return_value))
126
131
  );
127
132
 
128
- /*************************************************
129
- * Name: mld_sign_keypair
130
- *
131
- * Description: Generates public and private key.
132
- * When MLD_CONFIG_KEYGEN_PCT is set, performs a Pairwise
133
- * Consistency Test (PCT) as required by FIPS 140-3 IG.
134
- *
135
- * Arguments: - uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]: output public key
136
- * - uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]: output private key
137
- *
138
- * Returns: - 0: Success
139
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
140
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
141
- * - MLD_ERR_RNG_FAIL: Random number generation failed.
142
- * - MLD_ERR_FAIL: Other kinds of failure, incl. PCT failure
143
- * if MLD_CONFIG_KEYGEN_PCT is enabled.
144
- *
145
- * Specification: Implements @[FIPS204 Algorithm 1 (ML-DSA.KeyGen)]
146
- *
147
- **************************************************/
133
+ #if !defined(MLD_CONFIG_CORE_API_ONLY)
134
+ /**
135
+ * Generate a public-private key pair.
136
+ *
137
+ * When MLD_CONFIG_KEYGEN_PCT is set, performs a Pairwise Consistency Test
138
+ * (PCT) as required by FIPS 140-3 IG.
139
+ *
140
+ * @spec{Implements @[FIPS204 Algorithm 1 (ML-DSA.KeyGen)].}
141
+ *
142
+ * @param[out] pk Output public key.
143
+ * @param[out] sk Output private key.
144
+ * @param context Application context. Only present when
145
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
146
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
147
+ *
148
+ * @retval 0 Success.
149
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was
150
+ * used and an allocation via
151
+ * MLD_CUSTOM_ALLOC returned NULL.
152
+ * @retval MLD_ERR_RNG_FAIL Random number generation failed.
153
+ * @retval MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED The PCT's signing step exhausted
154
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS
155
+ * iterations. Only possible when
156
+ * MLD_CONFIG_KEYGEN_PCT is enabled.
157
+ * @retval MLD_ERR_FAIL Other kinds of failure, including
158
+ * PCT failure if
159
+ * MLD_CONFIG_KEYGEN_PCT is enabled.
160
+ */
148
161
  MLD_MUST_CHECK_RETURN_VALUE
149
162
  MLD_EXTERNAL_API
150
163
  int mld_sign_keypair(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
@@ -155,41 +168,48 @@ __contract__(
155
168
  requires(memory_no_alias(sk, MLDSA_CRYPTO_SECRETKEYBYTES))
156
169
  assigns(object_whole(pk))
157
170
  assigns(object_whole(sk))
158
- ensures(return_value == 0 || return_value == MLD_ERR_FAIL ||
159
- return_value == MLD_ERR_OUT_OF_MEMORY || return_value == MLD_ERR_RNG_FAIL)
171
+ ensures(return_value == 0 || MLD_ANY_ERROR(return_value))
160
172
  );
173
+ #endif /* !MLD_CONFIG_CORE_API_ONLY */
174
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
161
175
 
162
- /*************************************************
163
- * Name: mld_sign_signature_internal
164
- *
165
- * Description: Computes signature. Internal API.
166
- *
167
- * Arguments: - uint8_t sig[MLDSA_CRYPTO_BYTES]: output signature
168
- * - size_t *siglen: pointer to output length of
169
- * signature
170
- * - const uint8_t *m: pointer to message to be signed
171
- * - size_t mlen: length of message
172
- * - const uint8_t *pre: pointer to prefix string
173
- * - size_t prelen: length of prefix string
174
- * - const uint8_t rnd[MLDSA_RNDBYTES]:
175
- * random seed
176
- * - const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]:
177
- * bit-packed secret key
178
- * - int externalmu: indicates input message m is
179
- * processed as mu
180
- *
181
- * Returns: - 0: Success
182
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
183
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
184
- * - MLD_ERR_FAIL: Other kinds of failure
185
- *
186
- * If the returned value is non-zero, then the values of *sig and
187
- * *siglen should not be referenced.
188
- *
189
- * Reference: This code differs from the reference implementation
190
- * in that it adds an explicit check for nonce exhaustion
191
- * and can return MLD_ERR_FAIL in that case.
192
- **************************************************/
176
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
177
+ /**
178
+ * Compute signature using internal randomness.
179
+ *
180
+ * If the returned value is non-zero, then the values of *sig and *siglen
181
+ * should not be referenced.
182
+ *
183
+ * @param[out] sig Output signature.
184
+ * @param[out] siglen Pointer to output length of signature.
185
+ * @param[in] m Pointer to message to be signed (when
186
+ * externalmu == 0), or to a precomputed
187
+ * message representative mu (when externalmu != 0).
188
+ * @param mlen Length of m. Must equal MLDSA_CRHBYTES when
189
+ * externalmu != 0.
190
+ * @param[in] pre Pointer to prefix string. Ignored when
191
+ * externalmu != 0.
192
+ * @param prelen Length of prefix string. Ignored when
193
+ * externalmu != 0.
194
+ * @param[in] rnd Random seed.
195
+ * @param[in] sk Bit-packed secret key.
196
+ * @param externalmu 0: m/mlen is the raw message; mu = H(tr, pre, m) is
197
+ * computed internally.
198
+ * non-zero: m points to a precomputed mu of
199
+ * MLDSA_CRHBYTES bytes; pre/prelen unused.
200
+ * @param context Application context. Only present when
201
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
202
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
203
+ *
204
+ * @retval 0 Success.
205
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was
206
+ * used and an allocation via
207
+ * MLD_CUSTOM_ALLOC returned NULL.
208
+ * @retval MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED The rejection-sampling loop exceeded
209
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS
210
+ * iterations.
211
+ * @retval MLD_ERR_FAIL Other kinds of failure.
212
+ */
193
213
  MLD_MUST_CHECK_RETURN_VALUE
194
214
  MLD_EXTERNAL_API
195
215
  int mld_sign_signature_internal(uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen,
@@ -207,44 +227,46 @@ __contract__(
207
227
  requires(memory_no_alias(m, mlen))
208
228
  requires(memory_no_alias(rnd, MLDSA_RNDBYTES))
209
229
  requires(memory_no_alias(sk, MLDSA_CRYPTO_SECRETKEYBYTES))
210
- requires((externalmu == 0 && (prelen == 0 || memory_no_alias(pre, prelen))) ||
211
- (externalmu == 1 && mlen == MLDSA_CRHBYTES))
230
+ requires((externalmu == 0) ==> ((prelen == 0) || memory_no_alias(pre, prelen)))
231
+ requires((externalmu != 0) ==> (mlen == MLDSA_CRHBYTES))
212
232
  assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES))
213
233
  assigns(object_whole(siglen))
214
234
  ensures(return_value == 0 || return_value == MLD_ERR_FAIL ||
215
- return_value == MLD_ERR_OUT_OF_MEMORY)
235
+ return_value == MLD_ERR_OUT_OF_MEMORY ||
236
+ return_value == MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED)
216
237
  ensures(return_value == 0 ==> *siglen == MLDSA_CRYPTO_BYTES)
217
238
  ensures(return_value != 0 ==> *siglen == 0)
218
239
  );
219
240
 
220
- /*************************************************
221
- * Name: mld_sign_signature
222
- *
223
- * Description: Computes signature. This function implements the randomized
224
- * variant of ML-DSA. If you require the deterministic variant,
225
- * use mld_sign_signature_internal directly.
226
- *
227
- * Arguments: - uint8_t sig[MLDSA_CRYPTO_BYTES]: output signature
228
- * - size_t *siglen: pointer to output length of
229
- * signature
230
- * - const uint8_t *m: pointer to message to be signed
231
- * - size_t mlen: length of message
232
- * - uint8_t *ctx: pointer to context string.
233
- * May be NULL if ctxlen == 0.
234
- * - size_t ctxlen: length of context string.
235
- * Should be <= 255.
236
- * - const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]:
237
- * bit-packed secret key
238
- *
239
- * Returns: - 0: Success
240
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
241
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
242
- * - MLD_ERR_RNG_FAIL: Random number generation failed.
243
- * - MLD_ERR_FAIL: Other kinds of failure.
244
- *
245
- * Specification: Implements @[FIPS204 Algorithm 2 (ML-DSA.Sign)].
246
- *
247
- **************************************************/
241
+ #if !defined(MLD_CONFIG_CORE_API_ONLY)
242
+ /**
243
+ * Compute signature. This function implements the randomized variant of
244
+ * ML-DSA. If you require the deterministic variant, use
245
+ * mld_sign_signature_internal directly.
246
+ *
247
+ * @spec{Implements @[FIPS204 Algorithm 2 (ML-DSA.Sign)].}
248
+ *
249
+ * @param[out] sig Output signature.
250
+ * @param[out] siglen Pointer to output length of signature.
251
+ * @param[in] m Pointer to message to be signed.
252
+ * @param mlen Length of message.
253
+ * @param[in] ctx Pointer to context string. May be NULL if ctxlen == 0.
254
+ * @param ctxlen Length of context string. Should be <= 255.
255
+ * @param[in] sk Bit-packed secret key.
256
+ * @param context Application context. Only present when
257
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
258
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
259
+ *
260
+ * @retval 0 Success.
261
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was
262
+ * used and an allocation via
263
+ * MLD_CUSTOM_ALLOC returned NULL.
264
+ * @retval MLD_ERR_RNG_FAIL Random number generation failed.
265
+ * @retval MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED The rejection-sampling loop exceeded
266
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS
267
+ * iterations.
268
+ * @retval MLD_ERR_FAIL Other kinds of failure.
269
+ */
248
270
  MLD_MUST_CHECK_RETURN_VALUE
249
271
  MLD_EXTERNAL_API
250
272
  int mld_sign_signature(uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen,
@@ -263,34 +285,37 @@ __contract__(
263
285
  assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES))
264
286
  assigns(object_whole(siglen))
265
287
  ensures((return_value == 0 && *siglen == MLDSA_CRYPTO_BYTES) ||
266
- ((return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY || return_value == MLD_ERR_RNG_FAIL) && *siglen == 0))
288
+ (MLD_ANY_ERROR(return_value) && *siglen == 0))
267
289
  );
268
290
 
269
- /*************************************************
270
- * Name: mld_sign_signature_extmu
271
- *
272
- * Description: Computes signature. This function implements the randomized
273
- * variant of ML-DSA. If you require the deterministic variant,
274
- * use mld_sign_signature_internal directly.
275
- *
276
- * Arguments: - uint8_t sig[MLDSA_CRYPTO_BYTES]: output signature
277
- * - size_t *siglen: pointer to output length of
278
- * signature
279
- * - const uint8_t mu[MLDSA_CRHBYTES]:
280
- * input mu to be signed
281
- * - const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]:
282
- * bit-packed secret key
283
- *
284
- * Returns: - 0: Success
285
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
286
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
287
- * - MLD_ERR_RNG_FAIL: Random number generation failed.
288
- * - MLD_ERR_FAIL: Other kinds of failure.
289
- *
290
- * Specification: Implements @[FIPS204 Algorithm 2 (ML-DSA.Sign external mu
291
- * variant)]
292
- *
293
- **************************************************/
291
+ /**
292
+ * Compute signature in "external mu" mode: the caller has already computed
293
+ * the message representative mu = SHAKE256(tr || M', 64), where
294
+ * tr = SHAKE256(pk, 64) and M' is the FIPS 204 formatted message (e.g.
295
+ * 0x00 || ctxlen || ctx || msg for pure ML-DSA). This is the randomized
296
+ * variant; for the deterministic variant, use mld_sign_signature_internal
297
+ * directly with externalmu set to non-zero and an all-zero rnd.
298
+ *
299
+ * @spec{Implements @[FIPS204 Algorithm 2 (ML-DSA.Sign external mu variant)].}
300
+ *
301
+ * @param[out] sig Output signature.
302
+ * @param[out] siglen Pointer to output length of signature.
303
+ * @param[in] mu Precomputed message representative.
304
+ * @param[in] sk Bit-packed secret key.
305
+ * @param context Application context. Only present when
306
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
307
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
308
+ *
309
+ * @retval 0 Success.
310
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was
311
+ * used and an allocation via
312
+ * MLD_CUSTOM_ALLOC returned NULL.
313
+ * @retval MLD_ERR_RNG_FAIL Random number generation failed.
314
+ * @retval MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED The rejection-sampling loop exceeded
315
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS
316
+ * iterations.
317
+ * @retval MLD_ERR_FAIL Other kinds of failure.
318
+ */
294
319
  MLD_MUST_CHECK_RETURN_VALUE
295
320
  MLD_EXTERNAL_API
296
321
  int mld_sign_signature_extmu(uint8_t sig[MLDSA_CRYPTO_BYTES], size_t *siglen,
@@ -305,31 +330,33 @@ __contract__(
305
330
  assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES))
306
331
  assigns(object_whole(siglen))
307
332
  ensures((return_value == 0 && *siglen == MLDSA_CRYPTO_BYTES) ||
308
- ((return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY || return_value == MLD_ERR_RNG_FAIL) && *siglen == 0))
333
+ (MLD_ANY_ERROR(return_value) && *siglen == 0))
309
334
  );
310
335
 
311
- /*************************************************
312
- * Name: mld_sign
313
- *
314
- * Description: Compute signed message.
315
- *
316
- * Arguments: - uint8_t *sm: pointer to output signed message
317
- * (allocated array with MLDSA_CRYPTO_BYTES +
318
- *mlen bytes), can be equal to m
319
- * - size_t *smlen: pointer to output length of signed message
320
- * - const uint8_t *m: pointer to message to be signed
321
- * - size_t mlen: length of message
322
- * - const uint8_t *ctx: pointer to context string
323
- * - size_t ctxlen: length of context string
324
- * - const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]:
325
- * bit-packed secret key
326
- *
327
- * Returns: - 0: Success
328
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
329
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
330
- * - MLD_ERR_FAIL: Other kinds of failure
331
- *
332
- **************************************************/
336
+ /**
337
+ * Compute signed message.
338
+ *
339
+ * @param[out] sm Pointer to output signed message (allocated array with
340
+ * MLDSA_CRYPTO_BYTES + mlen bytes); can be equal to m.
341
+ * @param[out] smlen Pointer to output length of signed message.
342
+ * @param[in] m Pointer to message to be signed.
343
+ * @param mlen Length of message.
344
+ * @param[in] ctx Pointer to context string.
345
+ * @param ctxlen Length of context string.
346
+ * @param[in] sk Bit-packed secret key.
347
+ * @param context Application context. Only present when
348
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
349
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
350
+ *
351
+ * @retval 0 Success.
352
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was
353
+ * used and an allocation via
354
+ * MLD_CUSTOM_ALLOC returned NULL.
355
+ * @retval MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED The rejection-sampling loop exceeded
356
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS
357
+ * iterations.
358
+ * @retval MLD_ERR_FAIL Other kinds of failure.
359
+ */
333
360
  MLD_MUST_CHECK_RETURN_VALUE
334
361
  MLD_EXTERNAL_API
335
362
  int mld_sign(uint8_t *sm, size_t *smlen, const uint8_t *m, size_t mlen,
@@ -347,35 +374,40 @@ __contract__(
347
374
  assigns(memory_slice(sm, MLDSA_CRYPTO_BYTES + mlen))
348
375
  assigns(object_whole(smlen))
349
376
  ensures((return_value == 0 && *smlen == MLDSA_CRYPTO_BYTES + mlen) ||
350
- (return_value == MLD_ERR_FAIL
351
- || return_value == MLD_ERR_OUT_OF_MEMORY
352
- || return_value == MLD_ERR_RNG_FAIL))
377
+ (MLD_ANY_ERROR(return_value) && *smlen == 0))
353
378
  );
379
+ #endif /* !MLD_CONFIG_CORE_API_ONLY */
380
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
354
381
 
355
- /*************************************************
356
- * Name: mld_sign_verify_internal
357
- *
358
- * Description: Verifies signature. Internal API.
359
- *
360
- * Arguments: - const uint8_t *sig: pointer to input signature
361
- * - size_t siglen: length of signature
362
- * - const uint8_t *m: pointer to message
363
- * - size_t mlen: length of message
364
- * - const uint8_t *pre: pointer to prefix string
365
- * - size_t prelen: length of prefix string
366
- * - const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]:
367
- * bit-packed public key
368
- * - int externalmu: indicates input message m is processed as
369
- * mu
370
- *
371
- * Returns: - 0: Success
372
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
373
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
374
- * - MLD_ERR_FAIL: Signature verification failed
375
- *
376
- * Specification: Implements @[FIPS204 Algorithm 8 (ML-DSA.Verify_internal)]
377
- *
378
- **************************************************/
382
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
383
+ /**
384
+ * Verify signature.
385
+ *
386
+ * @spec{Implements @[FIPS204 Algorithm 8 (ML-DSA.Verify_internal)].}
387
+ *
388
+ * @param[in] sig Pointer to input signature.
389
+ * @param siglen Length of signature.
390
+ * @param[in] m Pointer to message (when externalmu == 0), or to a
391
+ * precomputed message representative mu (when
392
+ * externalmu != 0).
393
+ * @param mlen Length of m. Must equal MLDSA_CRHBYTES when
394
+ * externalmu != 0.
395
+ * @param[in] pre Pointer to prefix string. Ignored when externalmu != 0.
396
+ * @param prelen Length of prefix string. Ignored when externalmu != 0.
397
+ * @param[in] pk Bit-packed public key.
398
+ * @param externalmu 0: m/mlen is the raw message; mu = H(H(pk), pre, m) is
399
+ * computed internally.
400
+ * non-zero: m points to a precomputed mu of
401
+ * MLDSA_CRHBYTES bytes; pre/prelen unused.
402
+ * @param context Application context. Only present when
403
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
404
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
405
+ *
406
+ * @retval 0 Success.
407
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was used and an
408
+ * allocation via MLD_CUSTOM_ALLOC returned NULL.
409
+ * @retval MLD_ERR_FAIL Signature verification failed.
410
+ */
379
411
  MLD_MUST_CHECK_RETURN_VALUE
380
412
  MLD_EXTERNAL_API
381
413
  int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
@@ -390,35 +422,34 @@ __contract__(
390
422
  requires(siglen <= MLD_MAX_BUFFER_SIZE)
391
423
  requires(memory_no_alias(sig, siglen))
392
424
  requires(memory_no_alias(m, mlen))
393
- requires(externalmu == 0 || (externalmu == 1 && mlen == MLDSA_CRHBYTES))
394
- requires(externalmu == 1 || prelen == 0 || memory_no_alias(pre, prelen))
425
+ requires((externalmu == 0) ==> ((prelen == 0) || memory_no_alias(pre, prelen)))
426
+ requires((externalmu != 0) ==> (mlen == MLDSA_CRHBYTES))
395
427
  requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
396
428
  ensures(return_value == 0 || return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY)
397
429
  );
398
430
 
399
- /*************************************************
400
- * Name: mld_sign_verify
401
- *
402
- * Description: Verifies signature.
403
- *
404
- * Arguments: - const uint8_t *sig: pointer to input signature
405
- * - size_t siglen: length of signature
406
- * - const uint8_t *m: pointer to message
407
- * - size_t mlen: length of message
408
- * - const uint8_t *ctx: pointer to context string.
409
- * May be NULL if ctxlen == 0.
410
- * - size_t ctxlen: length of context string
411
- * - const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]:
412
- * bit-packed public key
413
- *
414
- * Returns: - 0: Success
415
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
416
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
417
- * - MLD_ERR_FAIL: Signature verification failed
418
- *
419
- * Specification: Implements @[FIPS204 Algorithm 3 (ML-DSA.Verify)]
420
- *
421
- **************************************************/
431
+ #if !defined(MLD_CONFIG_CORE_API_ONLY)
432
+ /**
433
+ * Verify signature.
434
+ *
435
+ * @spec{Implements @[FIPS204 Algorithm 3 (ML-DSA.Verify)].}
436
+ *
437
+ * @param[in] sig Pointer to input signature.
438
+ * @param siglen Length of signature.
439
+ * @param[in] m Pointer to message.
440
+ * @param mlen Length of message.
441
+ * @param[in] ctx Pointer to context string. May be NULL if ctxlen == 0.
442
+ * @param ctxlen Length of context string.
443
+ * @param[in] pk Bit-packed public key.
444
+ * @param context Application context. Only present when
445
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
446
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
447
+ *
448
+ * @retval 0 Success.
449
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was used and an
450
+ * allocation via MLD_CUSTOM_ALLOC returned NULL.
451
+ * @retval MLD_ERR_FAIL Signature verification failed.
452
+ */
422
453
  MLD_MUST_CHECK_RETURN_VALUE
423
454
  MLD_EXTERNAL_API
424
455
  int mld_sign_verify(const uint8_t *sig, size_t siglen, const uint8_t *m,
@@ -436,27 +467,28 @@ __contract__(
436
467
  ensures(return_value == 0 || return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY)
437
468
  );
438
469
 
439
- /*************************************************
440
- * Name: mld_sign_verify_extmu
441
- *
442
- * Description: Verifies signature.
443
- *
444
- * Arguments: - const uint8_t *sig: pointer to input signature
445
- * - size_t siglen: length of signature
446
- * - const uint8_t mu[MLDSA_CRHBYTES]:
447
- * input mu
448
- * - const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]:
449
- * bit-packed public key
450
- *
451
- * Returns: - 0: Success
452
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
453
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
454
- * - MLD_ERR_FAIL: Signature verification failed
455
- *
456
- * Specification: Implements @[FIPS204 Algorithm 3 (ML-DSA.Verify external mu
457
- * variant)]
458
- *
459
- **************************************************/
470
+ /**
471
+ * Verify signature in "external mu" mode: the caller has already computed
472
+ * the message representative mu = SHAKE256(tr || M', 64), where
473
+ * tr = SHAKE256(pk, 64) and M' is the FIPS 204 formatted message (e.g.
474
+ * 0x00 || ctxlen || ctx || msg for pure ML-DSA). The same mu must have been
475
+ * used at signing time.
476
+ *
477
+ * @spec{Implements @[FIPS204 Algorithm 3 (ML-DSA.Verify external mu variant)].}
478
+ *
479
+ * @param[in] sig Pointer to input signature.
480
+ * @param siglen Length of signature.
481
+ * @param[in] mu Precomputed message representative.
482
+ * @param[in] pk Bit-packed public key.
483
+ * @param context Application context. Only present when
484
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
485
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
486
+ *
487
+ * @retval 0 Success.
488
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was used and an
489
+ * allocation via MLD_CUSTOM_ALLOC returned NULL.
490
+ * @retval MLD_ERR_FAIL Signature verification failed.
491
+ */
460
492
  MLD_MUST_CHECK_RETURN_VALUE
461
493
  MLD_EXTERNAL_API
462
494
  int mld_sign_verify_extmu(const uint8_t *sig, size_t siglen,
@@ -471,27 +503,26 @@ __contract__(
471
503
  ensures(return_value == 0 || return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY)
472
504
  );
473
505
 
474
- /*************************************************
475
- * Name: mld_sign_open
476
- *
477
- * Description: Verify signed message.
478
- *
479
- * Arguments: - uint8_t *m: pointer to output message (allocated array
480
- * with smlen bytes), can be equal to sm
481
- * - size_t *mlen: pointer to output length of message
482
- * - const uint8_t *sm: pointer to signed message
483
- * - size_t smlen: length of signed message
484
- * - const uint8_t *ctx: pointer to context tring
485
- * - size_t ctxlen: length of context string
486
- * - const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]:
487
- * bit-packed public key
488
- *
489
- * Returns: - 0: Success
490
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
491
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
492
- * - MLD_ERR_FAIL: Signature verification failed
493
- *
494
- **************************************************/
506
+ /**
507
+ * Verify signed message.
508
+ *
509
+ * @param[out] m Pointer to output message (allocated array with smlen
510
+ * bytes); can be equal to sm.
511
+ * @param[out] mlen Pointer to output length of message.
512
+ * @param[in] sm Pointer to signed message.
513
+ * @param smlen Length of signed message.
514
+ * @param[in] ctx Pointer to context string.
515
+ * @param ctxlen Length of context string.
516
+ * @param[in] pk Bit-packed public key.
517
+ * @param context Application context. Only present when
518
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
519
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
520
+ *
521
+ * @retval 0 Success.
522
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was used and an
523
+ * allocation via MLD_CUSTOM_ALLOC returned NULL.
524
+ * @retval MLD_ERR_FAIL Signature verification failed.
525
+ */
495
526
  MLD_MUST_CHECK_RETURN_VALUE
496
527
  MLD_EXTERNAL_API
497
528
  int mld_sign_open(uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen,
@@ -510,41 +541,46 @@ __contract__(
510
541
  assigns(memory_slice(mlen, sizeof(size_t)))
511
542
  ensures(return_value == 0 || return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY)
512
543
  );
544
+ #endif /* !MLD_CONFIG_CORE_API_ONLY */
545
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
513
546
 
514
- /*************************************************
515
- * Name: mld_sign_signature_pre_hash_internal
516
- *
517
- * Description: FIPS 204: Algorithm 4 HashML-DSA.Sign.
518
- * Computes signature with pre-hashed message.
519
- *
520
- * Arguments: - uint8_t sig[MLDSA_CRYPTO_BYTES]:
521
- * output signature
522
- * - size_t *siglen: pointer to output length of signature
523
- * - const uint8_t *ph: pointer to pre-hashed message
524
- * - size_t phlen: length of pre-hashed message
525
- * - const uint8_t *ctx: pointer to context string
526
- * - size_t ctxlen: length of context string
527
- * - const uint8_t rnd[MLDSA_RNDBYTES]:
528
- * random seed
529
- * - const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]:
530
- * bit-packed secret key
531
- * - int hashalg: hash algorithm constant (one of
532
- * MLD_PREHASH_*)
533
- *
534
- * Returns: - 0: Success
535
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
536
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
537
- * - MLD_ERR_FAIL: Other kinds of failure
547
+ #if !defined(MLD_CONFIG_CORE_API_ONLY)
548
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
549
+ /**
550
+ * FIPS 204: Algorithm 4 HashML-DSA.Sign. Compute signature with pre-hashed
551
+ * message.
538
552
  *
539
553
  * Supported hash algorithm constants:
540
554
  * MLD_PREHASH_SHA2_224, MLD_PREHASH_SHA2_256, MLD_PREHASH_SHA2_384,
541
555
  * MLD_PREHASH_SHA2_512, MLD_PREHASH_SHA2_512_224, MLD_PREHASH_SHA2_512_256,
542
556
  * MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384,
543
- * MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256
557
+ * MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256.
544
558
  *
545
- * Warning: This is an unstable API that may change in the future. If you need
559
+ * @warning This is an unstable API that may change in the future. If you need
546
560
  * a stable API use mld_sign_signature_pre_hash_shake256.
547
- **************************************************/
561
+ *
562
+ * @param[out] sig Output signature.
563
+ * @param[out] siglen Pointer to output length of signature.
564
+ * @param[in] ph Pointer to pre-hashed message.
565
+ * @param phlen Length of pre-hashed message.
566
+ * @param[in] ctx Pointer to context string.
567
+ * @param ctxlen Length of context string.
568
+ * @param[in] rnd Random seed.
569
+ * @param[in] sk Bit-packed secret key.
570
+ * @param hashalg Hash algorithm constant (one of MLD_PREHASH_*).
571
+ * @param context Application context. Only present when
572
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
573
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
574
+ *
575
+ * @retval 0 Success.
576
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was
577
+ * used and an allocation via
578
+ * MLD_CUSTOM_ALLOC returned NULL.
579
+ * @retval MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED The rejection-sampling loop exceeded
580
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS
581
+ * iterations.
582
+ * @retval MLD_ERR_FAIL Other kinds of failure.
583
+ */
548
584
  MLD_MUST_CHECK_RETURN_VALUE
549
585
  MLD_EXTERNAL_API
550
586
  int mld_sign_signature_pre_hash_internal(
@@ -565,40 +601,41 @@ __contract__(
565
601
  assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES))
566
602
  assigns(object_whole(siglen))
567
603
  ensures((return_value == 0 && *siglen == MLDSA_CRYPTO_BYTES) ||
568
- ((return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY) && *siglen == 0))
604
+ ((return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY || return_value == MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED) && *siglen == 0))
569
605
  );
606
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
570
607
 
571
- /*************************************************
572
- * Name: mld_sign_verify_pre_hash_internal
573
- *
574
- * Description: FIPS 204: Algorithm 5 HashML-DSA.Verify.
575
- * Verifies signature with pre-hashed message.
576
- *
577
- * Arguments: - const uint8_t *sig: pointer to input signature
578
- * - size_t siglen: length of signature
579
- * - const uint8_t *ph: pointer to pre-hashed message
580
- * - size_t phlen: length of pre-hashed message
581
- * - const uint8_t *ctx: pointer to context string
582
- * - size_t ctxlen: length of context string
583
- * - const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]:
584
- * bit-packed public key
585
- * - int hashalg: hash algorithm constant (one of
586
- * MLD_PREHASH_*)
587
- *
588
- * Returns: - 0: Success
589
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
590
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
591
- * - MLD_ERR_FAIL: Signature verification failed
608
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
609
+ /**
610
+ * FIPS 204: Algorithm 5 HashML-DSA.Verify. Verify signature with pre-hashed
611
+ * message.
592
612
  *
593
613
  * Supported hash algorithm constants:
594
614
  * MLD_PREHASH_SHA2_224, MLD_PREHASH_SHA2_256, MLD_PREHASH_SHA2_384,
595
615
  * MLD_PREHASH_SHA2_512, MLD_PREHASH_SHA2_512_224, MLD_PREHASH_SHA2_512_256,
596
616
  * MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384,
597
- * MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256
617
+ * MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256.
598
618
  *
599
- * Warning: This is an unstable API that may change in the future. If you need
619
+ * @warning This is an unstable API that may change in the future. If you need
600
620
  * a stable API use mld_sign_verify_pre_hash_shake256.
601
- **************************************************/
621
+ *
622
+ * @param[in] sig Pointer to input signature.
623
+ * @param siglen Length of signature.
624
+ * @param[in] ph Pointer to pre-hashed message.
625
+ * @param phlen Length of pre-hashed message.
626
+ * @param[in] ctx Pointer to context string.
627
+ * @param ctxlen Length of context string.
628
+ * @param[in] pk Bit-packed public key.
629
+ * @param hashalg Hash algorithm constant (one of MLD_PREHASH_*).
630
+ * @param context Application context. Only present when
631
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
632
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
633
+ *
634
+ * @retval 0 Success.
635
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was used and an
636
+ * allocation via MLD_CUSTOM_ALLOC returned NULL.
637
+ * @retval MLD_ERR_FAIL Signature verification failed.
638
+ */
602
639
  MLD_MUST_CHECK_RETURN_VALUE
603
640
  MLD_EXTERNAL_API
604
641
  int mld_sign_verify_pre_hash_internal(
@@ -616,33 +653,36 @@ __contract__(
616
653
  requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
617
654
  ensures(return_value == 0 || return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY)
618
655
  );
656
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
619
657
 
620
- /*************************************************
621
- * Name: mld_sign_signature_pre_hash_shake256
622
- *
623
- * Description: FIPS 204: Algorithm 4 HashML-DSA.Sign with SHAKE256.
624
- * Computes signature with pre-hashed message using SHAKE256.
625
- * This function computes the SHAKE256 hash of the message
626
- *internally.
627
- *
628
- * Arguments: - uint8_t sig[MLDSA_CRYPTO_BYTES]:
629
- * output signature
630
- * - size_t *siglen: pointer to output length of signature
631
- * - const uint8_t *m: pointer to message to be hashed and signed
632
- * - size_t mlen: length of message
633
- * - const uint8_t *ctx: pointer to context string
634
- * - size_t ctxlen: length of context string
635
- * - const uint8_t rnd[MLDSA_RNDBYTES]:
636
- * random seed
637
- * - const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]:
638
- * bit-packed secret key
639
- *
640
- * Returns: - 0: Success
641
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
642
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
643
- * - MLD_ERR_FAIL: Other kinds of failure
644
- *
645
- **************************************************/
658
+ #if !defined(MLD_CONFIG_NO_SIGN_API)
659
+ /**
660
+ * FIPS 204: Algorithm 4 HashML-DSA.Sign with SHAKE256.
661
+ *
662
+ * Compute signature with pre-hashed message using SHAKE256. This function
663
+ * computes the SHAKE256 hash of the message internally.
664
+ *
665
+ * @param[out] sig Output signature.
666
+ * @param[out] siglen Pointer to output length of signature.
667
+ * @param[in] m Pointer to message to be hashed and signed.
668
+ * @param mlen Length of message.
669
+ * @param[in] ctx Pointer to context string.
670
+ * @param ctxlen Length of context string.
671
+ * @param[in] rnd Random seed.
672
+ * @param[in] sk Bit-packed secret key.
673
+ * @param context Application context. Only present when
674
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
675
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
676
+ *
677
+ * @retval 0 Success.
678
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was
679
+ * used and an allocation via
680
+ * MLD_CUSTOM_ALLOC returned NULL.
681
+ * @retval MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED The rejection-sampling loop exceeded
682
+ * MLD_CONFIG_MAX_SIGNING_ATTEMPTS
683
+ * iterations.
684
+ * @retval MLD_ERR_FAIL Other kinds of failure.
685
+ */
646
686
  MLD_MUST_CHECK_RETURN_VALUE
647
687
  MLD_EXTERNAL_API
648
688
  int mld_sign_signature_pre_hash_shake256(
@@ -663,33 +703,33 @@ __contract__(
663
703
  assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES))
664
704
  assigns(object_whole(siglen))
665
705
  ensures((return_value == 0 && *siglen == MLDSA_CRYPTO_BYTES) ||
666
- ((return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY) && *siglen == 0))
706
+ ((return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY || return_value == MLD_ERR_SIGN_ATTEMPTS_EXHAUSTED) && *siglen == 0))
667
707
  );
708
+ #endif /* !MLD_CONFIG_NO_SIGN_API */
668
709
 
669
- /*************************************************
670
- * Name: mld_sign_verify_pre_hash_shake256
671
- *
672
- * Description: FIPS 204: Algorithm 5 HashML-DSA.Verify with SHAKE256.
673
- * Verifies signature with pre-hashed message using SHAKE256.
674
- * This function computes the SHAKE256 hash of the message
675
- * internally.
676
- *
677
- * Arguments: - const uint8_t *sig: pointer to input signature
678
- * - size_t siglen: length of signature
679
- * - const uint8_t *m: pointer to message to be hashed and
680
- * verified
681
- * - size_t mlen: length of message
682
- * - const uint8_t *ctx: pointer to context string
683
- * - size_t ctxlen: length of context string
684
- * - const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]:
685
- * bit-packed public key
686
- *
687
- * Returns: - 0: Success
688
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
689
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
690
- * - MLD_ERR_FAIL: Signature verification failed
691
- *
692
- **************************************************/
710
+ #if !defined(MLD_CONFIG_NO_VERIFY_API)
711
+ /**
712
+ * FIPS 204: Algorithm 5 HashML-DSA.Verify with SHAKE256.
713
+ *
714
+ * Verify signature with pre-hashed message using SHAKE256. This function
715
+ * computes the SHAKE256 hash of the message internally.
716
+ *
717
+ * @param[in] sig Pointer to input signature.
718
+ * @param siglen Length of signature.
719
+ * @param[in] m Pointer to message to be hashed and verified.
720
+ * @param mlen Length of message.
721
+ * @param[in] ctx Pointer to context string.
722
+ * @param ctxlen Length of context string.
723
+ * @param[in] pk Bit-packed public key.
724
+ * @param context Application context. Only present when
725
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
726
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
727
+ *
728
+ * @retval 0 Success.
729
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was used and an
730
+ * allocation via MLD_CUSTOM_ALLOC returned NULL.
731
+ * @retval MLD_ERR_FAIL Signature verification failed.
732
+ */
693
733
  MLD_MUST_CHECK_RETURN_VALUE
694
734
  MLD_EXTERNAL_API
695
735
  int mld_sign_verify_pre_hash_shake256(
@@ -707,50 +747,47 @@ __contract__(
707
747
  requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
708
748
  ensures(return_value == 0 || return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY)
709
749
  );
750
+ #endif /* !MLD_CONFIG_NO_VERIFY_API */
710
751
 
752
+ #if !defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_NO_VERIFY_API)
711
753
  /* Maximum formatted domain separation message length:
712
754
  * - Pure ML-DSA: 0x00 || ctxlen || ctx (max 255)
713
755
  * - HashML-DSA: 0x01 || ctxlen || ctx (max 255) || oid (11) || ph (max 64) */
714
756
  #define MLD_DOMAIN_SEPARATION_MAX_BYTES (2 + 255 + 11 + 64)
715
757
 
716
- /*************************************************
717
- * Name: mld_prepare_domain_separation_prefix
718
- *
719
- * Description: Prepares domain separation prefix for ML-DSA signing.
720
- * For pure ML-DSA (hashalg == MLD_PREHASH_NONE):
721
- * Format: 0x00 || ctxlen (1 byte) || ctx
722
- * For HashML-DSA (hashalg != MLD_PREHASH_NONE):
723
- * Format: 0x01 || ctxlen (1 byte) || ctx || oid (11 bytes) || ph
724
- *
725
- * Arguments: - uint8_t prefix[MLD_DOMAIN_SEPARATION_MAX_BYTES]:
726
- * output domain separation prefix buffer
727
- * - const uint8_t *ph: pointer to pre-hashed message
728
- * (ignored for pure ML-DSA)
729
- * - size_t phlen: length of pre-hashed message
730
- * (ignored for pure ML-DSA)
731
- * - const uint8_t *ctx: pointer to context string (may be NULL)
732
- * - size_t ctxlen: length of context string
733
- * - int hashalg: hash algorithm constant
734
- * (MLD_PREHASH_NONE for pure ML-DSA, or MLD_PREHASH_* for
735
- * HashML-DSA)
736
- *
737
- * Returns the total length of the formatted prefix, or 0 on error.
758
+ /**
759
+ * Prepare domain separation prefix for ML-DSA signing.
760
+ *
761
+ * For pure ML-DSA (hashalg == MLD_PREHASH_NONE):
762
+ * Format: 0x00 || ctxlen (1 byte) || ctx.
763
+ *
764
+ * For HashML-DSA (hashalg != MLD_PREHASH_NONE):
765
+ * Format: 0x01 || ctxlen (1 byte) || ctx || oid (11 bytes) || ph.
738
766
  *
739
767
  * This function is useful for building incremental signing APIs.
740
768
  *
741
- * Specification:
742
- * - For HashML-DSA (hashalg != MLD_PREHASH_NONE), implements
743
- * @[FIPS204, Algorithm 4, L23]
744
- * - For Pure ML-DSA (hashalg == MLD_PREHASH_NONE), implements
745
- * ```
746
- * M' <- BytesToBits(IntegerToBytes(0, 1)
747
- * || IntegerToBytes(|ctx|, 1)
748
- * || ctx
749
- * ```
750
- * which is part of @[FIPS204, Algorithm 2 (ML-DSA.Sign), L10] and
751
- * @[FIPS204, Algorithm 3 (ML-DSA.Verify), L5].
752
- *
753
- **************************************************/
769
+ * @spec{For HashML-DSA (hashalg != MLD_PREHASH_NONE), implements
770
+ * @[FIPS204, Algorithm 4, L23]. For Pure ML-DSA (hashalg == MLD_PREHASH_NONE),
771
+ * implements
772
+ * ```
773
+ * M' <- BytesToBits(IntegerToBytes(0, 1)
774
+ * || IntegerToBytes(|ctx|, 1)
775
+ * || ctx
776
+ * ```
777
+ * which is part of @[FIPS204, Algorithm 2 (ML-DSA.Sign), L10] and
778
+ * @[FIPS204, Algorithm 3 (ML-DSA.Verify), L5].}
779
+ *
780
+ * @param[out] prefix Output domain separation prefix buffer.
781
+ * @param[in] ph Pointer to pre-hashed message (ignored for pure
782
+ * ML-DSA).
783
+ * @param phlen Length of pre-hashed message (ignored for pure ML-DSA).
784
+ * @param[in] ctx Pointer to context string (may be NULL).
785
+ * @param ctxlen Length of context string.
786
+ * @param hashalg Hash algorithm constant (MLD_PREHASH_NONE for pure
787
+ * ML-DSA, or MLD_PREHASH_* for HashML-DSA).
788
+ *
789
+ * @return The total length of the formatted prefix, or 0 on error.
790
+ */
754
791
  MLD_MUST_CHECK_RETURN_VALUE
755
792
  MLD_EXTERNAL_API
756
793
  size_t mld_prepare_domain_separation_prefix(
@@ -765,33 +802,32 @@ __contract__(
765
802
  assigns(memory_slice(prefix, MLD_DOMAIN_SEPARATION_MAX_BYTES))
766
803
  ensures(return_value <= MLD_DOMAIN_SEPARATION_MAX_BYTES)
767
804
  );
805
+ #endif /* !MLD_CONFIG_NO_SIGN_API || !MLD_CONFIG_NO_VERIFY_API */
768
806
 
769
- /*************************************************
770
- * Name: mld_sign_pk_from_sk
771
- *
772
- * Description: Performs basic validity checks on secret key, and derives
773
- * public key.
774
- *
775
- * Referring to the decoding of the secret key
776
- * `sk=(rho, K, tr, s1, s2, t0)`
777
- * (cf. [@FIPS204, Algorithm 25 skDecode]),
778
- * the following checks are performed:
779
- * - Check that s1 and s2 have coefficients in
780
- * [-MLDSA_ETA, MLDSA_ETA]
781
- * - Check that t0 and tr stored in sk match recomputed values.
782
- *
783
- * Arguments: - uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]: output public key
784
- * - const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]: input secret
785
- * key
786
- *
787
- * Returns: - 0: Success
788
- * - MLD_ERR_OUT_OF_MEMORY: If MLD_CONFIG_CUSTOM_ALLOC_FREE is
789
- * used and an allocation via MLD_CUSTOM_ALLOC returned NULL.
790
- * - MLD_ERR_FAIL: Secret key validation failed
791
- *
792
- * Note: This function leaks whether the secret key is valid or invalid
793
- * through its return value and timing.
794
- **************************************************/
807
+ #if !defined(MLD_CONFIG_NO_KEYPAIR_API)
808
+ /**
809
+ * Perform basic validity checks on secret key, and derive public key.
810
+ *
811
+ * Referring to the decoding of the secret key `sk=(rho, K, tr, s1, s2, t0)`
812
+ * (cf. @[FIPS204, Algorithm 25 skDecode]), the following checks are
813
+ * performed:
814
+ * - Check that s1 and s2 have coefficients in [-MLDSA_ETA, MLDSA_ETA].
815
+ * - Check that t0 and tr stored in sk match recomputed values.
816
+ *
817
+ * @note This function leaks whether the secret key is valid or invalid
818
+ * through its return value and timing.
819
+ *
820
+ * @param[out] pk Output public key.
821
+ * @param[in] sk Input secret key.
822
+ * @param context Application context. Only present when
823
+ * MLD_CONFIG_CONTEXT_PARAMETER is defined; type set by
824
+ * MLD_CONFIG_CONTEXT_PARAMETER_TYPE.
825
+ *
826
+ * @retval 0 Success.
827
+ * @retval MLD_ERR_OUT_OF_MEMORY MLD_CONFIG_CUSTOM_ALLOC_FREE was used and an
828
+ * allocation via MLD_CUSTOM_ALLOC returned NULL.
829
+ * @retval MLD_ERR_FAIL Secret key validation failed.
830
+ */
795
831
  MLD_MUST_CHECK_RETURN_VALUE
796
832
  MLD_EXTERNAL_API
797
833
  int mld_sign_pk_from_sk(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
@@ -803,4 +839,7 @@ __contract__(
803
839
  assigns(memory_slice(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
804
840
  ensures(return_value == 0 || return_value == MLD_ERR_FAIL || return_value == MLD_ERR_OUT_OF_MEMORY)
805
841
  );
842
+ #endif /* !MLD_CONFIG_NO_KEYPAIR_API */
843
+ #endif /* !MLD_CONFIG_CORE_API_ONLY */
844
+
806
845
  #endif /* !MLD_SIGN_H */