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
105#ifdef DMALLOC
106/*
107 * OpenSSL memory debugging. If dmalloc debugging is enabled, tell
108 * OpenSSL to use our private malloc/realloc/free functions so that
109 * we can dispatch them to dmalloc.
110 */
111void crypto_init_dmalloc(void);
112
113#endif /* DMALLOC */
114
115void show_available_ciphers(void);
116
117void show_available_digests(void);
118
119void show_available_engines(void);
120
134bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src,
135 struct gc_arena *gc);
136
146bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src);
147
148/*
149 *
150 * Random number functions, used in cases where we want
151 * reasonably strong cryptographic random number generation
152 * without depleting our entropy pool. Used for random
153 * IV values and a number of other miscellaneous tasks.
154 *
155 */
156
166int rand_bytes(uint8_t *output, int len);
167
168/*
169 *
170 * Generic cipher key type functions
171 *
172 */
173/*
174 * Max size in bytes of any cipher key that might conceivably be used.
175 *
176 * This value is checked at compile time in crypto.c to make sure
177 * it is always at least EVP_MAX_KEY_LENGTH.
178 *
179 * We define our own value, since this parameter
180 * is used to control the size of static key files.
181 * If the OpenSSL library increases EVP_MAX_KEY_LENGTH,
182 * we don't want our key files to be suddenly rendered
183 * unusable.
184 */
185#define MAX_CIPHER_KEY_LENGTH 64
186
200bool cipher_valid_reason(const char *ciphername, const char **reason);
201
211static inline bool
212cipher_valid(const char *ciphername)
213{
214 const char *reason;
215 return cipher_valid_reason(ciphername, &reason);
216}
217
225static inline bool
226cipher_defined(const char *ciphername)
227{
228 ASSERT(ciphername);
229 return strcmp(ciphername, "none") != 0;
230}
231
244const char *cipher_kt_name(const char *ciphername);
245
254int cipher_kt_key_size(const char *ciphername);
255
265int cipher_kt_iv_size(const char *ciphername);
266
274int cipher_kt_block_size(const char *ciphername);
275
284int cipher_kt_tag_size(const char *ciphername);
285
289bool cipher_kt_insecure(const char *ciphername);
290
291
299bool cipher_kt_mode_cbc(const char *ciphername);
300
308bool cipher_kt_mode_ofb_cfb(const char *ciphername);
309
317bool cipher_kt_mode_aead(const char *ciphername);
318
319
332
339
349void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphername,
351
362
370int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len);
371
380
390
398bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx);
399
408
416bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx);
417
427int cipher_ctx_reset(cipher_ctx_t *ctx, const uint8_t *iv_buf);
428
439int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len);
440
458int cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len);
459
470int cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len);
471
485int cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag,
486 size_t tag_len);
487
488
489/*
490 *
491 * Generic message digest information functions
492 *
493 */
494
495/*
496 * Max size in bytes of any HMAC key that might conceivably be used.
497 *
498 * This value is checked at compile time in crypto.c to make sure
499 * it is always at least EVP_MAX_MD_SIZE. We define our own value
500 * for the same reason as above.
501 */
502#define MAX_HMAC_KEY_LENGTH 64
503
510static inline bool
511md_defined(const char *mdname)
512{
513 return strcmp(mdname, "none") != 0;
514}
515
516
524bool md_valid(const char *digest);
525
534const char *md_kt_name(const char *mdname);
535
543unsigned char md_kt_size(const char *mdname);
544
545
546/*
547 *
548 * Generic message digest functions
549 *
550 */
551
562int md_full(const char *mdname, const uint8_t *src, int src_len, uint8_t *dst);
563
564/*
565 * Allocate a new message digest context
566 *
567 * @return a new zeroed MD context
568 */
569md_ctx_t *md_ctx_new(void);
570
571/*
572 * Free an existing, non-null message digest context
573 *
574 * @param ctx Message digest context
575 */
577
584void md_ctx_init(md_ctx_t *ctx, const char *mdname);
585
586/*
587 * Free the given message digest context.
588 *
589 * @param ctx Message digest context
590 */
592
593/*
594 * Returns the size of the message digest output by the given context
595 *
596 * @param ctx Message digest context.
597 *
598 * @return Size of the message digest, or \0 if ctx is NULL.
599 */
600int md_ctx_size(const md_ctx_t *ctx);
601
602/*
603 * Process the given data for use in the message digest.
604 *
605 * @param ctx Message digest context. May not be NULL.
606 * @param src Buffer to digest. May not be NULL.
607 * @param src_len The length of the incoming buffer.
608 */
609void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, size_t src_len);
610
611/*
612 * Output the message digest to the given buffer.
613 *
614 * @param ctx Message digest context. May not be NULL.
615 * @param dst Buffer to write the message digest to. May not be NULL.
616 */
617void md_ctx_final(md_ctx_t *ctx, uint8_t *dst);
618
619
620/*
621 *
622 * Generic HMAC functions
623 *
624 */
625
626/*
627 * Create a new HMAC context
628 *
629 * @return A new HMAC context
630 */
632
633/*
634 * Free an existing HMAC context
635 *
636 * @param ctx HMAC context to free
637 */
639
640/*
641 * Initialises the given HMAC context, using the given digest
642 * and key.
643 *
644 * @param ctx HMAC context to initialise
645 * @param key The key to use for the HMAC
646 * @param mdname message digest name
647 *
648 */
649void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname);
650
651
652/*
653 * Free the given HMAC context.
654 *
655 * @param ctx HMAC context
656 */
658
659/*
660 * Returns the size of the HMAC output by the given HMAC Context
661 *
662 * @param ctx HMAC context.
663 *
664 * @return Size of the HMAC, or \0 if ctx is NULL.
665 */
667
668/*
669 * Resets the given HMAC context, preserving the associated key information
670 *
671 * @param ctx HMAC context. May not be NULL.
672 */
674
675/*
676 * Process the given data for use in the HMAC.
677 *
678 * @param ctx HMAC context. May not be NULL.
679 * @param src The buffer to HMAC. May not be NULL.
680 * @param src_len The length of the incoming buffer.
681 */
682void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len);
683
684/*
685 * Output the HMAC to the given buffer.
686 *
687 * @param ctx HMAC context. May not be NULL.
688 * @param dst buffer to write the HMAC to. May not be NULL.
689 */
690void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst);
691
700const char *translate_cipher_name_from_openvpn(const char *cipher_name);
701
710const char *translate_cipher_name_to_openvpn(const char *cipher_name);
711
712
726bool ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len,
727 uint8_t *output, size_t output_len);
728
729#endif /* CRYPTO_BACKEND_H_ */
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:1797
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)
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)
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.
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)
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 char * translate_cipher_name_to_openvpn(const char *cipher_name)
Translate a crypto library cipher name to an OpenVPN cipher name.
Definition crypto.c:1810
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.
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.
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:60
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:116
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
struct gc_arena gc
Definition test_ssl.c:131