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> |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 27 | |
| 28 | #if defined(MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED) |
| 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 |
| 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 */ |
| 46 | static psa_status_t p256_to_psa_error(int ret) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 47 | { |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 48 | switch (ret) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 49 | case P256_SUCCESS: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 50 | return PSA_SUCCESS; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 51 | case P256_INVALID_PUBKEY: |
| 52 | case P256_INVALID_PRIVKEY: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 53 | return PSA_ERROR_INVALID_ARGUMENT; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 54 | case P256_INVALID_SIGNATURE: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 55 | return PSA_ERROR_INVALID_SIGNATURE; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 56 | case P256_RANDOM_FAILED: |
| 57 | default: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 58 | return PSA_ERROR_GENERIC_ERROR; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 62 | psa_status_t p256_transparent_generate_key( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 63 | const psa_key_attributes_t *attributes, |
| 64 | uint8_t *key_buffer, |
| 65 | size_t key_buffer_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 66 | size_t *key_buffer_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 67 | { |
| 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 Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 77 | if (key_buffer_size != 32) { |
| 78 | return status; |
| 79 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 80 | |
| 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 Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 86 | uint8_t public_key_buffer[64]; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 87 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 88 | status = p256_to_psa_error( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 89 | p256_gen_keypair(key_buffer, public_key_buffer)); |
| 90 | if (status == PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 91 | *key_buffer_length = 32; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 92 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 93 | |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 94 | return status; |
| 95 | } |
| 96 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 97 | psa_status_t p256_transparent_key_agreement( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 98 | 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 Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 106 | size_t *shared_secret_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 107 | { |
| 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. */ |
| 117 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 118 | if (key_buffer_size != 32 || shared_secret_size < 32 || |
| 119 | peer_key_length != 65) { |
| 120 | return status; |
| 121 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 122 | |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame^] | 123 | /* 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 Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 126 | status = p256_to_psa_error( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 127 | p256_ecdh_shared_secret(shared_secret, key_buffer, peer_key+1)); |
| 128 | if (status == PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 129 | *shared_secret_length = 32; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 130 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 131 | |
| 132 | return status; |
| 133 | } |
| 134 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 135 | psa_status_t p256_transparent_sign_hash( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 136 | 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 Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 144 | size_t *signature_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 145 | { |
| 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; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 153 | if (key_buffer_size != 32 || signature_size != 64) { |
| 154 | return status; |
| 155 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 156 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 157 | status = p256_to_psa_error( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 158 | p256_ecdsa_sign(signature, key_buffer, hash, hash_length)); |
| 159 | if (status == PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 160 | *signature_length = 64; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 161 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 162 | |
| 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 Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 168 | static psa_status_t p256_verify_hash_with_public_key( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 169 | 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 Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 174 | size_t signature_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 175 | { |
| 176 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 177 | if (key_buffer_size != 65 || signature_length != 64 || *key_buffer != 0x04) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 178 | return status; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 179 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 180 | |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame^] | 181 | /* 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 Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 184 | const uint8_t *public_key_buffer = key_buffer + 1; |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 185 | status = p256_to_psa_error( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 186 | p256_ecdsa_verify(signature, public_key_buffer, hash, hash_length)); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 187 | |
| 188 | return status; |
| 189 | } |
| 190 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 191 | psa_status_t p256_transparent_verify_hash( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 192 | 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 Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 199 | size_t signature_length) |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 200 | { |
| 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 Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 206 | uint8_t public_key_buffer[65]; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 207 | size_t public_key_buffer_size = 65; |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 208 | |
| 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 Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 214 | |
Aditya Deshpande | 641cb89 | 2023-04-19 03:31:10 +0100 | [diff] [blame^] | 215 | /* 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 Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 219 | status = psa_driver_wrapper_export_public_key( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 220 | attributes, |
| 221 | key_buffer, |
| 222 | key_buffer_size, |
| 223 | public_key_buffer, |
| 224 | public_key_buffer_size, |
Aditya Deshpande | 7b9934d | 2023-04-18 17:00:17 +0100 | [diff] [blame] | 225 | public_key_length_ptr); |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 226 | if (status != PSA_SUCCESS) { |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 227 | goto exit; |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 228 | } |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 229 | |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 230 | status = p256_verify_hash_with_public_key( |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 231 | public_key_buffer, |
| 232 | public_key_buffer_size, |
| 233 | hash, |
| 234 | hash_length, |
| 235 | signature, |
| 236 | signature_length); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 237 | |
| 238 | exit: |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 239 | return status; |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | #endif /* MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED */ |