blob: 8c8f85cb531be1c503e01cb7d253f3ed7dc06d12 [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"
Aditya Deshpande7b9934d2023-04-18 17:00:17 +010026#include <stddef.h>
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020027#include <string.h>
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000028
29#if defined(MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED)
30
Aditya Deshpande641cb892023-04-19 03:31:10 +010031/* INFORMATION ON PSA KEY EXPORT FORMATS:
32 *
33 * PSA exports SECP256R1 keys in two formats:
34 * 1. Keypair format: 32 byte string which is just the private key (public key
35 * can be calculated from the private key)
36 * 2. Public Key format: A leading byte 0x04 (indicating uncompressed format),
37 * followed by the 64 byte public key. This results in a
38 * total of 65 bytes.
39 *
40 * p256-m's internal format for private keys matches PSA. Its format for public
41 * keys is only 64 bytes; the same as PSA but without the leading byte (0x04).
42 * Hence, when passing public keys from PSA to p256-m, the leading byte is
43 * removed.
44 */
45
46/* Convert between p256-m and PSA error codes */
47static psa_status_t p256_to_psa_error(int ret)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000048{
Aditya Deshpandeac363d82023-03-21 18:56:31 +000049 switch (ret) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000050 case P256_SUCCESS:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000051 return PSA_SUCCESS;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000052 case P256_INVALID_PUBKEY:
53 case P256_INVALID_PRIVKEY:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000054 return PSA_ERROR_INVALID_ARGUMENT;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000055 case P256_INVALID_SIGNATURE:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000056 return PSA_ERROR_INVALID_SIGNATURE;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000057 case P256_RANDOM_FAILED:
58 default:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000059 return PSA_ERROR_GENERIC_ERROR;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000060 }
61}
62
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020063psa_status_t p256_transparent_import_key(const psa_key_attributes_t *attributes,
64 const uint8_t *data,
65 size_t data_length,
66 uint8_t *key_buffer,
67 size_t key_buffer_size,
68 size_t *key_buffer_length,
69 size_t *bits)
70{
71 /* Check the key size */
72 if (*bits != 0 && *bits != 256) {
73 return PSA_ERROR_NOT_SUPPORTED;
74 }
75
76 /* Validate the key (and its type and size) */
77 psa_key_type_t type = psa_get_key_type(attributes);
78 if (type == PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)) {
79 if (data_length != 65) {
80 return *bits == 0 ? PSA_ERROR_NOT_SUPPORTED : PSA_ERROR_INVALID_ARGUMENT;
81 }
82 if (p256_validate_pubkey(data + 1) != P256_SUCCESS) {
83 return PSA_ERROR_INVALID_ARGUMENT;
84 }
85 } else if (type == PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)) {
86 if (data_length != 32) {
87 return *bits == 0 ? PSA_ERROR_NOT_SUPPORTED : PSA_ERROR_INVALID_ARGUMENT;
88 }
89 if (p256_validate_privkey(data) != P256_SUCCESS) {
90 return PSA_ERROR_INVALID_ARGUMENT;
91 }
92 } else {
93 return PSA_ERROR_NOT_SUPPORTED;
94 }
95 *bits = 256;
96
97 /* We only support the export format for input, so just copy. */
98 if (key_buffer_size < data_length) {
99 return PSA_ERROR_BUFFER_TOO_SMALL;
100 }
101 memcpy(key_buffer, data, data_length);
102 *key_buffer_length = data_length;
103
104 return PSA_SUCCESS;
105}
106
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200107psa_status_t p256_transparent_export_public_key(const psa_key_attributes_t *attributes,
108 const uint8_t *key_buffer,
109 size_t key_buffer_size,
110 uint8_t *data,
111 size_t data_size,
112 size_t *data_length)
113{
114 /* Is this the right curve? */
115 size_t bits = psa_get_key_bits(attributes);
116 psa_key_type_t type = psa_get_key_type(attributes);
117 if (bits != 256 || type != PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)) {
118 return PSA_ERROR_NOT_SUPPORTED;
119 }
120
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200121 /* Validate sizes, as p256-m expects fixed-size buffers */
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200122 if (key_buffer_size != 32) {
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200123 return PSA_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200124 }
125 if (data_size < 65) {
126 return PSA_ERROR_BUFFER_TOO_SMALL;
127 }
128
129 /* Output public key in the PSA export format */
130 data[0] = 0x04;
131 int ret = p256_public_from_private(data + 1, key_buffer);
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200132 if (ret == P256_SUCCESS) {
133 *data_length = 65;
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200134 }
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200135
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200136 return p256_to_psa_error(ret);
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +0200137}
138
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000139psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000140 const psa_key_attributes_t *attributes,
141 uint8_t *key_buffer,
142 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000143 size_t *key_buffer_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000144{
145 /* We don't use this argument, but the specification mandates the signature
146 * of driver entry-points. (void) used to avoid compiler warning. */
147 (void) attributes;
148
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200149 /* Validate sizes, as p256-m expects fixed-size buffers */
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000150 if (key_buffer_size != 32) {
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200151 return PSA_ERROR_BUFFER_TOO_SMALL;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000152 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000153
154 /*
155 * p256-m's keypair generation function outputs both public and private
156 * keys. Allocate a buffer to which the public key will be written. The
157 * private key will be written to key_buffer, which is passed to this
158 * function as an argument. */
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100159 uint8_t public_key_buffer[64];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000160
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200161 int ret = p256_gen_keypair(key_buffer, public_key_buffer);
162 if (ret == P256_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000163 *key_buffer_length = 32;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000164 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000165
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200166 return p256_to_psa_error(ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000167}
168
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000169psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000170 const psa_key_attributes_t *attributes,
171 const uint8_t *key_buffer,
172 size_t key_buffer_size,
173 psa_algorithm_t alg,
174 const uint8_t *peer_key,
175 size_t peer_key_length,
176 uint8_t *shared_secret,
177 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000178 size_t *shared_secret_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000179{
180 /* We don't use these arguments, but the specification mandates the
181 * sginature of driver entry-points. (void) used to avoid compiler
182 * warning. */
183 (void) attributes;
184 (void) alg;
185
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200186 /* Validate sizes, as p256-m expects fixed-size buffers */
187 if (key_buffer_size != 32 || peer_key_length != 65) {
188 return PSA_ERROR_INVALID_ARGUMENT;
189 }
190 if (shared_secret_size < 32) {
191 return PSA_ERROR_BUFFER_TOO_SMALL;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000192 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000193
Aditya Deshpande641cb892023-04-19 03:31:10 +0100194 /* We add 1 to peer_key pointer to omit the leading byte of the public key
195 * representation (0x04). See information about PSA key formats at the top
196 * of the file. */
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200197 int ret = p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key + 1);
198 if (ret == P256_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000199 *shared_secret_length = 32;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000200 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000201
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200202 return p256_to_psa_error(ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000203}
204
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000205psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000206 const psa_key_attributes_t *attributes,
207 const uint8_t *key_buffer,
208 size_t key_buffer_size,
209 psa_algorithm_t alg,
210 const uint8_t *hash,
211 size_t hash_length,
212 uint8_t *signature,
213 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000214 size_t *signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000215{
216 /* We don't use these arguments, but the specification mandates the
217 * sginature of driver entry-points. (void) used to avoid compiler
218 * warning. */
219 (void) attributes;
220 (void) alg;
221
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200222 /* Validate sizes, as p256-m expects fixed-size buffers */
223 if (key_buffer_size != 32) {
224 return PSA_ERROR_CORRUPTION_DETECTED;
225 }
226 if (signature_size < 64) {
227 return PSA_ERROR_BUFFER_TOO_SMALL;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000228 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000229
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200230 int ret = p256_ecdsa_sign(signature, key_buffer, hash, hash_length);
231 if (ret == P256_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000232 *signature_length = 64;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000233 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000234
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200235 return p256_to_psa_error(ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000236}
237
238/* This function expects the key buffer to contain a 65 byte public key,
239 * as exported by psa_export_public_key() */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000240static psa_status_t p256_verify_hash_with_public_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000241 const uint8_t *key_buffer,
242 size_t key_buffer_size,
243 const uint8_t *hash,
244 size_t hash_length,
245 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000246 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000247{
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200248 /* Validate sizes, as p256-m expects fixed-size buffers */
249 if (key_buffer_size != 65 || *key_buffer != 0x04) {
250 return PSA_ERROR_CORRUPTION_DETECTED;
251 }
252 if (signature_length != 64) {
253 return PSA_ERROR_INVALID_SIGNATURE;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000254 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000255
Aditya Deshpande641cb892023-04-19 03:31:10 +0100256 /* We add 1 to public_key_buffer pointer to omit the leading byte of the
257 * public key representation (0x04). See information about PSA key formats
258 * at the top of the file. */
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000259 const uint8_t *public_key_buffer = key_buffer + 1;
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200260 int ret = p256_ecdsa_verify(signature, public_key_buffer, hash, hash_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000261
Manuel Pégourié-Gonnardf0251e02023-08-08 12:23:42 +0200262 return p256_to_psa_error(ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000263}
264
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000265psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000266 const psa_key_attributes_t *attributes,
267 const uint8_t *key_buffer,
268 size_t key_buffer_size,
269 psa_algorithm_t alg,
270 const uint8_t *hash,
271 size_t hash_length,
272 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000273 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000274{
275 /* We don't use this argument, but the specification mandates the signature
276 * of driver entry-points. (void) used to avoid compiler warning. */
277 (void) alg;
278
279 psa_status_t status;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100280 uint8_t public_key_buffer[65];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000281 size_t public_key_buffer_size = 65;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100282
283 size_t public_key_length = 65;
284 /* As p256-m doesn't require dynamic allocation, we want to avoid it in
285 * the entrypoint functions as well. psa_driver_wrapper_export_public_key()
286 * requires size_t*, so we use a pointer to a stack variable. */
287 size_t *public_key_length_ptr = &public_key_length;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000288
Aditya Deshpande641cb892023-04-19 03:31:10 +0100289 /* The contents of key_buffer may either be the 32 byte private key
290 * (keypair format), or 0x04 followed by the 64 byte public key (public
291 * key format). To ensure the key is in the latter format, the public key
292 * is exported. */
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000293 status = psa_driver_wrapper_export_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000294 attributes,
295 key_buffer,
296 key_buffer_size,
297 public_key_buffer,
298 public_key_buffer_size,
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100299 public_key_length_ptr);
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000300 if (status != PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000301 goto exit;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000302 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000303
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000304 status = p256_verify_hash_with_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000305 public_key_buffer,
306 public_key_buffer_size,
307 hash,
308 hash_length,
309 signature,
310 signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000311
312exit:
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000313 return status;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000314}
315
316#endif /* MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED */