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-2025 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2021 Fox Crypto B.V. <openvpn@foxcrypto.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#ifdef ENABLE_CRYPTO_MBEDTLS
36#include "crypto_mbedtls.h"
37#endif
38#include "basic.h"
39#include "buffer.h"
40
41/* TLS uses a tag of 128 bits, let's do the same for OpenVPN */
42#define OPENVPN_AEAD_TAG_LENGTH 16
43
44/* Maximum cipher block size (bytes) */
45#define OPENVPN_MAX_CIPHER_BLOCK_SIZE 32
46
47/* Maximum HMAC digest size (bytes) */
48#define OPENVPN_MAX_HMAC_SIZE 64
49
51typedef enum
52{
56
58typedef struct
59{
60 const char *openvpn_name;
61 const char *lib_name;
63
66extern const size_t cipher_name_translation_table_count;
67
68/*
69 * This routine should have additional OpenSSL crypto library initialisations
70 * used by both crypto and ssl components of OpenVPN.
71 */
72void crypto_init_lib(void);
73
74void crypto_uninit_lib(void);
75
76void crypto_clear_error(void);
77
78/*
79 * Initialise the given named crypto engine.
80 */
81void crypto_init_lib_engine(const char *engine_name);
82
83
89provider_t *crypto_load_provider(const char *provider);
90
96void crypto_unload_provider(const char *provname, provider_t *provider);
97
98#ifdef DMALLOC
99/*
100 * OpenSSL memory debugging. If dmalloc debugging is enabled, tell
101 * OpenSSL to use our private malloc/realloc/free functions so that
102 * we can dispatch them to dmalloc.
103 */
104void crypto_init_dmalloc(void);
105
106#endif /* DMALLOC */
107
108void show_available_ciphers(void);
109
110void show_available_digests(void);
111
112void show_available_engines(void);
113
127bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src,
128 struct gc_arena *gc);
129
139bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src);
140
141/*
142 *
143 * Random number functions, used in cases where we want
144 * reasonably strong cryptographic random number generation
145 * without depleting our entropy pool. Used for random
146 * IV values and a number of other miscellaneous tasks.
147 *
148 */
149
159int rand_bytes(uint8_t *output, int len);
160
161/*
162 *
163 * Generic cipher key type functions
164 *
165 */
166/*
167 * Max size in bytes of any cipher key that might conceivably be used.
168 *
169 * This value is checked at compile time in crypto.c to make sure
170 * it is always at least EVP_MAX_KEY_LENGTH.
171 *
172 * We define our own value, since this parameter
173 * is used to control the size of static key files.
174 * If the OpenSSL library increases EVP_MAX_KEY_LENGTH,
175 * we don't want our key files to be suddenly rendered
176 * unusable.
177 */
178#define MAX_CIPHER_KEY_LENGTH 64
179
193bool cipher_valid_reason(const char *ciphername, const char **reason);
194
204static inline bool
205cipher_valid(const char *ciphername)
206{
207 const char *reason;
208 return cipher_valid_reason(ciphername, &reason);
209}
210
218static inline bool
219cipher_defined(const char *ciphername)
220{
221 ASSERT(ciphername);
222 return strcmp(ciphername, "none") != 0;
223}
224
237const char *cipher_kt_name(const char *ciphername);
238
247int cipher_kt_key_size(const char *ciphername);
248
258int cipher_kt_iv_size(const char *ciphername);
259
267int cipher_kt_block_size(const char *ciphername);
268
277int cipher_kt_tag_size(const char *ciphername);
278
282bool cipher_kt_insecure(const char *ciphername);
283
284
292bool cipher_kt_mode_cbc(const char *ciphername);
293
301bool cipher_kt_mode_ofb_cfb(const char *ciphername);
302
310bool cipher_kt_mode_aead(const char *ciphername);
311
312
325
332
342void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphername,
344
355
363int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len);
364
373
383
391bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx);
392
401
409bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx);
410
420int cipher_ctx_reset(cipher_ctx_t *ctx, const uint8_t *iv_buf);
421
432int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len);
433
451int cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len);
452
463int cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len);
464
478int cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag,
479 size_t tag_len);
480
481
482/*
483 *
484 * Generic message digest information functions
485 *
486 */
487
488/*
489 * Max size in bytes of any HMAC key that might conceivably be used.
490 *
491 * This value is checked at compile time in crypto.c to make sure
492 * it is always at least EVP_MAX_MD_SIZE. We define our own value
493 * for the same reason as above.
494 */
495#define MAX_HMAC_KEY_LENGTH 64
496
503static inline bool
504md_defined(const char *mdname)
505{
506 return strcmp(mdname, "none") != 0;
507}
508
509
517bool md_valid(const char *digest);
518
527const char *md_kt_name(const char *mdname);
528
536unsigned char md_kt_size(const char *mdname);
537
538
539/*
540 *
541 * Generic message digest functions
542 *
543 */
544
555int md_full(const char *mdname, const uint8_t *src, int src_len, uint8_t *dst);
556
557/*
558 * Allocate a new message digest context
559 *
560 * @return a new zeroed MD context
561 */
562md_ctx_t *md_ctx_new(void);
563
564/*
565 * Free an existing, non-null message digest context
566 *
567 * @param ctx Message digest context
568 */
570
577void md_ctx_init(md_ctx_t *ctx, const char *mdname);
578
579/*
580 * Free the given message digest context.
581 *
582 * @param ctx Message digest context
583 */
585
586/*
587 * Returns the size of the message digest output by the given context
588 *
589 * @param ctx Message digest context.
590 *
591 * @return Size of the message digest, or \0 if ctx is NULL.
592 */
593int md_ctx_size(const md_ctx_t *ctx);
594
595/*
596 * Process the given data for use in the message digest.
597 *
598 * @param ctx Message digest context. May not be NULL.
599 * @param src Buffer to digest. May not be NULL.
600 * @param src_len The length of the incoming buffer.
601 */
602void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, int src_len);
603
604/*
605 * Output the message digest to the given buffer.
606 *
607 * @param ctx Message digest context. May not be NULL.
608 * @param dst Buffer to write the message digest to. May not be NULL.
609 */
610void md_ctx_final(md_ctx_t *ctx, uint8_t *dst);
611
612
613/*
614 *
615 * Generic HMAC functions
616 *
617 */
618
619/*
620 * Create a new HMAC context
621 *
622 * @return A new HMAC context
623 */
625
626/*
627 * Free an existing HMAC context
628 *
629 * @param ctx HMAC context to free
630 */
632
633/*
634 * Initialises the given HMAC context, using the given digest
635 * and key.
636 *
637 * @param ctx HMAC context to initialise
638 * @param key The key to use for the HMAC
639 * @param mdname message digest name
640 *
641 */
642void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname);
643
644
645/*
646 * Free the given HMAC context.
647 *
648 * @param ctx HMAC context
649 */
651
652/*
653 * Returns the size of the HMAC output by the given HMAC Context
654 *
655 * @param ctx HMAC context.
656 *
657 * @return Size of the HMAC, or \0 if ctx is NULL.
658 */
660
661/*
662 * Resets the given HMAC context, preserving the associated key information
663 *
664 * @param ctx HMAC context. May not be NULL.
665 */
667
668/*
669 * Process the given data for use in the HMAC.
670 *
671 * @param ctx HMAC context. May not be NULL.
672 * @param src The buffer to HMAC. May not be NULL.
673 * @param src_len The length of the incoming buffer.
674 */
675void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len);
676
677/*
678 * Output the HMAC to the given buffer.
679 *
680 * @param ctx HMAC context. May not be NULL.
681 * @param dst buffer to write the HMAC to. May not be NULL.
682 */
683void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst);
684
693const char *translate_cipher_name_from_openvpn(const char *cipher_name);
694
703const char *translate_cipher_name_to_openvpn(const char *cipher_name);
704
705
719bool ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len,
720 uint8_t *output, size_t output_len);
721
722#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:1777
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)
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.
void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, int src_len)
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:1790
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 mbed TLS-specific backend interface.
mbedtls_cipher_context_t cipher_ctx_t
Generic cipher context.
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
mbedtls_md_context_t md_ctx_t
Generic message digest context.
void provider_t
mbedtls_operation_t crypto_operation_t
Data Channel Cryptography OpenSSL-specific backend interface.
#define ASSERT(x)
Definition error.h:217
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:154