Hello,
I am using this example for the source of the my main purpose :
https://github.com/straight-coding/LPC407x-NoOS-LWIP-MBEDTLS-HTTPD-KEIL/blo…
This example using https but I'm trying to use this example on Modbus
Server.
This is init function for the server tcp connections:
BOOL
xMBTCPPortInit( USHORT usTCPPort )
{
struct altcp_pcb *pxPCBListenNew, *pxPCBListenOld;
BOOL bOkay = (BOOL)FALSE;
USHORT usPort;
extern struct altcp_tls_config* getTlsConfig(void);
tls_config = getTlsConfig();
mbedtls_ssl_conf_dbg(tls_config, my_debug, NULL);
mbedtls_debug_set_threshold(5);
if( usTCPPort == 0 )
{
usPort = MB_TCP_DEFAULT_PORT;
}
else
{
usPort = ( USHORT ) usTCPPort;
}
if( ( pxPCBListenNew = pxPCBListenOld = altcp_tls_new(
tls_config,IPADDR_TYPE_ANY) ) == NULL )
{
/* Can't create TCP socket. */
bOkay = (BOOL)FALSE;
}
else
if( altcp_bind( pxPCBListenNew, IP_ANY_TYPE, ( u16_t ) usPort ) !=
ERR_OK )
{
/* Bind failed - Maybe illegal port value or in use. */
( void )altcp_close( pxPCBListenOld );
bOkay = (BOOL)FALSE;
}
else if( ( pxPCBListenNew = altcp_listen( pxPCBListenNew ) ) == NULL )
{
( void )altcp_close( pxPCBListenOld );
bOkay = (BOOL)FALSE;
}
else
{
// altcp_tls_new(pxPCBListenNew, IP_GET_TYPE(ip_addr))*/;
/* Register callback function for new clients. */
altcp_accept( pxPCBListenNew, prvxMBTCPPortAccept );
/* Everything okay. Set global variable. */
pxPCBListen = pxPCBListenNew;
#ifdef MB_TCP_DEBUG
vMBPortLog( MB_LOG_DEBUG, "MBTCP-ACCEPT", "Protocol stack
ready.\r\n" );
#endif
SerialPrint("MBTCTP-ACCEPT");
}
bOkay = (BOOL)TRUE;
return bOkay;
}
struct altcp_tls_config* getTlsConfig(void)
{
struct altcp_tls_config* conf;
size_t privkey_len = strlen(privkey) + 1;
size_t privkey_pass_len = strlen(privkey_pass) + 1;
size_t cert_len = strlen(cert) + 1;
conf = altcp_tls_create_config_server_privkey_cert((u8_t*)privkey,
privkey_len, (u8_t*)privkey_pass, privkey_pass_len, (u8_t*)cert, cert_len);
return conf;
}
And I am using basic python tls client example to show successful mbedtls
handshake.
This is my client.py codes:
import time
from socket import create_connection
from ssl import SSLContext, PROTOCOL_TLS_CLIENT
import ssl
hostname='example.org'
ip = '192.168.1.2'
port = 502
context = SSLContext(PROTOCOL_TLS_CLIENT)
context.options |= ssl.OP_NO_SSLv3
context.options |= ssl.OP_NO_TLSv1
context.options |= ssl.OP_NO_TLSv1_1
context.load_verify_locations('cert.pem')
with create_connection((ip, port)) as client:
with context.wrap_socket(client, server_hostname=hostname) as tls:
print(f'Using {tls.version()}\n')
tls.sendall(b'Hello world')
data = tls.recv(1024)
print(f'Server says: {data}')
When I try to start communication I get below outputs on wireshark:
[image: image.png]
When the server send hello message I've this error on the line:
[image: image.png]
When I checked the low_level_output functions I get sending data bytes 150
byte but Ipv4 length shows us 576 byte, opt.h file set as default but if I
changed TCP_MSS as a 250 byte so I can send 136 byte and Ipv4 packet
lenght shows me 136. But does not make sense. I couldnt do successful
handshaking.
My mbedtls debug outputs in this link
https://paste.ofcode.org/PP3zFmrLcKqPdRMT3LzETz How cna I solve this
problem ? What is the reason for the lenght problem ?
Best Regards.
--
Embeded System Engineer
Hello,
I am very sorry about last email by mistakes. I have some questions about multiplication on ecp curves.
I add an ecp curve parameter in ecp_curve.c form SM2 algorithm standard, and the parameter is as follow:
Then I followed the loading method of secp256r1 to load, but I don’t know how to perform fast calculations, so I commented NIST_MODP( p256 ).
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
caseMBEDTLS_ECP_DP_SECP256R1:
NIST_MODP( p256 );
return( LOAD_GROUP( secp256r1 ) );
#endif
#if defined(MBEDTLS_ECP_DP_SM256_ENABLED)
caseMBEDTLS_ECP_DP_SM256:
//NIST_MODP( p256 );
return( LOAD_GROUP_A( sm256 ) );
#endif
Then I call the functional interface mbedtls_ecp_mul to perform the multiplication operation, but the heap memory keeps increasing .
voidtest()
{
intret;
mbedtls_mpiUd;
mbedtls_ecp_groupgrp;
mbedtls_ecp_pointT_Q;
mbedtls_mpi_init(&Ud);
mbedtls_ecp_group_init( &grp );
mbedtls_ecp_point_init( &T_Q );
ret=mbedtls_mpi_read_binary(&Ud, arrUd, sizeof(arr_U_d));
// ret = mbedtls_ecp_group_load(&grp,MBEDTLS_ECP_DP_SECP256R1);
ret=mbedtls_ecp_group_load(&grp,MBEDTLS_ECP_DP_SM256);
ret=mbedtls_ecp_mul(&grp, &T_Q, &Ud, &(grp.G), NULL, NULL) ;
printf("%x\n", -ret);
mbedtls_mpi_free(&Ud);
mbedtls_ecp_group_free( &grp );
mbedtls_ecp_point_free( &T_Q );
}
intmain()
{
for(inti=0; i<10; i++)
test();
return0;
}
The heap memory is mesaured by massif( valgring tools),
Can someone tell me what this is because of and how to fix this problem ?
Best Regards.
Shudong Zhang