Go to the documentation of this file.
35 #if defined(ENABLE_CRYPTO_MBEDTLS)
47 #include <mbedtls/base64.h>
48 #include <mbedtls/des.h>
49 #include <mbedtls/error.h>
50 #include <mbedtls/md5.h>
51 #include <mbedtls/cipher.h>
52 #include <mbedtls/pem.h>
54 #include <mbedtls/entropy.h>
55 #include <mbedtls/ssl.h>
67 msg(
M_WARN,
"Note: mbed TLS hardware crypto engine functionality is not "
76 msg(
M_WARN,
"Note: mbed TLS provider functionality is not available");
108 mbed_log_err(
unsigned int flags,
int errval,
const char *prefix)
113 mbedtls_strerror(errval, errstr,
sizeof(errstr));
117 prefix =
"mbed TLS error";
119 msg(flags,
"%s: %s", prefix, errstr);
131 if (!snprintf(prefix,
sizeof(prefix),
"%s:%d", func, line))
142 crypto_init_dmalloc(
void)
144 msg(
M_ERR,
"Error: dmalloc support is not available for mbed TLS.");
149 {
"BF-CBC",
"BLOWFISH-CBC" },
150 {
"BF-CFB",
"BLOWFISH-CFB64" },
151 {
"CAMELLIA-128-CFB",
"CAMELLIA-128-CFB128" },
152 {
"CAMELLIA-192-CFB",
"CAMELLIA-192-CFB128" },
153 {
"CAMELLIA-256-CFB",
"CAMELLIA-256-CFB128" }
161 const int *ciphers = mbedtls_cipher_list();
164 printf(
"The following ciphers and cipher modes are available for use\n"
165 "with " PACKAGE_NAME ". Each cipher shown below may be used as a\n"
166 "parameter to the --data-ciphers (or --cipher) option. Using a\n"
167 "GCM or CBC mode is recommended. In static key mode only CBC\n"
168 "mode is allowed.\n\n");
171 while (*ciphers != 0)
173 const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type(*ciphers);
183 printf(
"\nThe following ciphers have a block size of less than 128 bits, \n"
184 "and are therefore deprecated. Do not use unless you have to.\n\n");
185 ciphers = mbedtls_cipher_list();
186 while (*ciphers != 0)
188 const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type(*ciphers);
203 const int *digests = mbedtls_md_list();
206 printf(
"The following message digests are available for use with\n"
207 PACKAGE_NAME ". A message digest is used in conjunction with\n"
208 "the HMAC function, to authenticate received packets.\n"
209 "You can specify a message digest as parameter to\n"
210 "the --auth option.\n\n");
213 while (*digests != 0)
215 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*digests);
219 printf(
"%s %d bit default key\n", mbedtls_md_get_name(info),
220 mbedtls_md_get_size(info) * 8);
230 printf(
"Sorry, mbed TLS hardware crypto engine functionality is not "
239 char header[1000+1] = { 0 };
240 char footer[1000+1] = { 0 };
242 if (!snprintf(header,
sizeof(header),
"-----BEGIN %s-----\n", name))
246 if (!snprintf(footer,
sizeof(footer),
"-----END %s-----\n", name))
252 if (MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL !=
253 mbedtls_pem_write_buffer(header, footer,
BPTR(src),
BLEN(src),
262 if (!
mbed_ok(mbedtls_pem_write_buffer(header, footer,
BPTR(src),
BLEN(src),
278 char header[1000+1] = { 0 };
279 char footer[1000+1] = { 0 };
281 if (!snprintf(header,
sizeof(header),
"-----BEGIN %s-----", name))
285 if (!snprintf(footer,
sizeof(footer),
"-----END %s-----", name))
298 mbedtls_pem_context ctx = { 0 };
299 bool ret =
mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer,
BPTR(&input),
303 if (ret && !
buf_write(dst, buf, buf_size))
306 msg(
M_WARN,
"PEM decode error: destination buffer too small");
309 mbedtls_pem_free(&ctx);
327 mbedtls_ctr_drbg_context *
330 static mbedtls_entropy_context ec = {0};
331 static mbedtls_ctr_drbg_context cd_ctx = {0};
332 static bool rand_initialised =
false;
334 if (!rand_initialised)
347 mbedtls_entropy_init(&ec);
349 mbedtls_ctr_drbg_init(&cd_ctx);
350 if (!
mbed_ok(mbedtls_ctr_drbg_seed(&cd_ctx, mbedtls_entropy_func, &ec,
351 BPTR(&pers_string),
BLEN(&pers_string))))
353 msg(
M_FATAL,
"Failed to initialize random generator");
357 rand_initialised =
true;
363 #ifdef ENABLE_PREDICTION_RESISTANCE
365 rand_ctx_enable_prediction_resistance(
void)
369 mbedtls_ctr_drbg_set_prediction_resistance(cd_ctx, 1);
380 const size_t blen =
min_int(
len, MBEDTLS_CTR_DRBG_MAX_REQUEST);
381 if (0 != mbedtls_ctr_drbg_random(rng_ctx, output, blen))
398 static const mbedtls_cipher_info_t *
403 const mbedtls_cipher_info_t *cipher = NULL;
406 cipher = mbedtls_cipher_info_from_string(ciphername);
415 const mbedtls_cipher_info_t *cipher =
cipher_get(ciphername);
419 msg(
D_LOW,
"Cipher algorithm '%s' not found", ciphername);
420 *reason =
"disabled because unknown";
427 msg(
D_LOW,
"Cipher algorithm '%s' uses a default key size (%zu bytes) "
428 "which is larger than " PACKAGE_NAME "'s current maximum key size "
430 *reason =
"disabled due to key size too large";
441 const mbedtls_cipher_info_t *cipher_kt =
cipher_get(ciphername);
442 if (NULL == cipher_kt)
444 return "[null-cipher]";
453 const mbedtls_cipher_info_t *cipher_kt =
cipher_get(ciphername);
455 if (NULL == cipher_kt)
466 const mbedtls_cipher_info_t *cipher_kt =
cipher_get(ciphername);
468 if (NULL == cipher_kt)
478 const mbedtls_cipher_info_t *cipher_kt =
cipher_get(ciphername);
479 if (NULL == cipher_kt)
499 const mbedtls_cipher_info_t *cipher_kt =
cipher_get(ciphername);
506 #ifdef MBEDTLS_CHACHAPOLY_C
512 static mbedtls_cipher_mode_t
515 ASSERT(NULL != cipher_kt);
522 const mbedtls_cipher_info_t *cipher =
cipher_get(ciphername);
529 const mbedtls_cipher_info_t *cipher =
cipher_get(ciphername);
537 const mbedtls_cipher_info_t *cipher =
cipher_get(ciphername);
539 #ifdef MBEDTLS_CHACHAPOLY_C
552 mbedtls_cipher_context_t *
555 mbedtls_cipher_context_t *ctx;
556 ALLOC_OBJ(ctx, mbedtls_cipher_context_t);
563 mbedtls_cipher_free(ctx);
571 ASSERT(NULL != ciphername && NULL != ctx);
574 const mbedtls_cipher_info_t *kt =
cipher_get(ciphername);
578 if (!
mbed_ok(mbedtls_cipher_setup(ctx, kt)))
580 msg(
M_FATAL,
"mbed TLS cipher context init #1");
583 if (!
mbed_ok(mbedtls_cipher_setkey(ctx,
key, (
int)key_bitlen, enc)))
590 if (!
mbed_ok(mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7)))
592 msg(
M_FATAL,
"mbed TLS cipher set padding mode");
597 ASSERT(mbedtls_cipher_get_key_bitlen(ctx) <= key_bitlen);
603 return mbedtls_cipher_get_iv_size(ctx);
609 if (tag_len > SIZE_MAX)
614 if (!
mbed_ok(mbedtls_cipher_write_tag(ctx, (
unsigned char *) tag, tag_len)))
625 return (
int)mbedtls_cipher_get_block_size(ctx);
633 return mbedtls_cipher_get_cipher_mode(ctx);
654 #ifdef MBEDTLS_CHACHAPOLY_C
663 if (!
mbed_ok(mbedtls_cipher_reset(ctx)))
668 if (!
mbed_ok(mbedtls_cipher_set_iv(ctx, iv_buf, (
size_t)mbedtls_cipher_get_iv_size(ctx))))
679 if (src_len > SIZE_MAX)
684 if (!
mbed_ok(mbedtls_cipher_update_ad(ctx, src, src_len)))
694 int *dst_len, uint8_t *src,
int src_len)
696 size_t s_dst_len = *dst_len;
698 if (!
mbed_ok(mbedtls_cipher_update(ctx, src, (
size_t) src_len, dst,
704 *dst_len = s_dst_len;
712 size_t s_dst_len = *dst_len;
714 if (!
mbed_ok(mbedtls_cipher_finish(ctx, dst, &s_dst_len)))
719 *dst_len = s_dst_len;
726 int *dst_len, uint8_t *tag,
size_t tag_len)
730 if (MBEDTLS_DECRYPT != mbedtls_cipher_get_operation(ctx))
735 if (tag_len > SIZE_MAX)
740 if (!
mbed_ok(mbedtls_cipher_finish(ctx, dst, &olen)))
752 if (!
mbed_ok(mbedtls_cipher_check_tag(ctx, (
const unsigned char *) tag,
770 static const mbedtls_md_info_t *
771 md_get(
const char *digest)
773 const mbedtls_md_info_t *md = NULL;
776 md = mbedtls_md_info_from_string(digest);
779 msg(
M_FATAL,
"Message hash algorithm '%s' not found", digest);
783 msg(
M_FATAL,
"Message hash algorithm '%s' uses a default hash size (%d bytes) which is larger than " PACKAGE_NAME "'s current maximum hash size (%d bytes)",
785 mbedtls_md_get_size(md),
794 const mbedtls_md_info_t *md = mbedtls_md_info_from_string(digest);
801 if (!strcmp(
"none", mdname))
803 return "[null-digest]";
805 const mbedtls_md_info_t *kt =
md_get(mdname);
806 return mbedtls_md_get_name(kt);
812 if (!strcmp(
"none", mdname))
816 const mbedtls_md_info_t *kt =
md_get(mdname);
817 return mbedtls_md_get_size(kt);
827 md_full(
const char *mdname,
const uint8_t *src,
int src_len, uint8_t *dst)
829 const mbedtls_md_info_t *kt =
md_get(mdname);
830 return 0 == mbedtls_md(kt, src, src_len, dst);
833 mbedtls_md_context_t *
836 mbedtls_md_context_t *ctx;
848 md_ctx_init(mbedtls_md_context_t *ctx,
const char *mdname)
850 const mbedtls_md_info_t *kt =
md_get(mdname);
851 ASSERT(NULL != ctx && NULL != kt);
853 mbedtls_md_init(ctx);
854 ASSERT(0 == mbedtls_md_setup(ctx, kt, 0));
855 ASSERT(0 == mbedtls_md_starts(ctx));
861 mbedtls_md_free(ctx);
875 md_ctx_update(mbedtls_md_context_t *ctx,
const uint8_t *src,
int src_len)
877 ASSERT(0 == mbedtls_md_update(ctx, src, src_len));
883 ASSERT(0 == mbedtls_md_finish(ctx, dst));
884 mbedtls_md_free(ctx);
899 mbedtls_md_context_t *
902 mbedtls_md_context_t *ctx;
914 hmac_ctx_init(mbedtls_md_context_t *ctx,
const uint8_t *
key,
const char *mdname)
916 const mbedtls_md_info_t *kt =
md_get(mdname);
917 ASSERT(NULL != kt && NULL != ctx);
919 mbedtls_md_init(ctx);
920 int key_len = mbedtls_md_get_size(kt);
921 ASSERT(0 == mbedtls_md_setup(ctx, kt, 1));
922 ASSERT(0 == mbedtls_md_hmac_starts(ctx,
key, key_len));
925 ASSERT(mbedtls_md_get_size(kt) <= key_len);
931 mbedtls_md_free(ctx);
947 ASSERT(0 == mbedtls_md_hmac_reset(ctx));
951 hmac_ctx_update(mbedtls_md_context_t *ctx,
const uint8_t *src,
int src_len)
953 ASSERT(0 == mbedtls_md_hmac_update(ctx, src, src_len));
959 ASSERT(0 == mbedtls_md_hmac_finish(ctx, dst));
969 volatile const unsigned char *A = (
volatile const unsigned char *) a;
970 volatile const unsigned char *B = (
volatile const unsigned char *) b;
971 volatile unsigned char diff = 0;
973 for (
size_t i = 0; i < size; i++)
975 unsigned char x = A[i], y = B[i];
983 #if HAVE_MBEDTLS_SSL_TLS_PRF && defined(MBEDTLS_SSL_TLS_PRF_TLS1)
985 ssl_tls1_PRF(
const uint8_t *seed,
int seed_len,
const uint8_t *secret,
986 int secret_len, uint8_t *output,
int output_len)
988 return mbed_ok(mbedtls_ssl_tls_prf(MBEDTLS_SSL_TLS_PRF_TLS1, secret,
989 secret_len,
"", seed, seed_len, output,
1005 tls1_P_hash(
const mbedtls_md_info_t *md_kt,
const uint8_t *sec,
int sec_len,
1006 const uint8_t *seed,
int seed_len, uint8_t *out,
int olen)
1014 const int olen_orig = olen;
1015 const uint8_t *out_orig = out;
1024 int chunk = mbedtls_md_get_size(md_kt);
1025 unsigned int A1_len = mbedtls_md_get_size(md_kt);
1029 mbedtls_md_init(ctx);
1030 ASSERT(0 == mbedtls_md_setup(ctx, md_kt, 1));
1031 ASSERT(0 == mbedtls_md_hmac_starts(ctx, sec, sec_len));
1033 mbedtls_md_init(ctx_tmp);
1034 ASSERT(0 == mbedtls_md_setup(ctx_tmp, md_kt, 1));
1035 ASSERT(0 == mbedtls_md_hmac_starts(ctx_tmp, sec, sec_len));
1058 memcpy(out, A1, olen);
1092 ssl_tls1_PRF(
const uint8_t *label,
int label_len,
const uint8_t *sec,
1093 int slen, uint8_t *out1,
int olen)
1099 uint8_t *out2 = (uint8_t *)
gc_malloc(olen,
false, &gc);
1102 const uint8_t *S1 = sec;
1103 const uint8_t *S2 = &(sec[len]);
1106 tls1_P_hash(md5, S1, len, label, label_len, out1, olen);
1107 tls1_P_hash(sha1, S2, len, label, label_len, out2, olen);
1109 for (
int i = 0; i<olen; i++)
int md_full(const char *mdname, const uint8_t *src, int src_len, uint8_t *dst)
Calculates the message digest for the given buffer.
cipher_ctx_t * cipher_ctx_new(void)
Generic cipher functions.
void crypto_uninit_lib(void)
void hmac_ctx_cleanup(hmac_ctx_t *ctx)
#define OPENVPN_MODE_CBC
Cipher is in CBC mode.
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.
static struct gc_arena gc_new(void)
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
mbedtls_ctr_drbg_context * rand_ctx_get(void)
Returns a singleton instance of the mbed TLS random number generator.
int len
Length in bytes of the actual content within the allocated memory.
#define OPENVPN_MODE_OFB
Cipher is in OFB mode.
const char * translate_cipher_name_to_openvpn(const char *cipher_name)
Translate a crypto library cipher name to an OpenVPN cipher name.
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.
#define OPENVPN_MODE_CFB
Cipher is in CFB mode.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
void buf_null_terminate(struct buffer *buf)
void print_cipher(const char *ciphername)
Print a cipher list entry.
void show_available_ciphers(void)
int md_ctx_size(const md_ctx_t *ctx)
bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported CBC mode cipher.
static bool buf_copy(struct buffer *dest, const struct buffer *src)
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
const char * translate_cipher_name_from_openvpn(const char *cipher_name)
Translate an OpenVPN cipher name to a crypto library cipher name.
const char * cipher_kt_name(const char *ciphername)
Retrieve a normalised string describing the cipher (e.g.
#define mbed_ok(errval)
Check errval and log on error.
#define MAX_CIPHER_KEY_LENGTH
md_ctx_t * md_ctx_new(void)
bool cipher_ctx_mode_ofb_cfb(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
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.
Container for unidirectional cipher and HMAC key material.
static const mbedtls_md_info_t * mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
static size_t mbedtls_cipher_info_get_key_bitlen(const mbedtls_cipher_info_t *cipher)
void cipher_ctx_free(cipher_ctx_t *ctx)
Cleanup and free a cipher context.
int cipher_kt_block_size(const char *ciphername)
Returns the block size of the cipher, in bytes.
const cipher_name_pair cipher_name_translation_table[]
Cipher name translation table.
void md_ctx_final(md_ctx_t *ctx, uint8_t *dst)
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
#define MAX_HMAC_KEY_LENGTH
static const char * mbedtls_cipher_info_get_name(const mbedtls_cipher_info_t *cipher)
const char * md_kt_name(const char *mdname)
Retrieve a string describing the digest digest (e.g.
static bool buf_inc_len(struct buffer *buf, int inc)
#define OPENVPN_AEAD_TAG_LENGTH
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.
static mbedtls_cipher_type_t mbedtls_cipher_info_get_type(const mbedtls_cipher_info_t *cipher)
#define ALLOC_OBJ(dptr, type)
mbedtls_cipher_context_t cipher_ctx_t
Generic cipher context.
void md_ctx_cleanup(md_ctx_t *ctx)
int cipher_kt_mode(const EVP_CIPHER *cipher_kt)
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...
static const unsigned char * mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size)
void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *cipername, crypto_operation_t enc)
Initialise a cipher context, based on the given key and key type.
static evp_md_type * md_get(const char *digest)
void crypto_init_lib(void)
bool mbed_log_func_line(unsigned int flags, int errval, const char *func, int line)
Log the supplied mbed TLS error, prefixed by function name and line number.
void crypto_clear_error(void)
int cipher_ctx_mode(const cipher_ctx_t *ctx)
Returns the mode that the cipher runs in.
unsigned char md_kt_size(const char *mdname)
Returns the size of the message digest, 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...
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
void crypto_unload_provider(const char *provname, provider_t *provider)
Unloads the given (OpenSSL) provider.
bool cipher_kt_mode_aead(const char *ciphername)
Check if the supplied cipher is a supported AEAD mode cipher.
bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported AEAD mode cipher.
const size_t cipher_name_translation_table_count
Wrapper structure for dynamically allocated memory.
void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, int src_len)
void md_ctx_init(md_ctx_t *ctx, const char *mdname)
Initialises the given message digest context.
void hmac_ctx_reset(hmac_ctx_t *ctx)
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
void hmac_ctx_free(hmac_ctx_t *ctx)
Struct used in cipher name translation table.
void show_available_engines(void)
static bool buf_write(struct buffer *dest, const void *src, size_t size)
int cipher_ctx_block_size(const cipher_ctx_t *ctx)
Returns the block size of the cipher, in bytes.
static size_t mbedtls_cipher_info_get_block_size(const mbedtls_cipher_info_t *cipher)
mbedtls_md_info_t md_kt_t
Generic message digest key type context.
void show_available_digests(void)
bool cipher_kt_mode_ofb_cfb(const char *ciphername)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
bool cipher_kt_insecure(const char *ciphername)
Returns true if we consider this cipher to be insecure.
Garbage collection arena used to keep track of dynamically allocated memory.
bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src, struct gc_arena *gc)
Encode binary data as PEM.
#define OPENVPN_MODE_GCM
Cipher is in GCM mode.
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.
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
static size_t mbedtls_cipher_info_get_iv_size(const mbedtls_cipher_info_t *cipher)
static evp_cipher_type * cipher_get(const char *ciphername)
int cipher_kt_tag_size(const char *ciphername)
Returns the MAC tag size of the cipher, in bytes.
void md_ctx_free(md_ctx_t *ctx)
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
#define D_SHOW_KEY_SOURCE
bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
Decode a PEM buffer to binary data.
static int min_int(int x, int y)
static void gc_free(struct gc_arena *a)
provider_t * crypto_load_provider(const char *provider)
Load the given (OpenSSL) providers.
bool cipher_kt_mode_cbc(const char *ciphername)
Check if the supplied cipher is a supported CBC mode cipher.
#define ALLOC_OBJ_CLEAR(dptr, type)
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.
hmac_ctx_t * hmac_ctx_new(void)
bool mbed_log_err(unsigned int flags, int errval, const char *prefix)
Log the supplied mbed TLS error, prefixed by supplied prefix.
const char * time_string(time_t t, int usec, bool show_usec, struct gc_arena *gc)
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
void crypto_init_lib_engine(const char *engine_name)
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.
bool buf_printf(struct buffer *buf, const char *format,...)
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.
mbedtls_operation_t crypto_operation_t
static mbedtls_cipher_mode_t mbedtls_cipher_info_get_mode(const mbedtls_cipher_info_t *cipher)
int hmac_ctx_size(hmac_ctx_t *ctx)