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-2024 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, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
30#ifndef CRYPTO_BACKEND_H_
31#define CRYPTO_BACKEND_H_
32
33#ifdef ENABLE_CRYPTO_OPENSSL
34#include "crypto_openssl.h"
35#endif
36#ifdef ENABLE_CRYPTO_MBEDTLS
37#include "crypto_mbedtls.h"
38#endif
39#include "basic.h"
40#include "buffer.h"
41
42/* TLS uses a tag of 128 bits, let's do the same for OpenVPN */
43#define OPENVPN_AEAD_TAG_LENGTH 16
44
45/* Maximum cipher block size (bytes) */
46#define OPENVPN_MAX_CIPHER_BLOCK_SIZE 32
47
48/* Maximum HMAC digest size (bytes) */
49#define OPENVPN_MAX_HMAC_SIZE 64
50
56
58typedef struct {
59 const char *openvpn_name;
60 const char *lib_name;
62
65extern const size_t cipher_name_translation_table_count;
66
67/*
68 * This routine should have additional OpenSSL crypto library initialisations
69 * used by both crypto and ssl components of OpenVPN.
70 */
71void crypto_init_lib(void);
72
73void crypto_uninit_lib(void);
74
75void crypto_clear_error(void);
76
77/*
78 * Initialise the given named crypto engine.
79 */
80void crypto_init_lib_engine(const char *engine_name);
81
82
88provider_t *crypto_load_provider(const char *provider);
89
95void crypto_unload_provider(const char *provname, provider_t *provider);
96
97#ifdef DMALLOC
98/*
99 * OpenSSL memory debugging. If dmalloc debugging is enabled, tell
100 * OpenSSL to use our private malloc/realloc/free functions so that
101 * we can dispatch them to dmalloc.
102 */
103void crypto_init_dmalloc(void);
104
105#endif /* DMALLOC */
106
107void show_available_ciphers(void);
108
109void show_available_digests(void);
110
111void show_available_engines(void);
112
126bool crypto_pem_encode(const char *name, struct buffer *dst,
127 const struct buffer *src, struct gc_arena *gc);
128
138bool crypto_pem_decode(const char *name, struct buffer *dst,
139 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,
343 const char *ciphername, crypto_operation_t enc);
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,
452 uint8_t *src, int src_len);
453
464int cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len);
465
479int cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len,
480 uint8_t *tag, size_t tag_len);
481
482
483/*
484 *
485 * Generic message digest information functions
486 *
487 */
488
489/*
490 * Max size in bytes of any HMAC key that might conceivably be used.
491 *
492 * This value is checked at compile time in crypto.c to make sure
493 * it is always at least EVP_MAX_MD_SIZE. We define our own value
494 * for the same reason as above.
495 */
496#define MAX_HMAC_KEY_LENGTH 64
497
504static inline bool
505md_defined(const char *mdname)
506{
507 return strcmp(mdname, "none") != 0;
508}
509
510
518bool md_valid(const char *digest);
519
528const char *md_kt_name(const char *mdname);
529
537unsigned char md_kt_size(const char *mdname);
538
539
540/*
541 *
542 * Generic message digest functions
543 *
544 */
545
556int md_full(const char *mdname, const uint8_t *src, int src_len, uint8_t *dst);
557
558/*
559 * Allocate a new message digest context
560 *
561 * @return a new zeroed MD context
562 */
563md_ctx_t *md_ctx_new(void);
564
565/*
566 * Free an existing, non-null message digest context
567 *
568 * @param ctx Message digest context
569 */
571
578void md_ctx_init(md_ctx_t *ctx, const char *mdname);
579
580/*
581 * Free the given message digest context.
582 *
583 * @param ctx Message digest context
584 */
586
587/*
588 * Returns the size of the message digest output by the given context
589 *
590 * @param ctx Message digest context.
591 *
592 * @return Size of the message digest, or \0 if ctx is NULL.
593 */
594int md_ctx_size(const md_ctx_t *ctx);
595
596/*
597 * Process the given data for use in the message digest.
598 *
599 * @param ctx Message digest context. May not be NULL.
600 * @param src Buffer to digest. May not be NULL.
601 * @param src_len The length of the incoming buffer.
602 */
603void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, int src_len);
604
605/*
606 * Output the message digest to the given buffer.
607 *
608 * @param ctx Message digest context. May not be NULL.
609 * @param dst Buffer to write the message digest to. May not be NULL.
610 */
611void md_ctx_final(md_ctx_t *ctx, uint8_t *dst);
612
613
614/*
615 *
616 * Generic HMAC functions
617 *
618 */
619
620/*
621 * Create a new HMAC context
622 *
623 * @return A new HMAC context
624 */
626
627/*
628 * Free an existing HMAC context
629 *
630 * @param ctx HMAC context to free
631 */
633
634/*
635 * Initialises the given HMAC context, using the given digest
636 * and key.
637 *
638 * @param ctx HMAC context to initialise
639 * @param key The key to use for the HMAC
640 * @param mdname message digest name
641 *
642 */
643void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname);
644
645
646/*
647 * Free the given HMAC context.
648 *
649 * @param ctx HMAC context
650 */
652
653/*
654 * Returns the size of the HMAC output by the given HMAC Context
655 *
656 * @param ctx HMAC context.
657 *
658 * @return Size of the HMAC, or \0 if ctx is NULL.
659 */
661
662/*
663 * Resets the given HMAC context, preserving the associated key information
664 *
665 * @param ctx HMAC context. May not be NULL.
666 */
668
669/*
670 * Process the given data for use in the HMAC.
671 *
672 * @param ctx HMAC context. May not be NULL.
673 * @param src The buffer to HMAC. May not be NULL.
674 * @param src_len The length of the incoming buffer.
675 */
676void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len);
677
678/*
679 * Output the HMAC to the given buffer.
680 *
681 * @param ctx HMAC context. May not be NULL.
682 * @param dst buffer to write the HMAC to. May not be NULL.
683 */
684void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst);
685
694const char *translate_cipher_name_from_openvpn(const char *cipher_name);
695
704const char *translate_cipher_name_to_openvpn(const char *cipher_name);
705
706
720bool ssl_tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret,
721 int secret_len, uint8_t *output, int output_len);
722
723#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:1820
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)
bool ssl_tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret, int secret_len, uint8_t *output, int output_len)
Calculates the TLS 1.0-1.1 PRF function.
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:1833
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:195
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
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:117
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
struct gc_arena gc
Definition test_ssl.c:155