|
| 1 | +// Generates test vectors with intentionally corrupted ML-DSA private keys |
| 2 | +// |
| 3 | +// USAGE: |
| 4 | +// cd crypto/fipsmodule/ml_dsa |
| 5 | +// make generate |
| 6 | +// |
| 7 | +// This regenerates crypto/evp_extra/mldsa_corrupted_key_tests.txt |
| 8 | + |
| 9 | +#include <openssl/evp.h> |
| 10 | +#include <openssl/obj.h> |
| 11 | + |
| 12 | +#include <cassert> |
| 13 | +#include <cstddef> |
| 14 | +#include <cstdint> |
| 15 | +#include <cstdio> |
| 16 | +#include <cstring> |
| 17 | +#include <functional> |
| 18 | +#include <iostream> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +// Need ML-DSA internal headers to manipulate the expanded private key |
| 22 | +extern "C" { |
| 23 | +#include "./ml_dsa_ref/packing.h" |
| 24 | +#include "./ml_dsa_ref/params.h" |
| 25 | +#include "./ml_dsa_ref/polyvec.h" |
| 26 | +} |
| 27 | + |
| 28 | +static void PrintHex(const std::vector<uint8_t> &data) { |
| 29 | + for (uint8_t byte : data) { |
| 30 | + printf("%02x", byte); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +struct MLDSAParamSet { |
| 35 | + const char name[20]; |
| 36 | + const int nid; |
| 37 | +}; |
| 38 | + |
| 39 | +static const struct MLDSAParamSet kMLDSAs[] = {{"MLDSA44", NID_MLDSA44}, |
| 40 | + {"MLDSA65", NID_MLDSA65}, |
| 41 | + {"MLDSA87", NID_MLDSA87}}; |
| 42 | + |
| 43 | + |
| 44 | +// Corruption function type: takes unpacked key components and corrupts them |
| 45 | +using CorruptionFn = |
| 46 | + std::function<void(polyvecl *s1, polyveck *s2, polyveck *t0)>; |
| 47 | + |
| 48 | + |
| 49 | +// Generates a corrupted private key with the provided corruption |
| 50 | +static bool GenerateCorruptedKey(const MLDSAParamSet &ps, ml_dsa_params *params, |
| 51 | + const std::vector<uint8_t> &honest_key_bytes, |
| 52 | + const CorruptionFn &corruption) { |
| 53 | + polyvecl s1; |
| 54 | + polyveck s2, t0; |
| 55 | + uint8_t rho[ML_DSA_SEEDBYTES]; |
| 56 | + uint8_t tr[ML_DSA_TRBYTES]; |
| 57 | + uint8_t key[ML_DSA_SEEDBYTES]; |
| 58 | + |
| 59 | + // Unpack the honest key |
| 60 | + ml_dsa_unpack_sk(params, rho, tr, key, &t0, &s1, &s2, |
| 61 | + honest_key_bytes.data()); |
| 62 | + |
| 63 | + // Apply the corruption |
| 64 | + corruption(&s1, &s2, &t0); |
| 65 | + |
| 66 | + // Repack the corrupted key |
| 67 | + std::vector<uint8_t> corrupted_key(params->secret_key_bytes); |
| 68 | + ml_dsa_pack_sk(params, corrupted_key.data(), rho, tr, key, &t0, &s1, &s2); |
| 69 | + |
| 70 | + // Verify the corrupted key differs from the honest key |
| 71 | + assert(std::memcmp(corrupted_key.data(), honest_key_bytes.data(), |
| 72 | + params->secret_key_bytes) != 0); |
| 73 | + |
| 74 | + // Output the test vector |
| 75 | + printf("# corrupted private key with invalid s1 or s2, inconsistent\n"); |
| 76 | + printf("CorruptedKey = "); |
| 77 | + PrintHex(corrupted_key); |
| 78 | + printf("\n\n"); |
| 79 | + |
| 80 | + // Create a consistent version by recomputing the public key and tr |
| 81 | + std::vector<uint8_t> consistent_key = corrupted_key; |
| 82 | + |
| 83 | + // Recompute the public key. We cannot use ml_dsa_pack_pk_from_sk since we |
| 84 | + // fixed it to fail for invalid secret keys. Instead we adapt from |
| 85 | + // https://github.com/aws/aws-lc/blob/0336dd78a0f2623c1f9b209a98cd497026d9c779/crypto/fipsmodule/ml_dsa/ml_dsa_ref/packing.c#L7-L61 |
| 86 | + ml_dsa_unpack_sk(params, rho, tr, key, &t0, &s1, &s2, consistent_key.data()); |
| 87 | + polyvecl mat[ML_DSA_K_MAX]; |
| 88 | + ml_dsa_polyvec_matrix_expand(params, mat, rho); |
| 89 | + ml_dsa_polyvecl_ntt(params, &s1); |
| 90 | + polyveck t1; |
| 91 | + ml_dsa_polyvec_matrix_pointwise_montgomery(params, &t1, mat, &s1); |
| 92 | + ml_dsa_polyveck_reduce(params, &t1); |
| 93 | + ml_dsa_polyveck_invntt_tomont(params, &t1); |
| 94 | + ml_dsa_polyveck_add(params, &t1, &t1, &s2); |
| 95 | + ml_dsa_polyveck_caddq(params, &t1); |
| 96 | + ml_dsa_polyveck_power2round(params, &t1, &t0, &t1); |
| 97 | + std::vector<uint8_t> consistent_pk(params->public_key_bytes); |
| 98 | + ml_dsa_pack_pk(params, consistent_pk.data(), rho, &t1); |
| 99 | + |
| 100 | + // Recompute tr = SHAKE256(pk, 64) |
| 101 | + std::vector<uint8_t> new_tr(ML_DSA_TRBYTES); |
| 102 | + bssl::ScopedEVP_MD_CTX md_ctx; |
| 103 | + if (!EVP_DigestInit_ex(md_ctx.get(), EVP_shake256(), nullptr) || |
| 104 | + !EVP_DigestUpdate(md_ctx.get(), consistent_pk.data(), |
| 105 | + params->public_key_bytes) || |
| 106 | + !EVP_DigestFinalXOF(md_ctx.get(), new_tr.data(), new_tr.size())) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + // Repack the consistent corrupted key |
| 111 | + ml_dsa_pack_sk(params, consistent_key.data(), rho, new_tr.data(), |
| 112 | + consistent_pk.data(), &t0, &s1, &s2); |
| 113 | + |
| 114 | + // Verify the consistent key differs from the inconsistent one |
| 115 | + assert(std::memcmp(consistent_key.data(), corrupted_key.data(), |
| 116 | + params->secret_key_bytes) != 0); |
| 117 | + |
| 118 | + // Output the test vector |
| 119 | + printf("# corrupted private key with invalid s1 or s2, consistent\n"); |
| 120 | + printf("CorruptedKey = "); |
| 121 | + PrintHex(consistent_key); |
| 122 | + printf("\n\n"); |
| 123 | + |
| 124 | + return true; |
| 125 | +} |
| 126 | + |
| 127 | +static bool InitializeParams(int nid, ml_dsa_params *params) { |
| 128 | + if (nid == NID_MLDSA44) { |
| 129 | + ml_dsa_44_params_init(params); |
| 130 | + } else if (nid == NID_MLDSA65) { |
| 131 | + ml_dsa_65_params_init(params); |
| 132 | + } else if (nid == NID_MLDSA87) { |
| 133 | + ml_dsa_87_params_init(params); |
| 134 | + } else { |
| 135 | + std::cerr << "Unexpected NID: " << nid << "\n"; |
| 136 | + return false; |
| 137 | + } |
| 138 | + return true; |
| 139 | +} |
| 140 | + |
| 141 | +static bool GenerateHonestKey(const MLDSAParamSet &ps, |
| 142 | + const ml_dsa_params ¶ms, |
| 143 | + std::vector<uint8_t> *honest_key_bytes) { |
| 144 | + // Generate an honest private key from a fixed seed |
| 145 | + const std::vector<uint8_t> seed(32, 0x42); |
| 146 | + bssl::UniquePtr<EVP_PKEY> honest_pkey( |
| 147 | + EVP_PKEY_pqdsa_new_raw_private_key(ps.nid, seed.data(), seed.size())); |
| 148 | + if (!honest_pkey) { |
| 149 | + std::cerr << "Failed to generate honest key for " << ps.name << "\n"; |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + // Export the honest private key to bytes |
| 154 | + size_t key_len = params.secret_key_bytes; |
| 155 | + honest_key_bytes->resize(key_len); |
| 156 | + if (!EVP_PKEY_get_raw_private_key(honest_pkey.get(), honest_key_bytes->data(), |
| 157 | + &key_len)) { |
| 158 | + std::cerr << "Failed to export honest key for " << ps.name << "\n"; |
| 159 | + return false; |
| 160 | + } |
| 161 | + return true; |
| 162 | +} |
| 163 | + |
| 164 | +static std::vector<CorruptionFn> CreateCorruptionFunctions( |
| 165 | + const ml_dsa_params ¶ms, int vec_index, int coeff_index) { |
| 166 | + return { |
| 167 | + // Corrupt s1 with eta + 1 |
| 168 | + [¶ms, vec_index, coeff_index](polyvecl *s1, polyveck *, polyveck *) { |
| 169 | + s1->vec[vec_index].coeffs[coeff_index] = params.eta + 1; |
| 170 | + }, |
| 171 | + // Corrupt s1 with -(eta + 1) |
| 172 | + [¶ms, vec_index, coeff_index](polyvecl *s1, polyveck *, polyveck *) { |
| 173 | + s1->vec[vec_index].coeffs[coeff_index] = -(params.eta + 1); |
| 174 | + }, |
| 175 | + // Corrupt s2 with eta + 1 |
| 176 | + [¶ms, vec_index, coeff_index](polyvecl *, polyveck *s2, polyveck *) { |
| 177 | + s2->vec[vec_index].coeffs[coeff_index] = params.eta + 1; |
| 178 | + }, |
| 179 | + // Corrupt s2 with -(eta + 1) |
| 180 | + [¶ms, vec_index, coeff_index](polyvecl *, polyveck *s2, polyveck *) { |
| 181 | + s2->vec[vec_index].coeffs[coeff_index] = -(params.eta + 1); |
| 182 | + }, |
| 183 | + }; |
| 184 | +} |
| 185 | + |
| 186 | +int main() { |
| 187 | + printf( |
| 188 | + "# Invalid ML-DSA extended private keys\n" |
| 189 | + "# This file was generated by " |
| 190 | + "crypto/fipsmodule/ml_dsa/make_corrupted_key_tests.cc\n\n"); |
| 191 | + |
| 192 | + for (const auto &ps : kMLDSAs) { |
| 193 | + printf("[ParamSet = %s]\n", ps.name); |
| 194 | + |
| 195 | + ml_dsa_params params; |
| 196 | + if (!InitializeParams(ps.nid, ¶ms)) { |
| 197 | + return 1; |
| 198 | + } |
| 199 | + |
| 200 | + std::vector<uint8_t> honest_key_bytes; |
| 201 | + if (!GenerateHonestKey(ps, params, &honest_key_bytes)) { |
| 202 | + return 1; |
| 203 | + } |
| 204 | + |
| 205 | + // Test coefficient indices: first, last, and some random ones |
| 206 | + const int coeff_indices[] = {0, 255, 127, 95, 42, 224}; |
| 207 | + |
| 208 | + for (int vec_index = 0; vec_index < params.l; vec_index++) { |
| 209 | + for (int coeff_index : coeff_indices) { |
| 210 | + const std::vector<CorruptionFn> corruptions = |
| 211 | + CreateCorruptionFunctions(params, vec_index, coeff_index); |
| 212 | + |
| 213 | + for (const auto &corruption : corruptions) { |
| 214 | + if (!GenerateCorruptedKey(ps, ¶ms, honest_key_bytes, |
| 215 | + corruption)) { |
| 216 | + return 1; |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + return 0; |
| 223 | +} |
0 commit comments