kyber.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* kyber.h - the Kyber key encapsulation mechanism (header)
  2. * Copyright (C) 2024 g10 Code GmbH
  3. *
  4. * This file was modified for use by Libgcrypt.
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This file is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, see <https://www.gnu.org/licenses/>.
  18. * SPDX-License-Identifier: LGPL-2.1-or-later
  19. *
  20. * You can also use this file under the same licence of original code.
  21. * SPDX-License-Identifier: CC0 OR Apache-2.0
  22. *
  23. */
  24. /*
  25. Original code from:
  26. Repository: https://github.com/pq-crystals/kyber.git
  27. Branch: standard
  28. Commit: 11d00ff1f20cfca1f72d819e5a45165c1e0a2816
  29. Licence:
  30. Public Domain (https://creativecommons.org/share-your-work/public-domain/cc0/);
  31. or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html).
  32. Authors:
  33. Joppe Bos
  34. Léo Ducas
  35. Eike Kiltz
  36. Tancrède Lepoint
  37. Vadim Lyubashevsky
  38. John Schanck
  39. Peter Schwabe
  40. Gregor Seiler
  41. Damien Stehlé
  42. Kyber Home: https://www.pq-crystals.org/kyber/
  43. */
  44. /* Standalone use is possible either with KYBER_K defined with the
  45. * value (2, 3, or 4), or not defined. For the latter, routines for
  46. * three variants are available.
  47. */
  48. #ifndef KYBER_H
  49. #define KYBER_H
  50. #ifdef _GCRYPT_IN_LIBGCRYPT
  51. /**** Start of the glue code to libgcrypt ****/
  52. #define kyber_keypair _gcry_mlkem_keypair
  53. #define kyber_encap _gcry_mlkem_encap
  54. #define kyber_decap _gcry_mlkem_decap
  55. /**** End of the glue code ****/
  56. void kyber_keypair (int algo, uint8_t *pk, uint8_t *sk);
  57. void kyber_encap (int algo, uint8_t *ct, uint8_t *ss, const uint8_t *pk);
  58. void kyber_decap (int algo, uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
  59. #elif defined(KYBER_K)
  60. int crypto_kem_keypair (uint8_t *pk, uint8_t *sk);
  61. int crypto_kem_enc (uint8_t *ct, uint8_t *ss, const uint8_t *pk);
  62. int crypto_kem_dec (uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
  63. # if KYBER_K == 2
  64. # define CRYPTO_SECRETKEYBYTES (2*384+2*384+32+2*32)
  65. # define CRYPTO_PUBLICKEYBYTES (2*384+32)
  66. # define CRYPTO_CIPHERTEXTBYTES (128+2*320)
  67. # define CRYPTO_BYTES 32
  68. # define CRYPTO_ALGNAME "Kyber512"
  69. # elif KYBER_K == 3
  70. # define CRYPTO_SECRETKEYBYTES (3*384+3*384+32+2*32)
  71. # define CRYPTO_PUBLICKEYBYTES (3*384+32)
  72. # define CRYPTO_CIPHERTEXTBYTES (128+3*320)
  73. # define CRYPTO_BYTES 32
  74. # define CRYPTO_ALGNAME "Kyber768"
  75. # elif KYBER_K == 4
  76. # define CRYPTO_SECRETKEYBYTES (4*384+2*384+32+2*32)
  77. # define CRYPTO_PUBLICKEYBYTES (4*384+32)
  78. # define CRYPTO_CIPHERTEXTBYTES (160+2*352)
  79. # define CRYPTO_BYTES 32
  80. # define CRYPTO_ALGNAME "Kyber1024"
  81. # else
  82. # define CRYPTO_SECRETKEYBYTES_512 (2*384+2*384+32+2*32)
  83. # define CRYPTO_PUBLICKEYBYTES_512 (2*384+32)
  84. # define CRYPTO_CIPHERTEXTBYTES_512 (128+2*320)
  85. # define CRYPTO_BYTES_512 32
  86. # define CRYPTO_SECRETKEYBYTES_768 (3*384+3*384+32+2*32)
  87. # define CRYPTO_PUBLICKEYBYTES_768 (3*384+32)
  88. # define CRYPTO_CIPHERTEXTBYTES_768 (128+3*320)
  89. # define CRYPTO_BYTES_768 32
  90. # define CRYPTO_SECRETKEYBYTES_1024 (4*384+2*384+32+2*32)
  91. # define CRYPTO_PUBLICKEYBYTES_1024 (4*384+32)
  92. # define CRYPTO_CIPHERTEXTBYTES_1024 (160+2*352)
  93. # define CRYPTO_BYTES_1024 32
  94. # define CRYPTO_ALGNAME "Kyber"
  95. # define crypto_kem_keypair_2 crypto_kem_keypair_512
  96. # define crypto_kem_keypair_3 crypto_kem_keypair_768
  97. # define crypto_kem_keypair_4 crypto_kem_keypair_1024
  98. int crypto_kem_keypair_2 (uint8_t *pk, uint8_t *sk);
  99. int crypto_kem_keypair_3 (uint8_t *pk, uint8_t *sk);
  100. int crypto_kem_keypair_4 (uint8_t *pk, uint8_t *sk);
  101. # define crypto_kem_enc_2 crypto_kem_enc_512
  102. # define crypto_kem_enc_3 crypto_kem_enc_768
  103. # define crypto_kem_enc_4 crypto_kem_enc_1024
  104. int crypto_kem_enc_2 (uint8_t *ct, uint8_t *ss, const uint8_t *pk);
  105. int crypto_kem_enc_3 (uint8_t *ct, uint8_t *ss, const uint8_t *pk);
  106. int crypto_kem_enc_4 (uint8_t *ct, uint8_t *ss, const uint8_t *pk);
  107. # define crypto_kem_dec_2 crypto_kem_dec_512
  108. # define crypto_kem_dec_3 crypto_kem_dec_768
  109. # define crypto_kem_dec_4 crypto_kem_dec_1024
  110. int crypto_kem_dec_2 (uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
  111. int crypto_kem_dec_3 (uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
  112. int crypto_kem_dec_4 (uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
  113. # endif
  114. #endif
  115. #endif /* KYBER_H */