As of August 2020 the site you are on (wiki.newae.com) is deprecated, and content is now at rtfm.newae.com. |
Difference between revisions of "Tutorial B1 Building a SimpleSerial Project"
Line 123: | Line 123: | ||
|intro = = Completing Tutorial with CW1200 (Pro) = | |intro = = Completing Tutorial with CW1200 (Pro) = | ||
|content= Completing Tutorial with CW1200}} | |content= Completing Tutorial with CW1200}} | ||
− | |||
− | |||
− | |||
− | |||
<h1> Conclusion </h1> | <h1> Conclusion </h1> |
Revision as of 14:13, 17 September 2017
This tutorial has been updated for ChipWhisperer 4.0.0 release. If you are using 3.x.x see the "V3" link in the sidebar.
B1: Building a SimpleSerial Project | |
---|---|
Target Architecture | XMEGA |
Hardware Crypto | No |
Software Release | V3 / V4 / V5 |
This tutorial will introduce you to the 'simpleserial' communications system. It will show you how to perform different operations on data based on input from the ChipWhisperer software. This can be used for building your own system which you wish to 'break'.
What is SimpleSerial
SimpleSerial is the communications protocol used for almost all of the ChipWhisperer demo project. It's a very basic serial protocol which can be easily implemented on most systems. This system communicates using a standard asyncronous serial protocol, 38400 baud, 8-N-1.
All messages are sent in ASCII-text, and are normally terminated with a line-feed ('\n'). This allows you to interact with the simpleserial system over a standard terminal emulator.
The following message types are defined:
-
x
- Sending a 'x' resets the buffers. This does not require a line-feed termination. It is suggested to always send a stream of x's to initilize the system in case the device was already in some other mode due to noise/corruption.
-
k00112233445566778899AABBCCDDEEFF\\n
- Loads the encryption key
00112233445566778899AABBCCDDEEFF
into the system. If not called the system may use some default key. -
pAABBCCDDEEFF00112233445566778899\\n
- Encrypts the data
AABBCCDDEEFF00112233445566778899
with the key loaded with the 'k' command. The system will respond with a string starting with r, as shown next. -
rCBBD4A2B34F2571758FF6A797E09859D\\n
- This is the response from the system. If data has been encrypted with a 'p' for example, the system will respond with the 'r' sequence automatically. So sending the earlier example means the result of the encryption was
cbbd4a2b34f2571758ff6a797e09859d
.
Building the Basic Example
You'll need to have installed avr-gcc and avr-libc. You may have already done this by following the installation guide, or if using the ChipWhisperer-VM it comes prepared with avr-gcc already setup. See the Installing_ChipWhisperer guide for details.
Once you have a working compiler (check by typing 'avr-gcc' at the command line - if using Windows you may need to setup a special batch file to provide you with a avr-gcc command prompt).
- We want to use the existing SimpleSerial firmware as a base for our project, but we don't want to edit the existing firmware. Instead, we'll make a new project with a copy of this firmware. Copy the directory
simpleserial-base
which is found atchipwhisperer/hardware/victims/firmware/
of the chipwhisperer release to a new directory calledsimpleserial-base-lab1
. You must keep it in the same directory, as it will reference other files within that directory for the build process. - Open a terminal with avr-gcc in the path. If using Windows the sidebar on the Installing_ChipWhisperer page - you can either add WinAVR to your system path, or you can run the 'winavr.bat' file suggested.
Change the terminal to the newly copied directory. For example:
Windows:cd c:\chipwhisperer\hardware\victims\firmware\simpleserial-base-lab1
Linux/macOS:cd chipwhisperer/hardware/victims/firmware/simpleserial-base-lab1
Then, run
make
to build the system. Make sure you specify which platform you're using as your target. For example, for the ChipWhisperer Lite target, runmake PLATFORM=CW303
Which should have the following output:
...Bunch of lines removed... Creating Extended Listing: simpleserial-base.lss avr-objdump -h -S -z simpleserial-base.elf > simpleserial-base.lss Creating Symbol Table: simpleserial-base.sym avr-nm -n simpleserial-base.elf > simpleserial-base.sym Size after: AVR Memory Usage ---------------- Device: atxmega128d3 Program: 1524 bytes (1.1% Full) (.text + .data + .bootloader) Data: 224 bytes (2.7% Full) (.data + .bss + .noinit) Built for platform CW-Lite XMEGA -------- end --------
Ensure that the "Built for platform ___" matches your target device.
Modifying the Basic Example
At this point we want to modify the system to perform 'something' with the data, such that we can confirm the system is working. To do so, open the file simpleserial-base.c
with a code editor such as Programmer's Notepad (which ships with WinAVR).
Find the following code block towards the end of the file:
/********************************** * Start user-specific code here. */ trigger_high(); //16 hex bytes held in 'pt' were sent //from the computer. Store your response //back into 'pt', which will send 16 bytes //back to computer. Can ignore of course if //not needed trigger_low(); /* End user-specific code here. * ********************************/
Modify it to increment the value of each sent data byte:
/********************************** * Start user-specific code here. */ trigger_high(); //16 hex bytes held in 'pt' were sent //from the computer. Store your response //back into 'pt', which will send 16 bytes //back to computer. Can ignore of course if //not needed for(int i = 0; i < 16; i++){ pt[i]++; } trigger_low(); /* End user-specific code here. * ********************************/
- Rebuild the example using the
make
command. Remember you can press the up arrow on the keyboard to get recently typed commands in most OSes.
Completing Tutorial with CW1173 (Lite)
![Right-black-arrow.png](/images/e/e9/Right-black-arrow.png)
Setting up the Hardware
This tutorial uses the CW1173_ChipWhisperer-Lite hardware. No hardware setup is required normally, simply plug in the USB cable:
Note that under no circumstances as part of the setup should you use the CW1173 device to hold up furniture:
It is assumed that you've already followed the guide in Installing_ChipWhisperer. Thus it is assumed you are able to communicate with the ChipWhisperer CW1173 hardware (or whatever capture hardware you are using). Note in particular you must have configured the FPGA bitstream in the ChipWhisperer-Capture software, all part of the description in the Installing_ChipWhisperer guide.
Assuming this setup is complete, you can confirm you are able to communicate with the hardware by running the example capture of traces given in the CW1173_ChipWhisperer-Lite quick-start.
Programming the Example
Note with the CW1173 you need to configure a clock before programming of the device will succeed. Programming of the target device will be done as part of the CW-Capture software setup, discussed next.
Communicating from CW-Capture Software
Next, open the CW-Capture software. Then perform the following steps:
- Switch to the Python Console tab.
- The script selection window (2) lists available example scripts. Scroll down to "connect_cwlite_simpleserial.py" and click on it.
- You will see the script contents appear in the "Script Preview" window (3). You can either hit the "Run" button or double-click the filename of the script to execute it. Do either of those now.
The window should change to indicate the connect succeeded:
- The console lists the exact script that is executed. Note you could have manually executed the script commands line-by-line in this console.
- The "Scope" and "Target" buttons will show as connected.
- The Status Bar will show a connection.
Note in previous software versions, this tutorial took you through manual setup. This can still be done (using the GUI), but instead now the API has been made more powerful, so the example configuration script will be used instead.
To do so, simply scroll down and select the "setup_cwlite_xmega_aes.py" file:
You'll notice the contents of the script contain the following setup: 1 scope.gain.gain = 45
2 scope.adc.samples = 3000
3 scope.adc.offset = 1250
4 scope.adc.basic_mode = "rising_edge"
5 scope.clock.clkgen_freq = 7370000
6 scope.clock.adc_src = "clkgen_x4"
7 scope.trigger.triggers = "tio4"
8 scope.io.tio1 = "serial_rx"
9 scope.io.tio2 = "serial_tx"
10 scope.io.hs2 = "clkgen"
Line 1: Sets the input ADC gain
Line 2: Sets the number of samples to record as 3000 samples long (this is normally used for the AES algorithm).
Line 3: Sets an offset of 1250 samples from the trigger to when we start recording samples.
Line 4: Sets the trigger as being a "rising edge" trigger.
Line 5: Sets the internal clock generator to 7.37MHz
Line 6: Sets the ADC as running at 4x that clock (so 29.48MHz)
Line 7: Sets the trigger pin as GPIO4 (we previously set the trigger condition as rising edge, so this pin will be the one a rising edge is expected on).
Line 8: Configures GPIO1 as the RX (Input). This is what the XMEGA target expects.
Line 9: Configures GPIO2 as the TX (Output). This is what the XMEGA target expects.
Line 10: Sets the "High-Speed 2" (HS2) pin as having the 7.37MHz clock output.
You can now program the XMEGA device! To do so, open the XMEGA Programmer from the Tools menu:
Hit the Check Signature button and confirm the device is detected. If not you may have issues with the clock setup.
Using the Find button, navigate to the
simpleserial-base-cw303.hex
which you built earlier with themake
command. You can then press the Erase/Program/Verify button, and confirm the file is programmed into the XMEGA device:Note the programmer dialog not only shows the successful programming status, but also shows when the
.hex
file was last modified. Always confirm this matches with when you last remember compiling the program -- if it is widely different this suggests you have selected the wrong file!Finally we can check communications with the programmed file:
- Close the XMEGA programmer dialog.
- Open the status monitor under Tools > Encryption Status Monitor.
- Resize the monitor window. The monitor will show sent & received data to the target.
- Hit the Run 1 button (
). You may have to hit it a few times, as the very first serial data is often lost. You should see data populate in the Text Out field of the monitor window. Note that each byte of the Text In is incremented in the Text Out field.
Setting up the Hardware
This tutorial uses the CW1173_ChipWhisperer-Lite hardware. No hardware setup is required normally, simply plug in the USB cable:
Note that under no circumstances as part of the setup should you use the CW1173 device to hold up furniture:
It is assumed that you've already followed the guide in Installing_ChipWhisperer. Thus it is assumed you are able to communicate with the ChipWhisperer CW1173 hardware (or whatever capture hardware you are using). Note in particular you must have configured the FPGA bitstream in the ChipWhisperer-Capture software, all part of the description in the Installing_ChipWhisperer guide.
Assuming this setup is complete, you can confirm you are able to communicate with the hardware by running the example capture of traces given in the CW1173_ChipWhisperer-Lite quick-start.
Programming the Example
Note with the CW1173 you need to configure a clock before programming of the device will succeed. Programming of the target device will be done as part of the CW-Capture software setup, discussed next.
Communicating from CW-Capture Software
Next, open the CW-Capture software. Then perform the following steps:
- Switch to the Python Console tab.
- The script selection window (2) lists available example scripts. Scroll down to "connect_cwlite_simpleserial.py" and click on it.
- You will see the script contents appear in the "Script Preview" window (3). You can either hit the "Run" button or double-click the filename of the script to execute it. Do either of those now.
The window should change to indicate the connect succeeded:
- The console lists the exact script that is executed. Note you could have manually executed the script commands line-by-line in this console.
- The "Scope" and "Target" buttons will show as connected.
- The Status Bar will show a connection.
Note in previous software versions, this tutorial took you through manual setup. This can still be done (using the GUI), but instead now the API has been made more powerful, so the example configuration script will be used instead.
To do so, simply scroll down and select the "setup_cwlite_xmega_aes.py" file:
You'll notice the contents of the script contain the following setup: 1 scope.gain.gain = 45
2 scope.adc.samples = 3000
3 scope.adc.offset = 1250
4 scope.adc.basic_mode = "rising_edge"
5 scope.clock.clkgen_freq = 7370000
6 scope.clock.adc_src = "clkgen_x4"
7 scope.trigger.triggers = "tio4"
8 scope.io.tio1 = "serial_rx"
9 scope.io.tio2 = "serial_tx"
10 scope.io.hs2 = "clkgen"
Line 1: Sets the input ADC gain
Line 2: Sets the number of samples to record as 3000 samples long (this is normally used for the AES algorithm).
Line 3: Sets an offset of 1250 samples from the trigger to when we start recording samples.
Line 4: Sets the trigger as being a "rising edge" trigger.
Line 5: Sets the internal clock generator to 7.37MHz
Line 6: Sets the ADC as running at 4x that clock (so 29.48MHz)
Line 7: Sets the trigger pin as GPIO4 (we previously set the trigger condition as rising edge, so this pin will be the one a rising edge is expected on).
Line 8: Configures GPIO1 as the RX (Input). This is what the XMEGA target expects.
Line 9: Configures GPIO2 as the TX (Output). This is what the XMEGA target expects.
Line 10: Sets the "High-Speed 2" (HS2) pin as having the 7.37MHz clock output.
You can now program the XMEGA device! To do so, open the XMEGA Programmer from the Tools menu:
Hit the Check Signature button and confirm the device is detected. If not you may have issues with the clock setup.
Using the Find button, navigate to the
simpleserial-base-cw303.hex
which you built earlier with themake
command. You can then press the Erase/Program/Verify button, and confirm the file is programmed into the XMEGA device:Note the programmer dialog not only shows the successful programming status, but also shows when the
.hex
file was last modified. Always confirm this matches with when you last remember compiling the program -- if it is widely different this suggests you have selected the wrong file!Finally we can check communications with the programmed file:
- Close the XMEGA programmer dialog.
- Open the status monitor under Tools > Encryption Status Monitor.
- Resize the monitor window. The monitor will show sent & received data to the target.
- Hit the Run 1 button (
). You may have to hit it a few times, as the very first serial data is often lost. You should see data populate in the Text Out field of the monitor window. Note that each byte of the Text In is incremented in the Text Out field.
Completing Tutorial with CW1200 (Pro)
![Right-black-arrow.png](/images/e/e9/Right-black-arrow.png)
Setting up the Hardware
This tutorial uses the CW1200_ChipWhisperer-Pro hardware.
- Remove the ChipWhisperer-Pro main capture hardware, UFO Board, and SMA cable from the ChipWhisperer-Pro case.
- Attached the UFO board to the ChipWhisperer-Pro with the 20-pin cable, and connect the VOUT SMA connector to the MEASURE input.
- Power up the ChipWhisperer-Pro with the 5V DC power adapter, and connect the USB cable to the computer.
- If this the first time powering up, you will need to install the drivers (see CW1200_ChipWhisperer-Pro).
Note if you have modified the UFO board the jumpers may no longer be at default locations. The jumper settings required are:
- XMEGA Target board mounted
- J3 routes HS2/OUT to CLKIN
- J1 set to "J5-VREF" (right two pins shorted)
- J14 set to "FILT" (left two pins shorted)
- "3.3V SRC" switch set to "J1/CW"
It is assumed that you've already followed the guide in Installing_ChipWhisperer. Thus it is assumed you are able to communicate with the ChipWhisperer CW1173 hardware (or whatever capture hardware you are using). Note in particular you must have configured the FPGA bitstream in the ChipWhisperer-Capture software, all part of the description in the Installing_ChipWhisperer guide.
Assuming this setup is complete, you can confirm you are able to communicate with the hardware by running the example capture of traces given in the CW1173_ChipWhisperer-Lite quick-start.
Programming the Example
Note with the CW1173 you need to configure a clock before programming of the device will succeed. Programming of the target device will be done as part of the CW-Capture software setup, discussed next.
Communicating from CW-Capture Software
Next, open the CW-Capture software. Then perform the following steps:
- Switch to the Python Console tab.
- The script selection window (2) lists available example scripts. Scroll down to "connect_cwlite_simpleserial.py" and click on it.
- You will see the script contents appear in the "Script Preview" window (3). You can either hit the "Run" button or double-click the filename of the script to execute it. Do either of those now.
The window should change to indicate the connect succeeded:
- The console lists the exact script that is executed. Note you could have manually executed the script commands line-by-line in this console.
- The "Scope" and "Target" buttons will show as connected.
- The Status Bar will show a connection.
Note in previous software versions, this tutorial took you through manual setup. This can still be done (using the GUI), but instead now the API has been made more powerful, so the example configuration script will be used instead.
To do so, simply scroll down and select the "setup_cwlite_xmega_aes.py" file:
You'll notice the contents of the script contain the following setup: 1 scope.gain.gain = 45
2 scope.adc.samples = 3000
3 scope.adc.offset = 1250
4 scope.adc.basic_mode = "rising_edge"
5 scope.clock.clkgen_freq = 7370000
6 scope.clock.adc_src = "clkgen_x4"
7 scope.trigger.triggers = "tio4"
8 scope.io.tio1 = "serial_rx"
9 scope.io.tio2 = "serial_tx"
10 scope.io.hs2 = "clkgen"
Line 1: Sets the input ADC gain
Line 2: Sets the number of samples to record as 3000 samples long (this is normally used for the AES algorithm).
Line 3: Sets an offset of 1250 samples from the trigger to when we start recording samples.
Line 4: Sets the trigger as being a "rising edge" trigger.
Line 5: Sets the internal clock generator to 7.37MHz
Line 6: Sets the ADC as running at 4x that clock (so 29.48MHz)
Line 7: Sets the trigger pin as GPIO4 (we previously set the trigger condition as rising edge, so this pin will be the one a rising edge is expected on).
Line 8: Configures GPIO1 as the RX (Input). This is what the XMEGA target expects.
Line 9: Configures GPIO2 as the TX (Output). This is what the XMEGA target expects.
Line 10: Sets the "High-Speed 2" (HS2) pin as having the 7.37MHz clock output.
You can now program the XMEGA device! To do so, open the XMEGA Programmer from the Tools menu:
Hit the Check Signature button and confirm the device is detected. If not you may have issues with the clock setup.
Using the Find button, navigate to the
simpleserial-base-cw303.hex
which you built earlier with themake
command. You can then press the Erase/Program/Verify button, and confirm the file is programmed into the XMEGA device:Note the programmer dialog not only shows the successful programming status, but also shows when the
.hex
file was last modified. Always confirm this matches with when you last remember compiling the program -- if it is widely different this suggests you have selected the wrong file!Finally we can check communications with the programmed file:
- Close the XMEGA programmer dialog.
- Open the status monitor under Tools > Encryption Status Monitor.
- Resize the monitor window. The monitor will show sent & received data to the target.
- Hit the Run 1 button (
). You may have to hit it a few times, as the very first serial data is often lost. You should see data populate in the Text Out field of the monitor window. Note that each byte of the Text In is incremented in the Text Out field.
Setting up the Hardware
This tutorial uses the CW1200_ChipWhisperer-Pro hardware.
- Remove the ChipWhisperer-Pro main capture hardware, UFO Board, and SMA cable from the ChipWhisperer-Pro case.
- Attached the UFO board to the ChipWhisperer-Pro with the 20-pin cable, and connect the VOUT SMA connector to the MEASURE input.
- Power up the ChipWhisperer-Pro with the 5V DC power adapter, and connect the USB cable to the computer.
- If this the first time powering up, you will need to install the drivers (see CW1200_ChipWhisperer-Pro).
Note if you have modified the UFO board the jumpers may no longer be at default locations. The jumper settings required are:
- XMEGA Target board mounted
- J3 routes HS2/OUT to CLKIN
- J1 set to "J5-VREF" (right two pins shorted)
- J14 set to "FILT" (left two pins shorted)
- "3.3V SRC" switch set to "J1/CW"
It is assumed that you've already followed the guide in Installing_ChipWhisperer. Thus it is assumed you are able to communicate with the ChipWhisperer CW1173 hardware (or whatever capture hardware you are using). Note in particular you must have configured the FPGA bitstream in the ChipWhisperer-Capture software, all part of the description in the Installing_ChipWhisperer guide.
Assuming this setup is complete, you can confirm you are able to communicate with the hardware by running the example capture of traces given in the CW1173_ChipWhisperer-Lite quick-start.
Programming the Example
Note with the CW1173 you need to configure a clock before programming of the device will succeed. Programming of the target device will be done as part of the CW-Capture software setup, discussed next.
Communicating from CW-Capture Software
Next, open the CW-Capture software. Then perform the following steps:
- Switch to the Python Console tab.
- The script selection window (2) lists available example scripts. Scroll down to "connect_cwlite_simpleserial.py" and click on it.
- You will see the script contents appear in the "Script Preview" window (3). You can either hit the "Run" button or double-click the filename of the script to execute it. Do either of those now.
The window should change to indicate the connect succeeded:
- The console lists the exact script that is executed. Note you could have manually executed the script commands line-by-line in this console.
- The "Scope" and "Target" buttons will show as connected.
- The Status Bar will show a connection.
Note in previous software versions, this tutorial took you through manual setup. This can still be done (using the GUI), but instead now the API has been made more powerful, so the example configuration script will be used instead.
To do so, simply scroll down and select the "setup_cwlite_xmega_aes.py" file:
You'll notice the contents of the script contain the following setup: 1 scope.gain.gain = 45
2 scope.adc.samples = 3000
3 scope.adc.offset = 1250
4 scope.adc.basic_mode = "rising_edge"
5 scope.clock.clkgen_freq = 7370000
6 scope.clock.adc_src = "clkgen_x4"
7 scope.trigger.triggers = "tio4"
8 scope.io.tio1 = "serial_rx"
9 scope.io.tio2 = "serial_tx"
10 scope.io.hs2 = "clkgen"
Line 1: Sets the input ADC gain
Line 2: Sets the number of samples to record as 3000 samples long (this is normally used for the AES algorithm).
Line 3: Sets an offset of 1250 samples from the trigger to when we start recording samples.
Line 4: Sets the trigger as being a "rising edge" trigger.
Line 5: Sets the internal clock generator to 7.37MHz
Line 6: Sets the ADC as running at 4x that clock (so 29.48MHz)
Line 7: Sets the trigger pin as GPIO4 (we previously set the trigger condition as rising edge, so this pin will be the one a rising edge is expected on).
Line 8: Configures GPIO1 as the RX (Input). This is what the XMEGA target expects.
Line 9: Configures GPIO2 as the TX (Output). This is what the XMEGA target expects.
Line 10: Sets the "High-Speed 2" (HS2) pin as having the 7.37MHz clock output.
You can now program the XMEGA device! To do so, open the XMEGA Programmer from the Tools menu:
Hit the Check Signature button and confirm the device is detected. If not you may have issues with the clock setup.
Using the Find button, navigate to the
simpleserial-base-cw303.hex
which you built earlier with themake
command. You can then press the Erase/Program/Verify button, and confirm the file is programmed into the XMEGA device:Note the programmer dialog not only shows the successful programming status, but also shows when the
.hex
file was last modified. Always confirm this matches with when you last remember compiling the program -- if it is widely different this suggests you have selected the wrong file!Finally we can check communications with the programmed file:
- Close the XMEGA programmer dialog.
- Open the status monitor under Tools > Encryption Status Monitor.
- Resize the monitor window. The monitor will show sent & received data to the target.
- Hit the Run 1 button (
). You may have to hit it a few times, as the very first serial data is often lost. You should see data populate in the Text Out field of the monitor window. Note that each byte of the Text In is incremented in the Text Out field.
Conclusion
In this tutorial you have learned how to build a custom program for the microcontroller on the ChipWhisperer target board. You have programmed the built .hex file into the microcontroller, and confirmed communications with the ChipWhisperer device.
In future labs you will build on this knowledge to attack specific instructions.
Troubleshooting
Issues with compilation:
You may have to generate the .dep and objdir directories manually before make will work:
mkdir .dep mkdir objdir- On Windows 8, you may get an error like fork: resyntaxhighlight temporarily unavailable. This requires you to install an updated mysys.dll. Download from http://www.madwizard.org/download/electronics/msys-1.0-vista64.zip, unzip file, and copy the .dll to
C:\WinAVR-20100110\utils\bin
, replacing the existing file.- For the AVR Studio USB Drivers, you'll need to download a Special Update from Atmel.
- You may wish to use the "ChipWhisperer Virtual Machine" on newer Windows systems, which does not require any of the above setup.