md.c 22 KB

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