blob: 8df640cba8fd7acfe7374d65e2fcdb29df37a812 [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 Deshpandeac363d82023-03-21 18:56:31 +000030psa_status_t p256_to_psa_error(int ret)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000031{
Aditya Deshpandeac363d82023-03-21 18:56:31 +000032 switch (ret) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000033 case P256_SUCCESS:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000034 return PSA_SUCCESS;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000035 case P256_INVALID_PUBKEY:
36 case P256_INVALID_PRIVKEY:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000037 return PSA_ERROR_INVALID_ARGUMENT;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000038 case P256_INVALID_SIGNATURE:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000039 return PSA_ERROR_INVALID_SIGNATURE;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000040 case P256_RANDOM_FAILED:
41 default:
Aditya Deshpandeac363d82023-03-21 18:56:31 +000042 return PSA_ERROR_GENERIC_ERROR;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000043 }
44}
45
Aditya Deshpande695e44b2023-01-23 14:59:29 +000046psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000047 const psa_key_attributes_t *attributes,
48 uint8_t *key_buffer,
49 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +000050 size_t *key_buffer_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000051{
52 /* We don't use this argument, but the specification mandates the signature
53 * of driver entry-points. (void) used to avoid compiler warning. */
54 (void) attributes;
55
56 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
57
58 /*
59 * p256-m generates a 32 byte private key, and expects to write to a buffer
60 * that is of that size. */
Aditya Deshpandeac363d82023-03-21 18:56:31 +000061 if (key_buffer_size != 32) {
62 return status;
63 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000064
65 /*
66 * p256-m's keypair generation function outputs both public and private
67 * keys. Allocate a buffer to which the public key will be written. The
68 * private key will be written to key_buffer, which is passed to this
69 * function as an argument. */
Aditya Deshpande7b9934d2023-04-18 17:00:17 +010070 uint8_t public_key_buffer[64];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000071
Aditya Deshpande695e44b2023-01-23 14:59:29 +000072 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +000073 p256_gen_keypair(key_buffer, public_key_buffer));
74 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000075 *key_buffer_length = 32;
Aditya Deshpandeac363d82023-03-21 18:56:31 +000076 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000077
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000078 return status;
79}
80
Aditya Deshpande695e44b2023-01-23 14:59:29 +000081psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000082 const psa_key_attributes_t *attributes,
83 const uint8_t *key_buffer,
84 size_t key_buffer_size,
85 psa_algorithm_t alg,
86 const uint8_t *peer_key,
87 size_t peer_key_length,
88 uint8_t *shared_secret,
89 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +000090 size_t *shared_secret_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000091{
92 /* We don't use these arguments, but the specification mandates the
93 * sginature of driver entry-points. (void) used to avoid compiler
94 * warning. */
95 (void) attributes;
96 (void) alg;
97
98 /*
99 * Check that private key = 32 bytes, peer public key = 65 bytes,
100 * and that the shared secret buffer is big enough. */
101 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000102 if (key_buffer_size != 32 || shared_secret_size < 32 ||
103 peer_key_length != 65) {
104 return status;
105 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000106
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000107 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000108 p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key+1));
109 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000110 *shared_secret_length = 32;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000111 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000112
113 return status;
114}
115
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000116psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000117 const psa_key_attributes_t *attributes,
118 const uint8_t *key_buffer,
119 size_t key_buffer_size,
120 psa_algorithm_t alg,
121 const uint8_t *hash,
122 size_t hash_length,
123 uint8_t *signature,
124 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000125 size_t *signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000126{
127 /* We don't use these arguments, but the specification mandates the
128 * sginature of driver entry-points. (void) used to avoid compiler
129 * warning. */
130 (void) attributes;
131 (void) alg;
132
133 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000134 if (key_buffer_size != 32 || signature_size != 64) {
135 return status;
136 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000137
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000138 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000139 p256_ecdsa_sign(signature, key_buffer, hash, hash_length));
140 if (status == PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000141 *signature_length = 64;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000142 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000143
144 return status;
145}
146
147/* This function expects the key buffer to contain a 65 byte public key,
148 * as exported by psa_export_public_key() */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000149static psa_status_t p256_verify_hash_with_public_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000150 const uint8_t *key_buffer,
151 size_t key_buffer_size,
152 const uint8_t *hash,
153 size_t hash_length,
154 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000155 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000156{
157 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000158 if (key_buffer_size != 65 || signature_length != 64 || *key_buffer != 0x04) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000159 return status;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000160 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000161
162 const uint8_t *public_key_buffer = key_buffer + 1;
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000163 status = p256_to_psa_error(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000164 p256_ecdsa_verify(signature, public_key_buffer, hash, hash_length));
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000165
166 return status;
167}
168
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000169psa_status_t p256_transparent_verify_hash(
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 *hash,
175 size_t hash_length,
176 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000177 size_t signature_length)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000178{
179 /* We don't use this argument, but the specification mandates the signature
180 * of driver entry-points. (void) used to avoid compiler warning. */
181 (void) alg;
182
183 psa_status_t status;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100184 uint8_t public_key_buffer[65];
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000185 size_t public_key_buffer_size = 65;
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100186
187 size_t public_key_length = 65;
188 /* As p256-m doesn't require dynamic allocation, we want to avoid it in
189 * the entrypoint functions as well. psa_driver_wrapper_export_public_key()
190 * requires size_t*, so we use a pointer to a stack variable. */
191 size_t *public_key_length_ptr = &public_key_length;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000192
193 /* The contents of key_buffer may either be the 32 byte private key
194 * (keypair representation), or the 65 byte public key. To ensure the
195 * latter is obtained, the public key is exported. */
196 status = psa_driver_wrapper_export_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000197 attributes,
198 key_buffer,
199 key_buffer_size,
200 public_key_buffer,
201 public_key_buffer_size,
Aditya Deshpande7b9934d2023-04-18 17:00:17 +0100202 public_key_length_ptr);
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000203 if (status != PSA_SUCCESS) {
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000204 goto exit;
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000205 }
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000206
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000207 status = p256_verify_hash_with_public_key(
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000208 public_key_buffer,
209 public_key_buffer_size,
210 hash,
211 hash_length,
212 signature,
213 signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000214
215exit:
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000216 return status;
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000217}
218
219#endif /* MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED */