mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-19 19:40:56 +00:00
Add mtls and chacha20
This commit is contained in:
1 parent
71abdc1bda
commit
df257a8e8a
9 files changed
+3504
-641
No files matched your search
+145
-43
@@ -1936,6 +1936,116 @@ typedef uint64_t mg_uecc_word_t;
|
||||
|
||||
#endif /* _UECC_TYPES_H_ */
|
||||
// End of uecc BSD-2
|
||||
// portable8439 v1.0.1
|
||||
// Source: https://github.com/DavyLandman/portable8439
|
||||
// Licensed under CC0-1.0
|
||||
// Contains poly1305-donna e6ad6e091d30d7f4ec2d4f978be1fcfcbce72781 (Public
|
||||
// Domain)
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef __PORTABLE_8439_H
|
||||
#define __PORTABLE_8439_H
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// provide your own decl specificier like -DPORTABLE_8439_DECL=ICACHE_RAM_ATTR
|
||||
#ifndef PORTABLE_8439_DECL
|
||||
#define PORTABLE_8439_DECL
|
||||
#endif
|
||||
|
||||
/*
|
||||
This library implements RFC 8439 a.k.a. ChaCha20-Poly1305 AEAD
|
||||
|
||||
You can use this library to avoid attackers mutating or reusing your
|
||||
encrypted messages. This does assume you never reuse a nonce+key pair and,
|
||||
if possible, carefully pick your associated data.
|
||||
*/
|
||||
|
||||
// Make sure we are either nested in C++ or running in a C99+ compiler
|
||||
#if !defined(__cplusplus) && !defined(_MSC_VER) && \
|
||||
(!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
|
||||
#error "C99 or newer required"
|
||||
#endif
|
||||
|
||||
// #if CHAR_BIT > 8
|
||||
// # error "Systems without native octals not suppoted"
|
||||
// #endif
|
||||
|
||||
#if defined(_MSC_VER) || defined(__cplusplus)
|
||||
// add restrict support is possible
|
||||
#if (defined(_MSC_VER) && _MSC_VER >= 1900) || defined(__clang__) || \
|
||||
defined(__GNUC__)
|
||||
#define restrict __restrict
|
||||
#else
|
||||
#define restrict
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define RFC_8439_TAG_SIZE (16)
|
||||
#define RFC_8439_KEY_SIZE (32)
|
||||
#define RFC_8439_NONCE_SIZE (12)
|
||||
|
||||
/*
|
||||
Encrypt/Seal plain text bytes into a cipher text that can only be
|
||||
decrypted by knowing the key, nonce and associated data.
|
||||
|
||||
input:
|
||||
- key: RFC_8439_KEY_SIZE bytes that all parties have agreed
|
||||
upon beforehand
|
||||
- nonce: RFC_8439_NONCE_SIZE bytes that should never be repeated
|
||||
for the same key. A counter or a pseudo-random value are fine.
|
||||
- ad: associated data to include with calculating the tag of the
|
||||
cipher text. Can be null for empty.
|
||||
- plain_text: data to be encrypted, pointer + size should not overlap
|
||||
with cipher_text pointer
|
||||
|
||||
output:
|
||||
- cipher_text: encrypted plain_text with a tag appended. Make sure to
|
||||
allocate at least plain_text_size + RFC_8439_TAG_SIZE
|
||||
|
||||
returns:
|
||||
- size of bytes written to cipher_text, can be -1 if overlapping
|
||||
pointers are passed for plain_text and cipher_text
|
||||
*/
|
||||
PORTABLE_8439_DECL size_t mg_chacha20_poly1305_encrypt(
|
||||
uint8_t *restrict cipher_text, const uint8_t key[RFC_8439_KEY_SIZE],
|
||||
const uint8_t nonce[RFC_8439_NONCE_SIZE], const uint8_t *restrict ad,
|
||||
size_t ad_size, const uint8_t *restrict plain_text, size_t plain_text_size);
|
||||
|
||||
/*
|
||||
Decrypt/unseal cipher text given the right key, nonce, and additional data.
|
||||
|
||||
input:
|
||||
- key: RFC_8439_KEY_SIZE bytes that all parties have agreed
|
||||
upon beforehand
|
||||
- nonce: RFC_8439_NONCE_SIZE bytes that should never be repeated for
|
||||
the same key. A counter or a pseudo-random value are fine.
|
||||
- ad: associated data to include with calculating the tag of the
|
||||
cipher text. Can be null for empty.
|
||||
- cipher_text: encrypted message.
|
||||
|
||||
output:
|
||||
- plain_text: data to be encrypted, pointer + size should not overlap
|
||||
with cipher_text pointer, leave at least enough room for
|
||||
cipher_text_size - RFC_8439_TAG_SIZE
|
||||
|
||||
returns:
|
||||
- size of bytes written to plain_text, -1 signals either:
|
||||
- incorrect key/nonce/ad
|
||||
- corrupted cipher_text
|
||||
- overlapping pointers are passed for plain_text and cipher_text
|
||||
*/
|
||||
PORTABLE_8439_DECL size_t mg_chacha20_poly1305_decrypt(
|
||||
uint8_t *restrict plain_text, const uint8_t key[RFC_8439_KEY_SIZE],
|
||||
const uint8_t nonce[RFC_8439_NONCE_SIZE],
|
||||
const uint8_t *restrict cipher_text, size_t cipher_text_size);
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
struct mg_connection;
|
||||
@@ -2940,49 +3050,6 @@ struct mg_tcpip_driver_tm4c_data {
|
||||
#endif
|
||||
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500
|
||||
|
||||
#undef MG_ENABLE_TCPIP_DRIVER_INIT
|
||||
#define MG_ENABLE_TCPIP_DRIVER_INIT 0
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
|
||||
|
||||
struct mg_tcpip_driver_xmc7_data {
|
||||
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
|
||||
uint8_t phy_addr;
|
||||
};
|
||||
|
||||
#ifndef MG_TCPIP_PHY_ADDR
|
||||
#define MG_TCPIP_PHY_ADDR 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_DRIVER_MDC_CR
|
||||
#define MG_DRIVER_MDC_CR 3
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_xmc7_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
|
||||
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
|
||||
mif_.ip = MG_TCPIP_IP; \
|
||||
mif_.mask = MG_TCPIP_MASK; \
|
||||
mif_.gw = MG_TCPIP_GW; \
|
||||
mif_.driver = &mg_tcpip_driver_xmc7; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
|
||||
|
||||
struct mg_tcpip_driver_xmc_data {
|
||||
@@ -3029,6 +3096,41 @@ struct mg_tcpip_driver_xmc_data {
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
|
||||
|
||||
struct mg_tcpip_driver_xmc7_data {
|
||||
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
|
||||
uint8_t phy_addr;
|
||||
};
|
||||
|
||||
#ifndef MG_TCPIP_PHY_ADDR
|
||||
#define MG_TCPIP_PHY_ADDR 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_DRIVER_MDC_CR
|
||||
#define MG_DRIVER_MDC_CR 3
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_xmc7_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
|
||||
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
|
||||
mif_.ip = MG_TCPIP_IP; \
|
||||
mif_.mask = MG_TCPIP_MASK; \
|
||||
mif_.gw = MG_TCPIP_GW; \
|
||||
mif_.driver = &mg_tcpip_driver_xmc7; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user