OpenVPN
crypto_mbedtls.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2026 Sentyron B.V. <openvpn@sentyron.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include "syshead.h"
35
36#if defined(ENABLE_CRYPTO_MBEDTLS)
37#include <mbedtls/version.h>
38
39#if MBEDTLS_VERSION_NUMBER >= 0x04000000
40
41#include "errlevel.h"
42#include "basic.h"
43#include "buffer.h"
44#include "crypto.h"
45#include "integer.h"
46#include "crypto_backend.h"
47#include "crypto_mbedtls.h"
48#include "otime.h"
49#include "misc.h"
50
51#include <psa/crypto.h>
52#include <psa/crypto_config.h>
53#include <mbedtls/constant_time.h>
54#include <mbedtls/error.h>
55#include <mbedtls/pem.h>
56
57/*
58 *
59 * Hardware engine support. Allows loading/unloading of engines.
60 *
61 */
62
63void
64crypto_init_lib_engine(const char *engine_name)
65{
66 msg(M_WARN, "Note: mbed TLS hardware crypto engine functionality is not "
67 "available");
68}
69
71crypto_load_provider(const char *provider)
72{
73 if (provider)
74 {
75 msg(M_WARN, "Note: mbed TLS provider functionality is not available");
76 }
77 return NULL;
78}
79
80void
81crypto_unload_provider(const char *provname, provider_t *provider)
82{
83}
84
85/* The library doesn't support looking up algorithms by string anymore, so here
86 * is a lookup table. */
87static const cipher_info_t cipher_info_table[] = {
88/* TODO: Complete the table. */
89
90/* AES */
91#if PSA_WANT_KEY_TYPE_AES
92#if PSA_WANT_ALG_GCM
93 { "AES-128-GCM", PSA_KEY_TYPE_AES, PSA_ALG_GCM, 128 / 8, 96 / 8, 128 / 8 },
94 { "AES-192-GCM", PSA_KEY_TYPE_AES, PSA_ALG_GCM, 192 / 8, 96 / 8, 128 / 8 },
95 { "AES-256-GCM", PSA_KEY_TYPE_AES, PSA_ALG_GCM, 256 / 8, 96 / 8, 128 / 8 },
96#endif /* PSA_WANT_ALG_GCM */
97#if PSA_WANT_ALG_CBC_PKCS7
98 { "AES-128-CBC", PSA_KEY_TYPE_AES, PSA_ALG_CBC_PKCS7, 128 / 8, 128 / 8, 128 / 8 },
99 { "AES-192-CBC", PSA_KEY_TYPE_AES, PSA_ALG_CBC_PKCS7, 192 / 8, 128 / 8, 128 / 8 },
100 { "AES-256-CBC", PSA_KEY_TYPE_AES, PSA_ALG_CBC_PKCS7, 256 / 8, 128 / 8, 128 / 8 },
101#endif /* PSA_WANT_ALG_CBC_PKCS7 */
102#if PSA_WANT_ALG_CTR
103 { "AES-128-CTR", PSA_KEY_TYPE_AES, PSA_ALG_CTR, 128 / 8, 128 / 8, 128 / 8 },
104 { "AES-192-CTR", PSA_KEY_TYPE_AES, PSA_ALG_CTR, 192 / 8, 128 / 8, 128 / 8 },
105 { "AES-256-CTR", PSA_KEY_TYPE_AES, PSA_ALG_CTR, 256 / 8, 128 / 8, 128 / 8 },
106#endif /* PSA_WANT_ALG_CTR */
107#endif /* PSA_WANT_KEY_TYPE_AES */
108
109/* Chacha-Poly */
110#if PSA_WANT_KEY_TYPE_CHACHA20 && PSA_WANT_ALG_CHACHA20_POLY1305
111 { "CHACHA20-POLY1305", PSA_KEY_TYPE_CHACHA20, PSA_ALG_CHACHA20_POLY1305, 256 / 8, 96 / 8, 1 },
112#endif
113};
114static const size_t cipher_info_table_entries = sizeof(cipher_info_table) / sizeof(cipher_info_t);
115
116static const cipher_info_t *
117cipher_get(const char *ciphername)
118{
119 for (size_t i = 0; i < cipher_info_table_entries; i++)
120 {
121 if (strcmp(ciphername, cipher_info_table[i].name) == 0)
122 {
123 return &cipher_info_table[i];
124 }
125 }
126 return NULL;
127}
128
129/* Because Mbed TLS 4 doesn't support looking up algorithms by string, there's
130 * nothing to translate. */
134
135int
136rand_bytes(uint8_t *output, int len)
137{
138 if (len < 0)
139 {
140 return 0;
141 }
142 psa_status_t result = psa_generate_random(output, (size_t)len);
143 return result == PSA_SUCCESS;
144}
145
146bool
147cipher_valid_reason(const char *ciphername, const char **reason)
148{
149 ASSERT(reason);
150
151 const cipher_info_t *cipher_info = cipher_get(ciphername);
152
153 if (cipher_info == NULL)
154 {
155 msg(D_LOW, "Cipher algorithm '%s' not found", ciphername);
156 *reason = "disabled because unknown";
157 return false;
158 }
159
161 {
162 msg(D_LOW,
163 "Cipher algorithm '%s' uses a default key size (%d bytes) "
164 "which is larger than " PACKAGE_NAME "'s current maximum key size "
165 "(%d bytes)",
167 *reason = "disabled due to key size too large";
168 return false;
169 }
170
171 *reason = NULL;
172 return true;
173}
174
175const char *
176cipher_kt_name(const char *ciphername)
177{
178 const cipher_info_t *cipher_info = cipher_get(ciphername);
179 if (cipher_info == NULL)
180 {
181 return "[null-cipher]";
182 }
183 return cipher_info->name;
184}
185
186int
187cipher_kt_key_size(const char *ciphername)
188{
189 const cipher_info_t *cipher_info = cipher_get(ciphername);
190 if (cipher_info == NULL)
191 {
192 return 0;
193 }
194 return cipher_info->key_bytes;
195}
196
197int
198cipher_kt_iv_size(const char *ciphername)
199{
200 const cipher_info_t *cipher_info = cipher_get(ciphername);
201
202 if (cipher_info == NULL)
203 {
204 return 0;
205 }
206 return cipher_info->iv_bytes;
207}
208
209int
210cipher_kt_block_size(const char *ciphername)
211{
212 const cipher_info_t *cipher_info = cipher_get(ciphername);
213 if (cipher_info == NULL)
214 {
215 return 0;
216 }
217 return cipher_info->block_size;
218}
219
220int
221cipher_kt_tag_size(const char *ciphername)
222{
223 if (cipher_kt_mode_aead(ciphername))
224 {
226 }
227 return 0;
228}
229
230bool
231cipher_kt_insecure(const char *ciphername)
232{
233 const cipher_info_t *cipher_info = cipher_get(ciphername);
234 if (cipher_info == NULL)
235 {
236 return true;
237 }
238
239 return !(cipher_info->block_size >= 128 / 8
240 || cipher_info->psa_alg == PSA_ALG_CHACHA20_POLY1305);
241}
242
243bool
244cipher_kt_mode_cbc(const char *ciphername)
245{
246 const cipher_info_t *cipher_info = cipher_get(ciphername);
247 if (cipher_info == NULL)
248 {
249 return false;
250 }
251 return cipher_info->psa_alg == PSA_ALG_CBC_PKCS7;
252}
253
254bool
255cipher_kt_mode_ofb_cfb(const char *ciphername)
256{
257 const cipher_info_t *cipher_info = cipher_get(ciphername);
258 if (cipher_info == NULL)
259 {
260 return false;
261 }
262 return cipher_info->psa_alg == PSA_ALG_OFB || cipher_info->psa_alg == PSA_ALG_CFB;
263}
264
265bool
266cipher_kt_mode_aead(const char *ciphername)
267{
268 const cipher_info_t *cipher_info = cipher_get(ciphername);
269 if (cipher_info == NULL)
270 {
271 return false;
272 }
273 return cipher_info->psa_alg == PSA_ALG_GCM || cipher_info->psa_alg == PSA_ALG_CHACHA20_POLY1305;
274}
275
277cipher_ctx_new(void)
278{
279 cipher_ctx_t *ctx;
280 /* Initializing the object with zeros ensures that it is always safe to call
281 * cipher_ctx_free. */
283 return ctx;
284}
285
286void
288{
289 if (cipher_ctx_mode_aead(ctx))
290 {
291 ASSERT(psa_aead_abort(&ctx->operation.aead) == PSA_SUCCESS);
292 }
293 else
294 {
295 ASSERT(psa_cipher_abort(&ctx->operation.cipher) == PSA_SUCCESS);
296 }
297 ASSERT(psa_destroy_key(ctx->key) == PSA_SUCCESS);
298 free(ctx);
299}
300
301void
302cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphername,
304{
305 ASSERT(ciphername != NULL && ctx != NULL);
306 CLEAR(*ctx);
307
308 ctx->cipher_info = cipher_get(ciphername);
309 ASSERT(ctx->cipher_info != NULL);
310
311 psa_set_key_type(&ctx->key_attributes, ctx->cipher_info->psa_key_type);
312 psa_set_key_algorithm(&ctx->key_attributes, ctx->cipher_info->psa_alg);
313 psa_set_key_bits(&ctx->key_attributes, (size_t)ctx->cipher_info->key_bytes * 8);
314 psa_set_key_usage_flags(&ctx->key_attributes,
315 enc == OPENVPN_OP_ENCRYPT ? PSA_KEY_USAGE_ENCRYPT : PSA_KEY_USAGE_DECRYPT);
316
317 if (psa_import_key(&ctx->key_attributes, key, (size_t)ctx->cipher_info->key_bytes, &ctx->key) != PSA_SUCCESS)
318 {
319 msg(M_FATAL, "psa_import_key failed");
320 }
321
322 /* make sure we used a big enough key */
323 ASSERT(psa_get_key_bits(&ctx->key_attributes) == (size_t)(8 * ctx->cipher_info->key_bytes));
324}
325
326int
328{
329 return ctx->cipher_info->iv_bytes;
330}
331
332int
333cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len)
334{
335 if (!ctx->aead_finished || tag_len < OPENVPN_AEAD_TAG_LENGTH)
336 {
337 return 0;
338 }
339
340 memcpy(tag, ctx->tag, OPENVPN_AEAD_TAG_LENGTH);
341 return 1;
342}
343
344int
346{
347 return ctx->cipher_info->block_size;
348}
349
350int
352{
353 ASSERT(ctx != NULL);
354 return (int)psa_get_key_algorithm(&ctx->key_attributes);
355}
356
357bool
359{
360 return ctx != NULL && cipher_ctx_mode(ctx) == OPENVPN_MODE_CBC;
361}
362
363bool
365{
366 if (ctx == NULL)
367 {
368 return false;
369 }
370 int mode = cipher_ctx_mode(ctx);
371 return mode == OPENVPN_MODE_OFB || mode == OPENVPN_MODE_CFB;
372}
373
374bool
376{
377 if (ctx == NULL)
378 {
379 return false;
380 }
381 int mode = cipher_ctx_mode(ctx);
382 return mode == (int)PSA_ALG_GCM || mode == (int)PSA_ALG_CHACHA20_POLY1305;
383}
384
385static int
386cipher_ctx_direction(const cipher_ctx_t *ctx)
387{
388 psa_key_usage_t key_usage = psa_get_key_usage_flags(&ctx->key_attributes);
389 if (key_usage & PSA_KEY_USAGE_ENCRYPT)
390 {
391 return OPENVPN_OP_ENCRYPT;
392 }
393 else if (key_usage & PSA_KEY_USAGE_DECRYPT)
394 {
395 return OPENVPN_OP_DECRYPT;
396 }
397 else
398 {
399 return -1;
400 }
401}
402
403int
404cipher_ctx_reset(cipher_ctx_t *ctx, const uint8_t *iv_buf)
405{
406 psa_status_t status = 0;
407
408 if (cipher_ctx_mode_aead(ctx))
409 {
410 if (psa_aead_abort(&ctx->operation.aead) != PSA_SUCCESS)
411 {
412 return 0;
413 }
414
415 if (cipher_ctx_direction(ctx) == OPENVPN_OP_ENCRYPT)
416 {
417 status = psa_aead_encrypt_setup(&ctx->operation.aead, ctx->key, ctx->cipher_info->psa_alg);
418 }
419 else if (cipher_ctx_direction(ctx) == OPENVPN_OP_DECRYPT)
420 {
421 status = psa_aead_decrypt_setup(&ctx->operation.aead, ctx->key, ctx->cipher_info->psa_alg);
422 }
423 else
424 {
425 return 0;
426 }
427
428 if (status != PSA_SUCCESS)
429 {
430 return 0;
431 }
432
433 status = psa_aead_set_nonce(&ctx->operation.aead, iv_buf, ctx->cipher_info->iv_bytes);
434 if (status != PSA_SUCCESS)
435 {
436 return 0;
437 }
438 }
439 else
440 {
441 if (psa_cipher_abort(&ctx->operation.cipher) != PSA_SUCCESS)
442 {
443 return 0;
444 }
445
446 if (cipher_ctx_direction(ctx) == OPENVPN_OP_ENCRYPT)
447 {
448 status = psa_cipher_encrypt_setup(&ctx->operation.cipher, ctx->key, ctx->cipher_info->psa_alg);
449 }
450 else if (cipher_ctx_direction(ctx) == OPENVPN_OP_DECRYPT)
451 {
452 status = psa_cipher_decrypt_setup(&ctx->operation.cipher, ctx->key, ctx->cipher_info->psa_alg);
453 }
454 else
455 {
456 return 0;
457 }
458
459 if (status != PSA_SUCCESS)
460 {
461 return 0;
462 }
463
464 status = psa_cipher_set_iv(&ctx->operation.cipher, iv_buf, ctx->cipher_info->iv_bytes);
465 if (status != PSA_SUCCESS)
466 {
467 return 0;
468 }
469 }
470
471 return 1;
472}
473
474/* We rely on the caller to ensure that the destination buffer has enough room,
475 * but Mbed TLS always wants a size for the destination buffer. This function
476 * calculates the minimum necessary size for a given cipher and input length.
477 *
478 * This funcion assumes that src_len has been checked to be >= 0. */
479static size_t
480needed_dst_size(const cipher_ctx_t *ctx, int src_len)
481{
482 int mode = cipher_ctx_mode(ctx);
483 if (mode == PSA_ALG_CTR || mode == PSA_ALG_GCM || mode == PSA_ALG_CHACHA20_POLY1305)
484 {
485 /* These algorithms are based on a keystream, so the input and output
486 * length are always equal. */
487 return (size_t)src_len;
488 }
489 else
490 {
491 /* These algorithms are block-based. The number of output blocks that are
492 * produced is at most 1 + src_len / block_size. */
493 size_t block_size = (size_t)cipher_ctx_block_size(ctx);
494 size_t max_blocks = 1 + (size_t)src_len / block_size;
495 return max_blocks * block_size;
496 }
497}
498
499int
500cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len)
501{
502 if (src_len < 0 || !cipher_ctx_mode_aead(ctx))
503 {
504 return 0;
505 }
506
507 if (psa_aead_update_ad(&ctx->operation.aead, src, (size_t)src_len) != PSA_SUCCESS)
508 {
509 return 0;
510 }
511 return 1;
512}
513
514int
515cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len)
516{
517 if (src_len < 0)
518 {
519 return 0;
520 }
521
522 size_t dst_size = needed_dst_size(ctx, src_len);
523 size_t psa_output_len = 0;
524 psa_status_t status = 0;
525
526 if (cipher_ctx_mode_aead(ctx))
527 {
528 status = psa_aead_update(&ctx->operation.aead, src, (size_t)src_len, dst, dst_size, &psa_output_len);
529 }
530 else
531 {
532 status = psa_cipher_update(&ctx->operation.cipher, src, (size_t)src_len, dst, dst_size, &psa_output_len);
533 }
534
535 if (status != PSA_SUCCESS)
536 {
537 return 0;
538 }
539
540 if (psa_output_len > INT_MAX)
541 {
542 return 0;
543 }
544 *dst_len = (int)psa_output_len;
545
546 return 1;
547}
548
549int
550cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len)
551{
552 size_t dst_size = needed_dst_size(ctx, 0);
553 size_t psa_output_len = 0;
554 psa_status_t status = 0;
555
556 if (cipher_ctx_mode_aead(ctx))
557 {
558 size_t actual_tag_size = 0;
559 status = psa_aead_finish(&ctx->operation.aead,
560 dst,
561 dst_size,
562 &psa_output_len,
563 ctx->tag,
565 &actual_tag_size);
566 if (status != PSA_SUCCESS || psa_output_len > (size_t)INT_MAX || actual_tag_size != (size_t)OPENVPN_AEAD_TAG_LENGTH)
567 {
568 return 0;
569 }
570 ctx->aead_finished = true;
571 }
572 else
573 {
574 status = psa_cipher_finish(&ctx->operation.cipher, dst, dst_size, &psa_output_len);
575 if (status != PSA_SUCCESS || psa_output_len > (size_t)INT_MAX)
576 {
577 return 0;
578 }
579 }
580
581 *dst_len = (int)psa_output_len;
582
583 return 1;
584}
585
586int
587cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag, size_t tag_len)
588{
589 if (cipher_ctx_direction(ctx) != OPENVPN_OP_DECRYPT || !cipher_ctx_mode_aead(ctx))
590 {
591 return 0;
592 }
593
594 size_t psa_output_len = 0;
595 psa_status_t status = 0;
596
597 status = psa_aead_verify(&ctx->operation.aead, dst, 0, &psa_output_len, tag, tag_len);
598 if (status != PSA_SUCCESS || psa_output_len > (size_t)INT_MAX)
599 {
600 return 0;
601 }
602 *dst_len = (int)psa_output_len;
603
604 return 1;
605}
606
607static const md_info_t md_info_table[] = {
608 /* TODO: Fill out table. */
609 { "MD5", PSA_ALG_MD5 },
610 { "SHA1", PSA_ALG_SHA_1 },
611 { "SHA256", PSA_ALG_SHA_256 },
612};
613const size_t md_info_table_entries = sizeof(md_info_table) / sizeof(md_info_t);
614
615static const md_info_t *
616md_get(const char *digest_name)
617{
618 for (size_t i = 0; i < md_info_table_entries; i++)
619 {
620 if (strcmp(digest_name, md_info_table[i].name) == 0)
621 {
622 return &md_info_table[i];
623 }
624 }
625 return NULL;
626}
627
628bool
629md_valid(const char *digest)
630{
631 const md_info_t *md = md_get(digest);
632 return md != NULL;
633}
634
635const char *
636md_kt_name(const char *mdname)
637{
638 if (strcmp("none", mdname) == 0)
639 {
640 return "[null-digest]";
641 }
642 const md_info_t *md = md_get(mdname);
643 if (md == NULL)
644 {
645 return NULL;
646 }
647 return md->name;
648}
649
650unsigned char
651md_kt_size(const char *mdname)
652{
653 if (strcmp("none", mdname) == 0)
654 {
655 return 0;
656 }
657 const md_info_t *md_info = md_get(mdname);
658 if (md_info == NULL)
659 {
660 return 0;
661 }
662 return (unsigned char)PSA_HASH_LENGTH(md_info->psa_alg);
663}
664
665md_ctx_t *
666md_ctx_new(void)
667{
668 md_ctx_t *ctx;
670 return ctx;
671}
672
673int
674md_full(const char *mdname, const uint8_t *src, int src_len, uint8_t *dst)
675{
676 const md_info_t *md = md_get(mdname);
677 if (md == NULL || src_len < 0)
678 {
679 return 0;
680 }
681
682 /* We depend on the caller to ensure that dst has enough room for the hash,
683 * so we just tell PSA that it can hold the appropriate amount of bytes. */
684 size_t dst_size = PSA_HASH_LENGTH(md->psa_alg);
685 size_t hash_length = 0;
686
687 psa_status_t status = psa_hash_compute(md->psa_alg, src, (size_t)src_len, dst, dst_size, &hash_length);
688 if (status != PSA_SUCCESS || hash_length != dst_size)
689 {
690 return 0;
691 }
692 return 1;
693}
694
695void
697{
698 free(ctx);
699}
700
701void
702md_ctx_init(md_ctx_t *ctx, const char *mdname)
703{
704 const md_info_t *md_info = md_get(mdname);
705 ASSERT(ctx != NULL && md_info != NULL);
706
707 ctx->md_info = md_info;
708 ASSERT(psa_hash_setup(&ctx->operation, md_info->psa_alg) == PSA_SUCCESS);
709}
710
711void
713{
714 ASSERT(psa_hash_abort(&ctx->operation) == PSA_SUCCESS);
715}
716
717int
718md_ctx_size(const md_ctx_t *ctx)
719{
720 if (ctx == NULL)
721 {
722 return 0;
723 }
724 return (int)PSA_HASH_LENGTH(ctx->md_info->psa_alg);
725}
726
727void
728md_ctx_update(md_ctx_t *ctx, const uint8_t *src, size_t src_len)
729{
730 ASSERT(psa_hash_update(&ctx->operation, src, src_len) == PSA_SUCCESS);
731}
732
733void
734md_ctx_final(md_ctx_t *ctx, uint8_t *dst)
735{
736 /* We depend on the caller to ensure that dst has enough room for the hash,
737 * so we just tell PSA that it can hold the appropriate amount of bytes. */
738 size_t dst_size = PSA_HASH_LENGTH(ctx->md_info->psa_alg);
739 size_t hash_length = 0;
740
741 ASSERT(psa_hash_finish(&ctx->operation, dst, dst_size, &hash_length) == PSA_SUCCESS);
742 ASSERT(hash_length == dst_size);
743}
744
746hmac_ctx_new(void)
747{
748 hmac_ctx_t *ctx;
750 return ctx;
751}
752
753void
755{
756 free(ctx);
757}
758
759static void
760hmac_ctx_init_with_arbitrary_key_length(hmac_ctx_t *ctx, const uint8_t *key, size_t key_len, const md_info_t *md_info)
761{
762 ctx->md_info = md_info;
763 psa_set_key_type(&ctx->key_attributes, PSA_KEY_TYPE_HMAC);
764 psa_set_key_algorithm(&ctx->key_attributes, PSA_ALG_HMAC(md_info->psa_alg));
765 psa_set_key_usage_flags(&ctx->key_attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
766
767 if (psa_import_key(&ctx->key_attributes, key, key_len, &ctx->key) != PSA_SUCCESS)
768 {
769 msg(M_FATAL, "psa_import_key failed");
770 }
771
772 ASSERT(psa_mac_sign_setup(&ctx->operation, ctx->key, PSA_ALG_HMAC(md_info->psa_alg)) == PSA_SUCCESS);
773}
774
775void
776hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
777{
778 const md_info_t *md_info = md_get(mdname);
779 ASSERT(ctx != NULL && key != NULL && md_info != NULL);
780
781 hmac_ctx_init_with_arbitrary_key_length(ctx, key, PSA_HASH_LENGTH(md_info->psa_alg), md_info);
782}
783
784void
786{
787 ASSERT(psa_mac_abort(&ctx->operation) == PSA_SUCCESS);
788 ASSERT(psa_destroy_key(ctx->key) == PSA_SUCCESS);
789}
790
791int
793{
794 return (int)PSA_HASH_LENGTH(ctx->md_info->psa_alg);
795}
796
797void
799{
800 ASSERT(psa_mac_abort(&ctx->operation) == PSA_SUCCESS);
801 ASSERT(psa_mac_sign_setup(&ctx->operation, ctx->key, PSA_ALG_HMAC(ctx->md_info->psa_alg)) == PSA_SUCCESS);
802}
803
804void
805hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
806{
807 ASSERT(src_len >= 0);
808 ASSERT(psa_mac_update(&ctx->operation, src, (size_t)src_len) == PSA_SUCCESS);
809}
810
811void
812hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
813{
814 /* We depend on the caller to ensure that dst has enough room for the hash,
815 * so we just tell PSA that it can hold the appropriate amount of bytes. */
816 size_t dst_size = PSA_HASH_LENGTH(ctx->md_info->psa_alg);
817 size_t hmac_length = 0;
818
819 ASSERT(psa_mac_sign_finish(&ctx->operation, dst, dst_size, &hmac_length) == PSA_SUCCESS);
820 ASSERT(hmac_length == dst_size);
821}
822
823/*
824 * Generate the hash required by for the \c tls1_PRF function.
825 *
826 * @param md_kt Message digest to use
827 * @param sec Secret to base the hash on
828 * @param sec_len Length of the secret
829 * @param seed Seed to hash
830 * @param seed_len Length of the seed
831 * @param out Output buffer
832 * @param olen Length of the output buffer
833 */
834static void
835tls1_P_hash(const md_info_t *md_info, const uint8_t *sec, size_t sec_len, const uint8_t *seed,
836 int seed_len, uint8_t *out, size_t olen)
837{
838 struct gc_arena gc = gc_new();
839 uint8_t A1[MAX_HMAC_KEY_LENGTH];
840
841#ifdef ENABLE_DEBUG
842 /* used by the D_SHOW_KEY_SOURCE, guarded with ENABLE_DEBUG to avoid unused
843 * variables warnings if compiled with --enable-small */
844 const size_t olen_orig = olen;
845 const uint8_t *out_orig = out;
846#endif
847
848 hmac_ctx_t *ctx = hmac_ctx_new();
849 hmac_ctx_t *ctx_tmp = hmac_ctx_new();
850
851 dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash sec: %s", format_hex(sec, sec_len, 0, &gc));
852 dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash seed: %s", format_hex(seed, seed_len, 0, &gc));
853
854 unsigned int chunk = (unsigned int)PSA_HASH_LENGTH(md_info->psa_alg);
855 unsigned int A1_len = (unsigned int)PSA_HASH_LENGTH(md_info->psa_alg);
856
857 /* This is the only place where we init an HMAC with a key that is not
858 * equal to its size, therefore we init the hmac ctx manually here */
859 hmac_ctx_init_with_arbitrary_key_length(ctx, sec, sec_len, md_info);
860 hmac_ctx_init_with_arbitrary_key_length(ctx_tmp, sec, sec_len, md_info);
861
862 hmac_ctx_update(ctx, seed, seed_len);
863 hmac_ctx_final(ctx, A1);
864
865 for (;;)
866 {
867 hmac_ctx_reset(ctx);
868 hmac_ctx_reset(ctx_tmp);
869 hmac_ctx_update(ctx, A1, A1_len);
870 hmac_ctx_update(ctx_tmp, A1, A1_len);
871 hmac_ctx_update(ctx, seed, (int)seed_len);
872
873 if (olen > chunk)
874 {
875 hmac_ctx_final(ctx, out);
876 out += chunk;
877 olen -= chunk;
878 hmac_ctx_final(ctx_tmp, A1); /* calc the next A1 value */
879 }
880 else /* last one */
881 {
882 hmac_ctx_final(ctx, A1);
883 memcpy(out, A1, olen);
884 break;
885 }
886 }
887 hmac_ctx_cleanup(ctx);
888 hmac_ctx_free(ctx);
889 hmac_ctx_cleanup(ctx_tmp);
890 hmac_ctx_free(ctx_tmp);
891 secure_memzero(A1, sizeof(A1));
892
893 dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash out: %s", format_hex(out_orig, olen_orig, 0, &gc));
894 gc_free(&gc);
895}
896
897/*
898 * Use the TLS PRF function for generating data channel keys.
899 * This code is based on the OpenSSL library.
900 *
901 * TLS generates keys as such:
902 *
903 * master_secret[48] = PRF(pre_master_secret[48], "master secret",
904 * ClientHello.random[32] + ServerHello.random[32])
905 *
906 * key_block[] = PRF(SecurityParameters.master_secret[48],
907 * "key expansion",
908 * SecurityParameters.server_random[32] +
909 * SecurityParameters.client_random[32]);
910 *
911 * Notes:
912 *
913 * (1) key_block contains a full set of 4 keys.
914 * (2) The pre-master secret is generated by the client.
915 */
916bool
917ssl_tls1_PRF(const uint8_t *label, size_t label_len, const uint8_t *sec, size_t slen, uint8_t *out1,
918 size_t olen)
919{
920 const md_info_t *md5 = md_get("MD5");
921 const md_info_t *sha1 = md_get("SHA1");
922
923 if (label_len > (size_t)INT_MAX)
924 {
925 return false;
926 }
927
928 struct gc_arena gc = gc_new();
929
930 uint8_t *out2 = (uint8_t *)gc_malloc(olen, false, &gc);
931
932 size_t len = slen / 2;
933 const uint8_t *S1 = sec;
934 const uint8_t *S2 = &(sec[len]);
935 len += (slen & 1); /* add for odd, make longer */
936
937 tls1_P_hash(md5, S1, len, label, (int)label_len, out1, olen);
938 tls1_P_hash(sha1, S2, len, label, (int)label_len, out2, olen);
939
940 for (size_t i = 0; i < olen; i++)
941 {
942 out1[i] ^= out2[i];
943 }
944
945 secure_memzero(out2, olen);
946
947 dmsg(D_SHOW_KEY_SOURCE, "tls1_PRF out[%zu]: %s", olen, format_hex(out1, olen, 0, &gc));
948
949 gc_free(&gc);
950 return true;
951}
952
953void
954crypto_init_lib(void)
955{
956}
957
958void
960{
961}
962
963void
965{
966}
967
968bool
969mbed_log_err(unsigned int flags, int errval, const char *prefix)
970{
971 if (0 != errval)
972 {
973 char errstr[256];
974 mbedtls_strerror(errval, errstr, sizeof(errstr));
975
976 if (NULL == prefix)
977 {
978 prefix = "mbed TLS error";
979 }
980 msg(flags, "%s: %s", prefix, errstr);
981 }
982
983 return 0 == errval;
984}
985
986bool
987mbed_log_func_line(unsigned int flags, int errval, const char *func, int line)
988{
989 char prefix[256];
990
991 if (snprintf(prefix, sizeof(prefix), "%s:%d", func, line) >= sizeof(prefix))
992 {
993 return mbed_log_err(flags, errval, func);
994 }
995
996 return mbed_log_err(flags, errval, prefix);
997}
998
999int
1000memcmp_constant_time(const void *a, const void *b, size_t size)
1001{
1002 return mbedtls_ct_memcmp(a, b, size);
1003}
1004
1005void
1007{
1008 /* Mbed TLS 4 does not currently have a mechanism to discover available
1009 * ciphers. We instead print out the ciphers from cipher_info_table. */
1010
1011#ifndef ENABLE_SMALL
1012 printf("The following ciphers and cipher modes are available for use\n"
1013 "with " PACKAGE_NAME ". Each cipher shown below may be used as a\n"
1014 "parameter to the --data-ciphers (or --cipher) option. Using a\n"
1015 "GCM or CBC mode is recommended. In static key mode only CBC\n"
1016 "mode is allowed.\n\n");
1017#endif
1018
1019 for (size_t i = 0; i < cipher_info_table_entries; i++)
1020 {
1021 const cipher_info_t *info = &cipher_info_table[i];
1022 const char *name = info->name;
1023 if (!cipher_kt_insecure(name) && (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(name)))
1024 {
1025 print_cipher(name);
1026 }
1027 }
1028
1029 printf("\nThe following ciphers have a block size of less than 128 bits, \n"
1030 "and are therefore deprecated. Do not use unless you have to.\n\n");
1031 for (size_t i = 0; i < cipher_info_table_entries; i++)
1032 {
1033 const cipher_info_t *info = &cipher_info_table[i];
1034 const char *name = info->name;
1035 if (cipher_kt_insecure(name) && (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(name)))
1036 {
1037 print_cipher(name);
1038 }
1039 }
1040 printf("\n");
1041}
1042
1043void
1045{
1046 /* Mbed TLS 4 does not currently have a mechanism to discover available
1047 * message digests. We instead print out the digests from md_info_table. */
1048
1049#ifndef ENABLE_SMALL
1050 printf("The following message digests are available for use with\n" PACKAGE_NAME
1051 ". A message digest is used in conjunction with\n"
1052 "the HMAC function, to authenticate received packets.\n"
1053 "You can specify a message digest as parameter to\n"
1054 "the --auth option.\n\n");
1055#endif
1056
1057 for (size_t i = 0; i < md_info_table_entries; i++)
1058 {
1059 const md_info_t *info = &md_info_table[i];
1060 printf("%s %d bit default key\n", info->name,
1061 (unsigned char)PSA_HASH_LENGTH(info->psa_alg) * 8);
1062 }
1063 printf("\n");
1064}
1065
1066void
1068{
1069 printf("Sorry, mbed TLS hardware crypto engine functionality is not "
1070 "available\n");
1071}
1072
1073bool
1074crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src,
1075 struct gc_arena *gc)
1076{
1077 /* 1000 chars is the PEM line length limit (+1 for tailing NUL) */
1078 char header[1000 + 1] = { 0 };
1079 char footer[1000 + 1] = { 0 };
1080
1081 if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name) >= sizeof(header))
1082 {
1083 return false;
1084 }
1085 if (snprintf(footer, sizeof(footer), "-----END %s-----\n", name) >= sizeof(footer))
1086 {
1087 return false;
1088 }
1089
1090 size_t out_len = 0;
1091 if (MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL
1092 != mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src), NULL, 0, &out_len))
1093 {
1094 return false;
1095 }
1096
1097 /* We set the size buf to out_len-1 to NOT include the 0 byte that
1098 * mbedtls_pem_write_buffer in its length calculation */
1099 *dst = alloc_buf_gc(out_len, gc);
1100 if (!mbed_ok(mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src), BPTR(dst),
1101 BCAP(dst), &out_len))
1102 || !(out_len < INT_MAX && out_len > 1)
1103 || !buf_inc_len(dst, (int)out_len - 1))
1104 {
1105 CLEAR(*dst);
1106 return false;
1107 }
1108
1109 return true;
1110}
1111
1112bool
1113crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
1114{
1115 /* 1000 chars is the PEM line length limit (+1 for tailing NUL) */
1116 char header[1000 + 1] = { 0 };
1117 char footer[1000 + 1] = { 0 };
1118
1119 if (snprintf(header, sizeof(header), "-----BEGIN %s-----", name) >= sizeof(header))
1120 {
1121 return false;
1122 }
1123 if (snprintf(footer, sizeof(footer), "-----END %s-----", name) >= sizeof(footer))
1124 {
1125 return false;
1126 }
1127
1128 /* mbed TLS requires the src to be null-terminated */
1129 /* allocate a new buffer to avoid modifying the src buffer */
1130 struct gc_arena gc = gc_new();
1131 struct buffer input = alloc_buf_gc(BLEN(src) + 1, &gc);
1132 buf_copy(&input, src);
1134
1135 size_t use_len = 0;
1136 mbedtls_pem_context ctx = { 0 };
1137 bool ret =
1138 mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(&input), NULL, 0, &use_len));
1139 size_t buf_size = 0;
1140 const unsigned char *buf = mbedtls_pem_get_buffer(&ctx, &buf_size);
1141 if (ret && !buf_write(dst, buf, buf_size))
1142 {
1143 ret = false;
1144 msg(M_WARN, "PEM decode error: destination buffer too small");
1145 }
1146
1147 mbedtls_pem_free(&ctx);
1148 gc_free(&gc);
1149 return ret;
1150}
1151
1152#endif /* MBEDTLS_VERSION_NUMBER >= 0x04000000 */
1153#endif /* defined(ENABLE_CRYPTO_MBEDTLS) */
void buf_null_terminate(struct buffer *buf)
Definition buffer.c:532
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition buffer.c:336
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition buffer.h:704
#define BPTR(buf)
Definition buffer.h:123
static bool buf_inc_len(struct buffer *buf, int inc)
Definition buffer.h:588
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:414
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition buffer.h:660
#define BLEN(buf)
Definition buffer.h:126
static char * format_hex(const uint8_t *data, size_t size, size_t maxoutput, struct gc_arena *gc)
Definition buffer.h:503
#define BCAP(buf)
Definition buffer.h:129
static void gc_free(struct gc_arena *a)
Definition buffer.h:1025
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition buffer.h:1064
static struct gc_arena gc_new(void)
Definition buffer.h:1017
#define PACKAGE_NAME
Definition config.h:492
void print_cipher(const char *ciphername)
Print a cipher list entry.
Definition crypto.c:1748
Data Channel Cryptography Module.
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
Data Channel Cryptography SSL library-specific backend interface.
int cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len)
Updates the given cipher context, encrypting data in the source buffer, and placing any complete bloc...
bool ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len, uint8_t *output, size_t output_len)
Calculates the TLS 1.0-1.1 PRF function.
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
hmac_ctx_t * hmac_ctx_new(void)
int md_full(const char *mdname, const uint8_t *src, int src_len, uint8_t *dst)
Calculates the message digest for the given buffer.
void hmac_ctx_reset(hmac_ctx_t *ctx)
int cipher_ctx_block_size(const cipher_ctx_t *ctx)
Returns the block size of the cipher, in bytes.
bool cipher_kt_mode_cbc(const char *ciphername)
Check if the supplied cipher is a supported CBC mode cipher.
void show_available_engines(void)
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
md_ctx_t * md_ctx_new(void)
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, size_t src_len)
int cipher_ctx_iv_length(const cipher_ctx_t *ctx)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
void crypto_unload_provider(const char *provname, provider_t *provider)
Unloads the given (OpenSSL) provider.
void crypto_uninit_lib(void)
int cipher_kt_block_size(const char *ciphername)
Returns the block size of the cipher, in bytes.
bool cipher_kt_mode_aead(const char *ciphername)
Check if the supplied cipher is a supported AEAD mode cipher.
int md_ctx_size(const md_ctx_t *ctx)
void show_available_ciphers(void)
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported CBC mode cipher.
void crypto_init_lib(void)
cipher_ctx_t * cipher_ctx_new(void)
Generic cipher functions.
bool cipher_kt_mode_ofb_cfb(const char *ciphername)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
const char * md_kt_name(const char *mdname)
Retrieve a string describing the digest digest (e.g.
int hmac_ctx_size(hmac_ctx_t *ctx)
bool cipher_kt_insecure(const char *ciphername)
Returns true if we consider this cipher to be insecure.
#define MAX_CIPHER_KEY_LENGTH
void crypto_clear_error(void)
bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
Decode a PEM buffer to binary data.
provider_t * crypto_load_provider(const char *provider)
Load the given (OpenSSL) providers.
void cipher_ctx_free(cipher_ctx_t *ctx)
Cleanup and free a cipher context.
int cipher_ctx_mode(const cipher_ctx_t *ctx)
Returns the mode that the cipher runs in.
bool cipher_ctx_mode_ofb_cfb(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported AEAD mode cipher.
void hmac_ctx_free(hmac_ctx_t *ctx)
int cipher_kt_iv_size(const char *ciphername)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
int cipher_kt_tag_size(const char *ciphername)
Returns the MAC tag size of the cipher, in bytes.
int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len)
Updates the given cipher context, providing additional data (AD) for authenticated encryption with ad...
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
const size_t cipher_name_translation_table_count
const char * cipher_kt_name(const char *ciphername)
Retrieve a normalised string describing the cipher (e.g.
#define MAX_HMAC_KEY_LENGTH
#define OPENVPN_AEAD_TAG_LENGTH
void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphername, crypto_operation_t enc)
Initialise a cipher context, based on the given key and key type.
int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len)
Gets the computed message authenticated code (MAC) tag for this cipher.
int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
void crypto_init_lib_engine(const char *engine_name)
void hmac_ctx_cleanup(hmac_ctx_t *ctx)
int cipher_ctx_reset(cipher_ctx_t *ctx, const uint8_t *iv_buf)
Resets the given cipher context, setting the IV to the specified value.
const cipher_name_pair cipher_name_translation_table[]
Cipher name translation table.
void md_ctx_cleanup(md_ctx_t *ctx)
int cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len)
Pads the final cipher block using PKCS padding, and output to the destination buffer.
void md_ctx_final(md_ctx_t *ctx, uint8_t *dst)
unsigned char md_kt_size(const char *mdname)
Returns the size of the message digest, in bytes.
void show_available_digests(void)
bool cipher_valid_reason(const char *ciphername, const char **reason)
Returns if the cipher is valid, based on the given cipher name and provides a reason if invalid.
int cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag, size_t tag_len)
Like cipher_ctx_final, but check the computed authentication tag against the supplied (expected) tag.
void md_ctx_init(md_ctx_t *ctx, const char *mdname)
Initialises the given message digest context.
void md_ctx_free(md_ctx_t *ctx)
bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src, struct gc_arena *gc)
Encode binary data as PEM.
Data Channel Cryptography backend interface using the TF-PSA-Crypto library part of Mbed TLS 4.
#define OPENVPN_OP_DECRYPT
Cipher should decrypt.
#define OPENVPN_MODE_OFB
Cipher is in OFB mode.
#define mbed_ok(errval)
Check errval and log on error.
int crypto_operation_t
bool mbed_log_err(unsigned int flags, int errval, const char *prefix)
Log the supplied mbed TLS error, prefixed by supplied prefix.
#define OPENVPN_MODE_CFB
Cipher is in CFB mode.
#define OPENVPN_MODE_CBC
Cipher is in CBC mode.
void provider_t
#define OPENVPN_OP_ENCRYPT
Cipher should encrypt.
bool mbed_log_func_line(unsigned int flags, int errval, const char *func, int line)
Log the supplied mbed TLS error, prefixed by function name and line number.
static evp_cipher_type * cipher_get(const char *ciphername)
static evp_md_type * md_get(const char *digest)
#define D_SHOW_KEY_SOURCE
Definition errlevel.h:121
#define D_LOW
Definition errlevel.h:96
static SERVICE_STATUS status
Definition interactive.c:51
#define CLEAR(x)
Definition basic.h:32
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:172
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
cipher_operation_t operation
uint8_t tag[16]
psa_key_attributes_t key_attributes
mbedtls_svc_key_id_t key
const cipher_info_t * cipher_info
psa_key_type_t psa_key_type
psa_algorithm_t psa_alg
const char * name
Struct used in cipher name translation table.
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
const md_info_t * md_info
psa_mac_operation_t operation
psa_key_attributes_t key_attributes
mbedtls_svc_key_id_t key
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
const md_info_t * md_info
psa_hash_operation_t operation
psa_algorithm_t psa_alg
const char * name
struct gc_arena gc
Definition test_ssl.c:131
psa_aead_operation_t aead
psa_cipher_operation_t cipher