md.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. /*
  25. * Availability of functions in this module is controlled by two
  26. * feature macros:
  27. * - MBEDTLS_MD_C enables the whole module;
  28. * - MBEDTLS_MD_LIGHT enables only functions for hashing and accessing
  29. * most hash metadata (everything except string names); is it
  30. * automatically set whenever MBEDTLS_MD_C is defined.
  31. *
  32. * In this file, functions from MD_LIGHT are at the top, MD_C at the end.
  33. *
  34. * In the future we may want to change the contract of some functions
  35. * (behaviour with NULL arguments) depending on whether MD_C is defined or
  36. * only MD_LIGHT. Also, the exact scope of MD_LIGHT might vary.
  37. *
  38. * For these reasons, we're keeping MD_LIGHT internal for now.
  39. */
  40. #if defined(MBEDTLS_MD_LIGHT)
  41. #include "mbedtls/md.h"
  42. #include "md_wrap.h"
  43. #include "mbedtls/platform_util.h"
  44. #include "mbedtls/error.h"
  45. #include "mbedtls/md5.h"
  46. #include "mbedtls/ripemd160.h"
  47. #include "mbedtls/sha1.h"
  48. #include "mbedtls/sha256.h"
  49. #include "mbedtls/sha512.h"
  50. #if defined(MBEDTLS_MD_SOME_PSA)
  51. #include <psa/crypto.h>
  52. #include "psa_crypto_core.h"
  53. #endif
  54. #include "mbedtls/platform.h"
  55. #include <string.h>
  56. #if defined(MBEDTLS_FS_IO)
  57. #include <stdio.h>
  58. #endif
  59. #if defined(MBEDTLS_MD_CAN_MD5)
  60. const mbedtls_md_info_t mbedtls_md5_info = {
  61. "MD5",
  62. MBEDTLS_MD_MD5,
  63. 16,
  64. 64,
  65. };
  66. #endif
  67. #if defined(MBEDTLS_MD_CAN_RIPEMD160)
  68. const mbedtls_md_info_t mbedtls_ripemd160_info = {
  69. "RIPEMD160",
  70. MBEDTLS_MD_RIPEMD160,
  71. 20,
  72. 64,
  73. };
  74. #endif
  75. #if defined(MBEDTLS_MD_CAN_SHA1)
  76. const mbedtls_md_info_t mbedtls_sha1_info = {
  77. "SHA1",
  78. MBEDTLS_MD_SHA1,
  79. 20,
  80. 64,
  81. };
  82. #endif
  83. #if defined(MBEDTLS_MD_CAN_SHA224)
  84. const mbedtls_md_info_t mbedtls_sha224_info = {
  85. "SHA224",
  86. MBEDTLS_MD_SHA224,
  87. 28,
  88. 64,
  89. };
  90. #endif
  91. #if defined(MBEDTLS_MD_CAN_SHA256)
  92. const mbedtls_md_info_t mbedtls_sha256_info = {
  93. "SHA256",
  94. MBEDTLS_MD_SHA256,
  95. 32,
  96. 64,
  97. };
  98. #endif
  99. #if defined(MBEDTLS_MD_CAN_SHA384)
  100. const mbedtls_md_info_t mbedtls_sha384_info = {
  101. "SHA384",
  102. MBEDTLS_MD_SHA384,
  103. 48,
  104. 128,
  105. };
  106. #endif
  107. #if defined(MBEDTLS_MD_CAN_SHA512)
  108. const mbedtls_md_info_t mbedtls_sha512_info = {
  109. "SHA512",
  110. MBEDTLS_MD_SHA512,
  111. 64,
  112. 128,
  113. };
  114. #endif
  115. const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
  116. {
  117. switch (md_type) {
  118. #if defined(MBEDTLS_MD_CAN_MD5)
  119. case MBEDTLS_MD_MD5:
  120. return &mbedtls_md5_info;
  121. #endif
  122. #if defined(MBEDTLS_MD_CAN_RIPEMD160)
  123. case MBEDTLS_MD_RIPEMD160:
  124. return &mbedtls_ripemd160_info;
  125. #endif
  126. #if defined(MBEDTLS_MD_CAN_SHA1)
  127. case MBEDTLS_MD_SHA1:
  128. return &mbedtls_sha1_info;
  129. #endif
  130. #if defined(MBEDTLS_MD_CAN_SHA224)
  131. case MBEDTLS_MD_SHA224:
  132. return &mbedtls_sha224_info;
  133. #endif
  134. #if defined(MBEDTLS_MD_CAN_SHA256)
  135. case MBEDTLS_MD_SHA256:
  136. return &mbedtls_sha256_info;
  137. #endif
  138. #if defined(MBEDTLS_MD_CAN_SHA384)
  139. case MBEDTLS_MD_SHA384:
  140. return &mbedtls_sha384_info;
  141. #endif
  142. #if defined(MBEDTLS_MD_CAN_SHA512)
  143. case MBEDTLS_MD_SHA512:
  144. return &mbedtls_sha512_info;
  145. #endif
  146. default:
  147. return NULL;
  148. }
  149. }
  150. #if defined(MBEDTLS_MD_SOME_PSA)
  151. static psa_algorithm_t psa_alg_of_md(const mbedtls_md_info_t *info)
  152. {
  153. switch (info->type) {
  154. #if defined(MBEDTLS_MD_MD5_VIA_PSA)
  155. case MBEDTLS_MD_MD5:
  156. return PSA_ALG_MD5;
  157. #endif
  158. #if defined(MBEDTLS_MD_RIPEMD160_VIA_PSA)
  159. case MBEDTLS_MD_RIPEMD160:
  160. return PSA_ALG_RIPEMD160;
  161. #endif
  162. #if defined(MBEDTLS_MD_SHA1_VIA_PSA)
  163. case MBEDTLS_MD_SHA1:
  164. return PSA_ALG_SHA_1;
  165. #endif
  166. #if defined(MBEDTLS_MD_SHA224_VIA_PSA)
  167. case MBEDTLS_MD_SHA224:
  168. return PSA_ALG_SHA_224;
  169. #endif
  170. #if defined(MBEDTLS_MD_SHA256_VIA_PSA)
  171. case MBEDTLS_MD_SHA256:
  172. return PSA_ALG_SHA_256;
  173. #endif
  174. #if defined(MBEDTLS_MD_SHA384_VIA_PSA)
  175. case MBEDTLS_MD_SHA384:
  176. return PSA_ALG_SHA_384;
  177. #endif
  178. #if defined(MBEDTLS_MD_SHA512_VIA_PSA)
  179. case MBEDTLS_MD_SHA512:
  180. return PSA_ALG_SHA_512;
  181. #endif
  182. default:
  183. return PSA_ALG_NONE;
  184. }
  185. }
  186. static int md_can_use_psa(const mbedtls_md_info_t *info)
  187. {
  188. psa_algorithm_t alg = psa_alg_of_md(info);
  189. if (alg == PSA_ALG_NONE) {
  190. return 0;
  191. }
  192. return psa_can_do_hash(alg);
  193. }
  194. static int mbedtls_md_error_from_psa(psa_status_t status)
  195. {
  196. switch (status) {
  197. case PSA_SUCCESS:
  198. return 0;
  199. case PSA_ERROR_NOT_SUPPORTED:
  200. return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
  201. case PSA_ERROR_INSUFFICIENT_MEMORY:
  202. return MBEDTLS_ERR_MD_ALLOC_FAILED;
  203. default:
  204. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  205. }
  206. }
  207. #endif /* MBEDTLS_MD_SOME_PSA */
  208. void mbedtls_md_init(mbedtls_md_context_t *ctx)
  209. {
  210. /* Note: this sets engine (if present) to MBEDTLS_MD_ENGINE_LEGACY */
  211. memset(ctx, 0, sizeof(mbedtls_md_context_t));
  212. }
  213. void mbedtls_md_free(mbedtls_md_context_t *ctx)
  214. {
  215. if (ctx == NULL || ctx->md_info == NULL) {
  216. return;
  217. }
  218. if (ctx->md_ctx != NULL) {
  219. #if defined(MBEDTLS_MD_SOME_PSA)
  220. if (ctx->engine == MBEDTLS_MD_ENGINE_PSA) {
  221. psa_hash_abort(ctx->md_ctx);
  222. } else
  223. #endif
  224. switch (ctx->md_info->type) {
  225. #if defined(MBEDTLS_MD5_C)
  226. case MBEDTLS_MD_MD5:
  227. mbedtls_md5_free(ctx->md_ctx);
  228. break;
  229. #endif
  230. #if defined(MBEDTLS_RIPEMD160_C)
  231. case MBEDTLS_MD_RIPEMD160:
  232. mbedtls_ripemd160_free(ctx->md_ctx);
  233. break;
  234. #endif
  235. #if defined(MBEDTLS_SHA1_C)
  236. case MBEDTLS_MD_SHA1:
  237. mbedtls_sha1_free(ctx->md_ctx);
  238. break;
  239. #endif
  240. #if defined(MBEDTLS_SHA224_C)
  241. case MBEDTLS_MD_SHA224:
  242. mbedtls_sha256_free(ctx->md_ctx);
  243. break;
  244. #endif
  245. #if defined(MBEDTLS_SHA256_C)
  246. case MBEDTLS_MD_SHA256:
  247. mbedtls_sha256_free(ctx->md_ctx);
  248. break;
  249. #endif
  250. #if defined(MBEDTLS_SHA384_C)
  251. case MBEDTLS_MD_SHA384:
  252. mbedtls_sha512_free(ctx->md_ctx);
  253. break;
  254. #endif
  255. #if defined(MBEDTLS_SHA512_C)
  256. case MBEDTLS_MD_SHA512:
  257. mbedtls_sha512_free(ctx->md_ctx);
  258. break;
  259. #endif
  260. default:
  261. /* Shouldn't happen */
  262. break;
  263. }
  264. mbedtls_free(ctx->md_ctx);
  265. }
  266. #if defined(MBEDTLS_MD_C)
  267. if (ctx->hmac_ctx != NULL) {
  268. mbedtls_platform_zeroize(ctx->hmac_ctx,
  269. 2 * ctx->md_info->block_size);
  270. mbedtls_free(ctx->hmac_ctx);
  271. }
  272. #endif
  273. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_md_context_t));
  274. }
  275. int mbedtls_md_clone(mbedtls_md_context_t *dst,
  276. const mbedtls_md_context_t *src)
  277. {
  278. if (dst == NULL || dst->md_info == NULL ||
  279. src == NULL || src->md_info == NULL ||
  280. dst->md_info != src->md_info) {
  281. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  282. }
  283. #if defined(MBEDTLS_MD_SOME_PSA)
  284. if (src->engine != dst->engine) {
  285. /* This can happen with src set to legacy because PSA wasn't ready
  286. * yet, and dst to PSA because it became ready in the meantime.
  287. * We currently don't support that case (we'd need to re-allocate
  288. * md_ctx to the size of the appropriate MD context). */
  289. return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
  290. }
  291. if (src->engine == MBEDTLS_MD_ENGINE_PSA) {
  292. psa_status_t status = psa_hash_clone(src->md_ctx, dst->md_ctx);
  293. return mbedtls_md_error_from_psa(status);
  294. }
  295. #endif
  296. switch (src->md_info->type) {
  297. #if defined(MBEDTLS_MD5_C)
  298. case MBEDTLS_MD_MD5:
  299. mbedtls_md5_clone(dst->md_ctx, src->md_ctx);
  300. break;
  301. #endif
  302. #if defined(MBEDTLS_RIPEMD160_C)
  303. case MBEDTLS_MD_RIPEMD160:
  304. mbedtls_ripemd160_clone(dst->md_ctx, src->md_ctx);
  305. break;
  306. #endif
  307. #if defined(MBEDTLS_SHA1_C)
  308. case MBEDTLS_MD_SHA1:
  309. mbedtls_sha1_clone(dst->md_ctx, src->md_ctx);
  310. break;
  311. #endif
  312. #if defined(MBEDTLS_SHA224_C)
  313. case MBEDTLS_MD_SHA224:
  314. mbedtls_sha256_clone(dst->md_ctx, src->md_ctx);
  315. break;
  316. #endif
  317. #if defined(MBEDTLS_SHA256_C)
  318. case MBEDTLS_MD_SHA256:
  319. mbedtls_sha256_clone(dst->md_ctx, src->md_ctx);
  320. break;
  321. #endif
  322. #if defined(MBEDTLS_SHA384_C)
  323. case MBEDTLS_MD_SHA384:
  324. mbedtls_sha512_clone(dst->md_ctx, src->md_ctx);
  325. break;
  326. #endif
  327. #if defined(MBEDTLS_SHA512_C)
  328. case MBEDTLS_MD_SHA512:
  329. mbedtls_sha512_clone(dst->md_ctx, src->md_ctx);
  330. break;
  331. #endif
  332. default:
  333. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  334. }
  335. return 0;
  336. }
  337. #define ALLOC(type) \
  338. do { \
  339. ctx->md_ctx = mbedtls_calloc(1, sizeof(mbedtls_##type##_context)); \
  340. if (ctx->md_ctx == NULL) \
  341. return MBEDTLS_ERR_MD_ALLOC_FAILED; \
  342. mbedtls_##type##_init(ctx->md_ctx); \
  343. } \
  344. while (0)
  345. int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac)
  346. {
  347. if (md_info == NULL || ctx == NULL) {
  348. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  349. }
  350. ctx->md_info = md_info;
  351. ctx->md_ctx = NULL;
  352. #if defined(MBEDTLS_MD_C)
  353. ctx->hmac_ctx = NULL;
  354. #else
  355. if (hmac != 0) {
  356. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  357. }
  358. #endif
  359. #if defined(MBEDTLS_MD_SOME_PSA)
  360. if (md_can_use_psa(ctx->md_info)) {
  361. ctx->md_ctx = mbedtls_calloc(1, sizeof(psa_hash_operation_t));
  362. if (ctx->md_ctx == NULL) {
  363. return MBEDTLS_ERR_MD_ALLOC_FAILED;
  364. }
  365. ctx->engine = MBEDTLS_MD_ENGINE_PSA;
  366. } else
  367. #endif
  368. switch (md_info->type) {
  369. #if defined(MBEDTLS_MD5_C)
  370. case MBEDTLS_MD_MD5:
  371. ALLOC(md5);
  372. break;
  373. #endif
  374. #if defined(MBEDTLS_RIPEMD160_C)
  375. case MBEDTLS_MD_RIPEMD160:
  376. ALLOC(ripemd160);
  377. break;
  378. #endif
  379. #if defined(MBEDTLS_SHA1_C)
  380. case MBEDTLS_MD_SHA1:
  381. ALLOC(sha1);
  382. break;
  383. #endif
  384. #if defined(MBEDTLS_SHA224_C)
  385. case MBEDTLS_MD_SHA224:
  386. ALLOC(sha256);
  387. break;
  388. #endif
  389. #if defined(MBEDTLS_SHA256_C)
  390. case MBEDTLS_MD_SHA256:
  391. ALLOC(sha256);
  392. break;
  393. #endif
  394. #if defined(MBEDTLS_SHA384_C)
  395. case MBEDTLS_MD_SHA384:
  396. ALLOC(sha512);
  397. break;
  398. #endif
  399. #if defined(MBEDTLS_SHA512_C)
  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 defined(MBEDTLS_MD_C)
  408. if (hmac != 0) {
  409. ctx->hmac_ctx = mbedtls_calloc(2, md_info->block_size);
  410. if (ctx->hmac_ctx == NULL) {
  411. mbedtls_md_free(ctx);
  412. return MBEDTLS_ERR_MD_ALLOC_FAILED;
  413. }
  414. }
  415. #endif
  416. return 0;
  417. }
  418. #undef ALLOC
  419. int mbedtls_md_starts(mbedtls_md_context_t *ctx)
  420. {
  421. if (ctx == NULL || ctx->md_info == NULL) {
  422. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  423. }
  424. #if defined(MBEDTLS_MD_SOME_PSA)
  425. if (ctx->engine == MBEDTLS_MD_ENGINE_PSA) {
  426. psa_algorithm_t alg = psa_alg_of_md(ctx->md_info);
  427. psa_hash_abort(ctx->md_ctx);
  428. psa_status_t status = psa_hash_setup(ctx->md_ctx, alg);
  429. return mbedtls_md_error_from_psa(status);
  430. }
  431. #endif
  432. switch (ctx->md_info->type) {
  433. #if defined(MBEDTLS_MD5_C)
  434. case MBEDTLS_MD_MD5:
  435. return mbedtls_md5_starts(ctx->md_ctx);
  436. #endif
  437. #if defined(MBEDTLS_RIPEMD160_C)
  438. case MBEDTLS_MD_RIPEMD160:
  439. return mbedtls_ripemd160_starts(ctx->md_ctx);
  440. #endif
  441. #if defined(MBEDTLS_SHA1_C)
  442. case MBEDTLS_MD_SHA1:
  443. return mbedtls_sha1_starts(ctx->md_ctx);
  444. #endif
  445. #if defined(MBEDTLS_SHA224_C)
  446. case MBEDTLS_MD_SHA224:
  447. return mbedtls_sha256_starts(ctx->md_ctx, 1);
  448. #endif
  449. #if defined(MBEDTLS_SHA256_C)
  450. case MBEDTLS_MD_SHA256:
  451. return mbedtls_sha256_starts(ctx->md_ctx, 0);
  452. #endif
  453. #if defined(MBEDTLS_SHA384_C)
  454. case MBEDTLS_MD_SHA384:
  455. return mbedtls_sha512_starts(ctx->md_ctx, 1);
  456. #endif
  457. #if defined(MBEDTLS_SHA512_C)
  458. case MBEDTLS_MD_SHA512:
  459. return mbedtls_sha512_starts(ctx->md_ctx, 0);
  460. #endif
  461. default:
  462. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  463. }
  464. }
  465. int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
  466. {
  467. if (ctx == NULL || ctx->md_info == NULL) {
  468. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  469. }
  470. #if defined(MBEDTLS_MD_SOME_PSA)
  471. if (ctx->engine == MBEDTLS_MD_ENGINE_PSA) {
  472. psa_status_t status = psa_hash_update(ctx->md_ctx, input, ilen);
  473. return mbedtls_md_error_from_psa(status);
  474. }
  475. #endif
  476. switch (ctx->md_info->type) {
  477. #if defined(MBEDTLS_MD5_C)
  478. case MBEDTLS_MD_MD5:
  479. return mbedtls_md5_update(ctx->md_ctx, input, ilen);
  480. #endif
  481. #if defined(MBEDTLS_RIPEMD160_C)
  482. case MBEDTLS_MD_RIPEMD160:
  483. return mbedtls_ripemd160_update(ctx->md_ctx, input, ilen);
  484. #endif
  485. #if defined(MBEDTLS_SHA1_C)
  486. case MBEDTLS_MD_SHA1:
  487. return mbedtls_sha1_update(ctx->md_ctx, input, ilen);
  488. #endif
  489. #if defined(MBEDTLS_SHA224_C)
  490. case MBEDTLS_MD_SHA224:
  491. return mbedtls_sha256_update(ctx->md_ctx, input, ilen);
  492. #endif
  493. #if defined(MBEDTLS_SHA256_C)
  494. case MBEDTLS_MD_SHA256:
  495. return mbedtls_sha256_update(ctx->md_ctx, input, ilen);
  496. #endif
  497. #if defined(MBEDTLS_SHA384_C)
  498. case MBEDTLS_MD_SHA384:
  499. return mbedtls_sha512_update(ctx->md_ctx, input, ilen);
  500. #endif
  501. #if defined(MBEDTLS_SHA512_C)
  502. case MBEDTLS_MD_SHA512:
  503. return mbedtls_sha512_update(ctx->md_ctx, input, ilen);
  504. #endif
  505. default:
  506. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  507. }
  508. }
  509. int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output)
  510. {
  511. if (ctx == NULL || ctx->md_info == NULL) {
  512. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  513. }
  514. #if defined(MBEDTLS_MD_SOME_PSA)
  515. if (ctx->engine == MBEDTLS_MD_ENGINE_PSA) {
  516. size_t size = ctx->md_info->size;
  517. psa_status_t status = psa_hash_finish(ctx->md_ctx,
  518. output, size, &size);
  519. return mbedtls_md_error_from_psa(status);
  520. }
  521. #endif
  522. switch (ctx->md_info->type) {
  523. #if defined(MBEDTLS_MD5_C)
  524. case MBEDTLS_MD_MD5:
  525. return mbedtls_md5_finish(ctx->md_ctx, output);
  526. #endif
  527. #if defined(MBEDTLS_RIPEMD160_C)
  528. case MBEDTLS_MD_RIPEMD160:
  529. return mbedtls_ripemd160_finish(ctx->md_ctx, output);
  530. #endif
  531. #if defined(MBEDTLS_SHA1_C)
  532. case MBEDTLS_MD_SHA1:
  533. return mbedtls_sha1_finish(ctx->md_ctx, output);
  534. #endif
  535. #if defined(MBEDTLS_SHA224_C)
  536. case MBEDTLS_MD_SHA224:
  537. return mbedtls_sha256_finish(ctx->md_ctx, output);
  538. #endif
  539. #if defined(MBEDTLS_SHA256_C)
  540. case MBEDTLS_MD_SHA256:
  541. return mbedtls_sha256_finish(ctx->md_ctx, output);
  542. #endif
  543. #if defined(MBEDTLS_SHA384_C)
  544. case MBEDTLS_MD_SHA384:
  545. return mbedtls_sha512_finish(ctx->md_ctx, output);
  546. #endif
  547. #if defined(MBEDTLS_SHA512_C)
  548. case MBEDTLS_MD_SHA512:
  549. return mbedtls_sha512_finish(ctx->md_ctx, output);
  550. #endif
  551. default:
  552. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  553. }
  554. }
  555. int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  556. unsigned char *output)
  557. {
  558. if (md_info == NULL) {
  559. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  560. }
  561. #if defined(MBEDTLS_MD_SOME_PSA)
  562. if (md_can_use_psa(md_info)) {
  563. size_t size = md_info->size;
  564. psa_status_t status = psa_hash_compute(psa_alg_of_md(md_info),
  565. input, ilen,
  566. output, size, &size);
  567. return mbedtls_md_error_from_psa(status);
  568. }
  569. #endif
  570. switch (md_info->type) {
  571. #if defined(MBEDTLS_MD5_C)
  572. case MBEDTLS_MD_MD5:
  573. return mbedtls_md5(input, ilen, output);
  574. #endif
  575. #if defined(MBEDTLS_RIPEMD160_C)
  576. case MBEDTLS_MD_RIPEMD160:
  577. return mbedtls_ripemd160(input, ilen, output);
  578. #endif
  579. #if defined(MBEDTLS_SHA1_C)
  580. case MBEDTLS_MD_SHA1:
  581. return mbedtls_sha1(input, ilen, output);
  582. #endif
  583. #if defined(MBEDTLS_SHA224_C)
  584. case MBEDTLS_MD_SHA224:
  585. return mbedtls_sha256(input, ilen, output, 1);
  586. #endif
  587. #if defined(MBEDTLS_SHA256_C)
  588. case MBEDTLS_MD_SHA256:
  589. return mbedtls_sha256(input, ilen, output, 0);
  590. #endif
  591. #if defined(MBEDTLS_SHA384_C)
  592. case MBEDTLS_MD_SHA384:
  593. return mbedtls_sha512(input, ilen, output, 1);
  594. #endif
  595. #if defined(MBEDTLS_SHA512_C)
  596. case MBEDTLS_MD_SHA512:
  597. return mbedtls_sha512(input, ilen, output, 0);
  598. #endif
  599. default:
  600. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  601. }
  602. }
  603. unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
  604. {
  605. if (md_info == NULL) {
  606. return 0;
  607. }
  608. return md_info->size;
  609. }
  610. mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
  611. {
  612. if (md_info == NULL) {
  613. return MBEDTLS_MD_NONE;
  614. }
  615. return md_info->type;
  616. }
  617. /************************************************************************
  618. * Functions above this separator are part of MBEDTLS_MD_LIGHT, *
  619. * functions below are only available when MBEDTLS_MD_C is set. *
  620. ************************************************************************/
  621. #if defined(MBEDTLS_MD_C)
  622. /*
  623. * Reminder: update profiles in x509_crt.c when adding a new hash!
  624. */
  625. static const int supported_digests[] = {
  626. #if defined(MBEDTLS_MD_CAN_SHA512)
  627. MBEDTLS_MD_SHA512,
  628. #endif
  629. #if defined(MBEDTLS_MD_CAN_SHA384)
  630. MBEDTLS_MD_SHA384,
  631. #endif
  632. #if defined(MBEDTLS_MD_CAN_SHA256)
  633. MBEDTLS_MD_SHA256,
  634. #endif
  635. #if defined(MBEDTLS_MD_CAN_SHA224)
  636. MBEDTLS_MD_SHA224,
  637. #endif
  638. #if defined(MBEDTLS_MD_CAN_SHA1)
  639. MBEDTLS_MD_SHA1,
  640. #endif
  641. #if defined(MBEDTLS_MD_CAN_RIPEMD160)
  642. MBEDTLS_MD_RIPEMD160,
  643. #endif
  644. #if defined(MBEDTLS_MD_CAN_MD5)
  645. MBEDTLS_MD_MD5,
  646. #endif
  647. MBEDTLS_MD_NONE
  648. };
  649. const int *mbedtls_md_list(void)
  650. {
  651. return supported_digests;
  652. }
  653. const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
  654. {
  655. if (NULL == md_name) {
  656. return NULL;
  657. }
  658. /* Get the appropriate digest information */
  659. #if defined(MBEDTLS_MD_CAN_MD5)
  660. if (!strcmp("MD5", md_name)) {
  661. return mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
  662. }
  663. #endif
  664. #if defined(MBEDTLS_MD_CAN_RIPEMD160)
  665. if (!strcmp("RIPEMD160", md_name)) {
  666. return mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160);
  667. }
  668. #endif
  669. #if defined(MBEDTLS_MD_CAN_SHA1)
  670. if (!strcmp("SHA1", md_name) || !strcmp("SHA", md_name)) {
  671. return mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
  672. }
  673. #endif
  674. #if defined(MBEDTLS_MD_CAN_SHA224)
  675. if (!strcmp("SHA224", md_name)) {
  676. return mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
  677. }
  678. #endif
  679. #if defined(MBEDTLS_MD_CAN_SHA256)
  680. if (!strcmp("SHA256", md_name)) {
  681. return mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
  682. }
  683. #endif
  684. #if defined(MBEDTLS_MD_CAN_SHA384)
  685. if (!strcmp("SHA384", md_name)) {
  686. return mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
  687. }
  688. #endif
  689. #if defined(MBEDTLS_MD_CAN_SHA512)
  690. if (!strcmp("SHA512", md_name)) {
  691. return mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
  692. }
  693. #endif
  694. return NULL;
  695. }
  696. const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
  697. const mbedtls_md_context_t *ctx)
  698. {
  699. if (ctx == NULL) {
  700. return NULL;
  701. }
  702. return ctx->MBEDTLS_PRIVATE(md_info);
  703. }
  704. #if defined(MBEDTLS_FS_IO)
  705. int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output)
  706. {
  707. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  708. FILE *f;
  709. size_t n;
  710. mbedtls_md_context_t ctx;
  711. unsigned char buf[1024];
  712. if (md_info == NULL) {
  713. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  714. }
  715. if ((f = fopen(path, "rb")) == NULL) {
  716. return MBEDTLS_ERR_MD_FILE_IO_ERROR;
  717. }
  718. /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
  719. mbedtls_setbuf(f, NULL);
  720. mbedtls_md_init(&ctx);
  721. if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
  722. goto cleanup;
  723. }
  724. if ((ret = mbedtls_md_starts(&ctx)) != 0) {
  725. goto cleanup;
  726. }
  727. while ((n = fread(buf, 1, sizeof(buf), f)) > 0) {
  728. if ((ret = mbedtls_md_update(&ctx, buf, n)) != 0) {
  729. goto cleanup;
  730. }
  731. }
  732. if (ferror(f) != 0) {
  733. ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
  734. } else {
  735. ret = mbedtls_md_finish(&ctx, output);
  736. }
  737. cleanup:
  738. mbedtls_platform_zeroize(buf, sizeof(buf));
  739. fclose(f);
  740. mbedtls_md_free(&ctx);
  741. return ret;
  742. }
  743. #endif /* MBEDTLS_FS_IO */
  744. int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen)
  745. {
  746. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  747. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  748. unsigned char *ipad, *opad;
  749. if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL) {
  750. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  751. }
  752. if (keylen > (size_t) ctx->md_info->block_size) {
  753. if ((ret = mbedtls_md_starts(ctx)) != 0) {
  754. goto cleanup;
  755. }
  756. if ((ret = mbedtls_md_update(ctx, key, keylen)) != 0) {
  757. goto cleanup;
  758. }
  759. if ((ret = mbedtls_md_finish(ctx, sum)) != 0) {
  760. goto cleanup;
  761. }
  762. keylen = ctx->md_info->size;
  763. key = sum;
  764. }
  765. ipad = (unsigned char *) ctx->hmac_ctx;
  766. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  767. memset(ipad, 0x36, ctx->md_info->block_size);
  768. memset(opad, 0x5C, ctx->md_info->block_size);
  769. mbedtls_xor(ipad, ipad, key, keylen);
  770. mbedtls_xor(opad, opad, key, keylen);
  771. if ((ret = mbedtls_md_starts(ctx)) != 0) {
  772. goto cleanup;
  773. }
  774. if ((ret = mbedtls_md_update(ctx, ipad,
  775. ctx->md_info->block_size)) != 0) {
  776. goto cleanup;
  777. }
  778. cleanup:
  779. mbedtls_platform_zeroize(sum, sizeof(sum));
  780. return ret;
  781. }
  782. int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
  783. {
  784. if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL) {
  785. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  786. }
  787. return mbedtls_md_update(ctx, input, ilen);
  788. }
  789. int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
  790. {
  791. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  792. unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
  793. unsigned char *opad;
  794. if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL) {
  795. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  796. }
  797. opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
  798. if ((ret = mbedtls_md_finish(ctx, tmp)) != 0) {
  799. return ret;
  800. }
  801. if ((ret = mbedtls_md_starts(ctx)) != 0) {
  802. return ret;
  803. }
  804. if ((ret = mbedtls_md_update(ctx, opad,
  805. ctx->md_info->block_size)) != 0) {
  806. return ret;
  807. }
  808. if ((ret = mbedtls_md_update(ctx, tmp,
  809. ctx->md_info->size)) != 0) {
  810. return ret;
  811. }
  812. return mbedtls_md_finish(ctx, output);
  813. }
  814. int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
  815. {
  816. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  817. unsigned char *ipad;
  818. if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL) {
  819. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  820. }
  821. ipad = (unsigned char *) ctx->hmac_ctx;
  822. if ((ret = mbedtls_md_starts(ctx)) != 0) {
  823. return ret;
  824. }
  825. return mbedtls_md_update(ctx, ipad, ctx->md_info->block_size);
  826. }
  827. int mbedtls_md_hmac(const mbedtls_md_info_t *md_info,
  828. const unsigned char *key, size_t keylen,
  829. const unsigned char *input, size_t ilen,
  830. unsigned char *output)
  831. {
  832. mbedtls_md_context_t ctx;
  833. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  834. if (md_info == NULL) {
  835. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  836. }
  837. mbedtls_md_init(&ctx);
  838. if ((ret = mbedtls_md_setup(&ctx, md_info, 1)) != 0) {
  839. goto cleanup;
  840. }
  841. if ((ret = mbedtls_md_hmac_starts(&ctx, key, keylen)) != 0) {
  842. goto cleanup;
  843. }
  844. if ((ret = mbedtls_md_hmac_update(&ctx, input, ilen)) != 0) {
  845. goto cleanup;
  846. }
  847. if ((ret = mbedtls_md_hmac_finish(&ctx, output)) != 0) {
  848. goto cleanup;
  849. }
  850. cleanup:
  851. mbedtls_md_free(&ctx);
  852. return ret;
  853. }
  854. const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
  855. {
  856. if (md_info == NULL) {
  857. return NULL;
  858. }
  859. return md_info->name;
  860. }
  861. #endif /* MBEDTLS_MD_C */
  862. #endif /* MBEDTLS_MD_LIGHT */