psa_crypto_hash.c 21 KB

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