HarringayMakerSpace / awsiot

ESP8266 / ESP32 examples with Amazon AWS IoT
53 stars 38 forks source link

Reduce binary certificate & key buffer sizes #2

Closed viotti closed 5 years ago

viotti commented 6 years ago

Hello,

it seems there is a waste on the following instructions.

https://github.com/HarringayMakerSpace/awsiot/blob/2f1d954e68e61d2e5fbf930d2fe2ba2a17bf9ded/Esp8266AWSIoTExample/Esp8266AWSIoTExample.ino#L94

https://github.com/HarringayMakerSpace/awsiot/blob/2f1d954e68e61d2e5fbf930d2fe2ba2a17bf9ded/Esp8266AWSIoTExample/Esp8266AWSIoTExample.ino#L98

The raw data is not the same size of the Base64 encoded data. It is actually about 75% of its size.

uint8_t binaryCert[certificatePemCrt.length() * 3 / 4];
uint8_t binaryPrivate[privatePemKey.length() * 3 / 4]; 

C macros from other projects.

https://www.ffmpeg.org/doxygen/3.2/group__lavu__base64.html https://api-docs.iotivity.org/latest-c/base64_8h.html#a7d5a3b6936df0cd2df64ed83bcb303df

BTW, thanks for this great example.

torntrousers commented 6 years ago

Thats great, thanks. I knew it was unnecessarily large but didn't know about divide by 4 multiple by 3, I read more about it here https://stackoverflow.com/questions/34546498/calculate-the-size-to-a-base-64-decoded-message.

viotti commented 6 years ago

Yeah, there is some rounding / Base64 padding involved there, but I believe using x * 3 / 4 will give a sufficiently large buffer with negligible loss of space. I have tried this on my local sketch and it is working fine, but since I cannot ensure this formula will work 100% of the cases, I've linked to some other projects that are already doing this. Hopefully, with success! 😉

viotti commented 5 years ago

@torntrousers thank you for the fix.