blob: a21ec27ce3abc3f351aaa20993e58ea468f4be88 [file] [log] [blame]
Steven Cooreman2a1664c2020-07-20 15:33:08 +02001/*
Steven Cooreman56250fd2020-09-04 13:07:15 +02002 * Test driver for generating keys.
3 * Currently only supports generating ECC keys.
Steven Cooreman2a1664c2020-07-20 15:33:08 +02004 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02005/* Copyright The Mbed TLS Contributors
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Steven Cooreman2a1664c2020-07-20 15:33:08 +020019 */
20
21#if !defined(MBEDTLS_CONFIG_FILE)
22#include "mbedtls/config.h"
23#else
24#include MBEDTLS_CONFIG_FILE
25#endif
26
Steven Cooremanf1720ea2020-07-24 18:41:58 +020027#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +020028#include "psa/crypto.h"
Steven Cooreman15f58d22020-09-04 13:05:23 +020029#include "psa_crypto_core.h"
Steven Cooreman2a1664c2020-07-20 15:33:08 +020030#include "mbedtls/ecp.h"
31#include "mbedtls/error.h"
32
33#include "drivers/keygen.h"
34
35#include "test/random.h"
36
37#include <string.h>
38
39/* If non-null, on success, copy this to the output. */
40void *test_driver_keygen_forced_output = NULL;
41size_t test_driver_keygen_forced_output_length = 0;
42
43psa_status_t test_transparent_keygen_status = PSA_ERROR_NOT_SUPPORTED;
44unsigned long test_transparent_keygen_hit = 0;
45
46psa_status_t test_transparent_generate_key(
47 const psa_key_attributes_t *attributes,
48 uint8_t *key, size_t key_size, size_t *key_length )
49{
50 ++test_transparent_keygen_hit;
51
52 if( test_transparent_keygen_status != PSA_SUCCESS )
53 return( test_transparent_keygen_status );
54
55 if( test_driver_keygen_forced_output != NULL )
56 {
57 if( test_driver_keygen_forced_output_length > key_size )
58 return( PSA_ERROR_BUFFER_TOO_SMALL );
59 memcpy( key, test_driver_keygen_forced_output,
60 test_driver_keygen_forced_output_length );
61 *key_length = test_driver_keygen_forced_output_length;
62 return( PSA_SUCCESS );
63 }
64
65 /* Copied from psa_crypto.c */
66#if defined(MBEDTLS_ECP_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020067 if ( PSA_KEY_TYPE_IS_ECC( psa_get_key_type( attributes ) )
68 && PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020069 {
Steven Cooreman56250fd2020-09-04 13:07:15 +020070 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( psa_get_key_type( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020071 mbedtls_ecp_group_id grp_id =
Steven Cooreman56250fd2020-09-04 13:07:15 +020072 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020073 const mbedtls_ecp_curve_info *curve_info =
74 mbedtls_ecp_curve_info_from_grp_id( grp_id );
75 mbedtls_ecp_keypair ecp;
76 mbedtls_test_rnd_pseudo_info rnd_info;
77 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
78
79 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
80 if( attributes->domain_parameters_size != 0 )
81 return( PSA_ERROR_NOT_SUPPORTED );
82 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
83 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman56250fd2020-09-04 13:07:15 +020084 if( curve_info->bit_size != psa_get_key_bits( attributes ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020085 return( PSA_ERROR_INVALID_ARGUMENT );
86 mbedtls_ecp_keypair_init( &ecp );
87 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
88 &mbedtls_test_rnd_pseudo_rand,
89 &rnd_info );
90 if( ret != 0 )
91 {
92 mbedtls_ecp_keypair_free( &ecp );
93 return( mbedtls_to_psa_error( ret ) );
94 }
95
96 /* Make sure to use export representation */
Steven Cooreman56250fd2020-09-04 13:07:15 +020097 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020098 if( key_size < bytes )
99 {
100 mbedtls_ecp_keypair_free( &ecp );
101 return( PSA_ERROR_BUFFER_TOO_SMALL );
102 }
103 psa_status_t status = mbedtls_to_psa_error(
104 mbedtls_mpi_write_binary( &ecp.d, key, bytes ) );
105
106 if( status == PSA_SUCCESS )
107 {
108 *key_length = bytes;
109 }
110
111 mbedtls_ecp_keypair_free( &ecp );
112 return( status );
113 }
114 else
115#endif /* MBEDTLS_ECP_C */
116 return( PSA_ERROR_NOT_SUPPORTED );
117}
118
119psa_status_t test_opaque_generate_key(
120 const psa_key_attributes_t *attributes,
121 uint8_t *key, size_t key_size, size_t *key_length )
122{
123 (void) attributes;
124 (void) key;
125 (void) key_size;
126 (void) key_length;
127 return( PSA_ERROR_NOT_SUPPORTED );
128}
129
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200130#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */