Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 1 | /* |
| 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" |
Przemyslaw Stekiel | 2d18c7e | 2021-12-22 12:02:03 +0100 | [diff] [blame] | 27 | #include "test/drivers/asym.h" |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 28 | |
| 29 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) |
Przemyslaw Stekiel | 2ce7c9c | 2022-01-03 09:19:19 +0100 | [diff] [blame] | 30 | #include "libtestdriver1/library/psa_crypto_rsa.h" |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | mbedtls_test_driver_rsa_hooks_t mbedtls_test_driver_rsa_hooks = |
| 34 | MBEDTLS_TEST_DRIVER_RSA_INIT; |
| 35 | |
| 36 | psa_status_t mbedtls_test_transparent_asymmetric_encrypt( |
Przemyslaw Stekiel | 4576b91 | 2022-02-02 11:10:46 +0100 | [diff] [blame^] | 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 ) |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 41 | { |
| 42 | mbedtls_test_driver_rsa_hooks.hits++; |
| 43 | |
| 44 | if( mbedtls_test_driver_rsa_hooks.forced_output != NULL ) |
| 45 | { |
| 46 | if( output_size < mbedtls_test_driver_rsa_hooks.forced_output_length ) |
| 47 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 48 | |
| 49 | memcpy( output, |
| 50 | mbedtls_test_driver_rsa_hooks.forced_output, |
| 51 | mbedtls_test_driver_rsa_hooks.forced_output_length ); |
| 52 | *output_length = mbedtls_test_driver_rsa_hooks.forced_output_length; |
| 53 | |
| 54 | return( mbedtls_test_driver_rsa_hooks.forced_status ); |
| 55 | } |
| 56 | |
| 57 | if( mbedtls_test_driver_rsa_hooks.forced_status != PSA_SUCCESS ) |
| 58 | return( mbedtls_test_driver_rsa_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 | |
Przemyslaw Stekiel | 71284ea | 2021-12-13 09:00:52 +0100 | [diff] [blame] | 77 | psa_status_t mbedtls_test_transparent_asymmetric_decrypt( |
Przemyslaw Stekiel | 4576b91 | 2022-02-02 11:10:46 +0100 | [diff] [blame^] | 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 ) |
Przemyslaw Stekiel | 71284ea | 2021-12-13 09:00:52 +0100 | [diff] [blame] | 82 | { |
| 83 | mbedtls_test_driver_rsa_hooks.hits++; |
| 84 | |
| 85 | if( mbedtls_test_driver_rsa_hooks.forced_output != NULL ) |
| 86 | { |
| 87 | if( output_size < mbedtls_test_driver_rsa_hooks.forced_output_length ) |
| 88 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 89 | |
| 90 | memcpy( output, |
| 91 | mbedtls_test_driver_rsa_hooks.forced_output, |
| 92 | mbedtls_test_driver_rsa_hooks.forced_output_length ); |
| 93 | *output_length = mbedtls_test_driver_rsa_hooks.forced_output_length; |
| 94 | |
| 95 | return( mbedtls_test_driver_rsa_hooks.forced_status ); |
| 96 | } |
| 97 | |
| 98 | if( mbedtls_test_driver_rsa_hooks.forced_status != PSA_SUCCESS ) |
| 99 | return( mbedtls_test_driver_rsa_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 | |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 118 | /* |
| 119 | * opaque versions, to do |
| 120 | */ |
| 121 | psa_status_t mbedtls_test_opaque_asymmetric_encrypt( |
Przemyslaw Stekiel | 4576b91 | 2022-02-02 11:10:46 +0100 | [diff] [blame^] | 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 ) |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 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 | |
Przemyslaw Stekiel | 71284ea | 2021-12-13 09:00:52 +0100 | [diff] [blame] | 141 | psa_status_t mbedtls_test_opaque_asymmetric_decrypt( |
Przemyslaw Stekiel | 4576b91 | 2022-02-02 11:10:46 +0100 | [diff] [blame^] | 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 ) |
Przemyslaw Stekiel | 71284ea | 2021-12-13 09:00:52 +0100 | [diff] [blame] | 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 | } |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 160 | |
| 161 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |