blob: f8eeb6767d2420dfbf8beaf4823fc36dac684f0f [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"
Aditya Deshpande7b9934d2023-04-18 17:00:17 +010025#include <stddef.h>
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020026#include <string.h>
Xiaokang Qian077ffc02023-08-08 03:41:07 +000027#include "psa_crypto_driver_wrappers.h"
Xiaokang Qian4792b202023-09-07 07:19:06 +000028#include <stddef.h>
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000029
Gilles Peskineefaee9a2023-09-20 20:49:47 +020030#if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000031
Aditya Deshpande641cb892023-04-19 03:31:10 +010032/* INFORMATION ON PSA KEY EXPORT FORMATS:
33 *
34 * PSA exports SECP256R1 keys in two formats:
35 * 1. Keypair format: 32 byte string which is just the private key (public key
36 * can be calculated from the private key)
37 * 2. Public Key format: A leading byte 0x04 (indicating uncompressed format),
38 * followed by the 64 byte public key. This results in a
39 * total of 65 bytes.
40 *
41 * p256-m's internal format for private keys matches PSA. Its format for public
Manuel Pégourié-Gonnard5ca69342023-09-20 09:28:02 +020042 * keys is only 64 bytes: the same as PSA but without the leading byte (0x04).
Aditya Deshpande641cb892023-04-19 03:31:10 +010043 * Hence, when passing public keys from PSA to p256-m, the leading byte is
44 * removed.
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +020045 *
46 * Shared secret and signature have the same format between PSA and p256-m.
Aditya Deshpande641cb892023-04-19 03:31:10 +010047 */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +020048#define PSA_PUBKEY_SIZE 65
49#define PSA_PUBKEY_HEADER_BYTE 0x04
50#define P256_PUBKEY_SIZE 64
51#define PRIVKEY_SIZE 32
52#define SHARED_SECRET_SIZE 32
53#define SIGNATURE_SIZE 64
54
55#define CURVE_BITS 256
Aditya Deshpande641cb892023-04-19 03:31:10 +010056
57/* Convert between p256-m and PSA error codes */
58static psa_status_t p256_to_psa_error(int ret)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000059{
Aditya Deshpandeac363d82023-03-21 18:56:31 +000060 switch (ret) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000061 case P256_SUCCESS:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000062 return PSA_SUCCESS;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000063 case P256_INVALID_PUBKEY:
64 case P256_INVALID_PRIVKEY:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000065 return PSA_ERROR_INVALID_ARGUMENT;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000066 case P256_INVALID_SIGNATURE:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000067 return PSA_ERROR_INVALID_SIGNATURE;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000068 case P256_RANDOM_FAILED:
69 default:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000070 return PSA_ERROR_GENERIC_ERROR;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000071 }
72}
73
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020074psa_status_t p256_transparent_import_key(const psa_key_attributes_t *attributes,
75 const uint8_t *data,
76 size_t data_length,
77 uint8_t *key_buffer,
78 size_t key_buffer_size,
79 size_t *key_buffer_length,
80 size_t *bits)
81{
82 /* Check the key size */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +020083 if (*bits != 0 && *bits != CURVE_BITS) {
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020084 return PSA_ERROR_NOT_SUPPORTED;
85 }
86
87 /* Validate the key (and its type and size) */
88 psa_key_type_t type = psa_get_key_type(attributes);
89 if (type == PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)) {
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +020090 if (data_length != PSA_PUBKEY_SIZE) {
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020091 return *bits == 0 ? PSA_ERROR_NOT_SUPPORTED : PSA_ERROR_INVALID_ARGUMENT;
92 }
Manuel Pégourié-Gonnard5ca69342023-09-20 09:28:02 +020093 /* See INFORMATION ON PSA KEY EXPORT FORMATS near top of file */
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020094 if (p256_validate_pubkey(data + 1) != P256_SUCCESS) {
95 return PSA_ERROR_INVALID_ARGUMENT;
96 }
97 } else if (type == PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)) {
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +020098 if (data_length != PRIVKEY_SIZE) {
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020099 return *bits == 0 ? PSA_ERROR_NOT_SUPPORTED : PSA_ERROR_INVALID_ARGUMENT;
100 }
101 if (p256_validate_privkey(data) != P256_SUCCESS) {
102 return PSA_ERROR_INVALID_ARGUMENT;
103 }
104 } else {
105 return PSA_ERROR_NOT_SUPPORTED;
106 }
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200107 *bits = CURVE_BITS;
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +0200108
109 /* We only support the export format for input, so just copy. */
110 if (key_buffer_size < data_length) {
111 return PSA_ERROR_BUFFER_TOO_SMALL;
112 }
113 memcpy(key_buffer, data, data_length);
114 *key_buffer_length = data_length;
115
116 return PSA_SUCCESS;
117}
118
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200119psa_status_t p256_transparent_export_public_key(const psa_key_attributes_t *attributes,
120 const uint8_t *key_buffer,
121 size_t key_buffer_size,
122 uint8_t *data,
123 size_t data_size,
124 size_t *data_length)
125{
126 /* Is this the right curve? */
127 size_t bits = psa_get_key_bits(attributes);
128 psa_key_type_t type = psa_get_key_type(attributes);
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200129 if (bits != CURVE_BITS || type != PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)) {
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200130 return PSA_ERROR_NOT_SUPPORTED;
131 }
132
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200133 /* Validate sizes, as p256-m expects fixed-size buffers */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200134 if (key_buffer_size != PRIVKEY_SIZE) {
Manuel Pégourié-Gonnardfbea9d22023-09-20 09:22:29 +0200135 return PSA_ERROR_INVALID_ARGUMENT;
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200136 }
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200137 if (data_size < PSA_PUBKEY_SIZE) {
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200138 return PSA_ERROR_BUFFER_TOO_SMALL;
139 }
140
Manuel Pégourié-Gonnard5ca69342023-09-20 09:28:02 +0200141 /* See INFORMATION ON PSA KEY EXPORT FORMATS near top of file */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200142 data[0] = PSA_PUBKEY_HEADER_BYTE;
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200143 int ret = p256_public_from_private(data + 1, key_buffer);
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200144 if (ret == P256_SUCCESS) {
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200145 *data_length = PSA_PUBKEY_SIZE;
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200146 }
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200147
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200148 return p256_to_psa_error(ret);
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200149}
150
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000151psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000152 const psa_key_attributes_t *attributes,
153 uint8_t *key_buffer,
154 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000155 size_t *key_buffer_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000156{
157 /* We don't use this argument, but the specification mandates the signature
158 * of driver entry-points. (void) used to avoid compiler warning. */
159 (void) attributes;
160
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200161 /* Validate sizes, as p256-m expects fixed-size buffers */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200162 if (key_buffer_size != PRIVKEY_SIZE) {
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200163 return PSA_ERROR_BUFFER_TOO_SMALL;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000164 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000165
166 /*
167 * p256-m's keypair generation function outputs both public and private
168 * keys. Allocate a buffer to which the public key will be written. The
169 * private key will be written to key_buffer, which is passed to this
170 * function as an argument. */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200171 uint8_t public_key_buffer[P256_PUBKEY_SIZE];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000172
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200173 int ret = p256_gen_keypair(key_buffer, public_key_buffer);
174 if (ret == P256_SUCCESS) {
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200175 *key_buffer_length = PRIVKEY_SIZE;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000176 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000177
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200178 return p256_to_psa_error(ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000179}
180
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000181psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000182 const psa_key_attributes_t *attributes,
183 const uint8_t *key_buffer,
184 size_t key_buffer_size,
185 psa_algorithm_t alg,
186 const uint8_t *peer_key,
187 size_t peer_key_length,
188 uint8_t *shared_secret,
189 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000190 size_t *shared_secret_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000191{
192 /* We don't use these arguments, but the specification mandates the
193 * sginature of driver entry-points. (void) used to avoid compiler
194 * warning. */
195 (void) attributes;
196 (void) alg;
197
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200198 /* Validate sizes, as p256-m expects fixed-size buffers */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200199 if (key_buffer_size != PRIVKEY_SIZE || peer_key_length != PSA_PUBKEY_SIZE) {
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200200 return PSA_ERROR_INVALID_ARGUMENT;
201 }
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200202 if (shared_secret_size < SHARED_SECRET_SIZE) {
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200203 return PSA_ERROR_BUFFER_TOO_SMALL;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000204 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000205
Manuel Pégourié-Gonnard5ca69342023-09-20 09:28:02 +0200206 /* See INFORMATION ON PSA KEY EXPORT FORMATS near top of file */
Manuel Pégourié-Gonnard3ec976c2023-09-20 16:12:46 +0200207 const uint8_t *peer_key_p256m = peer_key + 1;
Manuel Pégourié-Gonnard5ca69342023-09-20 09:28:02 +0200208 int ret = p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key_p256m);
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200209 if (ret == P256_SUCCESS) {
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200210 *shared_secret_length = SHARED_SECRET_SIZE;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000211 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000212
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200213 return p256_to_psa_error(ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000214}
215
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000216psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000217 const psa_key_attributes_t *attributes,
218 const uint8_t *key_buffer,
219 size_t key_buffer_size,
220 psa_algorithm_t alg,
221 const uint8_t *hash,
222 size_t hash_length,
223 uint8_t *signature,
224 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000225 size_t *signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000226{
227 /* We don't use these arguments, but the specification mandates the
228 * sginature of driver entry-points. (void) used to avoid compiler
229 * warning. */
230 (void) attributes;
231 (void) alg;
232
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200233 /* Validate sizes, as p256-m expects fixed-size buffers */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200234 if (key_buffer_size != PRIVKEY_SIZE) {
Manuel Pégourié-Gonnardfbea9d22023-09-20 09:22:29 +0200235 return PSA_ERROR_INVALID_ARGUMENT;
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200236 }
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200237 if (signature_size < SIGNATURE_SIZE) {
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200238 return PSA_ERROR_BUFFER_TOO_SMALL;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000239 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000240
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200241 int ret = p256_ecdsa_sign(signature, key_buffer, hash, hash_length);
242 if (ret == P256_SUCCESS) {
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200243 *signature_length = SIGNATURE_SIZE;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000244 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000245
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200246 return p256_to_psa_error(ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000247}
248
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200249/* This function expects the key buffer to contain a PSA public key,
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000250 * as exported by psa_export_public_key() */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000251static psa_status_t p256_verify_hash_with_public_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000252 const uint8_t *key_buffer,
253 size_t key_buffer_size,
254 const uint8_t *hash,
255 size_t hash_length,
256 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000257 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000258{
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200259 /* Validate sizes, as p256-m expects fixed-size buffers */
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200260 if (key_buffer_size != PSA_PUBKEY_SIZE || *key_buffer != PSA_PUBKEY_HEADER_BYTE) {
Manuel Pégourié-Gonnardfbea9d22023-09-20 09:22:29 +0200261 return PSA_ERROR_INVALID_ARGUMENT;
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200262 }
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200263 if (signature_length != SIGNATURE_SIZE) {
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200264 return PSA_ERROR_INVALID_SIGNATURE;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000265 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000266
Manuel Pégourié-Gonnard5ca69342023-09-20 09:28:02 +0200267 /* See INFORMATION ON PSA KEY EXPORT FORMATS near top of file */
268 const uint8_t *public_key_p256m = key_buffer + 1;
269 int ret = p256_ecdsa_verify(signature, public_key_p256m, hash, hash_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000270
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200271 return p256_to_psa_error(ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000272}
273
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000274psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000275 const psa_key_attributes_t *attributes,
276 const uint8_t *key_buffer,
277 size_t key_buffer_size,
278 psa_algorithm_t alg,
279 const uint8_t *hash,
280 size_t hash_length,
281 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000282 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000283{
284 /* We don't use this argument, but the specification mandates the signature
285 * of driver entry-points. (void) used to avoid compiler warning. */
286 (void) alg;
287
288 psa_status_t status;
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200289 uint8_t public_key_buffer[PSA_PUBKEY_SIZE];
290 size_t public_key_buffer_size = PSA_PUBKEY_SIZE;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100291
Manuel Pégourié-Gonnardba63e0c2023-08-09 11:53:09 +0200292 size_t public_key_length = PSA_PUBKEY_SIZE;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100293 /* As p256-m doesn't require dynamic allocation, we want to avoid it in
294 * the entrypoint functions as well. psa_driver_wrapper_export_public_key()
295 * requires size_t*, so we use a pointer to a stack variable. */
296 size_t *public_key_length_ptr = &public_key_length;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000297
Aditya Deshpande641cb892023-04-19 03:31:10 +0100298 /* The contents of key_buffer may either be the 32 byte private key
299 * (keypair format), or 0x04 followed by the 64 byte public key (public
300 * key format). To ensure the key is in the latter format, the public key
301 * is exported. */
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000302 status = psa_driver_wrapper_export_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000303 attributes,
304 key_buffer,
305 key_buffer_size,
306 public_key_buffer,
307 public_key_buffer_size,
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100308 public_key_length_ptr);
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000309 if (status != PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000310 goto exit;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000311 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000312
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000313 status = p256_verify_hash_with_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000314 public_key_buffer,
315 public_key_buffer_size,
316 hash,
317 hash_length,
318 signature,
319 signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000320
321exit:
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000322 return status;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000323}
324
Gilles Peskineefaee9a2023-09-20 20:49:47 +0200325#endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */