md.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * \file md.h
  3. *
  4. * \brief Generic message digest wrapper
  5. *
  6. * \author Adriaan de Jong <dejong@fox-it.com>
  7. *
  8. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  9. * SPDX-License-Identifier: GPL-2.0
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * This file is part of mbed TLS (https://tls.mbed.org)
  26. */
  27. #ifndef MBEDTLS_MD_H
  28. #define MBEDTLS_MD_H
  29. #include <stddef.h>
  30. #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
  31. #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
  32. #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
  33. #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. typedef enum {
  38. MBEDTLS_MD_NONE=0,
  39. MBEDTLS_MD_MD2,
  40. MBEDTLS_MD_MD4,
  41. MBEDTLS_MD_MD5,
  42. MBEDTLS_MD_SHA1,
  43. MBEDTLS_MD_SHA224,
  44. MBEDTLS_MD_SHA256,
  45. MBEDTLS_MD_SHA384,
  46. MBEDTLS_MD_SHA512,
  47. MBEDTLS_MD_RIPEMD160,
  48. } mbedtls_md_type_t;
  49. #if defined(MBEDTLS_SHA512_C)
  50. #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
  51. #else
  52. #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
  53. #endif
  54. /**
  55. * Opaque struct defined in md_internal.h
  56. */
  57. typedef struct mbedtls_md_info_t mbedtls_md_info_t;
  58. /**
  59. * Generic message digest context.
  60. */
  61. typedef struct {
  62. /** Information about the associated message digest */
  63. const mbedtls_md_info_t *md_info;
  64. /** Digest-specific context */
  65. void *md_ctx;
  66. /** HMAC part of the context */
  67. void *hmac_ctx;
  68. } mbedtls_md_context_t;
  69. /**
  70. * \brief Returns the list of digests supported by the generic digest module.
  71. *
  72. * \return a statically allocated array of digests, the last entry
  73. * is 0.
  74. */
  75. const int *mbedtls_md_list( void );
  76. /**
  77. * \brief Returns the message digest information associated with the
  78. * given digest name.
  79. *
  80. * \param md_name Name of the digest to search for.
  81. *
  82. * \return The message digest information associated with md_name or
  83. * NULL if not found.
  84. */
  85. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
  86. /**
  87. * \brief Returns the message digest information associated with the
  88. * given digest type.
  89. *
  90. * \param md_type type of digest to search for.
  91. *
  92. * \return The message digest information associated with md_type or
  93. * NULL if not found.
  94. */
  95. const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
  96. /**
  97. * \brief Initialize a md_context (as NONE)
  98. * This should always be called first.
  99. * Prepares the context for mbedtls_md_setup() or mbedtls_md_free().
  100. */
  101. void mbedtls_md_init( mbedtls_md_context_t *ctx );
  102. /**
  103. * \brief Free and clear the internal structures of ctx.
  104. * Can be called at any time after mbedtls_md_init().
  105. * Mandatory once mbedtls_md_setup() has been called.
  106. */
  107. void mbedtls_md_free( mbedtls_md_context_t *ctx );
  108. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  109. #if defined(MBEDTLS_DEPRECATED_WARNING)
  110. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  111. #else
  112. #define MBEDTLS_DEPRECATED
  113. #endif
  114. /**
  115. * \brief Select MD to use and allocate internal structures.
  116. * Should be called after mbedtls_md_init() or mbedtls_md_free().
  117. * Makes it necessary to call mbedtls_md_free() later.
  118. *
  119. * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
  120. *
  121. * \param ctx Context to set up.
  122. * \param md_info Message digest to use.
  123. *
  124. * \returns \c 0 on success,
  125. * \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure,
  126. * \c MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure.
  127. */
  128. int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
  129. #undef MBEDTLS_DEPRECATED
  130. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  131. /**
  132. * \brief Select MD to use and allocate internal structures.
  133. * Should be called after mbedtls_md_init() or mbedtls_md_free().
  134. * Makes it necessary to call mbedtls_md_free() later.
  135. *
  136. * \param ctx Context to set up.
  137. * \param md_info Message digest to use.
  138. * \param hmac 0 to save some memory if HMAC will not be used,
  139. * non-zero is HMAC is going to be used with this context.
  140. *
  141. * \returns \c 0 on success,
  142. * \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure,
  143. * \c MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure.
  144. */
  145. int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
  146. /**
  147. * \brief Clone the state of an MD context
  148. *
  149. * \note The two contexts must have been setup to the same type
  150. * (cloning from SHA-256 to SHA-512 make no sense).
  151. *
  152. * \warning Only clones the MD state, not the HMAC state! (for now)
  153. *
  154. * \param dst The destination context
  155. * \param src The context to be cloned
  156. *
  157. * \return \c 0 on success,
  158. * \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure.
  159. */
  160. int mbedtls_md_clone( mbedtls_md_context_t *dst,
  161. const mbedtls_md_context_t *src );
  162. /**
  163. * \brief Returns the size of the message digest output.
  164. *
  165. * \param md_info message digest info
  166. *
  167. * \return size of the message digest output in bytes.
  168. */
  169. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
  170. /**
  171. * \brief Returns the type of the message digest output.
  172. *
  173. * \param md_info message digest info
  174. *
  175. * \return type of the message digest output.
  176. */
  177. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
  178. /**
  179. * \brief Returns the name of the message digest output.
  180. *
  181. * \param md_info message digest info
  182. *
  183. * \return name of the message digest output.
  184. */
  185. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
  186. /**
  187. * \brief Prepare the context to digest a new message.
  188. * Generally called after mbedtls_md_setup() or mbedtls_md_finish().
  189. * Followed by mbedtls_md_update().
  190. *
  191. * \param ctx generic message digest context.
  192. *
  193. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  194. * verification fails.
  195. */
  196. int mbedtls_md_starts( mbedtls_md_context_t *ctx );
  197. /**
  198. * \brief Generic message digest process buffer
  199. * Called between mbedtls_md_starts() and mbedtls_md_finish().
  200. * May be called repeatedly.
  201. *
  202. * \param ctx Generic message digest context
  203. * \param input buffer holding the datal
  204. * \param ilen length of the input data
  205. *
  206. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  207. * verification fails.
  208. */
  209. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
  210. /**
  211. * \brief Generic message digest final digest
  212. * Called after mbedtls_md_update().
  213. * Usually followed by mbedtls_md_free() or mbedtls_md_starts().
  214. *
  215. * \param ctx Generic message digest context
  216. * \param output Generic message digest checksum result
  217. *
  218. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  219. * verification fails.
  220. */
  221. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
  222. /**
  223. * \brief Output = message_digest( input buffer )
  224. *
  225. * \param md_info message digest info
  226. * \param input buffer holding the data
  227. * \param ilen length of the input data
  228. * \param output Generic message digest checksum result
  229. *
  230. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  231. * verification fails.
  232. */
  233. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  234. unsigned char *output );
  235. #if defined(MBEDTLS_FS_IO)
  236. /**
  237. * \brief Output = message_digest( file contents )
  238. *
  239. * \param md_info message digest info
  240. * \param path input file name
  241. * \param output generic message digest checksum result
  242. *
  243. * \return 0 if successful,
  244. * MBEDTLS_ERR_MD_FILE_IO_ERROR if file input failed,
  245. * MBEDTLS_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
  246. */
  247. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
  248. unsigned char *output );
  249. #endif /* MBEDTLS_FS_IO */
  250. /**
  251. * \brief Set HMAC key and prepare to authenticate a new message.
  252. * Usually called after mbedtls_md_setup() or mbedtls_md_hmac_finish().
  253. *
  254. * \param ctx HMAC context
  255. * \param key HMAC secret key
  256. * \param keylen length of the HMAC key in bytes
  257. *
  258. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  259. * verification fails.
  260. */
  261. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
  262. size_t keylen );
  263. /**
  264. * \brief Generic HMAC process buffer.
  265. * Called between mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
  266. * and mbedtls_md_hmac_finish().
  267. * May be called repeatedly.
  268. *
  269. * \param ctx HMAC context
  270. * \param input buffer holding the data
  271. * \param ilen length of the input data
  272. *
  273. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  274. * verification fails.
  275. */
  276. int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
  277. size_t ilen );
  278. /**
  279. * \brief Output HMAC.
  280. * Called after mbedtls_md_hmac_update().
  281. * Usually followed by mbedtls_md_hmac_reset(),
  282. * mbedtls_md_hmac_starts(), or mbedtls_md_free().
  283. *
  284. * \param ctx HMAC context
  285. * \param output Generic HMAC checksum result
  286. *
  287. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  288. * verification fails.
  289. */
  290. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
  291. /**
  292. * \brief Prepare to authenticate a new message with the same key.
  293. * Called after mbedtls_md_hmac_finish() and before
  294. * mbedtls_md_hmac_update().
  295. *
  296. * \param ctx HMAC context to be reset
  297. *
  298. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  299. * verification fails.
  300. */
  301. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
  302. /**
  303. * \brief Output = Generic_HMAC( hmac key, input buffer )
  304. *
  305. * \param md_info message digest info
  306. * \param key HMAC secret key
  307. * \param keylen length of the HMAC key in bytes
  308. * \param input buffer holding the data
  309. * \param ilen length of the input data
  310. * \param output Generic HMAC-result
  311. *
  312. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  313. * verification fails.
  314. */
  315. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
  316. const unsigned char *input, size_t ilen,
  317. unsigned char *output );
  318. /* Internal use */
  319. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
  320. #ifdef __cplusplus
  321. }
  322. #endif
  323. #endif /* MBEDTLS_MD_H */