dilithium.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* dilithium.h
  2. *
  3. * Copyright (C) 2006-2023 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/dilithium.h
  23. */
  24. /* Interfaces for Dilithium NIST Level 1 (Dilithium512) and Dilithium NIST Level 5
  25. * (Dilithium1024). */
  26. #ifndef WOLF_CRYPT_DILITHIUM_H
  27. #define WOLF_CRYPT_DILITHIUM_H
  28. #include <wolfssl/wolfcrypt/types.h>
  29. #if defined(HAVE_PQC) && defined(HAVE_DILITHIUM)
  30. #ifdef HAVE_LIBOQS
  31. #include <oqs/oqs.h>
  32. #endif
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Macros Definitions */
  37. #ifdef HAVE_LIBOQS
  38. #define DILITHIUM_LEVEL2_KEY_SIZE OQS_SIG_dilithium_2_length_secret_key
  39. #define DILITHIUM_LEVEL2_SIG_SIZE OQS_SIG_dilithium_2_length_signature
  40. #define DILITHIUM_LEVEL2_PUB_KEY_SIZE OQS_SIG_dilithium_2_length_public_key
  41. #define DILITHIUM_LEVEL2_PRV_KEY_SIZE (DILITHIUM_LEVEL2_PUB_KEY_SIZE+DILITHIUM_LEVEL2_KEY_SIZE)
  42. #define DILITHIUM_LEVEL3_KEY_SIZE OQS_SIG_dilithium_3_length_secret_key
  43. #define DILITHIUM_LEVEL3_SIG_SIZE OQS_SIG_dilithium_3_length_signature
  44. #define DILITHIUM_LEVEL3_PUB_KEY_SIZE OQS_SIG_dilithium_3_length_public_key
  45. #define DILITHIUM_LEVEL3_PRV_KEY_SIZE (DILITHIUM_LEVEL3_PUB_KEY_SIZE+DILITHIUM_LEVEL3_KEY_SIZE)
  46. #define DILITHIUM_LEVEL5_KEY_SIZE OQS_SIG_dilithium_5_length_secret_key
  47. #define DILITHIUM_LEVEL5_SIG_SIZE OQS_SIG_dilithium_5_length_signature
  48. #define DILITHIUM_LEVEL5_PUB_KEY_SIZE OQS_SIG_dilithium_5_length_public_key
  49. #define DILITHIUM_LEVEL5_PRV_KEY_SIZE (DILITHIUM_LEVEL5_PUB_KEY_SIZE+DILITHIUM_LEVEL5_KEY_SIZE)
  50. #endif
  51. #define DILITHIUM_MAX_KEY_SIZE DILITHIUM_LEVEL5_PRV_KEY_SIZE
  52. #define DILITHIUM_MAX_SIG_SIZE DILITHIUM_LEVEL5_SIG_SIZE
  53. #define DILITHIUM_MAX_PUB_KEY_SIZE DILITHIUM_LEVEL5_PUB_KEY_SIZE
  54. #define DILITHIUM_MAX_PRV_KEY_SIZE DILITHIUM_LEVEL5_PRV_KEY_SIZE
  55. /* Structs */
  56. struct dilithium_key {
  57. bool pubKeySet;
  58. bool prvKeySet;
  59. byte level; /* 2,3 or 5 */
  60. byte p[DILITHIUM_MAX_PUB_KEY_SIZE];
  61. byte k[DILITHIUM_MAX_PRV_KEY_SIZE];
  62. };
  63. #ifndef WC_DILITHIUMKEY_TYPE_DEFINED
  64. typedef struct dilithium_key dilithium_key;
  65. #define WC_DILITHIUMKEY_TYPE_DEFINED
  66. #endif
  67. /* Functions */
  68. WOLFSSL_API
  69. int wc_dilithium_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen,
  70. dilithium_key* key);
  71. WOLFSSL_API
  72. int wc_dilithium_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  73. word32 msgLen, int* res, dilithium_key* key);
  74. WOLFSSL_API
  75. int wc_dilithium_init(dilithium_key* key);
  76. WOLFSSL_API
  77. int wc_dilithium_set_level(dilithium_key* key, byte level);
  78. WOLFSSL_API
  79. int wc_dilithium_get_level(dilithium_key* key, byte* level);
  80. WOLFSSL_API
  81. void wc_dilithium_free(dilithium_key* key);
  82. WOLFSSL_API
  83. int wc_dilithium_import_public(const byte* in, word32 inLen, dilithium_key* key);
  84. WOLFSSL_API
  85. int wc_dilithium_import_private_only(const byte* priv, word32 privSz,
  86. dilithium_key* key);
  87. WOLFSSL_API
  88. int wc_dilithium_import_private_key(const byte* priv, word32 privSz,
  89. const byte* pub, word32 pubSz,
  90. dilithium_key* key);
  91. WOLFSSL_API
  92. int wc_dilithium_export_public(dilithium_key*, byte* out, word32* outLen);
  93. WOLFSSL_API
  94. int wc_dilithium_export_private_only(dilithium_key* key, byte* out, word32* outLen);
  95. WOLFSSL_API
  96. int wc_dilithium_export_private(dilithium_key* key, byte* out, word32* outLen);
  97. WOLFSSL_API
  98. int wc_dilithium_export_key(dilithium_key* key, byte* priv, word32 *privSz,
  99. byte* pub, word32 *pubSz);
  100. WOLFSSL_API
  101. int wc_dilithium_check_key(dilithium_key* key);
  102. WOLFSSL_API
  103. int wc_dilithium_size(dilithium_key* key);
  104. WOLFSSL_API
  105. int wc_dilithium_priv_size(dilithium_key* key);
  106. WOLFSSL_API
  107. int wc_dilithium_pub_size(dilithium_key* key);
  108. WOLFSSL_API
  109. int wc_dilithium_sig_size(dilithium_key* key);
  110. WOLFSSL_API int wc_Dilithium_PrivateKeyDecode(const byte* input,
  111. word32* inOutIdx,
  112. dilithium_key* key, word32 inSz);
  113. WOLFSSL_API int wc_Dilithium_PublicKeyDecode(const byte* input,
  114. word32* inOutIdx,
  115. dilithium_key* key, word32 inSz);
  116. WOLFSSL_API int wc_Dilithium_KeyToDer(dilithium_key* key, byte* output,
  117. word32 inLen);
  118. WOLFSSL_API int wc_Dilithium_PrivateKeyToDer(dilithium_key* key, byte* output,
  119. word32 inLen);
  120. WOLFSSL_API int wc_Dilithium_PublicKeyToDer(dilithium_key* key, byte* output,
  121. word32 inLen, int withAlg);
  122. #ifdef __cplusplus
  123. } /* extern "C" */
  124. #endif
  125. #endif /* HAVE_PQC && HAVE_DILITHIUM */
  126. #endif /* WOLF_CRYPT_DILITHIUM_H */