blob: b1880f778e828e5ca5007ff0093f0a3f52bcacce [file] [log] [blame]
Ronald Cronfa036c82021-03-23 09:33:25 +01001/*
2 * Test driver for hash entry points.
3 */
4/* Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Mateusz Starzykb4a01292021-05-27 14:49:25 +020020#include <test/helpers.h>
Ronald Cronfa036c82021-03-23 09:33:25 +010021
22#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
23#include "psa_crypto_hash.h"
24
25#include "test/drivers/hash.h"
26
Ronald Cron7f13fa22021-04-13 12:41:34 +020027mbedtls_test_driver_hash_hooks_t
28 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cronfa036c82021-03-23 09:33:25 +010029
Ronald Cron7f13fa22021-04-13 12:41:34 +020030psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cronfa036c82021-03-23 09:33:25 +010031 psa_algorithm_t alg,
32 const uint8_t *input, size_t input_length,
33 uint8_t *hash, size_t hash_size, size_t *hash_length )
34{
Ronald Cron7f13fa22021-04-13 12:41:34 +020035 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010036
Ronald Cron7f13fa22021-04-13 12:41:34 +020037 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010038 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020039 mbedtls_test_driver_hash_hooks.driver_status =
40 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010041 }
42 else
43 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020044 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010045 mbedtls_transparent_test_driver_hash_compute(
46 alg, input, input_length,
47 hash, hash_size, hash_length );
48 }
49
Ronald Cron7f13fa22021-04-13 12:41:34 +020050 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010051}
52
Ronald Cron7f13fa22021-04-13 12:41:34 +020053psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cronfa036c82021-03-23 09:33:25 +010054 mbedtls_transparent_test_driver_hash_operation_t *operation,
55 psa_algorithm_t alg )
56{
Ronald Cron7f13fa22021-04-13 12:41:34 +020057 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010058
Ronald Cron7f13fa22021-04-13 12:41:34 +020059 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010060 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020061 mbedtls_test_driver_hash_hooks.driver_status =
62 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010063 }
64 else
65 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020066 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010067 mbedtls_transparent_test_driver_hash_setup( operation, alg );
68 }
69
Ronald Cron7f13fa22021-04-13 12:41:34 +020070 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010071}
72
Ronald Cron7f13fa22021-04-13 12:41:34 +020073psa_status_t mbedtls_test_transparent_hash_clone(
Ronald Cronfa036c82021-03-23 09:33:25 +010074 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
75 mbedtls_transparent_test_driver_hash_operation_t *target_operation )
76{
Ronald Cron7f13fa22021-04-13 12:41:34 +020077 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010078
Ronald Cron7f13fa22021-04-13 12:41:34 +020079 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010080 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020081 mbedtls_test_driver_hash_hooks.driver_status =
82 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010083 }
84 else
85 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020086 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010087 mbedtls_transparent_test_driver_hash_clone( source_operation,
88 target_operation );
89 }
90
Ronald Cron7f13fa22021-04-13 12:41:34 +020091 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010092}
93
Ronald Cron7f13fa22021-04-13 12:41:34 +020094psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cronfa036c82021-03-23 09:33:25 +010095 mbedtls_transparent_test_driver_hash_operation_t *operation,
96 const uint8_t *input,
97 size_t input_length )
98{
Ronald Cron7f13fa22021-04-13 12:41:34 +020099 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100100
Ronald Cron7f13fa22021-04-13 12:41:34 +0200101 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100102 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200103 mbedtls_test_driver_hash_hooks.driver_status =
104 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100105 }
106 else
107 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200108 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100109 mbedtls_transparent_test_driver_hash_update(
110 operation, input, input_length );
111 }
112
Ronald Cron7f13fa22021-04-13 12:41:34 +0200113 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100114}
115
Ronald Cron7f13fa22021-04-13 12:41:34 +0200116psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cronfa036c82021-03-23 09:33:25 +0100117 mbedtls_transparent_test_driver_hash_operation_t *operation,
118 uint8_t *hash,
119 size_t hash_size,
120 size_t *hash_length )
121{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200122 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100123
Ronald Cron7f13fa22021-04-13 12:41:34 +0200124 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100125 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200126 mbedtls_test_driver_hash_hooks.driver_status =
127 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100128 }
129 else
130 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200131 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100132 mbedtls_transparent_test_driver_hash_finish(
133 operation, hash, hash_size, hash_length );
134 }
135
Ronald Cron7f13fa22021-04-13 12:41:34 +0200136 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100137}
138
Ronald Cron7f13fa22021-04-13 12:41:34 +0200139psa_status_t mbedtls_test_transparent_hash_abort(
Ronald Cronfa036c82021-03-23 09:33:25 +0100140 mbedtls_transparent_test_driver_hash_operation_t *operation )
141{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200142 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100143
Ronald Cron7f13fa22021-04-13 12:41:34 +0200144 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100145 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200146 mbedtls_test_driver_hash_hooks.driver_status =
147 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100148 }
149 else
150 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200151 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100152 mbedtls_transparent_test_driver_hash_abort( operation );
153 }
154
Ronald Cron7f13fa22021-04-13 12:41:34 +0200155 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100156}
157#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */