md.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. * \file md.h
  3. *
  4. * \brief This file contains the generic message-digest wrapper.
  5. *
  6. * \author Adriaan de Jong <[email protected]>
  7. */
  8. /*
  9. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. *
  24. * This file is part of Mbed TLS (https://tls.mbed.org)
  25. */
  26. #ifndef MBEDTLS_MD_H
  27. #define MBEDTLS_MD_H
  28. #include <stddef.h>
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34. #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
  35. #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
  36. #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
  37. #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
  38. #define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /**
  43. * \brief Supported message digests.
  44. *
  45. * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
  46. * their use constitutes a security risk. We recommend considering
  47. * stronger message digests instead.
  48. *
  49. */
  50. typedef enum {
  51. MBEDTLS_MD_NONE=0, /**< None. */
  52. MBEDTLS_MD_MD2, /**< The MD2 message digest. */
  53. MBEDTLS_MD_MD4, /**< The MD4 message digest. */
  54. MBEDTLS_MD_MD5, /**< The MD5 message digest. */
  55. MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
  56. MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
  57. MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
  58. MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
  59. MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
  60. MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
  61. } mbedtls_md_type_t;
  62. #if defined(MBEDTLS_SHA512_C)
  63. #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
  64. #else
  65. #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
  66. #endif
  67. /**
  68. * Opaque struct defined in md_internal.h.
  69. */
  70. typedef struct mbedtls_md_info_t mbedtls_md_info_t;
  71. /**
  72. * The generic message-digest context.
  73. */
  74. typedef struct {
  75. /** Information about the associated message digest. */
  76. const mbedtls_md_info_t *md_info;
  77. /** The digest-specific context. */
  78. void *md_ctx;
  79. /** The HMAC part of the context. */
  80. void *hmac_ctx;
  81. } mbedtls_md_context_t;
  82. /**
  83. * \brief This function returns the list of digests supported by the
  84. * generic digest module.
  85. *
  86. * \return A statically allocated array of digests. Each element
  87. * in the returned list is an integer belonging to the
  88. * message-digest enumeration #mbedtls_md_type_t.
  89. * The last entry is 0.
  90. */
  91. const int *mbedtls_md_list( void );
  92. /**
  93. * \brief This function returns the message-digest information
  94. * associated with the given digest name.
  95. *
  96. * \param md_name The name of the digest to search for.
  97. *
  98. * \return The message-digest information associated with \p md_name.
  99. * \return NULL if the associated message-digest information is not found.
  100. */
  101. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
  102. /**
  103. * \brief This function returns the message-digest information
  104. * associated with the given digest type.
  105. *
  106. * \param md_type The type of digest to search for.
  107. *
  108. * \return The message-digest information associated with \p md_type.
  109. * \return NULL if the associated message-digest information is not found.
  110. */
  111. const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
  112. /**
  113. * \brief This function initializes a message-digest context without
  114. * binding it to a particular message-digest algorithm.
  115. *
  116. * This function should always be called first. It prepares the
  117. * context for mbedtls_md_setup() for binding it to a
  118. * message-digest algorithm.
  119. */
  120. void mbedtls_md_init( mbedtls_md_context_t *ctx );
  121. /**
  122. * \brief This function clears the internal structure of \p ctx and
  123. * frees any embedded internal structure, but does not free
  124. * \p ctx itself.
  125. *
  126. * If you have called mbedtls_md_setup() on \p ctx, you must
  127. * call mbedtls_md_free() when you are no longer using the
  128. * context.
  129. * Calling this function if you have previously
  130. * called mbedtls_md_init() and nothing else is optional.
  131. * You must not call this function if you have not called
  132. * mbedtls_md_init().
  133. */
  134. void mbedtls_md_free( mbedtls_md_context_t *ctx );
  135. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  136. #if defined(MBEDTLS_DEPRECATED_WARNING)
  137. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  138. #else
  139. #define MBEDTLS_DEPRECATED
  140. #endif
  141. /**
  142. * \brief This function selects the message digest algorithm to use,
  143. * and allocates internal structures.
  144. *
  145. * It should be called after mbedtls_md_init() or mbedtls_md_free().
  146. * Makes it necessary to call mbedtls_md_free() later.
  147. *
  148. * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
  149. *
  150. * \param ctx The context to set up.
  151. * \param md_info The information structure of the message-digest algorithm
  152. * to use.
  153. *
  154. * \return \c 0 on success.
  155. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  156. * failure.
  157. * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
  158. */
  159. int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
  160. #undef MBEDTLS_DEPRECATED
  161. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  162. /**
  163. * \brief This function selects the message digest algorithm to use,
  164. * and allocates internal structures.
  165. *
  166. * It should be called after mbedtls_md_init() or
  167. * mbedtls_md_free(). Makes it necessary to call
  168. * mbedtls_md_free() later.
  169. *
  170. * \param ctx The context to set up.
  171. * \param md_info The information structure of the message-digest algorithm
  172. * to use.
  173. * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
  174. * or non-zero: HMAC is used with this context.
  175. *
  176. * \return \c 0 on success.
  177. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  178. * failure.
  179. * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
  180. */
  181. int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
  182. /**
  183. * \brief This function clones the state of an message-digest
  184. * context.
  185. *
  186. * \note You must call mbedtls_md_setup() on \c dst before calling
  187. * this function.
  188. *
  189. * \note The two contexts must have the same type,
  190. * for example, both are SHA-256.
  191. *
  192. * \warning This function clones the message-digest state, not the
  193. * HMAC state.
  194. *
  195. * \param dst The destination context.
  196. * \param src The context to be cloned.
  197. *
  198. * \return \c 0 on success.
  199. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
  200. */
  201. int mbedtls_md_clone( mbedtls_md_context_t *dst,
  202. const mbedtls_md_context_t *src );
  203. /**
  204. * \brief This function extracts the message-digest size from the
  205. * message-digest information structure.
  206. *
  207. * \param md_info The information structure of the message-digest algorithm
  208. * to use.
  209. *
  210. * \return The size of the message-digest output in Bytes.
  211. */
  212. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
  213. /**
  214. * \brief This function extracts the message-digest type from the
  215. * message-digest information structure.
  216. *
  217. * \param md_info The information structure of the message-digest algorithm
  218. * to use.
  219. *
  220. * \return The type of the message digest.
  221. */
  222. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
  223. /**
  224. * \brief This function extracts the message-digest name from the
  225. * message-digest information structure.
  226. *
  227. * \param md_info The information structure of the message-digest algorithm
  228. * to use.
  229. *
  230. * \return The name of the message digest.
  231. */
  232. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
  233. /**
  234. * \brief This function starts a message-digest computation.
  235. *
  236. * You must call this function after setting up the context
  237. * with mbedtls_md_setup(), and before passing data with
  238. * mbedtls_md_update().
  239. *
  240. * \param ctx The generic message-digest context.
  241. *
  242. * \return \c 0 on success.
  243. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  244. * failure.
  245. */
  246. int mbedtls_md_starts( mbedtls_md_context_t *ctx );
  247. /**
  248. * \brief This function feeds an input buffer into an ongoing
  249. * message-digest computation.
  250. *
  251. * You must call mbedtls_md_starts() before calling this
  252. * function. You may call this function multiple times.
  253. * Afterwards, call mbedtls_md_finish().
  254. *
  255. * \param ctx The generic message-digest context.
  256. * \param input The buffer holding the input data.
  257. * \param ilen The length of the input data.
  258. *
  259. * \return \c 0 on success.
  260. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  261. * failure.
  262. */
  263. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
  264. /**
  265. * \brief This function finishes the digest operation,
  266. * and writes the result to the output buffer.
  267. *
  268. * Call this function after a call to mbedtls_md_starts(),
  269. * followed by any number of calls to mbedtls_md_update().
  270. * Afterwards, you may either clear the context with
  271. * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
  272. * the context for another digest operation with the same
  273. * algorithm.
  274. *
  275. * \param ctx The generic message-digest context.
  276. * \param output The buffer for the generic message-digest checksum result.
  277. *
  278. * \return \c 0 on success.
  279. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  280. * failure.
  281. */
  282. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
  283. /**
  284. * \brief This function calculates the message-digest of a buffer,
  285. * with respect to a configurable message-digest algorithm
  286. * in a single call.
  287. *
  288. * The result is calculated as
  289. * Output = message_digest(input buffer).
  290. *
  291. * \param md_info The information structure of the message-digest algorithm
  292. * to use.
  293. * \param input The buffer holding the data.
  294. * \param ilen The length of the input data.
  295. * \param output The generic message-digest checksum result.
  296. *
  297. * \return \c 0 on success.
  298. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  299. * failure.
  300. */
  301. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  302. unsigned char *output );
  303. #if defined(MBEDTLS_FS_IO)
  304. /**
  305. * \brief This function calculates the message-digest checksum
  306. * result of the contents of the provided file.
  307. *
  308. * The result is calculated as
  309. * Output = message_digest(file contents).
  310. *
  311. * \param md_info The information structure of the message-digest algorithm
  312. * to use.
  313. * \param path The input file name.
  314. * \param output The generic message-digest checksum result.
  315. *
  316. * \return \c 0 on success.
  317. * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
  318. * the file pointed by \p path.
  319. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
  320. */
  321. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
  322. unsigned char *output );
  323. #endif /* MBEDTLS_FS_IO */
  324. /**
  325. * \brief This function sets the HMAC key and prepares to
  326. * authenticate a new message.
  327. *
  328. * Call this function after mbedtls_md_setup(), to use
  329. * the MD context for an HMAC calculation, then call
  330. * mbedtls_md_hmac_update() to provide the input data, and
  331. * mbedtls_md_hmac_finish() to get the HMAC value.
  332. *
  333. * \param ctx The message digest context containing an embedded HMAC
  334. * context.
  335. * \param key The HMAC secret key.
  336. * \param keylen The length of the HMAC key in Bytes.
  337. *
  338. * \return \c 0 on success.
  339. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  340. * failure.
  341. */
  342. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
  343. size_t keylen );
  344. /**
  345. * \brief This function feeds an input buffer into an ongoing HMAC
  346. * computation.
  347. *
  348. * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
  349. * before calling this function.
  350. * You may call this function multiple times to pass the
  351. * input piecewise.
  352. * Afterwards, call mbedtls_md_hmac_finish().
  353. *
  354. * \param ctx The message digest context containing an embedded HMAC
  355. * context.
  356. * \param input The buffer holding the input data.
  357. * \param ilen The length of the input data.
  358. *
  359. * \return \c 0 on success.
  360. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  361. * failure.
  362. */
  363. int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
  364. size_t ilen );
  365. /**
  366. * \brief This function finishes the HMAC operation, and writes
  367. * the result to the output buffer.
  368. *
  369. * Call this function after mbedtls_md_hmac_starts() and
  370. * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
  371. * you may either call mbedtls_md_free() to clear the context,
  372. * or call mbedtls_md_hmac_reset() to reuse the context with
  373. * the same HMAC key.
  374. *
  375. * \param ctx The message digest context containing an embedded HMAC
  376. * context.
  377. * \param output The generic HMAC checksum result.
  378. *
  379. * \return \c 0 on success.
  380. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  381. * failure.
  382. */
  383. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
  384. /**
  385. * \brief This function prepares to authenticate a new message with
  386. * the same key as the previous HMAC operation.
  387. *
  388. * You may call this function after mbedtls_md_hmac_finish().
  389. * Afterwards call mbedtls_md_hmac_update() to pass the new
  390. * input.
  391. *
  392. * \param ctx The message digest context containing an embedded HMAC
  393. * context.
  394. *
  395. * \return \c 0 on success.
  396. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  397. * failure.
  398. */
  399. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
  400. /**
  401. * \brief This function calculates the full generic HMAC
  402. * on the input buffer with the provided key.
  403. *
  404. * The function allocates the context, performs the
  405. * calculation, and frees the context.
  406. *
  407. * The HMAC result is calculated as
  408. * output = generic HMAC(hmac key, input buffer).
  409. *
  410. * \param md_info The information structure of the message-digest algorithm
  411. * to use.
  412. * \param key The HMAC secret key.
  413. * \param keylen The length of the HMAC secret key in Bytes.
  414. * \param input The buffer holding the input data.
  415. * \param ilen The length of the input data.
  416. * \param output The generic HMAC result.
  417. *
  418. * \return \c 0 on success.
  419. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  420. * failure.
  421. */
  422. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
  423. const unsigned char *input, size_t ilen,
  424. unsigned char *output );
  425. /* Internal use */
  426. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
  427. #ifdef __cplusplus
  428. }
  429. #endif
  430. #endif /* MBEDTLS_MD_H */