1 /*
2  * Test driver for key agreement 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 
24 #include "psa/crypto.h"
25 #include "psa_crypto_core.h"
26 #include "psa_crypto_ecp.h"
27 
28 #include "test/drivers/key_agreement.h"
29 #include "test/drivers/test_driver.h"
30 
31 #include <string.h>
32 
33 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
34 #include "libtestdriver1/include/psa/crypto.h"
35 #include "libtestdriver1/library/psa_crypto_ecp.h"
36 #endif
37 
38 mbedtls_test_driver_key_agreement_hooks_t
39     mbedtls_test_driver_key_agreement_hooks = MBEDTLS_TEST_DRIVER_KEY_AGREEMENT_INIT;
40 
mbedtls_test_transparent_key_agreement(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * peer_key,size_t peer_key_length,uint8_t * shared_secret,size_t shared_secret_size,size_t * shared_secret_length)41 psa_status_t mbedtls_test_transparent_key_agreement(
42     const psa_key_attributes_t *attributes,
43     const uint8_t *key_buffer,
44     size_t key_buffer_size,
45     psa_algorithm_t alg,
46     const uint8_t *peer_key,
47     size_t peer_key_length,
48     uint8_t *shared_secret,
49     size_t shared_secret_size,
50     size_t *shared_secret_length)
51 {
52     mbedtls_test_driver_key_agreement_hooks.hits++;
53 
54     if (mbedtls_test_driver_key_agreement_hooks.forced_status != PSA_SUCCESS) {
55         return mbedtls_test_driver_key_agreement_hooks.forced_status;
56     }
57 
58     if (mbedtls_test_driver_key_agreement_hooks.forced_output != NULL) {
59         if (mbedtls_test_driver_key_agreement_hooks.forced_output_length > shared_secret_size) {
60             return PSA_ERROR_BUFFER_TOO_SMALL;
61         }
62 
63         memcpy(shared_secret, mbedtls_test_driver_key_agreement_hooks.forced_output,
64                mbedtls_test_driver_key_agreement_hooks.forced_output_length);
65         *shared_secret_length = mbedtls_test_driver_key_agreement_hooks.forced_output_length;
66 
67         return PSA_SUCCESS;
68     }
69 
70     if (PSA_ALG_IS_ECDH(alg)) {
71 #if (defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
72         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_ALG_ECDH))
73         return libtestdriver1_mbedtls_psa_key_agreement_ecdh(
74             (const libtestdriver1_psa_key_attributes_t *) attributes,
75             key_buffer, key_buffer_size,
76             alg, peer_key, peer_key_length,
77             shared_secret, shared_secret_size,
78             shared_secret_length);
79 #elif defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
80         return mbedtls_psa_key_agreement_ecdh(
81             attributes,
82             key_buffer, key_buffer_size,
83             alg, peer_key, peer_key_length,
84             shared_secret, shared_secret_size,
85             shared_secret_length);
86 #else
87         (void) attributes;
88         (void) key_buffer;
89         (void) key_buffer_size;
90         (void) peer_key;
91         (void) peer_key_length;
92         (void) shared_secret;
93         (void) shared_secret_size;
94         (void) shared_secret_length;
95         return PSA_ERROR_NOT_SUPPORTED;
96 #endif
97     } else {
98         return PSA_ERROR_INVALID_ARGUMENT;
99     }
100 
101 }
102 
mbedtls_test_opaque_key_agreement(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * peer_key,size_t peer_key_length,uint8_t * shared_secret,size_t shared_secret_size,size_t * shared_secret_length)103 psa_status_t mbedtls_test_opaque_key_agreement(
104     const psa_key_attributes_t *attributes,
105     const uint8_t *key_buffer,
106     size_t key_buffer_size,
107     psa_algorithm_t alg,
108     const uint8_t *peer_key,
109     size_t peer_key_length,
110     uint8_t *shared_secret,
111     size_t shared_secret_size,
112     size_t *shared_secret_length)
113 {
114     (void) attributes;
115     (void) key_buffer;
116     (void) key_buffer_size;
117     (void) alg;
118     (void) peer_key;
119     (void) peer_key_length;
120     (void) shared_secret;
121     (void) shared_secret_size;
122     (void) shared_secret_length;
123     return PSA_ERROR_NOT_SUPPORTED;
124 }
125 
126 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
127