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     {
46         if( output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length )
47             return( PSA_ERROR_BUFFER_TOO_SMALL );
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 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
61     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
62     return( libtestdriver1_mbedtls_psa_asymmetric_encrypt(
63                 (const libtestdriver1_psa_key_attributes_t *)attributes,
64                 key_buffer, key_buffer_size,
65                 alg, input, input_length, salt, salt_length,
66                 output, output_size, output_length ) );
67 #else
68     return( mbedtls_psa_asymmetric_encrypt(
69                 attributes, key_buffer, key_buffer_size,
70                 alg, input, input_length, salt, salt_length,
71                 output, output_size, output_length ) );
72 #endif
73 
74     return( PSA_ERROR_NOT_SUPPORTED );
75 }
76 
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)77 psa_status_t mbedtls_test_transparent_asymmetric_decrypt(
78     const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
79     size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
80     size_t input_length, const uint8_t *salt, size_t salt_length,
81     uint8_t *output, size_t output_size, size_t *output_length )
82 {
83     mbedtls_test_driver_asymmetric_encryption_hooks.hits++;
84 
85     if( mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL )
86     {
87         if( output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length )
88             return( PSA_ERROR_BUFFER_TOO_SMALL );
89 
90         memcpy( output,
91                 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output,
92                 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length );
93         *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length;
94 
95         return( mbedtls_test_driver_asymmetric_encryption_hooks.forced_status );
96     }
97 
98     if( mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS )
99         return( mbedtls_test_driver_asymmetric_encryption_hooks.forced_status );
100 
101 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
102     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
103     return( libtestdriver1_mbedtls_psa_asymmetric_decrypt(
104                 (const libtestdriver1_psa_key_attributes_t *)attributes,
105                 key_buffer, key_buffer_size,
106                 alg, input, input_length, salt, salt_length,
107                 output, output_size, output_length ) );
108 #else
109     return( mbedtls_psa_asymmetric_decrypt(
110                 attributes, key_buffer, key_buffer_size,
111                 alg, input, input_length, salt, salt_length,
112                 output, output_size, output_length ) );
113 #endif
114 
115     return( PSA_ERROR_NOT_SUPPORTED );
116 }
117 
118 /*
119  * opaque versions - TODO
120  */
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)121 psa_status_t mbedtls_test_opaque_asymmetric_encrypt(
122     const psa_key_attributes_t *attributes, const uint8_t *key,
123     size_t key_length, psa_algorithm_t alg, const uint8_t *input,
124     size_t input_length, const uint8_t *salt, size_t salt_length,
125     uint8_t *output, size_t output_size, size_t *output_length )
126 {
127     (void) attributes;
128     (void) key;
129     (void) key_length;
130     (void) alg;
131     (void) input;
132     (void) input_length;
133     (void) salt;
134     (void) salt_length;
135     (void) output;
136     (void) output_size;
137     (void) output_length;
138     return( PSA_ERROR_NOT_SUPPORTED );
139 }
140 
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)141 psa_status_t mbedtls_test_opaque_asymmetric_decrypt(
142     const psa_key_attributes_t *attributes, const uint8_t *key,
143     size_t key_length, psa_algorithm_t alg, const uint8_t *input,
144     size_t input_length, const uint8_t *salt, size_t salt_length,
145     uint8_t *output, size_t output_size, size_t *output_length )
146 {
147     (void) attributes;
148     (void) key;
149     (void) key_length;
150     (void) alg;
151     (void) input;
152     (void) input_length;
153     (void) salt;
154     (void) salt_length;
155     (void) output;
156     (void) output_size;
157     (void) output_length;
158     return( PSA_ERROR_NOT_SUPPORTED );
159 }
160 
161 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
162