blob: 90dce1e18cb3958ed02cf9e104220121ef3c60d6 [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>
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000027
28#if defined(MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED)
29
Aditya Deshpande641cb892023-04-19 03:31:10 +010030/* INFORMATION ON PSA KEY EXPORT FORMATS:
31 *
32 * PSA exports SECP256R1 keys in two formats:
33 * 1. Keypair format: 32 byte string which is just the private key (public key
34 * can be calculated from the private key)
35 * 2. Public Key format: A leading byte 0x04 (indicating uncompressed format),
36 * followed by the 64 byte public key. This results in a
37 * total of 65 bytes.
38 *
39 * p256-m's internal format for private keys matches PSA. Its format for public
40 * keys is only 64 bytes; the same as PSA but without the leading byte (0x04).
41 * Hence, when passing public keys from PSA to p256-m, the leading byte is
42 * removed.
43 */
44
45/* Convert between p256-m and PSA error codes */
46static psa_status_t p256_to_psa_error(int ret)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000047{
Aditya Deshpandeac363d82023-03-21 18:56:31 +000048 switch (ret) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000049 case P256_SUCCESS:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000050 return PSA_SUCCESS;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000051 case P256_INVALID_PUBKEY:
52 case P256_INVALID_PRIVKEY:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000053 return PSA_ERROR_INVALID_ARGUMENT;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000054 case P256_INVALID_SIGNATURE:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000055 return PSA_ERROR_INVALID_SIGNATURE;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000056 case P256_RANDOM_FAILED:
57 default:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000058 return PSA_ERROR_GENERIC_ERROR;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000059 }
60}
61
Aditya Deshpande695e44b2023-01-23 14:59:29 +000062psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000063 const psa_key_attributes_t *attributes,
64 uint8_t *key_buffer,
65 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +000066 size_t *key_buffer_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000067{
68 /* We don't use this argument, but the specification mandates the signature
69 * of driver entry-points. (void) used to avoid compiler warning. */
70 (void) attributes;
71
72 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
73
74 /*
75 * p256-m generates a 32 byte private key, and expects to write to a buffer
76 * that is of that size. */
Aditya Deshpandeac363d82023-03-21 18:56:31 +000077 if (key_buffer_size != 32) {
78 return status;
79 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000080
81 /*
82 * p256-m's keypair generation function outputs both public and private
83 * keys. Allocate a buffer to which the public key will be written. The
84 * private key will be written to key_buffer, which is passed to this
85 * function as an argument. */
Aditya Deshpande7b9934d2023-04-18 17:00:17 +010086 uint8_t public_key_buffer[64];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000087
Aditya Deshpande695e44b2023-01-23 14:59:29 +000088 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +000089 p256_gen_keypair(key_buffer, public_key_buffer));
90 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000091 *key_buffer_length = 32;
Aditya Deshpandeac363d82023-03-21 18:56:31 +000092 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000093
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000094 return status;
95}
96
Aditya Deshpande695e44b2023-01-23 14:59:29 +000097psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000098 const psa_key_attributes_t *attributes,
99 const uint8_t *key_buffer,
100 size_t key_buffer_size,
101 psa_algorithm_t alg,
102 const uint8_t *peer_key,
103 size_t peer_key_length,
104 uint8_t *shared_secret,
105 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000106 size_t *shared_secret_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000107{
108 /* We don't use these arguments, but the specification mandates the
109 * sginature of driver entry-points. (void) used to avoid compiler
110 * warning. */
111 (void) attributes;
112 (void) alg;
113
114 /*
115 * Check that private key = 32 bytes, peer public key = 65 bytes,
116 * and that the shared secret buffer is big enough. */
Valerio Setti983923c2023-08-03 15:33:24 +0200117 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000118 if (key_buffer_size != 32 || shared_secret_size < 32 ||
119 peer_key_length != 65) {
120 return status;
121 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000122
Aditya Deshpande641cb892023-04-19 03:31:10 +0100123 /* We add 1 to peer_key pointer to omit the leading byte of the public key
124 * representation (0x04). See information about PSA key formats at the top
125 * of the file. */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000126 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000127 p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key+1));
128 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000129 *shared_secret_length = 32;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000130 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000131
132 return status;
133}
134
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000135psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000136 const psa_key_attributes_t *attributes,
137 const uint8_t *key_buffer,
138 size_t key_buffer_size,
139 psa_algorithm_t alg,
140 const uint8_t *hash,
141 size_t hash_length,
142 uint8_t *signature,
143 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000144 size_t *signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000145{
146 /* We don't use these arguments, but the specification mandates the
147 * sginature of driver entry-points. (void) used to avoid compiler
148 * warning. */
149 (void) attributes;
150 (void) alg;
151
152 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
Valerio Setti983923c2023-08-03 15:33:24 +0200153 if (key_buffer_size != 32 || signature_size < 64) {
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000154 return status;
155 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000156
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000157 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000158 p256_ecdsa_sign(signature, key_buffer, hash, hash_length));
159 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000160 *signature_length = 64;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000161 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000162
163 return status;
164}
165
166/* This function expects the key buffer to contain a 65 byte public key,
167 * as exported by psa_export_public_key() */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000168static psa_status_t p256_verify_hash_with_public_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000169 const uint8_t *key_buffer,
170 size_t key_buffer_size,
171 const uint8_t *hash,
172 size_t hash_length,
173 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000174 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000175{
176 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000177 if (key_buffer_size != 65 || signature_length != 64 || *key_buffer != 0x04) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000178 return status;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000179 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000180
Aditya Deshpande641cb892023-04-19 03:31:10 +0100181 /* We add 1 to public_key_buffer pointer to omit the leading byte of the
182 * public key representation (0x04). See information about PSA key formats
183 * at the top of the file. */
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000184 const uint8_t *public_key_buffer = key_buffer + 1;
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000185 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000186 p256_ecdsa_verify(signature, public_key_buffer, hash, hash_length));
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000187
188 return status;
189}
190
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000191psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000192 const psa_key_attributes_t *attributes,
193 const uint8_t *key_buffer,
194 size_t key_buffer_size,
195 psa_algorithm_t alg,
196 const uint8_t *hash,
197 size_t hash_length,
198 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000199 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000200{
201 /* We don't use this argument, but the specification mandates the signature
202 * of driver entry-points. (void) used to avoid compiler warning. */
203 (void) alg;
204
205 psa_status_t status;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100206 uint8_t public_key_buffer[65];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000207 size_t public_key_buffer_size = 65;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100208
209 size_t public_key_length = 65;
210 /* As p256-m doesn't require dynamic allocation, we want to avoid it in
211 * the entrypoint functions as well. psa_driver_wrapper_export_public_key()
212 * requires size_t*, so we use a pointer to a stack variable. */
213 size_t *public_key_length_ptr = &public_key_length;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000214
Aditya Deshpande641cb892023-04-19 03:31:10 +0100215 /* The contents of key_buffer may either be the 32 byte private key
216 * (keypair format), or 0x04 followed by the 64 byte public key (public
217 * key format). To ensure the key is in the latter format, the public key
218 * is exported. */
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000219 status = psa_driver_wrapper_export_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000220 attributes,
221 key_buffer,
222 key_buffer_size,
223 public_key_buffer,
224 public_key_buffer_size,
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100225 public_key_length_ptr);
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000226 if (status != PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000227 goto exit;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000228 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000229
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000230 status = p256_verify_hash_with_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000231 public_key_buffer,
232 public_key_buffer_size,
233 hash,
234 hash_length,
235 signature,
236 signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000237
238exit:
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000239 return status;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000240}
241
242#endif /* MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED */