blob: 67bb0f11e4853f5174297da8af2761c25d692ed7 [file] [log] [blame]
Ronald Cron00b7bfc2020-11-25 15:25:26 +01001/*
2 * PSA ECP layer on top of Mbed TLS crypto
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
21#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
26#include "psa_crypto_core.h"
27#include "psa_crypto_ecp.h"
Ronald Crone5ca3d82020-11-26 16:36:16 +010028#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010029
30#include <stdlib.h>
31#include <string.h>
32#include "mbedtls/platform.h"
33#if !defined(MBEDTLS_PLATFORM_C)
34#define mbedtls_calloc calloc
35#define mbedtls_free free
36#endif
37
38#include <mbedtls/ecp.h>
39#include <mbedtls/error.h>
40
Ronald Cronf1057d32020-11-26 19:19:10 +010041#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
42 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
43 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ) )
44#define BUILTIN_KEY_TYPE_ECC_KEY_PAIR 1
45#endif
46
47#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
48 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
49 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) ) )
50#define BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY 1
51#endif
52
53#if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
54 defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010055 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
56 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
57 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
58psa_status_t mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010059 psa_key_type_t type, size_t curve_bits,
60 const uint8_t *data, size_t data_length,
Ronald Cron00b7bfc2020-11-25 15:25:26 +010061 mbedtls_ecp_keypair **p_ecp )
62{
63 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
64 psa_status_t status;
65 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010066 size_t curve_bytes = data_length;
67 int explicit_bits = ( curve_bits != 0 );
Ronald Cron00b7bfc2020-11-25 15:25:26 +010068
69 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
70 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
71 {
72 /* A Weierstrass public key is represented as:
73 * - The byte 0x04;
74 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
75 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
76 * So its data length is 2m+1 where m is the curve size in bits.
77 */
78 if( ( data_length & 1 ) == 0 )
79 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010080 curve_bytes = data_length / 2;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010081
82 /* Montgomery public keys are represented in compressed format, meaning
Gilles Peskined88ccae2021-02-08 18:39:18 +010083 * their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +010084
85 /* Private keys are represented in uncompressed private random integer
Gilles Peskined88ccae2021-02-08 18:39:18 +010086 * format, meaning their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +010087 }
88
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010089 if( explicit_bits )
90 {
91 /* With an explicit bit-size, the data must have the matching length. */
92 if( curve_bytes != PSA_BITS_TO_BYTES( curve_bits ) )
93 return( PSA_ERROR_INVALID_ARGUMENT );
94 }
95 else
96 {
97 /* We need to infer the bit-size from the data. Since the only
98 * information we have is the length in bytes, the value of curve_bits
99 * at this stage is rounded up to the nearest multiple of 8. */
100 curve_bits = PSA_BYTES_TO_BITS( curve_bytes );
101 }
102
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100103 /* Allocate and initialize a key representation. */
104 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
105 if( ecp == NULL )
106 return( PSA_ERROR_INSUFFICIENT_MEMORY );
107 mbedtls_ecp_keypair_init( ecp );
108
109 /* Load the group. */
110 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100111 curve_bits, !explicit_bits );
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100112 if( grp_id == MBEDTLS_ECP_DP_NONE )
113 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100114 /* We can't distinguish between a nonsensical family/size combination
115 * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a
116 * well-regarded curve that Mbed TLS just doesn't know about (which
117 * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how
118 * curves that Mbed TLS knows about but for which support is disabled
119 * at build time, return NOT_SUPPORTED. */
120 status = PSA_ERROR_NOT_SUPPORTED;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100121 goto exit;
122 }
123
124 status = mbedtls_to_psa_error(
125 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
126 if( status != PSA_SUCCESS )
127 goto exit;
128
129 /* Load the key material. */
130 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
131 {
132 /* Load the public value. */
133 status = mbedtls_to_psa_error(
134 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
135 data,
136 data_length ) );
137 if( status != PSA_SUCCESS )
138 goto exit;
139
140 /* Check that the point is on the curve. */
141 status = mbedtls_to_psa_error(
142 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
143 if( status != PSA_SUCCESS )
144 goto exit;
145 }
146 else
147 {
148 /* Load and validate the secret value. */
149 status = mbedtls_to_psa_error(
150 mbedtls_ecp_read_key( ecp->grp.id,
151 ecp,
152 data,
153 data_length ) );
154 if( status != PSA_SUCCESS )
155 goto exit;
156 }
157
158 *p_ecp = ecp;
159exit:
160 if( status != PSA_SUCCESS )
161 {
162 mbedtls_ecp_keypair_free( ecp );
163 mbedtls_free( ecp );
164 }
165
166 return( status );
167}
Ronald Cronf1057d32020-11-26 19:19:10 +0100168#endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
169 * defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100170 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
171 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
172 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
173
Ronald Cronf1057d32020-11-26 19:19:10 +0100174#if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
175 defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crond6ec3032020-11-27 18:54:57 +0100176
Ronald Cron784fb322020-11-30 13:55:05 +0100177static psa_status_t ecp_import_key(
Ronald Crond6ec3032020-11-27 18:54:57 +0100178 const psa_key_attributes_t *attributes,
179 const uint8_t *data, size_t data_length,
180 uint8_t *key_buffer, size_t key_buffer_size,
181 size_t *key_buffer_length, size_t *bits )
182{
183 psa_status_t status;
184 mbedtls_ecp_keypair *ecp = NULL;
185
186 /* Parse input */
187 status = mbedtls_psa_ecp_load_representation( attributes->core.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100188 attributes->core.bits,
Ronald Crond6ec3032020-11-27 18:54:57 +0100189 data,
190 data_length,
191 &ecp );
192 if( status != PSA_SUCCESS )
193 goto exit;
194
195 if( PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ==
196 PSA_ECC_FAMILY_MONTGOMERY )
197 *bits = ecp->grp.nbits + 1;
198 else
199 *bits = ecp->grp.nbits;
200
201 /* Re-export the data to PSA export format. There is currently no support
202 * for other input formats then the export format, so this is a 1-1
203 * copy operation. */
204 status = mbedtls_psa_ecp_export_key( attributes->core.type,
205 ecp,
206 key_buffer,
207 key_buffer_size,
208 key_buffer_length );
209exit:
210 /* Always free the PK object (will also free contained ECP context) */
211 mbedtls_ecp_keypair_free( ecp );
212 mbedtls_free( ecp );
213
214 return( status );
215}
216
Ronald Crone5ca3d82020-11-26 16:36:16 +0100217psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type,
218 mbedtls_ecp_keypair *ecp,
219 uint8_t *data,
220 size_t data_size,
221 size_t *data_length )
222{
223 psa_status_t status;
224
225 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
226 {
227 /* Check whether the public part is loaded */
228 if( mbedtls_ecp_is_zero( &ecp->Q ) )
229 {
230 /* Calculate the public key */
231 status = mbedtls_to_psa_error(
232 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
233 mbedtls_psa_get_random,
234 MBEDTLS_PSA_RANDOM_STATE ) );
235 if( status != PSA_SUCCESS )
236 return( status );
237 }
238
239 status = mbedtls_to_psa_error(
240 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
241 MBEDTLS_ECP_PF_UNCOMPRESSED,
242 data_length,
243 data,
244 data_size ) );
245 if( status != PSA_SUCCESS )
246 memset( data, 0, data_size );
247
248 return( status );
249 }
250 else
251 {
252 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
253 return( PSA_ERROR_BUFFER_TOO_SMALL );
254
255 status = mbedtls_to_psa_error(
256 mbedtls_ecp_write_key( ecp,
257 data,
258 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
259 if( status == PSA_SUCCESS )
260 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
261 else
262 memset( data, 0, data_size );
263
264 return( status );
265 }
266}
267
Ronald Cronf1057d32020-11-26 19:19:10 +0100268static psa_status_t ecp_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100269 const psa_key_attributes_t *attributes,
270 const uint8_t *key_buffer, size_t key_buffer_size,
271 uint8_t *data, size_t data_size, size_t *data_length )
272{
273 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
274 mbedtls_ecp_keypair *ecp = NULL;
275
276 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100277 attributes->core.type, attributes->core.bits,
278 key_buffer, key_buffer_size, &ecp );
Ronald Crone5ca3d82020-11-26 16:36:16 +0100279 if( status != PSA_SUCCESS )
280 return( status );
281
282 status = mbedtls_psa_ecp_export_key(
283 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
284 PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ),
285 ecp, data, data_size, data_length );
286
287 mbedtls_ecp_keypair_free( ecp );
288 mbedtls_free( ecp );
289
290 return( status );
291}
Ronald Cronf1057d32020-11-26 19:19:10 +0100292#endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
293 * defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
294
Ronald Cron7023db52020-11-20 18:17:42 +0100295#if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100296static psa_status_t ecp_generate_key(
Ronald Cron7023db52020-11-20 18:17:42 +0100297 const psa_key_attributes_t *attributes,
298 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
299{
300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
302
303 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
304 attributes->core.type );
305 mbedtls_ecp_group_id grp_id =
306 mbedtls_ecc_group_of_psa( curve, attributes->core.bits, 0 );
307
308 const mbedtls_ecp_curve_info *curve_info =
309 mbedtls_ecp_curve_info_from_grp_id( grp_id );
310 mbedtls_ecp_keypair ecp;
311
312 if( attributes->domain_parameters_size != 0 )
313 return( PSA_ERROR_NOT_SUPPORTED );
314
315 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
316 return( PSA_ERROR_NOT_SUPPORTED );
317
318 mbedtls_ecp_keypair_init( &ecp );
319 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
320 mbedtls_psa_get_random,
321 MBEDTLS_PSA_RANDOM_STATE );
322 if( ret != 0 )
323 {
324 mbedtls_ecp_keypair_free( &ecp );
325 return( mbedtls_to_psa_error( ret ) );
326 }
327
328 status = mbedtls_to_psa_error(
329 mbedtls_ecp_write_key( &ecp, key_buffer, key_buffer_size ) );
330
331 mbedtls_ecp_keypair_free( &ecp );
332
333 if( status == PSA_SUCCESS )
334 *key_buffer_length = key_buffer_size;
335
336 return( status );
337}
338#endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
339
Ronald Cronf1057d32020-11-26 19:19:10 +0100340#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
341 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
342
Ronald Cron784fb322020-11-30 13:55:05 +0100343psa_status_t mbedtls_psa_ecp_import_key(
344 const psa_key_attributes_t *attributes,
345 const uint8_t *data, size_t data_length,
346 uint8_t *key_buffer, size_t key_buffer_size,
347 size_t *key_buffer_length, size_t *bits )
348{
349 return( ecp_import_key( attributes, data, data_length,
350 key_buffer, key_buffer_size,
351 key_buffer_length, bits ) );
352}
353
Ronald Cronf1057d32020-11-26 19:19:10 +0100354psa_status_t mbedtls_psa_ecp_export_public_key(
355 const psa_key_attributes_t *attributes,
356 const uint8_t *key_buffer, size_t key_buffer_size,
357 uint8_t *data, size_t data_size, size_t *data_length )
358{
359 return( ecp_export_public_key( attributes, key_buffer, key_buffer_size,
360 data, data_size, data_length ) );
361}
362
Ronald Crone5ca3d82020-11-26 16:36:16 +0100363#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
364 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
365
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100366#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
367psa_status_t mbedtls_psa_ecp_generate_key(
368 const psa_key_attributes_t *attributes,
369 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
370{
371 return( ecp_generate_key( attributes, key_buffer, key_buffer_size,
372 key_buffer_length ) );
373}
374#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
375
Ronald Cronf1057d32020-11-26 19:19:10 +0100376/*
377 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
378 */
379
380#if defined(PSA_CRYPTO_DRIVER_TEST)
381
382#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
383 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron784fb322020-11-30 13:55:05 +0100384
385psa_status_t mbedtls_transparent_test_driver_ecp_import_key(
386 const psa_key_attributes_t *attributes,
387 const uint8_t *data, size_t data_length,
388 uint8_t *key_buffer, size_t key_buffer_size,
389 size_t *key_buffer_length, size_t *bits )
390{
391 return( ecp_import_key( attributes, data, data_length,
392 key_buffer, key_buffer_size,
393 key_buffer_length, bits ) );
394}
395
Ronald Cronf1057d32020-11-26 19:19:10 +0100396psa_status_t mbedtls_transparent_test_driver_ecp_export_public_key(
397 const psa_key_attributes_t *attributes,
398 const uint8_t *key_buffer, size_t key_buffer_size,
399 uint8_t *data, size_t data_size, size_t *data_length )
400{
401 return( ecp_export_public_key( attributes, key_buffer, key_buffer_size,
402 data, data_size, data_length ) );
403}
Ronald Cron784fb322020-11-30 13:55:05 +0100404
Ronald Cronf1057d32020-11-26 19:19:10 +0100405#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
406 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
407
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100408#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) \
409 && defined(MBEDTLS_GENPRIME)
410psa_status_t mbedtls_transparent_test_driver_ecp_generate_key(
411 const psa_key_attributes_t *attributes,
412 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
413{
414 return( ecp_generate_key( attributes, key_buffer, key_buffer_size,
415 key_buffer_length ) );
416}
417#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
418 defined(MBEDTLS_GENPRIME) */
419
Ronald Cronf1057d32020-11-26 19:19:10 +0100420#endif /* PSA_CRYPTO_DRIVER_TEST */
421
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100422#endif /* MBEDTLS_PSA_CRYPTO_C */