Accelerate legacy WSS elliptic-curve operations and time TLS phases
Enable mbedTLS NIST modular reduction and report per-phase handshake timings. Add handshake-loop regression tests and a certificate verification benchmark, and document the full kernel and APP package requirement.
This commit is contained in:
@@ -1350,7 +1350,7 @@ void rws_socket_connect_to_host(_rws_socket *s)
|
||||
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout));
|
||||
}
|
||||
#endif
|
||||
LOG_I("[T:%.8d, L:%.5d, %*.*s] socket break\n", ql_rtos_get_systicks(), __LINE__, DBG_FUNCTION_NUM, DBG_FUNCTION_NUM, __FUNCTION__);
|
||||
LOG_I("[T:%.8d, L:%.5d, %*.*s] TCP connected; leaving connect loop\n", ql_rtos_get_systicks(), __LINE__, DBG_FUNCTION_NUM, DBG_FUNCTION_NUM, __FUNCTION__);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -1849,12 +1849,57 @@ void rws_debug(void *ctx, int level, const char *file, int line, const char *str
|
||||
LOG_I("%s", str ? str : NULL);
|
||||
}
|
||||
|
||||
static const char *rws_tls_phase_name(int state)
|
||||
{
|
||||
switch (state) {
|
||||
case MBEDTLS_SSL_CLIENT_HELLO: return "client hello";
|
||||
case MBEDTLS_SSL_SERVER_HELLO: return "server hello";
|
||||
case MBEDTLS_SSL_SERVER_CERTIFICATE: return "server certificate";
|
||||
case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: return "server key exchange";
|
||||
case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: return "client key exchange";
|
||||
case MBEDTLS_SSL_SERVER_FINISHED: return "server finished";
|
||||
default: return "other";
|
||||
}
|
||||
}
|
||||
|
||||
static int rws_tls_handshake_timed(mbedtls_ssl_context *ctx)
|
||||
{
|
||||
unsigned int started = ql_rtos_get_systicks();
|
||||
unsigned int phase_started = started, call_ticks = 0, retries = 0;
|
||||
int state = ctx->state, ret = 0;
|
||||
while (ctx->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
|
||||
unsigned int before = ql_rtos_get_systicks();
|
||||
ret = mbedtls_ssl_handshake_step(ctx);
|
||||
call_ticks += (unsigned int)(ql_rtos_get_systicks() - before);
|
||||
if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
|
||||
break;
|
||||
if (ret != 0) ++retries;
|
||||
if (ctx->state != state) {
|
||||
LOG_I("WSS TLS phase %s (%d): elapsed=%u ms, calls=%u ms, retries=%u\n",
|
||||
rws_tls_phase_name(state), state,
|
||||
(unsigned int)(ql_rtos_get_systicks() - phase_started) * 5,
|
||||
call_ticks * 5, retries);
|
||||
state = ctx->state;
|
||||
phase_started = ql_rtos_get_systicks();
|
||||
call_ticks = retries = 0;
|
||||
}
|
||||
if (ctx->state == MBEDTLS_SSL_HANDSHAKE_OVER) break;
|
||||
if ((unsigned int)(ql_rtos_get_systicks() - started) >= 4000) {
|
||||
ret = MBEDTLS_ERR_SSL_TIMEOUT; /* 20 seconds, 5 ms SDK ticks */
|
||||
break;
|
||||
}
|
||||
if (ret != 0) rws_thread_sleep(10);
|
||||
}
|
||||
LOG_I("WSS TLS handshake total=%u ms, state=%d, ret=%d\n",
|
||||
(unsigned int)(ql_rtos_get_systicks() - started) * 5, ctx->state, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int rws_ssl_conn(_rws_socket *s)
|
||||
{
|
||||
const char *pers = "talkingq-wss";
|
||||
const char *stage = "allocate TLS context";
|
||||
int ret = -1;
|
||||
unsigned int started;
|
||||
uint32_t flags;
|
||||
_rws_ssl *ssl;
|
||||
|
||||
@@ -1918,15 +1963,8 @@ int rws_ssl_conn(_rws_socket *s)
|
||||
if (ret != 0) goto exit;
|
||||
mbedtls_ssl_set_bio(&ssl->ssl_ctx, &ssl->net_ctx, mbedtls_net_send, mbedtls_net_recv, NULL);
|
||||
stage = "TLS handshake / server certificate";
|
||||
started = ql_rtos_get_systicks();
|
||||
while ((ret = mbedtls_ssl_handshake(&ssl->ssl_ctx)) != 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) goto exit;
|
||||
if ((unsigned int)(ql_rtos_get_systicks() - started) >= 4000) {
|
||||
ret = MBEDTLS_ERR_SSL_TIMEOUT; /* 20 seconds, 5 ms SDK ticks */
|
||||
goto exit;
|
||||
}
|
||||
rws_thread_sleep(10);
|
||||
}
|
||||
ret = rws_tls_handshake_timed(&ssl->ssl_ctx);
|
||||
if (ret != 0) goto exit;
|
||||
flags = mbedtls_ssl_get_verify_result(&ssl->ssl_ctx);
|
||||
if (flags != 0) {
|
||||
LOG_E("WSS certificate verification failed: 0x%08lx\n", (unsigned long)flags);
|
||||
|
||||
Reference in New Issue
Block a user