md.c 12 KB

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