Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA FFDH layer on top of Mbed TLS crypto |
| 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 | |
| 21 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
| 25 | #include <psa/crypto.h> |
| 26 | #include "psa_crypto_core.h" |
| 27 | #include "psa_crypto_ffdh.h" |
| 28 | #include "psa_crypto_random_impl.h" |
| 29 | |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \ |
| 31 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 32 | static psa_status_t mbedtls_psa_ffdh_set_prime_generator(size_t key_size, |
| 33 | mbedtls_mpi *P, |
| 34 | mbedtls_mpi *G) |
| 35 | { |
| 36 | const unsigned char *dhm_P = NULL; |
| 37 | const unsigned char *dhm_G = NULL; |
| 38 | size_t dhm_size_P = 0; |
| 39 | size_t dhm_size_G = 0; |
| 40 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 41 | |
| 42 | if (P == NULL && G == NULL) { |
| 43 | return PSA_ERROR_INVALID_ARGUMENT; |
| 44 | } |
| 45 | |
| 46 | static const unsigned char dhm_P_2048[] = |
| 47 | MBEDTLS_DHM_RFC7919_FFDHE2048_P_BIN; |
| 48 | static const unsigned char dhm_P_3072[] = |
| 49 | MBEDTLS_DHM_RFC7919_FFDHE3072_P_BIN; |
| 50 | static const unsigned char dhm_P_4096[] = |
| 51 | MBEDTLS_DHM_RFC7919_FFDHE4096_P_BIN; |
| 52 | static const unsigned char dhm_P_6144[] = |
| 53 | MBEDTLS_DHM_RFC7919_FFDHE6144_P_BIN; |
| 54 | static const unsigned char dhm_P_8192[] = |
| 55 | MBEDTLS_DHM_RFC7919_FFDHE8192_P_BIN; |
| 56 | static const unsigned char dhm_G_2048[] = |
| 57 | MBEDTLS_DHM_RFC7919_FFDHE2048_G_BIN; |
| 58 | static const unsigned char dhm_G_3072[] = |
| 59 | MBEDTLS_DHM_RFC7919_FFDHE3072_G_BIN; |
| 60 | static const unsigned char dhm_G_4096[] = |
| 61 | MBEDTLS_DHM_RFC7919_FFDHE4096_G_BIN; |
| 62 | static const unsigned char dhm_G_6144[] = |
| 63 | MBEDTLS_DHM_RFC7919_FFDHE6144_G_BIN; |
| 64 | static const unsigned char dhm_G_8192[] = |
| 65 | MBEDTLS_DHM_RFC7919_FFDHE8192_G_BIN; |
| 66 | |
Przemek Stekiel | 75095cc | 2023-04-28 14:20:27 +0200 | [diff] [blame] | 67 | switch(key_size) { |
| 68 | case sizeof(dhm_P_2048): |
| 69 | dhm_P = dhm_P_2048; |
| 70 | dhm_G = dhm_G_2048; |
| 71 | dhm_size_P = sizeof(dhm_P_2048); |
| 72 | dhm_size_G = sizeof(dhm_G_2048); |
| 73 | break; |
| 74 | case sizeof(dhm_P_3072): |
| 75 | dhm_P = dhm_P_3072; |
| 76 | dhm_G = dhm_G_3072; |
| 77 | dhm_size_P = sizeof(dhm_P_3072); |
| 78 | dhm_size_G = sizeof(dhm_G_3072); |
| 79 | break; |
| 80 | case sizeof(dhm_P_4096): |
| 81 | dhm_P = dhm_P_4096; |
| 82 | dhm_G = dhm_G_4096; |
| 83 | dhm_size_P = sizeof(dhm_P_4096); |
| 84 | dhm_size_G = sizeof(dhm_G_4096); |
| 85 | break; |
| 86 | case sizeof(dhm_P_6144): |
| 87 | dhm_P = dhm_P_6144; |
| 88 | dhm_G = dhm_G_6144; |
| 89 | dhm_size_P = sizeof(dhm_P_6144); |
| 90 | dhm_size_G = sizeof(dhm_G_6144); |
| 91 | break; |
| 92 | case sizeof(dhm_P_8192): |
| 93 | dhm_P = dhm_P_8192; |
| 94 | dhm_G = dhm_G_8192; |
| 95 | dhm_size_P = sizeof(dhm_P_8192); |
| 96 | dhm_size_G = sizeof(dhm_G_8192); |
| 97 | break; |
| 98 | default: |
| 99 | return PSA_ERROR_INVALID_ARGUMENT; |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | if (P != NULL) { |
| 103 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(P, dhm_P, |
| 104 | dhm_size_P)); |
| 105 | } |
| 106 | if (G != NULL) { |
| 107 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(G, dhm_G, |
| 108 | dhm_size_G)); |
| 109 | } |
| 110 | |
| 111 | cleanup: |
| 112 | if (ret != 0) { |
| 113 | return mbedtls_to_psa_error(ret); |
| 114 | } |
| 115 | |
| 116 | return PSA_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH) |
| 120 | psa_status_t mbedtls_psa_key_agreement_ffdh( |
| 121 | const psa_key_attributes_t *attributes, |
| 122 | const uint8_t *peer_key, |
| 123 | size_t peer_key_length, |
| 124 | const uint8_t *key_buffer, |
| 125 | size_t key_buffer_size, |
| 126 | uint8_t *shared_secret, |
| 127 | size_t shared_secret_size, |
| 128 | size_t *shared_secret_length) |
| 129 | { |
| 130 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 131 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 132 | mbedtls_mpi P, G, X, GY, K; |
| 133 | const size_t calculated_shared_secret_size = peer_key_length; |
| 134 | |
| 135 | if (peer_key_length != key_buffer_size || |
| 136 | calculated_shared_secret_size > shared_secret_size) { |
| 137 | return PSA_ERROR_INVALID_ARGUMENT; |
| 138 | } |
| 139 | |
Przemek Stekiel | a9ca131 | 2022-12-05 15:02:32 +0100 | [diff] [blame] | 140 | if (!PSA_KEY_TYPE_IS_DH_KEY_PAIR(psa_get_key_type(attributes))) { |
| 141 | return PSA_ERROR_INVALID_ARGUMENT; |
| 142 | } |
| 143 | |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 144 | mbedtls_mpi_init(&P); mbedtls_mpi_init(&G); |
| 145 | mbedtls_mpi_init(&X); mbedtls_mpi_init(&GY); |
| 146 | mbedtls_mpi_init(&K); |
| 147 | |
| 148 | status = mbedtls_psa_ffdh_set_prime_generator( |
| 149 | PSA_BITS_TO_BYTES(attributes->core.bits), &P, &G); |
| 150 | |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 151 | if(status != PSA_SUCCESS) { |
| 152 | goto cleanup; |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 153 | } |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 154 | |
| 155 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer, |
| 156 | key_buffer_size)); |
| 157 | |
| 158 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&GY, peer_key, |
| 159 | peer_key_length)); |
| 160 | |
| 161 | /* Calculate shared secret public key: K = G^(XY) mod P = GY^X mod P */ |
| 162 | MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &GY, &X, &P, NULL)); |
| 163 | |
| 164 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&K, shared_secret, |
| 165 | calculated_shared_secret_size)); |
| 166 | |
| 167 | *shared_secret_length = calculated_shared_secret_size; |
| 168 | |
| 169 | ret = 0; |
| 170 | |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 171 | cleanup: |
| 172 | mbedtls_mpi_free(&P); mbedtls_mpi_free(&G); |
| 173 | mbedtls_mpi_free(&X); mbedtls_mpi_free(&GY); |
| 174 | mbedtls_mpi_free(&K); |
| 175 | |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 176 | if(status == PSA_SUCCESS && ret != 0) { |
| 177 | status = mbedtls_to_psa_error(ret); |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 180 | return status; |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 181 | } |
| 182 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */ |
| 183 | |
| 184 | psa_status_t mbedtls_psa_export_ffdh_public_key( |
| 185 | const psa_key_attributes_t *attributes, |
| 186 | const uint8_t *key_buffer, |
| 187 | size_t key_buffer_size, |
| 188 | uint8_t *data, |
| 189 | size_t data_size, |
| 190 | size_t *data_length) |
| 191 | { |
| 192 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 193 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 194 | mbedtls_mpi GX, G, X, P; |
| 195 | |
| 196 | mbedtls_mpi_init(&GX); mbedtls_mpi_init(&G); |
| 197 | mbedtls_mpi_init(&X); mbedtls_mpi_init(&P); |
| 198 | |
| 199 | status = mbedtls_psa_ffdh_set_prime_generator( |
| 200 | PSA_BITS_TO_BYTES(attributes->core.bits), &P, &G); |
| 201 | |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 202 | if(status != PSA_SUCCESS) { |
| 203 | goto cleanup; |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 204 | } |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 205 | |
| 206 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer, |
| 207 | key_buffer_size)); |
| 208 | |
| 209 | MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&GX, &G, &X, &P, NULL)); |
| 210 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&GX, data, data_size)); |
| 211 | |
| 212 | *data_length = mbedtls_mpi_size(&GX); |
| 213 | |
| 214 | ret = 0; |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 215 | cleanup: |
| 216 | mbedtls_mpi_free(&P); mbedtls_mpi_free(&G); |
| 217 | mbedtls_mpi_free(&X); mbedtls_mpi_free(&GX); |
| 218 | |
| 219 | if (status == PSA_SUCCESS && ret != 0) { |
Przemek Stekiel | 6fd72b6 | 2023-04-27 10:20:56 +0200 | [diff] [blame] | 220 | status = mbedtls_to_psa_error(ret); |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return status; |
| 224 | } |
| 225 | |
| 226 | psa_status_t mbedtls_psa_ffdh_generate_key( |
| 227 | const psa_key_attributes_t *attributes, |
| 228 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length) |
| 229 | { |
| 230 | mbedtls_mpi X, P; |
| 231 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 232 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 233 | mbedtls_mpi_init(&P); mbedtls_mpi_init(&X); |
Przemek Stekiel | e1621a4 | 2023-05-05 09:53:37 +0200 | [diff] [blame^] | 234 | (void) attributes; |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 235 | |
Przemek Stekiel | e1621a4 | 2023-05-05 09:53:37 +0200 | [diff] [blame^] | 236 | status = mbedtls_psa_ffdh_set_prime_generator(key_buffer_size, &P, NULL); |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 237 | |
Przemek Stekiel | e1621a4 | 2023-05-05 09:53:37 +0200 | [diff] [blame^] | 238 | if (status != PSA_SUCCESS) { |
| 239 | goto cleanup; |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Przemek Stekiel | e1621a4 | 2023-05-05 09:53:37 +0200 | [diff] [blame^] | 242 | /* RFC7919: Traditional finite field Diffie-Hellman has each peer choose their |
| 243 | secret exponent from the range [2, P-2]. |
| 244 | Select random value in range [3, P-1] and decrease it by 1. */ |
| 245 | MBEDTLS_MPI_CHK(mbedtls_mpi_random(&X, 3, &P, mbedtls_psa_get_random, |
| 246 | MBEDTLS_PSA_RANDOM_STATE)); |
| 247 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&X, &X, 1)); |
| 248 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&X, key_buffer, key_buffer_size)); |
| 249 | *key_buffer_length = key_buffer_size; |
| 250 | |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 251 | cleanup: |
| 252 | mbedtls_mpi_free(&P); mbedtls_mpi_free(&X); |
| 253 | if (status == PSA_SUCCESS && ret != 0) { |
| 254 | return mbedtls_to_psa_error(ret); |
| 255 | } |
| 256 | |
| 257 | return status; |
| 258 | } |
Przemek Stekiel | 6d85afa | 2023-04-28 11:42:17 +0200 | [diff] [blame] | 259 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR || |
| 260 | MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY */ |
Przemek Stekiel | 359f462 | 2022-12-05 14:11:55 +0100 | [diff] [blame] | 261 | |
| 262 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |