blob: aba62eef3c3f12d3ce0168a91d53fd833e2fadbe [file] [log] [blame]
Przemek Stekiel359f4622022-12-05 14:11:55 +01001/*
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 Stekiel6d85afa2023-04-28 11:42:17 +020030#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
31 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel359f4622022-12-05 14:11:55 +010032static 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 Stekiel9275d5d2023-04-27 10:32:29 +020067 if (key_size == sizeof(dhm_P_2048)) {
Przemek Stekiel359f4622022-12-05 14:11:55 +010068 dhm_P = dhm_P_2048;
69 dhm_G = dhm_G_2048;
70 dhm_size_P = sizeof(dhm_P_2048);
71 dhm_size_G = sizeof(dhm_G_2048);
Przemek Stekiel9275d5d2023-04-27 10:32:29 +020072 } else if (key_size == sizeof(dhm_P_3072)) {
Przemek Stekiel359f4622022-12-05 14:11:55 +010073 dhm_P = dhm_P_3072;
74 dhm_G = dhm_G_3072;
75 dhm_size_P = sizeof(dhm_P_3072);
76 dhm_size_G = sizeof(dhm_G_3072);
Przemek Stekiel9275d5d2023-04-27 10:32:29 +020077 } else if (key_size == sizeof(dhm_P_4096)) {
Przemek Stekiel359f4622022-12-05 14:11:55 +010078 dhm_P = dhm_P_4096;
79 dhm_G = dhm_G_4096;
80 dhm_size_P = sizeof(dhm_P_4096);
81 dhm_size_G = sizeof(dhm_G_4096);
Przemek Stekiel9275d5d2023-04-27 10:32:29 +020082 } else if (key_size == sizeof(dhm_P_6144)) {
Przemek Stekiel359f4622022-12-05 14:11:55 +010083 dhm_P = dhm_P_6144;
84 dhm_G = dhm_G_6144;
85 dhm_size_P = sizeof(dhm_P_6144);
86 dhm_size_G = sizeof(dhm_G_6144);
Przemek Stekiel9275d5d2023-04-27 10:32:29 +020087 } else if (key_size == sizeof(dhm_P_8192)) {
Przemek Stekiel359f4622022-12-05 14:11:55 +010088 dhm_P = dhm_P_8192;
89 dhm_G = dhm_G_8192;
90 dhm_size_P = sizeof(dhm_P_8192);
91 dhm_size_G = sizeof(dhm_G_8192);
92 } else {
93 return PSA_ERROR_INVALID_ARGUMENT;
94 }
95
96 if (P != NULL) {
97 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(P, dhm_P,
98 dhm_size_P));
99 }
100 if (G != NULL) {
101 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(G, dhm_G,
102 dhm_size_G));
103 }
104
105cleanup:
106 if (ret != 0) {
107 return mbedtls_to_psa_error(ret);
108 }
109
110 return PSA_SUCCESS;
111}
112
113#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
114psa_status_t mbedtls_psa_key_agreement_ffdh(
115 const psa_key_attributes_t *attributes,
116 const uint8_t *peer_key,
117 size_t peer_key_length,
118 const uint8_t *key_buffer,
119 size_t key_buffer_size,
120 uint8_t *shared_secret,
121 size_t shared_secret_size,
122 size_t *shared_secret_length)
123{
124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
125 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
126 mbedtls_mpi P, G, X, GY, K;
127 const size_t calculated_shared_secret_size = peer_key_length;
128
129 if (peer_key_length != key_buffer_size ||
130 calculated_shared_secret_size > shared_secret_size) {
131 return PSA_ERROR_INVALID_ARGUMENT;
132 }
133
Przemek Stekiela9ca1312022-12-05 15:02:32 +0100134 if (!PSA_KEY_TYPE_IS_DH_KEY_PAIR(psa_get_key_type(attributes))) {
135 return PSA_ERROR_INVALID_ARGUMENT;
136 }
137
Przemek Stekiel359f4622022-12-05 14:11:55 +0100138 mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
139 mbedtls_mpi_init(&X); mbedtls_mpi_init(&GY);
140 mbedtls_mpi_init(&K);
141
142 status = mbedtls_psa_ffdh_set_prime_generator(
143 PSA_BITS_TO_BYTES(attributes->core.bits), &P, &G);
144
Przemek Stekiel6fd72b62023-04-27 10:20:56 +0200145 if(status != PSA_SUCCESS) {
146 goto cleanup;
Przemek Stekiel359f4622022-12-05 14:11:55 +0100147 }
Przemek Stekiel6fd72b62023-04-27 10:20:56 +0200148
149 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer,
150 key_buffer_size));
151
152 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&GY, peer_key,
153 peer_key_length));
154
155 /* Calculate shared secret public key: K = G^(XY) mod P = GY^X mod P */
156 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &GY, &X, &P, NULL));
157
158 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&K, shared_secret,
159 calculated_shared_secret_size));
160
161 *shared_secret_length = calculated_shared_secret_size;
162
163 ret = 0;
164
Przemek Stekiel359f4622022-12-05 14:11:55 +0100165cleanup:
166 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
167 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GY);
168 mbedtls_mpi_free(&K);
169
Przemek Stekiel6fd72b62023-04-27 10:20:56 +0200170 if(status == PSA_SUCCESS && ret != 0) {
171 status = mbedtls_to_psa_error(ret);
Przemek Stekiel359f4622022-12-05 14:11:55 +0100172 }
173
Przemek Stekiel6fd72b62023-04-27 10:20:56 +0200174 return status;
Przemek Stekiel359f4622022-12-05 14:11:55 +0100175}
176#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
177
178psa_status_t mbedtls_psa_export_ffdh_public_key(
179 const psa_key_attributes_t *attributes,
180 const uint8_t *key_buffer,
181 size_t key_buffer_size,
182 uint8_t *data,
183 size_t data_size,
184 size_t *data_length)
185{
186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
188 mbedtls_mpi GX, G, X, P;
189
190 mbedtls_mpi_init(&GX); mbedtls_mpi_init(&G);
191 mbedtls_mpi_init(&X); mbedtls_mpi_init(&P);
192
193 status = mbedtls_psa_ffdh_set_prime_generator(
194 PSA_BITS_TO_BYTES(attributes->core.bits), &P, &G);
195
Przemek Stekiel6fd72b62023-04-27 10:20:56 +0200196 if(status != PSA_SUCCESS) {
197 goto cleanup;
Przemek Stekiel359f4622022-12-05 14:11:55 +0100198 }
Przemek Stekiel6fd72b62023-04-27 10:20:56 +0200199
200 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer,
201 key_buffer_size));
202
203 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&GX, &G, &X, &P, NULL));
204 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&GX, data, data_size));
205
206 *data_length = mbedtls_mpi_size(&GX);
207
208 ret = 0;
Przemek Stekiel359f4622022-12-05 14:11:55 +0100209cleanup:
210 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
211 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GX);
212
213 if (status == PSA_SUCCESS && ret != 0) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +0200214 status = mbedtls_to_psa_error(ret);
Przemek Stekiel359f4622022-12-05 14:11:55 +0100215 }
216
217 return status;
218}
219
220psa_status_t mbedtls_psa_ffdh_generate_key(
221 const psa_key_attributes_t *attributes,
222 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
223{
224 mbedtls_mpi X, P;
225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
227 mbedtls_mpi_init(&P); mbedtls_mpi_init(&X);
228
229 status = mbedtls_psa_ffdh_set_prime_generator(
230 PSA_BITS_TO_BYTES(attributes->core.bits), &P, NULL);
231
232 if (status == PSA_SUCCESS) {
Przemek Stekiel654bef02022-12-15 13:28:02 +0100233 /* RFC7919: Traditional finite field Diffie-Hellman has each peer choose their
234 secret exponent from the range [2, P-2].
Przemek Stekielcf0156f2023-04-27 11:12:39 +0200235 Select random value in range [3, P-1] and decrease it by 1. */
236 MBEDTLS_MPI_CHK(mbedtls_mpi_random(&X, 3, &P, mbedtls_psa_get_random,
Przemek Stekiel359f4622022-12-05 14:11:55 +0100237 MBEDTLS_PSA_RANDOM_STATE));
Przemek Stekielcf0156f2023-04-27 11:12:39 +0200238 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&X, &X, 1));
Przemek Stekiel359f4622022-12-05 14:11:55 +0100239
240 *key_buffer_length = mbedtls_mpi_size(&X);
241
242 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&X, key_buffer,
243 key_buffer_size));
244 }
245
246cleanup:
247 mbedtls_mpi_free(&P); mbedtls_mpi_free(&X);
248 if (status == PSA_SUCCESS && ret != 0) {
249 return mbedtls_to_psa_error(ret);
250 }
251
252 return status;
253}
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200254#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR ||
255 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY */
Przemek Stekiel359f4622022-12-05 14:11:55 +0100256
257#endif /* MBEDTLS_PSA_CRYPTO_C */