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 "crypto_mbedtls.h"
38#endif
39
40#include "basic.h"
41#include "buffer.h"
42
43/* TLS uses a tag of 128 bits, let's do the same for OpenVPN */
44#define OPENVPN_AEAD_TAG_LENGTH 16
45
46/* Maximum cipher block size (bytes) */
47#define OPENVPN_MAX_CIPHER_BLOCK_SIZE 32
48
49/* Maximum HMAC digest size (bytes) */
50#define OPENVPN_MAX_HMAC_SIZE 64
51
53typedef enum
54{
58
60typedef struct
61{
62 const char *openvpn_name;
63 const char *lib_name;
65
68extern const size_t cipher_name_translation_table_count;
69
70/*
71 * This routine should have additional OpenSSL crypto library initialisations
72 * used by both crypto and ssl components of OpenVPN.
73 */
74void crypto_init_lib(void);
75
76void crypto_uninit_lib(void);
77
78void crypto_clear_error(void);
79
80/*
81 * Initialise the given named crypto engine.
82 */
83void crypto_init_lib_engine(const char *engine_name);
84
85
91provider_t *crypto_load_provider(const char *provider);
92
98void crypto_unload_provider(const char *provname, provider_t *provider);
99
100void show_available_ciphers(void);
101
102void show_available_digests(void);
103
104void show_available_engines(void);
105
119bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src,
120 struct gc_arena *gc);
121
131bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src);
132
133/*
134 *
135 * Random number functions, used in cases where we want
136 * reasonably strong cryptographic random number generation
137 * without depleting our entropy pool. Used for random
138 * IV values and a number of other miscellaneous tasks.
139 *
140 */
141
151int rand_bytes(uint8_t *output, int len);
152
153/*
154 *
155 * Generic cipher key type functions
156 *
157 */
158/*
159 * Max size in bytes of any cipher key that might conceivably be used.
160 *
161 * This value is checked at compile time in crypto.c to make sure
162 * it is always at least EVP_MAX_KEY_LENGTH.
163 *
164 * We define our own value, since this parameter
165 * is used to control the size of static key files.
166 * If the OpenSSL library increases EVP_MAX_KEY_LENGTH,
167 * we don't want our key files to be suddenly rendered
168 * unusable.
169 */
170#define MAX_CIPHER_KEY_LENGTH 64
171
185bool cipher_valid_reason(const char *ciphername, const char **reason);
186
196static inline bool
197cipher_valid(const char *ciphername)
198{
199 const char *reason;
200 return cipher_valid_reason(ciphername, &reason);
201}
202
210static inline bool
211cipher_defined(const char *ciphername)
212{
213 ASSERT(ciphername);
214 return strcmp(ciphername, "none") != 0;
215}
216
229const char *cipher_kt_name(const char *ciphername);
230
239unsigned int cipher_kt_key_size(const char *ciphername);
240
250unsigned int cipher_kt_iv_size(const char *ciphername);
251
259unsigned int cipher_kt_block_size(const char *ciphername);
260
269unsigned int cipher_kt_tag_size(const char *ciphername);
270
274bool cipher_kt_insecure(const char *ciphername);
275
276
284bool cipher_kt_mode_cbc(const char *ciphername);
285
293bool cipher_kt_mode_ofb_cfb(const char *ciphername);
294
302bool cipher_kt_mode_aead(const char *ciphername);
303
304
317
324
334void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphername,
336
346unsigned int cipher_ctx_iv_length(const cipher_ctx_t *ctx);
347
355int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len);
356
364unsigned int cipher_ctx_block_size(const cipher_ctx_t *ctx);
365
375
383bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx);
384
393
401bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx);
402
412int cipher_ctx_reset(cipher_ctx_t *ctx, const uint8_t *iv_buf);
413
424int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len);
425
443int cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, const uint8_t *src, int src_len);
444
455int cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len);
456
470int cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag,
471 size_t tag_len);
472
473
474/*
475 *
476 * Generic message digest information functions
477 *
478 */
479
480/*
481 * Max size in bytes of any HMAC key that might conceivably be used.
482 *
483 * This value is checked at compile time in crypto.c to make sure
484 * it is always at least EVP_MAX_MD_SIZE. We define our own value
485 * for the same reason as above.
486 */
487#define MAX_HMAC_KEY_LENGTH 64
488
495static inline bool
496md_defined(const char *mdname)
497{
498 return strcmp(mdname, "none") != 0;
499}
500
501
509bool md_valid(const char *digest);
510
519const char *md_kt_name(const char *mdname);
520
528unsigned char md_kt_size(const char *mdname);
529
530
531/*
532 *
533 * Generic message digest functions
534 *
535 */
536
537/*
538 * Allocate a new message digest context
539 *
540 * @return a new zeroed MD context
541 */
542md_ctx_t *md_ctx_new(void);
543
544/*
545 * Free an existing, non-null message digest context
546 *
547 * @param ctx Message digest context
548 */
550
557void md_ctx_init(md_ctx_t *ctx, const char *mdname);
558
559/*
560 * Free the given message digest context.
561 *
562 * @param ctx Message digest context
563 */
565
566/*
567 * Returns the size of the message digest output by the given context
568 *
569 * @param ctx Message digest context.
570 *
571 * @return Size of the message digest, or \0 if ctx is NULL.
572 */
573int md_ctx_size(const md_ctx_t *ctx);
574
575/*
576 * Process the given data for use in the message digest.
577 *
578 * @param ctx Message digest context. May not be NULL.
579 * @param src Buffer to digest. May not be NULL.
580 * @param src_len The length of the incoming buffer.
581 */
582void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, size_t src_len);
583
584/*
585 * Output the message digest to the given buffer.
586 *
587 * @param ctx Message digest context. May not be NULL.
588 * @param dst Buffer to write the message digest to. May not be NULL.
589 */
590void md_ctx_final(md_ctx_t *ctx, uint8_t *dst);
591
592
593/*
594 *
595 * Generic HMAC functions
596 *
597 */
598
599/*
600 * Create a new HMAC context
601 *
602 * @return A new HMAC context
603 */
605
606/*
607 * Free an existing HMAC context
608 *
609 * @param ctx HMAC context to free
610 */
612
613/*
614 * Initialises the given HMAC context, using the given digest
615 * and key.
616 *
617 * @param ctx HMAC context to initialise
618 * @param key The key to use for the HMAC
619 * @param mdname message digest name
620 *
621 */
622void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname);
623
624
625/*
626 * Free the given HMAC context.
627 *
628 * @param ctx HMAC context
629 */
631
632/*
633 * Returns the size of the HMAC output by the given HMAC Context
634 *
635 * @param ctx HMAC context.
636 *
637 * @return Size of the HMAC, or \0 if ctx is NULL.
638 */
640
641/*
642 * Resets the given HMAC context, preserving the associated key information
643 *
644 * @param ctx HMAC context. May not be NULL.
645 */
647
648/*
649 * Process the given data for use in the HMAC.
650 *
651 * @param ctx HMAC context. May not be NULL.
652 * @param src The buffer to HMAC. May not be NULL.
653 * @param src_len The length of the incoming buffer.
654 */
655void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len);
656
657/*
658 * Output the HMAC to the given buffer.
659 *
660 * @param ctx HMAC context. May not be NULL.
661 * @param dst buffer to write the HMAC to. May not be NULL.
662 */
663void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst);
664
673const char *translate_cipher_name_from_openvpn(const char *cipher_name);
674
683const char *translate_cipher_name_to_openvpn(const char *cipher_name);
684
685
699bool ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len,
700 uint8_t *output, size_t output_len);
701
702#endif /* CRYPTO_BACKEND_H_ */
Buffer management functions and garbage collection.
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:1763
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:1776
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.
int cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, const uint8_t *src, int src_len)
Updates the given cipher context, encrypting data in the source buffer, and placing any complete bloc...
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 OpenSSL-specific backend interface.
#define ASSERT(x)
Definition error.h:221
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:138