psa_crypto_hash.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. /* Use builtin defines specific to this compilation unit, since the test driver
  28. * relies on the software driver. */
  29. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) || \
  30. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_MD5) ) )
  31. #define BUILTIN_ALG_MD5 1
  32. #endif
  33. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) || \
  34. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160) ) )
  35. #define BUILTIN_ALG_RIPEMD160 1
  36. #endif
  37. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) || \
  38. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1) ) )
  39. #define BUILTIN_ALG_SHA_1 1
  40. #endif
  41. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) || \
  42. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224) ) )
  43. #define BUILTIN_ALG_SHA_224 1
  44. #endif
  45. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) || \
  46. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256) ) )
  47. #define BUILTIN_ALG_SHA_256 1
  48. #endif
  49. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) || \
  50. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384) ) )
  51. #define BUILTIN_ALG_SHA_384 1
  52. #endif
  53. #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) || \
  54. ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512) ) )
  55. #define BUILTIN_ALG_SHA_512 1
  56. #endif
  57. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
  58. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
  59. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
  60. defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
  61. const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
  62. {
  63. switch( alg )
  64. {
  65. #if defined(MBEDTLS_MD5_C)
  66. case PSA_ALG_MD5:
  67. return( &mbedtls_md5_info );
  68. #endif
  69. #if defined(MBEDTLS_RIPEMD160_C)
  70. case PSA_ALG_RIPEMD160:
  71. return( &mbedtls_ripemd160_info );
  72. #endif
  73. #if defined(MBEDTLS_SHA1_C)
  74. case PSA_ALG_SHA_1:
  75. return( &mbedtls_sha1_info );
  76. #endif
  77. #if defined(MBEDTLS_SHA224_C)
  78. case PSA_ALG_SHA_224:
  79. return( &mbedtls_sha224_info );
  80. #endif
  81. #if defined(MBEDTLS_SHA256_C)
  82. case PSA_ALG_SHA_256:
  83. return( &mbedtls_sha256_info );
  84. #endif
  85. #if defined(MBEDTLS_SHA384_C)
  86. case PSA_ALG_SHA_384:
  87. return( &mbedtls_sha384_info );
  88. #endif
  89. #if defined(MBEDTLS_SHA512_C)
  90. case PSA_ALG_SHA_512:
  91. return( &mbedtls_sha512_info );
  92. #endif
  93. default:
  94. return( NULL );
  95. }
  96. }
  97. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
  98. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
  99. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
  100. * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
  101. /* Implement the PSA driver hash interface on top of mbed TLS if either the
  102. * software driver or the test driver requires it. */
  103. #if defined(MBEDTLS_PSA_BUILTIN_HASH) || defined(PSA_CRYPTO_DRIVER_TEST)
  104. static psa_status_t hash_abort(
  105. mbedtls_psa_hash_operation_t *operation )
  106. {
  107. switch( operation->alg )
  108. {
  109. case 0:
  110. /* The object has (apparently) been initialized but it is not
  111. * in use. It's ok to call abort on such an object, and there's
  112. * nothing to do. */
  113. break;
  114. #if defined(BUILTIN_ALG_MD5)
  115. case PSA_ALG_MD5:
  116. mbedtls_md5_free( &operation->ctx.md5 );
  117. break;
  118. #endif
  119. #if defined(BUILTIN_ALG_RIPEMD160)
  120. case PSA_ALG_RIPEMD160:
  121. mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
  122. break;
  123. #endif
  124. #if defined(BUILTIN_ALG_SHA_1)
  125. case PSA_ALG_SHA_1:
  126. mbedtls_sha1_free( &operation->ctx.sha1 );
  127. break;
  128. #endif
  129. #if defined(BUILTIN_ALG_SHA_224)
  130. case PSA_ALG_SHA_224:
  131. mbedtls_sha256_free( &operation->ctx.sha256 );
  132. break;
  133. #endif
  134. #if defined(BUILTIN_ALG_SHA_256)
  135. case PSA_ALG_SHA_256:
  136. mbedtls_sha256_free( &operation->ctx.sha256 );
  137. break;
  138. #endif
  139. #if defined(BUILTIN_ALG_SHA_384)
  140. case PSA_ALG_SHA_384:
  141. mbedtls_sha512_free( &operation->ctx.sha512 );
  142. break;
  143. #endif
  144. #if defined(BUILTIN_ALG_SHA_512)
  145. case PSA_ALG_SHA_512:
  146. mbedtls_sha512_free( &operation->ctx.sha512 );
  147. break;
  148. #endif
  149. default:
  150. return( PSA_ERROR_BAD_STATE );
  151. }
  152. operation->alg = 0;
  153. return( PSA_SUCCESS );
  154. }
  155. static psa_status_t hash_setup(
  156. mbedtls_psa_hash_operation_t *operation,
  157. psa_algorithm_t alg )
  158. {
  159. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  160. /* A context must be freshly initialized before it can be set up. */
  161. if( operation->alg != 0 )
  162. {
  163. return( PSA_ERROR_BAD_STATE );
  164. }
  165. switch( alg )
  166. {
  167. #if defined(BUILTIN_ALG_MD5)
  168. case PSA_ALG_MD5:
  169. mbedtls_md5_init( &operation->ctx.md5 );
  170. ret = mbedtls_md5_starts( &operation->ctx.md5 );
  171. break;
  172. #endif
  173. #if defined(BUILTIN_ALG_RIPEMD160)
  174. case PSA_ALG_RIPEMD160:
  175. mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
  176. ret = mbedtls_ripemd160_starts( &operation->ctx.ripemd160 );
  177. break;
  178. #endif
  179. #if defined(BUILTIN_ALG_SHA_1)
  180. case PSA_ALG_SHA_1:
  181. mbedtls_sha1_init( &operation->ctx.sha1 );
  182. ret = mbedtls_sha1_starts( &operation->ctx.sha1 );
  183. break;
  184. #endif
  185. #if defined(BUILTIN_ALG_SHA_224)
  186. case PSA_ALG_SHA_224:
  187. mbedtls_sha256_init( &operation->ctx.sha256 );
  188. ret = mbedtls_sha256_starts( &operation->ctx.sha256, 1 );
  189. break;
  190. #endif
  191. #if defined(BUILTIN_ALG_SHA_256)
  192. case PSA_ALG_SHA_256:
  193. mbedtls_sha256_init( &operation->ctx.sha256 );
  194. ret = mbedtls_sha256_starts( &operation->ctx.sha256, 0 );
  195. break;
  196. #endif
  197. #if defined(BUILTIN_ALG_SHA_384)
  198. case PSA_ALG_SHA_384:
  199. mbedtls_sha512_init( &operation->ctx.sha512 );
  200. ret = mbedtls_sha512_starts( &operation->ctx.sha512, 1 );
  201. break;
  202. #endif
  203. #if defined(BUILTIN_ALG_SHA_512)
  204. case PSA_ALG_SHA_512:
  205. mbedtls_sha512_init( &operation->ctx.sha512 );
  206. ret = mbedtls_sha512_starts( &operation->ctx.sha512, 0 );
  207. break;
  208. #endif
  209. default:
  210. return( PSA_ALG_IS_HASH( alg ) ?
  211. PSA_ERROR_NOT_SUPPORTED :
  212. PSA_ERROR_INVALID_ARGUMENT );
  213. }
  214. if( ret == 0 )
  215. operation->alg = alg;
  216. else
  217. hash_abort( operation );
  218. return( mbedtls_to_psa_error( ret ) );
  219. }
  220. static psa_status_t hash_clone(
  221. const mbedtls_psa_hash_operation_t *source_operation,
  222. mbedtls_psa_hash_operation_t *target_operation )
  223. {
  224. switch( source_operation->alg )
  225. {
  226. case 0:
  227. return( PSA_ERROR_BAD_STATE );
  228. #if defined(BUILTIN_ALG_MD5)
  229. case PSA_ALG_MD5:
  230. mbedtls_md5_clone( &target_operation->ctx.md5,
  231. &source_operation->ctx.md5 );
  232. break;
  233. #endif
  234. #if defined(BUILTIN_ALG_RIPEMD160)
  235. case PSA_ALG_RIPEMD160:
  236. mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
  237. &source_operation->ctx.ripemd160 );
  238. break;
  239. #endif
  240. #if defined(BUILTIN_ALG_SHA_1)
  241. case PSA_ALG_SHA_1:
  242. mbedtls_sha1_clone( &target_operation->ctx.sha1,
  243. &source_operation->ctx.sha1 );
  244. break;
  245. #endif
  246. #if defined(BUILTIN_ALG_SHA_224)
  247. case PSA_ALG_SHA_224:
  248. mbedtls_sha256_clone( &target_operation->ctx.sha256,
  249. &source_operation->ctx.sha256 );
  250. break;
  251. #endif
  252. #if defined(BUILTIN_ALG_SHA_256)
  253. case PSA_ALG_SHA_256:
  254. mbedtls_sha256_clone( &target_operation->ctx.sha256,
  255. &source_operation->ctx.sha256 );
  256. break;
  257. #endif
  258. #if defined(BUILTIN_ALG_SHA_384)
  259. case PSA_ALG_SHA_384:
  260. mbedtls_sha512_clone( &target_operation->ctx.sha512,
  261. &source_operation->ctx.sha512 );
  262. break;
  263. #endif
  264. #if defined(BUILTIN_ALG_SHA_512)
  265. case PSA_ALG_SHA_512:
  266. mbedtls_sha512_clone( &target_operation->ctx.sha512,
  267. &source_operation->ctx.sha512 );
  268. break;
  269. #endif
  270. default:
  271. (void) source_operation;
  272. (void) target_operation;
  273. return( PSA_ERROR_NOT_SUPPORTED );
  274. }
  275. target_operation->alg = source_operation->alg;
  276. return( PSA_SUCCESS );
  277. }
  278. static psa_status_t hash_update(
  279. mbedtls_psa_hash_operation_t *operation,
  280. const uint8_t *input,
  281. size_t input_length )
  282. {
  283. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  284. switch( operation->alg )
  285. {
  286. #if defined(BUILTIN_ALG_MD5)
  287. case PSA_ALG_MD5:
  288. ret = mbedtls_md5_update( &operation->ctx.md5,
  289. input, input_length );
  290. break;
  291. #endif
  292. #if defined(BUILTIN_ALG_RIPEMD160)
  293. case PSA_ALG_RIPEMD160:
  294. ret = mbedtls_ripemd160_update( &operation->ctx.ripemd160,
  295. input, input_length );
  296. break;
  297. #endif
  298. #if defined(BUILTIN_ALG_SHA_1)
  299. case PSA_ALG_SHA_1:
  300. ret = mbedtls_sha1_update( &operation->ctx.sha1,
  301. input, input_length );
  302. break;
  303. #endif
  304. #if defined(BUILTIN_ALG_SHA_224)
  305. case PSA_ALG_SHA_224:
  306. ret = mbedtls_sha256_update( &operation->ctx.sha256,
  307. input, input_length );
  308. break;
  309. #endif
  310. #if defined(BUILTIN_ALG_SHA_256)
  311. case PSA_ALG_SHA_256:
  312. ret = mbedtls_sha256_update( &operation->ctx.sha256,
  313. input, input_length );
  314. break;
  315. #endif
  316. #if defined(BUILTIN_ALG_SHA_384)
  317. case PSA_ALG_SHA_384:
  318. ret = mbedtls_sha512_update( &operation->ctx.sha512,
  319. input, input_length );
  320. break;
  321. #endif
  322. #if defined(BUILTIN_ALG_SHA_512)
  323. case PSA_ALG_SHA_512:
  324. ret = mbedtls_sha512_update( &operation->ctx.sha512,
  325. input, input_length );
  326. break;
  327. #endif
  328. default:
  329. (void) input;
  330. (void) input_length;
  331. return( PSA_ERROR_BAD_STATE );
  332. }
  333. return( mbedtls_to_psa_error( ret ) );
  334. }
  335. static psa_status_t hash_finish(
  336. mbedtls_psa_hash_operation_t *operation,
  337. uint8_t *hash,
  338. size_t hash_size,
  339. size_t *hash_length )
  340. {
  341. psa_status_t status;
  342. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  343. size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
  344. /* Fill the output buffer with something that isn't a valid hash
  345. * (barring an attack on the hash and deliberately-crafted input),
  346. * in case the caller doesn't check the return status properly. */
  347. *hash_length = hash_size;
  348. /* If hash_size is 0 then hash may be NULL and then the
  349. * call to memset would have undefined behavior. */
  350. if( hash_size != 0 )
  351. memset( hash, '!', hash_size );
  352. if( hash_size < actual_hash_length )
  353. {
  354. status = PSA_ERROR_BUFFER_TOO_SMALL;
  355. goto exit;
  356. }
  357. switch( operation->alg )
  358. {
  359. #if defined(BUILTIN_ALG_MD5)
  360. case PSA_ALG_MD5:
  361. ret = mbedtls_md5_finish( &operation->ctx.md5, hash );
  362. break;
  363. #endif
  364. #if defined(BUILTIN_ALG_RIPEMD160)
  365. case PSA_ALG_RIPEMD160:
  366. ret = mbedtls_ripemd160_finish( &operation->ctx.ripemd160, hash );
  367. break;
  368. #endif
  369. #if defined(BUILTIN_ALG_SHA_1)
  370. case PSA_ALG_SHA_1:
  371. ret = mbedtls_sha1_finish( &operation->ctx.sha1, hash );
  372. break;
  373. #endif
  374. #if defined(BUILTIN_ALG_SHA_224)
  375. case PSA_ALG_SHA_224:
  376. ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
  377. break;
  378. #endif
  379. #if defined(BUILTIN_ALG_SHA_256)
  380. case PSA_ALG_SHA_256:
  381. ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
  382. break;
  383. #endif
  384. #if defined(BUILTIN_ALG_SHA_384)
  385. case PSA_ALG_SHA_384:
  386. ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
  387. break;
  388. #endif
  389. #if defined(BUILTIN_ALG_SHA_512)
  390. case PSA_ALG_SHA_512:
  391. ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
  392. break;
  393. #endif
  394. default:
  395. (void) hash;
  396. return( PSA_ERROR_BAD_STATE );
  397. }
  398. status = mbedtls_to_psa_error( ret );
  399. exit:
  400. if( status == PSA_SUCCESS )
  401. *hash_length = actual_hash_length;
  402. return( status );
  403. }
  404. static psa_status_t hash_compute(
  405. psa_algorithm_t alg,
  406. const uint8_t *input,
  407. size_t input_length,
  408. uint8_t *hash,
  409. size_t hash_size,
  410. size_t *hash_length)
  411. {
  412. mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
  413. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  414. psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
  415. *hash_length = hash_size;
  416. status = hash_setup( &operation, alg );
  417. if( status != PSA_SUCCESS )
  418. goto exit;
  419. status = hash_update( &operation, input, input_length );
  420. if( status != PSA_SUCCESS )
  421. goto exit;
  422. status = hash_finish( &operation, hash, hash_size, hash_length );
  423. if( status != PSA_SUCCESS )
  424. goto exit;
  425. exit:
  426. abort_status = hash_abort( &operation );
  427. if( status == PSA_SUCCESS )
  428. return( abort_status );
  429. else
  430. return( status );
  431. }
  432. #endif /* MBEDTLS_PSA_BUILTIN_HASH || PSA_CRYPTO_DRIVER_TEST */
  433. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  434. psa_status_t mbedtls_psa_hash_compute(
  435. psa_algorithm_t alg,
  436. const uint8_t *input,
  437. size_t input_length,
  438. uint8_t *hash,
  439. size_t hash_size,
  440. size_t *hash_length)
  441. {
  442. return( hash_compute( alg, input, input_length,
  443. hash, hash_size, hash_length ) );
  444. }
  445. psa_status_t mbedtls_psa_hash_setup(
  446. mbedtls_psa_hash_operation_t *operation,
  447. psa_algorithm_t alg )
  448. {
  449. return( hash_setup( operation, alg ) );
  450. }
  451. psa_status_t mbedtls_psa_hash_clone(
  452. const mbedtls_psa_hash_operation_t *source_operation,
  453. mbedtls_psa_hash_operation_t *target_operation )
  454. {
  455. return( hash_clone( source_operation, target_operation ) );
  456. }
  457. psa_status_t mbedtls_psa_hash_update(
  458. mbedtls_psa_hash_operation_t *operation,
  459. const uint8_t *input,
  460. size_t input_length )
  461. {
  462. return( hash_update( operation, input, input_length ) );
  463. }
  464. psa_status_t mbedtls_psa_hash_finish(
  465. mbedtls_psa_hash_operation_t *operation,
  466. uint8_t *hash,
  467. size_t hash_size,
  468. size_t *hash_length )
  469. {
  470. return( hash_finish( operation, hash, hash_size, hash_length ) );
  471. }
  472. psa_status_t mbedtls_psa_hash_abort(
  473. mbedtls_psa_hash_operation_t *operation )
  474. {
  475. return( hash_abort( operation ) );
  476. }
  477. #endif /* MBEDTLS_PSA_BUILTIN_HASH */
  478. /*
  479. * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
  480. */
  481. #if defined(PSA_CRYPTO_DRIVER_TEST)
  482. static int is_hash_accelerated( psa_algorithm_t alg )
  483. {
  484. switch( alg )
  485. {
  486. #if defined(MBEDTLS_PSA_ACCEL_ALG_MD5)
  487. case PSA_ALG_MD5:
  488. return( 1 );
  489. #endif
  490. #if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160)
  491. case PSA_ALG_RIPEMD160:
  492. return( 1 );
  493. #endif
  494. #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1)
  495. case PSA_ALG_SHA_1:
  496. return( 1 );
  497. #endif
  498. #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224)
  499. case PSA_ALG_SHA_224:
  500. return( 1 );
  501. #endif
  502. #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256)
  503. case PSA_ALG_SHA_256:
  504. return( 1 );
  505. #endif
  506. #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384)
  507. case PSA_ALG_SHA_384:
  508. return( 1 );
  509. #endif
  510. #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512)
  511. case PSA_ALG_SHA_512:
  512. return( 1 );
  513. #endif
  514. default:
  515. return( 0 );
  516. }
  517. }
  518. psa_status_t mbedtls_transparent_test_driver_hash_compute(
  519. psa_algorithm_t alg,
  520. const uint8_t *input,
  521. size_t input_length,
  522. uint8_t *hash,
  523. size_t hash_size,
  524. size_t *hash_length)
  525. {
  526. if( is_hash_accelerated( alg ) )
  527. return( hash_compute( alg, input, input_length,
  528. hash, hash_size, hash_length ) );
  529. else
  530. return( PSA_ERROR_NOT_SUPPORTED );
  531. }
  532. psa_status_t mbedtls_transparent_test_driver_hash_setup(
  533. mbedtls_transparent_test_driver_hash_operation_t *operation,
  534. psa_algorithm_t alg )
  535. {
  536. if( is_hash_accelerated( alg ) )
  537. return( hash_setup( operation, alg ) );
  538. else
  539. return( PSA_ERROR_NOT_SUPPORTED );
  540. }
  541. psa_status_t mbedtls_transparent_test_driver_hash_clone(
  542. const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
  543. mbedtls_transparent_test_driver_hash_operation_t *target_operation )
  544. {
  545. if( is_hash_accelerated( source_operation->alg ) )
  546. return( hash_clone( source_operation, target_operation ) );
  547. else
  548. return( PSA_ERROR_BAD_STATE );
  549. }
  550. psa_status_t mbedtls_transparent_test_driver_hash_update(
  551. mbedtls_transparent_test_driver_hash_operation_t *operation,
  552. const uint8_t *input,
  553. size_t input_length )
  554. {
  555. if( is_hash_accelerated( operation->alg ) )
  556. return( hash_update( operation, input, input_length ) );
  557. else
  558. return( PSA_ERROR_BAD_STATE );
  559. }
  560. psa_status_t mbedtls_transparent_test_driver_hash_finish(
  561. mbedtls_transparent_test_driver_hash_operation_t *operation,
  562. uint8_t *hash,
  563. size_t hash_size,
  564. size_t *hash_length )
  565. {
  566. if( is_hash_accelerated( operation->alg ) )
  567. return( hash_finish( operation, hash, hash_size, hash_length ) );
  568. else
  569. return( PSA_ERROR_BAD_STATE );
  570. }
  571. psa_status_t mbedtls_transparent_test_driver_hash_abort(
  572. mbedtls_transparent_test_driver_hash_operation_t *operation )
  573. {
  574. return( hash_abort( operation ) );
  575. }
  576. #endif /* PSA_CRYPTO_DRIVER_TEST */
  577. #endif /* MBEDTLS_PSA_CRYPTO_C */