psa_crypto_hash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. #include <psa/crypto.h>
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_hash.h"
  25. #include <mbedtls/error.h>
  26. #include <string.h>
  27. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
  28. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
  29. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
  30. defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
  31. const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
  32. {
  33. switch( alg )
  34. {
  35. #if defined(MBEDTLS_MD2_C)
  36. case PSA_ALG_MD2:
  37. return( &mbedtls_md2_info );
  38. #endif
  39. #if defined(MBEDTLS_MD4_C)
  40. case PSA_ALG_MD4:
  41. return( &mbedtls_md4_info );
  42. #endif
  43. #if defined(MBEDTLS_MD5_C)
  44. case PSA_ALG_MD5:
  45. return( &mbedtls_md5_info );
  46. #endif
  47. #if defined(MBEDTLS_RIPEMD160_C)
  48. case PSA_ALG_RIPEMD160:
  49. return( &mbedtls_ripemd160_info );
  50. #endif
  51. #if defined(MBEDTLS_SHA1_C)
  52. case PSA_ALG_SHA_1:
  53. return( &mbedtls_sha1_info );
  54. #endif
  55. #if defined(MBEDTLS_SHA256_C)
  56. case PSA_ALG_SHA_224:
  57. return( &mbedtls_sha224_info );
  58. #endif
  59. #if defined(MBEDTLS_SHA256_C)
  60. case PSA_ALG_SHA_256:
  61. return( &mbedtls_sha256_info );
  62. #endif
  63. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  64. case PSA_ALG_SHA_384:
  65. return( &mbedtls_sha384_info );
  66. #endif
  67. #if defined(MBEDTLS_SHA512_C)
  68. case PSA_ALG_SHA_512:
  69. return( &mbedtls_sha512_info );
  70. #endif
  71. default:
  72. return( NULL );
  73. }
  74. }
  75. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
  76. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
  77. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
  78. * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
  79. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  80. psa_status_t mbedtls_psa_hash_abort(
  81. mbedtls_psa_hash_operation_t *operation )
  82. {
  83. switch( operation->alg )
  84. {
  85. case 0:
  86. /* The object has (apparently) been initialized but it is not
  87. * in use. It's ok to call abort on such an object, and there's
  88. * nothing to do. */
  89. break;
  90. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
  91. case PSA_ALG_MD2:
  92. mbedtls_md2_free( &operation->ctx.md2 );
  93. break;
  94. #endif
  95. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
  96. case PSA_ALG_MD4:
  97. mbedtls_md4_free( &operation->ctx.md4 );
  98. break;
  99. #endif
  100. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  101. case PSA_ALG_MD5:
  102. mbedtls_md5_free( &operation->ctx.md5 );
  103. break;
  104. #endif
  105. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  106. case PSA_ALG_RIPEMD160:
  107. mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
  108. break;
  109. #endif
  110. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  111. case PSA_ALG_SHA_1:
  112. mbedtls_sha1_free( &operation->ctx.sha1 );
  113. break;
  114. #endif
  115. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  116. case PSA_ALG_SHA_224:
  117. mbedtls_sha256_free( &operation->ctx.sha256 );
  118. break;
  119. #endif
  120. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  121. case PSA_ALG_SHA_256:
  122. mbedtls_sha256_free( &operation->ctx.sha256 );
  123. break;
  124. #endif
  125. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  126. case PSA_ALG_SHA_384:
  127. mbedtls_sha512_free( &operation->ctx.sha512 );
  128. break;
  129. #endif
  130. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  131. case PSA_ALG_SHA_512:
  132. mbedtls_sha512_free( &operation->ctx.sha512 );
  133. break;
  134. #endif
  135. default:
  136. return( PSA_ERROR_BAD_STATE );
  137. }
  138. operation->alg = 0;
  139. return( PSA_SUCCESS );
  140. }
  141. psa_status_t mbedtls_psa_hash_setup(
  142. mbedtls_psa_hash_operation_t *operation,
  143. psa_algorithm_t alg )
  144. {
  145. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  146. /* A context must be freshly initialized before it can be set up. */
  147. if( operation->alg != 0 )
  148. {
  149. return( PSA_ERROR_BAD_STATE );
  150. }
  151. switch( alg )
  152. {
  153. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
  154. case PSA_ALG_MD2:
  155. mbedtls_md2_init( &operation->ctx.md2 );
  156. ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
  157. break;
  158. #endif
  159. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
  160. case PSA_ALG_MD4:
  161. mbedtls_md4_init( &operation->ctx.md4 );
  162. ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
  163. break;
  164. #endif
  165. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  166. case PSA_ALG_MD5:
  167. mbedtls_md5_init( &operation->ctx.md5 );
  168. ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
  169. break;
  170. #endif
  171. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  172. case PSA_ALG_RIPEMD160:
  173. mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
  174. ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
  175. break;
  176. #endif
  177. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  178. case PSA_ALG_SHA_1:
  179. mbedtls_sha1_init( &operation->ctx.sha1 );
  180. ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
  181. break;
  182. #endif
  183. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  184. case PSA_ALG_SHA_224:
  185. mbedtls_sha256_init( &operation->ctx.sha256 );
  186. ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
  187. break;
  188. #endif
  189. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  190. case PSA_ALG_SHA_256:
  191. mbedtls_sha256_init( &operation->ctx.sha256 );
  192. ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
  193. break;
  194. #endif
  195. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  196. case PSA_ALG_SHA_384:
  197. mbedtls_sha512_init( &operation->ctx.sha512 );
  198. ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
  199. break;
  200. #endif
  201. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  202. case PSA_ALG_SHA_512:
  203. mbedtls_sha512_init( &operation->ctx.sha512 );
  204. ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
  205. break;
  206. #endif
  207. default:
  208. return( PSA_ALG_IS_HASH( alg ) ?
  209. PSA_ERROR_NOT_SUPPORTED :
  210. PSA_ERROR_INVALID_ARGUMENT );
  211. }
  212. if( ret == 0 )
  213. operation->alg = alg;
  214. else
  215. mbedtls_psa_hash_abort( operation );
  216. return( mbedtls_to_psa_error( ret ) );
  217. }
  218. psa_status_t mbedtls_psa_hash_clone(
  219. const mbedtls_psa_hash_operation_t *source_operation,
  220. mbedtls_psa_hash_operation_t *target_operation )
  221. {
  222. switch( source_operation->alg )
  223. {
  224. case 0:
  225. return( PSA_ERROR_BAD_STATE );
  226. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
  227. case PSA_ALG_MD2:
  228. mbedtls_md2_clone( &target_operation->ctx.md2,
  229. &source_operation->ctx.md2 );
  230. break;
  231. #endif
  232. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
  233. case PSA_ALG_MD4:
  234. mbedtls_md4_clone( &target_operation->ctx.md4,
  235. &source_operation->ctx.md4 );
  236. break;
  237. #endif
  238. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  239. case PSA_ALG_MD5:
  240. mbedtls_md5_clone( &target_operation->ctx.md5,
  241. &source_operation->ctx.md5 );
  242. break;
  243. #endif
  244. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  245. case PSA_ALG_RIPEMD160:
  246. mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
  247. &source_operation->ctx.ripemd160 );
  248. break;
  249. #endif
  250. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  251. case PSA_ALG_SHA_1:
  252. mbedtls_sha1_clone( &target_operation->ctx.sha1,
  253. &source_operation->ctx.sha1 );
  254. break;
  255. #endif
  256. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  257. case PSA_ALG_SHA_224:
  258. mbedtls_sha256_clone( &target_operation->ctx.sha256,
  259. &source_operation->ctx.sha256 );
  260. break;
  261. #endif
  262. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  263. case PSA_ALG_SHA_256:
  264. mbedtls_sha256_clone( &target_operation->ctx.sha256,
  265. &source_operation->ctx.sha256 );
  266. break;
  267. #endif
  268. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  269. case PSA_ALG_SHA_384:
  270. mbedtls_sha512_clone( &target_operation->ctx.sha512,
  271. &source_operation->ctx.sha512 );
  272. break;
  273. #endif
  274. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  275. case PSA_ALG_SHA_512:
  276. mbedtls_sha512_clone( &target_operation->ctx.sha512,
  277. &source_operation->ctx.sha512 );
  278. break;
  279. #endif
  280. default:
  281. (void) source_operation;
  282. (void) target_operation;
  283. return( PSA_ERROR_NOT_SUPPORTED );
  284. }
  285. target_operation->alg = source_operation->alg;
  286. return( PSA_SUCCESS );
  287. }
  288. psa_status_t mbedtls_psa_hash_update(
  289. mbedtls_psa_hash_operation_t *operation,
  290. const uint8_t *input,
  291. size_t input_length )
  292. {
  293. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  294. switch( operation->alg )
  295. {
  296. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
  297. case PSA_ALG_MD2:
  298. ret = mbedtls_md2_update_ret( &operation->ctx.md2,
  299. input, input_length );
  300. break;
  301. #endif
  302. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
  303. case PSA_ALG_MD4:
  304. ret = mbedtls_md4_update_ret( &operation->ctx.md4,
  305. input, input_length );
  306. break;
  307. #endif
  308. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  309. case PSA_ALG_MD5:
  310. ret = mbedtls_md5_update_ret( &operation->ctx.md5,
  311. input, input_length );
  312. break;
  313. #endif
  314. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  315. case PSA_ALG_RIPEMD160:
  316. ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
  317. input, input_length );
  318. break;
  319. #endif
  320. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  321. case PSA_ALG_SHA_1:
  322. ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
  323. input, input_length );
  324. break;
  325. #endif
  326. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  327. case PSA_ALG_SHA_224:
  328. ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
  329. input, input_length );
  330. break;
  331. #endif
  332. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  333. case PSA_ALG_SHA_256:
  334. ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
  335. input, input_length );
  336. break;
  337. #endif
  338. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  339. case PSA_ALG_SHA_384:
  340. ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
  341. input, input_length );
  342. break;
  343. #endif
  344. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  345. case PSA_ALG_SHA_512:
  346. ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
  347. input, input_length );
  348. break;
  349. #endif
  350. default:
  351. (void) input;
  352. (void) input_length;
  353. return( PSA_ERROR_BAD_STATE );
  354. }
  355. return( mbedtls_to_psa_error( ret ) );
  356. }
  357. psa_status_t mbedtls_psa_hash_finish(
  358. mbedtls_psa_hash_operation_t *operation,
  359. uint8_t *hash,
  360. size_t hash_size,
  361. size_t *hash_length )
  362. {
  363. psa_status_t status;
  364. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  365. size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
  366. /* Fill the output buffer with something that isn't a valid hash
  367. * (barring an attack on the hash and deliberately-crafted input),
  368. * in case the caller doesn't check the return status properly. */
  369. *hash_length = hash_size;
  370. /* If hash_size is 0 then hash may be NULL and then the
  371. * call to memset would have undefined behavior. */
  372. if( hash_size != 0 )
  373. memset( hash, '!', hash_size );
  374. if( hash_size < actual_hash_length )
  375. {
  376. status = PSA_ERROR_BUFFER_TOO_SMALL;
  377. goto exit;
  378. }
  379. switch( operation->alg )
  380. {
  381. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
  382. case PSA_ALG_MD2:
  383. ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
  384. break;
  385. #endif
  386. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
  387. case PSA_ALG_MD4:
  388. ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
  389. break;
  390. #endif
  391. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  392. case PSA_ALG_MD5:
  393. ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
  394. break;
  395. #endif
  396. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  397. case PSA_ALG_RIPEMD160:
  398. ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
  399. break;
  400. #endif
  401. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  402. case PSA_ALG_SHA_1:
  403. ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
  404. break;
  405. #endif
  406. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  407. case PSA_ALG_SHA_224:
  408. ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
  409. break;
  410. #endif
  411. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  412. case PSA_ALG_SHA_256:
  413. ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
  414. break;
  415. #endif
  416. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  417. case PSA_ALG_SHA_384:
  418. ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
  419. break;
  420. #endif
  421. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  422. case PSA_ALG_SHA_512:
  423. ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
  424. break;
  425. #endif
  426. default:
  427. (void) hash;
  428. return( PSA_ERROR_BAD_STATE );
  429. }
  430. status = mbedtls_to_psa_error( ret );
  431. exit:
  432. if( status == PSA_SUCCESS )
  433. *hash_length = actual_hash_length;
  434. return( status );
  435. }
  436. psa_status_t mbedtls_psa_hash_compute(
  437. psa_algorithm_t alg,
  438. const uint8_t *input,
  439. size_t input_length,
  440. uint8_t *hash,
  441. size_t hash_size,
  442. size_t *hash_length)
  443. {
  444. mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
  445. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  446. psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
  447. *hash_length = hash_size;
  448. status = mbedtls_psa_hash_setup( &operation, alg );
  449. if( status != PSA_SUCCESS )
  450. goto exit;
  451. status = mbedtls_psa_hash_update( &operation, input, input_length );
  452. if( status != PSA_SUCCESS )
  453. goto exit;
  454. status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
  455. if( status != PSA_SUCCESS )
  456. goto exit;
  457. exit:
  458. abort_status = mbedtls_psa_hash_abort( &operation );
  459. if( status == PSA_SUCCESS )
  460. return( abort_status );
  461. else
  462. return( status );
  463. }
  464. #endif /* MBEDTLS_PSA_BUILTIN_HASH */
  465. #endif /* MBEDTLS_PSA_CRYPTO_C */