md.c 23 KB

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