Aditya Deshpande | 045b370 | 2023-02-20 17:08:30 +0000 | [diff] [blame] | 1 | /* |
| 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 Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 21 | #include "mbedtls/platform.h" |
| 22 | #include "p256-m_driver_entrypoints.h" |
| 23 | #include "p256-m/p256-m.h" |
| 24 | #include "psa/crypto.h" |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 25 | #include <stddef.h> |
Manuel Pégourié-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame] | 26 | #include <string.h> |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 27 | |
Gilles Peskine | efaee9a | 2023-09-20 20:49:47 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 29 | |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame] | 30 | /* 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 |
Manuel Pégourié-Gonnard | 5ca6934 | 2023-09-20 09:28:02 +0200 | [diff] [blame] | 40 | * keys is only 64 bytes: the same as PSA but without the leading byte (0x04). |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame] | 41 | * Hence, when passing public keys from PSA to p256-m, the leading byte is |
| 42 | * removed. |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 43 | * |
| 44 | * Shared secret and signature have the same format between PSA and p256-m. |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame] | 45 | */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 46 | #define PSA_PUBKEY_SIZE 65 |
| 47 | #define PSA_PUBKEY_HEADER_BYTE 0x04 |
| 48 | #define P256_PUBKEY_SIZE 64 |
| 49 | #define PRIVKEY_SIZE 32 |
| 50 | #define SHARED_SECRET_SIZE 32 |
| 51 | #define SIGNATURE_SIZE 64 |
| 52 | |
| 53 | #define CURVE_BITS 256 |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame] | 54 | |
| 55 | /* Convert between p256-m and PSA error codes */ |
| 56 | static psa_status_t p256_to_psa_error(int ret) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 57 | { |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 58 | switch (ret) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 59 | case P256_SUCCESS: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 60 | return PSA_SUCCESS; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 61 | case P256_INVALID_PUBKEY: |
| 62 | case P256_INVALID_PRIVKEY: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 63 | return PSA_ERROR_INVALID_ARGUMENT; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 64 | case P256_INVALID_SIGNATURE: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 65 | return PSA_ERROR_INVALID_SIGNATURE; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 66 | case P256_RANDOM_FAILED: |
| 67 | default: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 68 | return PSA_ERROR_GENERIC_ERROR; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Manuel Pégourié-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame] | 72 | psa_status_t p256_transparent_import_key(const psa_key_attributes_t *attributes, |
| 73 | const uint8_t *data, |
| 74 | size_t data_length, |
| 75 | uint8_t *key_buffer, |
| 76 | size_t key_buffer_size, |
| 77 | size_t *key_buffer_length, |
| 78 | size_t *bits) |
| 79 | { |
| 80 | /* Check the key size */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 81 | if (*bits != 0 && *bits != CURVE_BITS) { |
Manuel Pégourié-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame] | 82 | return PSA_ERROR_NOT_SUPPORTED; |
| 83 | } |
| 84 | |
| 85 | /* Validate the key (and its type and size) */ |
| 86 | psa_key_type_t type = psa_get_key_type(attributes); |
| 87 | if (type == PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)) { |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 88 | if (data_length != PSA_PUBKEY_SIZE) { |
Manuel Pégourié-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame] | 89 | return *bits == 0 ? PSA_ERROR_NOT_SUPPORTED : PSA_ERROR_INVALID_ARGUMENT; |
| 90 | } |
Manuel Pégourié-Gonnard | 5ca6934 | 2023-09-20 09:28:02 +0200 | [diff] [blame] | 91 | /* See INFORMATION ON PSA KEY EXPORT FORMATS near top of file */ |
Manuel Pégourié-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame] | 92 | if (p256_validate_pubkey(data + 1) != P256_SUCCESS) { |
| 93 | return PSA_ERROR_INVALID_ARGUMENT; |
| 94 | } |
| 95 | } else if (type == PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)) { |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 96 | if (data_length != PRIVKEY_SIZE) { |
Manuel Pégourié-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame] | 97 | return *bits == 0 ? PSA_ERROR_NOT_SUPPORTED : PSA_ERROR_INVALID_ARGUMENT; |
| 98 | } |
| 99 | if (p256_validate_privkey(data) != P256_SUCCESS) { |
| 100 | return PSA_ERROR_INVALID_ARGUMENT; |
| 101 | } |
| 102 | } else { |
| 103 | return PSA_ERROR_NOT_SUPPORTED; |
| 104 | } |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 105 | *bits = CURVE_BITS; |
Manuel Pégourié-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame] | 106 | |
| 107 | /* We only support the export format for input, so just copy. */ |
| 108 | if (key_buffer_size < data_length) { |
| 109 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 110 | } |
| 111 | memcpy(key_buffer, data, data_length); |
| 112 | *key_buffer_length = data_length; |
| 113 | |
| 114 | return PSA_SUCCESS; |
| 115 | } |
| 116 | |
Manuel Pégourié-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 117 | psa_status_t p256_transparent_export_public_key(const psa_key_attributes_t *attributes, |
| 118 | const uint8_t *key_buffer, |
| 119 | size_t key_buffer_size, |
| 120 | uint8_t *data, |
| 121 | size_t data_size, |
| 122 | size_t *data_length) |
| 123 | { |
| 124 | /* Is this the right curve? */ |
| 125 | size_t bits = psa_get_key_bits(attributes); |
| 126 | psa_key_type_t type = psa_get_key_type(attributes); |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 127 | if (bits != CURVE_BITS || type != PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)) { |
Manuel Pégourié-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 128 | return PSA_ERROR_NOT_SUPPORTED; |
| 129 | } |
| 130 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 131 | /* Validate sizes, as p256-m expects fixed-size buffers */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 132 | if (key_buffer_size != PRIVKEY_SIZE) { |
Manuel Pégourié-Gonnard | fbea9d2 | 2023-09-20 09:22:29 +0200 | [diff] [blame] | 133 | return PSA_ERROR_INVALID_ARGUMENT; |
Manuel Pégourié-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 134 | } |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 135 | if (data_size < PSA_PUBKEY_SIZE) { |
Manuel Pégourié-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 136 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 137 | } |
| 138 | |
Manuel Pégourié-Gonnard | 5ca6934 | 2023-09-20 09:28:02 +0200 | [diff] [blame] | 139 | /* See INFORMATION ON PSA KEY EXPORT FORMATS near top of file */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 140 | data[0] = PSA_PUBKEY_HEADER_BYTE; |
Manuel Pégourié-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 141 | int ret = p256_public_from_private(data + 1, key_buffer); |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 142 | if (ret == P256_SUCCESS) { |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 143 | *data_length = PSA_PUBKEY_SIZE; |
Manuel Pégourié-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 144 | } |
Manuel Pégourié-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 145 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 146 | return p256_to_psa_error(ret); |
Manuel Pégourié-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 149 | psa_status_t p256_transparent_generate_key( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 150 | const psa_key_attributes_t *attributes, |
| 151 | uint8_t *key_buffer, |
| 152 | size_t key_buffer_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 153 | size_t *key_buffer_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 154 | { |
| 155 | /* We don't use this argument, but the specification mandates the signature |
| 156 | * of driver entry-points. (void) used to avoid compiler warning. */ |
| 157 | (void) attributes; |
| 158 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 159 | /* Validate sizes, as p256-m expects fixed-size buffers */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 160 | if (key_buffer_size != PRIVKEY_SIZE) { |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 161 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 162 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * p256-m's keypair generation function outputs both public and private |
| 166 | * keys. Allocate a buffer to which the public key will be written. The |
| 167 | * private key will be written to key_buffer, which is passed to this |
| 168 | * function as an argument. */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 169 | uint8_t public_key_buffer[P256_PUBKEY_SIZE]; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 170 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 171 | int ret = p256_gen_keypair(key_buffer, public_key_buffer); |
| 172 | if (ret == P256_SUCCESS) { |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 173 | *key_buffer_length = PRIVKEY_SIZE; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 174 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 175 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 176 | return p256_to_psa_error(ret); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 179 | psa_status_t p256_transparent_key_agreement( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 180 | const psa_key_attributes_t *attributes, |
| 181 | const uint8_t *key_buffer, |
| 182 | size_t key_buffer_size, |
| 183 | psa_algorithm_t alg, |
| 184 | const uint8_t *peer_key, |
| 185 | size_t peer_key_length, |
| 186 | uint8_t *shared_secret, |
| 187 | size_t shared_secret_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 188 | size_t *shared_secret_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 189 | { |
| 190 | /* We don't use these arguments, but the specification mandates the |
| 191 | * sginature of driver entry-points. (void) used to avoid compiler |
| 192 | * warning. */ |
| 193 | (void) attributes; |
| 194 | (void) alg; |
| 195 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 196 | /* Validate sizes, as p256-m expects fixed-size buffers */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 197 | if (key_buffer_size != PRIVKEY_SIZE || peer_key_length != PSA_PUBKEY_SIZE) { |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 198 | return PSA_ERROR_INVALID_ARGUMENT; |
| 199 | } |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 200 | if (shared_secret_size < SHARED_SECRET_SIZE) { |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 201 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 202 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 203 | |
Manuel Pégourié-Gonnard | 5ca6934 | 2023-09-20 09:28:02 +0200 | [diff] [blame] | 204 | /* See INFORMATION ON PSA KEY EXPORT FORMATS near top of file */ |
Manuel Pégourié-Gonnard | 3ec976c | 2023-09-20 16:12:46 +0200 | [diff] [blame] | 205 | const uint8_t *peer_key_p256m = peer_key + 1; |
Manuel Pégourié-Gonnard | 5ca6934 | 2023-09-20 09:28:02 +0200 | [diff] [blame] | 206 | int ret = p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key_p256m); |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 207 | if (ret == P256_SUCCESS) { |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 208 | *shared_secret_length = SHARED_SECRET_SIZE; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 209 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 210 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 211 | return p256_to_psa_error(ret); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 214 | psa_status_t p256_transparent_sign_hash( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 215 | 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 Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 223 | size_t *signature_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 224 | { |
| 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 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 231 | /* Validate sizes, as p256-m expects fixed-size buffers */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 232 | if (key_buffer_size != PRIVKEY_SIZE) { |
Manuel Pégourié-Gonnard | fbea9d2 | 2023-09-20 09:22:29 +0200 | [diff] [blame] | 233 | return PSA_ERROR_INVALID_ARGUMENT; |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 234 | } |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 235 | if (signature_size < SIGNATURE_SIZE) { |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 236 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 237 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 239 | int ret = p256_ecdsa_sign(signature, key_buffer, hash, hash_length); |
| 240 | if (ret == P256_SUCCESS) { |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 241 | *signature_length = SIGNATURE_SIZE; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 242 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 243 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 244 | return p256_to_psa_error(ret); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 247 | /* This function expects the key buffer to contain a PSA public key, |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 248 | * as exported by psa_export_public_key() */ |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 249 | static psa_status_t p256_verify_hash_with_public_key( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 250 | const uint8_t *key_buffer, |
| 251 | size_t key_buffer_size, |
| 252 | const uint8_t *hash, |
| 253 | size_t hash_length, |
| 254 | const uint8_t *signature, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 255 | size_t signature_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 256 | { |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 257 | /* Validate sizes, as p256-m expects fixed-size buffers */ |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 258 | if (key_buffer_size != PSA_PUBKEY_SIZE || *key_buffer != PSA_PUBKEY_HEADER_BYTE) { |
Manuel Pégourié-Gonnard | fbea9d2 | 2023-09-20 09:22:29 +0200 | [diff] [blame] | 259 | return PSA_ERROR_INVALID_ARGUMENT; |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 260 | } |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 261 | if (signature_length != SIGNATURE_SIZE) { |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 262 | return PSA_ERROR_INVALID_SIGNATURE; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 263 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 264 | |
Manuel Pégourié-Gonnard | 5ca6934 | 2023-09-20 09:28:02 +0200 | [diff] [blame] | 265 | /* See INFORMATION ON PSA KEY EXPORT FORMATS near top of file */ |
| 266 | const uint8_t *public_key_p256m = key_buffer + 1; |
| 267 | int ret = p256_ecdsa_verify(signature, public_key_p256m, hash, hash_length); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 268 | |
Manuel Pégourié-Gonnard | f0251e0 | 2023-08-08 12:23:42 +0200 | [diff] [blame] | 269 | return p256_to_psa_error(ret); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 272 | psa_status_t p256_transparent_verify_hash( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 273 | const psa_key_attributes_t *attributes, |
| 274 | const uint8_t *key_buffer, |
| 275 | size_t key_buffer_size, |
| 276 | psa_algorithm_t alg, |
| 277 | const uint8_t *hash, |
| 278 | size_t hash_length, |
| 279 | const uint8_t *signature, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 280 | size_t signature_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 281 | { |
| 282 | /* We don't use this argument, but the specification mandates the signature |
| 283 | * of driver entry-points. (void) used to avoid compiler warning. */ |
| 284 | (void) alg; |
| 285 | |
| 286 | psa_status_t status; |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 287 | uint8_t public_key_buffer[PSA_PUBKEY_SIZE]; |
| 288 | size_t public_key_buffer_size = PSA_PUBKEY_SIZE; |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 289 | |
Manuel Pégourié-Gonnard | ba63e0c | 2023-08-09 11:53:09 +0200 | [diff] [blame] | 290 | size_t public_key_length = PSA_PUBKEY_SIZE; |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 291 | /* As p256-m doesn't require dynamic allocation, we want to avoid it in |
| 292 | * the entrypoint functions as well. psa_driver_wrapper_export_public_key() |
| 293 | * requires size_t*, so we use a pointer to a stack variable. */ |
| 294 | size_t *public_key_length_ptr = &public_key_length; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 295 | |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame] | 296 | /* The contents of key_buffer may either be the 32 byte private key |
| 297 | * (keypair format), or 0x04 followed by the 64 byte public key (public |
| 298 | * key format). To ensure the key is in the latter format, the public key |
| 299 | * is exported. */ |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 300 | status = psa_driver_wrapper_export_public_key( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 301 | attributes, |
| 302 | key_buffer, |
| 303 | key_buffer_size, |
| 304 | public_key_buffer, |
| 305 | public_key_buffer_size, |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 306 | public_key_length_ptr); |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 307 | if (status != PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 308 | goto exit; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 309 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 310 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 311 | status = p256_verify_hash_with_public_key( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 312 | public_key_buffer, |
| 313 | public_key_buffer_size, |
| 314 | hash, |
| 315 | hash_length, |
| 316 | signature, |
| 317 | signature_length); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 318 | |
| 319 | exit: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 320 | return status; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Gilles Peskine | efaee9a | 2023-09-20 20:49:47 +0200 | [diff] [blame] | 323 | #endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */ |