xmss.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* xmss.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/xmss.h
  23. */
  24. #ifndef WOLF_CRYPT_XMSS_H
  25. #define WOLF_CRYPT_XMSS_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #include <wolfssl/wolfcrypt/random.h>
  28. #ifdef WOLFSSL_HAVE_XMSS
  29. /* Note on XMSS/XMSS^MT pub/priv key sizes:
  30. * - The XMSS/XMSS^MT pub key has a defined format and size.
  31. * - The XMSS/XMSS^MT private key is implementation and parameter
  32. * specific. It does not have a standardized format or size.
  33. *
  34. * The XMSS/XMSS^MT public and secret key format and length is:
  35. * PK = OID || root || SEED;
  36. * PK_len = 4 + 2 * n
  37. *
  38. * SK = OID || (implementation defined)
  39. * SK_len = 4 + (implementation defined)
  40. *
  41. * where n is the number of bytes in the hash function, which is 32
  42. * in this SHA256 implementation.
  43. *
  44. * However the private key is implementation specific. For example,
  45. * in xmss-reference the private key size varies from 137 bytes to
  46. * 1377 bytes between slow and fast implementations with param name
  47. * "XMSSMT-SHA2_20/2_256".
  48. *
  49. * References:
  50. * - RFC 8391
  51. * - Table 2 of Kampanakis, Fluhrer, IACR, 2017.
  52. * */
  53. #define XMSS_SHA256_PUBLEN (68)
  54. /* Supported XMSS/XMSS^MT parameter set names:
  55. * We are supporting all SHA256 parameter sets with n=32 and
  56. * Winternitz=16, from RFC 8391 and NIST SP 800-208.
  57. *
  58. * ----------------------------------------------------------
  59. * | Name OID n w len h d |
  60. * XMSS: | "XMSS-SHA2_10_256" 0x00000001 32 16 67 10 1 |
  61. * | "XMSS-SHA2_16_256" 0x00000002 32 16 67 16 1 |
  62. * | "XMSS-SHA2_20_256" 0x00000003 32 16 67 20 1 |
  63. * | |
  64. * XMSSMT: | "XMSSMT-SHA2_20/2_256" 0x00000001 32 16 67 20 2 |
  65. * | "XMSSMT-SHA2_20/4_256" 0x00000002 32 16 67 20 4 |
  66. * | "XMSSMT-SHA2_40/2_256" 0x00000003 32 16 67 40 2 |
  67. * | "XMSSMT-SHA2_40/4_256" 0x00000004 32 16 67 40 4 |
  68. * | "XMSSMT-SHA2_40/8_256" 0x00000005 32 16 67 40 8 |
  69. * | "XMSSMT-SHA2_60/3_256" 0x00000006 32 16 67 60 3 |
  70. * | "XMSSMT-SHA2_60/6_256" 0x00000007 32 16 67 60 6 |
  71. * | "XMSSMT-SHA2_60/12_256" 0x00000008 32 16 67 60 12 |
  72. * ----------------------------------------------------------
  73. *
  74. * Note that some XMSS and XMSSMT names do have overlapping OIDs.
  75. *
  76. * References:
  77. * 1. NIST SP 800-208
  78. * 2. RFC 8391
  79. * */
  80. #define XMSS_NAME_LEN (16) /* strlen("XMSS-SHA2_10_256") */
  81. #define XMSSMT_NAME_MIN_LEN (20) /* strlen("XMSSMT-SHA2_20/2_256") */
  82. #define XMSSMT_NAME_MAX_LEN (21) /* strlen("XMSSMT-SHA2_60/12_256") */
  83. typedef struct XmssKey XmssKey;
  84. /* Return codes returned by private key callbacks. */
  85. enum wc_XmssRc {
  86. WC_XMSS_RC_NONE,
  87. WC_XMSS_RC_BAD_ARG, /* Bad arg in read or write callback. */
  88. WC_XMSS_RC_WRITE_FAIL, /* Write or update private key failed. */
  89. WC_XMSS_RC_READ_FAIL, /* Read private key failed. */
  90. WC_XMSS_RC_SAVED_TO_NV_MEMORY, /* Wrote private key to nonvolatile storage. */
  91. WC_XMSS_RC_READ_TO_MEMORY /* Read private key from storage. */
  92. };
  93. /* enum wc_XmssState is to help track the state of an XMSS Key. */
  94. enum wc_XmssState {
  95. WC_XMSS_STATE_FREED, /* Key has been freed from memory. */
  96. WC_XMSS_STATE_INITED, /* Key has been inited, ready to set params.*/
  97. WC_XMSS_STATE_PARMSET, /* Params are set, ready to MakeKey or Reload. */
  98. WC_XMSS_STATE_OK, /* Able to sign signatures and verify. */
  99. WC_XMSS_STATE_VERIFYONLY, /* A public only XmssKey. */
  100. WC_XMSS_STATE_BAD, /* Can't guarantee key's state. */
  101. WC_XMSS_STATE_NOSIGS /* Signatures exhausted. */
  102. };
  103. /* Private key write and read callbacks. */
  104. typedef enum wc_XmssRc (*write_private_key_cb)(const byte * priv, word32 privSz, void *context);
  105. typedef enum wc_XmssRc (*read_private_key_cb)(byte * priv, word32 privSz, void *context);
  106. #ifdef __cplusplus
  107. extern "C" {
  108. #endif
  109. WOLFSSL_API int wc_XmssKey_Init(XmssKey * key, void * heap, int devId);
  110. WOLFSSL_API int wc_XmssKey_SetParamStr(XmssKey * key, const char * str);
  111. #ifndef WOLFSSL_XMSS_VERIFY_ONLY
  112. WOLFSSL_API int wc_XmssKey_SetWriteCb(XmssKey * key,
  113. write_private_key_cb write_cb);
  114. WOLFSSL_API int wc_XmssKey_SetReadCb(XmssKey * key,
  115. read_private_key_cb read_cb);
  116. WOLFSSL_API int wc_XmssKey_SetContext(XmssKey * key, void * context);
  117. WOLFSSL_API int wc_XmssKey_MakeKey(XmssKey * key, WC_RNG * rng);
  118. WOLFSSL_API int wc_XmssKey_Reload(XmssKey * key);
  119. WOLFSSL_API int wc_XmssKey_GetPrivLen(const XmssKey * key, word32 * len);
  120. WOLFSSL_API int wc_XmssKey_Sign(XmssKey * key, byte * sig, word32 * sigSz,
  121. const byte * msg, int msgSz);
  122. WOLFSSL_API int wc_XmssKey_SigsLeft(XmssKey * key);
  123. #endif /* ifndef WOLFSSL_XMSS_VERIFY_ONLY */
  124. WOLFSSL_API void wc_XmssKey_Free(XmssKey * key);
  125. WOLFSSL_API int wc_XmssKey_GetSigLen(const XmssKey * key, word32 * len);
  126. WOLFSSL_API int wc_XmssKey_GetPubLen(const XmssKey * key, word32 * len);
  127. WOLFSSL_API int wc_XmssKey_ExportPub(XmssKey * keyDst, const XmssKey * keySrc);
  128. WOLFSSL_API int wc_XmssKey_ExportPubRaw(const XmssKey * key, byte * out,
  129. word32 * outLen);
  130. WOLFSSL_API int wc_XmssKey_ImportPubRaw(XmssKey * key, const byte * in,
  131. word32 inLen);
  132. WOLFSSL_API int wc_XmssKey_Verify(XmssKey * key, const byte * sig, word32 sigSz,
  133. const byte * msg, int msgSz);
  134. #ifdef __cplusplus
  135. } /* extern "C" */
  136. #endif
  137. #endif /* WOLFSSL_HAVE_XMSS */
  138. #endif /* WOLF_CRYPT_XMSS_H */