md.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /**
  2. * \file md.h
  3. *
  4. * \brief This file contains the generic functions for message-digest
  5. * (hashing) and HMAC.
  6. *
  7. * \author Adriaan de Jong <dejong@fox-it.com>
  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_MD_H
  14. #define MBEDTLS_MD_H
  15. #include "mbedtls/private_access.h"
  16. #include <stddef.h>
  17. #include "mbedtls/build_info.h"
  18. #include "mbedtls/platform_util.h"
  19. /** The selected feature is not available. */
  20. #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
  21. /** Bad input parameters to function. */
  22. #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
  23. /** Failed to allocate memory. */
  24. #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
  25. /** Opening or reading of file failed. */
  26. #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /**
  31. * \brief Supported message digests.
  32. *
  33. * \warning MD5 and SHA-1 are considered weak message digests and
  34. * their use constitutes a security risk. We recommend considering
  35. * stronger message digests instead.
  36. *
  37. */
  38. /* Note: these are aligned with the definitions of PSA_ALG_ macros for hashes,
  39. * in order to enable an efficient implementation of conversion functions.
  40. * This is tested by md_to_from_psa() in test_suite_md. */
  41. typedef enum {
  42. MBEDTLS_MD_NONE=0, /**< None. */
  43. MBEDTLS_MD_MD5=0x03, /**< The MD5 message digest. */
  44. MBEDTLS_MD_RIPEMD160=0x04, /**< The RIPEMD-160 message digest. */
  45. MBEDTLS_MD_SHA1=0x05, /**< The SHA-1 message digest. */
  46. MBEDTLS_MD_SHA224=0x08, /**< The SHA-224 message digest. */
  47. MBEDTLS_MD_SHA256=0x09, /**< The SHA-256 message digest. */
  48. MBEDTLS_MD_SHA384=0x0a, /**< The SHA-384 message digest. */
  49. MBEDTLS_MD_SHA512=0x0b, /**< The SHA-512 message digest. */
  50. MBEDTLS_MD_SHA3_224=0x10, /**< The SHA3-224 message digest. */
  51. MBEDTLS_MD_SHA3_256=0x11, /**< The SHA3-256 message digest. */
  52. MBEDTLS_MD_SHA3_384=0x12, /**< The SHA3-384 message digest. */
  53. MBEDTLS_MD_SHA3_512=0x13, /**< The SHA3-512 message digest. */
  54. } mbedtls_md_type_t;
  55. /* Note: this should always be >= PSA_HASH_MAX_SIZE
  56. * in all builds with both CRYPTO_C and MD_LIGHT.
  57. *
  58. * This is to make things easier for modules such as TLS that may define a
  59. * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA
  60. * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another
  61. * part of the code based on PSA.
  62. */
  63. #if defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA3_512)
  64. #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
  65. #elif defined(MBEDTLS_MD_CAN_SHA384) || defined(MBEDTLS_MD_CAN_SHA3_384)
  66. #define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
  67. #elif defined(MBEDTLS_MD_CAN_SHA256) || defined(MBEDTLS_MD_CAN_SHA3_256)
  68. #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
  69. #elif defined(MBEDTLS_MD_CAN_SHA224) || defined(MBEDTLS_MD_CAN_SHA3_224)
  70. #define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
  71. #else
  72. #define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
  73. or smaller (MD5 and earlier) */
  74. #endif
  75. #if defined(MBEDTLS_MD_CAN_SHA3_224)
  76. #define MBEDTLS_MD_MAX_BLOCK_SIZE 144 /* the longest known is SHA3-224 */
  77. #elif defined(MBEDTLS_MD_CAN_SHA3_256)
  78. #define MBEDTLS_MD_MAX_BLOCK_SIZE 136
  79. #elif defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA384)
  80. #define MBEDTLS_MD_MAX_BLOCK_SIZE 128
  81. #elif defined(MBEDTLS_MD_CAN_SHA3_384)
  82. #define MBEDTLS_MD_MAX_BLOCK_SIZE 104
  83. #elif defined(MBEDTLS_MD_CAN_SHA3_512)
  84. #define MBEDTLS_MD_MAX_BLOCK_SIZE 72
  85. #else
  86. #define MBEDTLS_MD_MAX_BLOCK_SIZE 64
  87. #endif
  88. /**
  89. * Opaque struct.
  90. *
  91. * Constructed using either #mbedtls_md_info_from_string or
  92. * #mbedtls_md_info_from_type.
  93. *
  94. * Fields can be accessed with #mbedtls_md_get_size,
  95. * #mbedtls_md_get_type and #mbedtls_md_get_name.
  96. */
  97. /* Defined internally in library/md_wrap.h. */
  98. typedef struct mbedtls_md_info_t mbedtls_md_info_t;
  99. /**
  100. * Used internally to indicate whether a context uses legacy or PSA.
  101. *
  102. * Internal use only.
  103. */
  104. typedef enum {
  105. MBEDTLS_MD_ENGINE_LEGACY = 0,
  106. MBEDTLS_MD_ENGINE_PSA,
  107. } mbedtls_md_engine_t;
  108. /**
  109. * The generic message-digest context.
  110. */
  111. typedef struct mbedtls_md_context_t {
  112. /** Information about the associated message digest. */
  113. const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
  114. #if defined(MBEDTLS_MD_SOME_PSA)
  115. /** Are hash operations dispatched to PSA or legacy? */
  116. mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
  117. #endif
  118. /** The digest-specific context (legacy) or the PSA operation. */
  119. void *MBEDTLS_PRIVATE(md_ctx);
  120. #if defined(MBEDTLS_MD_C)
  121. /** The HMAC part of the context. */
  122. void *MBEDTLS_PRIVATE(hmac_ctx);
  123. #endif
  124. } mbedtls_md_context_t;
  125. /**
  126. * \brief This function returns the message-digest information
  127. * associated with the given digest type.
  128. *
  129. * \param md_type The type of digest to search for.
  130. *
  131. * \return The message-digest information associated with \p md_type.
  132. * \return NULL if the associated message-digest information is not found.
  133. */
  134. const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
  135. /**
  136. * \brief This function initializes a message-digest context without
  137. * binding it to a particular message-digest algorithm.
  138. *
  139. * This function should always be called first. It prepares the
  140. * context for mbedtls_md_setup() for binding it to a
  141. * message-digest algorithm.
  142. */
  143. void mbedtls_md_init(mbedtls_md_context_t *ctx);
  144. /**
  145. * \brief This function clears the internal structure of \p ctx and
  146. * frees any embedded internal structure, but does not free
  147. * \p ctx itself.
  148. *
  149. * If you have called mbedtls_md_setup() on \p ctx, you must
  150. * call mbedtls_md_free() when you are no longer using the
  151. * context.
  152. * Calling this function if you have previously
  153. * called mbedtls_md_init() and nothing else is optional.
  154. * You must not call this function if you have not called
  155. * mbedtls_md_init().
  156. */
  157. void mbedtls_md_free(mbedtls_md_context_t *ctx);
  158. /**
  159. * \brief This function selects the message digest algorithm to use,
  160. * and allocates internal structures.
  161. *
  162. * It should be called after mbedtls_md_init() or
  163. * mbedtls_md_free(). Makes it necessary to call
  164. * mbedtls_md_free() later.
  165. *
  166. * \param ctx The context to set up.
  167. * \param md_info The information structure of the message-digest algorithm
  168. * to use.
  169. * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
  170. * or non-zero: HMAC is used with this context.
  171. *
  172. * \return \c 0 on success.
  173. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  174. * failure.
  175. * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
  176. */
  177. MBEDTLS_CHECK_RETURN_TYPICAL
  178. int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
  179. /**
  180. * \brief This function clones the state of a message-digest
  181. * context.
  182. *
  183. * \note You must call mbedtls_md_setup() on \c dst before calling
  184. * this function.
  185. *
  186. * \note The two contexts must have the same type,
  187. * for example, both are SHA-256.
  188. *
  189. * \warning This function clones the message-digest state, not the
  190. * HMAC state.
  191. *
  192. * \param dst The destination context.
  193. * \param src The context to be cloned.
  194. *
  195. * \return \c 0 on success.
  196. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
  197. * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
  198. * not using the same engine. This can be avoided by moving
  199. * the call to psa_crypto_init() before the first call to
  200. * mbedtls_md_setup().
  201. */
  202. MBEDTLS_CHECK_RETURN_TYPICAL
  203. int mbedtls_md_clone(mbedtls_md_context_t *dst,
  204. const mbedtls_md_context_t *src);
  205. /**
  206. * \brief This function extracts the message-digest size from the
  207. * message-digest information structure.
  208. *
  209. * \param md_info The information structure of the message-digest algorithm
  210. * to use.
  211. *
  212. * \return The size of the message-digest output in Bytes.
  213. */
  214. unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
  215. /**
  216. * \brief This function gives the message-digest size associated to
  217. * message-digest type.
  218. *
  219. * \param md_type The message-digest type.
  220. *
  221. * \return The size of the message-digest output in Bytes,
  222. * or 0 if the message-digest type is not known.
  223. */
  224. static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
  225. {
  226. return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type));
  227. }
  228. /**
  229. * \brief This function extracts the message-digest type from the
  230. * message-digest information structure.
  231. *
  232. * \param md_info The information structure of the message-digest algorithm
  233. * to use.
  234. *
  235. * \return The type of the message digest.
  236. */
  237. mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
  238. /**
  239. * \brief This function starts a message-digest computation.
  240. *
  241. * You must call this function after setting up the context
  242. * with mbedtls_md_setup(), and before passing data with
  243. * mbedtls_md_update().
  244. *
  245. * \param ctx The generic message-digest context.
  246. *
  247. * \return \c 0 on success.
  248. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  249. * failure.
  250. */
  251. MBEDTLS_CHECK_RETURN_TYPICAL
  252. int mbedtls_md_starts(mbedtls_md_context_t *ctx);
  253. /**
  254. * \brief This function feeds an input buffer into an ongoing
  255. * message-digest computation.
  256. *
  257. * You must call mbedtls_md_starts() before calling this
  258. * function. You may call this function multiple times.
  259. * Afterwards, call mbedtls_md_finish().
  260. *
  261. * \param ctx The generic message-digest context.
  262. * \param input The buffer holding the input data.
  263. * \param ilen The length of the input data.
  264. *
  265. * \return \c 0 on success.
  266. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  267. * failure.
  268. */
  269. MBEDTLS_CHECK_RETURN_TYPICAL
  270. int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
  271. /**
  272. * \brief This function finishes the digest operation,
  273. * and writes the result to the output buffer.
  274. *
  275. * Call this function after a call to mbedtls_md_starts(),
  276. * followed by any number of calls to mbedtls_md_update().
  277. * Afterwards, you may either clear the context with
  278. * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
  279. * the context for another digest operation with the same
  280. * algorithm.
  281. *
  282. * \param ctx The generic message-digest context.
  283. * \param output The buffer for the generic message-digest checksum result.
  284. *
  285. * \return \c 0 on success.
  286. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  287. * failure.
  288. */
  289. MBEDTLS_CHECK_RETURN_TYPICAL
  290. int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
  291. /**
  292. * \brief This function calculates the message-digest of a buffer,
  293. * with respect to a configurable message-digest algorithm
  294. * in a single call.
  295. *
  296. * The result is calculated as
  297. * Output = message_digest(input buffer).
  298. *
  299. * \param md_info The information structure of the message-digest algorithm
  300. * to use.
  301. * \param input The buffer holding the data.
  302. * \param ilen The length of the input data.
  303. * \param output The generic message-digest checksum result.
  304. *
  305. * \return \c 0 on success.
  306. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  307. * failure.
  308. */
  309. MBEDTLS_CHECK_RETURN_TYPICAL
  310. int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  311. unsigned char *output);
  312. /**
  313. * \brief This function returns the list of digests supported by the
  314. * generic digest module.
  315. *
  316. * \note The list starts with the strongest available hashes.
  317. *
  318. * \return A statically allocated array of digests. Each element
  319. * in the returned list is an integer belonging to the
  320. * message-digest enumeration #mbedtls_md_type_t.
  321. * The last entry is 0.
  322. */
  323. const int *mbedtls_md_list(void);
  324. /**
  325. * \brief This function returns the message-digest information
  326. * associated with the given digest name.
  327. *
  328. * \param md_name The name of the digest to search for.
  329. *
  330. * \return The message-digest information associated with \p md_name.
  331. * \return NULL if the associated message-digest information is not found.
  332. */
  333. const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
  334. /**
  335. * \brief This function returns the name of the message digest for
  336. * the message-digest information structure given.
  337. *
  338. * \param md_info The information structure of the message-digest algorithm
  339. * to use.
  340. *
  341. * \return The name of the message digest.
  342. */
  343. const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
  344. /**
  345. * \brief This function returns the message-digest information
  346. * from the given context.
  347. *
  348. * \param ctx The context from which to extract the information.
  349. * This must be initialized (or \c NULL).
  350. *
  351. * \return The message-digest information associated with \p ctx.
  352. * \return \c NULL if \p ctx is \c NULL.
  353. */
  354. const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
  355. const mbedtls_md_context_t *ctx);
  356. #if defined(MBEDTLS_FS_IO)
  357. /**
  358. * \brief This function calculates the message-digest checksum
  359. * result of the contents of the provided file.
  360. *
  361. * The result is calculated as
  362. * Output = message_digest(file contents).
  363. *
  364. * \param md_info The information structure of the message-digest algorithm
  365. * to use.
  366. * \param path The input file name.
  367. * \param output The generic message-digest checksum result.
  368. *
  369. * \return \c 0 on success.
  370. * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
  371. * the file pointed by \p path.
  372. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
  373. */
  374. MBEDTLS_CHECK_RETURN_TYPICAL
  375. int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
  376. unsigned char *output);
  377. #endif /* MBEDTLS_FS_IO */
  378. /**
  379. * \brief This function sets the HMAC key and prepares to
  380. * authenticate a new message.
  381. *
  382. * Call this function after mbedtls_md_setup(), to use
  383. * the MD context for an HMAC calculation, then call
  384. * mbedtls_md_hmac_update() to provide the input data, and
  385. * mbedtls_md_hmac_finish() to get the HMAC value.
  386. *
  387. * \param ctx The message digest context containing an embedded HMAC
  388. * context.
  389. * \param key The HMAC secret key.
  390. * \param keylen The length of the HMAC key in Bytes.
  391. *
  392. * \return \c 0 on success.
  393. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  394. * failure.
  395. */
  396. MBEDTLS_CHECK_RETURN_TYPICAL
  397. int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
  398. size_t keylen);
  399. /**
  400. * \brief This function feeds an input buffer into an ongoing HMAC
  401. * computation.
  402. *
  403. * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
  404. * before calling this function.
  405. * You may call this function multiple times to pass the
  406. * input piecewise.
  407. * Afterwards, call mbedtls_md_hmac_finish().
  408. *
  409. * \param ctx The message digest context containing an embedded HMAC
  410. * context.
  411. * \param input The buffer holding the input data.
  412. * \param ilen The length of the input data.
  413. *
  414. * \return \c 0 on success.
  415. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  416. * failure.
  417. */
  418. MBEDTLS_CHECK_RETURN_TYPICAL
  419. int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
  420. size_t ilen);
  421. /**
  422. * \brief This function finishes the HMAC operation, and writes
  423. * the result to the output buffer.
  424. *
  425. * Call this function after mbedtls_md_hmac_starts() and
  426. * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
  427. * you may either call mbedtls_md_free() to clear the context,
  428. * or call mbedtls_md_hmac_reset() to reuse the context with
  429. * the same HMAC key.
  430. *
  431. * \param ctx The message digest context containing an embedded HMAC
  432. * context.
  433. * \param output The generic HMAC checksum result.
  434. *
  435. * \return \c 0 on success.
  436. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  437. * failure.
  438. */
  439. MBEDTLS_CHECK_RETURN_TYPICAL
  440. int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
  441. /**
  442. * \brief This function prepares to authenticate a new message with
  443. * the same key as the previous HMAC operation.
  444. *
  445. * You may call this function after mbedtls_md_hmac_finish().
  446. * Afterwards call mbedtls_md_hmac_update() to pass the new
  447. * input.
  448. *
  449. * \param ctx The message digest context containing an embedded HMAC
  450. * context.
  451. *
  452. * \return \c 0 on success.
  453. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  454. * failure.
  455. */
  456. MBEDTLS_CHECK_RETURN_TYPICAL
  457. int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
  458. /**
  459. * \brief This function calculates the full generic HMAC
  460. * on the input buffer with the provided key.
  461. *
  462. * The function allocates the context, performs the
  463. * calculation, and frees the context.
  464. *
  465. * The HMAC result is calculated as
  466. * output = generic HMAC(hmac key, input buffer).
  467. *
  468. * \param md_info The information structure of the message-digest algorithm
  469. * to use.
  470. * \param key The HMAC secret key.
  471. * \param keylen The length of the HMAC secret key in Bytes.
  472. * \param input The buffer holding the input data.
  473. * \param ilen The length of the input data.
  474. * \param output The generic HMAC result.
  475. *
  476. * \return \c 0 on success.
  477. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  478. * failure.
  479. */
  480. MBEDTLS_CHECK_RETURN_TYPICAL
  481. int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
  482. const unsigned char *input, size_t ilen,
  483. unsigned char *output);
  484. #ifdef __cplusplus
  485. }
  486. #endif
  487. #endif /* MBEDTLS_MD_H */