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 OR GPL-2.0-or-later
7 */
8
9 #include "common.h"
10
11 #if defined(MBEDTLS_PSA_CRYPTO_C)
12
13 #include <psa/crypto.h>
14 #include "psa_crypto_core.h"
15 #include "psa_crypto_ffdh.h"
16 #include "psa_crypto_random_impl.h"
17 #include "mbedtls/platform.h"
18 #include "mbedtls/error.h"
19
20 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
21 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) || \
22 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
23 defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
mbedtls_psa_ffdh_set_prime_generator(size_t key_size,mbedtls_mpi * P,mbedtls_mpi * G)24 static psa_status_t mbedtls_psa_ffdh_set_prime_generator(size_t key_size,
25 mbedtls_mpi *P,
26 mbedtls_mpi *G)
27 {
28 const unsigned char *dhm_P = NULL;
29 const unsigned char *dhm_G = NULL;
30 size_t dhm_size_P = 0;
31 size_t dhm_size_G = 0;
32 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
33
34 if (P == NULL && G == NULL) {
35 return PSA_ERROR_INVALID_ARGUMENT;
36 }
37
38 static const unsigned char dhm_P_2048[] =
39 MBEDTLS_DHM_RFC7919_FFDHE2048_P_BIN;
40 static const unsigned char dhm_P_3072[] =
41 MBEDTLS_DHM_RFC7919_FFDHE3072_P_BIN;
42 static const unsigned char dhm_P_4096[] =
43 MBEDTLS_DHM_RFC7919_FFDHE4096_P_BIN;
44 static const unsigned char dhm_P_6144[] =
45 MBEDTLS_DHM_RFC7919_FFDHE6144_P_BIN;
46 static const unsigned char dhm_P_8192[] =
47 MBEDTLS_DHM_RFC7919_FFDHE8192_P_BIN;
48 static const unsigned char dhm_G_2048[] =
49 MBEDTLS_DHM_RFC7919_FFDHE2048_G_BIN;
50 static const unsigned char dhm_G_3072[] =
51 MBEDTLS_DHM_RFC7919_FFDHE3072_G_BIN;
52 static const unsigned char dhm_G_4096[] =
53 MBEDTLS_DHM_RFC7919_FFDHE4096_G_BIN;
54 static const unsigned char dhm_G_6144[] =
55 MBEDTLS_DHM_RFC7919_FFDHE6144_G_BIN;
56 static const unsigned char dhm_G_8192[] =
57 MBEDTLS_DHM_RFC7919_FFDHE8192_G_BIN;
58
59 switch (key_size) {
60 case sizeof(dhm_P_2048):
61 dhm_P = dhm_P_2048;
62 dhm_G = dhm_G_2048;
63 dhm_size_P = sizeof(dhm_P_2048);
64 dhm_size_G = sizeof(dhm_G_2048);
65 break;
66 case sizeof(dhm_P_3072):
67 dhm_P = dhm_P_3072;
68 dhm_G = dhm_G_3072;
69 dhm_size_P = sizeof(dhm_P_3072);
70 dhm_size_G = sizeof(dhm_G_3072);
71 break;
72 case sizeof(dhm_P_4096):
73 dhm_P = dhm_P_4096;
74 dhm_G = dhm_G_4096;
75 dhm_size_P = sizeof(dhm_P_4096);
76 dhm_size_G = sizeof(dhm_G_4096);
77 break;
78 case sizeof(dhm_P_6144):
79 dhm_P = dhm_P_6144;
80 dhm_G = dhm_G_6144;
81 dhm_size_P = sizeof(dhm_P_6144);
82 dhm_size_G = sizeof(dhm_G_6144);
83 break;
84 case sizeof(dhm_P_8192):
85 dhm_P = dhm_P_8192;
86 dhm_G = dhm_G_8192;
87 dhm_size_P = sizeof(dhm_P_8192);
88 dhm_size_G = sizeof(dhm_G_8192);
89 break;
90 default:
91 return PSA_ERROR_INVALID_ARGUMENT;
92 }
93
94 if (P != NULL) {
95 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(P, dhm_P,
96 dhm_size_P));
97 }
98 if (G != NULL) {
99 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(G, dhm_G,
100 dhm_size_G));
101 }
102
103 cleanup:
104 if (ret != 0) {
105 return mbedtls_to_psa_error(ret);
106 }
107
108 return PSA_SUCCESS;
109 }
110 #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT ||
111 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE ||
112 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
113 MBEDTLS_PSA_BUILTIN_ALG_FFDH */
114
115 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
116 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
mbedtls_psa_ffdh_export_public_key(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,uint8_t * data,size_t data_size,size_t * data_length)117 psa_status_t mbedtls_psa_ffdh_export_public_key(
118 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 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
127 mbedtls_mpi GX, G, X, P;
128 psa_key_type_t type = attributes->core.type;
129
130 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
131 if (key_buffer_size > data_size) {
132 return PSA_ERROR_BUFFER_TOO_SMALL;
133 }
134 memcpy(data, key_buffer, key_buffer_size);
135 memset(data + key_buffer_size, 0,
136 data_size - key_buffer_size);
137 *data_length = key_buffer_size;
138 return PSA_SUCCESS;
139 }
140
141 mbedtls_mpi_init(&GX); mbedtls_mpi_init(&G);
142 mbedtls_mpi_init(&X); mbedtls_mpi_init(&P);
143
144 size_t key_len = PSA_BITS_TO_BYTES(attributes->core.bits);
145
146 status = mbedtls_psa_ffdh_set_prime_generator(key_len, &P, &G);
147
148 if (status != PSA_SUCCESS) {
149 goto cleanup;
150 }
151
152 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer,
153 key_buffer_size));
154
155 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&GX, &G, &X, &P, NULL));
156 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&GX, data, key_len));
157
158 *data_length = key_len;
159
160 ret = 0;
161 cleanup:
162 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
163 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GX);
164
165 if (status == PSA_SUCCESS && ret != 0) {
166 status = mbedtls_to_psa_error(ret);
167 }
168
169 return status;
170 }
171 #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT ||
172 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY */
173
174 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
mbedtls_psa_ffdh_generate_key(const psa_key_attributes_t * attributes,uint8_t * key_buffer,size_t key_buffer_size,size_t * key_buffer_length)175 psa_status_t mbedtls_psa_ffdh_generate_key(
176 const psa_key_attributes_t *attributes,
177 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
178 {
179 mbedtls_mpi X, P;
180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
182 mbedtls_mpi_init(&P); mbedtls_mpi_init(&X);
183 (void) attributes;
184
185 status = mbedtls_psa_ffdh_set_prime_generator(key_buffer_size, &P, NULL);
186
187 if (status != PSA_SUCCESS) {
188 goto cleanup;
189 }
190
191 /* RFC7919: Traditional finite field Diffie-Hellman has each peer choose their
192 secret exponent from the range [2, P-2].
193 Select random value in range [3, P-1] and decrease it by 1. */
194 MBEDTLS_MPI_CHK(mbedtls_mpi_random(&X, 3, &P, mbedtls_psa_get_random,
195 MBEDTLS_PSA_RANDOM_STATE));
196 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&X, &X, 1));
197 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&X, key_buffer, key_buffer_size));
198 *key_buffer_length = key_buffer_size;
199
200 cleanup:
201 mbedtls_mpi_free(&P); mbedtls_mpi_free(&X);
202 if (status == PSA_SUCCESS && ret != 0) {
203 return mbedtls_to_psa_error(ret);
204 }
205
206 return status;
207 }
208 #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE */
209
210 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT)
mbedtls_psa_ffdh_import_key(const psa_key_attributes_t * attributes,const uint8_t * data,size_t data_length,uint8_t * key_buffer,size_t key_buffer_size,size_t * key_buffer_length,size_t * bits)211 psa_status_t mbedtls_psa_ffdh_import_key(
212 const psa_key_attributes_t *attributes,
213 const uint8_t *data, size_t data_length,
214 uint8_t *key_buffer, size_t key_buffer_size,
215 size_t *key_buffer_length, size_t *bits)
216 {
217 (void) attributes;
218
219 if (key_buffer_size < data_length) {
220 return PSA_ERROR_BUFFER_TOO_SMALL;
221 }
222 memcpy(key_buffer, data, data_length);
223 *key_buffer_length = data_length;
224 *bits = PSA_BYTES_TO_BITS(data_length);
225
226 return PSA_SUCCESS;
227 }
228 #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT */
229
230 #if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
mbedtls_psa_ffdh_key_agreement(const psa_key_attributes_t * attributes,const uint8_t * peer_key,size_t peer_key_length,const uint8_t * key_buffer,size_t key_buffer_size,uint8_t * shared_secret,size_t shared_secret_size,size_t * shared_secret_length)231 psa_status_t mbedtls_psa_ffdh_key_agreement(
232 const psa_key_attributes_t *attributes,
233 const uint8_t *peer_key,
234 size_t peer_key_length,
235 const uint8_t *key_buffer,
236 size_t key_buffer_size,
237 uint8_t *shared_secret,
238 size_t shared_secret_size,
239 size_t *shared_secret_length)
240 {
241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
243 mbedtls_mpi P, G, X, GY, K;
244 const size_t calculated_shared_secret_size = peer_key_length;
245
246 if (peer_key_length != key_buffer_size ||
247 calculated_shared_secret_size > shared_secret_size) {
248 return PSA_ERROR_INVALID_ARGUMENT;
249 }
250
251 if (!PSA_KEY_TYPE_IS_DH_KEY_PAIR(psa_get_key_type(attributes))) {
252 return PSA_ERROR_INVALID_ARGUMENT;
253 }
254
255 mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
256 mbedtls_mpi_init(&X); mbedtls_mpi_init(&GY);
257 mbedtls_mpi_init(&K);
258
259 status = mbedtls_psa_ffdh_set_prime_generator(
260 PSA_BITS_TO_BYTES(attributes->core.bits), &P, &G);
261
262 if (status != PSA_SUCCESS) {
263 goto cleanup;
264 }
265
266 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer,
267 key_buffer_size));
268
269 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&GY, peer_key,
270 peer_key_length));
271
272 /* Calculate shared secret public key: K = G^(XY) mod P = GY^X mod P */
273 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &GY, &X, &P, NULL));
274
275 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&K, shared_secret,
276 calculated_shared_secret_size));
277
278 *shared_secret_length = calculated_shared_secret_size;
279
280 ret = 0;
281
282 cleanup:
283 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
284 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GY);
285 mbedtls_mpi_free(&K);
286
287 if (status == PSA_SUCCESS && ret != 0) {
288 status = mbedtls_to_psa_error(ret);
289 }
290
291 return status;
292 }
293 #endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
294
295 #endif /* MBEDTLS_PSA_CRYPTO_C */
296