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 #if !defined(MBEDTLS_CONFIG_FILE)
22 #include "mbedtls/config.h"
23 #else
24 #include MBEDTLS_CONFIG_FILE
25 #endif
26 
27 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
28 #include "psa/crypto.h"
29 #include "psa_crypto_cipher.h"
30 #include "psa_crypto_core.h"
31 #include "mbedtls/cipher.h"
32 
33 #include "test/drivers/cipher.h"
34 
35 #include "test/random.h"
36 
37 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
38 #include "libtestdriver1/library/psa_crypto_cipher.h"
39 #endif
40 
41 #include <string.h>
42 
43 mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks =
44     MBEDTLS_TEST_DRIVER_CIPHER_INIT;
45 
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 * iv,size_t iv_length,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)46 psa_status_t mbedtls_test_transparent_cipher_encrypt(
47     const psa_key_attributes_t *attributes,
48     const uint8_t *key_buffer,
49     size_t key_buffer_size,
50     psa_algorithm_t alg,
51     const uint8_t *iv,
52     size_t iv_length,
53     const uint8_t *input,
54     size_t input_length,
55     uint8_t *output,
56     size_t output_size,
57     size_t *output_length )
58 {
59     mbedtls_test_driver_cipher_hooks.hits++;
60 
61     if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
62     {
63         if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
64             return( PSA_ERROR_BUFFER_TOO_SMALL );
65 
66         memcpy( output,
67                 mbedtls_test_driver_cipher_hooks.forced_output,
68                 mbedtls_test_driver_cipher_hooks.forced_output_length );
69         *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
70 
71         return( mbedtls_test_driver_cipher_hooks.forced_status );
72     }
73 
74     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
75         return( mbedtls_test_driver_cipher_hooks.forced_status );
76 
77 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
78     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
79     return( libtestdriver1_mbedtls_psa_cipher_encrypt(
80                 (const libtestdriver1_psa_key_attributes_t *)attributes,
81                 key_buffer, key_buffer_size,
82                 alg, iv, iv_length, input, input_length,
83                 output, output_size, output_length ) );
84 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
85     return( mbedtls_psa_cipher_encrypt(
86                 attributes, key_buffer, key_buffer_size,
87                 alg, iv, iv_length, input, input_length,
88                 output, output_size, output_length ) );
89 #endif
90 
91     return( PSA_ERROR_NOT_SUPPORTED );
92 }
93 
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)94 psa_status_t mbedtls_test_transparent_cipher_decrypt(
95     const psa_key_attributes_t *attributes,
96     const uint8_t *key_buffer,
97     size_t key_buffer_size,
98     psa_algorithm_t alg,
99     const uint8_t *input,
100     size_t input_length,
101     uint8_t *output,
102     size_t output_size,
103     size_t *output_length )
104 {
105    mbedtls_test_driver_cipher_hooks.hits++;
106 
107     if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
108     {
109         if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
110             return( PSA_ERROR_BUFFER_TOO_SMALL );
111 
112         memcpy( output,
113                 mbedtls_test_driver_cipher_hooks.forced_output,
114                 mbedtls_test_driver_cipher_hooks.forced_output_length );
115         *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
116 
117         return( mbedtls_test_driver_cipher_hooks.forced_status );
118     }
119 
120     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
121         return( mbedtls_test_driver_cipher_hooks.forced_status );
122 
123 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
124     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
125     return( libtestdriver1_mbedtls_psa_cipher_decrypt(
126                 (const libtestdriver1_psa_key_attributes_t *)attributes,
127                 key_buffer, key_buffer_size,
128                 alg, input, input_length,
129                 output, output_size, output_length ) );
130 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
131     return( mbedtls_psa_cipher_decrypt(
132                 attributes, key_buffer, key_buffer_size,
133                 alg, input, input_length,
134                 output, output_size, output_length ) );
135 #endif
136 
137     return( PSA_ERROR_NOT_SUPPORTED );
138 }
139 
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)140 psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
141     mbedtls_transparent_test_driver_cipher_operation_t *operation,
142     const psa_key_attributes_t *attributes,
143     const uint8_t *key, size_t key_length,
144     psa_algorithm_t alg)
145 {
146     mbedtls_test_driver_cipher_hooks.hits++;
147 
148     /* Wiping the entire struct here, instead of member-by-member. This is
149      * useful for the test suite, since it gives a chance of catching memory
150      * corruption errors should the core not have allocated (enough) memory for
151      * our context struct. */
152     memset( operation, 0, sizeof( *operation ) );
153 
154     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
155         return( mbedtls_test_driver_cipher_hooks.forced_status );
156 
157 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
158     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
159     return( libtestdriver1_mbedtls_psa_cipher_encrypt_setup(
160                 operation,
161                 (const libtestdriver1_psa_key_attributes_t *)attributes,
162                 key, key_length, alg ) );
163 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
164     return( mbedtls_psa_cipher_encrypt_setup(
165                 operation, attributes, key, key_length, alg ) );
166 #endif
167 
168     return( PSA_ERROR_NOT_SUPPORTED );
169 }
170 
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)171 psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
172     mbedtls_transparent_test_driver_cipher_operation_t *operation,
173     const psa_key_attributes_t *attributes,
174     const uint8_t *key, size_t key_length,
175     psa_algorithm_t alg)
176 {
177     mbedtls_test_driver_cipher_hooks.hits++;
178 
179     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
180         return( mbedtls_test_driver_cipher_hooks.forced_status );
181 
182 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
183     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
184     return( libtestdriver1_mbedtls_psa_cipher_decrypt_setup(
185                 operation,
186                 (const libtestdriver1_psa_key_attributes_t *)attributes,
187                 key, key_length, alg ) );
188 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
189     return( mbedtls_psa_cipher_decrypt_setup(
190                 operation, attributes, key, key_length, alg ) );
191 #endif
192 
193     return( PSA_ERROR_NOT_SUPPORTED );
194 }
195 
mbedtls_test_transparent_cipher_abort(mbedtls_transparent_test_driver_cipher_operation_t * operation)196 psa_status_t mbedtls_test_transparent_cipher_abort(
197     mbedtls_transparent_test_driver_cipher_operation_t *operation)
198 {
199     mbedtls_test_driver_cipher_hooks.hits++;
200 
201     if( operation->alg == 0 )
202         return( PSA_SUCCESS );
203 
204 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
205     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
206     libtestdriver1_mbedtls_psa_cipher_abort( operation );
207 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
208     mbedtls_psa_cipher_abort( operation );
209 #endif
210 
211     /* Wiping the entire struct here, instead of member-by-member. This is
212      * useful for the test suite, since it gives a chance of catching memory
213      * corruption errors should the core not have allocated (enough) memory for
214      * our context struct. */
215     memset( operation, 0, sizeof( *operation ) );
216 
217     return( mbedtls_test_driver_cipher_hooks.forced_status );
218 }
219 
mbedtls_test_transparent_cipher_set_iv(mbedtls_transparent_test_driver_cipher_operation_t * operation,const uint8_t * iv,size_t iv_length)220 psa_status_t mbedtls_test_transparent_cipher_set_iv(
221     mbedtls_transparent_test_driver_cipher_operation_t *operation,
222     const uint8_t *iv,
223     size_t iv_length)
224 {
225     mbedtls_test_driver_cipher_hooks.hits++;
226 
227     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
228         return( mbedtls_test_driver_cipher_hooks.forced_status );
229 
230 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
231     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
232     return( libtestdriver1_mbedtls_psa_cipher_set_iv(
233                 operation, iv, iv_length ) );
234 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
235     return( mbedtls_psa_cipher_set_iv( operation, iv, iv_length ) );
236 #endif
237 
238     return( PSA_ERROR_NOT_SUPPORTED );
239 }
240 
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)241 psa_status_t mbedtls_test_transparent_cipher_update(
242     mbedtls_transparent_test_driver_cipher_operation_t *operation,
243     const uint8_t *input,
244     size_t input_length,
245     uint8_t *output,
246     size_t output_size,
247     size_t *output_length)
248 {
249     mbedtls_test_driver_cipher_hooks.hits++;
250 
251     if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
252     {
253         if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
254             return PSA_ERROR_BUFFER_TOO_SMALL;
255 
256         memcpy( output,
257                 mbedtls_test_driver_cipher_hooks.forced_output,
258                 mbedtls_test_driver_cipher_hooks.forced_output_length );
259         *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
260 
261         return( mbedtls_test_driver_cipher_hooks.forced_status );
262     }
263 
264     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
265         return( mbedtls_test_driver_cipher_hooks.forced_status );
266 
267 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
268     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
269     return( libtestdriver1_mbedtls_psa_cipher_update(
270                 operation, input, input_length,
271                 output, output_size, output_length ) );
272 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
273     return( mbedtls_psa_cipher_update(
274                 operation, input, input_length,
275                 output, output_size, output_length ) );
276 #endif
277 
278     return( PSA_ERROR_NOT_SUPPORTED );
279 }
280 
mbedtls_test_transparent_cipher_finish(mbedtls_transparent_test_driver_cipher_operation_t * operation,uint8_t * output,size_t output_size,size_t * output_length)281 psa_status_t mbedtls_test_transparent_cipher_finish(
282     mbedtls_transparent_test_driver_cipher_operation_t *operation,
283     uint8_t *output,
284     size_t output_size,
285     size_t *output_length)
286 {
287     mbedtls_test_driver_cipher_hooks.hits++;
288 
289     if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
290     {
291         if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
292             return PSA_ERROR_BUFFER_TOO_SMALL;
293 
294         memcpy( output,
295                 mbedtls_test_driver_cipher_hooks.forced_output,
296                 mbedtls_test_driver_cipher_hooks.forced_output_length );
297         *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
298 
299         return( mbedtls_test_driver_cipher_hooks.forced_status );
300     }
301 
302     if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
303         return( mbedtls_test_driver_cipher_hooks.forced_status );
304 
305 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
306     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
307     return( libtestdriver1_mbedtls_psa_cipher_finish(
308                 operation, output, output_size, output_length ) );
309 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
310     return( mbedtls_psa_cipher_finish(
311                 operation, output, output_size, output_length ) );
312 #endif
313 
314     return( PSA_ERROR_NOT_SUPPORTED );
315 }
316 
317 /*
318  * opaque versions, to do
319  */
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 * iv,size_t iv_length,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)320 psa_status_t mbedtls_test_opaque_cipher_encrypt(
321     const psa_key_attributes_t *attributes,
322     const uint8_t *key, size_t key_length,
323     psa_algorithm_t alg,
324     const uint8_t *iv, size_t iv_length,
325     const uint8_t *input, size_t input_length,
326     uint8_t *output, size_t output_size, size_t *output_length)
327 {
328     (void) attributes;
329     (void) key;
330     (void) key_length;
331     (void) alg;
332     (void) iv;
333     (void) iv_length;
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_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)342 psa_status_t mbedtls_test_opaque_cipher_decrypt(
343     const psa_key_attributes_t *attributes,
344     const uint8_t *key, size_t key_length,
345     psa_algorithm_t alg,
346     const uint8_t *input, size_t input_length,
347     uint8_t *output, size_t output_size, size_t *output_length)
348 {
349     (void) attributes;
350     (void) key;
351     (void) key_length;
352     (void) alg;
353     (void) input;
354     (void) input_length;
355     (void) output;
356     (void) output_size;
357     (void) output_length;
358     return( PSA_ERROR_NOT_SUPPORTED );
359 }
360 
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)361 psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
362     mbedtls_opaque_test_driver_cipher_operation_t *operation,
363     const psa_key_attributes_t *attributes,
364     const uint8_t *key, size_t key_length,
365     psa_algorithm_t alg)
366 {
367     (void) operation;
368     (void) attributes;
369     (void) key;
370     (void) key_length;
371     (void) alg;
372     return( PSA_ERROR_NOT_SUPPORTED );
373 }
374 
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)375 psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
376     mbedtls_opaque_test_driver_cipher_operation_t *operation,
377     const psa_key_attributes_t *attributes,
378     const uint8_t *key, size_t key_length,
379     psa_algorithm_t alg)
380 {
381     (void) operation;
382     (void) attributes;
383     (void) key;
384     (void) key_length;
385     (void) alg;
386     return( PSA_ERROR_NOT_SUPPORTED );
387 }
388 
mbedtls_test_opaque_cipher_abort(mbedtls_opaque_test_driver_cipher_operation_t * operation)389 psa_status_t mbedtls_test_opaque_cipher_abort(
390     mbedtls_opaque_test_driver_cipher_operation_t *operation )
391 {
392     (void) operation;
393     return( PSA_ERROR_NOT_SUPPORTED );
394 }
395 
mbedtls_test_opaque_cipher_set_iv(mbedtls_opaque_test_driver_cipher_operation_t * operation,const uint8_t * iv,size_t iv_length)396 psa_status_t mbedtls_test_opaque_cipher_set_iv(
397     mbedtls_opaque_test_driver_cipher_operation_t *operation,
398     const uint8_t *iv,
399     size_t iv_length)
400 {
401     (void) operation;
402     (void) iv;
403     (void) iv_length;
404     return( PSA_ERROR_NOT_SUPPORTED );
405 }
406 
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)407 psa_status_t mbedtls_test_opaque_cipher_update(
408     mbedtls_opaque_test_driver_cipher_operation_t *operation,
409     const uint8_t *input,
410     size_t input_length,
411     uint8_t *output,
412     size_t output_size,
413     size_t *output_length)
414 {
415     (void) operation;
416     (void) input;
417     (void) input_length;
418     (void) output;
419     (void) output_size;
420     (void) output_length;
421     return( PSA_ERROR_NOT_SUPPORTED );
422 }
423 
mbedtls_test_opaque_cipher_finish(mbedtls_opaque_test_driver_cipher_operation_t * operation,uint8_t * output,size_t output_size,size_t * output_length)424 psa_status_t mbedtls_test_opaque_cipher_finish(
425     mbedtls_opaque_test_driver_cipher_operation_t *operation,
426     uint8_t *output,
427     size_t output_size,
428     size_t *output_length)
429 {
430     (void) operation;
431     (void) output;
432     (void) output_size;
433     (void) output_length;
434     return( PSA_ERROR_NOT_SUPPORTED );
435 }
436 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
437