blob: a9c475a8257e29b1cacbffbce58ef80adea9b19d [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 Starzykf2b11a92021-05-21 09:33:46 +020020#define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
Ronald Cronfa036c82021-03-23 09:33:25 +010022#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
29#include "psa_crypto_hash.h"
30
31#include "test/drivers/hash.h"
32
Ronald Cron7f13fa22021-04-13 12:41:34 +020033mbedtls_test_driver_hash_hooks_t
34 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cronfa036c82021-03-23 09:33:25 +010035
Ronald Cron7f13fa22021-04-13 12:41:34 +020036psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cronfa036c82021-03-23 09:33:25 +010037 psa_algorithm_t alg,
38 const uint8_t *input, size_t input_length,
39 uint8_t *hash, size_t hash_size, size_t *hash_length )
40{
Ronald Cron7f13fa22021-04-13 12:41:34 +020041 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010042
Ronald Cron7f13fa22021-04-13 12:41:34 +020043 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010044 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020045 mbedtls_test_driver_hash_hooks.driver_status =
46 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010047 }
48 else
49 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020050 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010051 mbedtls_transparent_test_driver_hash_compute(
52 alg, input, input_length,
53 hash, hash_size, hash_length );
54 }
55
Ronald Cron7f13fa22021-04-13 12:41:34 +020056 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010057}
58
Ronald Cron7f13fa22021-04-13 12:41:34 +020059psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cronfa036c82021-03-23 09:33:25 +010060 mbedtls_transparent_test_driver_hash_operation_t *operation,
61 psa_algorithm_t alg )
62{
Ronald Cron7f13fa22021-04-13 12:41:34 +020063 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010064
Ronald Cron7f13fa22021-04-13 12:41:34 +020065 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010066 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020067 mbedtls_test_driver_hash_hooks.driver_status =
68 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010069 }
70 else
71 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020072 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010073 mbedtls_transparent_test_driver_hash_setup( operation, alg );
74 }
75
Ronald Cron7f13fa22021-04-13 12:41:34 +020076 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010077}
78
Ronald Cron7f13fa22021-04-13 12:41:34 +020079psa_status_t mbedtls_test_transparent_hash_clone(
Ronald Cronfa036c82021-03-23 09:33:25 +010080 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
81 mbedtls_transparent_test_driver_hash_operation_t *target_operation )
82{
Ronald Cron7f13fa22021-04-13 12:41:34 +020083 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010084
Ronald Cron7f13fa22021-04-13 12:41:34 +020085 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010086 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020087 mbedtls_test_driver_hash_hooks.driver_status =
88 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010089 }
90 else
91 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020092 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010093 mbedtls_transparent_test_driver_hash_clone( source_operation,
94 target_operation );
95 }
96
Ronald Cron7f13fa22021-04-13 12:41:34 +020097 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010098}
99
Ronald Cron7f13fa22021-04-13 12:41:34 +0200100psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cronfa036c82021-03-23 09:33:25 +0100101 mbedtls_transparent_test_driver_hash_operation_t *operation,
102 const uint8_t *input,
103 size_t input_length )
104{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200105 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100106
Ronald Cron7f13fa22021-04-13 12:41:34 +0200107 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100108 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200109 mbedtls_test_driver_hash_hooks.driver_status =
110 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100111 }
112 else
113 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200114 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100115 mbedtls_transparent_test_driver_hash_update(
116 operation, input, input_length );
117 }
118
Ronald Cron7f13fa22021-04-13 12:41:34 +0200119 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100120}
121
Ronald Cron7f13fa22021-04-13 12:41:34 +0200122psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cronfa036c82021-03-23 09:33:25 +0100123 mbedtls_transparent_test_driver_hash_operation_t *operation,
124 uint8_t *hash,
125 size_t hash_size,
126 size_t *hash_length )
127{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200128 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100129
Ronald Cron7f13fa22021-04-13 12:41:34 +0200130 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100131 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200132 mbedtls_test_driver_hash_hooks.driver_status =
133 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100134 }
135 else
136 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200137 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100138 mbedtls_transparent_test_driver_hash_finish(
139 operation, hash, hash_size, hash_length );
140 }
141
Ronald Cron7f13fa22021-04-13 12:41:34 +0200142 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100143}
144
Ronald Cron7f13fa22021-04-13 12:41:34 +0200145psa_status_t mbedtls_test_transparent_hash_abort(
Ronald Cronfa036c82021-03-23 09:33:25 +0100146 mbedtls_transparent_test_driver_hash_operation_t *operation )
147{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200148 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100149
Ronald Cron7f13fa22021-04-13 12:41:34 +0200150 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100151 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200152 mbedtls_test_driver_hash_hooks.driver_status =
153 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100154 }
155 else
156 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200157 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100158 mbedtls_transparent_test_driver_hash_abort( operation );
159 }
160
Ronald Cron7f13fa22021-04-13 12:41:34 +0200161 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100162}
163#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */