blob: 5cdaa33ae738062841dd6ef621f443b1d6f47173 [file] [log] [blame]
Przemyslaw Stekielb6a66502021-12-09 11:11:54 +01001/*
2 * Test driver for rsa functions.
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
28#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
29#include "libtestdriver1/library/psa_crypto_rsa_crypto.h"
30#endif
31
32mbedtls_test_driver_rsa_hooks_t mbedtls_test_driver_rsa_hooks =
33 MBEDTLS_TEST_DRIVER_RSA_INIT;
34
35psa_status_t mbedtls_test_transparent_asymmetric_encrypt(
36 const psa_key_attributes_t *attributes,
37 const uint8_t *key_buffer,
38 size_t key_buffer_size,
39 psa_algorithm_t alg,
40 const uint8_t *input,
41 size_t input_length,
42 const uint8_t *salt,
43 size_t salt_length,
44 uint8_t *output,
45 size_t output_size,
46 size_t *output_length )
47{
48 mbedtls_test_driver_rsa_hooks.hits++;
49
50 if( mbedtls_test_driver_rsa_hooks.forced_output != NULL )
51 {
52 if( output_size < mbedtls_test_driver_rsa_hooks.forced_output_length )
53 return( PSA_ERROR_BUFFER_TOO_SMALL );
54
55 memcpy( output,
56 mbedtls_test_driver_rsa_hooks.forced_output,
57 mbedtls_test_driver_rsa_hooks.forced_output_length );
58 *output_length = mbedtls_test_driver_rsa_hooks.forced_output_length;
59
60 return( mbedtls_test_driver_rsa_hooks.forced_status );
61 }
62
63 if( mbedtls_test_driver_rsa_hooks.forced_status != PSA_SUCCESS )
64 return( mbedtls_test_driver_rsa_hooks.forced_status );
65
66#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
67 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
68 return( libtestdriver1_mbedtls_psa_asymmetric_encrypt(
69 (const libtestdriver1_psa_key_attributes_t *)attributes,
70 key_buffer, key_buffer_size,
71 alg, input, input_length, salt, salt_length,
72 output, output_size, output_length ) );
73#else
74 return( mbedtls_psa_asymmetric_encrypt(
75 attributes, key_buffer, key_buffer_size,
76 alg, input, input_length, salt, salt_length,
77 output, output_size, output_length ) );
78#endif
79
80 return( PSA_ERROR_NOT_SUPPORTED );
81}
82
83/*
84 * opaque versions, to do
85 */
86psa_status_t mbedtls_test_opaque_asymmetric_encrypt(
87 const psa_key_attributes_t *attributes,
88 const uint8_t *key,
89 size_t key_length,
90 psa_algorithm_t alg,
91 const uint8_t *input,
92 size_t input_length,
93 const uint8_t *salt,
94 size_t salt_length,
95 uint8_t *output,
96 size_t output_size,
97 size_t *output_length)
98{
99 (void) attributes;
100 (void) key;
101 (void) key_length;
102 (void) alg;
103 (void) input;
104 (void) input_length;
105 (void) salt;
106 (void) salt_length;
107 (void) output;
108 (void) output_size;
109 (void) output_length;
110 return( PSA_ERROR_NOT_SUPPORTED );
111}
112
113
114#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */