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