From 07da5ac924c0f9cc5c39db78c9fb27b24a9f557e Mon Sep 17 00:00:00 2001 From: Gregg Date: Thu, 23 Sep 2021 21:12:37 -0500 Subject: [PATCH] Updated SRP code for 2.0.0 compatibility Arduino-ESP32 has modified the Mbed TLS library so that it uses ESP32 hardware acceleration. However, there is a 512-byte limit to the size of the variables used in an exponential modulo calculation. One of the steps in the SRP code used a 768-byte variable, which cannot be handled in version 2.0.0 though it works fine in version 1.0.6. Solution was to simply reduce the 768-byte variable by modulo N prior to performing the exponential modulo calculation. --- src/SRP.cpp | 13 +++++++------ src/src.ino | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/SRP.cpp b/src/SRP.cpp index 9fd0f1d..8af05eb 100644 --- a/src/SRP.cpp +++ b/src/SRP.cpp @@ -144,8 +144,8 @@ void SRP6A::createPublicKey(){ void SRP6A::getPrivateKey(){ uint8_t privateKey[32]; + randombytes_buf(privateKey,32); // generate 32 random bytes using libsodium (which uses the ESP32 hardware-based random number generator) - mbedtls_mpi_read_binary(&b,privateKey,32); } @@ -164,10 +164,11 @@ void SRP6A::createSessionKey(){ mbedtls_mpi_read_binary(&u,tHash,64); // load hash result into mpi structure u // compute S = (Av^u)^b %N - + mbedtls_mpi_exp_mod(&t1,&v,&u,&N,&_rr); // t1 = v^u %N mbedtls_mpi_mul_mpi(&t2,&A,&t1); // t2 = A*t1 - mbedtls_mpi_exp_mod(&S,&t2,&b,&N,&_rr); // S = t2^b %N + mbedtls_mpi_mod_mpi(&t1,&t2,&N); // t1 = t2 %N (this is needed to reduce size of t2 before next calculation) + mbedtls_mpi_exp_mod(&S,&t1,&b,&N,&_rr); // S = t1^b %N // compute K = SHA512( S ) @@ -176,7 +177,7 @@ void SRP6A::createSessionKey(){ mbedtls_mpi_read_binary(&K,tHash,64); // load hash result into mpi structure K. This is the SRP SHARED SECRET KEY mbedtls_mpi_write_binary(&K,sharedSecret,64); // store SHARED SECRET in easy-to-use binary (uint8_t) format - + } ////////////////////////////////////// @@ -267,10 +268,10 @@ int SRP6A::writeTLV(kTLVType tag, mbedtls_mpi *mpi){ void SRP6A::print(mbedtls_mpi *mpi){ - char sBuf[1000]; + char sBuf[2000]; size_t sLen; - mbedtls_mpi_write_string(mpi,16,sBuf,1000,&sLen); + mbedtls_mpi_write_string(mpi,16,sBuf,2000,&sLen); Serial.print((sLen-1)/2); // subtract 1 for null-terminator, and then divide by 2 to get number of bytes (e.g. 4F = 2 characters, but represents just one mpi byte) Serial.print(" "); diff --git a/src/src.ino b/src/src.ino index 4e2fd9f..d294292 100644 --- a/src/src.ino +++ b/src/src.ino @@ -8,7 +8,7 @@ void setup() { Serial.begin(115200); - homeSpan.setLogLevel(1); + homeSpan.setLogLevel(2); homeSpan.setStatusPin(5); homeSpan.setControlPin(33);