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