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 | |
| 29 | #if defined(MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED) |
| 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 |
| 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 */ |
| 47 | static psa_status_t p256_to_psa_error(int ret) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 48 | { |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 49 | switch (ret) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 50 | case P256_SUCCESS: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 51 | return PSA_SUCCESS; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 52 | case P256_INVALID_PUBKEY: |
| 53 | case P256_INVALID_PRIVKEY: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 54 | return PSA_ERROR_INVALID_ARGUMENT; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 55 | case P256_INVALID_SIGNATURE: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 56 | return PSA_ERROR_INVALID_SIGNATURE; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 57 | case P256_RANDOM_FAILED: |
| 58 | default: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 59 | return PSA_ERROR_GENERIC_ERROR; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Manuel Pégourié-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame^] | 63 | psa_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 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 107 | psa_status_t p256_transparent_generate_key( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 108 | const psa_key_attributes_t *attributes, |
| 109 | uint8_t *key_buffer, |
| 110 | size_t key_buffer_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 111 | size_t *key_buffer_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 112 | { |
| 113 | /* We don't use this argument, but the specification mandates the signature |
| 114 | * of driver entry-points. (void) used to avoid compiler warning. */ |
| 115 | (void) attributes; |
| 116 | |
| 117 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 118 | |
| 119 | /* |
| 120 | * p256-m generates a 32 byte private key, and expects to write to a buffer |
| 121 | * that is of that size. */ |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 122 | if (key_buffer_size != 32) { |
| 123 | return status; |
| 124 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * p256-m's keypair generation function outputs both public and private |
| 128 | * keys. Allocate a buffer to which the public key will be written. The |
| 129 | * private key will be written to key_buffer, which is passed to this |
| 130 | * function as an argument. */ |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 131 | uint8_t public_key_buffer[64]; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 132 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 133 | status = p256_to_psa_error( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 134 | p256_gen_keypair(key_buffer, public_key_buffer)); |
| 135 | if (status == PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 136 | *key_buffer_length = 32; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 137 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 138 | |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 139 | return status; |
| 140 | } |
| 141 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 142 | psa_status_t p256_transparent_key_agreement( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 143 | const psa_key_attributes_t *attributes, |
| 144 | const uint8_t *key_buffer, |
| 145 | size_t key_buffer_size, |
| 146 | psa_algorithm_t alg, |
| 147 | const uint8_t *peer_key, |
| 148 | size_t peer_key_length, |
| 149 | uint8_t *shared_secret, |
| 150 | size_t shared_secret_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 151 | size_t *shared_secret_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 152 | { |
| 153 | /* We don't use these arguments, but the specification mandates the |
| 154 | * sginature of driver entry-points. (void) used to avoid compiler |
| 155 | * warning. */ |
| 156 | (void) attributes; |
| 157 | (void) alg; |
| 158 | |
| 159 | /* |
| 160 | * Check that private key = 32 bytes, peer public key = 65 bytes, |
| 161 | * and that the shared secret buffer is big enough. */ |
Valerio Setti | 983923c | 2023-08-03 15:33:24 +0200 | [diff] [blame] | 162 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 163 | if (key_buffer_size != 32 || shared_secret_size < 32 || |
| 164 | peer_key_length != 65) { |
| 165 | return status; |
| 166 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 167 | |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame] | 168 | /* We add 1 to peer_key pointer to omit the leading byte of the public key |
| 169 | * representation (0x04). See information about PSA key formats at the top |
| 170 | * of the file. */ |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 171 | status = p256_to_psa_error( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 172 | p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key+1)); |
| 173 | if (status == PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 174 | *shared_secret_length = 32; |
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 | |
| 177 | return status; |
| 178 | } |
| 179 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 180 | psa_status_t p256_transparent_sign_hash( |
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 *hash, |
| 186 | size_t hash_length, |
| 187 | uint8_t *signature, |
| 188 | size_t signature_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 189 | size_t *signature_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 | |
| 197 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
Valerio Setti | 983923c | 2023-08-03 15:33:24 +0200 | [diff] [blame] | 198 | if (key_buffer_size != 32 || signature_size < 64) { |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 199 | return status; |
| 200 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 201 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 202 | status = p256_to_psa_error( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 203 | p256_ecdsa_sign(signature, key_buffer, hash, hash_length)); |
| 204 | if (status == PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 205 | *signature_length = 64; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 206 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 207 | |
| 208 | return status; |
| 209 | } |
| 210 | |
| 211 | /* This function expects the key buffer to contain a 65 byte public key, |
| 212 | * as exported by psa_export_public_key() */ |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 213 | static psa_status_t p256_verify_hash_with_public_key( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 214 | const uint8_t *key_buffer, |
| 215 | size_t key_buffer_size, |
| 216 | const uint8_t *hash, |
| 217 | size_t hash_length, |
| 218 | const uint8_t *signature, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 219 | size_t signature_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 220 | { |
| 221 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 222 | if (key_buffer_size != 65 || signature_length != 64 || *key_buffer != 0x04) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 223 | return status; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 224 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 225 | |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame] | 226 | /* We add 1 to public_key_buffer pointer to omit the leading byte of the |
| 227 | * public key representation (0x04). See information about PSA key formats |
| 228 | * at the top of the file. */ |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 229 | const uint8_t *public_key_buffer = key_buffer + 1; |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 230 | status = p256_to_psa_error( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 231 | p256_ecdsa_verify(signature, public_key_buffer, hash, hash_length)); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 232 | |
| 233 | return status; |
| 234 | } |
| 235 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 236 | psa_status_t p256_transparent_verify_hash( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 237 | const psa_key_attributes_t *attributes, |
| 238 | const uint8_t *key_buffer, |
| 239 | size_t key_buffer_size, |
| 240 | psa_algorithm_t alg, |
| 241 | const uint8_t *hash, |
| 242 | size_t hash_length, |
| 243 | const uint8_t *signature, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 244 | size_t signature_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 245 | { |
| 246 | /* We don't use this argument, but the specification mandates the signature |
| 247 | * of driver entry-points. (void) used to avoid compiler warning. */ |
| 248 | (void) alg; |
| 249 | |
| 250 | psa_status_t status; |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 251 | uint8_t public_key_buffer[65]; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 252 | size_t public_key_buffer_size = 65; |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 253 | |
| 254 | size_t public_key_length = 65; |
| 255 | /* As p256-m doesn't require dynamic allocation, we want to avoid it in |
| 256 | * the entrypoint functions as well. psa_driver_wrapper_export_public_key() |
| 257 | * requires size_t*, so we use a pointer to a stack variable. */ |
| 258 | size_t *public_key_length_ptr = &public_key_length; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 259 | |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame] | 260 | /* The contents of key_buffer may either be the 32 byte private key |
| 261 | * (keypair format), or 0x04 followed by the 64 byte public key (public |
| 262 | * key format). To ensure the key is in the latter format, the public key |
| 263 | * is exported. */ |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 264 | status = psa_driver_wrapper_export_public_key( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 265 | attributes, |
| 266 | key_buffer, |
| 267 | key_buffer_size, |
| 268 | public_key_buffer, |
| 269 | public_key_buffer_size, |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 270 | public_key_length_ptr); |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 271 | if (status != PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 272 | goto exit; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 273 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 274 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 275 | status = p256_verify_hash_with_public_key( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 276 | public_key_buffer, |
| 277 | public_key_buffer_size, |
| 278 | hash, |
| 279 | hash_length, |
| 280 | signature, |
| 281 | signature_length); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 282 | |
| 283 | exit: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 284 | return status; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | #endif /* MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED */ |