lmots.h 14 KB

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