kyber512r3_symmetric-shake.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "kyber512r3_params.h"
  2. #include "kyber512r3_fips202.h"
  3. #include "kyber512r3_symmetric.h"
  4. #include <stdlib.h>
  5. /*************************************************
  6. * Name: kyber_shake128_absorb
  7. *
  8. * Description: Absorb step of the SHAKE128 specialized for the Kyber context.
  9. * Arguments: - keccak_state *s: pointer to (uninitialized) output Keccak state
  10. * - const uint8_t *input: pointer to S2N_KYBER_512_R3_SYMBYTES input to be absorbed into s
  11. * - uint8_t i additional byte of input
  12. * - uint8_t j additional byte of input
  13. **************************************************/
  14. void kyber_shake128_absorb(keccak_state *s, const uint8_t *input, uint8_t x, uint8_t y) {
  15. size_t i;
  16. uint8_t extseed[S2N_KYBER_512_R3_SYMBYTES + 2];
  17. for (i = 0; i < S2N_KYBER_512_R3_SYMBYTES; i++) {
  18. extseed[i] = input[i];
  19. }
  20. extseed[i++] = x;
  21. extseed[i] = y;
  22. shake128_absorb(s, extseed, S2N_KYBER_512_R3_SYMBYTES + 2);
  23. }
  24. /*************************************************
  25. * Name: shake256_prf
  26. *
  27. * Description: Usage of SHAKE256 as a PRF, concatenates secret and public input
  28. * and then generates outlen bytes of SHAKE256 output
  29. *
  30. * Arguments: - uint8_t *output: pointer to output
  31. * - size_t outlen: number of requested output bytes
  32. * - const uint8_t * key: pointer to the key (of length S2N_KYBER_512_R3_SYMBYTES)
  33. * - uint8_t nonce: single-byte nonce (public PRF input)
  34. **************************************************/
  35. void shake256_prf(uint8_t *output, size_t outlen, const uint8_t *key, uint8_t nonce) {
  36. uint8_t extkey[S2N_KYBER_512_R3_SYMBYTES + 1];
  37. size_t i;
  38. for (i = 0; i < S2N_KYBER_512_R3_SYMBYTES; i++) {
  39. extkey[i] = key[i];
  40. }
  41. extkey[i] = nonce;
  42. shake256(output, outlen, extkey, S2N_KYBER_512_R3_SYMBYTES + 1);
  43. }