md.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /**
  2. * \file mbedtls_md.c
  3. *
  4. * \brief Generic message digest wrapper for mbed TLS
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "mbedtls/config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #if defined(MBEDTLS_MD_C)
  33. #include "mbedtls/md.h"
  34. #include "mbedtls/md_internal.h"
  35. #if defined(MBEDTLS_PLATFORM_C)
  36. #include "mbedtls/platform.h"
  37. #else
  38. #include <stdlib.h>
  39. #define mbedtls_calloc calloc
  40. #define mbedtls_free free
  41. #endif
  42. #include <string.h>
  43. #if defined(MBEDTLS_FS_IO)
  44. #include <stdio.h>
  45. #endif
  46. /* Implementation that should never be optimized out by the compiler */
  47. static void mbedtls_zeroize( void *v, size_t n ) {
  48. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  49. }
  50. /*
  51. * Reminder: update profiles in x509_crt.c when adding a new hash!
  52. */
  53. static const int supported_digests[] = {
  54. #if defined(MBEDTLS_SHA512_C)
  55. MBEDTLS_MD_SHA512,
  56. MBEDTLS_MD_SHA384,
  57. #endif
  58. #if defined(MBEDTLS_SHA256_C)
  59. MBEDTLS_MD_SHA256,
  60. MBEDTLS_MD_SHA224,
  61. #endif
  62. #if defined(MBEDTLS_SHA1_C)
  63. MBEDTLS_MD_SHA1,
  64. #endif
  65. #if defined(MBEDTLS_RIPEMD160_C)
  66. MBEDTLS_MD_RIPEMD160,
  67. #endif
  68. #if defined(MBEDTLS_MD5_C)
  69. MBEDTLS_MD_MD5,
  70. #endif
  71. #if defined(MBEDTLS_MD4_C)
  72. MBEDTLS_MD_MD4,
  73. #endif
  74. #if defined(MBEDTLS_MD2_C)
  75. MBEDTLS_MD_MD2,
  76. #endif
  77. MBEDTLS_MD_NONE
  78. };
  79. const int *mbedtls_md_list( void )
  80. {
  81. return( supported_digests );
  82. }
  83. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
  84. {
  85. if( NULL == md_name )
  86. return( NULL );
  87. /* Get the appropriate digest information */
  88. #if defined(MBEDTLS_MD2_C)
  89. if( !strcmp( "MD2", md_name ) )
  90. return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
  91. #endif
  92. #if defined(MBEDTLS_MD4_C)
  93. if( !strcmp( "MD4", md_name ) )
  94. return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
  95. #endif
  96. #if defined(MBEDTLS_MD5_C)
  97. if( !strcmp( "MD5", md_name ) )
  98. return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
  99. #endif
  100. #if defined(MBEDTLS_RIPEMD160_C)
  101. if( !strcmp( "RIPEMD160", md_name ) )
  102. return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
  103. #endif
  104. #if defined(MBEDTLS_SHA1_C)
  105. if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
  106. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  107. #endif
  108. #if defined(MBEDTLS_SHA256_C)
  109. if( !strcmp( "SHA224", md_name ) )
  110. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
  111. if( !strcmp( "SHA256", md_name ) )
  112. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
  113. #endif
  114. #if defined(MBEDTLS_SHA512_C)
  115. if( !strcmp( "SHA384", md_name ) )
  116. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
  117. if( !strcmp( "SHA512", md_name ) )
  118. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
  119. #endif
  120. return( NULL );
  121. }
  122. const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
  123. {
  124. switch( md_type )
  125. {
  126. #if defined(MBEDTLS_MD2_C)
  127. case MBEDTLS_MD_MD2:
  128. return( &mbedtls_md2_info );
  129. #endif
  130. #if defined(MBEDTLS_MD4_C)
  131. case MBEDTLS_MD_MD4:
  132. return( &mbedtls_md4_info );
  133. #endif
  134. #if defined(MBEDTLS_MD5_C)
  135. case MBEDTLS_MD_MD5:
  136. return( &mbedtls_md5_info );
  137. #endif
  138. #if defined(MBEDTLS_RIPEMD160_C)
  139. case MBEDTLS_MD_RIPEMD160:
  140. return( &mbedtls_ripemd160_info );
  141. #endif
  142. #if defined(MBEDTLS_SHA1_C)
  143. case MBEDTLS_MD_SHA1:
  144. return( &mbedtls_sha1_info );
  145. #endif
  146. #if defined(MBEDTLS_SHA256_C)
  147. case MBEDTLS_MD_SHA224:
  148. return( &mbedtls_sha224_info );
  149. case MBEDTLS_MD_SHA256:
  150. return( &mbedtls_sha256_info );
  151. #endif
  152. #if defined(MBEDTLS_SHA512_C)
  153. case MBEDTLS_MD_SHA384:
  154. return( &mbedtls_sha384_info );
  155. case MBEDTLS_MD_SHA512:
  156. return( &mbedtls_sha512_info );
  157. #endif
  158. default:
  159. return( NULL );
  160. }
  161. }
  162. void mbedtls_md_init( mbedtls_md_context_t *ctx )
  163. {
  164. memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
  165. }
  166. void mbedtls_md_free( mbedtls_md_context_t *ctx )
  167. {
  168. if( ctx == NULL || ctx->md_info == NULL )
  169. return;
  170. if( ctx->md_ctx != NULL )
  171. ctx->md_info->ctx_free_func( ctx->md_ctx );
  172. if( ctx->hmac_ctx != NULL )
  173. {
  174. mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
  175. mbedtls_free( ctx->hmac_ctx );
  176. }
  177. mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
  178. }
  179. int mbedtls_md_clone( mbedtls_md_context_t *dst,
  180. const mbedtls_md_context_t *src )
  181. {
  182. if( dst == NULL || dst->md_info == NULL ||
  183. src == NULL || src->md_info == NULL ||
  184. dst->md_info != src->md_info )
  185. {
  186. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  187. }
  188. dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
  189. return( 0 );
  190. }
  191. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  192. int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
  193. {
  194. return mbedtls_md_setup( ctx, md_info, 1 );
  195. }
  196. #endif
  197. int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
  198. {
  199. if( md_info == NULL || ctx == NULL )
  200. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  201. if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
  202. return( MBEDTLS_ERR_MD_ALLOC_FAILED );
  203. if( hmac != 0 )
  204. {
  205. ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
  206. if( ctx->hmac_ctx == NULL )
  207. {
  208. md_info->ctx_free_func( ctx->md_ctx );
  209. return( MBEDTLS_ERR_MD_ALLOC_FAILED );
  210. }
  211. }
  212. ctx->md_info = md_info;
  213. return( 0 );
  214. }
  215. int mbedtls_md_starts( mbedtls_md_context_t *ctx )
  216. {
  217. if( ctx == NULL || ctx->md_info == NULL )
  218. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  219. ctx->md_info->starts_func( ctx->md_ctx );
  220. return( 0 );
  221. }
  222. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  223. {
  224. if( ctx == NULL || ctx->md_info == NULL )
  225. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  226. ctx->md_info->update_func( ctx->md_ctx, input, ilen );
  227. return( 0 );
  228. }
  229. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  230. {
  231. if( ctx == NULL || ctx->md_info == NULL )
  232. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  233. ctx->md_info->finish_func( ctx->md_ctx, output );
  234. return( 0 );
  235. }
  236. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  237. unsigned char *output )
  238. {
  239. if( md_info == NULL )
  240. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  241. md_info->digest_func( input, ilen, output );
  242. return( 0 );
  243. }
  244. #if defined(MBEDTLS_FS_IO)
  245. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
  246. {
  247. int ret;
  248. FILE *f;
  249. size_t n;
  250. mbedtls_md_context_t ctx;
  251. unsigned char buf[1024];
  252. if( md_info == NULL )
  253. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  254. if( ( f = fopen( path, "rb" ) ) == NULL )
  255. return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
  256. mbedtls_md_init( &ctx );
  257. if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
  258. goto cleanup;
  259. md_info->starts_func( ctx.md_ctx );
  260. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  261. md_info->update_func( ctx.md_ctx, buf, n );
  262. if( ferror( f ) != 0 )
  263. {
  264. ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
  265. goto cleanup;
  266. }
  267. md_info->finish_func( ctx.md_ctx, output );
  268. cleanup:
  269. fclose( f );
  270. mbedtls_md_free( &ctx );
  271. return( ret );
  272. }
  273. #endif /* MBEDTLS_FS_IO */
  274. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
  275. {
  276. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  277. unsigned char *ipad, *opad;
  278. size_t i;
  279. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  280. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  281. if( keylen > (size_t) ctx->md_info->block_size )
  282. {
  283. ctx->md_info->starts_func( ctx->md_ctx );
  284. ctx->md_info->update_func( ctx->md_ctx, key, keylen );
  285. ctx->md_info->finish_func( ctx->md_ctx, sum );
  286. keylen = ctx->md_info->size;
  287. key = sum;
  288. }
  289. ipad = (unsigned char *) ctx->hmac_ctx;
  290. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  291. memset( ipad, 0x36, ctx->md_info->block_size );
  292. memset( opad, 0x5C, ctx->md_info->block_size );
  293. for( i = 0; i < keylen; i++ )
  294. {
  295. ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
  296. opad[i] = (unsigned char)( opad[i] ^ key[i] );
  297. }
  298. mbedtls_zeroize( sum, sizeof( sum ) );
  299. ctx->md_info->starts_func( ctx->md_ctx );
  300. ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
  301. return( 0 );
  302. }
  303. int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  304. {
  305. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  306. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  307. ctx->md_info->update_func( ctx->md_ctx, input, ilen );
  308. return( 0 );
  309. }
  310. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  311. {
  312. unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
  313. unsigned char *opad;
  314. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  315. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  316. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  317. ctx->md_info->finish_func( ctx->md_ctx, tmp );
  318. ctx->md_info->starts_func( ctx->md_ctx );
  319. ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size );
  320. ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size );
  321. ctx->md_info->finish_func( ctx->md_ctx, output );
  322. return( 0 );
  323. }
  324. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
  325. {
  326. unsigned char *ipad;
  327. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  328. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  329. ipad = (unsigned char *) ctx->hmac_ctx;
  330. ctx->md_info->starts_func( ctx->md_ctx );
  331. ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
  332. return( 0 );
  333. }
  334. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
  335. const unsigned char *input, size_t ilen,
  336. unsigned char *output )
  337. {
  338. mbedtls_md_context_t ctx;
  339. int ret;
  340. if( md_info == NULL )
  341. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  342. mbedtls_md_init( &ctx );
  343. if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
  344. return( ret );
  345. mbedtls_md_hmac_starts( &ctx, key, keylen );
  346. mbedtls_md_hmac_update( &ctx, input, ilen );
  347. mbedtls_md_hmac_finish( &ctx, output );
  348. mbedtls_md_free( &ctx );
  349. return( 0 );
  350. }
  351. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
  352. {
  353. if( ctx == NULL || ctx->md_info == NULL )
  354. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  355. ctx->md_info->process_func( ctx->md_ctx, data );
  356. return( 0 );
  357. }
  358. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
  359. {
  360. if( md_info == NULL )
  361. return( 0 );
  362. return md_info->size;
  363. }
  364. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
  365. {
  366. if( md_info == NULL )
  367. return( MBEDTLS_MD_NONE );
  368. return md_info->type;
  369. }
  370. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
  371. {
  372. if( md_info == NULL )
  373. return( NULL );
  374. return md_info->name;
  375. }
  376. #endif /* MBEDTLS_MD_C */