66#include "mbedtls/net_sockets.h"
67#include "mbedtls/platform.h"
68#include "mbedtls/private/ctr_drbg.h"
69#include "mbedtls/private/entropy.h"
70#include "mbedtls/private/sha256.h"
71#include "mbedtls/ssl.h"
72#include "mbedtls/x509_crt.h"
75#include "psa/crypto.h"
87typedef enum : uint32_t {
114typedef enum : uint8_t {
125typedef enum : uint8_t {
136typedef enum : uint8_t {
146typedef enum : uint16_t {
159typedef enum : uint8_t {
165typedef enum : uint8_t {
174static const uint8_t
s_demo_mac[6] = {0x02U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U};
220 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
221 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
222 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
225#ifndef RA8_OFF_TARGET
268 __asm__
volatile(
"wfi");
287 uint32_t cpuclk0_hz = 0U;
328 if (s == (
const char*)0) {
350 if (buf ==
nullptr || len == 0U) {
356#ifndef RA8_OFF_TARGET
426 if (s != NX_SUCCESS) {
432 s = nx_ip_create(&
s_ip,
441 if (s != NX_SUCCESS) {
448 (void)nx_ip_interface_physical_address_set(&
s_ip, 0U, msw, lsw, NX_TRUE);
454 if (s != NX_SUCCESS) {
457 s = nx_tcp_enable(&
s_ip);
458 if (s != NX_SUCCESS) {
461 (void)nx_icmp_enable(&
s_ip);
483 UINT s = nx_tcp_socket_create(&
s_ip,
492 if (s != NX_SUCCESS) {
495 s = nx_tcp_client_socket_bind(&
s_tls_socket, NX_ANY_PORT, NX_WAIT_FOREVER);
496 if (s != NX_SUCCESS) {
527 if (buf ==
nullptr || len == 0U) {
531 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
534 NX_PACKET* pkt = NX_NULL;
535 UINT s = nx_packet_allocate(&
s_packet_pool, &pkt, NX_TCP_PACKET, NX_WAIT_FOREVER);
536 if (s != NX_SUCCESS || pkt == NX_NULL) {
537 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
540 if (s != NX_SUCCESS) {
541 (void)nx_packet_release(pkt);
542 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
545 if (s != NX_SUCCESS) {
546 (void)nx_packet_release(pkt);
547 if (s == NX_NO_PACKET || s == NX_WINDOW_OVERFLOW) {
548 return MBEDTLS_ERR_SSL_WANT_WRITE;
550 return MBEDTLS_ERR_NET_SEND_FAILED;
576 if (buf ==
nullptr || len == 0U) {
579 NX_PACKET* pkt = NX_NULL;
581 if (s == NX_NO_PACKET) {
582 return MBEDTLS_ERR_SSL_WANT_READ;
584 if (s != NX_SUCCESS || pkt == NX_NULL) {
585 return MBEDTLS_ERR_NET_RECV_FAILED;
588 (void)nx_packet_data_retrieve(pkt, (VOID*)buf, &copied);
589 (void)nx_packet_release(pkt);
590 if (copied > (
ULONG)len) {
623 if (buf ==
nullptr || olen ==
nullptr) {
624 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
628 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
634#ifndef DEMO_DISABLE_CERT_PIN
654 const mbedtls_x509_crt* peer = mbedtls_ssl_get_peer_cert(&
s_ssl);
655 if (peer ==
nullptr) {
685 size_t request_len = 0U;
686 const char* request_parts[] = {
687 "GET / HTTP/1.1\r\nHost: ",
689 "\r\nConnection: close\r\nUser-Agent: ra8d2-https-client/0.1\r\n\r\n",
692 for (
size_t i = 0U; i < (
sizeof(request_parts) /
sizeof(request_parts[0])); ++i) {
693 const size_t part_len =
strlen(request_parts[i]);
698 request_len += part_len;
701 if (request_len == 0U) {
705 while (written < request_len) {
707 if (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE) {
713 written += (size_t)rc;
718 uint32_t body_dumped = 0U;
719 uint8_t saw_header_end = 0U;
723 if (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE) {
726 if (rc == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY || rc == 0) {
733 if (saw_header_end == 0U) {
735 for (
int i = 0; i < rc; i++) {
750 if (saw_header_end == 0U) {
754 int avail = rc - start;
755 uint32_t take = (uint32_t)avail;
782 mbedtls_ssl_init(&
s_ssl);
796 if (mbedtls_ssl_config_defaults(&
s_ssl_cfg,
797 MBEDTLS_SSL_IS_CLIENT,
798 MBEDTLS_SSL_TRANSPORT_STREAM,
799 MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
806 mbedtls_ssl_conf_authmode(&
s_ssl_cfg, MBEDTLS_SSL_VERIFY_OPTIONAL);
823 while ((rc = mbedtls_ssl_handshake(&
s_ssl)) != 0) {
824 if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE) {
828#ifndef DEMO_DISABLE_CERT_PIN
859 if (psa_crypto_init() != PSA_SUCCESS) {
898 if ((n == 0U) || (size == 0U)) {
901 if (n > (SIZE_MAX / size)) {
904 const size_t total = n * size;
909 (void)
memset((
void*)p, 0, total);
930 (void)tx_byte_release(p);
947 (void)first_unused_memory;
955 nx_system_initialize();
982#ifndef RA8_OFF_TARGET
void main(void)
Secure fallback main entry point.
static CHAR s_socket_name[]
static const uint8_t s_demo_ip[4]
IPv4 address: 192.168.1.42 / 255.255.255.0.
static const uint8_t s_demo_host_ip[4]
Static IPv4 for www.example.com (legacy edge-of-net IP).
demo_ip_octet_t
Octet indices into a packed IPv4 address.
@ k_demo_ip_oct_d
Demo IP oct d.
@ k_demo_ip_oct_c
Demo IP oct c.
@ k_demo_ip_oct_b
Demo IP oct b.
@ k_demo_ip_oct_a
Demo IP oct a.
static const char s_demo_host_name[]
HTTP/1.1 Host header literal – matches the IP pin above.
static void internal_demo_pack_mac(ULONG *msw, ULONG *lsw)
Pack the local MAC into the msw/lsw fields NetX expects.
static int internal_demo_tls_session(void)
Run the TLS bring-up: DRBG, config, configured pin policy, GET, dump.
static UINT internal_demo_tcp_connect(void)
Open a NetX Duo TCP socket and connect to www.example.com:443.
static mbedtls_ssl_config s_ssl_cfg
demo_ipv4_t
Demo IPv4 addresses (board, gateway, host) as octet enums.
@ k_demo_ipaddr_0
192.168.1.42 board.
@ k_demo_ipaddr_2
Demo ipaddr 2.
@ k_demo_hostip_3
Demo hostip 3.
@ k_demo_netmask_b
255.255.255.0 (first three octets).
@ k_demo_hostip_1
Demo hostip 1.
@ k_demo_ipaddr_3
Demo ipaddr 3.
@ k_demo_hostip_2
Demo hostip 2.
@ k_demo_hostip_0
93.184.216.34 (www.example.com).
@ k_demo_ipaddr_1
Demo ipaddr 1.
static mbedtls_entropy_context s_entropy
static const uint8_t s_demo_mask[4]
Subnet mask: 255.255.255.0.
static TX_THREAD s_demo_thread
static int internal_demo_http_get(void)
Issue the HTTP GET and dump the first k_demo_dump_bytes to SCI8.
static int internal_demo_entropy_source(void *ctx, unsigned char *buf, size_t len, size_t *olen)
Pull entropy from the RSIP TRNG into Mbed TLS.
static void internal_demo_write_bytes(const uint8_t *buf, uint32_t len)
Write an arbitrary byte buffer to SCI8 (no NUL required).
demo_ip_shift_t
Bit shifts that pack a 4-octet IPv4 into NetX's ULONG.
@ k_demo_ip_shift_a
Demo IP shift a.
@ k_demo_ip_shift_d
Demo IP shift d.
@ k_demo_ip_shift_b
Demo IP shift b.
@ k_demo_ip_shift_c
Demo IP shift c.
http_ascii_t
ASCII control bytes used by the HTTP line scanner.
@ k_ascii_cr
Carriage return.
demo_mac_word_shift_t
Shifts that pack the 6-byte MAC into msw/lsw ULONG pairs.
@ k_demo_mac_lsw_shift_b2
Demo MAC lsw shift b2.
@ k_demo_mac_lsw_shift_b4
Demo MAC lsw shift b4.
@ k_demo_mac_lsw_shift_b3
Demo MAC lsw shift b3.
@ k_demo_mac_lsw_shift_b5
Demo MAC lsw shift b5.
@ k_demo_mac_msw_shift_b0
Demo MAC msw shift b0.
@ k_demo_mac_msw_shift_b1
Demo MAC msw shift b1.
static void internal_demo_print(const char *s)
Convenience wrapper to write a NUL-terminated string to SCI8.
static void internal_demo_panic_halt(void)
Halt forever in WFI.
static const uint8_t s_demo_cert_pin_sha256[32]
SHA-256 of the expected leaf certificate DER, compile-time pinned.
static int internal_demo_bio_send(void *ctx, const unsigned char *buf, size_t len)
Mbed TLS BIO send callback bound to nx_tcp_socket_send.
static UINT internal_demo_netx_bring_up(void)
Bring NetX Duo up: pool, IP, ARP, TCP, ICMP.
static int internal_demo_verify_cert_pin(void)
Verify the peer's leaf certificate matches our compile-time pin.
static ULONG s_ip_stack[k_demo_ip_stack/sizeof(ULONG)]
demo_mac_idx_t
MAC-address byte indices.
@ k_demo_mac_idx_0
Demo MAC index 0.
@ k_demo_mac_idx_3
Demo MAC index 3.
@ k_demo_mac_idx_5
Demo MAC index 5.
@ k_demo_mac_idx_2
Demo MAC index 2.
@ k_demo_mac_idx_1
Demo MAC index 1.
@ k_demo_mac_idx_4
Demo MAC index 4.
static NX_PACKET_POOL s_packet_pool
static mbedtls_ssl_context s_ssl
static void internal_demo_free(void *p)
mbedtls_free hook backed by the ThreadX byte pool.
demo_config_t
Compile-time settings for the HTTPS client demo.
@ k_demo_log_buf_bytes
Demo log buffer bytes.
@ k_demo_recv_timeout
Ticks; ~2 s at TX_TIMER_TICKS_PER_SECOND=100.
@ k_demo_socket_ttl
Demo socket ttl.
@ k_demo_app_thread_pri
Demo app thread priority.
@ k_demo_thread_stack
Demo thread stack.
@ k_demo_handshake_max
Max ticks waiting for handshake.
@ k_demo_https_port
Demo HTTPS port.
@ k_demo_packet_count
Demo packet count.
@ k_demo_dump_bytes
First 1 KiB of body to SCI8.
@ k_demo_ip_thread_pri
Demo IP thread priority.
@ k_demo_packet_size
Demo packet size.
@ k_demo_byte_pool_size
Demo byte pool size.
@ k_demo_arp_cache
Demo arp cache.
@ k_demo_pool_bytes
Demo pool bytes.
@ k_demo_drbg_seed_len
Bytes pulled from RSIP TRNG.
@ k_demo_response_buf
Demo response buffer.
@ k_demo_request_buf
Demo request buffer.
@ k_demo_recv_window
Demo recv window.
@ k_demo_ip_stack
Demo IP stack.
static ULONG internal_demo_pack_ip(const uint8_t *octets)
Pack a 4-octet IPv4 array into NetX's host-order ULONG.
static const uint8_t s_demo_gw[4]
Default gateway: 192.168.1.1.
static void internal_demo_thread_entry(ULONG thread_input)
ThreadX worker entry: bring NetX up, run TLS session, dump body.
static mbedtls_ctr_drbg_context s_drbg
static UCHAR s_byte_pool_memory[k_demo_byte_pool_size]
static UCHAR s_tls_send_staging[k_demo_pool_bytes]
static UCHAR s_demo_stack[k_demo_thread_stack]
static UCHAR s_response_buf[k_demo_response_buf]
static TX_BYTE_POOL s_byte_pool
static int internal_demo_bio_recv(void *ctx, unsigned char *buf, size_t len)
Mbed TLS BIO recv callback bound to nx_tcp_socket_receive.
static NX_TCP_SOCKET s_tls_socket
static void internal_demo_setup_or_halt(void)
Bring CGC + the J-Link OB VCOM console + on-board Ethernet + RSIP up.
static void * internal_demo_calloc(size_t n, size_t size)
mbedtls_calloc hook backed by the ThreadX byte pool.
static UCHAR s_request_buf[k_demo_request_buf]
static ULONG s_arp_cache[k_demo_arp_cache/sizeof(ULONG)]
static UCHAR s_pool_memory[k_demo_pool_bytes]
static const uint8_t s_demo_mac[6]
void tx_application_define(void *first_unused_memory)
ThreadX system-define hook: build worker thread + byte pool.
static CHAR s_packet_pool_name[]
Persistent mutable names retained by NetX Duo and ThreadX objects.
static CHAR s_thread_name[]
static CHAR s_byte_pool_name[]
NetX Duo network driver shim that bridges onto the RA8 ra8_eth HAL.
void nx_ether_driver_ra8_eth(NX_IP_DRIVER *driver_req)
NetX Duo network-driver entry point bridging onto ra8_eth_*.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_ethernet_init(void)
Bring up the on-board RGMII Ethernet PHY and RMAC1/ETHA1.
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
Boot entry points shared between a vector table and its startup code.
High-level Clock Generation Circuit driver.
ra8_err_t ra8_cgc_get_clock_hz(ra8_clock_id_t id, uint32_t *out_hz)
Query the current frequency of a clock-tree domain.
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Ethernet Switch Module (ESWM) + frame TX/RX driver.
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
size_t strlen(const char *s)
Calculate string length.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Renesas Secure IP (RSIP-E50D) HAL driver – public API.
ra8_err_t ra8_rsip_init(const ra8_rsip_config_t *cfg)
Power on the RSIP engine and (optionally) run BIST.
ra8_err_t ra8_rsip_trng_read(uint8_t *buf, uint32_t len)
Drain len bytes from the RSIP true RNG – fail-closed, no backend.
ra8_err_t ra8_rsip_sha256(const uint8_t *msg, uint32_t msg_len, uint8_t *digest)
Compute SHA-256 of an in-memory buffer.
SysTick-based tick counter, delay and timestamp helpers.
ra8_err_t ra8_time_init(uint32_t cpu_hz)
Initialise SysTick for a 1 kHz tick interrupt.
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
char CHAR
ThreadX-compatible CHAR (host stub).
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
Opaque thread stand-in for the host build.
Initial configuration for ra8_rsip_init.