blob: 6131daec29cb89e6d32ede6d3095287da106ce52 [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
121 /* Validate input and output sizes */
122 if (key_buffer_size != 32) {
123 return PSA_ERROR_INVALID_ARGUMENT;
124 }
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);
132 if (ret != P256_SUCCESS) {
133 /* The only possible error is the private key was invalid */
134 return PSA_ERROR_INVALID_ARGUMENT;
135 }
136 *data_length = 65;
137
138 return PSA_SUCCESS;
139}
140
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000141psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000142 const psa_key_attributes_t *attributes,
143 uint8_t *key_buffer,
144 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000145 size_t *key_buffer_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000146{
147 /* We don't use this argument, but the specification mandates the signature
148 * of driver entry-points. (void) used to avoid compiler warning. */
149 (void) attributes;
150
151 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
152
153 /*
154 * p256-m generates a 32 byte private key, and expects to write to a buffer
155 * that is of that size. */
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000156 if (key_buffer_size != 32) {
157 return status;
158 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000159
160 /*
161 * p256-m's keypair generation function outputs both public and private
162 * keys. Allocate a buffer to which the public key will be written. The
163 * private key will be written to key_buffer, which is passed to this
164 * function as an argument. */
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100165 uint8_t public_key_buffer[64];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000166
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000167 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000168 p256_gen_keypair(key_buffer, public_key_buffer));
169 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000170 *key_buffer_length = 32;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000171 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000172
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000173 return status;
174}
175
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000176psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000177 const psa_key_attributes_t *attributes,
178 const uint8_t *key_buffer,
179 size_t key_buffer_size,
180 psa_algorithm_t alg,
181 const uint8_t *peer_key,
182 size_t peer_key_length,
183 uint8_t *shared_secret,
184 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000185 size_t *shared_secret_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000186{
187 /* We don't use these arguments, but the specification mandates the
188 * sginature of driver entry-points. (void) used to avoid compiler
189 * warning. */
190 (void) attributes;
191 (void) alg;
192
193 /*
194 * Check that private key = 32 bytes, peer public key = 65 bytes,
195 * and that the shared secret buffer is big enough. */
Valerio Setti983923c2023-08-03 15:33:24 +0200196 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000197 if (key_buffer_size != 32 || shared_secret_size < 32 ||
198 peer_key_length != 65) {
199 return status;
200 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000201
Aditya Deshpande641cb892023-04-19 03:31:10 +0100202 /* We add 1 to peer_key pointer to omit the leading byte of the public key
203 * representation (0x04). See information about PSA key formats at the top
204 * of the file. */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000205 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000206 p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key+1));
207 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000208 *shared_secret_length = 32;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000209 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000210
211 return status;
212}
213
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000214psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000215 const psa_key_attributes_t *attributes,
216 const uint8_t *key_buffer,
217 size_t key_buffer_size,
218 psa_algorithm_t alg,
219 const uint8_t *hash,
220 size_t hash_length,
221 uint8_t *signature,
222 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000223 size_t *signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000224{
225 /* We don't use these arguments, but the specification mandates the
226 * sginature of driver entry-points. (void) used to avoid compiler
227 * warning. */
228 (void) attributes;
229 (void) alg;
230
231 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
Valerio Setti983923c2023-08-03 15:33:24 +0200232 if (key_buffer_size != 32 || signature_size < 64) {
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000233 return status;
234 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000235
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000236 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000237 p256_ecdsa_sign(signature, key_buffer, hash, hash_length));
238 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000239 *signature_length = 64;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000240 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000241
242 return status;
243}
244
245/* This function expects the key buffer to contain a 65 byte public key,
246 * as exported by psa_export_public_key() */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000247static psa_status_t p256_verify_hash_with_public_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000248 const uint8_t *key_buffer,
249 size_t key_buffer_size,
250 const uint8_t *hash,
251 size_t hash_length,
252 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000253 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000254{
255 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000256 if (key_buffer_size != 65 || signature_length != 64 || *key_buffer != 0x04) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000257 return status;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000258 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000259
Aditya Deshpande641cb892023-04-19 03:31:10 +0100260 /* We add 1 to public_key_buffer pointer to omit the leading byte of the
261 * public key representation (0x04). See information about PSA key formats
262 * at the top of the file. */
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000263 const uint8_t *public_key_buffer = key_buffer + 1;
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000264 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000265 p256_ecdsa_verify(signature, public_key_buffer, hash, hash_length));
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000266
267 return status;
268}
269
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000270psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000271 const psa_key_attributes_t *attributes,
272 const uint8_t *key_buffer,
273 size_t key_buffer_size,
274 psa_algorithm_t alg,
275 const uint8_t *hash,
276 size_t hash_length,
277 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000278 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000279{
280 /* We don't use this argument, but the specification mandates the signature
281 * of driver entry-points. (void) used to avoid compiler warning. */
282 (void) alg;
283
284 psa_status_t status;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100285 uint8_t public_key_buffer[65];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000286 size_t public_key_buffer_size = 65;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100287
288 size_t public_key_length = 65;
289 /* As p256-m doesn't require dynamic allocation, we want to avoid it in
290 * the entrypoint functions as well. psa_driver_wrapper_export_public_key()
291 * requires size_t*, so we use a pointer to a stack variable. */
292 size_t *public_key_length_ptr = &public_key_length;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000293
Aditya Deshpande641cb892023-04-19 03:31:10 +0100294 /* The contents of key_buffer may either be the 32 byte private key
295 * (keypair format), or 0x04 followed by the 64 byte public key (public
296 * key format). To ensure the key is in the latter format, the public key
297 * is exported. */
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000298 status = psa_driver_wrapper_export_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000299 attributes,
300 key_buffer,
301 key_buffer_size,
302 public_key_buffer,
303 public_key_buffer_size,
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100304 public_key_length_ptr);
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000305 if (status != PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000306 goto exit;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000307 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000308
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000309 status = p256_verify_hash_with_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000310 public_key_buffer,
311 public_key_buffer_size,
312 hash,
313 hash_length,
314 signature,
315 signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000316
317exit:
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000318 return status;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000319}
320
321#endif /* MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED */