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