mlkem.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Internal functions for the ML-KEM cryptosystem, exposed in a header
  3. * that is expected to be included only by mlkem.c and test programs.
  4. */
  5. #ifndef PUTTY_CRYPTO_MLKEM_H
  6. #define PUTTY_CRYPTO_MLKEM_H
  7. typedef struct mlkem_params mlkem_params;
  8. extern const mlkem_params mlkem_params_512;
  9. extern const mlkem_params mlkem_params_768;
  10. extern const mlkem_params mlkem_params_1024;
  11. /*
  12. * ML-KEM key generation.
  13. *
  14. * The official spec gives two APIs for this function: an outer one
  15. * that invents random data from an implicit PRNG parameter, and an
  16. * inner one that takes the randomness as explicit input for running
  17. * test vectors.
  18. *
  19. * To make side-channel testing easier, I introduce a third API inside
  20. * the latter. The spec's "inner" function takes a parameter 'd'
  21. * containing 32 bytes of randomness, which it immediately expands
  22. * into a 64-byte hash and then uses the two halves of that hash for
  23. * different purposes. My even-more-inner function expects the caller
  24. * to have done that hashing already, and to present the two 32-byte
  25. * half-hashes rho and sigma separately.
  26. *
  27. * Rationale: it would be difficult to make the keygen running time
  28. * independent of rho, becase the required technique for constructing
  29. * a matrix from rho uses rejection sampling, so timing will depend on
  30. * how many samples were rejected. Happily, it's also not _necessary_
  31. * to make the timing independent of rho, because rho is part of the
  32. * _public_ key, so it's sent in clear over the wire anyway. So for
  33. * testsc purposes, it's convenient to regard rho as fixed and vary
  34. * sigma, so that the timing variations due to rho don't show up as
  35. * failures in the test.
  36. *
  37. * Inputs: 'd', 'z', 'rho' and 'sigma' are all 32-byte random strings.
  38. *
  39. * Return: the encryption and decryption keys are written to the two
  40. * provided BinarySinks.
  41. */
  42. void mlkem_keygen(
  43. BinarySink *ek, BinarySink *dk, const mlkem_params *params);
  44. void mlkem_keygen_internal(
  45. BinarySink *ek, BinarySink *dk, const mlkem_params *params,
  46. const void *d, const void *z);
  47. void mlkem_keygen_rho_sigma(
  48. BinarySink *ek, BinarySink *dk, const mlkem_params *params,
  49. const void *rho, const void *sigma, const void *z);
  50. /*
  51. * ML-KEM key encapsulation, with only two forms, the outer (random)
  52. * and inner (for test vectors) versions from the spec.
  53. *
  54. * Inputs: the encryption key from keygen. 'm' should be a 32-byte
  55. * random string if provided.
  56. *
  57. * Returns: if successful, returns true, and writes to the two
  58. * BinarySinks a ciphertext to send to the other side, and our copy of
  59. * the output shared secret k. If failure, returns false, and the
  60. * strbuf pointers aren't filled in at all.
  61. */
  62. bool mlkem_encaps(BinarySink *ciphertext, BinarySink *kout,
  63. const mlkem_params *params, ptrlen ek);
  64. bool mlkem_encaps_internal(BinarySink *ciphertext, BinarySink *kout,
  65. const mlkem_params *params, ptrlen ek,
  66. const void *m);
  67. /*
  68. * ML-KEM key decapsulation. This doesn't use any randomness, so even
  69. * the official spec only presents one version of it. (Actually it
  70. * defines two functions, but the outer one adds nothing over the
  71. * inner one.)
  72. *
  73. * Inputs: the decryption key from keygen, and the ciphertext output
  74. * from encapsulation.
  75. *
  76. * Returns: false on validation failure, and true otherwise
  77. * (regardless of whether the ciphertext was implicitly rejected). The
  78. * shared secret k is written to the provided BinarySink.
  79. */
  80. bool mlkem_decaps(BinarySink *k, const mlkem_params *params,
  81. ptrlen dk, ptrlen c);
  82. #endif /* PUTTY_CRYPTO_MLKEM_H */