md.h 12 KB

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