md.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /**
  2. * \file md.c
  3. *
  4. * \brief Generic message digest wrapper for mbed TLS
  5. *
  6. * \author Adriaan de Jong <[email protected]>
  7. *
  8. * Copyright The Mbed TLS Contributors
  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. #include "common.h"
  24. #if defined(MBEDTLS_MD_C)
  25. #include "mbedtls/md.h"
  26. #include "md_wrap.h"
  27. #include "mbedtls/platform_util.h"
  28. #include "mbedtls/error.h"
  29. #include "mbedtls/md5.h"
  30. #include "mbedtls/ripemd160.h"
  31. #include "mbedtls/sha1.h"
  32. #include "mbedtls/sha256.h"
  33. #include "mbedtls/sha512.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. #if defined(MBEDTLS_MD5_C)
  46. const mbedtls_md_info_t mbedtls_md5_info = {
  47. "MD5",
  48. MBEDTLS_MD_MD5,
  49. 16,
  50. 64,
  51. };
  52. #endif
  53. #if defined(MBEDTLS_RIPEMD160_C)
  54. const mbedtls_md_info_t mbedtls_ripemd160_info = {
  55. "RIPEMD160",
  56. MBEDTLS_MD_RIPEMD160,
  57. 20,
  58. 64,
  59. };
  60. #endif
  61. #if defined(MBEDTLS_SHA1_C)
  62. const mbedtls_md_info_t mbedtls_sha1_info = {
  63. "SHA1",
  64. MBEDTLS_MD_SHA1,
  65. 20,
  66. 64,
  67. };
  68. #endif
  69. #if defined(MBEDTLS_SHA224_C)
  70. const mbedtls_md_info_t mbedtls_sha224_info = {
  71. "SHA224",
  72. MBEDTLS_MD_SHA224,
  73. 28,
  74. 64,
  75. };
  76. #endif
  77. #if defined(MBEDTLS_SHA256_C)
  78. const mbedtls_md_info_t mbedtls_sha256_info = {
  79. "SHA256",
  80. MBEDTLS_MD_SHA256,
  81. 32,
  82. 64,
  83. };
  84. #endif
  85. #if defined(MBEDTLS_SHA384_C)
  86. const mbedtls_md_info_t mbedtls_sha384_info = {
  87. "SHA384",
  88. MBEDTLS_MD_SHA384,
  89. 48,
  90. 128,
  91. };
  92. #endif
  93. #if defined(MBEDTLS_SHA512_C)
  94. const mbedtls_md_info_t mbedtls_sha512_info = {
  95. "SHA512",
  96. MBEDTLS_MD_SHA512,
  97. 64,
  98. 128,
  99. };
  100. #endif
  101. /*
  102. * Reminder: update profiles in x509_crt.c when adding a new hash!
  103. */
  104. static const int supported_digests[] = {
  105. #if defined(MBEDTLS_SHA512_C)
  106. MBEDTLS_MD_SHA512,
  107. #endif
  108. #if defined(MBEDTLS_SHA384_C)
  109. MBEDTLS_MD_SHA384,
  110. #endif
  111. #if defined(MBEDTLS_SHA256_C)
  112. MBEDTLS_MD_SHA256,
  113. #endif
  114. #if defined(MBEDTLS_SHA224_C)
  115. MBEDTLS_MD_SHA224,
  116. #endif
  117. #if defined(MBEDTLS_SHA1_C)
  118. MBEDTLS_MD_SHA1,
  119. #endif
  120. #if defined(MBEDTLS_RIPEMD160_C)
  121. MBEDTLS_MD_RIPEMD160,
  122. #endif
  123. #if defined(MBEDTLS_MD5_C)
  124. MBEDTLS_MD_MD5,
  125. #endif
  126. MBEDTLS_MD_NONE
  127. };
  128. const int *mbedtls_md_list( void )
  129. {
  130. return( supported_digests );
  131. }
  132. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
  133. {
  134. if( NULL == md_name )
  135. return( NULL );
  136. /* Get the appropriate digest information */
  137. #if defined(MBEDTLS_MD5_C)
  138. if( !strcmp( "MD5", md_name ) )
  139. return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
  140. #endif
  141. #if defined(MBEDTLS_RIPEMD160_C)
  142. if( !strcmp( "RIPEMD160", md_name ) )
  143. return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
  144. #endif
  145. #if defined(MBEDTLS_SHA1_C)
  146. if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
  147. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  148. #endif
  149. #if defined(MBEDTLS_SHA224_C)
  150. if( !strcmp( "SHA224", md_name ) )
  151. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
  152. #endif
  153. #if defined(MBEDTLS_SHA256_C)
  154. if( !strcmp( "SHA256", md_name ) )
  155. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
  156. #endif
  157. #if defined(MBEDTLS_SHA384_C)
  158. if( !strcmp( "SHA384", md_name ) )
  159. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
  160. #endif
  161. #if defined(MBEDTLS_SHA512_C)
  162. if( !strcmp( "SHA512", md_name ) )
  163. return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
  164. #endif
  165. return( NULL );
  166. }
  167. const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
  168. {
  169. switch( md_type )
  170. {
  171. #if defined(MBEDTLS_MD5_C)
  172. case MBEDTLS_MD_MD5:
  173. return( &mbedtls_md5_info );
  174. #endif
  175. #if defined(MBEDTLS_RIPEMD160_C)
  176. case MBEDTLS_MD_RIPEMD160:
  177. return( &mbedtls_ripemd160_info );
  178. #endif
  179. #if defined(MBEDTLS_SHA1_C)
  180. case MBEDTLS_MD_SHA1:
  181. return( &mbedtls_sha1_info );
  182. #endif
  183. #if defined(MBEDTLS_SHA224_C)
  184. case MBEDTLS_MD_SHA224:
  185. return( &mbedtls_sha224_info );
  186. #endif
  187. #if defined(MBEDTLS_SHA256_C)
  188. case MBEDTLS_MD_SHA256:
  189. return( &mbedtls_sha256_info );
  190. #endif
  191. #if defined(MBEDTLS_SHA384_C)
  192. case MBEDTLS_MD_SHA384:
  193. return( &mbedtls_sha384_info );
  194. #endif
  195. #if defined(MBEDTLS_SHA512_C)
  196. case MBEDTLS_MD_SHA512:
  197. return( &mbedtls_sha512_info );
  198. #endif
  199. default:
  200. return( NULL );
  201. }
  202. }
  203. void mbedtls_md_init( mbedtls_md_context_t *ctx )
  204. {
  205. memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
  206. }
  207. void mbedtls_md_free( mbedtls_md_context_t *ctx )
  208. {
  209. if( ctx == NULL || ctx->md_info == NULL )
  210. return;
  211. if( ctx->md_ctx != NULL )
  212. {
  213. switch( ctx->md_info->type )
  214. {
  215. #if defined(MBEDTLS_MD5_C)
  216. case MBEDTLS_MD_MD5:
  217. mbedtls_md5_free( ctx->md_ctx );
  218. break;
  219. #endif
  220. #if defined(MBEDTLS_RIPEMD160_C)
  221. case MBEDTLS_MD_RIPEMD160:
  222. mbedtls_ripemd160_free( ctx->md_ctx );
  223. break;
  224. #endif
  225. #if defined(MBEDTLS_SHA1_C)
  226. case MBEDTLS_MD_SHA1:
  227. mbedtls_sha1_free( ctx->md_ctx );
  228. break;
  229. #endif
  230. #if defined(MBEDTLS_SHA224_C)
  231. case MBEDTLS_MD_SHA224:
  232. mbedtls_sha256_free( ctx->md_ctx );
  233. break;
  234. #endif
  235. #if defined(MBEDTLS_SHA256_C)
  236. case MBEDTLS_MD_SHA256:
  237. mbedtls_sha256_free( ctx->md_ctx );
  238. break;
  239. #endif
  240. #if defined(MBEDTLS_SHA384_C)
  241. case MBEDTLS_MD_SHA384:
  242. mbedtls_sha512_free( ctx->md_ctx );
  243. break;
  244. #endif
  245. #if defined(MBEDTLS_SHA512_C)
  246. case MBEDTLS_MD_SHA512:
  247. mbedtls_sha512_free( ctx->md_ctx );
  248. break;
  249. #endif
  250. default:
  251. /* Shouldn't happen */
  252. break;
  253. }
  254. mbedtls_free( ctx->md_ctx );
  255. }
  256. if( ctx->hmac_ctx != NULL )
  257. {
  258. mbedtls_platform_zeroize( ctx->hmac_ctx,
  259. 2 * ctx->md_info->block_size );
  260. mbedtls_free( ctx->hmac_ctx );
  261. }
  262. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
  263. }
  264. int mbedtls_md_clone( mbedtls_md_context_t *dst,
  265. const mbedtls_md_context_t *src )
  266. {
  267. if( dst == NULL || dst->md_info == NULL ||
  268. src == NULL || src->md_info == NULL ||
  269. dst->md_info != src->md_info )
  270. {
  271. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  272. }
  273. switch( src->md_info->type )
  274. {
  275. #if defined(MBEDTLS_MD5_C)
  276. case MBEDTLS_MD_MD5:
  277. mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
  278. break;
  279. #endif
  280. #if defined(MBEDTLS_RIPEMD160_C)
  281. case MBEDTLS_MD_RIPEMD160:
  282. mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
  283. break;
  284. #endif
  285. #if defined(MBEDTLS_SHA1_C)
  286. case MBEDTLS_MD_SHA1:
  287. mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
  288. break;
  289. #endif
  290. #if defined(MBEDTLS_SHA224_C)
  291. case MBEDTLS_MD_SHA224:
  292. mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
  293. break;
  294. #endif
  295. #if defined(MBEDTLS_SHA256_C)
  296. case MBEDTLS_MD_SHA256:
  297. mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
  298. break;
  299. #endif
  300. #if defined(MBEDTLS_SHA384_C)
  301. case MBEDTLS_MD_SHA384:
  302. mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
  303. break;
  304. #endif
  305. #if defined(MBEDTLS_SHA512_C)
  306. case MBEDTLS_MD_SHA512:
  307. mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
  308. break;
  309. #endif
  310. default:
  311. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  312. }
  313. return( 0 );
  314. }
  315. #define ALLOC( type ) \
  316. do { \
  317. ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
  318. if( ctx->md_ctx == NULL ) \
  319. return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
  320. mbedtls_##type##_init( ctx->md_ctx ); \
  321. } \
  322. while( 0 )
  323. int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
  324. {
  325. if( md_info == NULL || ctx == NULL )
  326. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  327. ctx->md_info = md_info;
  328. ctx->md_ctx = NULL;
  329. ctx->hmac_ctx = NULL;
  330. switch( md_info->type )
  331. {
  332. #if defined(MBEDTLS_MD5_C)
  333. case MBEDTLS_MD_MD5:
  334. ALLOC( md5 );
  335. break;
  336. #endif
  337. #if defined(MBEDTLS_RIPEMD160_C)
  338. case MBEDTLS_MD_RIPEMD160:
  339. ALLOC( ripemd160 );
  340. break;
  341. #endif
  342. #if defined(MBEDTLS_SHA1_C)
  343. case MBEDTLS_MD_SHA1:
  344. ALLOC( sha1 );
  345. break;
  346. #endif
  347. #if defined(MBEDTLS_SHA224_C)
  348. case MBEDTLS_MD_SHA224:
  349. ALLOC( sha256 );
  350. break;
  351. #endif
  352. #if defined(MBEDTLS_SHA256_C)
  353. case MBEDTLS_MD_SHA256:
  354. ALLOC( sha256 );
  355. break;
  356. #endif
  357. #if defined(MBEDTLS_SHA384_C)
  358. case MBEDTLS_MD_SHA384:
  359. ALLOC( sha512 );
  360. break;
  361. #endif
  362. #if defined(MBEDTLS_SHA512_C)
  363. case MBEDTLS_MD_SHA512:
  364. ALLOC( sha512 );
  365. break;
  366. #endif
  367. default:
  368. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  369. }
  370. if( hmac != 0 )
  371. {
  372. ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
  373. if( ctx->hmac_ctx == NULL )
  374. {
  375. mbedtls_md_free( ctx );
  376. return( MBEDTLS_ERR_MD_ALLOC_FAILED );
  377. }
  378. }
  379. return( 0 );
  380. }
  381. #undef ALLOC
  382. int mbedtls_md_starts( mbedtls_md_context_t *ctx )
  383. {
  384. if( ctx == NULL || ctx->md_info == NULL )
  385. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  386. switch( ctx->md_info->type )
  387. {
  388. #if defined(MBEDTLS_MD5_C)
  389. case MBEDTLS_MD_MD5:
  390. return( mbedtls_md5_starts( ctx->md_ctx ) );
  391. #endif
  392. #if defined(MBEDTLS_RIPEMD160_C)
  393. case MBEDTLS_MD_RIPEMD160:
  394. return( mbedtls_ripemd160_starts( ctx->md_ctx ) );
  395. #endif
  396. #if defined(MBEDTLS_SHA1_C)
  397. case MBEDTLS_MD_SHA1:
  398. return( mbedtls_sha1_starts( ctx->md_ctx ) );
  399. #endif
  400. #if defined(MBEDTLS_SHA224_C)
  401. case MBEDTLS_MD_SHA224:
  402. return( mbedtls_sha256_starts( ctx->md_ctx, 1 ) );
  403. #endif
  404. #if defined(MBEDTLS_SHA256_C)
  405. case MBEDTLS_MD_SHA256:
  406. return( mbedtls_sha256_starts( ctx->md_ctx, 0 ) );
  407. #endif
  408. #if defined(MBEDTLS_SHA384_C)
  409. case MBEDTLS_MD_SHA384:
  410. return( mbedtls_sha512_starts( ctx->md_ctx, 1 ) );
  411. #endif
  412. #if defined(MBEDTLS_SHA512_C)
  413. case MBEDTLS_MD_SHA512:
  414. return( mbedtls_sha512_starts( ctx->md_ctx, 0 ) );
  415. #endif
  416. default:
  417. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  418. }
  419. }
  420. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  421. {
  422. if( ctx == NULL || ctx->md_info == NULL )
  423. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  424. switch( ctx->md_info->type )
  425. {
  426. #if defined(MBEDTLS_MD5_C)
  427. case MBEDTLS_MD_MD5:
  428. return( mbedtls_md5_update( ctx->md_ctx, input, ilen ) );
  429. #endif
  430. #if defined(MBEDTLS_RIPEMD160_C)
  431. case MBEDTLS_MD_RIPEMD160:
  432. return( mbedtls_ripemd160_update( ctx->md_ctx, input, ilen ) );
  433. #endif
  434. #if defined(MBEDTLS_SHA1_C)
  435. case MBEDTLS_MD_SHA1:
  436. return( mbedtls_sha1_update( ctx->md_ctx, input, ilen ) );
  437. #endif
  438. #if defined(MBEDTLS_SHA224_C)
  439. case MBEDTLS_MD_SHA224:
  440. return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
  441. #endif
  442. #if defined(MBEDTLS_SHA256_C)
  443. case MBEDTLS_MD_SHA256:
  444. return( mbedtls_sha256_update( ctx->md_ctx, input, ilen ) );
  445. #endif
  446. #if defined(MBEDTLS_SHA384_C)
  447. case MBEDTLS_MD_SHA384:
  448. return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
  449. #endif
  450. #if defined(MBEDTLS_SHA512_C)
  451. case MBEDTLS_MD_SHA512:
  452. return( mbedtls_sha512_update( ctx->md_ctx, input, ilen ) );
  453. #endif
  454. default:
  455. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  456. }
  457. }
  458. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  459. {
  460. if( ctx == NULL || ctx->md_info == NULL )
  461. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  462. switch( ctx->md_info->type )
  463. {
  464. #if defined(MBEDTLS_MD5_C)
  465. case MBEDTLS_MD_MD5:
  466. return( mbedtls_md5_finish( ctx->md_ctx, output ) );
  467. #endif
  468. #if defined(MBEDTLS_RIPEMD160_C)
  469. case MBEDTLS_MD_RIPEMD160:
  470. return( mbedtls_ripemd160_finish( ctx->md_ctx, output ) );
  471. #endif
  472. #if defined(MBEDTLS_SHA1_C)
  473. case MBEDTLS_MD_SHA1:
  474. return( mbedtls_sha1_finish( ctx->md_ctx, output ) );
  475. #endif
  476. #if defined(MBEDTLS_SHA224_C)
  477. case MBEDTLS_MD_SHA224:
  478. return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
  479. #endif
  480. #if defined(MBEDTLS_SHA256_C)
  481. case MBEDTLS_MD_SHA256:
  482. return( mbedtls_sha256_finish( ctx->md_ctx, output ) );
  483. #endif
  484. #if defined(MBEDTLS_SHA384_C)
  485. case MBEDTLS_MD_SHA384:
  486. return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
  487. #endif
  488. #if defined(MBEDTLS_SHA512_C)
  489. case MBEDTLS_MD_SHA512:
  490. return( mbedtls_sha512_finish( ctx->md_ctx, output ) );
  491. #endif
  492. default:
  493. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  494. }
  495. }
  496. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  497. unsigned char *output )
  498. {
  499. if( md_info == NULL )
  500. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  501. switch( md_info->type )
  502. {
  503. #if defined(MBEDTLS_MD5_C)
  504. case MBEDTLS_MD_MD5:
  505. return( mbedtls_md5( input, ilen, output ) );
  506. #endif
  507. #if defined(MBEDTLS_RIPEMD160_C)
  508. case MBEDTLS_MD_RIPEMD160:
  509. return( mbedtls_ripemd160( input, ilen, output ) );
  510. #endif
  511. #if defined(MBEDTLS_SHA1_C)
  512. case MBEDTLS_MD_SHA1:
  513. return( mbedtls_sha1( input, ilen, output ) );
  514. #endif
  515. #if defined(MBEDTLS_SHA224_C)
  516. case MBEDTLS_MD_SHA224:
  517. return( mbedtls_sha256( input, ilen, output, 1 ) );
  518. #endif
  519. #if defined(MBEDTLS_SHA256_C)
  520. case MBEDTLS_MD_SHA256:
  521. return( mbedtls_sha256( input, ilen, output, 0 ) );
  522. #endif
  523. #if defined(MBEDTLS_SHA384_C)
  524. case MBEDTLS_MD_SHA384:
  525. return( mbedtls_sha512( input, ilen, output, 1 ) );
  526. #endif
  527. #if defined(MBEDTLS_SHA512_C)
  528. case MBEDTLS_MD_SHA512:
  529. return( mbedtls_sha512( input, ilen, output, 0 ) );
  530. #endif
  531. default:
  532. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  533. }
  534. }
  535. #if defined(MBEDTLS_FS_IO)
  536. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
  537. {
  538. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  539. FILE *f;
  540. size_t n;
  541. mbedtls_md_context_t ctx;
  542. unsigned char buf[1024];
  543. if( md_info == NULL )
  544. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  545. if( ( f = fopen( path, "rb" ) ) == NULL )
  546. return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
  547. mbedtls_md_init( &ctx );
  548. if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
  549. goto cleanup;
  550. if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
  551. goto cleanup;
  552. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  553. if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
  554. goto cleanup;
  555. if( ferror( f ) != 0 )
  556. ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
  557. else
  558. ret = mbedtls_md_finish( &ctx, output );
  559. cleanup:
  560. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  561. fclose( f );
  562. mbedtls_md_free( &ctx );
  563. return( ret );
  564. }
  565. #endif /* MBEDTLS_FS_IO */
  566. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
  567. {
  568. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  569. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  570. unsigned char *ipad, *opad;
  571. size_t i;
  572. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  573. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  574. if( keylen > (size_t) ctx->md_info->block_size )
  575. {
  576. if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
  577. goto cleanup;
  578. if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
  579. goto cleanup;
  580. if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
  581. goto cleanup;
  582. keylen = ctx->md_info->size;
  583. key = sum;
  584. }
  585. ipad = (unsigned char *) ctx->hmac_ctx;
  586. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  587. memset( ipad, 0x36, ctx->md_info->block_size );
  588. memset( opad, 0x5C, ctx->md_info->block_size );
  589. for( i = 0; i < keylen; i++ )
  590. {
  591. ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
  592. opad[i] = (unsigned char)( opad[i] ^ key[i] );
  593. }
  594. if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
  595. goto cleanup;
  596. if( ( ret = mbedtls_md_update( ctx, ipad,
  597. ctx->md_info->block_size ) ) != 0 )
  598. goto cleanup;
  599. cleanup:
  600. mbedtls_platform_zeroize( sum, sizeof( sum ) );
  601. return( ret );
  602. }
  603. int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
  604. {
  605. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  606. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  607. return( mbedtls_md_update( ctx, input, ilen ) );
  608. }
  609. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
  610. {
  611. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  612. unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
  613. unsigned char *opad;
  614. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  615. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  616. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  617. if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
  618. return( ret );
  619. if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
  620. return( ret );
  621. if( ( ret = mbedtls_md_update( ctx, opad,
  622. ctx->md_info->block_size ) ) != 0 )
  623. return( ret );
  624. if( ( ret = mbedtls_md_update( ctx, tmp,
  625. ctx->md_info->size ) ) != 0 )
  626. return( ret );
  627. return( mbedtls_md_finish( ctx, output ) );
  628. }
  629. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
  630. {
  631. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  632. unsigned char *ipad;
  633. if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
  634. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  635. ipad = (unsigned char *) ctx->hmac_ctx;
  636. if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
  637. return( ret );
  638. return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
  639. }
  640. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
  641. const unsigned char *key, size_t keylen,
  642. const unsigned char *input, size_t ilen,
  643. unsigned char *output )
  644. {
  645. mbedtls_md_context_t ctx;
  646. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  647. if( md_info == NULL )
  648. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  649. mbedtls_md_init( &ctx );
  650. if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
  651. goto cleanup;
  652. if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
  653. goto cleanup;
  654. if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
  655. goto cleanup;
  656. if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
  657. goto cleanup;
  658. cleanup:
  659. mbedtls_md_free( &ctx );
  660. return( ret );
  661. }
  662. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
  663. {
  664. if( ctx == NULL || ctx->md_info == NULL )
  665. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  666. switch( ctx->md_info->type )
  667. {
  668. #if defined(MBEDTLS_MD5_C)
  669. case MBEDTLS_MD_MD5:
  670. return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
  671. #endif
  672. #if defined(MBEDTLS_RIPEMD160_C)
  673. case MBEDTLS_MD_RIPEMD160:
  674. return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
  675. #endif
  676. #if defined(MBEDTLS_SHA1_C)
  677. case MBEDTLS_MD_SHA1:
  678. return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
  679. #endif
  680. #if defined(MBEDTLS_SHA224_C)
  681. case MBEDTLS_MD_SHA224:
  682. return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
  683. #endif
  684. #if defined(MBEDTLS_SHA256_C)
  685. case MBEDTLS_MD_SHA256:
  686. return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
  687. #endif
  688. #if defined(MBEDTLS_SHA384_C)
  689. case MBEDTLS_MD_SHA384:
  690. return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
  691. #endif
  692. #if defined(MBEDTLS_SHA512_C)
  693. case MBEDTLS_MD_SHA512:
  694. return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
  695. #endif
  696. default:
  697. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  698. }
  699. }
  700. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
  701. {
  702. if( md_info == NULL )
  703. return( 0 );
  704. return md_info->size;
  705. }
  706. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
  707. {
  708. if( md_info == NULL )
  709. return( MBEDTLS_MD_NONE );
  710. return md_info->type;
  711. }
  712. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
  713. {
  714. if( md_info == NULL )
  715. return( NULL );
  716. return md_info->name;
  717. }
  718. #endif /* MBEDTLS_MD_C */