1 /*
2  * Test driver for asymmetric encryption.
3  */
4 /*  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #include <test/helpers.h>
9 
10 #if defined(PSA_CRYPTO_DRIVER_TEST)
11 #include "psa/crypto.h"
12 #include "mbedtls/rsa.h"
13 #include "psa_crypto_rsa.h"
14 #include "string.h"
15 #include "test/drivers/asymmetric_encryption.h"
16 #include "test/drivers/key_management.h"
17 
18 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
19 #include "libtestdriver1/library/psa_crypto_rsa.h"
20 #endif
21 
22 #define PSA_RSA_KEY_PAIR_MAX_SIZE \
23     PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
24 
25 mbedtls_test_driver_asymmetric_encryption_hooks_t mbedtls_test_driver_asymmetric_encryption_hooks =
26     MBEDTLS_TEST_DRIVER_ASYMMETRIC_ENCRYPTION_INIT;
27 
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)28 psa_status_t mbedtls_test_transparent_asymmetric_encrypt(
29     const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
30     size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
31     size_t input_length, const uint8_t *salt, size_t salt_length,
32     uint8_t *output, size_t output_size, size_t *output_length)
33 {
34     mbedtls_test_driver_asymmetric_encryption_hooks.hits++;
35 
36     if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL) {
37         if (output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length) {
38             return PSA_ERROR_BUFFER_TOO_SMALL;
39         }
40 
41         memcpy(output,
42                mbedtls_test_driver_asymmetric_encryption_hooks.forced_output,
43                mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length);
44         *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length;
45 
46         return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
47     }
48 
49     if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS) {
50         return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
51     }
52 
53 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
54     return libtestdriver1_mbedtls_psa_asymmetric_encrypt(
55         (const libtestdriver1_psa_key_attributes_t *) attributes,
56         key_buffer, key_buffer_size,
57         alg, input, input_length, salt, salt_length,
58         output, output_size, output_length);
59 #else
60     return mbedtls_psa_asymmetric_encrypt(
61         attributes, key_buffer, key_buffer_size,
62         alg, input, input_length, salt, salt_length,
63         output, output_size, output_length);
64 #endif
65 
66     return PSA_ERROR_NOT_SUPPORTED;
67 }
68 
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)69 psa_status_t mbedtls_test_transparent_asymmetric_decrypt(
70     const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
71     size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
72     size_t input_length, const uint8_t *salt, size_t salt_length,
73     uint8_t *output, size_t output_size, size_t *output_length)
74 {
75     mbedtls_test_driver_asymmetric_encryption_hooks.hits++;
76 
77     if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL) {
78         if (output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length) {
79             return PSA_ERROR_BUFFER_TOO_SMALL;
80         }
81 
82         memcpy(output,
83                mbedtls_test_driver_asymmetric_encryption_hooks.forced_output,
84                mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length);
85         *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length;
86 
87         return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
88     }
89 
90     if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS) {
91         return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
92     }
93 
94 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
95     return libtestdriver1_mbedtls_psa_asymmetric_decrypt(
96         (const libtestdriver1_psa_key_attributes_t *) attributes,
97         key_buffer, key_buffer_size,
98         alg, input, input_length, salt, salt_length,
99         output, output_size, output_length);
100 #else
101     return mbedtls_psa_asymmetric_decrypt(
102         attributes, key_buffer, key_buffer_size,
103         alg, input, input_length, salt, salt_length,
104         output, output_size, output_length);
105 #endif
106 
107     return PSA_ERROR_NOT_SUPPORTED;
108 }
109 
110 /*
111  * opaque versions
112  */
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)113 psa_status_t mbedtls_test_opaque_asymmetric_encrypt(
114     const psa_key_attributes_t *attributes, const uint8_t *key,
115     size_t key_length, psa_algorithm_t alg, const uint8_t *input,
116     size_t input_length, const uint8_t *salt, size_t salt_length,
117     uint8_t *output, size_t output_size, size_t *output_length)
118 {
119     unsigned char unwrapped_key[PSA_RSA_KEY_PAIR_MAX_SIZE];
120     size_t unwrapped_key_length;
121     psa_status_t status;
122 
123     status = mbedtls_test_opaque_unwrap_key(key, key_length,
124                                             unwrapped_key, sizeof(unwrapped_key),
125                                             &unwrapped_key_length);
126     if (status != PSA_SUCCESS) {
127         return status;
128     }
129 
130 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
131     (defined(MBEDTLS_PSA_ACCEL_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_CRYPT))
132     return libtestdriver1_mbedtls_psa_asymmetric_encrypt(
133         (const libtestdriver1_psa_key_attributes_t *) attributes,
134         unwrapped_key, unwrapped_key_length,
135         alg, input, input_length, salt, salt_length,
136         output, output_size, output_length);
137 #else
138     return mbedtls_psa_asymmetric_encrypt(
139         attributes, unwrapped_key, unwrapped_key_length,
140         alg, input, input_length, salt, salt_length,
141         output, output_size, output_length);
142 #endif
143 
144     return PSA_ERROR_NOT_SUPPORTED;
145 }
146 
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)147 psa_status_t mbedtls_test_opaque_asymmetric_decrypt(
148     const psa_key_attributes_t *attributes, const uint8_t *key,
149     size_t key_length, psa_algorithm_t alg, const uint8_t *input,
150     size_t input_length, const uint8_t *salt, size_t salt_length,
151     uint8_t *output, size_t output_size, size_t *output_length)
152 {
153     unsigned char unwrapped_key[PSA_RSA_KEY_PAIR_MAX_SIZE];
154     size_t unwrapped_key_length;
155     psa_status_t status;
156 
157     status = mbedtls_test_opaque_unwrap_key(key, key_length,
158                                             unwrapped_key, sizeof(unwrapped_key),
159                                             &unwrapped_key_length);
160     if (status != PSA_SUCCESS) {
161         return status;
162     }
163 
164 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
165     (defined(MBEDTLS_PSA_ACCEL_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_CRYPT))
166     return libtestdriver1_mbedtls_psa_asymmetric_decrypt(
167         (const libtestdriver1_psa_key_attributes_t *) attributes,
168         unwrapped_key, unwrapped_key_length,
169         alg, input, input_length, salt, salt_length,
170         output, output_size, output_length);
171 #else
172     return mbedtls_psa_asymmetric_decrypt(
173         attributes, unwrapped_key, unwrapped_key_length,
174         alg, input, input_length, salt, salt_length,
175         output, output_size, output_length);
176 #endif
177 
178     return PSA_ERROR_NOT_SUPPORTED;
179 }
180 
181 #endif /* PSA_CRYPTO_DRIVER_TEST */
182