• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if( mbedtls_test_driver_key_agreement_hooks.forced_output != NULL )
58     {
59         if( mbedtls_test_driver_key_agreement_hooks.forced_output_length > shared_secret_size )
60             return( PSA_ERROR_BUFFER_TOO_SMALL );
61 
62         memcpy( shared_secret, mbedtls_test_driver_key_agreement_hooks.forced_output,
63                 mbedtls_test_driver_key_agreement_hooks.forced_output_length );
64         *shared_secret_length = mbedtls_test_driver_key_agreement_hooks.forced_output_length;
65 
66         return( PSA_SUCCESS );
67     }
68 
69     if( PSA_ALG_IS_ECDH(alg) )
70     {
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     }
98     else
99     {
100         return( PSA_ERROR_INVALID_ARGUMENT );
101     }
102 
103 }
104 
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)105 psa_status_t mbedtls_test_opaque_key_agreement(
106     const psa_key_attributes_t *attributes,
107     const uint8_t *key_buffer,
108     size_t key_buffer_size,
109     psa_algorithm_t alg,
110     const uint8_t *peer_key,
111     size_t peer_key_length,
112     uint8_t *shared_secret,
113     size_t shared_secret_size,
114     size_t *shared_secret_length )
115 {
116     (void) attributes;
117     (void) key_buffer;
118     (void) key_buffer_size;
119     (void) alg;
120     (void) peer_key;
121     (void) peer_key_length;
122     (void) shared_secret;
123     (void) shared_secret_size;
124     (void) shared_secret_length;
125     return( PSA_ERROR_NOT_SUPPORTED );
126 }
127 
128 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
129