md.h 19 KB

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