sphincs.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* sphincs.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/sphincs.h
  23. */
  24. /* Interfaces for Sphincs:
  25. * - SPHINCS_FAST_LEVEL1 (AKA SPHINCS+-SHAKE-128f-simple)
  26. * - SPHINCS_FAST_LEVEL3 (AKA SPHINCS+-SHAKE-192f-simple)
  27. * - SPHINCS_FAST_LEVEL5 (AKA SPHINCS+-SHAKE-256f-simple)
  28. * - SPHINCS_SMALL_LEVEL1 (AKA SPHINCS+-SHAKE-128s-simple)
  29. * - SPHINCS_SMALL_LEVEL3 (AKA SPHINCS+-SHAKE-192s-simple)
  30. * - SPHINCS_SMALL_LEVEL5 (AKA SPHINCS+-SHAKE-256s-simple)
  31. */
  32. #ifndef WOLF_CRYPT_SPHINCS_H
  33. #define WOLF_CRYPT_SPHINCS_H
  34. #include <wolfssl/wolfcrypt/types.h>
  35. #if defined(HAVE_PQC) && defined(HAVE_SPHINCS)
  36. #ifdef HAVE_LIBOQS
  37. #include <oqs/oqs.h>
  38. #endif
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /* Macros Definitions */
  43. #ifdef HAVE_LIBOQS
  44. #define SPHINCS_FAST_LEVEL1_SIG_SIZE OQS_SIG_sphincs_shake_128f_simple_length_signature
  45. #define SPHINCS_FAST_LEVEL3_SIG_SIZE OQS_SIG_sphincs_shake_192f_simple_length_signature
  46. #define SPHINCS_FAST_LEVEL5_SIG_SIZE OQS_SIG_sphincs_shake_256f_simple_length_signature
  47. #define SPHINCS_SMALL_LEVEL1_SIG_SIZE OQS_SIG_sphincs_shake_128s_simple_length_signature
  48. #define SPHINCS_SMALL_LEVEL3_SIG_SIZE OQS_SIG_sphincs_shake_192s_simple_length_signature
  49. #define SPHINCS_SMALL_LEVEL5_SIG_SIZE OQS_SIG_sphincs_shake_256s_simple_length_signature
  50. #define SPHINCS_LEVEL1_KEY_SIZE OQS_SIG_sphincs_shake_128f_simple_length_secret_key
  51. #define SPHINCS_LEVEL1_PUB_KEY_SIZE OQS_SIG_sphincs_shake_128f_simple_length_public_key
  52. #define SPHINCS_LEVEL1_PRV_KEY_SIZE (SPHINCS_LEVEL1_PUB_KEY_SIZE+SPHINCS_LEVEL1_KEY_SIZE)
  53. #define SPHINCS_LEVEL3_KEY_SIZE OQS_SIG_sphincs_shake_192f_simple_length_secret_key
  54. #define SPHINCS_LEVEL3_PUB_KEY_SIZE OQS_SIG_sphincs_shake_192f_simple_length_public_key
  55. #define SPHINCS_LEVEL3_PRV_KEY_SIZE (SPHINCS_LEVEL3_PUB_KEY_SIZE+SPHINCS_LEVEL3_KEY_SIZE)
  56. #define SPHINCS_LEVEL5_KEY_SIZE OQS_SIG_sphincs_shake_256f_simple_length_secret_key
  57. #define SPHINCS_LEVEL5_PUB_KEY_SIZE OQS_SIG_sphincs_shake_256f_simple_length_public_key
  58. #define SPHINCS_LEVEL5_PRV_KEY_SIZE (SPHINCS_LEVEL5_PUB_KEY_SIZE+SPHINCS_LEVEL5_KEY_SIZE)
  59. #endif
  60. #define SPHINCS_MAX_SIG_SIZE SPHINCS_FAST_LEVEL5_SIG_SIZE
  61. #define SPHINCS_MAX_KEY_SIZE SPHINCS_LEVEL5_PRV_KEY_SIZE
  62. #define SPHINCS_MAX_PUB_KEY_SIZE SPHINCS_LEVEL5_PUB_KEY_SIZE
  63. #define SPHINCS_MAX_PRV_KEY_SIZE SPHINCS_LEVEL5_PRV_KEY_SIZE
  64. #define FAST_VARIANT 1
  65. #define SMALL_VARIANT 2
  66. /* Structs */
  67. struct sphincs_key {
  68. bool pubKeySet;
  69. bool prvKeySet;
  70. byte level; /* 1,3 or 5 */
  71. byte optim; /* FAST_VARIANT or SMALL_VARIANT */
  72. byte p[SPHINCS_MAX_PUB_KEY_SIZE];
  73. byte k[SPHINCS_MAX_PRV_KEY_SIZE];
  74. };
  75. #ifndef WC_SPHINCSKEY_TYPE_DEFINED
  76. typedef struct sphincs_key sphincs_key;
  77. #define WC_SPHINCSKEY_TYPE_DEFINED
  78. #endif
  79. /* Functions */
  80. WOLFSSL_API
  81. int wc_sphincs_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen,
  82. sphincs_key* key);
  83. WOLFSSL_API
  84. int wc_sphincs_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  85. word32 msgLen, int* res, sphincs_key* key);
  86. WOLFSSL_API
  87. int wc_sphincs_init(sphincs_key* key);
  88. WOLFSSL_API
  89. int wc_sphincs_set_level_and_optim(sphincs_key* key, byte level, byte optim);
  90. WOLFSSL_API
  91. int wc_sphincs_get_level_and_optim(sphincs_key* key, byte* level, byte *optim);
  92. WOLFSSL_API
  93. void wc_sphincs_free(sphincs_key* key);
  94. WOLFSSL_API
  95. int wc_sphincs_import_public(const byte* in, word32 inLen, sphincs_key* key);
  96. WOLFSSL_API
  97. int wc_sphincs_import_private_only(const byte* priv, word32 privSz,
  98. sphincs_key* key);
  99. WOLFSSL_API
  100. int wc_sphincs_import_private_key(const byte* priv, word32 privSz,
  101. const byte* pub, word32 pubSz,
  102. sphincs_key* key);
  103. WOLFSSL_API
  104. int wc_sphincs_export_public(sphincs_key*, byte* out, word32* outLen);
  105. WOLFSSL_API
  106. int wc_sphincs_export_private_only(sphincs_key* key, byte* out, word32* outLen);
  107. WOLFSSL_API
  108. int wc_sphincs_export_private(sphincs_key* key, byte* out, word32* outLen);
  109. WOLFSSL_API
  110. int wc_sphincs_export_key(sphincs_key* key, byte* priv, word32 *privSz,
  111. byte* pub, word32 *pubSz);
  112. WOLFSSL_API
  113. int wc_sphincs_check_key(sphincs_key* key);
  114. WOLFSSL_API
  115. int wc_sphincs_size(sphincs_key* key);
  116. WOLFSSL_API
  117. int wc_sphincs_priv_size(sphincs_key* key);
  118. WOLFSSL_API
  119. int wc_sphincs_pub_size(sphincs_key* key);
  120. WOLFSSL_API
  121. int wc_sphincs_sig_size(sphincs_key* key);
  122. WOLFSSL_API int wc_Sphincs_PrivateKeyDecode(const byte* input,
  123. word32* inOutIdx,
  124. sphincs_key* key, word32 inSz);
  125. WOLFSSL_API int wc_Sphincs_PublicKeyDecode(const byte* input,
  126. word32* inOutIdx,
  127. sphincs_key* key, word32 inSz);
  128. WOLFSSL_API int wc_Sphincs_KeyToDer(sphincs_key* key, byte* output,
  129. word32 inLen);
  130. WOLFSSL_API int wc_Sphincs_PrivateKeyToDer(sphincs_key* key, byte* output,
  131. word32 inLen);
  132. WOLFSSL_API int wc_Sphincs_PublicKeyToDer(sphincs_key* key, byte* output,
  133. word32 inLen, int withAlg);
  134. #ifdef __cplusplus
  135. } /* extern "C" */
  136. #endif
  137. #endif /* HAVE_PQC && HAVE_SPHINCS */
  138. #endif /* WOLF_CRYPT_SPHINCS_H */