md.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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-2010, Brainspark B.V.
  9. *
  10. * This file is part of PolarSSL (http://www.polarssl.org)
  11. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  12. *
  13. * All rights reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  28. */
  29. #ifndef POLARSSL_MD_H
  30. #define POLARSSL_MD_H
  31. #include <string.h>
  32. #if defined(_MSC_VER) && !defined(inline)
  33. #define inline _inline
  34. #else
  35. #if defined(__ARMCC_VERSION) && !defined(inline)
  36. #define inline __inline
  37. #endif /* __ARMCC_VERSION */
  38. #endif /*_MSC_VER */
  39. #define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
  40. #define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
  41. #define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
  42. #define POLARSSL_ERR_MD_FILE_OPEN_FAILED -0x5200 /**< Opening of file failed. */
  43. #define POLARSSL_ERR_MD_FILE_READ_FAILED -0x5280 /**< Failure when reading from file. */
  44. typedef enum {
  45. POLARSSL_MD_NONE=0,
  46. POLARSSL_MD_MD2,
  47. POLARSSL_MD_MD4,
  48. POLARSSL_MD_MD5,
  49. POLARSSL_MD_SHA1,
  50. POLARSSL_MD_SHA224,
  51. POLARSSL_MD_SHA256,
  52. POLARSSL_MD_SHA384,
  53. POLARSSL_MD_SHA512,
  54. } md_type_t;
  55. #define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
  56. /**
  57. * Message digest information. Allows message digest functions to be called
  58. * in a generic way.
  59. */
  60. typedef struct {
  61. /** Digest identifier */
  62. md_type_t type;
  63. /** Name of the message digest */
  64. const char * name;
  65. /** Output length of the digest function */
  66. int size;
  67. /** Digest initialisation function */
  68. void (*starts_func)( void *ctx );
  69. /** Digest update function */
  70. void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
  71. /** Digest finalisation function */
  72. void (*finish_func)( void *ctx, unsigned char *output );
  73. /** Generic digest function */
  74. void (*digest_func)( const unsigned char *input, size_t ilen,
  75. unsigned char *output );
  76. /** Generic file digest function */
  77. int (*file_func)( const char *path, unsigned char *output );
  78. /** HMAC Initialisation function */
  79. void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
  80. /** HMAC update function */
  81. void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
  82. /** HMAC finalisation function */
  83. void (*hmac_finish_func)( void *ctx, unsigned char *output);
  84. /** HMAC context reset function */
  85. void (*hmac_reset_func)( void *ctx );
  86. /** Generic HMAC function */
  87. void (*hmac_func)( const unsigned char *key, size_t keylen,
  88. const unsigned char *input, size_t ilen,
  89. unsigned char *output );
  90. /** Allocate a new context */
  91. void * (*ctx_alloc_func)( void );
  92. /** Free the given context */
  93. void (*ctx_free_func)( void *ctx );
  94. } md_info_t;
  95. /**
  96. * Generic message digest context.
  97. */
  98. typedef struct {
  99. /** Information about the associated message digest */
  100. const md_info_t *md_info;
  101. /** Digest-specific context */
  102. void *md_ctx;
  103. } md_context_t;
  104. #define MD_CONTEXT_T_INIT { \
  105. NULL, /* md_info */ \
  106. NULL, /* md_ctx */ \
  107. }
  108. #ifdef __cplusplus
  109. extern "C" {
  110. #endif
  111. /**
  112. * \brief Returns the list of digests supported by the generic digest module.
  113. *
  114. * \return a statically allocated array of digests, the last entry
  115. * is 0.
  116. */
  117. const int *md_list( void );
  118. /**
  119. * \brief Returns the message digest information associated with the
  120. * given digest name.
  121. *
  122. * \param md_name Name of the digest to search for.
  123. *
  124. * \return The message digest information associated with md_name or
  125. * NULL if not found.
  126. */
  127. const md_info_t *md_info_from_string( const char *md_name );
  128. /**
  129. * \brief Returns the message digest information associated with the
  130. * given digest type.
  131. *
  132. * \param md_type type of digest to search for.
  133. *
  134. * \return The message digest information associated with md_type or
  135. * NULL if not found.
  136. */
  137. const md_info_t *md_info_from_type( md_type_t md_type );
  138. /**
  139. * \brief Initialises and fills the message digest context structure with
  140. * the appropriate values.
  141. *
  142. * \param ctx context to initialise. May not be NULL. The
  143. * digest-specific context (ctx->md_ctx) must be NULL. It will
  144. * be allocated, and must be freed using md_free_ctx() later.
  145. * \param md_info message digest to use.
  146. *
  147. * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
  148. * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
  149. * allocation of the cipher-specific context failed.
  150. */
  151. int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
  152. /**
  153. * \brief Free the message-specific context of ctx. Freeing ctx itself
  154. * remains the responsibility of the caller.
  155. *
  156. * \param ctx Free the message-specific context
  157. *
  158. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  159. * verification fails.
  160. */
  161. int md_free_ctx( md_context_t *ctx );
  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.
  168. */
  169. static inline unsigned char md_get_size( const md_info_t *md_info )
  170. {
  171. return md_info->size;
  172. }
  173. /**
  174. * \brief Returns the type of the message digest output.
  175. *
  176. * \param md_info message digest info
  177. *
  178. * \return type of the message digest output.
  179. */
  180. static inline md_type_t md_get_type( const md_info_t *md_info )
  181. {
  182. return md_info->type;
  183. }
  184. /**
  185. * \brief Returns the name of the message digest output.
  186. *
  187. * \param md_info message digest info
  188. *
  189. * \return name of the message digest output.
  190. */
  191. static inline const char *md_get_name( const md_info_t *md_info )
  192. {
  193. return md_info->name;
  194. }
  195. /**
  196. * \brief Set-up the given context for a new message digest
  197. *
  198. * \param ctx generic message digest context.
  199. *
  200. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  201. * verification fails.
  202. */
  203. int md_starts( md_context_t *ctx );
  204. /**
  205. * \brief Generic message digest process buffer
  206. *
  207. * \param ctx Generic message digest context
  208. * \param input buffer holding the datal
  209. * \param ilen length of the input data
  210. *
  211. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  212. * verification fails.
  213. */
  214. int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
  215. /**
  216. * \brief Generic message digest final digest
  217. *
  218. * \param ctx Generic message digest context
  219. * \param output Generic message digest checksum result
  220. *
  221. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  222. * verification fails.
  223. */
  224. int md_finish( md_context_t *ctx, unsigned char *output );
  225. /**
  226. * \brief Output = message_digest( input buffer )
  227. *
  228. * \param md_info message digest info
  229. * \param input buffer holding the data
  230. * \param ilen length of the input data
  231. * \param output Generic message digest checksum result
  232. *
  233. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  234. * verification fails.
  235. */
  236. int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
  237. unsigned char *output );
  238. /**
  239. * \brief Output = message_digest( file contents )
  240. *
  241. * \param md_info message digest info
  242. * \param path input file name
  243. * \param output generic message digest checksum result
  244. *
  245. * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
  246. * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
  247. * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
  248. */
  249. int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
  250. /**
  251. * \brief Generic HMAC context setup
  252. *
  253. * \param ctx HMAC context to be initialized
  254. * \param key HMAC secret key
  255. * \param keylen length of the HMAC key
  256. *
  257. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  258. * verification fails.
  259. */
  260. int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
  261. /**
  262. * \brief Generic HMAC process buffer
  263. *
  264. * \param ctx HMAC context
  265. * \param input buffer holding the data
  266. * \param ilen length of the input data
  267. *
  268. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  269. * verification fails.
  270. */
  271. int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
  272. /**
  273. * \brief Generic HMAC final digest
  274. *
  275. * \param ctx HMAC context
  276. * \param output Generic HMAC checksum result
  277. *
  278. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  279. * verification fails.
  280. */
  281. int md_hmac_finish( md_context_t *ctx, unsigned char *output);
  282. /**
  283. * \brief Generic HMAC context reset
  284. *
  285. * \param ctx HMAC context to be reset
  286. *
  287. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  288. * verification fails.
  289. */
  290. int md_hmac_reset( md_context_t *ctx );
  291. /**
  292. * \brief Output = Generic_HMAC( hmac key, input buffer )
  293. *
  294. * \param md_info message digest info
  295. * \param key HMAC secret key
  296. * \param keylen length of the HMAC key
  297. * \param input buffer holding the data
  298. * \param ilen length of the input data
  299. * \param output Generic HMAC-result
  300. *
  301. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
  302. * verification fails.
  303. */
  304. int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
  305. const unsigned char *input, size_t ilen,
  306. unsigned char *output );
  307. #ifdef __cplusplus
  308. }
  309. #endif
  310. #endif /* POLARSSL_MD_H */