md.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. #include <stdio.h>
  43. #endif
  44. /* Implementation that should never be optimized out by the compiler */
  45. static void mbedtls_zeroize( void *v, size_t n ) {
  46. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  47. }
  48. /*
  49. * Reminder: update profiles in x509_crt.c when adding a new hash!
  50. */
  51. static const int supported_digests[] = {
  52. #if defined(MBEDTLS_SHA512_C)
  53. MBEDTLS_MD_SHA512,
  54. MBEDTLS_MD_SHA384,
  55. #endif
  56. #if defined(MBEDTLS_SHA256_C)
  57. MBEDTLS_MD_SHA256,
  58. MBEDTLS_MD_SHA224,
  59. #endif
  60. #if defined(MBEDTLS_SHA1_C)
  61. MBEDTLS_MD_SHA1,
  62. #endif
  63. #if defined(MBEDTLS_RIPEMD160_C)
  64. MBEDTLS_MD_RIPEMD160,
  65. #endif
  66. #if defined(MBEDTLS_MD5_C)
  67. MBEDTLS_MD_MD5,
  68. #endif
  69. #if defined(MBEDTLS_MD4_C)
  70. MBEDTLS_MD_MD4,
  71. #endif
  72. #if defined(MBEDTLS_MD2_C)
  73. MBEDTLS_MD_MD2,
  74. #endif
  75. MBEDTLS_MD_NONE
  76. };
  77. const int *mbedtls_md_list( void )
  78. {
  79. return( supported_digests );
  80. }
  81. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
  82. {
  83. if( NULL == md_name )
  84. return( NULL );
  85. /* Get the appropriate digest information */
  86. #if defined(MBEDTLS_MD2_C)
  87. if( !strcmp( "MD2", md_name ) )
  88. return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
  89. #endif
  90. #if defined(MBEDTLS_MD4_C)
  91. if( !strcmp( "MD4", md_name ) )
  92. return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
  93. #endif
  94. #if defined(MBEDTLS_MD5_C)
  95. if( !strcmp( "MD5", md_name ) )
  96. return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
  97. #endif
  98. #if defined(MBEDTLS_RIPEMD160_C)
  99. if( !strcmp( "RIPEMD160", md_name ) )
  100. return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
  101. #endif
  102. #if defined(MBEDTLS_SHA1_C)
  103. if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
  104. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  105. #endif
  106. #if defined(MBEDTLS_SHA256_C)
  107. if( !strcmp( "SHA224", md_name ) )
  108. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
  109. if( !strcmp( "SHA256", md_name ) )
  110. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
  111. #endif
  112. #if defined(MBEDTLS_SHA512_C)
  113. if( !strcmp( "SHA384", md_name ) )
  114. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
  115. if( !strcmp( "SHA512", md_name ) )
  116. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
  117. #endif
  118. return( NULL );
  119. }
  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. return( ctx->md_info->starts_func( ctx->md_ctx ) );
  218. }
  219. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  220. {
  221. if( ctx == NULL || ctx->md_info == NULL )
  222. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  223. return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
  224. }
  225. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  226. {
  227. if( ctx == NULL || ctx->md_info == NULL )
  228. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  229. return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
  230. }
  231. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  232. unsigned char *output )
  233. {
  234. if( md_info == NULL )
  235. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  236. return( md_info->digest_func( input, ilen, output ) );
  237. }
  238. #if defined(MBEDTLS_FS_IO)
  239. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
  240. {
  241. int ret;
  242. FILE *f;
  243. size_t n;
  244. mbedtls_md_context_t ctx;
  245. unsigned char buf[1024];
  246. if( md_info == NULL )
  247. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  248. if( ( f = fopen( path, "rb" ) ) == NULL )
  249. return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
  250. mbedtls_md_init( &ctx );
  251. if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
  252. goto cleanup;
  253. if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
  254. goto cleanup;
  255. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  256. if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
  257. goto cleanup;
  258. if( ferror( f ) != 0 )
  259. ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
  260. else
  261. ret = md_info->finish_func( ctx.md_ctx, output );
  262. cleanup:
  263. mbedtls_zeroize( buf, sizeof( buf ) );
  264. fclose( f );
  265. mbedtls_md_free( &ctx );
  266. return( ret );
  267. }
  268. #endif /* MBEDTLS_FS_IO */
  269. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
  270. {
  271. int ret;
  272. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  273. unsigned char *ipad, *opad;
  274. size_t i;
  275. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  276. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  277. if( keylen > (size_t) ctx->md_info->block_size )
  278. {
  279. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  280. goto cleanup;
  281. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
  282. goto cleanup;
  283. if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
  284. goto cleanup;
  285. keylen = ctx->md_info->size;
  286. key = sum;
  287. }
  288. ipad = (unsigned char *) ctx->hmac_ctx;
  289. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  290. memset( ipad, 0x36, ctx->md_info->block_size );
  291. memset( opad, 0x5C, ctx->md_info->block_size );
  292. for( i = 0; i < keylen; i++ )
  293. {
  294. ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
  295. opad[i] = (unsigned char)( opad[i] ^ key[i] );
  296. }
  297. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  298. goto cleanup;
  299. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
  300. ctx->md_info->block_size ) ) != 0 )
  301. goto cleanup;
  302. cleanup:
  303. mbedtls_zeroize( sum, sizeof( sum ) );
  304. return( ret );
  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. return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
  311. }
  312. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  313. {
  314. int ret;
  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. if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
  321. return( ret );
  322. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  323. return( ret );
  324. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
  325. ctx->md_info->block_size ) ) != 0 )
  326. return( ret );
  327. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
  328. ctx->md_info->size ) ) != 0 )
  329. return( ret );
  330. return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
  331. }
  332. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
  333. {
  334. int ret;
  335. unsigned char *ipad;
  336. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  337. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  338. ipad = (unsigned char *) ctx->hmac_ctx;
  339. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  340. return( ret );
  341. return( ctx->md_info->update_func( ctx->md_ctx, ipad,
  342. ctx->md_info->block_size ) );
  343. }
  344. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
  345. const unsigned char *key, size_t keylen,
  346. const unsigned char *input, size_t ilen,
  347. unsigned char *output )
  348. {
  349. mbedtls_md_context_t ctx;
  350. int ret;
  351. if( md_info == NULL )
  352. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  353. mbedtls_md_init( &ctx );
  354. if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
  355. goto cleanup;
  356. if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
  357. goto cleanup;
  358. if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
  359. goto cleanup;
  360. if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
  361. goto cleanup;
  362. cleanup:
  363. mbedtls_md_free( &ctx );
  364. return( ret );
  365. }
  366. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
  367. {
  368. if( ctx == NULL || ctx->md_info == NULL )
  369. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  370. return( ctx->md_info->process_func( ctx->md_ctx, data ) );
  371. }
  372. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
  373. {
  374. if( md_info == NULL )
  375. return( 0 );
  376. return md_info->size;
  377. }
  378. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
  379. {
  380. if( md_info == NULL )
  381. return( MBEDTLS_MD_NONE );
  382. return md_info->type;
  383. }
  384. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
  385. {
  386. if( md_info == NULL )
  387. return( NULL );
  388. return md_info->name;
  389. }
  390. #endif /* MBEDTLS_MD_C */