1 /*
2  * Test driver for cipher functions.
3  * Currently only supports multi-part operations using AES-CTR.
4  */
5 /*  Copyright The Mbed TLS Contributors
6  *  SPDX-License-Identifier: Apache-2.0
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
9  *  not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 #include <test/helpers.h>
22 
23 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
24 #include "psa/crypto.h"
25 #include "psa_crypto_cipher.h"
26 #include "psa_crypto_core.h"
27 #include "mbedtls/cipher.h"
28 
29 #include "test/drivers/cipher.h"
30 
31 #include "test/random.h"
32 
33 #include <string.h>
34 
35 mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks =
36     MBEDTLS_TEST_DRIVER_CIPHER_INIT;
37 
mbedtls_test_transparent_cipher_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,uint8_t * output,size_t output_size,size_t * output_length)38 psa_status_t mbedtls_test_transparent_cipher_encrypt(
39     const psa_key_attributes_t *attributes,
40     const uint8_t *key_buffer,
41     size_t key_buffer_size,
42     psa_algorithm_t alg,
43     const uint8_t *input,
44     size_t input_length,
45     uint8_t *output,
46     size_t output_size,
47     size_t *output_length )
48 {
49     mbedtls_test_driver_cipher_hooks.hits++;
50 
51     if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
52     {
53         if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
54             return( PSA_ERROR_BUFFER_TOO_SMALL );
55 
56         memcpy( output,
57                 mbedtls_test_driver_cipher_hooks.forced_output,
58                 mbedtls_test_driver_cipher_hooks.forced_output_length );
59         *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
60 
61         return( mbedtls_test_driver_cipher_hooks.forced_status );
62     }
63 
64     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
65         return( mbedtls_test_driver_cipher_hooks.forced_status );
66 
67     psa_generate_random( output, PSA_CIPHER_IV_LENGTH( attributes->core.type, alg ) );
68 
69     return( mbedtls_transparent_test_driver_cipher_encrypt(
70                 attributes, key_buffer, key_buffer_size,
71                 alg, input, input_length,
72                 output, output_size, output_length ) );
73 }
74 
mbedtls_test_transparent_cipher_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,uint8_t * output,size_t output_size,size_t * output_length)75 psa_status_t mbedtls_test_transparent_cipher_decrypt(
76     const psa_key_attributes_t *attributes,
77     const uint8_t *key_buffer,
78     size_t key_buffer_size,
79     psa_algorithm_t alg,
80     const uint8_t *input,
81     size_t input_length,
82     uint8_t *output,
83     size_t output_size,
84     size_t *output_length )
85 {
86    mbedtls_test_driver_cipher_hooks.hits++;
87 
88     if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
89     {
90         if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
91             return( PSA_ERROR_BUFFER_TOO_SMALL );
92 
93         memcpy( output,
94                 mbedtls_test_driver_cipher_hooks.forced_output,
95                 mbedtls_test_driver_cipher_hooks.forced_output_length );
96         *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
97 
98         return( mbedtls_test_driver_cipher_hooks.forced_status );
99     }
100 
101     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
102         return( mbedtls_test_driver_cipher_hooks.forced_status );
103 
104     return( mbedtls_transparent_test_driver_cipher_decrypt(
105                 attributes, key_buffer, key_buffer_size,
106                 alg, input, input_length,
107                 output, output_size, output_length ) );
108 }
109 
mbedtls_test_transparent_cipher_encrypt_setup(mbedtls_transparent_test_driver_cipher_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg)110 psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
111     mbedtls_transparent_test_driver_cipher_operation_t *operation,
112     const psa_key_attributes_t *attributes,
113     const uint8_t *key, size_t key_length,
114     psa_algorithm_t alg)
115 {
116     mbedtls_test_driver_cipher_hooks.hits++;
117 
118     /* Wiping the entire struct here, instead of member-by-member. This is
119      * useful for the test suite, since it gives a chance of catching memory
120      * corruption errors should the core not have allocated (enough) memory for
121      * our context struct. */
122     memset( operation, 0, sizeof( *operation ) );
123 
124     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
125         return( mbedtls_test_driver_cipher_hooks.forced_status );
126 
127     return ( mbedtls_transparent_test_driver_cipher_encrypt_setup(
128                  operation, attributes, key, key_length, alg ) );
129 }
130 
mbedtls_test_transparent_cipher_decrypt_setup(mbedtls_transparent_test_driver_cipher_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg)131 psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
132     mbedtls_transparent_test_driver_cipher_operation_t *operation,
133     const psa_key_attributes_t *attributes,
134     const uint8_t *key, size_t key_length,
135     psa_algorithm_t alg)
136 {
137     mbedtls_test_driver_cipher_hooks.hits++;
138 
139     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
140         return( mbedtls_test_driver_cipher_hooks.forced_status );
141 
142     return ( mbedtls_transparent_test_driver_cipher_decrypt_setup(
143                  operation, attributes, key, key_length, alg ) );
144 }
145 
mbedtls_test_transparent_cipher_abort(mbedtls_transparent_test_driver_cipher_operation_t * operation)146 psa_status_t mbedtls_test_transparent_cipher_abort(
147     mbedtls_transparent_test_driver_cipher_operation_t *operation)
148 {
149     mbedtls_test_driver_cipher_hooks.hits++;
150 
151     if( operation->alg == 0 )
152         return( PSA_SUCCESS );
153 
154     mbedtls_transparent_test_driver_cipher_abort( operation );
155 
156     /* Wiping the entire struct here, instead of member-by-member. This is
157      * useful for the test suite, since it gives a chance of catching memory
158      * corruption errors should the core not have allocated (enough) memory for
159      * our context struct. */
160     memset( operation, 0, sizeof( *operation ) );
161 
162     return( mbedtls_test_driver_cipher_hooks.forced_status );
163 }
164 
mbedtls_test_transparent_cipher_set_iv(mbedtls_transparent_test_driver_cipher_operation_t * operation,const uint8_t * iv,size_t iv_length)165 psa_status_t mbedtls_test_transparent_cipher_set_iv(
166     mbedtls_transparent_test_driver_cipher_operation_t *operation,
167     const uint8_t *iv,
168     size_t iv_length)
169 {
170     mbedtls_test_driver_cipher_hooks.hits++;
171 
172     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
173         return( mbedtls_test_driver_cipher_hooks.forced_status );
174 
175     return( mbedtls_transparent_test_driver_cipher_set_iv(
176                 operation, iv, iv_length ) );
177 }
178 
mbedtls_test_transparent_cipher_update(mbedtls_transparent_test_driver_cipher_operation_t * operation,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)179 psa_status_t mbedtls_test_transparent_cipher_update(
180     mbedtls_transparent_test_driver_cipher_operation_t *operation,
181     const uint8_t *input,
182     size_t input_length,
183     uint8_t *output,
184     size_t output_size,
185     size_t *output_length)
186 {
187     mbedtls_test_driver_cipher_hooks.hits++;
188 
189     if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
190     {
191         if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
192             return PSA_ERROR_BUFFER_TOO_SMALL;
193 
194         memcpy( output,
195                 mbedtls_test_driver_cipher_hooks.forced_output,
196                 mbedtls_test_driver_cipher_hooks.forced_output_length );
197         *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
198 
199         return( mbedtls_test_driver_cipher_hooks.forced_status );
200     }
201 
202     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
203         return( mbedtls_test_driver_cipher_hooks.forced_status );
204 
205     return( mbedtls_transparent_test_driver_cipher_update(
206                 operation, input, input_length,
207                 output, output_size, output_length ) );
208 }
209 
mbedtls_test_transparent_cipher_finish(mbedtls_transparent_test_driver_cipher_operation_t * operation,uint8_t * output,size_t output_size,size_t * output_length)210 psa_status_t mbedtls_test_transparent_cipher_finish(
211     mbedtls_transparent_test_driver_cipher_operation_t *operation,
212     uint8_t *output,
213     size_t output_size,
214     size_t *output_length)
215 {
216     mbedtls_test_driver_cipher_hooks.hits++;
217 
218     if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
219     {
220         if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
221             return PSA_ERROR_BUFFER_TOO_SMALL;
222 
223         memcpy( output,
224                 mbedtls_test_driver_cipher_hooks.forced_output,
225                 mbedtls_test_driver_cipher_hooks.forced_output_length );
226         *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
227 
228         return( mbedtls_test_driver_cipher_hooks.forced_status );
229     }
230 
231     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
232         return( mbedtls_test_driver_cipher_hooks.forced_status );
233 
234     return( mbedtls_transparent_test_driver_cipher_finish(
235                 operation, output, output_size, output_length ) );
236 }
237 
238 /*
239  * opaque versions, to do
240  */
mbedtls_test_opaque_cipher_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,uint8_t * output,size_t output_size,size_t * output_length)241 psa_status_t mbedtls_test_opaque_cipher_encrypt(
242     const psa_key_attributes_t *attributes,
243     const uint8_t *key, size_t key_length,
244     psa_algorithm_t alg,
245     const uint8_t *input, size_t input_length,
246     uint8_t *output, size_t output_size, size_t *output_length)
247 {
248     (void) attributes;
249     (void) key;
250     (void) key_length;
251     (void) alg;
252     (void) input;
253     (void) input_length;
254     (void) output;
255     (void) output_size;
256     (void) output_length;
257     return( PSA_ERROR_NOT_SUPPORTED );
258 }
259 
mbedtls_test_opaque_cipher_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,uint8_t * output,size_t output_size,size_t * output_length)260 psa_status_t mbedtls_test_opaque_cipher_decrypt(
261     const psa_key_attributes_t *attributes,
262     const uint8_t *key, size_t key_length,
263     psa_algorithm_t alg,
264     const uint8_t *input, size_t input_length,
265     uint8_t *output, size_t output_size, size_t *output_length)
266 {
267     (void) attributes;
268     (void) key;
269     (void) key_length;
270     (void) alg;
271     (void) input;
272     (void) input_length;
273     (void) output;
274     (void) output_size;
275     (void) output_length;
276     return( PSA_ERROR_NOT_SUPPORTED );
277 }
278 
mbedtls_test_opaque_cipher_encrypt_setup(mbedtls_opaque_test_driver_cipher_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg)279 psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
280     mbedtls_opaque_test_driver_cipher_operation_t *operation,
281     const psa_key_attributes_t *attributes,
282     const uint8_t *key, size_t key_length,
283     psa_algorithm_t alg)
284 {
285     (void) operation;
286     (void) attributes;
287     (void) key;
288     (void) key_length;
289     (void) alg;
290     return( PSA_ERROR_NOT_SUPPORTED );
291 }
292 
mbedtls_test_opaque_cipher_decrypt_setup(mbedtls_opaque_test_driver_cipher_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg)293 psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
294     mbedtls_opaque_test_driver_cipher_operation_t *operation,
295     const psa_key_attributes_t *attributes,
296     const uint8_t *key, size_t key_length,
297     psa_algorithm_t alg)
298 {
299     (void) operation;
300     (void) attributes;
301     (void) key;
302     (void) key_length;
303     (void) alg;
304     return( PSA_ERROR_NOT_SUPPORTED );
305 }
306 
mbedtls_test_opaque_cipher_abort(mbedtls_opaque_test_driver_cipher_operation_t * operation)307 psa_status_t mbedtls_test_opaque_cipher_abort(
308     mbedtls_opaque_test_driver_cipher_operation_t *operation )
309 {
310     (void) operation;
311     return( PSA_ERROR_NOT_SUPPORTED );
312 }
313 
mbedtls_test_opaque_cipher_set_iv(mbedtls_opaque_test_driver_cipher_operation_t * operation,const uint8_t * iv,size_t iv_length)314 psa_status_t mbedtls_test_opaque_cipher_set_iv(
315     mbedtls_opaque_test_driver_cipher_operation_t *operation,
316     const uint8_t *iv,
317     size_t iv_length)
318 {
319     (void) operation;
320     (void) iv;
321     (void) iv_length;
322     return( PSA_ERROR_NOT_SUPPORTED );
323 }
324 
mbedtls_test_opaque_cipher_update(mbedtls_opaque_test_driver_cipher_operation_t * operation,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)325 psa_status_t mbedtls_test_opaque_cipher_update(
326     mbedtls_opaque_test_driver_cipher_operation_t *operation,
327     const uint8_t *input,
328     size_t input_length,
329     uint8_t *output,
330     size_t output_size,
331     size_t *output_length)
332 {
333     (void) operation;
334     (void) input;
335     (void) input_length;
336     (void) output;
337     (void) output_size;
338     (void) output_length;
339     return( PSA_ERROR_NOT_SUPPORTED );
340 }
341 
mbedtls_test_opaque_cipher_finish(mbedtls_opaque_test_driver_cipher_operation_t * operation,uint8_t * output,size_t output_size,size_t * output_length)342 psa_status_t mbedtls_test_opaque_cipher_finish(
343     mbedtls_opaque_test_driver_cipher_operation_t *operation,
344     uint8_t *output,
345     size_t output_size,
346     size_t *output_length)
347 {
348     (void) operation;
349     (void) output;
350     (void) output_size;
351     (void) output_length;
352     return( PSA_ERROR_NOT_SUPPORTED );
353 }
354 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
355