Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

127 righe
4.4 KiB

  1. /**
  2. * @file parsing.c
  3. * @brief Functions to parse secret key, public key and ciphertext of the HQC scheme
  4. */
  5. #include "nistseedexpander.h"
  6. #include "parameters.h"
  7. #include "parsing.h"
  8. #include "vector.h"
  9. #include <stdint.h>
  10. #include <string.h>
  11. /**
  12. * @brief Parse a secret key into a string
  13. *
  14. * The secret key is composed of the seed used to generate vectors <b>x</b> and <b>y</b>.
  15. * As technicality, the public key is appended to the secret key in order to respect NIST API.
  16. *
  17. * @param[out] sk String containing the secret key
  18. * @param[in] sk_seed Seed used to generate the secret key
  19. * @param[in] pk String containing the public key
  20. */
  21. void PQCLEAN_HQC2561CCA2_LEAKTIME_hqc_secret_key_to_string(uint8_t *sk, const uint8_t *sk_seed, const uint8_t *pk) {
  22. memcpy(sk, sk_seed, SEED_BYTES);
  23. memcpy(sk + SEED_BYTES, pk, PUBLIC_KEY_BYTES);
  24. }
  25. /**
  26. * @brief Parse a secret key from a string
  27. *
  28. * The secret key is composed of the seed used to generate vectors <b>x</b> and <b>y</b>.
  29. * As technicality, the public key is appended to the secret key in order to respect NIST API.
  30. *
  31. * @param[out] x uint8_t representation of vector x
  32. * @param[out] y uint8_t representation of vector y
  33. * @param[out] pk String containing the public key
  34. * @param[in] sk String containing the secret key
  35. */
  36. void PQCLEAN_HQC2561CCA2_LEAKTIME_hqc_secret_key_from_string(uint8_t *x, uint32_t *y, uint8_t *pk, const uint8_t *sk) {
  37. AES_XOF_struct sk_seedexpander;
  38. uint8_t sk_seed[SEED_BYTES] = {0};
  39. memcpy(sk_seed, sk, SEED_BYTES);
  40. seedexpander_init(&sk_seedexpander, sk_seed, sk_seed + 32, SEEDEXPANDER_MAX_LENGTH);
  41. PQCLEAN_HQC2561CCA2_LEAKTIME_vect_set_random_fixed_weight(&sk_seedexpander, x, PARAM_OMEGA);
  42. PQCLEAN_HQC2561CCA2_LEAKTIME_vect_set_random_fixed_weight_by_coordinates(&sk_seedexpander, y, PARAM_OMEGA);
  43. memcpy(pk, sk + SEED_BYTES, PUBLIC_KEY_BYTES);
  44. }
  45. /**
  46. * @brief Parse a public key into a string
  47. *
  48. * The public key is composed of the syndrome <b>s</b> as well as the seed used to generate the vector <b>h</b>
  49. *
  50. * @param[out] pk String containing the public key
  51. * @param[in] pk_seed Seed used to generate the public key
  52. * @param[in] s uint8_t representation of vector s
  53. */
  54. void PQCLEAN_HQC2561CCA2_LEAKTIME_hqc_public_key_to_string(uint8_t *pk, const uint8_t *pk_seed, const uint8_t *s) {
  55. memcpy(pk, pk_seed, SEED_BYTES);
  56. memcpy(pk + SEED_BYTES, s, VEC_N_SIZE_BYTES);
  57. }
  58. /**
  59. * @brief Parse a public key from a string
  60. *
  61. * The public key is composed of the syndrome <b>s</b> as well as the seed used to generate the vector <b>h</b>
  62. *
  63. * @param[out] h uint8_t representation of vector h
  64. * @param[out] s uint8_t representation of vector s
  65. * @param[in] pk String containing the public key
  66. */
  67. void PQCLEAN_HQC2561CCA2_LEAKTIME_hqc_public_key_from_string(uint8_t *h, uint8_t *s, const uint8_t *pk) {
  68. AES_XOF_struct pk_seedexpander;
  69. uint8_t pk_seed[SEED_BYTES] = {0};
  70. memcpy(pk_seed, pk, SEED_BYTES);
  71. seedexpander_init(&pk_seedexpander, pk_seed, pk_seed + 32, SEEDEXPANDER_MAX_LENGTH);
  72. PQCLEAN_HQC2561CCA2_LEAKTIME_vect_set_random(&pk_seedexpander, h);
  73. memcpy(s, pk + SEED_BYTES, VEC_N_SIZE_BYTES);
  74. }
  75. /**
  76. * @brief Parse a ciphertext into a string
  77. *
  78. * The ciphertext is composed of vectors <b>u</b>, <b>v</b> and hash <b>d</b>.
  79. *
  80. * @param[out] ct String containing the ciphertext
  81. * @param[in] u uint8_t representation of vector u
  82. * @param[in] v uint8_t representation of vector v
  83. * @param[in] d String containing the hash d
  84. */
  85. void PQCLEAN_HQC2561CCA2_LEAKTIME_hqc_ciphertext_to_string(uint8_t *ct, const uint8_t *u, const uint8_t *v, const uint8_t *d) {
  86. memcpy(ct, u, VEC_N_SIZE_BYTES);
  87. memcpy(ct + VEC_N_SIZE_BYTES, v, VEC_N1N2_SIZE_BYTES);
  88. memcpy(ct + VEC_N_SIZE_BYTES + VEC_N1N2_SIZE_BYTES, d, SHA512_BYTES);
  89. }
  90. /**
  91. * @brief Parse a ciphertext from a string
  92. *
  93. * The ciphertext is composed of vectors <b>u</b>, <b>v</b> and hash <b>d</b>.
  94. *
  95. * @param[out] u uint8_t representation of vector u
  96. * @param[out] v uint8_t representation of vector v
  97. * @param[out] d String containing the hash d
  98. * @param[in] ct String containing the ciphertext
  99. */
  100. void PQCLEAN_HQC2561CCA2_LEAKTIME_hqc_ciphertext_from_string(uint8_t *u, uint8_t *v, uint8_t *d, const uint8_t *ct) {
  101. memcpy(u, ct, VEC_N_SIZE_BYTES);
  102. memcpy(v, ct + VEC_N_SIZE_BYTES, VEC_N1N2_SIZE_BYTES);
  103. memcpy(d, ct + VEC_N_SIZE_BYTES + VEC_N1N2_SIZE_BYTES, SHA512_BYTES);
  104. }