1 /*
2 * Test driver for asymmetric encryption.
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.h"
24 #include "mbedtls/rsa.h"
25 #include "psa_crypto_rsa.h"
26 #include "string.h"
27 #include "test/drivers/asymmetric_encryption.h"
28
29 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
30 #include "libtestdriver1/library/psa_crypto_rsa.h"
31 #endif
32
33 mbedtls_test_driver_asymmetric_encryption_hooks_t mbedtls_test_driver_asymmetric_encryption_hooks =
34 MBEDTLS_TEST_DRIVER_ASYMMETRIC_ENCRYPTION_INIT;
35
mbedtls_test_transparent_asymmetric_encrypt(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * input,size_t input_length,const uint8_t * salt,size_t salt_length,uint8_t * output,size_t output_size,size_t * output_length)36 psa_status_t mbedtls_test_transparent_asymmetric_encrypt(
37 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
38 size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
39 size_t input_length, const uint8_t *salt, size_t salt_length,
40 uint8_t *output, size_t output_size, size_t *output_length)
41 {
42 mbedtls_test_driver_asymmetric_encryption_hooks.hits++;
43
44 if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL) {
45 if (output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length) {
46 return PSA_ERROR_BUFFER_TOO_SMALL;
47 }
48
49 memcpy(output,
50 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output,
51 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length);
52 *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length;
53
54 return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
55 }
56
57 if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS) {
58 return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
59 }
60
61 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
62 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
63 return libtestdriver1_mbedtls_psa_asymmetric_encrypt(
64 (const libtestdriver1_psa_key_attributes_t *) attributes,
65 key_buffer, key_buffer_size,
66 alg, input, input_length, salt, salt_length,
67 output, output_size, output_length);
68 #else
69 return mbedtls_psa_asymmetric_encrypt(
70 attributes, key_buffer, key_buffer_size,
71 alg, input, input_length, salt, salt_length,
72 output, output_size, output_length);
73 #endif
74
75 return PSA_ERROR_NOT_SUPPORTED;
76 }
77
mbedtls_test_transparent_asymmetric_decrypt(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * input,size_t input_length,const uint8_t * salt,size_t salt_length,uint8_t * output,size_t output_size,size_t * output_length)78 psa_status_t mbedtls_test_transparent_asymmetric_decrypt(
79 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
80 size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
81 size_t input_length, const uint8_t *salt, size_t salt_length,
82 uint8_t *output, size_t output_size, size_t *output_length)
83 {
84 mbedtls_test_driver_asymmetric_encryption_hooks.hits++;
85
86 if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL) {
87 if (output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length) {
88 return PSA_ERROR_BUFFER_TOO_SMALL;
89 }
90
91 memcpy(output,
92 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output,
93 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length);
94 *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length;
95
96 return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
97 }
98
99 if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS) {
100 return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
101 }
102
103 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
104 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
105 return libtestdriver1_mbedtls_psa_asymmetric_decrypt(
106 (const libtestdriver1_psa_key_attributes_t *) attributes,
107 key_buffer, key_buffer_size,
108 alg, input, input_length, salt, salt_length,
109 output, output_size, output_length);
110 #else
111 return mbedtls_psa_asymmetric_decrypt(
112 attributes, key_buffer, key_buffer_size,
113 alg, input, input_length, salt, salt_length,
114 output, output_size, output_length);
115 #endif
116
117 return PSA_ERROR_NOT_SUPPORTED;
118 }
119
120 /*
121 * opaque versions - TODO
122 */
mbedtls_test_opaque_asymmetric_encrypt(const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg,const uint8_t * input,size_t input_length,const uint8_t * salt,size_t salt_length,uint8_t * output,size_t output_size,size_t * output_length)123 psa_status_t mbedtls_test_opaque_asymmetric_encrypt(
124 const psa_key_attributes_t *attributes, const uint8_t *key,
125 size_t key_length, psa_algorithm_t alg, const uint8_t *input,
126 size_t input_length, const uint8_t *salt, size_t salt_length,
127 uint8_t *output, size_t output_size, size_t *output_length)
128 {
129 (void) attributes;
130 (void) key;
131 (void) key_length;
132 (void) alg;
133 (void) input;
134 (void) input_length;
135 (void) salt;
136 (void) salt_length;
137 (void) output;
138 (void) output_size;
139 (void) output_length;
140 return PSA_ERROR_NOT_SUPPORTED;
141 }
142
mbedtls_test_opaque_asymmetric_decrypt(const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg,const uint8_t * input,size_t input_length,const uint8_t * salt,size_t salt_length,uint8_t * output,size_t output_size,size_t * output_length)143 psa_status_t mbedtls_test_opaque_asymmetric_decrypt(
144 const psa_key_attributes_t *attributes, const uint8_t *key,
145 size_t key_length, psa_algorithm_t alg, const uint8_t *input,
146 size_t input_length, const uint8_t *salt, size_t salt_length,
147 uint8_t *output, size_t output_size, size_t *output_length)
148 {
149 (void) attributes;
150 (void) key;
151 (void) key_length;
152 (void) alg;
153 (void) input;
154 (void) input_length;
155 (void) salt;
156 (void) salt_length;
157 (void) output;
158 (void) output_size;
159 (void) output_length;
160 return PSA_ERROR_NOT_SUPPORTED;
161 }
162
163 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
164