Skip to content

Commit 6cb1c3c

Browse files
authored
Merge pull request #2778 from cesanta/tls7
Add mtls and chacha20
2 parents 71abdc1 + df257a8 commit 6cb1c3c

File tree

9 files changed

+3486
-623
lines changed

9 files changed

+3486
-623
lines changed

mongoose.c

Lines changed: 1621 additions & 294 deletions
Large diffs are not rendered by default.

mongoose.h

Lines changed: 136 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,116 @@ typedef uint64_t mg_uecc_word_t;
19361936

19371937
#endif /* _UECC_TYPES_H_ */
19381938
// End of uecc BSD-2
1939+
// portable8439 v1.0.1
1940+
// Source: https://github.com/DavyLandman/portable8439
1941+
// Licensed under CC0-1.0
1942+
// Contains poly1305-donna e6ad6e091d30d7f4ec2d4f978be1fcfcbce72781 (Public
1943+
// Domain)
1944+
1945+
1946+
1947+
1948+
#ifndef __PORTABLE_8439_H
1949+
#define __PORTABLE_8439_H
1950+
#if defined(__cplusplus)
1951+
extern "C" {
1952+
#endif
1953+
1954+
// provide your own decl specificier like -DPORTABLE_8439_DECL=ICACHE_RAM_ATTR
1955+
#ifndef PORTABLE_8439_DECL
1956+
#define PORTABLE_8439_DECL
1957+
#endif
1958+
1959+
/*
1960+
This library implements RFC 8439 a.k.a. ChaCha20-Poly1305 AEAD
1961+
1962+
You can use this library to avoid attackers mutating or reusing your
1963+
encrypted messages. This does assume you never reuse a nonce+key pair and,
1964+
if possible, carefully pick your associated data.
1965+
*/
1966+
1967+
// Make sure we are either nested in C++ or running in a C99+ compiler
1968+
#if !defined(__cplusplus) && !defined(_MSC_VER) && \
1969+
(!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
1970+
#error "C99 or newer required"
1971+
#endif
1972+
1973+
// #if CHAR_BIT > 8
1974+
// # error "Systems without native octals not suppoted"
1975+
// #endif
1976+
1977+
#if defined(_MSC_VER) || defined(__cplusplus)
1978+
// add restrict support is possible
1979+
#if (defined(_MSC_VER) && _MSC_VER >= 1900) || defined(__clang__) || \
1980+
defined(__GNUC__)
1981+
#define restrict __restrict
1982+
#else
1983+
#define restrict
1984+
#endif
1985+
#endif
1986+
1987+
#define RFC_8439_TAG_SIZE (16)
1988+
#define RFC_8439_KEY_SIZE (32)
1989+
#define RFC_8439_NONCE_SIZE (12)
1990+
1991+
/*
1992+
Encrypt/Seal plain text bytes into a cipher text that can only be
1993+
decrypted by knowing the key, nonce and associated data.
1994+
1995+
input:
1996+
- key: RFC_8439_KEY_SIZE bytes that all parties have agreed
1997+
upon beforehand
1998+
- nonce: RFC_8439_NONCE_SIZE bytes that should never be repeated
1999+
for the same key. A counter or a pseudo-random value are fine.
2000+
- ad: associated data to include with calculating the tag of the
2001+
cipher text. Can be null for empty.
2002+
- plain_text: data to be encrypted, pointer + size should not overlap
2003+
with cipher_text pointer
2004+
2005+
output:
2006+
- cipher_text: encrypted plain_text with a tag appended. Make sure to
2007+
allocate at least plain_text_size + RFC_8439_TAG_SIZE
2008+
2009+
returns:
2010+
- size of bytes written to cipher_text, can be -1 if overlapping
2011+
pointers are passed for plain_text and cipher_text
2012+
*/
2013+
PORTABLE_8439_DECL size_t mg_chacha20_poly1305_encrypt(
2014+
uint8_t *restrict cipher_text, const uint8_t key[RFC_8439_KEY_SIZE],
2015+
const uint8_t nonce[RFC_8439_NONCE_SIZE], const uint8_t *restrict ad,
2016+
size_t ad_size, const uint8_t *restrict plain_text, size_t plain_text_size);
2017+
2018+
/*
2019+
Decrypt/unseal cipher text given the right key, nonce, and additional data.
2020+
2021+
input:
2022+
- key: RFC_8439_KEY_SIZE bytes that all parties have agreed
2023+
upon beforehand
2024+
- nonce: RFC_8439_NONCE_SIZE bytes that should never be repeated for
2025+
the same key. A counter or a pseudo-random value are fine.
2026+
- ad: associated data to include with calculating the tag of the
2027+
cipher text. Can be null for empty.
2028+
- cipher_text: encrypted message.
2029+
2030+
output:
2031+
- plain_text: data to be encrypted, pointer + size should not overlap
2032+
with cipher_text pointer, leave at least enough room for
2033+
cipher_text_size - RFC_8439_TAG_SIZE
2034+
2035+
returns:
2036+
- size of bytes written to plain_text, -1 signals either:
2037+
- incorrect key/nonce/ad
2038+
- corrupted cipher_text
2039+
- overlapping pointers are passed for plain_text and cipher_text
2040+
*/
2041+
PORTABLE_8439_DECL size_t mg_chacha20_poly1305_decrypt(
2042+
uint8_t *restrict plain_text, const uint8_t key[RFC_8439_KEY_SIZE],
2043+
const uint8_t nonce[RFC_8439_NONCE_SIZE],
2044+
const uint8_t *restrict cipher_text, size_t cipher_text_size);
2045+
#if defined(__cplusplus)
2046+
}
2047+
#endif
2048+
#endif
19392049

19402050

19412051
struct mg_connection;
@@ -2940,17 +3050,22 @@ struct mg_tcpip_driver_tm4c_data {
29403050
#endif
29413051

29423052

2943-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500
2944-
2945-
#undef MG_ENABLE_TCPIP_DRIVER_INIT
2946-
#define MG_ENABLE_TCPIP_DRIVER_INIT 0
2947-
2948-
#endif
2949-
2950-
2951-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
3053+
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
29523054

2953-
struct mg_tcpip_driver_xmc7_data {
3055+
struct mg_tcpip_driver_xmc_data {
3056+
// 13.2.8.1 Station Management Functions
3057+
// MDC clock divider (). MDC clock is derived from ETH MAC clock
3058+
// It must not exceed 2.5MHz
3059+
// ETH Clock range DIVIDER mdc_cr VALUE
3060+
// --------------------------------------------
3061+
// -1 <-- tell driver to guess the value
3062+
// 60-100 MHz ETH Clock/42 0
3063+
// 100-150 MHz ETH Clock/62 1
3064+
// 20-35 MHz ETH Clock/16 2
3065+
// 35-60 MHz ETH Clock/26 3
3066+
// 150-250 MHz ETH Clock/102 4
3067+
// 250-300 MHz ETH Clock/124 5
3068+
// 110, 111 Reserved
29543069
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
29553070
uint8_t phy_addr;
29563071
};
@@ -2960,45 +3075,31 @@ struct mg_tcpip_driver_xmc7_data {
29603075
#endif
29613076

29623077
#ifndef MG_DRIVER_MDC_CR
2963-
#define MG_DRIVER_MDC_CR 3
3078+
#define MG_DRIVER_MDC_CR 4
29643079
#endif
29653080

29663081
#define MG_TCPIP_DRIVER_INIT(mgr) \
29673082
do { \
2968-
static struct mg_tcpip_driver_xmc7_data driver_data_; \
3083+
static struct mg_tcpip_driver_xmc_data driver_data_; \
29693084
static struct mg_tcpip_if mif_; \
29703085
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
29713086
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
29723087
mif_.ip = MG_TCPIP_IP; \
29733088
mif_.mask = MG_TCPIP_MASK; \
29743089
mif_.gw = MG_TCPIP_GW; \
2975-
mif_.driver = &mg_tcpip_driver_xmc7; \
3090+
mif_.driver = &mg_tcpip_driver_xmc; \
29763091
mif_.driver_data = &driver_data_; \
29773092
MG_SET_MAC_ADDRESS(mif_.mac); \
29783093
mg_tcpip_init(mgr, &mif_); \
2979-
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
3094+
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
29803095
} while (0)
29813096

29823097
#endif
29833098

29843099

3100+
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
29853101

2986-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
2987-
2988-
struct mg_tcpip_driver_xmc_data {
2989-
// 13.2.8.1 Station Management Functions
2990-
// MDC clock divider (). MDC clock is derived from ETH MAC clock
2991-
// It must not exceed 2.5MHz
2992-
// ETH Clock range DIVIDER mdc_cr VALUE
2993-
// --------------------------------------------
2994-
// -1 <-- tell driver to guess the value
2995-
// 60-100 MHz ETH Clock/42 0
2996-
// 100-150 MHz ETH Clock/62 1
2997-
// 20-35 MHz ETH Clock/16 2
2998-
// 35-60 MHz ETH Clock/26 3
2999-
// 150-250 MHz ETH Clock/102 4
3000-
// 250-300 MHz ETH Clock/124 5
3001-
// 110, 111 Reserved
3102+
struct mg_tcpip_driver_xmc7_data {
30023103
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
30033104
uint8_t phy_addr;
30043105
};
@@ -3008,27 +3109,28 @@ struct mg_tcpip_driver_xmc_data {
30083109
#endif
30093110

30103111
#ifndef MG_DRIVER_MDC_CR
3011-
#define MG_DRIVER_MDC_CR 4
3112+
#define MG_DRIVER_MDC_CR 3
30123113
#endif
30133114

30143115
#define MG_TCPIP_DRIVER_INIT(mgr) \
30153116
do { \
3016-
static struct mg_tcpip_driver_xmc_data driver_data_; \
3117+
static struct mg_tcpip_driver_xmc7_data driver_data_; \
30173118
static struct mg_tcpip_if mif_; \
30183119
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
30193120
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
30203121
mif_.ip = MG_TCPIP_IP; \
30213122
mif_.mask = MG_TCPIP_MASK; \
30223123
mif_.gw = MG_TCPIP_GW; \
3023-
mif_.driver = &mg_tcpip_driver_xmc; \
3124+
mif_.driver = &mg_tcpip_driver_xmc7; \
30243125
mif_.driver_data = &driver_data_; \
30253126
MG_SET_MAC_ADDRESS(mif_.mac); \
30263127
mg_tcpip_init(mgr, &mif_); \
3027-
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
3128+
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
30283129
} while (0)
30293130

30303131
#endif
30313132

3133+
30323134
#ifdef __cplusplus
30333135
}
30343136
#endif

src/net.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
8585
if ((str.buf[i] >= '0' && str.buf[i] <= '9') ||
8686
(str.buf[i] >= 'a' && str.buf[i] <= 'f') ||
8787
(str.buf[i] >= 'A' && str.buf[i] <= 'F')) {
88-
unsigned long val; // TODO(): This loops on chars, refactor
88+
unsigned long val = 0; // TODO(): This loops on chars, refactor
8989
if (i > j + 3) return false;
9090
// MG_DEBUG(("%lu %lu [%.*s]", i, j, (int) (i - j + 1), &str.buf[j]));
9191
mg_str_to_num(mg_str_n(&str.buf[j], i - j + 1), 16, &val, sizeof(val));

0 commit comments

Comments
 (0)