wc_kyber.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* wc_kyber.h
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/wc_kyber.h
  23. */
  24. #ifndef WOLF_CRYPT_WC_KYBER_H
  25. #define WOLF_CRYPT_WC_KYBER_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #include <wolfssl/wolfcrypt/random.h>
  28. #include <wolfssl/wolfcrypt/sha3.h>
  29. #include <wolfssl/wolfcrypt/kyber.h>
  30. #ifdef WOLFSSL_HAVE_KYBER
  31. #if defined(_MSC_VER)
  32. #define KYBER_NOINLINE __declspec(noinline)
  33. #elif defined(__GNUC__)
  34. #define KYBER_NOINLINE __attribute__((noinline))
  35. #else
  36. #define KYBER_NOINLINE
  37. #endif
  38. /* Define algorithm type when not excluded. */
  39. #ifndef WOLFSSL_NO_KYBER512
  40. #define WOLFSSL_KYBER512
  41. #endif
  42. #ifndef WOLFSSL_NO_KYBER768
  43. #define WOLFSSL_KYBER768
  44. #endif
  45. #ifndef WOLFSSL_NO_KYBER1024
  46. #define WOLFSSL_KYBER1024
  47. #endif
  48. enum {
  49. /* Flags of Kyber keys. */
  50. KYBER_FLAG_PRIV_SET = 0x0001,
  51. KYBER_FLAG_PUB_SET = 0x0002,
  52. KYBER_FLAG_BOTH_SET = 0x0003,
  53. KYBER_FLAG_H_SET = 0x0004,
  54. /* 2 bits of random used to create noise value. */
  55. KYBER_CBD_ETA2 = 2,
  56. /* 3 bits of random used to create noise value. */
  57. KYBER_CBD_ETA3 = 3,
  58. /* Number of bits to compress to. */
  59. KYBER_COMP_4BITS = 4,
  60. KYBER_COMP_5BITS = 5,
  61. KYBER_COMP_10BITS = 10,
  62. KYBER_COMP_11BITS = 11,
  63. };
  64. /* SHAKE128 rate. */
  65. #define XOF_BLOCK_SIZE 168
  66. /* Modulus of co-efficients of polynomial. */
  67. #define KYBER_Q 3329
  68. /* Kyber-512 parameters */
  69. #ifdef WOLFSSL_KYBER512
  70. /* Number of bits of random to create noise from. */
  71. #define KYBER512_ETA1 KYBER_CBD_ETA3
  72. #endif /* WOLFSSL_KYBER512 */
  73. /* Kyber-768 parameters */
  74. #ifdef WOLFSSL_KYBER768
  75. /* Number of bits of random to create noise from. */
  76. #define KYBER768_ETA1 KYBER_CBD_ETA2
  77. #endif /* WOLFSSL_KYBER768 */
  78. /* Kyber-1024 parameters */
  79. #ifdef WOLFSSL_KYBER1024
  80. /* Number of bits of random to create noise from. */
  81. #define KYBER1024_ETA1 KYBER_CBD_ETA2
  82. #endif /* WOLFSSL_KYBER1024 */
  83. /* The data type of the pseudo-random function. */
  84. #define KYBER_PRF_T wc_Shake
  85. /* Kyber key. */
  86. struct KyberKey {
  87. /* Type of key: KYBER512, KYBER768, KYBER1024 */
  88. int type;
  89. /* Dynamic memory allocation hint. */
  90. void* heap;
  91. #if defined(WOLF_CRYPTO_CB)
  92. /* Device Id. */
  93. int devId;
  94. #endif
  95. /* Flags indicating what is stored in the key. */
  96. int flags;
  97. /* A pseudo-random function object. */
  98. KYBER_PRF_T prf;
  99. /* Private key as a vector. */
  100. sword16 priv[KYBER_MAX_K * KYBER_N];
  101. /* Public key as a vector. */
  102. sword16 pub[KYBER_MAX_K * KYBER_N];
  103. /* Public seed. */
  104. byte pubSeed[KYBER_SYM_SZ];
  105. /* Public hash - hash of encoded public key. */
  106. byte h[KYBER_SYM_SZ];
  107. /* Randomizer for decapsulation. */
  108. byte z[KYBER_SYM_SZ];
  109. };
  110. #ifdef __cplusplus
  111. extern "C" {
  112. #endif
  113. WOLFSSL_LOCAL
  114. void kyber_init(void);
  115. WOLFSSL_LOCAL
  116. void kyber_keygen(sword16* priv, sword16* pub, sword16* e, const sword16* a,
  117. int kp);
  118. WOLFSSL_LOCAL
  119. void kyber_encapsulate(const sword16* pub, sword16* bp, sword16* v,
  120. const sword16* at, sword16* sp, const sword16* ep, const sword16* epp,
  121. const sword16* m, int kp);
  122. WOLFSSL_LOCAL
  123. void kyber_decapsulate(const sword16* priv, sword16* mp, sword16* bp,
  124. const sword16* v, int kp);
  125. WOLFSSL_LOCAL
  126. int kyber_gen_matrix(KYBER_PRF_T* prf, sword16* a, int kp, byte* seed,
  127. int transposed);
  128. WOLFSSL_LOCAL
  129. int kyber_get_noise(KYBER_PRF_T* prf, int kp, sword16* vec1, sword16* vec2,
  130. sword16* poly, byte* seed);
  131. #ifdef USE_INTEL_SPEEDUP
  132. WOLFSSL_LOCAL
  133. int kyber_kdf(byte* seed, int seedLen, byte* out, int outLen);
  134. #endif
  135. WOLFSSL_LOCAL
  136. void kyber_prf_init(KYBER_PRF_T* prf);
  137. WOLFSSL_LOCAL
  138. int kyber_prf_new(KYBER_PRF_T* prf, void* heap, int devId);
  139. WOLFSSL_LOCAL
  140. void kyber_prf_free(KYBER_PRF_T* prf);
  141. WOLFSSL_LOCAL
  142. int kyber_cmp(const byte* a, const byte* b, int sz);
  143. WOLFSSL_LOCAL
  144. void kyber_vec_compress_10(byte* r, sword16* v, unsigned int kp);
  145. WOLFSSL_LOCAL
  146. void kyber_vec_compress_11(byte* r, sword16* v);
  147. WOLFSSL_LOCAL
  148. void kyber_vec_decompress_10(sword16* v, const unsigned char* b,
  149. unsigned int kp);
  150. WOLFSSL_LOCAL
  151. void kyber_vec_decompress_11(sword16* v, const unsigned char* b);
  152. WOLFSSL_LOCAL
  153. void kyber_compress_4(byte* b, sword16* p);
  154. WOLFSSL_LOCAL
  155. void kyber_compress_5(byte* b, sword16* p);
  156. WOLFSSL_LOCAL
  157. void kyber_decompress_4(sword16* p, const unsigned char* b);
  158. WOLFSSL_LOCAL
  159. void kyber_decompress_5(sword16* p, const unsigned char* b);
  160. WOLFSSL_LOCAL
  161. void kyber_from_msg(sword16* p, const byte* msg);
  162. WOLFSSL_LOCAL
  163. void kyber_to_msg(byte* msg, sword16* p);
  164. WOLFSSL_LOCAL
  165. void kyber_from_bytes(sword16* p, const byte* b, int k);
  166. WOLFSSL_LOCAL
  167. void kyber_to_bytes(byte* b, sword16* p, int k);
  168. #ifdef USE_INTEL_SPEEDUP
  169. WOLFSSL_LOCAL
  170. void kyber_keygen_avx2(sword16* priv, sword16* pub, sword16* e,
  171. const sword16* a, int kp);
  172. WOLFSSL_LOCAL
  173. void kyber_encapsulate_avx2(const sword16* pub, sword16* bp, sword16* v,
  174. const sword16* at, sword16* sp, const sword16* ep, const sword16* epp,
  175. const sword16* m, int kp);
  176. WOLFSSL_LOCAL
  177. void kyber_decapsulate_avx2(const sword16* priv, sword16* mp, sword16* bp,
  178. const sword16* v, int kp);
  179. WOLFSSL_LOCAL
  180. unsigned int kyber_rej_uniform_n_avx2(sword16* p, unsigned int len,
  181. const byte* r, unsigned int rLen);
  182. WOLFSSL_LOCAL
  183. unsigned int kyber_rej_uniform_avx2(sword16* p, unsigned int len, const byte* r,
  184. unsigned int rLen);
  185. WOLFSSL_LOCAL
  186. void kyber_redistribute_21_rand_avx2(const word64* s, byte* r0, byte* r1,
  187. byte* r2, byte* r3);
  188. void kyber_redistribute_17_rand_avx2(const word64* s, byte* r0, byte* r1,
  189. byte* r2, byte* r3);
  190. void kyber_redistribute_16_rand_avx2(const word64* s, byte* r0, byte* r1,
  191. byte* r2, byte* r3);
  192. void kyber_redistribute_8_rand_avx2(const word64* s, byte* r0, byte* r1,
  193. byte* r2, byte* r3);
  194. WOLFSSL_LOCAL
  195. void kyber_sha3_blocksx4_avx2(word64* s);
  196. WOLFSSL_LOCAL
  197. void kyber_sha3_128_blocksx4_seed_avx2(word64* s, byte* seed);
  198. WOLFSSL_LOCAL
  199. void kyber_sha3_256_blocksx4_seed_avx2(word64* s, byte* seed);
  200. WOLFSSL_LOCAL
  201. void kyber_cbd_eta2_avx2(sword16* p, const byte* r);
  202. WOLFSSL_LOCAL
  203. void kyber_cbd_eta3_avx2(sword16* p, const byte* r);
  204. WOLFSSL_LOCAL
  205. void kyber_from_msg_avx2(sword16* p, const byte* msg);
  206. WOLFSSL_LOCAL
  207. void kyber_to_msg_avx2(byte* msg, sword16* p);
  208. WOLFSSL_LOCAL
  209. void kyber_from_bytes_avx2(sword16* p, const byte* b);
  210. WOLFSSL_LOCAL
  211. void kyber_to_bytes_avx2(byte* b, sword16* p);
  212. WOLFSSL_LOCAL
  213. void kyber_compress_10_avx2(byte* r, const sword16* p, int n);
  214. WOLFSSL_LOCAL
  215. void kyber_decompress_10_avx2(sword16* p, const byte* r, int n);
  216. WOLFSSL_LOCAL
  217. void kyber_compress_11_avx2(byte* r, const sword16* p, int n);
  218. WOLFSSL_LOCAL
  219. void kyber_decompress_11_avx2(sword16* p, const byte* r, int n);
  220. WOLFSSL_LOCAL
  221. void kyber_compress_4_avx2(byte* r, const sword16* p);
  222. WOLFSSL_LOCAL
  223. void kyber_decompress_4_avx2(sword16* p, const byte* r);
  224. WOLFSSL_LOCAL
  225. void kyber_compress_5_avx2(byte* r, const sword16* p);
  226. WOLFSSL_LOCAL
  227. void kyber_decompress_5_avx2(sword16* p, const byte* r);
  228. WOLFSSL_LOCAL
  229. int kyber_cmp_avx2(const byte* a, const byte* b, int sz);
  230. #endif
  231. #ifdef __cplusplus
  232. } /* extern "C" */
  233. #endif
  234. #endif /* WOLFSSL_HAVE_KYBER */
  235. #endif /* WOLF_CRYPT_WC_KYBER_H */