OpenVPN
crypto_backend.h
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
29#ifndef CRYPTO_BACKEND_H_
30#define CRYPTO_BACKEND_H_
31
32#ifdef ENABLE_CRYPTO_OPENSSL
33#include "crypto_openssl.h"
34#endif
35
36#ifdef ENABLE_CRYPTO_MBEDTLS
37#include <mbedtls/version.h>
38#if MBEDTLS_VERSION_NUMBER < 0x04000000
40#else
41#include "crypto_mbedtls.h"
42#endif
43#endif
44
45#include "basic.h"
46#include "buffer.h"
47
48/* TLS uses a tag of 128 bits, let's do the same for OpenVPN */
49#define OPENVPN_AEAD_TAG_LENGTH 16
50
51/* Maximum cipher block size (bytes) */
52#define OPENVPN_MAX_CIPHER_BLOCK_SIZE 32
53
54/* Maximum HMAC digest size (bytes) */
55#define OPENVPN_MAX_HMAC_SIZE 64
56
58typedef enum
59{
63
65typedef struct
66{
67 const char *openvpn_name;
68 const char *lib_name;
70
73extern const size_t cipher_name_translation_table_count;
74
75/*
76 * This routine should have additional OpenSSL crypto library initialisations
77 * used by both crypto and ssl components of OpenVPN.
78 */
79void crypto_init_lib(void);
80
81void crypto_uninit_lib(void);
82
83void crypto_clear_error(void);
84
85/*
86 * Initialise the given named crypto engine.
87 */
88void crypto_init_lib_engine(const char *engine_name);
89
90
96provider_t *crypto_load_provider(const char *provider);
97
103void crypto_unload_provider(const char *provname, provider_t *provider);
104
105void show_available_ciphers(void);
106
107void show_available_digests(void);
108
109void show_available_engines(void);
110
124bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src,
125 struct gc_arena *gc);
126
136bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src);
137
138/*
139 *
140 * Random number functions, used in cases where we want
141 * reasonably strong cryptographic random number generation
142 * without depleting our entropy pool. Used for random
143 * IV values and a number of other miscellaneous tasks.
144 *
145 */
146
156int rand_bytes(uint8_t *output, int len);
157
158/*
159 *
160 * Generic cipher key type functions
161 *
162 */
163/*
164 * Max size in bytes of any cipher key that might conceivably be used.
165 *
166 * This value is checked at compile time in crypto.c to make sure
167 * it is always at least EVP_MAX_KEY_LENGTH.
168 *
169 * We define our own value, since this parameter
170 * is used to control the size of static key files.
171 * If the OpenSSL library increases EVP_MAX_KEY_LENGTH,
172 * we don't want our key files to be suddenly rendered
173 * unusable.
174 */
175#define MAX_CIPHER_KEY_LENGTH 64
176
190bool cipher_valid_reason(const char *ciphername, const char **reason);
191
201static inline bool
202cipher_valid(const char *ciphername)
203{
204 const char *reason;
205 return cipher_valid_reason(ciphername, &reason);
206}
207
215static inline bool
216cipher_defined(const char *ciphername)
217{
218 ASSERT(ciphername);
219 return strcmp(ciphername, "none") != 0;
220}
221
234const char *cipher_kt_name(const char *ciphername);
235
244unsigned int cipher_kt_key_size(const char *ciphername);
245
255unsigned int cipher_kt_iv_size(const char *ciphername);
256
264unsigned int cipher_kt_block_size(const char *ciphername);
265
274unsigned int cipher_kt_tag_size(const char *ciphername);
275
279bool cipher_kt_insecure(const char *ciphername);
280
281
289bool cipher_kt_mode_cbc(const char *ciphername);
290
298bool cipher_kt_mode_ofb_cfb(const char *ciphername);
299
307bool cipher_kt_mode_aead(const char *ciphername);
308
309
322
329
339void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphername,
341
351unsigned int cipher_ctx_iv_length(const cipher_ctx_t *ctx);
352
360int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len);
361
369unsigned int cipher_ctx_block_size(const cipher_ctx_t *ctx);
370
380
388bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx);
389
398
406bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx);
407
417int cipher_ctx_reset(cipher_ctx_t *ctx, const uint8_t *iv_buf);
418
429int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len);
430
448int cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len);
449
460int cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len);
461
475int cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag,
476 size_t tag_len);
477
478
479/*
480 *
481 * Generic message digest information functions
482 *
483 */
484
485/*
486 * Max size in bytes of any HMAC key that might conceivably be used.
487 *
488 * This value is checked at compile time in crypto.c to make sure
489 * it is always at least EVP_MAX_MD_SIZE. We define our own value
490 * for the same reason as above.
491 */
492#define MAX_HMAC_KEY_LENGTH 64
493
500static inline bool
501md_defined(const char *mdname)
502{
503 return strcmp(mdname, "none") != 0;
504}
505
506
514bool md_valid(const char *digest);
515
524const char *md_kt_name(const char *mdname);
525
533unsigned char md_kt_size(const char *mdname);
534
535
536/*
537 *
538 * Generic message digest functions
539 *
540 */
541
542/*
543 * Allocate a new message digest context
544 *
545 * @return a new zeroed MD context
546 */
547md_ctx_t *md_ctx_new(void);
548
549/*
550 * Free an existing, non-null message digest context
551 *
552 * @param ctx Message digest context
553 */
555
562void md_ctx_init(md_ctx_t *ctx, const char *mdname);
563
564/*
565 * Free the given message digest context.
566 *
567 * @param ctx Message digest context
568 */
570
571/*
572 * Returns the size of the message digest output by the given context
573 *
574 * @param ctx Message digest context.
575 *
576 * @return Size of the message digest, or \0 if ctx is NULL.
577 */
578int md_ctx_size(const md_ctx_t *ctx);
579
580/*
581 * Process the given data for use in the message digest.
582 *
583 * @param ctx Message digest context. May not be NULL.
584 * @param src Buffer to digest. May not be NULL.
585 * @param src_len The length of the incoming buffer.
586 */
587void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, size_t src_len);
588
589/*
590 * Output the message digest to the given buffer.
591 *
592 * @param ctx Message digest context. May not be NULL.
593 * @param dst Buffer to write the message digest to. May not be NULL.
594 */
595void md_ctx_final(md_ctx_t *ctx, uint8_t *dst);
596
597
598/*
599 *
600 * Generic HMAC functions
601 *
602 */
603
604/*
605 * Create a new HMAC context
606 *
607 * @return A new HMAC context
608 */
610
611/*
612 * Free an existing HMAC context
613 *
614 * @param ctx HMAC context to free
615 */
617
618/*
619 * Initialises the given HMAC context, using the given digest
620 * and key.
621 *
622 * @param ctx HMAC context to initialise
623 * @param key The key to use for the HMAC
624 * @param mdname message digest name
625 *
626 */
627void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname);
628
629
630/*
631 * Free the given HMAC context.
632 *
633 * @param ctx HMAC context
634 */
636
637/*
638 * Returns the size of the HMAC output by the given HMAC Context
639 *
640 * @param ctx HMAC context.
641 *
642 * @return Size of the HMAC, or \0 if ctx is NULL.
643 */
645
646/*
647 * Resets the given HMAC context, preserving the associated key information
648 *
649 * @param ctx HMAC context. May not be NULL.
650 */
652
653/*
654 * Process the given data for use in the HMAC.
655 *
656 * @param ctx HMAC context. May not be NULL.
657 * @param src The buffer to HMAC. May not be NULL.
658 * @param src_len The length of the incoming buffer.
659 */
660void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len);
661
662/*
663 * Output the HMAC to the given buffer.
664 *
665 * @param ctx HMAC context. May not be NULL.
666 * @param dst buffer to write the HMAC to. May not be NULL.
667 */
668void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst);
669
678const char *translate_cipher_name_from_openvpn(const char *cipher_name);
679
688const char *translate_cipher_name_to_openvpn(const char *cipher_name);
689
690
704bool ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len,
705 uint8_t *output, size_t output_len);
706
707#endif /* CRYPTO_BACKEND_H_ */
Buffer management functions and garbage collection.
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...
const char * translate_cipher_name_from_openvpn(const char *cipher_name)
Translate an OpenVPN cipher name to a crypto library cipher name.
Definition crypto.c:1798
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)
void hmac_ctx_reset(hmac_ctx_t *ctx)
bool cipher_kt_mode_cbc(const char *ciphername)
Check if the supplied cipher is a supported CBC mode cipher.
void show_available_engines(void)
hash_algo_type
Types referencing specific message digest hashing algorithms.
@ MD_SHA256
@ MD_SHA1
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
md_ctx_t * md_ctx_new(void)
static bool cipher_defined(const char *ciphername)
Checks if the cipher is defined and is not the null (none) cipher.
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)
unsigned 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.
unsigned 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)
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.
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.
static bool cipher_valid(const char *ciphername)
Returns if the cipher is valid, based on the given cipher name.
void hmac_ctx_free(hmac_ctx_t *ctx)
unsigned 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 char * translate_cipher_name_to_openvpn(const char *cipher_name)
Translate a crypto library cipher name to an OpenVPN cipher name.
Definition crypto.c:1811
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.
unsigned int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
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.
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)
unsigned int cipher_ctx_block_size(const cipher_ctx_t *ctx)
Returns the block size of the cipher, in bytes.
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.
unsigned int cipher_kt_block_size(const char *ciphername)
Returns the block size of the cipher, in bytes.
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.
static bool md_defined(const char *mdname)
Checks if the cipher is defined and is not the null (none) cipher.
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.
int crypto_operation_t
void provider_t
Data Channel Cryptography mbed TLS-specific backend interface.
Data Channel Cryptography OpenSSL-specific backend interface.
#define ASSERT(x)
Definition error.h:219
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
Struct used in cipher name translation table.
const char * openvpn_name
Cipher name used by OpenVPN.
const char * lib_name
Cipher name used by crypto library.
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
struct gc_arena gc
Definition test_ssl.c:122