md.c 23 KB

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