md.c 11 KB

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