blob: dd6022477142835362f3e476170b6dafaa66db1f [file] [log] [blame]
Aditya Deshpande045b3702023-02-20 17:08:30 +00001/*
2 * Driver entry points for p256-m
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * 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.
19 */
20
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000021#include "mbedtls/platform.h"
22#include "p256-m_driver_entrypoints.h"
23#include "p256-m/p256-m.h"
24#include "psa/crypto.h"
25#include "psa_crypto_driver_wrappers.h"
26
27#if defined(MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED)
28
Aditya Deshpande695e44b2023-01-23 14:59:29 +000029psa_status_t p256_to_psa_error( int ret )
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000030{
31 switch( ret )
32 {
33 case P256_SUCCESS:
34 return( PSA_SUCCESS );
35 case P256_INVALID_PUBKEY:
36 case P256_INVALID_PRIVKEY:
37 return( PSA_ERROR_INVALID_ARGUMENT );
38 case P256_INVALID_SIGNATURE:
39 return( PSA_ERROR_INVALID_SIGNATURE );
40 case P256_RANDOM_FAILED:
41 default:
42 return( PSA_ERROR_GENERIC_ERROR );
43 }
44}
45
Aditya Deshpande695e44b2023-01-23 14:59:29 +000046psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000047 const psa_key_attributes_t *attributes,
48 uint8_t *key_buffer,
49 size_t key_buffer_size,
50 size_t *key_buffer_length )
51{
52 /* We don't use this argument, but the specification mandates the signature
53 * of driver entry-points. (void) used to avoid compiler warning. */
54 (void) attributes;
55
56 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
57
58 /*
59 * p256-m generates a 32 byte private key, and expects to write to a buffer
60 * that is of that size. */
61 if( key_buffer_size != 32 )
62 return( status );
63
64 /*
65 * p256-m's keypair generation function outputs both public and private
66 * keys. Allocate a buffer to which the public key will be written. The
67 * private key will be written to key_buffer, which is passed to this
68 * function as an argument. */
69 uint8_t *public_key_buffer = NULL;
70 public_key_buffer = mbedtls_calloc( 1, 64);
71 if( public_key_buffer == NULL)
72 return( PSA_ERROR_INSUFFICIENT_MEMORY );
73
Aditya Deshpande695e44b2023-01-23 14:59:29 +000074 status = p256_to_psa_error(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000075 p256_gen_keypair(key_buffer, public_key_buffer) );
76 if( status == PSA_SUCCESS )
77 *key_buffer_length = 32;
78
79 /*
80 * The storage format for a SECP256R1 keypair is just the private key, so
81 * the public key does not need to be passed back to the caller. Therefore
82 * the buffer containing it can be freed. */
83 free( public_key_buffer );
84
85 return status;
86}
87
Aditya Deshpande695e44b2023-01-23 14:59:29 +000088psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000089 const psa_key_attributes_t *attributes,
90 const uint8_t *key_buffer,
91 size_t key_buffer_size,
92 psa_algorithm_t alg,
93 const uint8_t *peer_key,
94 size_t peer_key_length,
95 uint8_t *shared_secret,
96 size_t shared_secret_size,
97 size_t *shared_secret_length )
98{
99 /* We don't use these arguments, but the specification mandates the
100 * sginature of driver entry-points. (void) used to avoid compiler
101 * warning. */
102 (void) attributes;
103 (void) alg;
104
105 /*
106 * Check that private key = 32 bytes, peer public key = 65 bytes,
107 * and that the shared secret buffer is big enough. */
108 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
109 if( key_buffer_size != 32 || shared_secret_size < 32 ||
110 peer_key_length != 65 )
111 return ( status );
112
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000113 status = p256_to_psa_error(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000114 p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key+1) );
115 if( status == PSA_SUCCESS )
116 *shared_secret_length = 32;
117
118 return status;
119}
120
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000121psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000122 const psa_key_attributes_t *attributes,
123 const uint8_t *key_buffer,
124 size_t key_buffer_size,
125 psa_algorithm_t alg,
126 const uint8_t *hash,
127 size_t hash_length,
128 uint8_t *signature,
129 size_t signature_size,
130 size_t *signature_length )
131{
132 /* We don't use these arguments, but the specification mandates the
133 * sginature of driver entry-points. (void) used to avoid compiler
134 * warning. */
135 (void) attributes;
136 (void) alg;
137
138 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
139 if( key_buffer_size != 32 || signature_size != 64)
140 return( status );
141
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000142 status = p256_to_psa_error(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000143 p256_ecdsa_sign(signature, key_buffer, hash, hash_length) );
144 if( status == PSA_SUCCESS )
145 *signature_length = 64;
146
147 return status;
148}
149
150/* This function expects the key buffer to contain a 65 byte public key,
151 * as exported by psa_export_public_key() */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000152static psa_status_t p256_verify_hash_with_public_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000153 const uint8_t *key_buffer,
154 size_t key_buffer_size,
155 const uint8_t *hash,
156 size_t hash_length,
157 const uint8_t *signature,
158 size_t signature_length )
159{
160 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
161 if( key_buffer_size != 65 || signature_length != 64 || *key_buffer != 0x04 )
162 return status;
163
164 const uint8_t *public_key_buffer = key_buffer + 1;
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000165 status = p256_to_psa_error(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000166 p256_ecdsa_verify( signature, public_key_buffer, hash, hash_length) );
167
168 return status;
169}
170
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000171psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000172 const psa_key_attributes_t *attributes,
173 const uint8_t *key_buffer,
174 size_t key_buffer_size,
175 psa_algorithm_t alg,
176 const uint8_t *hash,
177 size_t hash_length,
178 const uint8_t *signature,
179 size_t signature_length )
180{
181 /* We don't use this argument, but the specification mandates the signature
182 * of driver entry-points. (void) used to avoid compiler warning. */
183 (void) alg;
184
185 psa_status_t status;
186 uint8_t *public_key_buffer = NULL;
187 size_t public_key_buffer_size = 65;
188 public_key_buffer = mbedtls_calloc( 1, public_key_buffer_size);
189 if( public_key_buffer == NULL)
190 return( PSA_ERROR_INSUFFICIENT_MEMORY );
191 size_t *public_key_length = NULL;
192 public_key_length = mbedtls_calloc( 1, sizeof(size_t) );
193 if( public_key_length == NULL)
194 return( PSA_ERROR_INSUFFICIENT_MEMORY );
195 *public_key_length = 65;
196
197 /* The contents of key_buffer may either be the 32 byte private key
198 * (keypair representation), or the 65 byte public key. To ensure the
199 * latter is obtained, the public key is exported. */
200 status = psa_driver_wrapper_export_public_key(
201 attributes,
202 key_buffer,
203 key_buffer_size,
204 public_key_buffer,
205 public_key_buffer_size,
206 public_key_length );
207 if( status != PSA_SUCCESS )
208 goto exit;
209
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000210 status = p256_verify_hash_with_public_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000211 public_key_buffer,
212 public_key_buffer_size,
213 hash,
214 hash_length,
215 signature,
216 signature_length );
217
218exit:
219 free( public_key_buffer );
220 free( public_key_length );
221 return ( status );
222}
223
224#endif /* MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED */