blob: 4686955e70a78156348bcb0ac2ab31065ade2de2 [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)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020026# include <psa/crypto_driver_common.h>
27# include <psa/crypto.h>
Steven Cooreman37941cb2020-07-28 18:49:51 +020028
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020029# 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;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038 /* Count the amount of times one of the cipher driver functions is called.
39 */
Steven Cooremanacb5a102020-09-08 14:06:57 +020040 unsigned long hits;
Ronald Cron7f13fa22021-04-13 12:41:34 +020041} mbedtls_test_driver_cipher_hooks_t;
Steven Cooreman37941cb2020-07-28 18:49:51 +020042
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043# define MBEDTLS_TEST_DRIVER_CIPHER_INIT \
44 { \
45 NULL, 0, PSA_SUCCESS, 0 \
46 }
Ronald Cron7f13fa22021-04-13 12:41:34 +020047static inline mbedtls_test_driver_cipher_hooks_t
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048mbedtls_test_driver_cipher_hooks_init(void)
Steven Cooremanacb5a102020-09-08 14:06:57 +020049{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050 const mbedtls_test_driver_cipher_hooks_t v =
51 MBEDTLS_TEST_DRIVER_CIPHER_INIT;
52 return v;
Steven Cooremanacb5a102020-09-08 14:06:57 +020053}
54
Ronald Cron7f13fa22021-04-13 12:41:34 +020055extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks;
Steven Cooreman37941cb2020-07-28 18:49:51 +020056
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057psa_status_t
58mbedtls_test_transparent_cipher_encrypt(const psa_key_attributes_t *attributes,
59 const uint8_t *key,
60 size_t key_length,
61 psa_algorithm_t alg,
62 const uint8_t *input,
63 size_t input_length,
64 uint8_t *output,
65 size_t output_size,
66 size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +020067
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068psa_status_t
69mbedtls_test_transparent_cipher_decrypt(const psa_key_attributes_t *attributes,
70 const uint8_t *key,
71 size_t key_length,
72 psa_algorithm_t alg,
73 const uint8_t *input,
74 size_t input_length,
75 uint8_t *output,
76 size_t output_size,
77 size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +020078
Ronald Cron7f13fa22021-04-13 12:41:34 +020079psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010080 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +020081 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082 const uint8_t *key,
83 size_t key_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +020084 psa_algorithm_t alg);
85
Ronald Cron7f13fa22021-04-13 12:41:34 +020086psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010087 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +020088 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089 const uint8_t *key,
90 size_t key_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +020091 psa_algorithm_t alg);
92
Ronald Cron7f13fa22021-04-13 12:41:34 +020093psa_status_t mbedtls_test_transparent_cipher_abort(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020094 mbedtls_transparent_test_driver_cipher_operation_t *operation);
Steven Cooreman37941cb2020-07-28 18:49:51 +020095
Ronald Cron7f13fa22021-04-13 12:41:34 +020096psa_status_t mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +010097 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020098 const uint8_t *iv,
99 size_t iv_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200100
Ronald Cron7f13fa22021-04-13 12:41:34 +0200101psa_status_t mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100102 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200103 const uint8_t *input,
104 size_t input_length,
105 uint8_t *output,
106 size_t output_size,
107 size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200108
Ronald Cron7f13fa22021-04-13 12:41:34 +0200109psa_status_t mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100110 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200111 uint8_t *output,
112 size_t output_size,
113 size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200114
115/*
116 * opaque versions
117 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200118psa_status_t
119mbedtls_test_opaque_cipher_encrypt(const psa_key_attributes_t *attributes,
120 const uint8_t *key,
121 size_t key_length,
122 psa_algorithm_t alg,
123 const uint8_t *input,
124 size_t input_length,
125 uint8_t *output,
126 size_t output_size,
127 size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200128
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129psa_status_t
130mbedtls_test_opaque_cipher_decrypt(const psa_key_attributes_t *attributes,
131 const uint8_t *key,
132 size_t key_length,
133 psa_algorithm_t alg,
134 const uint8_t *input,
135 size_t input_length,
136 uint8_t *output,
137 size_t output_size,
138 size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200139
Ronald Cron7f13fa22021-04-13 12:41:34 +0200140psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100141 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200142 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200143 const uint8_t *key,
144 size_t key_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200145 psa_algorithm_t alg);
146
Ronald Cron7f13fa22021-04-13 12:41:34 +0200147psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100148 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200149 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200150 const uint8_t *key,
151 size_t key_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200152 psa_algorithm_t alg);
153
Ronald Cron7f13fa22021-04-13 12:41:34 +0200154psa_status_t mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100155 mbedtls_opaque_test_driver_cipher_operation_t *operation);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200156
Ronald Cron7f13fa22021-04-13 12:41:34 +0200157psa_status_t mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100158 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200159 const uint8_t *iv,
160 size_t iv_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200161
Ronald Cron7f13fa22021-04-13 12:41:34 +0200162psa_status_t mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100163 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164 const uint8_t *input,
165 size_t input_length,
166 uint8_t *output,
167 size_t output_size,
168 size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200169
Ronald Cron7f13fa22021-04-13 12:41:34 +0200170psa_status_t mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100171 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200172 uint8_t *output,
173 size_t output_size,
174 size_t *output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200175
176#endif /* PSA_CRYPTO_DRIVER_TEST */
177#endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */