So, hello guys, welcome back to the gettobyte YouTube channel. The video you have just seen is the encryption-decryption of an image using Microcontrollers. And that’s what we will implement and learn to do in today’s video.
We are going to explore, how to use cryptography technology in microcontrollers. Will be doing the hands-on activity for encryption/decryption of data and images using the ElecronicsV2 Development board. Encryption/Decryption is the cryptography concept and what we are going to achieve on doing so is to know how to use cryptography technology through microcontrollers.
For doing Encryption/decryption we are using the AES cryptography algorithm, which is supported in the ElecronicsV2 development board through its SHE standard-based CSEc peripheral. For those who are new to the channel and world of cryptography technology, would recommend watching these videos in the following hierarchical order to get an understanding of cryptography and the technical terms that have been stated just now.
What i have done here is take an image, which is an original image and convert it into a string of bytes. After that, wrote software to store that string of bytes in the ElecronicsV2 development board and to do encryption through AES cryptography algorithm on the stored data. On successful encryption, those string of bytes are changed to the encrypted format of data and then that encrypted data is processed to print the encrypted image on color LCD Screen. This is just a basic demo on how to use cryptography technology for securing the original data. Here we are printing encrypted image on the LCD Screen, Same way we can send, encrypted image data to other microcontroller via CAN, LIN or any other protocol. Will be doing this activity too in upcoming blogs. Follow this cryptography technology playlist to explore more such demos.
Now to achieve this, we will be needing some Hardware and Software tools.
1) ElecronicsV2 Development Board: BuyLink
2) JlinkV9 Debugger: Debugger to program the ElecronicsV2 Board: Buy Link
3) Double Breadboard:
4) ST7789 Color LCD Screen: LCD screen for printing images
ElecronicsV2 is a development board built on NXP Semiconductors S32K144 Microcontroller. S32K144 MCU is an automotive Microcontroller based on ARM Cortex M4 Processor and is part of NXP S32 Automotive Microcontroller family.
ElecronicsV2 board which is built on S32K144 MCU has a cryptography peripheral present in it, called as CSEc(Cryptography Security Engine). Which is a SHE standard based security sub-system for doing cryptographic operations and establishing secure communication b/w the ECU and the sensors/actuators of the car.
We are gonna use CSEc peripheral of S32K144, to do encryption/decryption of data
1) S32 Design Studio IDE: Which is a Desktop Application for programming S32 Family of microcontroller’s/microprocessor.
2) LCD image converter: Desktop application to convert the image into string of bytes
S32 Design Studio is the IDE that is used for development of Embedded Software Development on S32 family of Microcontrollers. IDE stands for Integrated Development Environment, which is used for doing software development on microcontrollers. ElecronicsV2 board has S32K144 Microcontroller, which is part of NXP Semiconductor S32 family of MCU. Thus, we will be using S32 Design Studio for doing Handson activity with ElecronicsV2 boards,
S32 Design Studio: is an Integrated Development Environment to write the microcontroller programs for S32 Automotive Microcontrollers. Install the S32 Design Studio software in your PC and in addition to it user also have to download the Real Time Driver (RTD) package for S32K1 MCU. Refer to this blog for full installation guide of S32 Design Studio step-by-step: Getting Started with S32 Design Studio Part 1 | Gettobyte
Okay so now let’s get started with it, i hope so you guys have gathered all the software/hardware that i have mentioned above and connected all the hardware items. We will at first directly gonna do the practical implementation and then going to dwell deep into it.
Now for microcontroller programming on ElecronicsV2, I am making a separate GitHub repo, where all the demo examples and peripherals codes would be committed with time. Clone the repo, into your local PC. Link of the repo:
Now in the repo, navigate to: Peripheral Drivers-> CSEc , here you will see different examples related to cryptography technology in ElecronicsV2 Development board. ￼
The CSEc_AES_ENC_CBC_ECB_ST7789, is the project for which demo video is shown to you in the starting of the blog. We are going to execute and understand this project.
After cloning the project into your PC, you can see the same folder structure as shown to you at GitHub repo at the path Peripheral_Drivers->CSEc:
Now open the S32 Design Studio and we have to open the Project CSEc_AES_ENC_CBC_ECB_ST7789 into it. CSEc_AES_ENC_CBC_ECB_ST7789 is already developed code, so to open it navigate to file->Open Project from File System To know how to open already developed project in S32 Design Studio, refer to this blog: Getting Started with S32 Design Studio Part 1 | Gettobyte.
After opening the project, you can see it open in project explorer window of S32 Design Studio.
Opening the Cryptography example projects in S32 Design Studio. In this example project you will find 7 projects:
To Open these projects, Right click on project and go to Build Configurations –> Set Archive
After hovering over the Set-Archive tab, you will see list of all example project:
Select the project, which you want to explore, and you will see its name, adjacent to your project name in the project explorer.
Under the src folder, you will see different .c files. There is one .c file for each of the example projects. By double clicking on the corresponding .c file, you can open that file and explore it
Okay so you have opened your project in S32 Design Studio, now it’s time to build the project. For building the project in S32 Design Studio. Simply click on project name and go to menu as shown in below pic:
On doing so, you will see a console perspective in which code is compiling.
Some pointers to be taken care of:
2) After your project is builded, you will see, a corresponding example project name folder in Project explorer, inside which you will find binary files:
Okay so now, we have got the ELF file of our project. Now it’s time to dump this elf file into our project. For doing so, we need a hardware tool called as debugger to transfer this elf file which is in our PC to the microcontroller S32K144 which is in ElecronicsV2 board.
The hardware tool that we are going to use is JlinkV9 debugger which is universal debugger for all ARM Cortex M4 based microcontroller. Viewers can buy this debugger from here: JLink V9 Debugger Emulator – ARM MCU Development Tool – Electronics Infra.
To connect JlinkV9 debugger with ElecronicsV2 board, follow this video or users can refer the Getting to know Hardware Section of Getting Started Manual of ElecronicsV2:
After doing the hardware connection’s, Now we have to do some Hardware Debugger configuration’s in S32 Design Studio IDE so as to detect the debugger hardware tool: JlinkV9 and ElecronicsV2 Board.
For doing so, follow the below steps:
2) Now in the Debug Configuration window, you will see list of hardware debuggers that are supported in S32 Design Studio. We will select the GDB SEGGER J-Link Debugging
3) After that click on top left icon of New Configuration, after which you will see a new debug configuration of your project is made:
4) After that browse the elf file in C/C++ application tab. And select the Build configuration as Use Active.
5) In the Debugger tab of Debug Configuration, you have written the Device Name: S32K144:
These steps will be same for all the example projects. Next steps will only be for CSEC_Crypto_Engine_Init and CSEC_Crypto_Erase_Keys example project.
6) In startup tab of Debug Configuration, check the RAM application option. As we have to run CSEC_Crypto_Engine_Init and CSEC_Crypto_Erase_Keys example project from RAM memory of the controller.
After all these 5-6 steps, click on debug and your debug configuration will be loaded and Debug Perspective will started coming up. In the end something like this will look like in the end.
If your this window come’s up and shows to you. Your debugger and all
This example demonstrates on how to do AES encryption decryption of string of bytes using CBC mode.
Now at first compile the CSEC_Crypto_Engine_Init example project. Refer to building the project section, to know how to build the project.
Now after building the example, it’s time to dump the code into ElecronicsV2 and see some action. We will flash the code and debug the code. To do so, do the Debug/Run configuration for this example project in S32 Design Studio as shown below. Refer to Running/Debugging the project session (follow steps from 1-5) to know more in detail.
Expected behavior, after running this example project is that on successfull encryption you will see a green LED turn on
Example project 2nd is the one which is the DIY project statement which I have stated above.
/* Load the selected key */
/* First load => counter == 1 */
/* increment the 3rd parameter of the function: loadKey().*/
keyLoaded = loadKey(CSEC_KEY_1, key, 1);
3) Use the 7th example: CSEC_Crypto_Erase_keys, only when you want to erase the Cryptography keys and de-initialize the CSEC peripheral. And after running this example project, if you want to use CSEc peripheral again its mandatory to run the first example CSEC_Crypto_Engine_Init again.
If you dont run the first example project, all of your Cryptographic operations will fail.
So, this is a simple DIY project that we have done and i hope you guys got the practical experience of doing cryptography using microcontrollers. Now in the upcoming blogs we will dwell into depth of above DIY project implementation. We will understand what is CSEc peripheral? how does it work? Understanding of API’s which are used in above code’s? What is SHE standard? What is the concept of keys and crypto ciphers in SHE standard?
Follow the below cryptography related blogs to grow your knowledge and skill sets in cryptography field.
We will be using the code’s which are used in this DIY blog on cryptography as a reference to corelate theoretical concepts of upcoming blogs to practical understanding