md.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  10. *
  11. * This file is provided under the Apache License 2.0, or the
  12. * GNU General Public License v2.0 or later.
  13. *
  14. * **********
  15. * Apache License 2.0:
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18. * not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. *
  29. * **********
  30. *
  31. * **********
  32. * GNU General Public License v2.0 or later:
  33. *
  34. * This program is free software; you can redistribute it and/or modify
  35. * it under the terms of the GNU General Public License as published by
  36. * the Free Software Foundation; either version 2 of the License, or
  37. * (at your option) any later version.
  38. *
  39. * This program is distributed in the hope that it will be useful,
  40. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. * GNU General Public License for more details.
  43. *
  44. * You should have received a copy of the GNU General Public License along
  45. * with this program; if not, write to the Free Software Foundation, Inc.,
  46. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  47. *
  48. * **********
  49. */
  50. #if !defined(MBEDTLS_CONFIG_FILE)
  51. #include "mbedtls/config.h"
  52. #else
  53. #include MBEDTLS_CONFIG_FILE
  54. #endif
  55. #if defined(MBEDTLS_MD_C)
  56. #include "mbedtls/md.h"
  57. #include "mbedtls/md_internal.h"
  58. #include "mbedtls/platform_util.h"
  59. #if defined(MBEDTLS_PLATFORM_C)
  60. #include "mbedtls/platform.h"
  61. #else
  62. #include <stdlib.h>
  63. #define mbedtls_calloc calloc
  64. #define mbedtls_free free
  65. #endif
  66. #include <string.h>
  67. #if defined(MBEDTLS_FS_IO)
  68. #include <stdio.h>
  69. #endif
  70. /*
  71. * Reminder: update profiles in x509_crt.c when adding a new hash!
  72. */
  73. static const int supported_digests[] = {
  74. #if defined(MBEDTLS_SHA512_C)
  75. MBEDTLS_MD_SHA512,
  76. MBEDTLS_MD_SHA384,
  77. #endif
  78. #if defined(MBEDTLS_SHA256_C)
  79. MBEDTLS_MD_SHA256,
  80. MBEDTLS_MD_SHA224,
  81. #endif
  82. #if defined(MBEDTLS_SHA1_C)
  83. MBEDTLS_MD_SHA1,
  84. #endif
  85. #if defined(MBEDTLS_RIPEMD160_C)
  86. MBEDTLS_MD_RIPEMD160,
  87. #endif
  88. #if defined(MBEDTLS_MD5_C)
  89. MBEDTLS_MD_MD5,
  90. #endif
  91. #if defined(MBEDTLS_MD4_C)
  92. MBEDTLS_MD_MD4,
  93. #endif
  94. #if defined(MBEDTLS_MD2_C)
  95. MBEDTLS_MD_MD2,
  96. #endif
  97. MBEDTLS_MD_NONE
  98. };
  99. const int *mbedtls_md_list( void )
  100. {
  101. return( supported_digests );
  102. }
  103. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
  104. {
  105. if( NULL == md_name )
  106. return( NULL );
  107. /* Get the appropriate digest information */
  108. #if defined(MBEDTLS_MD2_C)
  109. if( !strcmp( "MD2", md_name ) )
  110. return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
  111. #endif
  112. #if defined(MBEDTLS_MD4_C)
  113. if( !strcmp( "MD4", md_name ) )
  114. return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
  115. #endif
  116. #if defined(MBEDTLS_MD5_C)
  117. if( !strcmp( "MD5", md_name ) )
  118. return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
  119. #endif
  120. #if defined(MBEDTLS_RIPEMD160_C)
  121. if( !strcmp( "RIPEMD160", md_name ) )
  122. return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
  123. #endif
  124. #if defined(MBEDTLS_SHA1_C)
  125. if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
  126. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  127. #endif
  128. #if defined(MBEDTLS_SHA256_C)
  129. if( !strcmp( "SHA224", md_name ) )
  130. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
  131. if( !strcmp( "SHA256", md_name ) )
  132. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
  133. #endif
  134. #if defined(MBEDTLS_SHA512_C)
  135. if( !strcmp( "SHA384", md_name ) )
  136. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
  137. if( !strcmp( "SHA512", md_name ) )
  138. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
  139. #endif
  140. return( NULL );
  141. }
  142. const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
  143. {
  144. switch( md_type )
  145. {
  146. #if defined(MBEDTLS_MD2_C)
  147. case MBEDTLS_MD_MD2:
  148. return( &mbedtls_md2_info );
  149. #endif
  150. #if defined(MBEDTLS_MD4_C)
  151. case MBEDTLS_MD_MD4:
  152. return( &mbedtls_md4_info );
  153. #endif
  154. #if defined(MBEDTLS_MD5_C)
  155. case MBEDTLS_MD_MD5:
  156. return( &mbedtls_md5_info );
  157. #endif
  158. #if defined(MBEDTLS_RIPEMD160_C)
  159. case MBEDTLS_MD_RIPEMD160:
  160. return( &mbedtls_ripemd160_info );
  161. #endif
  162. #if defined(MBEDTLS_SHA1_C)
  163. case MBEDTLS_MD_SHA1:
  164. return( &mbedtls_sha1_info );
  165. #endif
  166. #if defined(MBEDTLS_SHA256_C)
  167. case MBEDTLS_MD_SHA224:
  168. return( &mbedtls_sha224_info );
  169. case MBEDTLS_MD_SHA256:
  170. return( &mbedtls_sha256_info );
  171. #endif
  172. #if defined(MBEDTLS_SHA512_C)
  173. case MBEDTLS_MD_SHA384:
  174. return( &mbedtls_sha384_info );
  175. case MBEDTLS_MD_SHA512:
  176. return( &mbedtls_sha512_info );
  177. #endif
  178. default:
  179. return( NULL );
  180. }
  181. }
  182. void mbedtls_md_init( mbedtls_md_context_t *ctx )
  183. {
  184. memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
  185. }
  186. void mbedtls_md_free( mbedtls_md_context_t *ctx )
  187. {
  188. if( ctx == NULL || ctx->md_info == NULL )
  189. return;
  190. if( ctx->md_ctx != NULL )
  191. ctx->md_info->ctx_free_func( ctx->md_ctx );
  192. if( ctx->hmac_ctx != NULL )
  193. {
  194. mbedtls_platform_zeroize( ctx->hmac_ctx,
  195. 2 * ctx->md_info->block_size );
  196. mbedtls_free( ctx->hmac_ctx );
  197. }
  198. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
  199. }
  200. int mbedtls_md_clone( mbedtls_md_context_t *dst,
  201. const mbedtls_md_context_t *src )
  202. {
  203. if( dst == NULL || dst->md_info == NULL ||
  204. src == NULL || src->md_info == NULL ||
  205. dst->md_info != src->md_info )
  206. {
  207. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  208. }
  209. dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
  210. return( 0 );
  211. }
  212. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  213. int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
  214. {
  215. return mbedtls_md_setup( ctx, md_info, 1 );
  216. }
  217. #endif
  218. int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
  219. {
  220. if( md_info == NULL || ctx == NULL )
  221. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  222. if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
  223. return( MBEDTLS_ERR_MD_ALLOC_FAILED );
  224. if( hmac != 0 )
  225. {
  226. ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
  227. if( ctx->hmac_ctx == NULL )
  228. {
  229. md_info->ctx_free_func( ctx->md_ctx );
  230. return( MBEDTLS_ERR_MD_ALLOC_FAILED );
  231. }
  232. }
  233. ctx->md_info = md_info;
  234. return( 0 );
  235. }
  236. int mbedtls_md_starts( mbedtls_md_context_t *ctx )
  237. {
  238. if( ctx == NULL || ctx->md_info == NULL )
  239. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  240. return( ctx->md_info->starts_func( ctx->md_ctx ) );
  241. }
  242. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  243. {
  244. if( ctx == NULL || ctx->md_info == NULL )
  245. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  246. return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
  247. }
  248. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  249. {
  250. if( ctx == NULL || ctx->md_info == NULL )
  251. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  252. return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
  253. }
  254. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  255. unsigned char *output )
  256. {
  257. if( md_info == NULL )
  258. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  259. return( md_info->digest_func( input, ilen, output ) );
  260. }
  261. #if defined(MBEDTLS_FS_IO)
  262. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
  263. {
  264. int ret;
  265. FILE *f;
  266. size_t n;
  267. mbedtls_md_context_t ctx;
  268. unsigned char buf[1024];
  269. if( md_info == NULL )
  270. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  271. if( ( f = fopen( path, "rb" ) ) == NULL )
  272. return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
  273. mbedtls_md_init( &ctx );
  274. if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
  275. goto cleanup;
  276. if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
  277. goto cleanup;
  278. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  279. if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
  280. goto cleanup;
  281. if( ferror( f ) != 0 )
  282. ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
  283. else
  284. ret = md_info->finish_func( ctx.md_ctx, output );
  285. cleanup:
  286. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  287. fclose( f );
  288. mbedtls_md_free( &ctx );
  289. return( ret );
  290. }
  291. #endif /* MBEDTLS_FS_IO */
  292. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
  293. {
  294. int ret;
  295. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  296. unsigned char *ipad, *opad;
  297. size_t i;
  298. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  299. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  300. if( keylen > (size_t) ctx->md_info->block_size )
  301. {
  302. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  303. goto cleanup;
  304. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
  305. goto cleanup;
  306. if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
  307. goto cleanup;
  308. keylen = ctx->md_info->size;
  309. key = sum;
  310. }
  311. ipad = (unsigned char *) ctx->hmac_ctx;
  312. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  313. memset( ipad, 0x36, ctx->md_info->block_size );
  314. memset( opad, 0x5C, ctx->md_info->block_size );
  315. for( i = 0; i < keylen; i++ )
  316. {
  317. ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
  318. opad[i] = (unsigned char)( opad[i] ^ key[i] );
  319. }
  320. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  321. goto cleanup;
  322. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
  323. ctx->md_info->block_size ) ) != 0 )
  324. goto cleanup;
  325. cleanup:
  326. mbedtls_platform_zeroize( sum, sizeof( sum ) );
  327. return( ret );
  328. }
  329. int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  330. {
  331. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  332. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  333. return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
  334. }
  335. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  336. {
  337. int ret;
  338. unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
  339. unsigned char *opad;
  340. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  341. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  342. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  343. if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
  344. return( ret );
  345. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  346. return( ret );
  347. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
  348. ctx->md_info->block_size ) ) != 0 )
  349. return( ret );
  350. if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
  351. ctx->md_info->size ) ) != 0 )
  352. return( ret );
  353. return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
  354. }
  355. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
  356. {
  357. int ret;
  358. unsigned char *ipad;
  359. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  360. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  361. ipad = (unsigned char *) ctx->hmac_ctx;
  362. if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
  363. return( ret );
  364. return( ctx->md_info->update_func( ctx->md_ctx, ipad,
  365. ctx->md_info->block_size ) );
  366. }
  367. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
  368. const unsigned char *key, size_t keylen,
  369. const unsigned char *input, size_t ilen,
  370. unsigned char *output )
  371. {
  372. mbedtls_md_context_t ctx;
  373. int ret;
  374. if( md_info == NULL )
  375. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  376. mbedtls_md_init( &ctx );
  377. if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
  378. goto cleanup;
  379. if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
  380. goto cleanup;
  381. if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
  382. goto cleanup;
  383. if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
  384. goto cleanup;
  385. cleanup:
  386. mbedtls_md_free( &ctx );
  387. return( ret );
  388. }
  389. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
  390. {
  391. if( ctx == NULL || ctx->md_info == NULL )
  392. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  393. return( ctx->md_info->process_func( ctx->md_ctx, data ) );
  394. }
  395. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
  396. {
  397. if( md_info == NULL )
  398. return( 0 );
  399. return md_info->size;
  400. }
  401. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
  402. {
  403. if( md_info == NULL )
  404. return( MBEDTLS_MD_NONE );
  405. return md_info->type;
  406. }
  407. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
  408. {
  409. if( md_info == NULL )
  410. return( NULL );
  411. return md_info->name;
  412. }
  413. #endif /* MBEDTLS_MD_C */