blob: 2294ecf64f2d950432b012f2ac0f17755c8f1088 [file] [log] [blame]
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001/*
2 * Test driver for MAC 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_mac.h"
28
29#include "test/drivers/mac.h"
30
Steven Cooremanbe21dab2021-05-10 11:18:20 +020031mbedtls_test_driver_mac_hooks_t mbedtls_test_driver_mac_hooks =
32 MBEDTLS_TEST_DRIVER_MAC_INIT;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020033
34psa_status_t mbedtls_test_transparent_mac_compute(
35 const psa_key_attributes_t *attributes,
36 const uint8_t *key_buffer,
37 size_t key_buffer_size,
38 psa_algorithm_t alg,
39 const uint8_t *input,
40 size_t input_length,
41 uint8_t *mac,
42 size_t mac_size,
43 size_t *mac_length )
44{
Steven Cooremanbe21dab2021-05-10 11:18:20 +020045 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020046
Steven Cooremanbe21dab2021-05-10 11:18:20 +020047 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020048 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +020049 mbedtls_test_driver_mac_hooks.driver_status =
50 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020051 }
52 else
53 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +020054 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020055 mbedtls_transparent_test_driver_mac_compute(
56 attributes, key_buffer, key_buffer_size, alg,
57 input, input_length,
58 mac, mac_size, mac_length );
59 }
60
Steven Cooremanbe21dab2021-05-10 11:18:20 +020061 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020062}
63
64psa_status_t mbedtls_test_transparent_mac_sign_setup(
65 mbedtls_transparent_test_driver_mac_operation_t *operation,
66 const psa_key_attributes_t *attributes,
67 const uint8_t *key_buffer,
68 size_t key_buffer_size,
69 psa_algorithm_t alg )
70{
Steven Cooremanbe21dab2021-05-10 11:18:20 +020071 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020072
Steven Cooremanbe21dab2021-05-10 11:18:20 +020073 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020074 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +020075 mbedtls_test_driver_mac_hooks.driver_status =
76 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020077 }
78 else
79 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +020080 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020081 mbedtls_transparent_test_driver_mac_sign_setup(
82 operation, attributes, key_buffer, key_buffer_size, alg );
83 }
84
Steven Cooremanbe21dab2021-05-10 11:18:20 +020085 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020086}
87
88psa_status_t mbedtls_test_transparent_mac_verify_setup(
89 mbedtls_transparent_test_driver_mac_operation_t *operation,
90 const psa_key_attributes_t *attributes,
91 const uint8_t *key_buffer,
92 size_t key_buffer_size,
93 psa_algorithm_t alg )
94{
Steven Cooremanbe21dab2021-05-10 11:18:20 +020095 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020096
Steven Cooremanbe21dab2021-05-10 11:18:20 +020097 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +020098 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +020099 mbedtls_test_driver_mac_hooks.driver_status =
100 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200101 }
102 else
103 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200104 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200105 mbedtls_transparent_test_driver_mac_verify_setup(
106 operation, attributes, key_buffer, key_buffer_size, alg );
107 }
108
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200109 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200110}
111
112psa_status_t mbedtls_test_transparent_mac_update(
113 mbedtls_transparent_test_driver_mac_operation_t *operation,
114 const uint8_t *input,
115 size_t input_length )
116{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200117 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200118
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200119 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200120 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200121 mbedtls_test_driver_mac_hooks.driver_status =
122 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200123 }
124 else
125 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200126 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200127 mbedtls_transparent_test_driver_mac_update(
128 operation, input, input_length );
129 }
130
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200131 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200132}
133
134psa_status_t mbedtls_test_transparent_mac_sign_finish(
135 mbedtls_transparent_test_driver_mac_operation_t *operation,
136 uint8_t *mac,
137 size_t mac_size,
138 size_t *mac_length )
139{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200140 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200141
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200142 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200143 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200144 mbedtls_test_driver_mac_hooks.driver_status =
145 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200146 }
147 else
148 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200149 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200150 mbedtls_transparent_test_driver_mac_sign_finish(
151 operation, mac, mac_size, mac_length );
152 }
153
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200154 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200155}
156
157psa_status_t mbedtls_test_transparent_mac_verify_finish(
158 mbedtls_transparent_test_driver_mac_operation_t *operation,
159 const uint8_t *mac,
160 size_t mac_length )
161{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200162 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200163
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200164 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200165 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200166 mbedtls_test_driver_mac_hooks.driver_status =
167 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200168 }
169 else
170 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200171 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200172 mbedtls_transparent_test_driver_mac_verify_finish(
173 operation, mac, mac_length );
174 }
175
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200176 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200177}
178
179psa_status_t mbedtls_test_transparent_mac_abort(
180 mbedtls_transparent_test_driver_mac_operation_t *operation )
181{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200182 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200183
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200184 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200185 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200186 mbedtls_test_driver_mac_hooks.driver_status =
187 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200188 }
189 else
190 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200191 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200192 mbedtls_transparent_test_driver_mac_abort( operation );
193 }
194
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200195 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200196}
197
198psa_status_t mbedtls_test_opaque_mac_compute(
199 const psa_key_attributes_t *attributes,
200 const uint8_t *key_buffer,
201 size_t key_buffer_size,
202 psa_algorithm_t alg,
203 const uint8_t *input,
204 size_t input_length,
205 uint8_t *mac,
206 size_t mac_size,
207 size_t *mac_length )
208{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200209 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200210
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200211 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200212 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200213 mbedtls_test_driver_mac_hooks.driver_status =
214 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200215 }
216 else
217 {
Ronald Crona2dbe662021-07-01 11:24:02 +0200218 (void) attributes;
219 (void) key_buffer;
220 (void) key_buffer_size;
221 (void) alg;
222 (void) input;
223 (void) input_length;
224 (void) mac;
225 (void) mac_size;
226 (void) mac_length;
227 mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200228 }
229
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200230 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200231}
232
233psa_status_t mbedtls_test_opaque_mac_sign_setup(
234 mbedtls_opaque_test_driver_mac_operation_t *operation,
235 const psa_key_attributes_t *attributes,
236 const uint8_t *key_buffer,
237 size_t key_buffer_size,
238 psa_algorithm_t alg )
239{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200240 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200241
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200242 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200243 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200244 mbedtls_test_driver_mac_hooks.driver_status =
245 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200246 }
247 else
248 {
Ronald Crona2dbe662021-07-01 11:24:02 +0200249 (void) operation;
250 (void) attributes;
251 (void) key_buffer;
252 (void) key_buffer_size;
253 (void) alg;
254 mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200255 }
256
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200257 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200258}
259
260psa_status_t mbedtls_test_opaque_mac_verify_setup(
261 mbedtls_opaque_test_driver_mac_operation_t *operation,
262 const psa_key_attributes_t *attributes,
263 const uint8_t *key_buffer,
264 size_t key_buffer_size,
265 psa_algorithm_t alg )
266{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200267 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200268
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200269 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200270 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200271 mbedtls_test_driver_mac_hooks.driver_status =
272 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200273 }
274 else
275 {
Ronald Crona2dbe662021-07-01 11:24:02 +0200276 (void) operation;
277 (void) attributes;
278 (void) key_buffer;
279 (void) key_buffer_size;
280 (void) alg;
281 mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200282 }
283
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200284 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200285}
286
287psa_status_t mbedtls_test_opaque_mac_update(
288 mbedtls_opaque_test_driver_mac_operation_t *operation,
289 const uint8_t *input,
290 size_t input_length )
291{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200292 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200293
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200294 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200295 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200296 mbedtls_test_driver_mac_hooks.driver_status =
297 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200298 }
299 else
300 {
Ronald Crona2dbe662021-07-01 11:24:02 +0200301 (void) operation;
302 (void) input;
303 (void) input_length;
304 mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200305 }
306
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200307 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200308}
309
310psa_status_t mbedtls_test_opaque_mac_sign_finish(
311 mbedtls_opaque_test_driver_mac_operation_t *operation,
312 uint8_t *mac,
313 size_t mac_size,
314 size_t *mac_length )
315{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200316 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200317
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200318 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200319 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200320 mbedtls_test_driver_mac_hooks.driver_status =
321 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200322 }
323 else
324 {
Ronald Crona2dbe662021-07-01 11:24:02 +0200325 (void) operation;
326 (void) mac;
327 (void) mac_size;
328 (void) mac_length;
329 mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200330 }
331
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200332 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200333}
334
335psa_status_t mbedtls_test_opaque_mac_verify_finish(
336 mbedtls_opaque_test_driver_mac_operation_t *operation,
337 const uint8_t *mac,
338 size_t mac_length )
339{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200340 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200341
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200342 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200343 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200344 mbedtls_test_driver_mac_hooks.driver_status =
345 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200346 }
347 else
348 {
Ronald Crona2dbe662021-07-01 11:24:02 +0200349 (void) operation;
350 (void) mac;
351 (void) mac_length;
352 mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200353 }
354
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200355 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200356}
357
358psa_status_t mbedtls_test_opaque_mac_abort(
359 mbedtls_opaque_test_driver_mac_operation_t *operation )
360{
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200361 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200362
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200363 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200364 {
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200365 mbedtls_test_driver_mac_hooks.driver_status =
366 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200367 }
368 else
369 {
Ronald Crona2dbe662021-07-01 11:24:02 +0200370 (void) operation;
371 mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200372 }
373
Steven Cooremanbe21dab2021-05-10 11:18:20 +0200374 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooreman2d9a3f92021-04-29 21:10:11 +0200375}
376
377#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */