From beef66eec0e97b37d9b614f8c6bfffe29544e66b Mon Sep 17 00:00:00 2001 From: Gregg Date: Tue, 26 Dec 2023 15:53:38 -0600 Subject: [PATCH] Added new initializer to TempBuffer Allows TempBuffer to be initialized by concatenation of multiple existing buffers. Constructor contains pairs of (T *buf), nElements, where last argument MUST BE NULL to signify end of variable argument list. Will work with any type T, and nElements is in units of elements of type T. --- src/HAP.cpp | 12 ++++-------- src/Utils.cpp | 13 ------------- src/Utils.h | 34 +++++++++++++++++++++++----------- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 2574a1b..7825485 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -518,8 +518,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){ // Concatenate iosDeviceX, IOS ID, and IOS PublicKey into iosDeviceInfo - TempBuffer iosDeviceInfo(iosDeviceX.len()+(*itIdentifier).len+(*itPublicKey).len); - Utils::memcat(iosDeviceInfo,3,iosDeviceX.get(),iosDeviceX.len(),(*itIdentifier).val.get(),(*itIdentifier).len,(*itPublicKey).val.get(),(*itPublicKey).len); + TempBuffer iosDeviceInfo(iosDeviceX,iosDeviceX.len(),(*itIdentifier).val.get(),(*itIdentifier).len,(*itPublicKey).val.get(),(*itPublicKey).len,NULL); if(crypto_sign_verify_detached(*itSignature, iosDeviceInfo, iosDeviceInfo.len(), *itPublicKey) != 0){ // verify signature of iosDeviceInfo using iosDeviceLTPK LOG0("\n*** ERROR: LPTK Signature Verification Failed\n\n"); @@ -539,8 +538,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){ // Concatenate accessoryX, Accessory ID, and Accessory PublicKey into accessoryInfo - TempBuffer accessoryInfo(accessoryX.len()+hap_accessory_IDBYTES+crypto_sign_PUBLICKEYBYTES); - Utils::memcat(accessoryInfo,3,accessoryX.get(),accessoryX.len(),accessory.ID,hap_accessory_IDBYTES,accessory.LTPK,crypto_sign_PUBLICKEYBYTES); + TempBuffer accessoryInfo(accessoryX,accessoryX.len(),accessory.ID,hap_accessory_IDBYTES,accessory.LTPK,crypto_sign_PUBLICKEYBYTES,NULL); subTLV.clear(); // clear existing SUBTLV records @@ -646,8 +644,7 @@ int HAPClient::postPairVerifyURL(uint8_t *content, size_t len){ // concatenate Accessory's Curve25519 Public Key, Accessory's Pairing ID, and Controller's Curve25519 Public Key into accessoryInfo - TempBuffer accessoryInfo(crypto_box_PUBLICKEYBYTES+hap_accessory_IDBYTES+crypto_box_PUBLICKEYBYTES); - Utils::memcat(accessoryInfo,3,publicCurveKey,crypto_box_PUBLICKEYBYTES,accessory.ID,hap_accessory_IDBYTES,iosCurveKey,crypto_box_PUBLICKEYBYTES); + TempBuffer accessoryInfo(publicCurveKey,crypto_box_PUBLICKEYBYTES,accessory.ID,hap_accessory_IDBYTES,iosCurveKey,crypto_box_PUBLICKEYBYTES,NULL); subTLV.add(kTLVType_Identifier,hap_accessory_IDBYTES,accessory.ID); // set Identifier subTLV record as Accessory's Pairing ID auto itSignature=subTLV.add(kTLVType_Signature,crypto_sign_BYTES,NULL); // create blank Signature subTLV @@ -738,8 +735,7 @@ int HAPClient::postPairVerifyURL(uint8_t *content, size_t len){ // concatenate Controller's Curve25519 Public Key (from previous step), Controller's Pairing ID, and Accessory's Curve25519 Public Key (from previous step) into iosDeviceInfo - TempBuffer iosDeviceInfo(crypto_box_PUBLICKEYBYTES+hap_controller_IDBYTES+crypto_box_PUBLICKEYBYTES); - Utils::memcat(iosDeviceInfo,3,iosCurveKey,crypto_box_PUBLICKEYBYTES,tPair->ID,hap_controller_IDBYTES,publicCurveKey,crypto_box_PUBLICKEYBYTES); + TempBuffer iosDeviceInfo(iosCurveKey,crypto_box_PUBLICKEYBYTES,tPair->ID,hap_controller_IDBYTES,publicCurveKey,crypto_box_PUBLICKEYBYTES,NULL); if(crypto_sign_verify_detached(*itSignature, iosDeviceInfo, iosDeviceInfo.len(), tPair->LTPK) != 0){ // verify signature of iosDeviceInfo using Controller's LTPK LOG0("\n*** ERROR: LPTK Signature Verification Failed\n\n"); diff --git a/src/Utils.cpp b/src/Utils.cpp index ca22729..7af158e 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -87,19 +87,6 @@ String Utils::mask(char *c, int n){ return(s); } // mask -////////////////////////////////////// - -void Utils::memcat(uint8_t *buf, size_t n...){ - va_list args; - va_start(args, n); - while(n-->0){ - uint8_t *addBuf=va_arg(args,uint8_t *); - size_t len=va_arg(args,size_t); - memcpy(buf,addBuf,len); - buf+=len; - } -} - //////////////////////////////// // PushButton // //////////////////////////////// diff --git a/src/Utils.h b/src/Utils.h index 1842aed..5fe89af 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -42,7 +42,6 @@ namespace Utils { char *readSerial(char *c, int max); // read serial port into 'c' until , but storing only first 'max' characters (the rest are discarded) String mask(char *c, int n); // simply utility that creates a String from 'c' with all except the first and last 'n' characters replaced by '*' -void memcat(uint8_t *buf, size_t n...); } @@ -55,29 +54,42 @@ class TempBuffer { private: - bufType *buf; - int nBytes; - int nElements; + bufType *buf=NULL; + size_t nElements; public: - TempBuffer(int _nElements) : nElements(_nElements) { - nBytes=nElements*sizeof(bufType); - buf=(bufType *)HS_MALLOC(nBytes); + TempBuffer(size_t _nElements) : nElements(_nElements) { + buf=(bufType *)HS_MALLOC(nElements*sizeof(bufType)); if(buf==NULL){ - Serial.print("\n\n*** FATAL ERROR: Requested allocation of "); - Serial.print(nBytes); - Serial.print(" bytes failed. Program Halting.\n\n"); + Serial.printf("\n\n*** FATAL ERROR: Requested allocation of %d bytes failed. Program Halting.\n\n",nElements*sizeof(bufType)); while(1); } } + TempBuffer(bufType *addBuf...) : nElements(0) { + va_list args; + va_start(args,addBuf); + while(addBuf!=NULL){ + size_t addElements=va_arg(args,size_t); + buf=(bufType *)HS_REALLOC(buf,(nElements+addElements)*sizeof(bufType)); + if(buf==NULL){ + Serial.printf("\n\n*** FATAL ERROR: Requested allocation of %d bytes failed. Program Halting.\n\n",nElements*sizeof(bufType)); + while(1); + } + memcpy(buf+nElements,addBuf,addElements*sizeof(bufType)); + nElements+=addElements; + addBuf=va_arg(args,bufType *); + } + va_end(args); + } + ~TempBuffer(){ free(buf); } int len(){ - return(nBytes); + return(nElements*sizeof(bufType)); } int size(){