blob: f95aa6b617f0efb1a5380d39b8957c26c11e6456 [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
20#if !defined(MBEDTLS_CONFIG_FILE)
21#include "mbedtls/config.h"
22#else
23#include MBEDTLS_CONFIG_FILE
24#endif
25
26#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
27#include "psa_crypto_hash.h"
28
29#include "test/drivers/hash.h"
30
Ronald Cron7f13fa22021-04-13 12:41:34 +020031mbedtls_test_driver_hash_hooks_t
32 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cronfa036c82021-03-23 09:33:25 +010033
Ronald Cron7f13fa22021-04-13 12:41:34 +020034psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cronfa036c82021-03-23 09:33:25 +010035 psa_algorithm_t alg,
36 const uint8_t *input, size_t input_length,
37 uint8_t *hash, size_t hash_size, size_t *hash_length )
38{
Ronald Cron7f13fa22021-04-13 12:41:34 +020039 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010040
Ronald Cron7f13fa22021-04-13 12:41:34 +020041 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010042 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020043 mbedtls_test_driver_hash_hooks.driver_status =
44 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010045 }
46 else
47 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020048 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010049 mbedtls_transparent_test_driver_hash_compute(
50 alg, input, input_length,
51 hash, hash_size, hash_length );
52 }
53
Ronald Cron7f13fa22021-04-13 12:41:34 +020054 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010055}
56
Ronald Cron7f13fa22021-04-13 12:41:34 +020057psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cronfa036c82021-03-23 09:33:25 +010058 mbedtls_transparent_test_driver_hash_operation_t *operation,
59 psa_algorithm_t alg )
60{
Ronald Cron7f13fa22021-04-13 12:41:34 +020061 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010062
Ronald Cron7f13fa22021-04-13 12:41:34 +020063 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010064 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020065 mbedtls_test_driver_hash_hooks.driver_status =
66 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010067 }
68 else
69 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020070 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010071 mbedtls_transparent_test_driver_hash_setup( operation, alg );
72 }
73
Ronald Cron7f13fa22021-04-13 12:41:34 +020074 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010075}
76
Ronald Cron7f13fa22021-04-13 12:41:34 +020077psa_status_t mbedtls_test_transparent_hash_clone(
Ronald Cronfa036c82021-03-23 09:33:25 +010078 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
79 mbedtls_transparent_test_driver_hash_operation_t *target_operation )
80{
Ronald Cron7f13fa22021-04-13 12:41:34 +020081 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010082
Ronald Cron7f13fa22021-04-13 12:41:34 +020083 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010084 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020085 mbedtls_test_driver_hash_hooks.driver_status =
86 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010087 }
88 else
89 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020090 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010091 mbedtls_transparent_test_driver_hash_clone( source_operation,
92 target_operation );
93 }
94
Ronald Cron7f13fa22021-04-13 12:41:34 +020095 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010096}
97
Ronald Cron7f13fa22021-04-13 12:41:34 +020098psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cronfa036c82021-03-23 09:33:25 +010099 mbedtls_transparent_test_driver_hash_operation_t *operation,
100 const uint8_t *input,
101 size_t input_length )
102{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200103 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100104
Ronald Cron7f13fa22021-04-13 12:41:34 +0200105 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100106 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200107 mbedtls_test_driver_hash_hooks.driver_status =
108 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100109 }
110 else
111 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200112 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100113 mbedtls_transparent_test_driver_hash_update(
114 operation, input, input_length );
115 }
116
Ronald Cron7f13fa22021-04-13 12:41:34 +0200117 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100118}
119
Ronald Cron7f13fa22021-04-13 12:41:34 +0200120psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cronfa036c82021-03-23 09:33:25 +0100121 mbedtls_transparent_test_driver_hash_operation_t *operation,
122 uint8_t *hash,
123 size_t hash_size,
124 size_t *hash_length )
125{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200126 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100127
Ronald Cron7f13fa22021-04-13 12:41:34 +0200128 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100129 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200130 mbedtls_test_driver_hash_hooks.driver_status =
131 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100132 }
133 else
134 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200135 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100136 mbedtls_transparent_test_driver_hash_finish(
137 operation, hash, hash_size, hash_length );
138 }
139
Ronald Cron7f13fa22021-04-13 12:41:34 +0200140 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100141}
142
Ronald Cron7f13fa22021-04-13 12:41:34 +0200143psa_status_t mbedtls_test_transparent_hash_abort(
Ronald Cronfa036c82021-03-23 09:33:25 +0100144 mbedtls_transparent_test_driver_hash_operation_t *operation )
145{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200146 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100147
Ronald Cron7f13fa22021-04-13 12:41:34 +0200148 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100149 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200150 mbedtls_test_driver_hash_hooks.driver_status =
151 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100152 }
153 else
154 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200155 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100156 mbedtls_transparent_test_driver_hash_abort( operation );
157 }
158
Ronald Cron7f13fa22021-04-13 12:41:34 +0200159 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100160}
161#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */