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