blob: 7f14b20efc360e23a1d664479832183203b35a93 [file] [log] [blame]
Steven Cooreman2a1664c2020-07-20 15:33:08 +02001/*
2 * Test driver for signature functions
3 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02004/* Copyright The Mbed TLS Contributors
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005 * 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.
Steven Cooreman2a1664c2020-07-20 15:33:08 +020018 */
19
20#if !defined(MBEDTLS_CONFIG_FILE)
21#include "mbedtls/config.h"
22#else
23#include MBEDTLS_CONFIG_FILE
24#endif
25
Steven Cooremanf1720ea2020-07-24 18:41:58 +020026#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +020027#include "psa/crypto.h"
28#include "mbedtls/ecp.h"
29#include "mbedtls/error.h"
30
31#include "drivers/keygen.h"
32
33#include "test/random.h"
34
35#include <string.h>
36
37/* If non-null, on success, copy this to the output. */
38void *test_driver_keygen_forced_output = NULL;
39size_t test_driver_keygen_forced_output_length = 0;
40
41psa_status_t test_transparent_keygen_status = PSA_ERROR_NOT_SUPPORTED;
42unsigned long test_transparent_keygen_hit = 0;
43
44psa_status_t test_transparent_generate_key(
45 const psa_key_attributes_t *attributes,
46 uint8_t *key, size_t key_size, size_t *key_length )
47{
48 ++test_transparent_keygen_hit;
49
50 if( test_transparent_keygen_status != PSA_SUCCESS )
51 return( test_transparent_keygen_status );
52
53 if( test_driver_keygen_forced_output != NULL )
54 {
55 if( test_driver_keygen_forced_output_length > key_size )
56 return( PSA_ERROR_BUFFER_TOO_SMALL );
57 memcpy( key, test_driver_keygen_forced_output,
58 test_driver_keygen_forced_output_length );
59 *key_length = test_driver_keygen_forced_output_length;
60 return( PSA_SUCCESS );
61 }
62
63 /* Copied from psa_crypto.c */
64#if defined(MBEDTLS_ECP_C)
65 if ( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) && PSA_KEY_TYPE_IS_KEY_PAIR( attributes->core.type ) )
66 {
67 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type );
68 mbedtls_ecp_group_id grp_id =
69 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( attributes->core.bits ) );
70 const mbedtls_ecp_curve_info *curve_info =
71 mbedtls_ecp_curve_info_from_grp_id( grp_id );
72 mbedtls_ecp_keypair ecp;
73 mbedtls_test_rnd_pseudo_info rnd_info;
74 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
75
76 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
77 if( attributes->domain_parameters_size != 0 )
78 return( PSA_ERROR_NOT_SUPPORTED );
79 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
80 return( PSA_ERROR_NOT_SUPPORTED );
81 if( curve_info->bit_size != attributes->core.bits )
82 return( PSA_ERROR_INVALID_ARGUMENT );
83 mbedtls_ecp_keypair_init( &ecp );
84 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
85 &mbedtls_test_rnd_pseudo_rand,
86 &rnd_info );
87 if( ret != 0 )
88 {
89 mbedtls_ecp_keypair_free( &ecp );
90 return( mbedtls_to_psa_error( ret ) );
91 }
92
93 /* Make sure to use export representation */
94 size_t bytes = PSA_BITS_TO_BYTES( attributes->core.bits );
95 if( key_size < bytes )
96 {
97 mbedtls_ecp_keypair_free( &ecp );
98 return( PSA_ERROR_BUFFER_TOO_SMALL );
99 }
100 psa_status_t status = mbedtls_to_psa_error(
101 mbedtls_mpi_write_binary( &ecp.d, key, bytes ) );
102
103 if( status == PSA_SUCCESS )
104 {
105 *key_length = bytes;
106 }
107
108 mbedtls_ecp_keypair_free( &ecp );
109 return( status );
110 }
111 else
112#endif /* MBEDTLS_ECP_C */
113 return( PSA_ERROR_NOT_SUPPORTED );
114}
115
116psa_status_t test_opaque_generate_key(
117 const psa_key_attributes_t *attributes,
118 uint8_t *key, size_t key_size, size_t *key_length )
119{
120 (void) attributes;
121 (void) key;
122 (void) key_size;
123 (void) key_length;
124 return( PSA_ERROR_NOT_SUPPORTED );
125}
126
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200127#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */