1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci * Test driver for asymmetric encryption.
3a8e1175bSopenharmony_ci */
4a8e1175bSopenharmony_ci/*  Copyright The Mbed TLS Contributors
5a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6a8e1175bSopenharmony_ci */
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_ci#include <test/helpers.h>
9a8e1175bSopenharmony_ci
10a8e1175bSopenharmony_ci#if defined(PSA_CRYPTO_DRIVER_TEST)
11a8e1175bSopenharmony_ci#include "psa/crypto.h"
12a8e1175bSopenharmony_ci#include "mbedtls/rsa.h"
13a8e1175bSopenharmony_ci#include "psa_crypto_rsa.h"
14a8e1175bSopenharmony_ci#include "string.h"
15a8e1175bSopenharmony_ci#include "test/drivers/asymmetric_encryption.h"
16a8e1175bSopenharmony_ci#include "test/drivers/key_management.h"
17a8e1175bSopenharmony_ci
18a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
19a8e1175bSopenharmony_ci#include "libtestdriver1/library/psa_crypto_rsa.h"
20a8e1175bSopenharmony_ci#endif
21a8e1175bSopenharmony_ci
22a8e1175bSopenharmony_ci#define PSA_RSA_KEY_PAIR_MAX_SIZE \
23a8e1175bSopenharmony_ci    PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
24a8e1175bSopenharmony_ci
25a8e1175bSopenharmony_cimbedtls_test_driver_asymmetric_encryption_hooks_t mbedtls_test_driver_asymmetric_encryption_hooks =
26a8e1175bSopenharmony_ci    MBEDTLS_TEST_DRIVER_ASYMMETRIC_ENCRYPTION_INIT;
27a8e1175bSopenharmony_ci
28a8e1175bSopenharmony_cipsa_status_t mbedtls_test_transparent_asymmetric_encrypt(
29a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
30a8e1175bSopenharmony_ci    size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
31a8e1175bSopenharmony_ci    size_t input_length, const uint8_t *salt, size_t salt_length,
32a8e1175bSopenharmony_ci    uint8_t *output, size_t output_size, size_t *output_length)
33a8e1175bSopenharmony_ci{
34a8e1175bSopenharmony_ci    mbedtls_test_driver_asymmetric_encryption_hooks.hits++;
35a8e1175bSopenharmony_ci
36a8e1175bSopenharmony_ci    if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL) {
37a8e1175bSopenharmony_ci        if (output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length) {
38a8e1175bSopenharmony_ci            return PSA_ERROR_BUFFER_TOO_SMALL;
39a8e1175bSopenharmony_ci        }
40a8e1175bSopenharmony_ci
41a8e1175bSopenharmony_ci        memcpy(output,
42a8e1175bSopenharmony_ci               mbedtls_test_driver_asymmetric_encryption_hooks.forced_output,
43a8e1175bSopenharmony_ci               mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length);
44a8e1175bSopenharmony_ci        *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length;
45a8e1175bSopenharmony_ci
46a8e1175bSopenharmony_ci        return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
47a8e1175bSopenharmony_ci    }
48a8e1175bSopenharmony_ci
49a8e1175bSopenharmony_ci    if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS) {
50a8e1175bSopenharmony_ci        return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
51a8e1175bSopenharmony_ci    }
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
54a8e1175bSopenharmony_ci    return libtestdriver1_mbedtls_psa_asymmetric_encrypt(
55a8e1175bSopenharmony_ci        (const libtestdriver1_psa_key_attributes_t *) attributes,
56a8e1175bSopenharmony_ci        key_buffer, key_buffer_size,
57a8e1175bSopenharmony_ci        alg, input, input_length, salt, salt_length,
58a8e1175bSopenharmony_ci        output, output_size, output_length);
59a8e1175bSopenharmony_ci#else
60a8e1175bSopenharmony_ci    return mbedtls_psa_asymmetric_encrypt(
61a8e1175bSopenharmony_ci        attributes, key_buffer, key_buffer_size,
62a8e1175bSopenharmony_ci        alg, input, input_length, salt, salt_length,
63a8e1175bSopenharmony_ci        output, output_size, output_length);
64a8e1175bSopenharmony_ci#endif
65a8e1175bSopenharmony_ci
66a8e1175bSopenharmony_ci    return PSA_ERROR_NOT_SUPPORTED;
67a8e1175bSopenharmony_ci}
68a8e1175bSopenharmony_ci
69a8e1175bSopenharmony_cipsa_status_t mbedtls_test_transparent_asymmetric_decrypt(
70a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
71a8e1175bSopenharmony_ci    size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
72a8e1175bSopenharmony_ci    size_t input_length, const uint8_t *salt, size_t salt_length,
73a8e1175bSopenharmony_ci    uint8_t *output, size_t output_size, size_t *output_length)
74a8e1175bSopenharmony_ci{
75a8e1175bSopenharmony_ci    mbedtls_test_driver_asymmetric_encryption_hooks.hits++;
76a8e1175bSopenharmony_ci
77a8e1175bSopenharmony_ci    if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL) {
78a8e1175bSopenharmony_ci        if (output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length) {
79a8e1175bSopenharmony_ci            return PSA_ERROR_BUFFER_TOO_SMALL;
80a8e1175bSopenharmony_ci        }
81a8e1175bSopenharmony_ci
82a8e1175bSopenharmony_ci        memcpy(output,
83a8e1175bSopenharmony_ci               mbedtls_test_driver_asymmetric_encryption_hooks.forced_output,
84a8e1175bSopenharmony_ci               mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length);
85a8e1175bSopenharmony_ci        *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length;
86a8e1175bSopenharmony_ci
87a8e1175bSopenharmony_ci        return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
88a8e1175bSopenharmony_ci    }
89a8e1175bSopenharmony_ci
90a8e1175bSopenharmony_ci    if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS) {
91a8e1175bSopenharmony_ci        return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status;
92a8e1175bSopenharmony_ci    }
93a8e1175bSopenharmony_ci
94a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
95a8e1175bSopenharmony_ci    return libtestdriver1_mbedtls_psa_asymmetric_decrypt(
96a8e1175bSopenharmony_ci        (const libtestdriver1_psa_key_attributes_t *) attributes,
97a8e1175bSopenharmony_ci        key_buffer, key_buffer_size,
98a8e1175bSopenharmony_ci        alg, input, input_length, salt, salt_length,
99a8e1175bSopenharmony_ci        output, output_size, output_length);
100a8e1175bSopenharmony_ci#else
101a8e1175bSopenharmony_ci    return mbedtls_psa_asymmetric_decrypt(
102a8e1175bSopenharmony_ci        attributes, key_buffer, key_buffer_size,
103a8e1175bSopenharmony_ci        alg, input, input_length, salt, salt_length,
104a8e1175bSopenharmony_ci        output, output_size, output_length);
105a8e1175bSopenharmony_ci#endif
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ci    return PSA_ERROR_NOT_SUPPORTED;
108a8e1175bSopenharmony_ci}
109a8e1175bSopenharmony_ci
110a8e1175bSopenharmony_ci/*
111a8e1175bSopenharmony_ci * opaque versions
112a8e1175bSopenharmony_ci */
113a8e1175bSopenharmony_cipsa_status_t mbedtls_test_opaque_asymmetric_encrypt(
114a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes, const uint8_t *key,
115a8e1175bSopenharmony_ci    size_t key_length, psa_algorithm_t alg, const uint8_t *input,
116a8e1175bSopenharmony_ci    size_t input_length, const uint8_t *salt, size_t salt_length,
117a8e1175bSopenharmony_ci    uint8_t *output, size_t output_size, size_t *output_length)
118a8e1175bSopenharmony_ci{
119a8e1175bSopenharmony_ci    unsigned char unwrapped_key[PSA_RSA_KEY_PAIR_MAX_SIZE];
120a8e1175bSopenharmony_ci    size_t unwrapped_key_length;
121a8e1175bSopenharmony_ci    psa_status_t status;
122a8e1175bSopenharmony_ci
123a8e1175bSopenharmony_ci    status = mbedtls_test_opaque_unwrap_key(key, key_length,
124a8e1175bSopenharmony_ci                                            unwrapped_key, sizeof(unwrapped_key),
125a8e1175bSopenharmony_ci                                            &unwrapped_key_length);
126a8e1175bSopenharmony_ci    if (status != PSA_SUCCESS) {
127a8e1175bSopenharmony_ci        return status;
128a8e1175bSopenharmony_ci    }
129a8e1175bSopenharmony_ci
130a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
131a8e1175bSopenharmony_ci    (defined(MBEDTLS_PSA_ACCEL_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_CRYPT))
132a8e1175bSopenharmony_ci    return libtestdriver1_mbedtls_psa_asymmetric_encrypt(
133a8e1175bSopenharmony_ci        (const libtestdriver1_psa_key_attributes_t *) attributes,
134a8e1175bSopenharmony_ci        unwrapped_key, unwrapped_key_length,
135a8e1175bSopenharmony_ci        alg, input, input_length, salt, salt_length,
136a8e1175bSopenharmony_ci        output, output_size, output_length);
137a8e1175bSopenharmony_ci#else
138a8e1175bSopenharmony_ci    return mbedtls_psa_asymmetric_encrypt(
139a8e1175bSopenharmony_ci        attributes, unwrapped_key, unwrapped_key_length,
140a8e1175bSopenharmony_ci        alg, input, input_length, salt, salt_length,
141a8e1175bSopenharmony_ci        output, output_size, output_length);
142a8e1175bSopenharmony_ci#endif
143a8e1175bSopenharmony_ci
144a8e1175bSopenharmony_ci    return PSA_ERROR_NOT_SUPPORTED;
145a8e1175bSopenharmony_ci}
146a8e1175bSopenharmony_ci
147a8e1175bSopenharmony_cipsa_status_t mbedtls_test_opaque_asymmetric_decrypt(
148a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes, const uint8_t *key,
149a8e1175bSopenharmony_ci    size_t key_length, psa_algorithm_t alg, const uint8_t *input,
150a8e1175bSopenharmony_ci    size_t input_length, const uint8_t *salt, size_t salt_length,
151a8e1175bSopenharmony_ci    uint8_t *output, size_t output_size, size_t *output_length)
152a8e1175bSopenharmony_ci{
153a8e1175bSopenharmony_ci    unsigned char unwrapped_key[PSA_RSA_KEY_PAIR_MAX_SIZE];
154a8e1175bSopenharmony_ci    size_t unwrapped_key_length;
155a8e1175bSopenharmony_ci    psa_status_t status;
156a8e1175bSopenharmony_ci
157a8e1175bSopenharmony_ci    status = mbedtls_test_opaque_unwrap_key(key, key_length,
158a8e1175bSopenharmony_ci                                            unwrapped_key, sizeof(unwrapped_key),
159a8e1175bSopenharmony_ci                                            &unwrapped_key_length);
160a8e1175bSopenharmony_ci    if (status != PSA_SUCCESS) {
161a8e1175bSopenharmony_ci        return status;
162a8e1175bSopenharmony_ci    }
163a8e1175bSopenharmony_ci
164a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
165a8e1175bSopenharmony_ci    (defined(MBEDTLS_PSA_ACCEL_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_CRYPT))
166a8e1175bSopenharmony_ci    return libtestdriver1_mbedtls_psa_asymmetric_decrypt(
167a8e1175bSopenharmony_ci        (const libtestdriver1_psa_key_attributes_t *) attributes,
168a8e1175bSopenharmony_ci        unwrapped_key, unwrapped_key_length,
169a8e1175bSopenharmony_ci        alg, input, input_length, salt, salt_length,
170a8e1175bSopenharmony_ci        output, output_size, output_length);
171a8e1175bSopenharmony_ci#else
172a8e1175bSopenharmony_ci    return mbedtls_psa_asymmetric_decrypt(
173a8e1175bSopenharmony_ci        attributes, unwrapped_key, unwrapped_key_length,
174a8e1175bSopenharmony_ci        alg, input, input_length, salt, salt_length,
175a8e1175bSopenharmony_ci        output, output_size, output_length);
176a8e1175bSopenharmony_ci#endif
177a8e1175bSopenharmony_ci
178a8e1175bSopenharmony_ci    return PSA_ERROR_NOT_SUPPORTED;
179a8e1175bSopenharmony_ci}
180a8e1175bSopenharmony_ci
181a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_DRIVER_TEST */
182