1 /*
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 #include <test/helpers.h>
21 
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 
27 mbedtls_test_driver_hash_hooks_t
28     mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
29 
mbedtls_test_transparent_hash_compute(psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * hash,size_t hash_size,size_t * hash_length)30 psa_status_t mbedtls_test_transparent_hash_compute(
31     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 {
35     mbedtls_test_driver_hash_hooks.hits++;
36 
37     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
38     {
39          mbedtls_test_driver_hash_hooks.driver_status =
40              mbedtls_test_driver_hash_hooks.forced_status;
41     }
42     else
43     {
44         mbedtls_test_driver_hash_hooks.driver_status =
45             mbedtls_transparent_test_driver_hash_compute(
46                 alg, input, input_length,
47                 hash, hash_size, hash_length );
48     }
49 
50     return( mbedtls_test_driver_hash_hooks.driver_status );
51 }
52 
mbedtls_test_transparent_hash_setup(mbedtls_transparent_test_driver_hash_operation_t * operation,psa_algorithm_t alg)53 psa_status_t mbedtls_test_transparent_hash_setup(
54     mbedtls_transparent_test_driver_hash_operation_t *operation,
55     psa_algorithm_t alg )
56 {
57     mbedtls_test_driver_hash_hooks.hits++;
58 
59     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
60     {
61          mbedtls_test_driver_hash_hooks.driver_status =
62              mbedtls_test_driver_hash_hooks.forced_status;
63     }
64     else
65     {
66         mbedtls_test_driver_hash_hooks.driver_status =
67             mbedtls_transparent_test_driver_hash_setup( operation, alg );
68     }
69 
70     return( mbedtls_test_driver_hash_hooks.driver_status );
71 }
72 
mbedtls_test_transparent_hash_clone(const mbedtls_transparent_test_driver_hash_operation_t * source_operation,mbedtls_transparent_test_driver_hash_operation_t * target_operation)73 psa_status_t mbedtls_test_transparent_hash_clone(
74     const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
75     mbedtls_transparent_test_driver_hash_operation_t *target_operation )
76 {
77     mbedtls_test_driver_hash_hooks.hits++;
78 
79     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
80     {
81          mbedtls_test_driver_hash_hooks.driver_status =
82              mbedtls_test_driver_hash_hooks.forced_status;
83     }
84     else
85     {
86         mbedtls_test_driver_hash_hooks.driver_status =
87             mbedtls_transparent_test_driver_hash_clone( source_operation,
88                                                         target_operation );
89     }
90 
91     return( mbedtls_test_driver_hash_hooks.driver_status );
92 }
93 
mbedtls_test_transparent_hash_update(mbedtls_transparent_test_driver_hash_operation_t * operation,const uint8_t * input,size_t input_length)94 psa_status_t mbedtls_test_transparent_hash_update(
95     mbedtls_transparent_test_driver_hash_operation_t *operation,
96     const uint8_t *input,
97     size_t input_length )
98 {
99     mbedtls_test_driver_hash_hooks.hits++;
100 
101     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
102     {
103          mbedtls_test_driver_hash_hooks.driver_status =
104              mbedtls_test_driver_hash_hooks.forced_status;
105     }
106     else
107     {
108         mbedtls_test_driver_hash_hooks.driver_status =
109             mbedtls_transparent_test_driver_hash_update(
110                 operation, input, input_length );
111     }
112 
113     return( mbedtls_test_driver_hash_hooks.driver_status );
114 }
115 
mbedtls_test_transparent_hash_finish(mbedtls_transparent_test_driver_hash_operation_t * operation,uint8_t * hash,size_t hash_size,size_t * hash_length)116 psa_status_t mbedtls_test_transparent_hash_finish(
117     mbedtls_transparent_test_driver_hash_operation_t *operation,
118     uint8_t *hash,
119     size_t hash_size,
120     size_t *hash_length )
121 {
122     mbedtls_test_driver_hash_hooks.hits++;
123 
124     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
125     {
126          mbedtls_test_driver_hash_hooks.driver_status =
127              mbedtls_test_driver_hash_hooks.forced_status;
128     }
129     else
130     {
131         mbedtls_test_driver_hash_hooks.driver_status =
132             mbedtls_transparent_test_driver_hash_finish(
133                 operation, hash, hash_size, hash_length );
134     }
135 
136     return( mbedtls_test_driver_hash_hooks.driver_status );
137 }
138 
mbedtls_test_transparent_hash_abort(mbedtls_transparent_test_driver_hash_operation_t * operation)139 psa_status_t mbedtls_test_transparent_hash_abort(
140     mbedtls_transparent_test_driver_hash_operation_t *operation )
141 {
142     mbedtls_test_driver_hash_hooks.hits++;
143 
144     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
145     {
146          mbedtls_test_driver_hash_hooks.driver_status =
147              mbedtls_test_driver_hash_hooks.forced_status;
148     }
149     else
150     {
151         mbedtls_test_driver_hash_hooks.driver_status =
152             mbedtls_transparent_test_driver_hash_abort( operation );
153     }
154 
155     return( mbedtls_test_driver_hash_hooks.driver_status );
156 }
157 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
158