diff --git a/mongoose.c b/mongoose.c index 1b7241d9..363838d0 100644 --- a/mongoose.c +++ b/mongoose.c @@ -9378,6 +9378,7 @@ void mg_tcpip_call(struct mg_tcpip_if *ifp, int ev, void *ev_data) { "TCPIP_EV_WIFI_SCAN_END", "TCPIP_EV_WIFI_CONNECT_ERR", "TCPIP_EV_DRIVER", "TCPIP_EV_STATE6_CHANGE", "TCPIP_EV_PHY_RX", "TCPIP_EV_PHY_TX", + "TCPIP_EV_PHY_INIT", "TCPIP_EV_USER"}; if (ev != MG_TCPIP_EV_TIMER_1S && ev < (int) (sizeof(names) / sizeof(names[0]))) { MG_PROF_ADD(ifp, names[ev]); // TODO(): call MG_PROF_DUMP() MG_PROF_FREE() @@ -19138,25 +19139,26 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) { // EC certificate verify packet uint8_t verify[82] = {0x0f, 0x00, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00}; size_t sigsz, verifysz = 0; - int adj1, adj2; + size_t n1, n2, z1, z2; uint8_t sig[64] = {0}; + uint8_t *p1 = sig, *p2 = sig + 32; if (mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig) != 1) { mg_error(c, "P-256 sign"); return false; } - adj1 = sig[0] == 0 && !(sig[1] & 0x80) ? -1 : !!(sig[0] & 0x80); - adj2 = sig[32] == 0 && !(sig[33] & 0x80) ? -1 : !!(sig[32] & 0x80); + for (n1 = 32; n1 > 1 && p1[0] == 0 && !(p1[1] & 0x80); n1--) p1++; + for (n2 = 32; n2 > 1 && p2[0] == 0 && !(p2[1] & 0x80); n2--) p2++; + z1 = (p1[0] & 0x80) ? 1 : 0; + z2 = (p2[0] & 0x80) ? 1 : 0; verify[8] = 0x30; // ASN.1 SEQUENCE - verify[9] = (uint8_t) (68 + adj1 + adj2); + verify[9] = (uint8_t) (4 + n1 + z1 + n2 + z2); verify[10] = 0x02; // ASN.1 INTEGER - verify[11] = (uint8_t) (32 + adj1); - memmove(verify + (size_t) (12 + (adj1 > 0)), sig + (size_t) (adj1 < 0), - (size_t) (32 - (adj1 < 0))); - verify[12 + 32 + adj1] = 0x02; // ASN.1 INTEGER - verify[13 + 32 + adj1] = (uint8_t) (32 + adj2); - memmove(verify + (size_t) (14 + 32 + adj1 + (adj2 > 0)), - sig + (size_t) (32 + (adj2 < 0)), (size_t) (32 - (adj2 < 0))); - sigsz = (size_t) (70 + adj1 + adj2); + verify[11] = (uint8_t) (n1 + z1); + memmove(verify + 12 + z1, p1, n1); + verify[12 + n1 + z1] = 0x02; // ASN.1 INTEGER + verify[13 + n1 + z1] = (uint8_t) (n2 + z2); + memmove(verify + 14 + n1 + z1 + z2, p2, n2); + sigsz = 6 + n1 + z1 + n2 + z2; verifysz = 8U + sigsz; verify[3] = (uint8_t) (sigsz + 4); verify[7] = (uint8_t) sigsz; @@ -32367,6 +32369,7 @@ static bool mg_tcpip_driver_imxrt_init(struct mg_tcpip_if *ifp) { ENET->MSCR = (1 << 8) | ((cr & 0x3f) << 1); // HOLDTIME 2 clks struct mg_phy phy = {enet_read_phy, enet_write_phy}; mg_phy_init(&phy, d->phy_addr, MG_PHY_LEDS_ACTIVE_HIGH); // MAC clocks PHY + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); // Select RMII mode, 100M, keep CRC, set max rx length, disable loop ENET->RCR = (1518 << 16) | MG_BIT(8) | MG_BIT(2); // ENET->RCR |= MG_BIT(3); // Receive all @@ -33208,6 +33211,7 @@ static bool mg_tcpip_driver_netc_init(struct mg_tcpip_if *ifp) { NETC_EMDIO->EMDIO_CFG = ((mdc_cr & 0x1ffU) << 7) | MG_BIT(2); // MDIO_CLK_DIV, MDIO_HOLD mg_phy_init(&phy, d->phy_addr, MG_PHY_RGMII); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); // Initialize ENETC1 PSI0 and port NETC_ENETC1_PCI->CMD |= MG_BIT(2) | MG_BIT(1); // BUS_MASTER_EN, MEM_ACCESS @@ -34285,6 +34289,7 @@ static bool mg_tcpip_driver_ra_init(struct mg_tcpip_if *ifp) { MG_DEBUG(("PHY addr: %d, smispin: %d", d->phy_addr, s_smispin)); struct mg_phy phy = {raeth_read_phy, raeth_write_phy}; mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); // Select RMII mode, ETHERC->ECMR = MG_BIT(2) | MG_BIT(1); // 100M, Full-duplex, CRC @@ -35211,6 +35216,7 @@ static bool mg_tcpip_driver_rw612_init(struct mg_tcpip_if *ifp) { ENET->MSCR = ((d->mdc_cr & 0x3f) << 1) | ((d->mdc_holdtime & 7) << 8); struct mg_phy phy = {eth_read_phy, eth_write_phy}; mg_phy_init(&phy, d->phy_addr, 0); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); ENET->ECR |= MG_BIT(0); // reset ETH // initialize descriptors @@ -35937,6 +35943,7 @@ static bool mg_tcpip_driver_same54_init(struct mg_tcpip_if *ifp) { struct mg_phy phy = {eth_read_phy, eth_write_phy}; mg_phy_init(&phy, d->phy_addr, 0); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); for (int i = 0; i < ETH_DESC_CNT; i++) { // Init TX descriptors s_txdesc[i][0] = (uint32_t) s_txbuf[i]; // Point to data buffer @@ -36915,6 +36922,7 @@ static bool mg_tcpip_driver_stm32f_init(struct mg_tcpip_if *ifp) { ETH->MACFFR = MG_BIT(10); // Perfect filtering struct mg_phy phy = {eth_read_phy, eth_write_phy}; mg_phy_init(&phy, phy_addr, MG_PHY_CLOCKS_MAC); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); ETH->DMARDLAR = (uint32_t) (uintptr_t) s_rxdesc; // RX descriptors ETH->DMATDLAR = (uint32_t) (uintptr_t) s_txdesc; // RX descriptors ETH->DMAIER = MG_BIT(6) | MG_BIT(16); // RIE, NISE @@ -37224,6 +37232,7 @@ static bool mg_tcpip_driver_stm32h_init(struct mg_tcpip_if *ifp) { #endif struct mg_phy phy = {eth_read_phy, eth_write_phy}; mg_phy_init(&phy, phy_addr, phy_conf); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); ETH->DMACRDLAR = (uint32_t) (uintptr_t) s_rxdesc; // RX descriptors start address ETH->DMACRDRLR = ETH_DESC_CNT - 1; // ring length @@ -37772,6 +37781,7 @@ static bool mg_tcpip_driver_tms570_init(struct mg_tcpip_if *ifp) { while (delay-- != 0) (void) 0; struct mg_phy phy = {emac_read_phy, emac_write_phy}; mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); uint32_t channel; for (channel = 0; channel < 8; channel++) { EMAC->MACINDEX = channel; @@ -39600,6 +39610,7 @@ static bool mg_tcpip_driver_xmc_init(struct mg_tcpip_if *ifp) { // init phy struct mg_phy phy = {eth_read_phy, eth_write_phy}; mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); // configure MAC: DO, DM, FES, TC ETH0->MAC_CONFIGURATION = MG_BIT(13) | MG_BIT(11) | MG_BIT(14) | MG_BIT(24); @@ -39906,6 +39917,7 @@ static bool mg_tcpip_driver_xmc7_init(struct mg_tcpip_if *ifp) { // init phy struct mg_phy phy = {eth_read_phy, eth_write_phy}; mg_phy_init(&phy, d->phy_addr, MG_PHY_RGMII); + mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_INIT, &phy); (void) d; return true; diff --git a/mongoose.h b/mongoose.h index b6e9edd0..b6712ab6 100644 --- a/mongoose.h +++ b/mongoose.h @@ -4801,6 +4801,7 @@ enum { MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6) MG_TCPIP_EV_PHY_RX, // Radio frame received struct mg_str * MG_TCPIP_EV_PHY_TX, // Radio frame about to be sent struct mg_str * + MG_TCPIP_EV_PHY_INIT, // PHY initialised struct mg_phy * MG_TCPIP_EV_USER // Starting ID for user-defined events }; // clang-format on diff --git a/src/tls_builtin.c b/src/tls_builtin.c index 4369ae54..5def2a04 100644 --- a/src/tls_builtin.c +++ b/src/tls_builtin.c @@ -1175,25 +1175,26 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) { // EC certificate verify packet uint8_t verify[82] = {0x0f, 0x00, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00}; size_t sigsz, verifysz = 0; - int adj1, adj2; + size_t n1, n2, z1, z2; uint8_t sig[64] = {0}; + uint8_t *p1 = sig, *p2 = sig + 32; if (mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig) != 1) { mg_error(c, "P-256 sign"); return false; } - adj1 = sig[0] == 0 && !(sig[1] & 0x80) ? -1 : !!(sig[0] & 0x80); - adj2 = sig[32] == 0 && !(sig[33] & 0x80) ? -1 : !!(sig[32] & 0x80); + for (n1 = 32; n1 > 1 && p1[0] == 0 && !(p1[1] & 0x80); n1--) p1++; + for (n2 = 32; n2 > 1 && p2[0] == 0 && !(p2[1] & 0x80); n2--) p2++; + z1 = (p1[0] & 0x80) ? 1 : 0; + z2 = (p2[0] & 0x80) ? 1 : 0; verify[8] = 0x30; // ASN.1 SEQUENCE - verify[9] = (uint8_t) (68 + adj1 + adj2); + verify[9] = (uint8_t) (4 + n1 + z1 + n2 + z2); verify[10] = 0x02; // ASN.1 INTEGER - verify[11] = (uint8_t) (32 + adj1); - memmove(verify + (size_t) (12 + (adj1 > 0)), sig + (size_t) (adj1 < 0), - (size_t) (32 - (adj1 < 0))); - verify[12 + 32 + adj1] = 0x02; // ASN.1 INTEGER - verify[13 + 32 + adj1] = (uint8_t) (32 + adj2); - memmove(verify + (size_t) (14 + 32 + adj1 + (adj2 > 0)), - sig + (size_t) (32 + (adj2 < 0)), (size_t) (32 - (adj2 < 0))); - sigsz = (size_t) (70 + adj1 + adj2); + verify[11] = (uint8_t) (n1 + z1); + memmove(verify + 12 + z1, p1, n1); + verify[12 + n1 + z1] = 0x02; // ASN.1 INTEGER + verify[13 + n1 + z1] = (uint8_t) (n2 + z2); + memmove(verify + 14 + n1 + z1 + z2, p2, n2); + sigsz = 6 + n1 + z1 + n2 + z2; verifysz = 8U + sigsz; verify[3] = (uint8_t) (sigsz + 4); verify[7] = (uint8_t) sigsz;