pq_crypto 0.6.5 → 0.6.7

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 (157) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +233 -0
  3. data/README.md +16 -3
  4. data/SECURITY.md +46 -0
  5. data/ext/pqcrypto/extconf.rb +8 -1
  6. data/ext/pqcrypto/pq_externalmu.c +35 -0
  7. data/ext/pqcrypto/pqcrypto_native_api.h +91 -75
  8. data/ext/pqcrypto/pqcrypto_ruby_secure.c +97 -9
  9. data/ext/pqcrypto/pqcrypto_secure.c +95 -33
  10. data/ext/pqcrypto/pqcrypto_secure.h +66 -48
  11. data/ext/pqcrypto/pqcrypto_version.h +1 -1
  12. data/ext/pqcrypto/vendor/.vendored +7 -7
  13. data/ext/pqcrypto/vendor/mldsa-native/BUILDING.md +5 -2
  14. data/ext/pqcrypto/vendor/mldsa-native/LICENSE +21 -2
  15. data/ext/pqcrypto/vendor/mldsa-native/README.md +20 -7
  16. data/ext/pqcrypto/vendor/mldsa-native/RELEASE.md +160 -0
  17. data/ext/pqcrypto/vendor/mldsa-native/SECURITY.md +1 -1
  18. data/ext/pqcrypto/vendor/mldsa-native/mldsa/README.md +2 -2
  19. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native.c +85 -59
  20. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native.h +292 -348
  21. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native_asm.S +122 -76
  22. data/ext/pqcrypto/vendor/mldsa-native/mldsa/mldsa_native_config.h +184 -86
  23. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/cbmc.h +49 -4
  24. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/common.h +49 -81
  25. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/context.h +152 -0
  26. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/ct.h +25 -12
  27. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/debug.c +2 -0
  28. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/debug.h +2 -0
  29. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/fips202x4.c +2 -2
  30. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/keccakf1600.c +9 -11
  31. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/auto.h +19 -11
  32. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_scalar_aarch64_asm.S +6 -4
  33. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x1_v84a_aarch64_asm.S +7 -4
  34. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x2_v84a_aarch64_asm.S +7 -4
  35. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_scalar_hybrid_aarch64_asm.S +12 -9
  36. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/src/keccak_f1600_x4_v8a_v84a_scalar_hybrid_aarch64_asm.S +12 -9
  37. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_scalar.h +1 -1
  38. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x1_v84a.h +3 -2
  39. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x2_v84a.h +3 -2
  40. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_scalar.h +6 -1
  41. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/aarch64/x4_v8a_v84a_scalar.h +3 -2
  42. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/api.h +11 -11
  43. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/mve.h +9 -22
  44. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.S +8 -5
  45. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/keccak_f1600_x4_mve.c +1 -0
  46. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/{state_extract_bytes_x4_mve.S → keccak_f1600_x4_state_extract_bytes_mve.S} +14 -14
  47. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/armv81m/src/{state_xor_bytes_x4_mve.S → keccak_f1600_x4_state_xor_bytes_mve.S} +12 -12
  48. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/auto.h +5 -4
  49. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/keccak_f1600_x4_avx2.h +2 -2
  50. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/fips202_native_x86_64.h +1 -0
  51. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/fips202/native/x86_64/src/keccak_f1600_x4_avx2_asm.S +36 -2
  52. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/meta.h +62 -4
  53. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/arith_native_aarch64.h +87 -54
  54. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{intt_aarch64_asm.S → mldsa_intt_aarch64_asm.S} +39 -6
  55. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{ntt_aarch64_asm.S → mldsa_ntt_aarch64_asm.S} +39 -6
  56. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{pointwise_montgomery_aarch64_asm.S → mldsa_pointwise_montgomery_aarch64_asm.S} +25 -3
  57. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{poly_caddq_aarch64_asm.S → mldsa_poly_caddq_aarch64_asm.S} +19 -3
  58. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{poly_chknorm_aarch64_asm.S → mldsa_poly_chknorm_aarch64_asm.S} +24 -3
  59. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{poly_decompose_32_aarch64_asm.S → mldsa_poly_decompose_32_aarch64_asm.S} +25 -3
  60. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{poly_decompose_88_aarch64_asm.S → mldsa_poly_decompose_88_aarch64_asm.S} +25 -3
  61. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{poly_use_hint_32_aarch64_asm.S → mldsa_poly_use_hint_32_aarch64_asm.S} +25 -3
  62. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{poly_use_hint_88_aarch64_asm.S → mldsa_poly_use_hint_88_aarch64_asm.S} +25 -3
  63. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{mld_polyvecl_pointwise_acc_montgomery_l4_aarch64_asm.S → mldsa_polyvecl_pointwise_acc_montgomery_l4_aarch64_asm.S} +31 -3
  64. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{mld_polyvecl_pointwise_acc_montgomery_l5_aarch64_asm.S → mldsa_polyvecl_pointwise_acc_montgomery_l5_aarch64_asm.S} +31 -3
  65. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{mld_polyvecl_pointwise_acc_montgomery_l7_aarch64_asm.S → mldsa_polyvecl_pointwise_acc_montgomery_l7_aarch64_asm.S} +31 -3
  66. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{polyz_unpack_17_aarch64_asm.S → mldsa_polyz_unpack_17_aarch64_asm.S} +31 -3
  67. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{polyz_unpack_19_aarch64_asm.S → mldsa_polyz_unpack_19_aarch64_asm.S} +31 -3
  68. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{rej_uniform_aarch64_asm.S → mldsa_rej_uniform_aarch64_asm.S} +48 -15
  69. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{rej_uniform_eta2_aarch64_asm.S → mldsa_rej_uniform_eta2_aarch64_asm.S} +42 -9
  70. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/aarch64/src/{rej_uniform_eta4_aarch64_asm.S → mldsa_rej_uniform_eta4_aarch64_asm.S} +42 -9
  71. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/api.h +11 -3
  72. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/meta.h +3 -2
  73. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/meta.h +28 -28
  74. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/arith_native_x86_64.h +171 -49
  75. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/{intt_avx2_asm.S → mldsa_intt_avx2_asm.S} +23 -1
  76. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/{ntt_avx2_asm.S → mldsa_ntt_avx2_asm.S} +23 -1
  77. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/{nttunpack_avx2_asm.S → mldsa_nttunpack_avx2_asm.S} +17 -1
  78. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/{pointwise_acc_l4_avx2_asm.S → mldsa_pointwise_acc_l4_avx2_asm.S} +37 -3
  79. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/{pointwise_acc_l5_avx2_asm.S → mldsa_pointwise_acc_l5_avx2_asm.S} +37 -3
  80. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/{pointwise_acc_l7_avx2_asm.S → mldsa_pointwise_acc_l7_avx2_asm.S} +37 -3
  81. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/{pointwise_avx2_asm.S → mldsa_pointwise_avx2_asm.S} +31 -3
  82. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/{poly_caddq_avx2_asm.S → mldsa_poly_caddq_avx2_asm.S} +18 -9
  83. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_chknorm_avx2_asm.S +176 -0
  84. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_decompose_32_avx2_asm.S +490 -0
  85. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_decompose_88_avx2_asm.S +489 -0
  86. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_use_hint_32_avx2_asm.S +123 -0
  87. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_poly_use_hint_88_avx2_asm.S +125 -0
  88. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_polyz_unpack_17_avx2_asm.S +355 -0
  89. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_polyz_unpack_19_avx2_asm.S +355 -0
  90. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_rej_uniform_avx2_asm.S +132 -0
  91. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_rej_uniform_eta2_avx2_asm.S +205 -0
  92. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/mldsa_rej_uniform_eta4_avx2_asm.S +176 -0
  93. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/packing.c +27 -36
  94. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/packing.h +42 -8
  95. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/params.h +93 -17
  96. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly.c +74 -15
  97. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly.h +97 -11
  98. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly_kl.c +7 -38
  99. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/poly_kl.h +49 -7
  100. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec.c +16 -17
  101. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec.h +26 -9
  102. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec_lazy.c +3 -0
  103. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/polyvec_lazy.h +18 -19
  104. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/reduce.h +15 -3
  105. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/rounding.h +28 -6
  106. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sign.c +311 -246
  107. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sign.h +245 -240
  108. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/sys.h +64 -5
  109. data/ext/pqcrypto/vendor/mlkem-native/BUILDING.md +5 -2
  110. data/ext/pqcrypto/vendor/mlkem-native/LICENSE +21 -3
  111. data/ext/pqcrypto/vendor/mlkem-native/README.md +2 -2
  112. data/ext/pqcrypto/vendor/mlkem-native/RELEASE.md +113 -0
  113. data/ext/pqcrypto/vendor/mlkem-native/mlkem/README.md +2 -2
  114. data/ext/pqcrypto/vendor/mlkem-native/mlkem/mlkem_native.c +17 -27
  115. data/ext/pqcrypto/vendor/mlkem-native/mlkem/mlkem_native.h +68 -151
  116. data/ext/pqcrypto/vendor/mlkem-native/mlkem/mlkem_native_asm.S +17 -27
  117. data/ext/pqcrypto/vendor/mlkem-native/mlkem/mlkem_native_config.h +46 -44
  118. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/cbmc.h +25 -0
  119. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/common.h +37 -6
  120. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/context.h +9 -0
  121. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/fips202.h +2 -2
  122. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/keccakf1600.c +8 -8
  123. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x1_scalar.h +1 -1
  124. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x1_v84a.h +3 -3
  125. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x2_v84a.h +3 -3
  126. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x4_v8a_scalar.h +2 -2
  127. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/aarch64/x4_v8a_v84a_scalar.h +3 -3
  128. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/api.h +11 -11
  129. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/armv81m/mve.h +3 -3
  130. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/fips202/native/x86_64/keccak_f1600_x4_avx2.h +2 -2
  131. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/indcpa.h +14 -11
  132. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/kem.c +28 -11
  133. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/kem.h +39 -14
  134. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/aarch64/meta.h +10 -10
  135. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/native/x86_64/meta.h +20 -20
  136. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/sys.h +5 -5
  137. data/ext/pqcrypto/vendor/mlkem-native/mlkem/src/verify.h +11 -10
  138. data/lib/pq_crypto/internal.rb +10 -0
  139. data/lib/pq_crypto/kem.rb +47 -5
  140. data/lib/pq_crypto/key.rb +14 -8
  141. data/lib/pq_crypto/pkcs8.rb +13 -5
  142. data/lib/pq_crypto/signature.rb +34 -9
  143. data/lib/pq_crypto/spki.rb +4 -2
  144. data/lib/pq_crypto/version.rb +1 -1
  145. data/lib/pq_crypto.rb +9 -0
  146. data/script/vendor_libs.rb +6 -6
  147. metadata +40 -38
  148. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_chknorm_avx2.c +0 -52
  149. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_decompose_32_avx2.c +0 -157
  150. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_decompose_88_avx2.c +0 -157
  151. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_use_hint_32_avx2.c +0 -103
  152. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/poly_use_hint_88_avx2.c +0 -105
  153. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/polyz_unpack_17_avx2.c +0 -94
  154. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/polyz_unpack_19_avx2.c +0 -96
  155. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_avx2.c +0 -126
  156. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_eta2_avx2.c +0 -157
  157. data/ext/pqcrypto/vendor/mldsa-native/mldsa/src/native/x86_64/src/rej_uniform_eta4_avx2.c +0 -141
@@ -104,7 +104,7 @@
104
104
  *
105
105
  * By default, mlkem-native includes support for generating key pairs.
106
106
  * If you don't need this, set MLK_CONFIG_NO_KEYPAIR_API to exclude
107
- * crypto_kem_keypair and crypto_kem_keypair_derand, and all internal
107
+ * keypair and keypair_derand, and all internal
108
108
  * APIs only needed by those functions.
109
109
  */
110
110
  /* #define MLK_CONFIG_NO_KEYPAIR_API */
@@ -114,11 +114,11 @@
114
114
  *
115
115
  * By default, mlkem-native includes support for encapsulation. If you
116
116
  * don't need this, set MLK_CONFIG_NO_ENCAPS_API to exclude
117
- * crypto_kem_enc, crypto_kem_enc_derand, crypto_kem_check_pk, and
117
+ * enc, enc_derand, check_pk, and
118
118
  * all internal APIs only needed by those functions.
119
119
  *
120
120
  * @note Setting this option is incompatible with MLK_CONFIG_KEYGEN_PCT
121
- * as the current PCT implementation requires crypto_kem_enc().
121
+ * as the current PCT implementation requires enc().
122
122
  */
123
123
  /* #define MLK_CONFIG_NO_ENCAPS_API */
124
124
 
@@ -127,11 +127,11 @@
127
127
  *
128
128
  * By default, mlkem-native includes support for decapsulation. If you
129
129
  * don't need this, set MLK_CONFIG_NO_DECAPS_API to exclude
130
- * crypto_kem_dec, crypto_kem_check_sk, and all internal APIs only
130
+ * dec, check_sk, and all internal APIs only
131
131
  * needed by those functions.
132
132
  *
133
133
  * @note Setting this option is incompatible with MLK_CONFIG_KEYGEN_PCT
134
- * as the current PCT implementation requires crypto_kem_dec().
134
+ * as the current PCT implementation requires dec().
135
135
  */
136
136
  /* #define MLK_CONFIG_NO_DECAPS_API */
137
137
 
@@ -139,28 +139,16 @@
139
139
  * MLK_CONFIG_NO_RANDOMIZED_API
140
140
  *
141
141
  * If this option is set, mlkem-native will be built without the randomized
142
- * API functions (crypto_kem_keypair and crypto_kem_enc). This allows users
142
+ * API functions (keypair and enc). This allows users
143
143
  * to build mlkem-native without providing a randombytes() implementation
144
- * if they only need the deterministic API (crypto_kem_keypair_derand,
145
- * crypto_kem_enc_derand, crypto_kem_dec).
144
+ * if they only need the deterministic API (keypair_derand,
145
+ * enc_derand, dec).
146
146
  *
147
147
  * @note This option is incompatible with MLK_CONFIG_KEYGEN_PCT as the
148
- * current PCT implementation requires crypto_kem_enc().
148
+ * current PCT implementation requires enc().
149
149
  */
150
150
  /* #define MLK_CONFIG_NO_RANDOMIZED_API */
151
151
 
152
- /**
153
- * MLK_CONFIG_NO_SUPERCOP
154
- *
155
- * By default, mlkem_native.h exposes the mlkem-native API in the SUPERCOP
156
- * naming convention (crypto_kem_xxx). If you don't need this, set
157
- * MLK_CONFIG_NO_SUPERCOP.
158
- *
159
- * @note You must set this for a multi-level build as the SUPERCOP naming
160
- * does not disambiguate between the parameter sets.
161
- */
162
- /* #define MLK_CONFIG_NO_SUPERCOP */
163
-
164
152
  /**
165
153
  * MLK_CONFIG_CONSTANTS_ONLY
166
154
  *
@@ -328,7 +316,7 @@
328
316
  * implementation, different from the one shipped with mlkem-native.
329
317
  *
330
318
  * If set, it must be the name of a file serving as the replacement for
331
- * mlkem/fips202/fips202.h, and exposing the same API (see FIPS202.md).
319
+ * mlkem/src/fips202/fips202.h, and exposing the same API (see FIPS202.md).
332
320
  */
333
321
  /* #define MLK_CONFIG_FIPS202_CUSTOM_HEADER "SOME_FILE.h" */
334
322
 
@@ -341,7 +329,7 @@
341
329
  * implementation, different from the one shipped with mlkem-native.
342
330
  *
343
331
  * If set, it must be the name of a file serving as the replacement for
344
- * mlkem/fips202/fips202x4.h, and exposing the same API (see FIPS202.md).
332
+ * mlkem/src/fips202/fips202x4.h, and exposing the same API (see FIPS202.md).
345
333
  */
346
334
  /* #define MLK_CONFIG_FIPS202X4_CUSTOM_HEADER "SOME_FILE.h" */
347
335
 
@@ -349,25 +337,35 @@
349
337
  * MLK_CONFIG_CUSTOM_ZEROIZE
350
338
  *
351
339
  * In compliance with @[FIPS203, Section 3.3], mlkem-native zeroizes
352
- * intermediate stack buffers before returning from function calls.
340
+ * intermediate buffers before returning from function calls. By default,
341
+ * those buffers are allocated from the stack; if MLK_CONFIG_CUSTOM_ALLOC_FREE
342
+ * is set, they are (mostly -- few exceptions remain at present) allocated from
343
+ * the configured custom allocator.
344
+ *
345
+ * mlkem-native also zeroizes caller-owned output buffers as needed to uphold
346
+ * the API convention that outputs be either unmodified or zeroized upon
347
+ * failure.
353
348
  *
354
349
  * Set this option and define `mlk_zeroize` if you want to use a custom
355
- * method to zeroize intermediate stack buffers. The default implementation
356
- * uses SecureZeroMemory on Windows and a memset + compiler barrier
357
- * otherwise. If neither of those is available on the target platform,
358
- * compilation will fail, and you will need to use MLK_CONFIG_CUSTOM_ZEROIZE
359
- * to provide a custom implementation of `mlk_zeroize()`.
360
- *
361
- * @warning The explicit stack zeroization conducted by mlkem-native reduces
362
- * the likelihood of data leaking on the stack, but does not
363
- * eliminate it. The C standard makes no guarantee about where a
364
- * compiler allocates structures and whether/where it makes copies
365
- * of them. Also, in addition to entire structures, there may also
366
- * be potentially exploitable leakage of individual values on the
367
- * stack. If you need bullet-proof zeroization of the stack, you
368
- * need to consider additional measures instead of what this
369
- * feature provides. In this case, you can set mlk_zeroize to a
370
- * no-op.
350
+ * method to zeroize intermediate and output buffers.
351
+ *
352
+ * The default implementation uses SecureZeroMemory on Windows and a
353
+ * memset + compiler barrier otherwise. If neither of those is available on
354
+ * the target platform, compilation will fail, and you will need to use
355
+ * MLK_CONFIG_CUSTOM_ZEROIZE to provide a custom implementation of
356
+ * `mlk_zeroize()`.
357
+ *
358
+ * @warning
359
+ * The zeroization conducted by mlkem-native reduces the likelihood of data
360
+ * leaking on the stack or custom allocators, but it does not eliminate it.
361
+ * For example, the C standard makes no guarantee about where a compiler
362
+ * allocates local structures and whether/where it makes copies of them.
363
+ * Also, in addition to entire structures, there may also be potentially
364
+ * exploitable leakage of individual values on the stack. If you need
365
+ * bullet-proof zeroization of the stack, you need to consider additional
366
+ * measures instead of what this feature provides. In this case, you can
367
+ * set mlk_zeroize to a no-op. Note that in this case you are also responsible
368
+ * for zeroizing output buffers upon failure.
371
369
  */
372
370
  /* #define MLK_CONFIG_CUSTOM_ZEROIZE
373
371
  #if !defined(__ASSEMBLER__)
@@ -458,7 +456,7 @@
458
456
  *
459
457
  * @warning This option is experimental. Its scope, configuration and
460
458
  * function/macro signatures may change at any time. We expect a
461
- * stable API for v2.
459
+ * stable API in a future version.
462
460
  *
463
461
  * @note Even if this option is set, some allocations further down the call
464
462
  * stack will still be made from the stack, consuming up to 3KB of
@@ -585,8 +583,8 @@
585
583
  * can be exported.
586
584
  *
587
585
  * Set this option if such a check should be implemented. In this case,
588
- * crypto_kem_keypair_derand and crypto_kem_keypair will return a non-zero
589
- * error code if the PCT failed.
586
+ * keypair_derand and keypair will return
587
+ * MLK_ERR_PCT_FAIL if the PCT failed.
590
588
  *
591
589
  * @note This feature will drastically lower the performance of key
592
590
  * generation.
@@ -670,7 +668,11 @@
670
668
  * e.g., PQCP_MLKEM_NATIVE_MLKEM512_
671
669
  */
672
670
 
673
- #if MLK_CONFIG_PARAMETER_SET == 512
671
+ #if defined(MLK_CONFIG_MULTILEVEL_BUILD)
672
+ /* In a multi-level build the parameter set is appended by the namespacing
673
+ * machinery, so the default prefix must not embed it. */
674
+ #define MLK_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM
675
+ #elif MLK_CONFIG_PARAMETER_SET == 512
674
676
  #define MLK_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM512
675
677
  #elif MLK_CONFIG_PARAMETER_SET == 768
676
678
  #define MLK_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM768
@@ -180,8 +180,33 @@
180
180
 
181
181
  #define array_unchanged_u64(array_var, N) \
182
182
  array_unchanged_u64_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var))
183
+
184
+ #define array_unchanged_u8_core(qvar, array_var, N) \
185
+ forall(qvar, 0, (N), \
186
+ ((array_var)[(qvar)]) == (old(* (uint8_t (*)[(N)])(array_var)))[(qvar)])
187
+
188
+ #define array_unchanged_u8(array_var, N) \
189
+ array_unchanged_u8_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), (array_var), (N))
190
+
191
+ #define array_zeroized_u8_core(qvar, array_var, N) \
192
+ forall(qvar, 0, (N), ((array_var)[(qvar)]) == 0)
193
+
194
+ #define array_zeroized_u8(array_var, N) \
195
+ array_zeroized_u8_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), (array_var), (N))
183
196
  /* clang-format on */
184
197
 
198
+ /*
199
+ * Output-buffer discipline on failure, as documented in API-CONVENTIONS.md:
200
+ * when a function fails, each caller-owned output buffer is left either
201
+ * fully unchanged or fully zeroized -- never holding partially computed or
202
+ * stale data that could be mistaken for a valid result.
203
+ *
204
+ * Note the disjunction is over the buffer as a whole: it is not enough for
205
+ * each byte to be individually either unchanged or zero.
206
+ */
207
+ #define array_unchanged_or_zeroized_u8(array_var, N) \
208
+ (array_unchanged_u8((array_var), (N)) || array_zeroized_u8((array_var), (N)))
209
+
185
210
  /* Wrapper around array_bound operating on absolute values.
186
211
  *
187
212
  * The absolute value bound `k` is exclusive.
@@ -2,6 +2,22 @@
2
2
  * Copyright (c) The mlkem-native project authors
3
3
  * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
4
  */
5
+
6
+ /* References
7
+ * ==========
8
+ *
9
+ * - [FIPS140_3_IG]
10
+ * Implementation Guidance for FIPS 140-3 and the Cryptographic Module
11
+ * Validation Program
12
+ * National Institute of Standards and Technology
13
+ * https://csrc.nist.gov/projects/cryptographic-module-validation-program/fips-140-3-ig-announcements
14
+ *
15
+ * - [FIPS203]
16
+ * FIPS 203 Module-Lattice-Based Key-Encapsulation Mechanism Standard
17
+ * National Institute of Standards and Technology
18
+ * https://csrc.nist.gov/pubs/fips/203/final
19
+ */
20
+
5
21
  #ifndef MLK_COMMON_H
6
22
  #define MLK_COMMON_H
7
23
 
@@ -64,7 +80,7 @@
64
80
  #define MLK_NAMESPACE_K(s) MLK_CONCAT(MLK_NAMESPACE_PREFIX_K, s)
65
81
 
66
82
  /* On Apple platforms, we need to emit leading underscore
67
- * in front of assembly symbols. We thus introducee a separate
83
+ * in front of assembly symbols. We thus introduce a separate
68
84
  * namespace wrapper for ASM symbols. */
69
85
  #if !defined(__APPLE__)
70
86
  #define MLK_ASM_NAMESPACE(sym) MLK_NAMESPACE(sym)
@@ -122,15 +138,15 @@
122
138
  #endif
123
139
 
124
140
  #if defined(MLK_CONFIG_NO_RANDOMIZED_API) && defined(MLK_CONFIG_KEYGEN_PCT)
125
- #error Bad configuration: MLK_CONFIG_NO_RANDOMIZED_API is incompatible with MLK_CONFIG_KEYGEN_PCT as the current PCT implementation requires crypto_kem_enc()
141
+ #error Bad configuration: MLK_CONFIG_NO_RANDOMIZED_API is incompatible with MLK_CONFIG_KEYGEN_PCT as the current PCT implementation requires enc()
126
142
  #endif
127
143
 
128
144
  #if defined(MLK_CONFIG_NO_ENCAPS_API) && defined(MLK_CONFIG_KEYGEN_PCT)
129
- #error Bad configuration: MLK_CONFIG_NO_ENCAPS_API is incompatible with MLK_CONFIG_KEYGEN_PCT as the current PCT implementation requires crypto_kem_enc()
145
+ #error Bad configuration: MLK_CONFIG_NO_ENCAPS_API is incompatible with MLK_CONFIG_KEYGEN_PCT as the current PCT implementation requires enc()
130
146
  #endif
131
147
 
132
148
  #if defined(MLK_CONFIG_NO_DECAPS_API) && defined(MLK_CONFIG_KEYGEN_PCT)
133
- #error Bad configuration: MLK_CONFIG_NO_DECAPS_API is incompatible with MLK_CONFIG_KEYGEN_PCT as the current PCT implementation requires crypto_kem_dec()
149
+ #error Bad configuration: MLK_CONFIG_NO_DECAPS_API is incompatible with MLK_CONFIG_KEYGEN_PCT as the current PCT implementation requires dec()
134
150
  #endif
135
151
 
136
152
  #if defined(MLK_CONFIG_USE_NATIVE_BACKEND_ARITH)
@@ -220,6 +236,7 @@
220
236
  #define MLK_FREE(v, T, N, context) \
221
237
  do \
222
238
  { \
239
+ MLK_CONTEXT_UNUSED(context); \
223
240
  mlk_zeroize(mlk_alloc_##v, sizeof(mlk_alloc_##v)); \
224
241
  (v) = NULL; \
225
242
  } while (0)
@@ -251,14 +268,28 @@
251
268
 
252
269
  /****************************** Error codes ***********************************/
253
270
 
254
- /* Generic failure condition */
271
+ /* Generic failure condition. Currently not returned by any function;
272
+ * reserved for failures that no more specific code covers. */
255
273
  #define MLK_ERR_FAIL (-1)
256
274
  /* An allocation failed. This can only happen if MLK_CONFIG_CUSTOM_ALLOC_FREE
257
275
  * is defined and the provided MLK_CUSTOM_ALLOC can fail. */
258
276
  #define MLK_ERR_OUT_OF_MEMORY (-2)
259
- /* An rng failure occured. Might be due to insufficient entropy or
277
+ /* An RNG failure occurred. Might be due to insufficient entropy or
260
278
  * system misconfiguration. */
261
279
  #define MLK_ERR_RNG_FAIL (-3)
280
+ /* Public key validation failed: the @[FIPS203, Section 7.2, 'modulus check']
281
+ * found a coefficient outside [0,q-1]. Returned by check_pk and by the
282
+ * encapsulation API. */
283
+ #define MLK_ERR_INVALID_PK (-4)
284
+ /* Secret key validation failed: the @[FIPS203, Section 7.3, 'hash check']
285
+ * found the embedded public key hash inconsistent. Returned by check_sk and
286
+ * by the decapsulation API. */
287
+ #define MLK_ERR_INVALID_SK (-5)
288
+ /* The 'Pairwise Consistency Test' @[FIPS140_3_IG, p.87] and
289
+ * @[FIPS203, Section 7.1, Pairwise Consistency] failed. Only possible when
290
+ * MLK_CONFIG_KEYGEN_PCT is enabled; signals that the freshly generated key
291
+ * pair failed its encaps/decaps self-test. */
292
+ #define MLK_ERR_PCT_FAIL (-6)
262
293
 
263
294
  #endif /* !__ASSEMBLER__ */
264
295
 
@@ -34,6 +34,15 @@
34
34
  (arg0, arg1, arg2, arg3)
35
35
  #endif /* !MLK_CONFIG_CONTEXT_PARAMETER */
36
36
 
37
+ /* Consume a context parameter carried only for the integration's benefit,
38
+ * avoiding -Wunused-parameter; expands to nothing when no context is
39
+ * configured. */
40
+ #if defined(MLK_CONFIG_CONTEXT_PARAMETER)
41
+ #define MLK_CONTEXT_UNUSED(context) ((void)(context))
42
+ #else
43
+ #define MLK_CONTEXT_UNUSED(context) ((void)0)
44
+ #endif
45
+
37
46
  #if defined(MLK_CONFIG_CONTEXT_PARAMETER_TYPE) != \
38
47
  defined(MLK_CONFIG_CONTEXT_PARAMETER)
39
48
  #error MLK_CONFIG_CONTEXT_PARAMETER_TYPE must be defined if and only if MLK_CONFIG_CONTEXT_PARAMETER is defined
@@ -132,12 +132,12 @@ __contract__(
132
132
  );
133
133
 
134
134
  #if !defined(MLK_CONFIG_USE_NATIVE_BACKEND_FIPS202) || \
135
- !defined(MLK_USE_FIPS202_X4_NATIVE)
135
+ !defined(MLK_USE_NATIVE_FIPS202_X4)
136
136
  /* If you provide your own FIPS-202 implementation where the x4-
137
137
  * Keccak-f1600-x4 implementation falls back to 4-fold Keccak-f1600,
138
138
  * set this to gain a small speedup. */
139
139
  #define FIPS202_X4_DEFAULT_IMPLEMENTATION
140
- #endif /* !MLK_CONFIG_USE_NATIVE_BACKEND_FIPS202 || !MLK_USE_FIPS202_X4_NATIVE \
140
+ #endif /* !MLK_CONFIG_USE_NATIVE_BACKEND_FIPS202 || !MLK_USE_NATIVE_FIPS202_X4 \
141
141
  */
142
142
 
143
143
 
@@ -115,14 +115,14 @@ void mlk_keccakf1600x4_extract_bytes(uint64_t *state, unsigned char *data0,
115
115
  unsigned char *data3, unsigned offset,
116
116
  unsigned length)
117
117
  {
118
- #if defined(MLK_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE)
118
+ #if defined(MLK_USE_NATIVE_FIPS202_X4_EXTRACT_BYTES)
119
119
  if (mlk_keccakf1600_extract_bytes_x4_native(state, data0, data1, data2, data3,
120
120
  offset, length) ==
121
121
  MLK_NATIVE_FUNC_SUCCESS)
122
122
  {
123
123
  return;
124
124
  }
125
- #endif /* MLK_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE */
125
+ #endif /* MLK_USE_NATIVE_FIPS202_X4_EXTRACT_BYTES */
126
126
  mlk_keccakf1600x4_extract_bytes_c(state, data0, data1, data2, data3, offset,
127
127
  length);
128
128
  }
@@ -164,26 +164,26 @@ void mlk_keccakf1600x4_xor_bytes(uint64_t *state, const unsigned char *data0,
164
164
  const unsigned char *data3, unsigned offset,
165
165
  unsigned length)
166
166
  {
167
- #if defined(MLK_USE_FIPS202_X4_XOR_BYTES_NATIVE)
167
+ #if defined(MLK_USE_NATIVE_FIPS202_X4_XOR_BYTES)
168
168
  if (mlk_keccakf1600_xor_bytes_x4_native(state, data0, data1, data2, data3,
169
169
  offset,
170
170
  length) == MLK_NATIVE_FUNC_SUCCESS)
171
171
  {
172
172
  return;
173
173
  }
174
- #endif /* MLK_USE_FIPS202_X4_XOR_BYTES_NATIVE */
174
+ #endif /* MLK_USE_NATIVE_FIPS202_X4_XOR_BYTES */
175
175
  mlk_keccakf1600x4_xor_bytes_c(state, data0, data1, data2, data3, offset,
176
176
  length);
177
177
  }
178
178
 
179
179
  void mlk_keccakf1600x4_permute(uint64_t *state)
180
180
  {
181
- #if defined(MLK_USE_FIPS202_X4_NATIVE)
181
+ #if defined(MLK_USE_NATIVE_FIPS202_X4)
182
182
  if (mlk_keccak_f1600_x4_native(state) == MLK_NATIVE_FUNC_SUCCESS)
183
183
  {
184
184
  return;
185
185
  }
186
- #endif /* MLK_USE_FIPS202_X4_NATIVE */
186
+ #endif /* MLK_USE_NATIVE_FIPS202_X4 */
187
187
  mlk_keccakf1600_permute(state + MLK_KECCAK_LANES * 0);
188
188
  mlk_keccakf1600_permute(state + MLK_KECCAK_LANES * 1);
189
189
  mlk_keccakf1600_permute(state + MLK_KECCAK_LANES * 2);
@@ -478,12 +478,12 @@ __contract__(
478
478
 
479
479
  void mlk_keccakf1600_permute(uint64_t *state)
480
480
  {
481
- #if defined(MLK_USE_FIPS202_X1_NATIVE)
481
+ #if defined(MLK_USE_NATIVE_FIPS202_X1)
482
482
  if (mlk_keccak_f1600_x1_native(state) == MLK_NATIVE_FUNC_SUCCESS)
483
483
  {
484
484
  return;
485
485
  }
486
- #endif /* MLK_USE_FIPS202_X1_NATIVE */
486
+ #endif /* MLK_USE_NATIVE_FIPS202_X1 */
487
487
  mlk_keccakf1600_permute_c(state);
488
488
  }
489
489
 
@@ -7,7 +7,7 @@
7
7
  #define MLK_FIPS202_NATIVE_AARCH64_X1_SCALAR_H
8
8
 
9
9
  /* Part of backend API */
10
- #define MLK_USE_FIPS202_X1_NATIVE
10
+ #define MLK_USE_NATIVE_FIPS202_X1
11
11
  /* Guard for assembly file */
12
12
  #define MLK_FIPS202_AARCH64_NEED_X1_SCALAR
13
13
 
@@ -11,7 +11,7 @@
11
11
  #endif
12
12
 
13
13
  /* Part of backend API */
14
- #define MLK_USE_FIPS202_X1_NATIVE
14
+ #define MLK_USE_NATIVE_FIPS202_X1
15
15
  /* Guard for assembly file */
16
16
  #define MLK_FIPS202_AARCH64_NEED_X1_V84A
17
17
 
@@ -21,8 +21,8 @@
21
21
  MLK_MUST_CHECK_RETURN_VALUE
22
22
  static MLK_INLINE int mlk_keccak_f1600_x1_native(uint64_t *state)
23
23
  {
24
- if (!mlk_sys_check_capability(MLK_SYS_CAP_NEON) ||
25
- !mlk_sys_check_capability(MLK_SYS_CAP_SHA3))
24
+ if (!mlk_sys_check_capability(MLK_SYS_CAP_AARCH64_NEON) ||
25
+ !mlk_sys_check_capability(MLK_SYS_CAP_AARCH64_SHA3))
26
26
  {
27
27
  return MLK_NATIVE_FUNC_FALLBACK;
28
28
  }
@@ -11,7 +11,7 @@
11
11
  #endif
12
12
 
13
13
  /* Part of backend API */
14
- #define MLK_USE_FIPS202_X4_NATIVE
14
+ #define MLK_USE_NATIVE_FIPS202_X4
15
15
  /* Guard for assembly file */
16
16
  #define MLK_FIPS202_AARCH64_NEED_X2_V84A
17
17
 
@@ -21,8 +21,8 @@
21
21
  MLK_MUST_CHECK_RETURN_VALUE
22
22
  static MLK_INLINE int mlk_keccak_f1600_x4_native(uint64_t *state)
23
23
  {
24
- if (!mlk_sys_check_capability(MLK_SYS_CAP_NEON) ||
25
- !mlk_sys_check_capability(MLK_SYS_CAP_SHA3))
24
+ if (!mlk_sys_check_capability(MLK_SYS_CAP_AARCH64_NEON) ||
25
+ !mlk_sys_check_capability(MLK_SYS_CAP_AARCH64_SHA3))
26
26
  {
27
27
  return MLK_NATIVE_FUNC_FALLBACK;
28
28
  }
@@ -7,7 +7,7 @@
7
7
  #define MLK_FIPS202_NATIVE_AARCH64_X4_V8A_SCALAR_H
8
8
 
9
9
  /* Part of backend API */
10
- #define MLK_USE_FIPS202_X4_NATIVE
10
+ #define MLK_USE_NATIVE_FIPS202_X4
11
11
  /* Guard for assembly file */
12
12
  #define MLK_FIPS202_AARCH64_NEED_X4_V8A_SCALAR_HYBRID
13
13
 
@@ -17,7 +17,7 @@
17
17
  MLK_MUST_CHECK_RETURN_VALUE
18
18
  static MLK_INLINE int mlk_keccak_f1600_x4_native(uint64_t *state)
19
19
  {
20
- if (!mlk_sys_check_capability(MLK_SYS_CAP_NEON))
20
+ if (!mlk_sys_check_capability(MLK_SYS_CAP_AARCH64_NEON))
21
21
  {
22
22
  return MLK_NATIVE_FUNC_FALLBACK;
23
23
  }
@@ -11,7 +11,7 @@
11
11
  #endif
12
12
 
13
13
  /* Part of backend API */
14
- #define MLK_USE_FIPS202_X4_NATIVE
14
+ #define MLK_USE_NATIVE_FIPS202_X4
15
15
  /* Guard for assembly file */
16
16
  #define MLK_FIPS202_AARCH64_NEED_X4_V8A_V84A_SCALAR_HYBRID
17
17
 
@@ -21,8 +21,8 @@
21
21
  MLK_MUST_CHECK_RETURN_VALUE
22
22
  static MLK_INLINE int mlk_keccak_f1600_x4_native(uint64_t *state)
23
23
  {
24
- if (!mlk_sys_check_capability(MLK_SYS_CAP_NEON) ||
25
- !mlk_sys_check_capability(MLK_SYS_CAP_SHA3))
24
+ if (!mlk_sys_check_capability(MLK_SYS_CAP_AARCH64_NEON) ||
25
+ !mlk_sys_check_capability(MLK_SYS_CAP_AARCH64_SHA3))
26
26
  {
27
27
  return MLK_NATIVE_FUNC_FALLBACK;
28
28
  }
@@ -29,13 +29,13 @@
29
29
  * A _backend_ is a specific implementation of parts of this interface.
30
30
  *
31
31
  * You can replace 1-fold or 4-fold batched Keccak-F1600.
32
- * To enable, set MLK_USE_FIPS202_X1_NATIVE or MLK_USE_FIPS202_X4_NATIVE
32
+ * To enable, set MLK_USE_NATIVE_FIPS202_X1 or MLK_USE_NATIVE_FIPS202_X4
33
33
  * in your backend, and define the inline wrappers mlk_keccak_f1600_x1_native()
34
34
  * and/or mlk_keccak_f1600_x4_native(), respectively, to forward to your
35
35
  * implementation.
36
36
  */
37
37
 
38
- #if defined(MLK_USE_FIPS202_X1_NATIVE)
38
+ #if defined(MLK_USE_NATIVE_FIPS202_X1)
39
39
  MLK_MUST_CHECK_RETURN_VALUE
40
40
  static MLK_INLINE int mlk_keccak_f1600_x1_native(uint64_t *state)
41
41
  __contract__(
@@ -43,8 +43,8 @@ __contract__(
43
43
  assigns(memory_slice(state, sizeof(uint64_t) * 25 * 1))
44
44
  ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
45
45
  ensures((return_value == MLK_NATIVE_FUNC_FALLBACK) ==> array_unchanged_u64(state, 25 * 1)));
46
- #endif /* MLK_USE_FIPS202_X1_NATIVE */
47
- #if defined(MLK_USE_FIPS202_X4_NATIVE)
46
+ #endif /* MLK_USE_NATIVE_FIPS202_X1 */
47
+ #if defined(MLK_USE_NATIVE_FIPS202_X4)
48
48
  MLK_MUST_CHECK_RETURN_VALUE
49
49
  static MLK_INLINE int mlk_keccak_f1600_x4_native(uint64_t *state)
50
50
  __contract__(
@@ -52,7 +52,7 @@ __contract__(
52
52
  assigns(memory_slice(state, sizeof(uint64_t) * 25 * 4))
53
53
  ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
54
54
  ensures((return_value == MLK_NATIVE_FUNC_FALLBACK) ==> array_unchanged_u64(state, 25 * 4)));
55
- #endif /* MLK_USE_FIPS202_X4_NATIVE */
55
+ #endif /* MLK_USE_NATIVE_FIPS202_X4 */
56
56
 
57
57
  /*
58
58
  * Native x4 XOR bytes and extract bytes interface.
@@ -66,12 +66,12 @@ __contract__(
66
66
  * NOTE: We assume that the custom representation of the zero state is the
67
67
  * all-zero state.
68
68
  *
69
- * MLK_USE_FIPS202_X4_XOR_BYTES_NATIVE: Backend provides native XOR bytes
70
- * MLK_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE: Backend provides native extract
69
+ * MLK_USE_NATIVE_FIPS202_X4_XOR_BYTES: Backend provides native XOR bytes
70
+ * MLK_USE_NATIVE_FIPS202_X4_EXTRACT_BYTES: Backend provides native extract
71
71
  * bytes
72
72
  */
73
73
 
74
- #if defined(MLK_USE_FIPS202_X4_XOR_BYTES_NATIVE)
74
+ #if defined(MLK_USE_NATIVE_FIPS202_X4_XOR_BYTES)
75
75
  MLK_MUST_CHECK_RETURN_VALUE
76
76
  static MLK_INLINE int mlk_keccakf1600_xor_bytes_x4_native(
77
77
  uint64_t *state, const unsigned char *data0, const unsigned char *data1,
@@ -91,9 +91,9 @@ __contract__(
91
91
  assigns(memory_slice(state, sizeof(uint64_t) * 25 * 4))
92
92
  ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
93
93
  ensures((return_value == MLK_NATIVE_FUNC_FALLBACK) ==> array_unchanged_u64(state, 25 * 4)));
94
- #endif /* MLK_USE_FIPS202_X4_XOR_BYTES_NATIVE */
94
+ #endif /* MLK_USE_NATIVE_FIPS202_X4_XOR_BYTES */
95
95
 
96
- #if defined(MLK_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE)
96
+ #if defined(MLK_USE_NATIVE_FIPS202_X4_EXTRACT_BYTES)
97
97
  MLK_MUST_CHECK_RETURN_VALUE
98
98
  static MLK_INLINE int mlk_keccakf1600_extract_bytes_x4_native(
99
99
  uint64_t *state, unsigned char *data0, unsigned char *data1,
@@ -112,6 +112,6 @@ __contract__(
112
112
  assigns(memory_slice(data2, length))
113
113
  assigns(memory_slice(data3, length))
114
114
  ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS));
115
- #endif /* MLK_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE */
115
+ #endif /* MLK_USE_NATIVE_FIPS202_X4_EXTRACT_BYTES */
116
116
 
117
117
  #endif /* !MLK_FIPS202_NATIVE_API_H */
@@ -9,9 +9,9 @@
9
9
  #define MLK_FIPS202_NATIVE_ARMV81M
10
10
 
11
11
  /* Part of backend API */
12
- #define MLK_USE_FIPS202_X4_NATIVE
13
- #define MLK_USE_FIPS202_X4_XOR_BYTES_NATIVE
14
- #define MLK_USE_FIPS202_X4_EXTRACT_BYTES_NATIVE
12
+ #define MLK_USE_NATIVE_FIPS202_X4
13
+ #define MLK_USE_NATIVE_FIPS202_X4_XOR_BYTES
14
+ #define MLK_USE_NATIVE_FIPS202_X4_EXTRACT_BYTES
15
15
  /* Guard for assembly file */
16
16
  #define MLK_FIPS202_ARMV81M_NEED_X4
17
17
 
@@ -11,7 +11,7 @@
11
11
  #define MLK_FIPS202_X86_64_NEED_X4_AVX2
12
12
 
13
13
  /* Part of backend API */
14
- #define MLK_USE_FIPS202_X4_NATIVE
14
+ #define MLK_USE_NATIVE_FIPS202_X4
15
15
 
16
16
  #if !defined(__ASSEMBLER__)
17
17
  #include "../api.h"
@@ -19,7 +19,7 @@
19
19
  MLK_MUST_CHECK_RETURN_VALUE
20
20
  static MLK_INLINE int mlk_keccak_f1600_x4_native(uint64_t *state)
21
21
  {
22
- if (!mlk_sys_check_capability(MLK_SYS_CAP_AVX2))
22
+ if (!mlk_sys_check_capability(MLK_SYS_CAP_X86_64_AVX2))
23
23
  {
24
24
  return MLK_NATIVE_FUNC_FALLBACK;
25
25
  }
@@ -64,10 +64,8 @@ __contract__(
64
64
  * MLK_CONFIG_CONTEXT_PARAMETER_TYPE.
65
65
  *
66
66
  * @retval 0 Success.
67
- * @retval MLK_ERR_FAIL MLK_CONFIG_KEYGEN_PCT enabled and PCT failed.
68
67
  * @retval MLK_ERR_OUT_OF_MEMORY MLK_CONFIG_CUSTOM_ALLOC_FREE was used and
69
68
  * MLK_CUSTOM_ALLOC returned NULL.
70
- * @retval MLK_ERR_RNG_FAIL Random number generation failed.
71
69
  */
72
70
  MLK_INTERNAL_API
73
71
  MLK_MUST_CHECK_RETURN_VALUE
@@ -81,9 +79,12 @@ __contract__(
81
79
  requires(memory_no_alias(coins, MLKEM_SYMBYTES))
82
80
  assigns(memory_slice(pk, MLKEM_INDCPA_PUBLICKEYBYTES))
83
81
  assigns(memory_slice(sk, MLKEM_INDCPA_SECRETKEYBYTES))
84
- ensures(return_value == 0 || return_value == MLK_ERR_FAIL ||
85
- return_value == MLK_ERR_OUT_OF_MEMORY ||
86
- return_value == MLK_ERR_RNG_FAIL)
82
+ ensures(return_value == 0 || return_value == MLK_ERR_OUT_OF_MEMORY)
83
+ /* Output buffers on error, per API-CONVENTIONS.md */
84
+ ensures(return_value != 0 ==>
85
+ array_unchanged_or_zeroized_u8(pk, MLKEM_INDCPA_PUBLICKEYBYTES))
86
+ ensures(return_value != 0 ==>
87
+ array_unchanged_or_zeroized_u8(sk, MLKEM_INDCPA_SECRETKEYBYTES))
87
88
  );
88
89
  #endif /* !MLK_CONFIG_NO_KEYPAIR_API */
89
90
 
@@ -106,7 +107,6 @@ __contract__(
106
107
  * MLK_CONFIG_CONTEXT_PARAMETER_TYPE.
107
108
  *
108
109
  * @retval 0 Success.
109
- * @retval MLK_ERR_FAIL Operation failed.
110
110
  * @retval MLK_ERR_OUT_OF_MEMORY MLK_CONFIG_CUSTOM_ALLOC_FREE was used and
111
111
  * MLK_CUSTOM_ALLOC returned NULL.
112
112
  */
@@ -123,8 +123,10 @@ __contract__(
123
123
  requires(memory_no_alias(pk, MLKEM_INDCPA_PUBLICKEYBYTES))
124
124
  requires(memory_no_alias(coins, MLKEM_SYMBYTES))
125
125
  assigns(memory_slice(c, MLKEM_INDCPA_BYTES))
126
- ensures(return_value == 0 || return_value == MLK_ERR_FAIL ||
127
- return_value == MLK_ERR_OUT_OF_MEMORY)
126
+ ensures(return_value == 0 || return_value == MLK_ERR_OUT_OF_MEMORY)
127
+ /* Output buffers on error, per API-CONVENTIONS.md */
128
+ ensures(return_value != 0 ==>
129
+ array_unchanged_or_zeroized_u8(c, MLKEM_INDCPA_BYTES))
128
130
  );
129
131
  #endif /* !MLK_CONFIG_NO_ENCAPS_API || !MLK_CONFIG_NO_DECAPS_API */
130
132
 
@@ -146,7 +148,6 @@ __contract__(
146
148
  * MLK_CONFIG_CONTEXT_PARAMETER_TYPE.
147
149
  *
148
150
  * @retval 0 Success.
149
- * @retval MLK_ERR_FAIL Operation failed.
150
151
  * @retval MLK_ERR_OUT_OF_MEMORY MLK_CONFIG_CUSTOM_ALLOC_FREE was used and
151
152
  * MLK_CUSTOM_ALLOC returned NULL.
152
153
  */
@@ -161,8 +162,10 @@ __contract__(
161
162
  requires(memory_no_alias(m, MLKEM_INDCPA_MSGBYTES))
162
163
  requires(memory_no_alias(sk, MLKEM_INDCPA_SECRETKEYBYTES))
163
164
  assigns(memory_slice(m, MLKEM_INDCPA_MSGBYTES))
164
- ensures(return_value == 0 || return_value == MLK_ERR_FAIL ||
165
- return_value == MLK_ERR_OUT_OF_MEMORY)
165
+ ensures(return_value == 0 || return_value == MLK_ERR_OUT_OF_MEMORY)
166
+ /* Output buffers on error, per API-CONVENTIONS.md */
167
+ ensures(return_value != 0 ==>
168
+ array_unchanged_or_zeroized_u8(m, MLKEM_INDCPA_MSGBYTES))
166
169
  );
167
170
  #endif /* !MLK_CONFIG_NO_DECAPS_API */
168
171