md.c 12 KB

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