psa_crypto_hash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * PSA hashing layer on top of Mbed TLS software crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. */
  8. #include "common.h"
  9. #if defined(MBEDTLS_PSA_CRYPTO_C)
  10. #include <psa/crypto.h>
  11. #include "psa_crypto_core.h"
  12. #include "psa_crypto_hash.h"
  13. #include <mbedtls/error.h>
  14. #include <string.h>
  15. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  16. psa_status_t mbedtls_psa_hash_abort(
  17. mbedtls_psa_hash_operation_t *operation)
  18. {
  19. switch (operation->alg) {
  20. case 0:
  21. /* The object has (apparently) been initialized but it is not
  22. * in use. It's ok to call abort on such an object, and there's
  23. * nothing to do. */
  24. break;
  25. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  26. case PSA_ALG_MD5:
  27. mbedtls_md5_free(&operation->ctx.md5);
  28. break;
  29. #endif
  30. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  31. case PSA_ALG_RIPEMD160:
  32. mbedtls_ripemd160_free(&operation->ctx.ripemd160);
  33. break;
  34. #endif
  35. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  36. case PSA_ALG_SHA_1:
  37. mbedtls_sha1_free(&operation->ctx.sha1);
  38. break;
  39. #endif
  40. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  41. case PSA_ALG_SHA_224:
  42. mbedtls_sha256_free(&operation->ctx.sha256);
  43. break;
  44. #endif
  45. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  46. case PSA_ALG_SHA_256:
  47. mbedtls_sha256_free(&operation->ctx.sha256);
  48. break;
  49. #endif
  50. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  51. case PSA_ALG_SHA_384:
  52. mbedtls_sha512_free(&operation->ctx.sha512);
  53. break;
  54. #endif
  55. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  56. case PSA_ALG_SHA_512:
  57. mbedtls_sha512_free(&operation->ctx.sha512);
  58. break;
  59. #endif
  60. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
  61. case PSA_ALG_SHA3_224:
  62. #endif
  63. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
  64. case PSA_ALG_SHA3_256:
  65. #endif
  66. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
  67. case PSA_ALG_SHA3_384:
  68. #endif
  69. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  70. case PSA_ALG_SHA3_512:
  71. #endif
  72. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
  73. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
  74. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
  75. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  76. mbedtls_sha3_free(&operation->ctx.sha3);
  77. break;
  78. #endif
  79. default:
  80. return PSA_ERROR_BAD_STATE;
  81. }
  82. operation->alg = 0;
  83. return PSA_SUCCESS;
  84. }
  85. psa_status_t mbedtls_psa_hash_setup(
  86. mbedtls_psa_hash_operation_t *operation,
  87. psa_algorithm_t alg)
  88. {
  89. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  90. /* A context must be freshly initialized before it can be set up. */
  91. if (operation->alg != 0) {
  92. return PSA_ERROR_BAD_STATE;
  93. }
  94. switch (alg) {
  95. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  96. case PSA_ALG_MD5:
  97. mbedtls_md5_init(&operation->ctx.md5);
  98. ret = mbedtls_md5_starts(&operation->ctx.md5);
  99. break;
  100. #endif
  101. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  102. case PSA_ALG_RIPEMD160:
  103. mbedtls_ripemd160_init(&operation->ctx.ripemd160);
  104. ret = mbedtls_ripemd160_starts(&operation->ctx.ripemd160);
  105. break;
  106. #endif
  107. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  108. case PSA_ALG_SHA_1:
  109. mbedtls_sha1_init(&operation->ctx.sha1);
  110. ret = mbedtls_sha1_starts(&operation->ctx.sha1);
  111. break;
  112. #endif
  113. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  114. case PSA_ALG_SHA_224:
  115. mbedtls_sha256_init(&operation->ctx.sha256);
  116. ret = mbedtls_sha256_starts(&operation->ctx.sha256, 1);
  117. break;
  118. #endif
  119. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  120. case PSA_ALG_SHA_256:
  121. mbedtls_sha256_init(&operation->ctx.sha256);
  122. ret = mbedtls_sha256_starts(&operation->ctx.sha256, 0);
  123. break;
  124. #endif
  125. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  126. case PSA_ALG_SHA_384:
  127. mbedtls_sha512_init(&operation->ctx.sha512);
  128. ret = mbedtls_sha512_starts(&operation->ctx.sha512, 1);
  129. break;
  130. #endif
  131. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  132. case PSA_ALG_SHA_512:
  133. mbedtls_sha512_init(&operation->ctx.sha512);
  134. ret = mbedtls_sha512_starts(&operation->ctx.sha512, 0);
  135. break;
  136. #endif
  137. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
  138. case PSA_ALG_SHA3_224:
  139. mbedtls_sha3_init(&operation->ctx.sha3);
  140. ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_224);
  141. break;
  142. #endif
  143. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
  144. case PSA_ALG_SHA3_256:
  145. mbedtls_sha3_init(&operation->ctx.sha3);
  146. ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_256);
  147. break;
  148. #endif
  149. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
  150. case PSA_ALG_SHA3_384:
  151. mbedtls_sha3_init(&operation->ctx.sha3);
  152. ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_384);
  153. break;
  154. #endif
  155. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  156. case PSA_ALG_SHA3_512:
  157. mbedtls_sha3_init(&operation->ctx.sha3);
  158. ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_512);
  159. break;
  160. #endif
  161. default:
  162. return PSA_ALG_IS_HASH(alg) ?
  163. PSA_ERROR_NOT_SUPPORTED :
  164. PSA_ERROR_INVALID_ARGUMENT;
  165. }
  166. if (ret == 0) {
  167. operation->alg = alg;
  168. } else {
  169. mbedtls_psa_hash_abort(operation);
  170. }
  171. return mbedtls_to_psa_error(ret);
  172. }
  173. psa_status_t mbedtls_psa_hash_clone(
  174. const mbedtls_psa_hash_operation_t *source_operation,
  175. mbedtls_psa_hash_operation_t *target_operation)
  176. {
  177. switch (source_operation->alg) {
  178. case 0:
  179. return PSA_ERROR_BAD_STATE;
  180. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  181. case PSA_ALG_MD5:
  182. mbedtls_md5_clone(&target_operation->ctx.md5,
  183. &source_operation->ctx.md5);
  184. break;
  185. #endif
  186. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  187. case PSA_ALG_RIPEMD160:
  188. mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160,
  189. &source_operation->ctx.ripemd160);
  190. break;
  191. #endif
  192. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  193. case PSA_ALG_SHA_1:
  194. mbedtls_sha1_clone(&target_operation->ctx.sha1,
  195. &source_operation->ctx.sha1);
  196. break;
  197. #endif
  198. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  199. case PSA_ALG_SHA_224:
  200. mbedtls_sha256_clone(&target_operation->ctx.sha256,
  201. &source_operation->ctx.sha256);
  202. break;
  203. #endif
  204. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  205. case PSA_ALG_SHA_256:
  206. mbedtls_sha256_clone(&target_operation->ctx.sha256,
  207. &source_operation->ctx.sha256);
  208. break;
  209. #endif
  210. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  211. case PSA_ALG_SHA_384:
  212. mbedtls_sha512_clone(&target_operation->ctx.sha512,
  213. &source_operation->ctx.sha512);
  214. break;
  215. #endif
  216. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  217. case PSA_ALG_SHA_512:
  218. mbedtls_sha512_clone(&target_operation->ctx.sha512,
  219. &source_operation->ctx.sha512);
  220. break;
  221. #endif
  222. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
  223. case PSA_ALG_SHA3_224:
  224. #endif
  225. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
  226. case PSA_ALG_SHA3_256:
  227. #endif
  228. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
  229. case PSA_ALG_SHA3_384:
  230. #endif
  231. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  232. case PSA_ALG_SHA3_512:
  233. #endif
  234. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
  235. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
  236. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
  237. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  238. mbedtls_sha3_clone(&target_operation->ctx.sha3,
  239. &source_operation->ctx.sha3);
  240. break;
  241. #endif
  242. default:
  243. (void) source_operation;
  244. (void) target_operation;
  245. return PSA_ERROR_NOT_SUPPORTED;
  246. }
  247. target_operation->alg = source_operation->alg;
  248. return PSA_SUCCESS;
  249. }
  250. psa_status_t mbedtls_psa_hash_update(
  251. mbedtls_psa_hash_operation_t *operation,
  252. const uint8_t *input,
  253. size_t input_length)
  254. {
  255. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  256. switch (operation->alg) {
  257. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  258. case PSA_ALG_MD5:
  259. ret = mbedtls_md5_update(&operation->ctx.md5,
  260. input, input_length);
  261. break;
  262. #endif
  263. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  264. case PSA_ALG_RIPEMD160:
  265. ret = mbedtls_ripemd160_update(&operation->ctx.ripemd160,
  266. input, input_length);
  267. break;
  268. #endif
  269. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  270. case PSA_ALG_SHA_1:
  271. ret = mbedtls_sha1_update(&operation->ctx.sha1,
  272. input, input_length);
  273. break;
  274. #endif
  275. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  276. case PSA_ALG_SHA_224:
  277. ret = mbedtls_sha256_update(&operation->ctx.sha256,
  278. input, input_length);
  279. break;
  280. #endif
  281. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  282. case PSA_ALG_SHA_256:
  283. ret = mbedtls_sha256_update(&operation->ctx.sha256,
  284. input, input_length);
  285. break;
  286. #endif
  287. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  288. case PSA_ALG_SHA_384:
  289. ret = mbedtls_sha512_update(&operation->ctx.sha512,
  290. input, input_length);
  291. break;
  292. #endif
  293. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  294. case PSA_ALG_SHA_512:
  295. ret = mbedtls_sha512_update(&operation->ctx.sha512,
  296. input, input_length);
  297. break;
  298. #endif
  299. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
  300. case PSA_ALG_SHA3_224:
  301. #endif
  302. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
  303. case PSA_ALG_SHA3_256:
  304. #endif
  305. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
  306. case PSA_ALG_SHA3_384:
  307. #endif
  308. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  309. case PSA_ALG_SHA3_512:
  310. #endif
  311. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
  312. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
  313. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
  314. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  315. ret = mbedtls_sha3_update(&operation->ctx.sha3,
  316. input, input_length);
  317. break;
  318. #endif
  319. default:
  320. (void) input;
  321. (void) input_length;
  322. return PSA_ERROR_BAD_STATE;
  323. }
  324. return mbedtls_to_psa_error(ret);
  325. }
  326. psa_status_t mbedtls_psa_hash_finish(
  327. mbedtls_psa_hash_operation_t *operation,
  328. uint8_t *hash,
  329. size_t hash_size,
  330. size_t *hash_length)
  331. {
  332. psa_status_t status;
  333. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  334. size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg);
  335. /* Fill the output buffer with something that isn't a valid hash
  336. * (barring an attack on the hash and deliberately-crafted input),
  337. * in case the caller doesn't check the return status properly. */
  338. *hash_length = hash_size;
  339. /* If hash_size is 0 then hash may be NULL and then the
  340. * call to memset would have undefined behavior. */
  341. if (hash_size != 0) {
  342. memset(hash, '!', hash_size);
  343. }
  344. if (hash_size < actual_hash_length) {
  345. status = PSA_ERROR_BUFFER_TOO_SMALL;
  346. goto exit;
  347. }
  348. switch (operation->alg) {
  349. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  350. case PSA_ALG_MD5:
  351. ret = mbedtls_md5_finish(&operation->ctx.md5, hash);
  352. break;
  353. #endif
  354. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  355. case PSA_ALG_RIPEMD160:
  356. ret = mbedtls_ripemd160_finish(&operation->ctx.ripemd160, hash);
  357. break;
  358. #endif
  359. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  360. case PSA_ALG_SHA_1:
  361. ret = mbedtls_sha1_finish(&operation->ctx.sha1, hash);
  362. break;
  363. #endif
  364. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  365. case PSA_ALG_SHA_224:
  366. ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
  367. break;
  368. #endif
  369. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  370. case PSA_ALG_SHA_256:
  371. ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
  372. break;
  373. #endif
  374. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  375. case PSA_ALG_SHA_384:
  376. ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
  377. break;
  378. #endif
  379. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  380. case PSA_ALG_SHA_512:
  381. ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
  382. break;
  383. #endif
  384. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
  385. case PSA_ALG_SHA3_224:
  386. #endif
  387. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
  388. case PSA_ALG_SHA3_256:
  389. #endif
  390. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
  391. case PSA_ALG_SHA3_384:
  392. #endif
  393. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  394. case PSA_ALG_SHA3_512:
  395. #endif
  396. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
  397. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
  398. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
  399. defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
  400. ret = mbedtls_sha3_finish(&operation->ctx.sha3, hash, hash_size);
  401. break;
  402. #endif
  403. default:
  404. (void) hash;
  405. return PSA_ERROR_BAD_STATE;
  406. }
  407. status = mbedtls_to_psa_error(ret);
  408. exit:
  409. if (status == PSA_SUCCESS) {
  410. *hash_length = actual_hash_length;
  411. }
  412. return status;
  413. }
  414. psa_status_t mbedtls_psa_hash_compute(
  415. psa_algorithm_t alg,
  416. const uint8_t *input,
  417. size_t input_length,
  418. uint8_t *hash,
  419. size_t hash_size,
  420. size_t *hash_length)
  421. {
  422. mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
  423. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  424. psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
  425. *hash_length = hash_size;
  426. status = mbedtls_psa_hash_setup(&operation, alg);
  427. if (status != PSA_SUCCESS) {
  428. goto exit;
  429. }
  430. status = mbedtls_psa_hash_update(&operation, input, input_length);
  431. if (status != PSA_SUCCESS) {
  432. goto exit;
  433. }
  434. status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length);
  435. if (status != PSA_SUCCESS) {
  436. goto exit;
  437. }
  438. exit:
  439. abort_status = mbedtls_psa_hash_abort(&operation);
  440. if (status == PSA_SUCCESS) {
  441. return abort_status;
  442. } else {
  443. return status;
  444. }
  445. }
  446. #endif /* MBEDTLS_PSA_BUILTIN_HASH */
  447. #endif /* MBEDTLS_PSA_CRYPTO_C */