Skip to content

Commit

Permalink
Added new initializer to TempBuffer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HomeSpan committed Dec 26, 2023
1 parent 223107d commit beef66e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
12 changes: 4 additions & 8 deletions src/HAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 518,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){

// Concatenate iosDeviceX, IOS ID, and IOS PublicKey into iosDeviceInfo

TempBuffer<uint8_t> 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<uint8_t> 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");
Expand All @@ -539,8 538,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){

// Concatenate accessoryX, Accessory ID, and Accessory PublicKey into accessoryInfo

TempBuffer<uint8_t> 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<uint8_t> accessoryInfo(accessoryX,accessoryX.len(),accessory.ID,hap_accessory_IDBYTES,accessory.LTPK,crypto_sign_PUBLICKEYBYTES,NULL);

subTLV.clear(); // clear existing SUBTLV records

Expand Down Expand Up @@ -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<uint8_t> 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<uint8_t> 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
Expand Down Expand Up @@ -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<uint8_t> 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<uint8_t> 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");
Expand Down
13 changes: 0 additions & 13 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
////////////////////////////////
Expand Down
34 changes: 23 additions & 11 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 42,6 @@ namespace Utils {

char *readSerial(char *c, int max); // read serial port into 'c' until <newline>, 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...);

}

Expand All @@ -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(){
Expand Down

0 comments on commit beef66e

Please sign in to comment.