blob: 4c830c35e6fc4e4ad252985e75b52f0a5281b9d6 [file] [log] [blame]
Steven Cooreman2a1664c2020-07-20 15:33:08 +02001/*
2 * Test driver for signature functions
3 */
4/* Copyright (C) 2020, ARM Limited, All Rights Reserved
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 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(MBEDTLS_TEST_HOOKS)
29#include "psa/crypto.h"
30#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)
67 if ( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) && PSA_KEY_TYPE_IS_KEY_PAIR( attributes->core.type ) )
68 {
69 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type );
70 mbedtls_ecp_group_id grp_id =
71 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( attributes->core.bits ) );
72 const mbedtls_ecp_curve_info *curve_info =
73 mbedtls_ecp_curve_info_from_grp_id( grp_id );
74 mbedtls_ecp_keypair ecp;
75 mbedtls_test_rnd_pseudo_info rnd_info;
76 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
77
78 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
79 if( attributes->domain_parameters_size != 0 )
80 return( PSA_ERROR_NOT_SUPPORTED );
81 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
82 return( PSA_ERROR_NOT_SUPPORTED );
83 if( curve_info->bit_size != attributes->core.bits )
84 return( PSA_ERROR_INVALID_ARGUMENT );
85 mbedtls_ecp_keypair_init( &ecp );
86 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
87 &mbedtls_test_rnd_pseudo_rand,
88 &rnd_info );
89 if( ret != 0 )
90 {
91 mbedtls_ecp_keypair_free( &ecp );
92 return( mbedtls_to_psa_error( ret ) );
93 }
94
95 /* Make sure to use export representation */
96 size_t bytes = PSA_BITS_TO_BYTES( attributes->core.bits );
97 if( key_size < bytes )
98 {
99 mbedtls_ecp_keypair_free( &ecp );
100 return( PSA_ERROR_BUFFER_TOO_SMALL );
101 }
102 psa_status_t status = mbedtls_to_psa_error(
103 mbedtls_mpi_write_binary( &ecp.d, key, bytes ) );
104
105 if( status == PSA_SUCCESS )
106 {
107 *key_length = bytes;
108 }
109
110 mbedtls_ecp_keypair_free( &ecp );
111 return( status );
112 }
113 else
114#endif /* MBEDTLS_ECP_C */
115 return( PSA_ERROR_NOT_SUPPORTED );
116}
117
118psa_status_t test_opaque_generate_key(
119 const psa_key_attributes_t *attributes,
120 uint8_t *key, size_t key_size, size_t *key_length )
121{
122 (void) attributes;
123 (void) key;
124 (void) key_size;
125 (void) key_length;
126 return( PSA_ERROR_NOT_SUPPORTED );
127}
128
129#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && MBEDTLS_TEST_HOOKS */