Data Encryption Between ESP32 and Windows
The ESP32 microcontroller can send data over a network with other computers. To maintain the confidentiality of the data sent, it is necessary to encrypt the data sent. In this concise writing, a compatible data encryption library between ESP32 and Windows allows you to exchange data with encryption between the two.
An example of encrypting data with AES-128 can be followed in the following article: https://everythingesp.com/esp32-arduino-tutorial-encryption-aes128-in-ecb-mode/
The encryption in the example is very simple:
- Using AES-128 with ECB mode (Electronic Codebook). As is known, the ECB’s fashion has a lot of weaknesses, so if you want more professionals we must use other modes such as CBC (Cipher Block Chaining).
- Data size must be 16 bytes multiples. If the data size is more than 16 bytes, then the function must be invoked repeatedly, and if the data size is not a multiples of 16, it needs to be done padding to be a multiples of 16.
Next, how to make encrypted data in ESP32 readable in Windows? For that it needs to be installed the same/compatible libraries in Windows. For simplicity, in this example will be used the same library so as not to change the source code.
The Library used in the ESP32 is Mbed TLS (https://tls.mbed.org/). The Mbed TLS Library is available in the form of source code, so it can also be compiled alone for other platforms. For Windows, there are already compiled libraries, so stay installed. The versions are available for the GNU-based compiler (Cygwin, MinGW) and Visual Studio.
The following example will be used IDE Netbeans 8.2 with Cygwin compiler. The latest NetBeans is version 11, but Netbeans is already support C/C++ new until version 8.2, so version 11 does not yet support C/C++ language.
Libraries need to be installed from Cygwin’s Setup program. Run the Cygwin (Setup-x86_64. exe) installation program, and then enter the packages selection menu. Select the View “Full”, and Search in “Mbedtls”. Select to install the MBEDTLS and Mbedtls-devel libraries.
The library name for Mbed TLS is libmbedcrypto. Next add the library in the project settings from Netbeans
Here’s how the library looks at Netbeans. The location of the library is in C:/Cygwin64/lib/libmbedcrypto. dll. A
After that, ported from the AES-128 program ESP32 to Netbeans. The results can be found at the following link: https://github.com/waskita/embedded/blob/master/win-crypto/main.c
The following source code is ported to the program:
- Move the setup () content on the Arduino to Main () in Netbeans
- Add the file header stdio. h and String. h
- Replace Serial. Print () with printf ()
|Output in ESP32 Arduino||Windows Output 32|
|RST: 0x1 (POWERON_RESET), boot: 0x13 (SPI_FAST_FLASH_BOOT)|
Configsip: 0, SPIWP: 0xEE
clk_drv: 0x00, q_drv: 0x00, d_drv: 0x00, cs0_drv: 0x00, hd_drv: 0x00, wp_drv:
0x00 mode: DIO, clock d
iv: 1 load: 0x3fff0018,
Len: 4 load: 0x3fff001c,
len: 928 ho 0 Tail 12
Room 4 load: 0x40078000, L
en: 8740 load: 0x40080400,
Len: 5788 entry 0x
4008069c Original Plain
text : Tech Tutor
ials x Ciphered
1d2 Deciphered tex
t: Tech tutorials X
|Original Plain Text: Tech tutorials x|
Tech tutorials x
RUN SUCCESSFUL (total time: 70ms)
From the results above, it appears that the ciphered text is identical, and both can do the decryption process. So it can be deduced both programs have the same function, so the encrypted data in ESP32 can be read on Windows and vice versa.
The next step is to add the data communication process using UDP/TCP in ESP32 and Windows, but it will be another post again.