blob: 8dbee9263fc46afa972d1cf05ea473ec94bc48da [file] [log] [blame]
Steven Cooreman37941cb2020-07-28 18:49:51 +02001/*
2 * Test driver for cipher functions
3 */
Steven Cooreman3ec40182020-09-02 16:27:46 +02004/* Copyright The Mbed TLS Contributors
Steven Cooreman37941cb2020-07-28 18:49:51 +02005 * 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.
Steven Cooreman37941cb2020-07-28 18:49:51 +020018 */
19
20#ifndef PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
21#define PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
22
Bence Szépkútic662b362021-05-27 11:25:03 +020023#include "mbedtls/build_info.h"
Steven Cooreman37941cb2020-07-28 18:49:51 +020024
25#if defined(PSA_CRYPTO_DRIVER_TEST)
26#include <psa/crypto_driver_common.h>
Ronald Cron8d310ad2020-12-15 15:17:20 +010027#include <psa/crypto.h>
Steven Cooreman37941cb2020-07-28 18:49:51 +020028
29#include "mbedtls/cipher.h"
Steven Cooreman37941cb2020-07-28 18:49:51 +020030
Steven Cooremanacb5a102020-09-08 14:06:57 +020031typedef struct {
32 /* If non-null, on success, copy this to the output. */
33 void *forced_output;
34 size_t forced_output_length;
35 /* If not PSA_SUCCESS, return this error code instead of processing the
36 * function call. */
37 psa_status_t forced_status;
Steven Cooreman5240e8b2020-09-09 11:51:45 +020038 /* Count the amount of times one of the cipher driver functions is called. */
Steven Cooremanacb5a102020-09-08 14:06:57 +020039 unsigned long hits;
Ronald Cron7f13fa22021-04-13 12:41:34 +020040} mbedtls_test_driver_cipher_hooks_t;
Steven Cooreman37941cb2020-07-28 18:49:51 +020041
Ronald Cron7f13fa22021-04-13 12:41:34 +020042#define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 }
43static inline mbedtls_test_driver_cipher_hooks_t
44 mbedtls_test_driver_cipher_hooks_init( void )
Steven Cooremanacb5a102020-09-08 14:06:57 +020045{
Ronald Cron7f13fa22021-04-13 12:41:34 +020046 const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT;
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020047 return v ;
Steven Cooremanacb5a102020-09-08 14:06:57 +020048}
49
Ronald Cron7f13fa22021-04-13 12:41:34 +020050extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks;
Steven Cooreman37941cb2020-07-28 18:49:51 +020051
Ronald Cron7f13fa22021-04-13 12:41:34 +020052psa_status_t mbedtls_test_transparent_cipher_encrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +020053 const psa_key_attributes_t *attributes,
54 const uint8_t *key, size_t key_length,
55 psa_algorithm_t alg,
56 const uint8_t *input, size_t input_length,
57 uint8_t *output, size_t output_size, size_t *output_length);
58
Ronald Cron7f13fa22021-04-13 12:41:34 +020059psa_status_t mbedtls_test_transparent_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +020060 const psa_key_attributes_t *attributes,
61 const uint8_t *key, size_t key_length,
62 psa_algorithm_t alg,
63 const uint8_t *input, size_t input_length,
64 uint8_t *output, size_t output_size, size_t *output_length);
65
Ronald Cron7f13fa22021-04-13 12:41:34 +020066psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010067 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +020068 const psa_key_attributes_t *attributes,
69 const uint8_t *key, size_t key_length,
70 psa_algorithm_t alg);
71
Ronald Cron7f13fa22021-04-13 12:41:34 +020072psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010073 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +020074 const psa_key_attributes_t *attributes,
75 const uint8_t *key, size_t key_length,
76 psa_algorithm_t alg);
77
Ronald Cron7f13fa22021-04-13 12:41:34 +020078psa_status_t mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010079 mbedtls_transparent_test_driver_cipher_operation_t *operation );
Steven Cooreman37941cb2020-07-28 18:49:51 +020080
Ronald Cron7f13fa22021-04-13 12:41:34 +020081psa_status_t mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010082 mbedtls_transparent_test_driver_cipher_operation_t *operation,
83 const uint8_t *iv, size_t iv_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +020084
Ronald Cron7f13fa22021-04-13 12:41:34 +020085psa_status_t mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010086 mbedtls_transparent_test_driver_cipher_operation_t *operation,
87 const uint8_t *input, size_t input_length,
88 uint8_t *output, size_t output_size, size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +020089
Ronald Cron7f13fa22021-04-13 12:41:34 +020090psa_status_t mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010091 mbedtls_transparent_test_driver_cipher_operation_t *operation,
92 uint8_t *output, size_t output_size, size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +020093
94/*
95 * opaque versions
96 */
Ronald Cron7f13fa22021-04-13 12:41:34 +020097psa_status_t mbedtls_test_opaque_cipher_encrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +020098 const psa_key_attributes_t *attributes,
99 const uint8_t *key, size_t key_length,
100 psa_algorithm_t alg,
101 const uint8_t *input, size_t input_length,
102 uint8_t *output, size_t output_size, size_t *output_length);
103
Ronald Cron7f13fa22021-04-13 12:41:34 +0200104psa_status_t mbedtls_test_opaque_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200105 const psa_key_attributes_t *attributes,
106 const uint8_t *key, size_t key_length,
107 psa_algorithm_t alg,
108 const uint8_t *input, size_t input_length,
109 uint8_t *output, size_t output_size, size_t *output_length);
110
Ronald Cron7f13fa22021-04-13 12:41:34 +0200111psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100112 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200113 const psa_key_attributes_t *attributes,
114 const uint8_t *key, size_t key_length,
115 psa_algorithm_t alg);
116
Ronald Cron7f13fa22021-04-13 12:41:34 +0200117psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100118 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200119 const psa_key_attributes_t *attributes,
120 const uint8_t *key, size_t key_length,
121 psa_algorithm_t alg);
122
Ronald Cron7f13fa22021-04-13 12:41:34 +0200123psa_status_t mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100124 mbedtls_opaque_test_driver_cipher_operation_t *operation);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200125
Ronald Cron7f13fa22021-04-13 12:41:34 +0200126psa_status_t mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100127 mbedtls_opaque_test_driver_cipher_operation_t *operation,
128 const uint8_t *iv, size_t iv_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200129
Ronald Cron7f13fa22021-04-13 12:41:34 +0200130psa_status_t mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100131 mbedtls_opaque_test_driver_cipher_operation_t *operation,
132 const uint8_t *input, size_t input_length,
133 uint8_t *output, size_t output_size, size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200134
Ronald Cron7f13fa22021-04-13 12:41:34 +0200135psa_status_t mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100136 mbedtls_opaque_test_driver_cipher_operation_t *operation,
137 uint8_t *output, size_t output_size, size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200138
139#endif /* PSA_CRYPTO_DRIVER_TEST */
140#endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */