1 #ifndef EVE_TLS_H_INCLUDED 2 #define EVE_TLS_H_INCLUDED 42 #include <mbedtls/config.h> 43 #include <mbedtls/net.h> 44 #include <mbedtls/ssl.h> 45 #include <mbedtls/entropy.h> 46 #include <mbedtls/x509_crt.h> 47 #include <mbedtls/ctr_drbg.h> 48 #include <mbedtls/debug.h> 67 #define TLS_DEBUG_LEVEL DEBUG_MSG 69 #if TLS_DEBUG_LEVEL == 0 70 #define TLS_PRINTD(level, FORMAT, args...) do {} while (0) 72 #define TLS_PRINTD(level, FORMAT, args...) \ 73 do { if ((level) <= TLS_DEBUG_LEVEL) printf("[TLS] " FORMAT, ##args); } while(0) 76 #define TLS_CHECK_RET(ret, string) \ 77 do { if (ret) { TLS_PRINTD(1, string " FAILED with error %s0x%04X\n", \ 78 ((ret)<0) ? "-" : "", ((ret) < 0) ? -(ret) : (ret)); goto exit; } } while(0) 91 mbedtls_ctr_drbg_context
Drbg;
95 mbedtls_pk_context
Pk;
136 const uint8_t* Cert,
int CertLen,
137 const uint8_t* Key,
int KeyLen);
151 const uint8_t* Psk,
int PskLen,
152 const uint8_t* Identity,
int IdentityLen);
246 int TlsSend(
tls_conn_t* TlsConn,
const uint8_t* Buffer,
size_t Length, uint32_t Timeout);
253 enum tls_swap_direction_t
259 enum tls_swap_target_t
265 struct tls_swap_params_t
267 enum tls_swap_direction_t Dir;
268 enum tls_swap_target_t Target;
274 extern bool TlsSwapCallback(
const struct tls_swap_params_t *Params);
276 bool TlsRxBufferHeapSwapOut(
const struct md_heap_t *RxHeap, uint32_t Pos,
const uint8_t **DataPtr, uint32_t Size);
277 bool TlsRxBufferHeapSwapIn(
const struct md_heap_t *RxHeap, uint8_t **DataPtr, uint32_t Pos, uint32_t Size);
278 bool TlsTxBufferHeapSwapOut(
const struct md_heap_t *TxHeap, uint32_t Pos,
const uint8_t **DataPtr, uint32_t Size);
279 bool TlsTxBufferHeapSwapIn(
const struct md_heap_t *TxHeap, uint8_t **DataPtr, uint32_t Pos, uint32_t Size);
282 #ifndef TLS_HEAP_AUX_NUM_ENTRIES 283 #define TLS_HEAP_AUX_NUM_ENTRIES(NumRsaConfigs, NumPskConfigs, NumSockets) \ 284 (MAX(80, ((NumPskConfigs) * 7 + (NumRsaConfigs) * 30)) + 16 * (NumSockets)) 288 #ifndef TLS_HEAP_AUX_RAM_BUFFER_SIZE 289 #define TLS_HEAP_AUX_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets) \ 290 (512 + 1600 + 1000 * (NumPskConfigs) + 3600 * (NumRsaConfigs) + 2200 * (NumSockets)) 293 #define TLS_HEAP_NUM_ENTRIES(NumRsaConfigs, NumPskConfigs, NumSockets) \ 296 #define TLS_HEAP_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets) \ 297 ((NumSockets) * (1348 + 4) + 4) 299 #define TLS_HEAP_TINY_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets) \ 300 ((NumSockets) * 4 + 1348 + 4) 302 #define TLS_HEAP_EXT_RAM_SIZE(NumSockets) \ 303 ((NumSockets) * 1348) 305 #define TLS_HEAP_AUX_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets) \ 306 static struct md_descriptor_t TlsHeapEntries[TLS_HEAP_AUX_NUM_ENTRIES(NumRsaConfigs, NumPskConfigs, NumSockets)]; \ 307 static uint8_t TlsHeapRamBuffer[TLS_HEAP_AUX_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets)];\ 308 static struct md_state_t TlsHeapState; \ 309 const struct md_heap_t TlsHeap; \ 310 static void TlsHeapCtor(void) __attribute__((constructor)); \ 311 static void TlsHeapCtor(void) \ 315 const struct md_heap_t TlsHeap = \ 317 .Entries = TlsHeapEntries, \ 318 .NumEntries = TLS_HEAP_AUX_NUM_ENTRIES(NumRsaConfigs, NumPskConfigs, NumSockets), \ 319 .RamBuffer = TlsHeapRamBuffer, \ 320 .RamBufferSize = TLS_HEAP_AUX_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets), \ 321 .State = &TlsHeapState, \ 327 #define TLS_HEAP_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets, Direction) \ 328 static struct md_descriptor_t Tls ## Direction ## BufferHeapEntries[TLS_HEAP_NUM_ENTRIES(NumRsaConfigs, NumPskConfigs, NumSockets)]; \ 329 static uint8_t Tls ## Direction ## BufferHeapRamBuffer[TLS_HEAP_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets)]; \ 330 static struct md_state_t Tls ## Direction ## BufferHeapState; \ 331 const struct md_heap_t Tls ## Direction ## BufferHeap; \ 332 static void Tls ## Direction ## BufferHeapCtor(void) __attribute__((constructor)); \ 333 static void Tls ## Direction ## BufferHeapCtor(void) \ 335 MemInit(&Tls ## Direction ## BufferHeap); \ 337 const struct md_heap_t Tls ## Direction ## BufferHeap = \ 339 .Entries = Tls ## Direction ## BufferHeapEntries, \ 340 .NumEntries = TLS_HEAP_NUM_ENTRIES(NumRsaConfigs, NumPskConfigs, NumSockets), \ 341 .RamBuffer = Tls ## Direction ## BufferHeapRamBuffer, \ 342 .RamBufferSize = TLS_HEAP_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets), \ 343 .State = &Tls ## Direction ## BufferHeapState, \ 348 #define TLS_HEAP(NumRsaConfigs, NumPskConfigs, NumSockets) \ 349 TLS_HEAP_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets, Rx); \ 350 TLS_HEAP_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets, Tx); \ 351 TLS_HEAP_AUX_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets) 353 #define TLS_TINY_HEAP_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets, Direction) \ 354 static struct md_descriptor_t Tls ## Direction ## BufferHeapEntries[TLS_HEAP_NUM_ENTRIES(NumRsaConfigs, NumPskConfigs, NumSockets)]; \ 355 static uint8_t Tls ## Direction ## BufferHeapRamBuffer[TLS_HEAP_TINY_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets)]; \ 356 static struct md_state_t Tls ## Direction ## BufferHeapState; \ 357 const struct md_heap_t Tls ## Direction ## BufferHeap; \ 358 static void Tls ## Direction ## BufferHeapCtor(void) __attribute__((constructor)); \ 359 static void Tls ## Direction ## BufferHeapCtor(void) \ 361 MemInit(&Tls ## Direction ## BufferHeap); \ 363 const struct md_heap_t Tls ## Direction ## BufferHeap = \ 365 .Entries = Tls ## Direction ## BufferHeapEntries, \ 366 .NumEntries = TLS_HEAP_NUM_ENTRIES(NumRsaConfigs, NumPskConfigs, NumSockets), \ 367 .RamBuffer = Tls ## Direction ## BufferHeapRamBuffer, \ 368 .RamBufferSize = TLS_HEAP_TINY_RAM_BUFFER_SIZE(NumRsaConfigs, NumPskConfigs, NumSockets), \ 369 .State = &Tls ## Direction ## BufferHeapState, \ 370 .ExtRamSize = TLS_HEAP_EXT_RAM_SIZE(NumSockets), \ 371 .SwapOut = Tls ## Direction ## BufferHeapSwapOut, \ 372 .SwapIn = Tls ## Direction ## BufferHeapSwapIn, \ 374 #define TLS_TINY_HEAP(NumRsaConfigs, NumPskConfigs, NumSockets) \ 375 TLS_TINY_HEAP_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets, Rx); \ 376 TLS_TINY_HEAP_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets, Tx); \ 377 TLS_HEAP_AUX_IMPL(NumRsaConfigs, NumPskConfigs, NumSockets) 381 #endif // EVE_TLS_H_INCLUDED
int TlsConnectionInit(tls_conn_t *TlsConn, tls_cfg_t *TlsCfg)
void TlsConnectionFree(tls_conn_t *TlsConn)
int TlsReceive(tls_conn_t *TlsConn, uint8_t *Buffer, size_t Length, uint32_t Timeout)
int TlsSend(tls_conn_t *TlsConn, const uint8_t *Buffer, size_t Length, uint32_t Timeout)
const struct datapump_cfg_t * PumpCfg
void TlsPlatformInit(void)
int TlsHandshake(tls_conn_t *TlsConn, uint32_t Timeout)
void TlsPumpFree(tls_pump_t *TlsPump)
mbedtls_entropy_context Entropy
mbedtls_ctr_drbg_context Drbg
Header file for datapump interface.
int TlsPumpInit(tls_pump_t *TlsPump, tls_cfg_t *TlsCfg, const struct datapump_cfg_t *PumpCfg)
struct tls_cfg_t tls_cfg_t
struct tls_conn_t tls_conn_t
const struct md_heap_t TlsTxBufferHeap
User-defined TLS Tx heap.
int TlsCfgInitPsk(tls_cfg_t *TlsCfg, int Role, const uint8_t *Psk, int PskLen, const uint8_t *Identity, int IdentityLen)
void TlsCfgFree(tls_cfg_t *TlsCfg)
void TlsConnectionClose(tls_conn_t *TlsConn)
int TlsCfgInitRsa(tls_cfg_t *TlsCfg, int Role, const uint8_t *Cert, int CertLen, const uint8_t *Key, int KeyLen)
const struct md_heap_t TlsHeap
User-defined TLS processing.
struct tls_pump_t tls_pump_t
const struct md_heap_t TlsRxBufferHeap
User-defined TLS Rx heap.