lmots.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * \file lmots.h
  3. *
  4. * \brief This file provides an API for the LM-OTS post-quantum-safe one-time
  5. * public-key signature scheme as defined in RFC8554 and NIST.SP.200-208.
  6. * This implementation currently only supports a single parameter set
  7. * MBEDTLS_LMOTS_SHA256_N32_W8 in order to reduce complexity.
  8. */
  9. /*
  10. * Copyright The Mbed TLS Contributors
  11. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  12. */
  13. #ifndef MBEDTLS_LMOTS_H
  14. #define MBEDTLS_LMOTS_H
  15. #include "mbedtls/build_info.h"
  16. #include "psa/crypto.h"
  17. #include "mbedtls/lms.h"
  18. #include <stdint.h>
  19. #include <stddef.h>
  20. #define MBEDTLS_LMOTS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \
  21. MBEDTLS_LMOTS_I_KEY_ID_LEN + \
  22. MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \
  23. MBEDTLS_LMOTS_N_HASH_LEN(type))
  24. #define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
  25. #define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + \
  26. MBEDTLS_LMOTS_TYPE_LEN)
  27. #define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(type) (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + \
  28. MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type))
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #if defined(MBEDTLS_TEST_HOOKS)
  33. extern int (*mbedtls_lmots_sign_private_key_invalidated_hook)(unsigned char *);
  34. #endif /* defined(MBEDTLS_TEST_HOOKS) */
  35. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  36. /**
  37. * \brief This function converts a \ref psa_status_t to a
  38. * low-level LMS error code.
  39. *
  40. * \param status The psa_status_t to convert
  41. *
  42. * \return The corresponding LMS error code.
  43. */
  44. int MBEDTLS_DEPRECATED mbedtls_lms_error_from_psa(psa_status_t status);
  45. #endif
  46. /**
  47. * \brief This function initializes a public LMOTS context
  48. *
  49. * \param ctx The uninitialized LMOTS context that will then be
  50. * initialized.
  51. */
  52. void mbedtls_lmots_public_init(mbedtls_lmots_public_t *ctx);
  53. /**
  54. * \brief This function uninitializes a public LMOTS context
  55. *
  56. * \param ctx The initialized LMOTS context that will then be
  57. * uninitialized.
  58. */
  59. void mbedtls_lmots_public_free(mbedtls_lmots_public_t *ctx);
  60. /**
  61. * \brief This function imports an LMOTS public key into a
  62. * LMOTS context.
  63. *
  64. * \note Before this function is called, the context must
  65. * have been initialized.
  66. *
  67. * \note See IETF RFC8554 for details of the encoding of
  68. * this public key.
  69. *
  70. * \param ctx The initialized LMOTS context store the key in.
  71. * \param key The buffer from which the key will be read.
  72. * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read
  73. * from this.
  74. *
  75. * \return \c 0 on success.
  76. * \return A non-zero error code on failure.
  77. */
  78. int mbedtls_lmots_import_public_key(mbedtls_lmots_public_t *ctx,
  79. const unsigned char *key, size_t key_size);
  80. /**
  81. * \brief This function exports an LMOTS public key from a
  82. * LMOTS context that already contains a public key.
  83. *
  84. * \note Before this function is called, the context must
  85. * have been initialized and the context must contain
  86. * a public key.
  87. *
  88. * \note See IETF RFC8554 for details of the encoding of
  89. * this public key.
  90. *
  91. * \param ctx The initialized LMOTS context that contains the
  92. * public key.
  93. * \param key The buffer into which the key will be output. Must
  94. * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size.
  95. *
  96. * \return \c 0 on success.
  97. * \return A non-zero error code on failure.
  98. */
  99. int mbedtls_lmots_export_public_key(const mbedtls_lmots_public_t *ctx,
  100. unsigned char *key, size_t key_size,
  101. size_t *key_len);
  102. /**
  103. * \brief This function creates a candidate public key from
  104. * an LMOTS signature. This can then be compared to
  105. * the real public key to determine the validity of
  106. * the signature.
  107. *
  108. * \note This function is exposed publicly to be used in LMS
  109. * signature verification, it is expected that
  110. * mbedtls_lmots_verify will be used for LMOTS
  111. * signature verification.
  112. *
  113. * \param params The LMOTS parameter set, q and I values as an
  114. * mbedtls_lmots_parameters_t struct.
  115. * \param msg The buffer from which the message will be read.
  116. * \param msg_size The size of the message that will be read.
  117. * \param sig The buffer from which the signature will be read.
  118. * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
  119. * this.
  120. * \param out The buffer where the candidate public key will be
  121. * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
  122. * bytes in size.
  123. *
  124. * \return \c 0 on success.
  125. * \return A non-zero error code on failure.
  126. */
  127. int mbedtls_lmots_calculate_public_key_candidate(const mbedtls_lmots_parameters_t *params,
  128. const unsigned char *msg,
  129. size_t msg_size,
  130. const unsigned char *sig,
  131. size_t sig_size,
  132. unsigned char *out,
  133. size_t out_size,
  134. size_t *out_len);
  135. /**
  136. * \brief This function verifies a LMOTS signature, using a
  137. * LMOTS context that contains a public key.
  138. *
  139. * \warning This function is **not intended for use in
  140. * production**, due to as-yet unsolved problems with
  141. * handling stateful keys. The API for this function
  142. * may change considerably in future versions.
  143. *
  144. * \note Before this function is called, the context must
  145. * have been initialized and must contain a public key
  146. * (either by import or calculation from a private
  147. * key).
  148. *
  149. * \param ctx The initialized LMOTS context from which the public
  150. * key will be read.
  151. * \param msg The buffer from which the message will be read.
  152. * \param msg_size The size of the message that will be read.
  153. * \param sig The buf from which the signature will be read.
  154. * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
  155. * this.
  156. *
  157. * \return \c 0 on successful verification.
  158. * \return A non-zero error code on failure.
  159. */
  160. int mbedtls_lmots_verify(const mbedtls_lmots_public_t *ctx,
  161. const unsigned char *msg,
  162. size_t msg_size, const unsigned char *sig,
  163. size_t sig_size);
  164. #if defined(MBEDTLS_LMS_PRIVATE)
  165. /**
  166. * \brief This function initializes a private LMOTS context
  167. *
  168. * \param ctx The uninitialized LMOTS context that will then be
  169. * initialized.
  170. */
  171. void mbedtls_lmots_private_init(mbedtls_lmots_private_t *ctx);
  172. /**
  173. * \brief This function uninitializes a private LMOTS context
  174. *
  175. * \param ctx The initialized LMOTS context that will then be
  176. * uninitialized.
  177. */
  178. void mbedtls_lmots_private_free(mbedtls_lmots_private_t *ctx);
  179. /**
  180. * \brief This function calculates an LMOTS private key, and
  181. * stores in into an LMOTS context.
  182. *
  183. * \warning This function is **not intended for use in
  184. * production**, due to as-yet unsolved problems with
  185. * handling stateful keys. The API for this function
  186. * may change considerably in future versions.
  187. *
  188. * \note The seed must have at least 256 bits of entropy.
  189. *
  190. * \param ctx The initialized LMOTS context to generate the key
  191. * into.
  192. * \param I_key_identifier The key identifier of the key, as a 16-byte string.
  193. * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
  194. * not being used as part of an LMS key, this should
  195. * be set to 0.
  196. * \param seed The seed used to deterministically generate the
  197. * key.
  198. * \param seed_size The length of the seed.
  199. *
  200. * \return \c 0 on success.
  201. * \return A non-zero error code on failure.
  202. */
  203. int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx,
  204. mbedtls_lmots_algorithm_type_t type,
  205. const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
  206. uint32_t q_leaf_identifier,
  207. const unsigned char *seed,
  208. size_t seed_size);
  209. /**
  210. * \brief This function generates an LMOTS public key from a
  211. * LMOTS context that already contains a private key.
  212. *
  213. * \note Before this function is called, the context must
  214. * have been initialized and the context must contain
  215. * a private key.
  216. *
  217. * \param ctx The initialized LMOTS context to generate the key
  218. * from and store it into.
  219. *
  220. * \return \c 0 on success.
  221. * \return A non-zero error code on failure.
  222. */
  223. int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx,
  224. const mbedtls_lmots_private_t *priv_ctx);
  225. /**
  226. * \brief This function creates a LMOTS signature, using a
  227. * LMOTS context that contains a private key.
  228. *
  229. * \note Before this function is called, the context must
  230. * have been initialized and must contain a private
  231. * key.
  232. *
  233. * \note LMOTS private keys can only be used once, otherwise
  234. * attackers may be able to create forged signatures.
  235. * If the signing operation is successful, the private
  236. * key in the context will be erased, and no further
  237. * signing will be possible until another private key
  238. * is loaded
  239. *
  240. * \param ctx The initialized LMOTS context from which the
  241. * private key will be read.
  242. * \param f_rng The RNG function to be used for signature
  243. * generation.
  244. * \param p_rng The RNG context to be passed to f_rng
  245. * \param msg The buffer from which the message will be read.
  246. * \param msg_size The size of the message that will be read.
  247. * \param sig The buf into which the signature will be stored.
  248. * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
  249. *
  250. * \return \c 0 on success.
  251. * \return A non-zero error code on failure.
  252. */
  253. int mbedtls_lmots_sign(mbedtls_lmots_private_t *ctx,
  254. int (*f_rng)(void *, unsigned char *, size_t),
  255. void *p_rng, const unsigned char *msg, size_t msg_size,
  256. unsigned char *sig, size_t sig_size, size_t *sig_len);
  257. #endif /* defined(MBEDTLS_LMS_PRIVATE) */
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. #endif /* MBEDTLS_LMOTS_H */