lms.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* lms.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/lms.h
  23. */
  24. #ifndef WOLF_CRYPT_LMS_H
  25. #define WOLF_CRYPT_LMS_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #include <wolfssl/wolfcrypt/random.h>
  28. #ifdef WOLFSSL_HAVE_LMS
  29. typedef struct LmsKey LmsKey;
  30. /* Private key write and read callbacks. */
  31. typedef int (*write_private_key_cb)(const byte * priv, word32 privSz, void *context);
  32. typedef int (*read_private_key_cb)(byte * priv, word32 privSz, void *context);
  33. /* Return codes returned by private key callbacks. */
  34. enum wc_LmsRc {
  35. WC_LMS_RC_NONE,
  36. WC_LMS_RC_BAD_ARG, /* Bad arg in read or write callback. */
  37. WC_LMS_RC_WRITE_FAIL, /* Write or update private key failed. */
  38. WC_LMS_RC_READ_FAIL, /* Read private key failed. */
  39. WC_LMS_RC_SAVED_TO_NV_MEMORY, /* Wrote private key to nonvolatile storage. */
  40. WC_LMS_RC_READ_TO_MEMORY /* Read private key from storage. */
  41. };
  42. /* LMS/HSS signatures are defined by 3 parameters:
  43. * levels: number of levels of Merkle trees.
  44. * height: height of an individual Merkle tree.
  45. * winternitz: number of bits from hash used in a Winternitz chain.
  46. *
  47. * The acceptable parameter values are those in RFC8554:
  48. * levels = {1..8}
  49. * height = {5, 10, 15, 20, 25}
  50. * winternitz = {1, 2, 4, 8}
  51. *
  52. * The number of available signatures is:
  53. * N = 2 ** (levels * height)
  54. *
  55. * Signature sizes are determined by levels and winternitz
  56. * parameters primarily, and height to a lesser extent:
  57. * - Larger levels values increase signature size significantly.
  58. * - Larger height values increase signature size moderately.
  59. * - Larger winternitz values will reduce the signature size, at
  60. * the expense of longer key generation and sign/verify times.
  61. *
  62. * Key generation time is strongly determined by the height of
  63. * the first level tree. A 3 level, 5 height tree is much faster
  64. * than 1 level, 15 height at initial key gen, even if the number
  65. * of available signatures is the same.
  66. * */
  67. /* Predefined LMS/HSS parameter sets for convenience.
  68. *
  69. * Not predefining a set with Winternitz=1, because the signatures
  70. * will be large. */
  71. enum wc_LmsParm {
  72. WC_LMS_PARM_NONE = 0,
  73. WC_LMS_PARM_L1_H15_W2 = 1, /* 1 level Merkle tree of 15 height. */
  74. WC_LMS_PARM_L1_H15_W4 = 2,
  75. WC_LMS_PARM_L2_H10_W2 = 3, /* 2 level Merkle tree of 10 height. */
  76. WC_LMS_PARM_L2_H10_W4 = 4,
  77. WC_LMS_PARM_L2_H10_W8 = 5,
  78. WC_LMS_PARM_L3_H5_W2 = 6, /* 3 level Merkle tree of 5 height. */
  79. WC_LMS_PARM_L3_H5_W4 = 7,
  80. WC_LMS_PARM_L3_H5_W8 = 8,
  81. WC_LMS_PARM_L3_H10_W4 = 9, /* 3 level Merkle tree of 10 height. */
  82. WC_LMS_PARM_L4_H5_W8 = 10, /* 4 level Merkle tree of 5 height. */
  83. };
  84. /* enum wc_LmsState is to help track the state of an LMS/HSS Key. */
  85. enum wc_LmsState {
  86. WC_LMS_STATE_FREED, /* Key has been freed from memory. */
  87. WC_LMS_STATE_INITED, /* Key has been inited, ready to set params.*/
  88. WC_LMS_STATE_PARMSET, /* Params are set, ready to MakeKey or Reload. */
  89. WC_LMS_STATE_OK, /* Able to sign signatures and verify. */
  90. WC_LMS_STATE_VERIFYONLY, /* A public only LmsKey. */
  91. WC_LMS_STATE_BAD, /* Can't guarantee key's state. */
  92. WC_LMS_STATE_NOSIGS /* Signatures exhausted. */
  93. };
  94. #ifdef __cplusplus
  95. extern "C" {
  96. #endif
  97. WOLFSSL_API int wc_LmsKey_Init(LmsKey * key, void * heap, int devId);
  98. WOLFSSL_API int wc_LmsKey_SetLmsParm(LmsKey * key, enum wc_LmsParm lmsParm);
  99. WOLFSSL_API int wc_LmsKey_SetParameters(LmsKey * key, int levels,
  100. int height, int winternitz);
  101. WOLFSSL_API int wc_LmsKey_GetParameters(const LmsKey * key, int * levels,
  102. int * height, int * winternitz);
  103. #ifndef WOLFSSL_LMS_VERIFY_ONLY
  104. WOLFSSL_API int wc_LmsKey_SetWriteCb(LmsKey * key,
  105. write_private_key_cb write_cb);
  106. WOLFSSL_API int wc_LmsKey_SetReadCb(LmsKey * key,
  107. read_private_key_cb read_cb);
  108. WOLFSSL_API int wc_LmsKey_SetContext(LmsKey * key, void * context);
  109. WOLFSSL_API int wc_LmsKey_MakeKey(LmsKey * key, WC_RNG * rng);
  110. WOLFSSL_API int wc_LmsKey_Reload(LmsKey * key);
  111. WOLFSSL_API int wc_LmsKey_GetPrivLen(const LmsKey * key, word32 * len);
  112. WOLFSSL_API int wc_LmsKey_Sign(LmsKey * key, byte * sig, word32 * sigSz,
  113. const byte * msg, int msgSz);
  114. WOLFSSL_API int wc_LmsKey_SigsLeft(LmsKey * key);
  115. #endif /* ifndef WOLFSSL_LMS_VERIFY_ONLY */
  116. WOLFSSL_API void wc_LmsKey_Free(LmsKey * key);
  117. WOLFSSL_API int wc_LmsKey_GetSigLen(const LmsKey * key, word32 * len);
  118. WOLFSSL_API int wc_LmsKey_GetPubLen(const LmsKey * key, word32 * len);
  119. WOLFSSL_API int wc_LmsKey_ExportPub(LmsKey * keyDst, const LmsKey * keySrc);
  120. WOLFSSL_API int wc_LmsKey_ExportPubRaw(const LmsKey * key, byte * out,
  121. word32 * outLen);
  122. WOLFSSL_API int wc_LmsKey_ImportPubRaw(LmsKey * key, const byte * in,
  123. word32 inLen);
  124. WOLFSSL_API int wc_LmsKey_Verify(LmsKey * key, const byte * sig, word32 sigSz,
  125. const byte * msg, int msgSz);
  126. WOLFSSL_API const char * wc_LmsKey_ParmToStr(enum wc_LmsParm lmsParm);
  127. WOLFSSL_API const char * wc_LmsKey_RcToStr(enum wc_LmsRc lmsRc);
  128. #ifdef __cplusplus
  129. } /* extern "C" */
  130. #endif
  131. #endif /* WOLFSSL_HAVE_LMS */
  132. #endif /* WOLF_CRYPT_LMS_H */