md.c 24 KB

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