As of August 2020 the site you are on (wiki.newae.com) is deprecated, and content is now at rtfm.newae.com. |
V4:Tutorial B2 Viewing Instruction Power Differences
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.
B2: Viewing Instruction Power Differences | |
---|---|
Target Architecture | XMEGA/Arm/Other |
Hardware Crypto | No |
Software Release | V3 / V4 / V5 |
This tutorial will introduce you to measuring the power consumption of a device under attack. It will demonstrate how you can view the difference between assembly instructions
Contents
- 1 Prerequisites
- 2 Setting Up the Example
- 3 Modifying the Basic Example
- 4 Hardware Setup
- 5 Programming the Target
- 6 Capturing Power Traces
- 7 Modifying the Target
- 8 Clock Phase Adjustment
- 9 Conclusion
- 10 Links
Prerequisites
You should have already completed Tutorial_B1_Building_a_SimpleSerial_Project. This tutorial assumes you are capable of building firmware for the target, programming the code, and connecting to the ChipWhisperer.
Setting Up the Example
In this tutorial, we will once again be working off of the simpleserial-base
firmware. Like with the previous tutorial, you'll need to have a copy of the firmware you want to modify and be able to build for your platform. The instructions are repeated in the drop down menus below, but if you're comfortable with the previous example, feel free to skip them and to just build your new firmware. Alternatively, if you're not too attached to your code, you can just modify your firmware from Tutorial B1 Building a SimpleSerial Project and rebuild it from the same directory.
Building for CWLite with XMEGA Target
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 of the firmware you want to modify in the
chipwhisperer/hardware/vicitims/firmware
to a new folder. The folder you copy will depend on what tutorial you're doing. Typically, the firmware you want to use is listed above the "Building for ..." drop down menus in this wiki. The name is arbitrary, but for this example, we'll call itsimpleserial-LAB-SPECIFIC-FOLDER
(though depending on what firmware and tutorial you're working off of, you may want to call it something different). 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-LAB-SPECIFIC-FOLDER
Linux/macOS:cd chipwhisperer/hardware/victims/firmware/simpleserial-LAB-SPECIFIC-FOLDER
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.
=== Building for CWLite with XMEGA Target === 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 of the firmware you want to modify in the
chipwhisperer/hardware/vicitims/firmware
to a new folder. The folder you copy will depend on what tutorial you're doing. Typically, the firmware you want to use is listed above the "Building for ..." drop down menus in this wiki. The name is arbitrary, but for this example, we'll call itsimpleserial-LAB-SPECIFIC-FOLDER
(though depending on what firmware and tutorial you're working off of, you may want to call it something different). 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-LAB-SPECIFIC-FOLDER
Linux/macOS:cd chipwhisperer/hardware/victims/firmware/simpleserial-LAB-SPECIFIC-FOLDER
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.
Building for CWLite with Arm Target
You'll need to have installed the GNU Embedded Toolchain for ARM. If you haven't yet, see the Installing_ChipWhisperer guide, specifically the Installing ARM Toolchain section, for details.
Once you have a working compiler (check by typing 'arm-none-eabi-gcc' at the command line).
- 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 of the firmware you want to modify in the
chipwhisperer/hardware/vicitims/firmware
to a new folder. The folder you copy will depend on what tutorial you're doing. Typically, the firmware you want to use is listed above the "Building for ..." drop down menus in this tutorial. The name is arbitrary, but for this example, we'll call itsimpleserial-LAB-SPECIFIC-FOLDER
(though depending on what firmware and tutorial you're working off of, you may want to call it something different). 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 arm-none-eabi-gcc in the path. If using Windows the sidebar on the Installing_ChipWhisperer page
Change the terminal to the newly copied directory. For example:
Windows:cd c:\chipwhisperer\hardware\victims\firmware\simpleserial-LAB-SPECIFIC-FOLDER
Linux/macOS:cd chipwhisperer/hardware/victims/firmware/simpleserial-LAB-SPECIFIC-FOLDER
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=CWLITEARM CRYPTO_TARGET=TINYAES128C
Which should have the following output:
...Bunch of lines removed... Linking: simpleserial-base-CWLITEARM.elf arm-none-eabi-gcc -mcpu=cortex-m4 -I. -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fmessage-length=0 -ffunction-sections -gdwarf-2 -DSS_VER=SS_VER_1_1 -DSTM32F303xC -DSTM32F3 -DSTM32 -DDEBUG -DHAL_TYPE=HAL_stm32f3 -DPLATFORM=CWLITEARM -DTINYAES128C -DF_CPU=7372800UL -Os -funsigned-char -funsigned-bitfields -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=objdir/simpleserial-base.o -I.././simpleserial/ -I.././hal -I.././hal/stm32f3 -I.././hal/stm32f3/CMSIS -I.././hal/stm32f3/CMSIS/core -I.././hal/stm32f3/CMSIS/device -I.././hal/stm32f4/Legacy -I.././crypto/ -I.././crypto/tiny-AES128-C -std=gnu99 -MMD -MP -MF .dep/simpleserial-base-CWLITEARM.elf.d objdir/simpleserial-base.o objdir/simpleserial.o objdir/stm32f3_hal.o objdir/stm32f3_hal_lowlevel.o objdir/stm32f3_sysmem.o objdir/aes.o objdir/aes-independant.o objdir/stm32f3_startup.o --output simpleserial-base-CWLITEARM.elf --specs=nano.specs -T .././hal/stm32f3/LinkerScript.ld -Wl,--gc-sections -lm -Wl,-Map=simpleserial-base-CWLITEARM.map,--cref -lm . Creating load file for Flash: simpleserial-base-CWLITEARM.hex arm-none-eabi-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature simpleserial-base-CWLITEARM.elf simpleserial-base-CWLITEARM.hex . Creating load file for EEPROM: simpleserial-base-CWLITEARM.eep arm-none-eabi-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \ --change-section-lma .eeprom=0 --no-change-warnings -O ihex simpleserial-base-CWLITEARM.elf simpleserial-base-CWLITEARM.eep || exit 0 . Creating Extended Listing: simpleserial-base-CWLITEARM.lss arm-none-eabi-objdump -h -S -z simpleserial-base-CWLITEARM.elf > simpleserial-base-CWLITEARM.lss . Creating Symbol Table: simpleserial-base-CWLITEARM.sym arm-none-eabi-nm -n simpleserial-base-CWLITEARM.elf > simpleserial-base-CWLITEARM.sym Size after: text data bss dec hex filename 4588 8 1296 5892 1704 simpleserial-base-CWLITEARM.elf +-------------------------------------------------------- + Built for platform CW-Lite Arm (STM32F3) +--------------------------------------------------------
Ensure that the "Built for platform ___" matches your target device.
=== Building for CWLite with Arm Target === You'll need to have installed the GNU Embedded Toolchain for ARM. If you haven't yet, see the Installing_ChipWhisperer guide, specifically the Installing ARM Toolchain section, for details.
Once you have a working compiler (check by typing 'arm-none-eabi-gcc' at the command line).
- 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 of the firmware you want to modify in the
chipwhisperer/hardware/vicitims/firmware
to a new folder. The folder you copy will depend on what tutorial you're doing. Typically, the firmware you want to use is listed above the "Building for ..." drop down menus in this tutorial. The name is arbitrary, but for this example, we'll call itsimpleserial-LAB-SPECIFIC-FOLDER
(though depending on what firmware and tutorial you're working off of, you may want to call it something different). 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 arm-none-eabi-gcc in the path. If using Windows the sidebar on the Installing_ChipWhisperer page
Change the terminal to the newly copied directory. For example:
Windows:cd c:\chipwhisperer\hardware\victims\firmware\simpleserial-LAB-SPECIFIC-FOLDER
Linux/macOS:cd chipwhisperer/hardware/victims/firmware/simpleserial-LAB-SPECIFIC-FOLDER
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=CWLITEARM CRYPTO_TARGET=TINYAES128C
Which should have the following output:
...Bunch of lines removed... Linking: simpleserial-base-CWLITEARM.elf arm-none-eabi-gcc -mcpu=cortex-m4 -I. -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fmessage-length=0 -ffunction-sections -gdwarf-2 -DSS_VER=SS_VER_1_1 -DSTM32F303xC -DSTM32F3 -DSTM32 -DDEBUG -DHAL_TYPE=HAL_stm32f3 -DPLATFORM=CWLITEARM -DTINYAES128C -DF_CPU=7372800UL -Os -funsigned-char -funsigned-bitfields -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=objdir/simpleserial-base.o -I.././simpleserial/ -I.././hal -I.././hal/stm32f3 -I.././hal/stm32f3/CMSIS -I.././hal/stm32f3/CMSIS/core -I.././hal/stm32f3/CMSIS/device -I.././hal/stm32f4/Legacy -I.././crypto/ -I.././crypto/tiny-AES128-C -std=gnu99 -MMD -MP -MF .dep/simpleserial-base-CWLITEARM.elf.d objdir/simpleserial-base.o objdir/simpleserial.o objdir/stm32f3_hal.o objdir/stm32f3_hal_lowlevel.o objdir/stm32f3_sysmem.o objdir/aes.o objdir/aes-independant.o objdir/stm32f3_startup.o --output simpleserial-base-CWLITEARM.elf --specs=nano.specs -T .././hal/stm32f3/LinkerScript.ld -Wl,--gc-sections -lm -Wl,-Map=simpleserial-base-CWLITEARM.map,--cref -lm . Creating load file for Flash: simpleserial-base-CWLITEARM.hex arm-none-eabi-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature simpleserial-base-CWLITEARM.elf simpleserial-base-CWLITEARM.hex . Creating load file for EEPROM: simpleserial-base-CWLITEARM.eep arm-none-eabi-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \ --change-section-lma .eeprom=0 --no-change-warnings -O ihex simpleserial-base-CWLITEARM.elf simpleserial-base-CWLITEARM.eep || exit 0 . Creating Extended Listing: simpleserial-base-CWLITEARM.lss arm-none-eabi-objdump -h -S -z simpleserial-base-CWLITEARM.elf > simpleserial-base-CWLITEARM.lss . Creating Symbol Table: simpleserial-base-CWLITEARM.sym arm-none-eabi-nm -n simpleserial-base-CWLITEARM.elf > simpleserial-base-CWLITEARM.sym Size after: text data bss dec hex filename 4588 8 1296 5892 1704 simpleserial-base-CWLITEARM.elf +-------------------------------------------------------- + Built for platform CW-Lite Arm (STM32F3) +--------------------------------------------------------
Ensure that the "Built for platform ___" matches your target device.
Building for Other Targets
Building for other targets typically requires additional programs and tools. Additionally, some targets may have a unique build process, meaning the instructions here will not apply to them. Please see the page for the specific target you want to build for before following these instructions, which can be found under the Hardware Documentation section of the Main Page.
Once you have a working compiler:
- 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 of the firmware you want to modify in the
chipwhisperer/hardware/vicitims/firmware
to a new folder. The folder you copy will depend on what tutorial you're doing. Typically, the firmware you want to use is listed above the "Building for ..." drop down menus. The name is arbitrary, but for this example, we'll call itsimpleserial-base-lab1
(though depending on what firmware and tutorial you're working off of, you may want to call it something different). You must keep it in the same directory, as it will reference other files within that directory for the build process. 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. You can see a list of supported targets by typingmake PLATFORM=
. You'll also need to specify aCRYPTO_TARGET
. Most targets and tutorials work withTINYAES128C
, so if you're unsure, this is usually a reliable option. For example, for the NXP Kinetis K24F target, run:make PLATFORM=CW308_K24F CRYPTO_TARGET=TINYAES128C
Which should have the following output:
...Bunch of lines removed... Linking: simpleserial-base-CW308_K24F.elf arm-none-eabi-gcc -I. -O0 -g -DDEBUG -DCPU_MK24FN1M0VLL12 -DFRDM_K64F -DFREEDOM -w -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -mthumb -mapcs -std=gnu99 -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -MMD -MP -static -gdwarf-2 -DSS_VER=SS_VER_1_1 -DHAL_TYPE=HAL_k24f -DPLATFORM=CW308_K24F -DTINYAES128C -DF_CPU=7372800UL -Os -funsigned-char -funsigned-bitfields -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=objdir/simpleserial-base.o -I.././simpleserial/ -I.././hal -I.././hal/k24f -I.././hal/k24f/CMSIS -I.././hal/k24f/Drivers -I.././crypto/ -I.././crypto/tiny-AES128-C -std=gnu99 -MMD -MP -MF .dep/simpleserial-base-CW308_K24F.elf.d objdir/simpleserial-base.o objdir/simpleserial.o objdir/clock_config.o objdir/fsl_adc16.o objdir/fsl_clock.o objdir/fsl_cmp.o objdir/fsl_cmt.o objdir/fsl_common.o objdir/fsl_crc.o objdir/fsl_dac.o objdir/fsl_dmamux.o objdir/fsl_dspi.o objdir/fsl_dspi_edma.o objdir/fsl_edma.o objdir/fsl_ewm.o objdir/fsl_flash.o objdir/fsl_flexbus.o objdir/fsl_flexcan.o objdir/fsl_ftm.o objdir/fsl_gpio.o objdir/fsl_i2c.o objdir/fsl_i2c_edma.o objdir/fsl_llwu.o objdir/fsl_lptmr.o objdir/fsl_mmcau.o objdir/fsl_pdb.o objdir/fsl_pit.o objdir/fsl_pmc.o objdir/fsl_rcm.o objdir/fsl_rnga.o objdir/fsl_rtc.o objdir/fsl_sai.o objdir/fsl_sai_edma.o objdir/fsl_sdhc.o objdir/fsl_sim.o objdir/fsl_smc.o objdir/fsl_sysmpu.o objdir/fsl_uart.o objdir/fsl_uart_edma.o objdir/fsl_vref.o objdir/fsl_wdog.o objdir/k24f_hal.o objdir/system_MK24F12.o objdir/aes.o objdir/aes-independant.o objdir/startup_MK24F12.o --output simpleserial-base-CW308_K24F.elf -Xlinker --gc-sections -Xlinker -static -Xlinker -z -Xlinker muldefs -T .././hal/k24f/MK24FN1M0xxx12_flash.ld --specs=nano.specs --specs=nosys.specs -Wl,--start-group -L .././hal/k24f/ -l:lib_mmcau.a -lm -lc -lgcc -lnosys -Wl,--end-group -Wl,-Map=simpleserial-base-CW308_K24F.map,--cref -lm . Creating load file for Flash: simpleserial-base-CW308_K24F.hex arm-none-eabi-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature simpleserial-base-CW308_K24F.elf simpleserial-base-CW308_K24F.hex . Creating load file for EEPROM: simpleserial-base-CW308_K24F.eep arm-none-eabi-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \ --change-section-lma .eeprom=0 --no-change-warnings -O ihex simpleserial-base-CW308_K24F.elf simpleserial-base-CW308_K24F.eep || exit 0 . Creating Extended Listing: simpleserial-base-CW308_K24F.lss arm-none-eabi-objdump -h -S -z simpleserial-base-CW308_K24F.elf > simpleserial-base-CW308_K24F.lss . Creating Symbol Table: simpleserial-base-CW308_K24F.sym arm-none-eabi-nm -n simpleserial-base-CW308_K24F.elf > simpleserial-base-CW308_K24F.sym Size after: text data bss dec hex filename 11600 120 2388 14108 371c simpleserial-base-CW308_K24F.elf +-------------------------------------------------------- + Built for platform k24f Target +--------------------------------------------------------
Ensure that the "Built for platform ___" matches your target device.
=== Building for Other Targets === Building for other targets typically requires additional programs and tools. Additionally, some targets may have a unique build process, meaning the instructions here will not apply to them. Please see the page for the specific target you want to build for before following these instructions, which can be found under the Hardware Documentation section of the Main Page.
Once you have a working compiler:
- 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 of the firmware you want to modify in the
chipwhisperer/hardware/vicitims/firmware
to a new folder. The folder you copy will depend on what tutorial you're doing. Typically, the firmware you want to use is listed above the "Building for ..." drop down menus. The name is arbitrary, but for this example, we'll call itsimpleserial-base-lab1
(though depending on what firmware and tutorial you're working off of, you may want to call it something different). You must keep it in the same directory, as it will reference other files within that directory for the build process. 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. You can see a list of supported targets by typingmake PLATFORM=
. You'll also need to specify aCRYPTO_TARGET
. Most targets and tutorials work withTINYAES128C
, so if you're unsure, this is usually a reliable option. For example, for the NXP Kinetis K24F target, run:make PLATFORM=CW308_K24F CRYPTO_TARGET=TINYAES128C
Which should have the following output:
...Bunch of lines removed... Linking: simpleserial-base-CW308_K24F.elf arm-none-eabi-gcc -I. -O0 -g -DDEBUG -DCPU_MK24FN1M0VLL12 -DFRDM_K64F -DFREEDOM -w -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -mthumb -mapcs -std=gnu99 -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -MMD -MP -static -gdwarf-2 -DSS_VER=SS_VER_1_1 -DHAL_TYPE=HAL_k24f -DPLATFORM=CW308_K24F -DTINYAES128C -DF_CPU=7372800UL -Os -funsigned-char -funsigned-bitfields -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=objdir/simpleserial-base.o -I.././simpleserial/ -I.././hal -I.././hal/k24f -I.././hal/k24f/CMSIS -I.././hal/k24f/Drivers -I.././crypto/ -I.././crypto/tiny-AES128-C -std=gnu99 -MMD -MP -MF .dep/simpleserial-base-CW308_K24F.elf.d objdir/simpleserial-base.o objdir/simpleserial.o objdir/clock_config.o objdir/fsl_adc16.o objdir/fsl_clock.o objdir/fsl_cmp.o objdir/fsl_cmt.o objdir/fsl_common.o objdir/fsl_crc.o objdir/fsl_dac.o objdir/fsl_dmamux.o objdir/fsl_dspi.o objdir/fsl_dspi_edma.o objdir/fsl_edma.o objdir/fsl_ewm.o objdir/fsl_flash.o objdir/fsl_flexbus.o objdir/fsl_flexcan.o objdir/fsl_ftm.o objdir/fsl_gpio.o objdir/fsl_i2c.o objdir/fsl_i2c_edma.o objdir/fsl_llwu.o objdir/fsl_lptmr.o objdir/fsl_mmcau.o objdir/fsl_pdb.o objdir/fsl_pit.o objdir/fsl_pmc.o objdir/fsl_rcm.o objdir/fsl_rnga.o objdir/fsl_rtc.o objdir/fsl_sai.o objdir/fsl_sai_edma.o objdir/fsl_sdhc.o objdir/fsl_sim.o objdir/fsl_smc.o objdir/fsl_sysmpu.o objdir/fsl_uart.o objdir/fsl_uart_edma.o objdir/fsl_vref.o objdir/fsl_wdog.o objdir/k24f_hal.o objdir/system_MK24F12.o objdir/aes.o objdir/aes-independant.o objdir/startup_MK24F12.o --output simpleserial-base-CW308_K24F.elf -Xlinker --gc-sections -Xlinker -static -Xlinker -z -Xlinker muldefs -T .././hal/k24f/MK24FN1M0xxx12_flash.ld --specs=nano.specs --specs=nosys.specs -Wl,--start-group -L .././hal/k24f/ -l:lib_mmcau.a -lm -lc -lgcc -lnosys -Wl,--end-group -Wl,-Map=simpleserial-base-CW308_K24F.map,--cref -lm . Creating load file for Flash: simpleserial-base-CW308_K24F.hex arm-none-eabi-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature simpleserial-base-CW308_K24F.elf simpleserial-base-CW308_K24F.hex . Creating load file for EEPROM: simpleserial-base-CW308_K24F.eep arm-none-eabi-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \ --change-section-lma .eeprom=0 --no-change-warnings -O ihex simpleserial-base-CW308_K24F.elf simpleserial-base-CW308_K24F.eep || exit 0 . Creating Extended Listing: simpleserial-base-CW308_K24F.lss arm-none-eabi-objdump -h -S -z simpleserial-base-CW308_K24F.elf > simpleserial-base-CW308_K24F.lss . Creating Symbol Table: simpleserial-base-CW308_K24F.sym arm-none-eabi-nm -n simpleserial-base-CW308_K24F.elf > simpleserial-base-CW308_K24F.sym Size after: text data bss dec hex filename 11600 120 2388 14108 371c simpleserial-base-CW308_K24F.elf +-------------------------------------------------------- + Built for platform k24f Target +--------------------------------------------------------
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 a number of operations. We won't actually use the input data. To do so, open the file
simpleserial-base.c
with a text editor such as Programmer's Notepad (which ships with WinAVR).Find the following code block towards the end of the file, which may look different if you just completed Tutorial_B1_Building_a_SimpleSerial_Project.
/********************************** * 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 do some work with no-ops and multiplication instructions:
/********************************** * 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 asm volatile( "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" :: ); asm volatile( "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" :: ); trigger_low(); /* End user-specific code here. * ********************************/
Change the terminal to the directory with your source, and run the same
make
command you did earlier to build the firmware. Remember you can press the up arrow on the keyboard to get recently typed commands in most OSes:
Hardware Setup
The hardware setup is the same as in Tutorial B1 Building a SimpleSerial Project. The setup is repeated in the drop down menus below, but if you've already done that, skip to the next section.
CW1173 (Lite) Hardware Setup
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:
=== CW1173 (Lite) Hardware Setup === 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:
CW1200 (Pro) Hardware Setup
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"
=== CW1200 (Pro) Hardware Setup === 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"
CW308 (UFO) Hardware Setup
Coming soon!
=== CW308 (UFO) Hardware Setup === Coming soon!
Programming the Target
Programming the target is the same as in previous tutorials. The steps are repeated in the drop down menus below.
Programming the XMEGA Target
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 XMEGA target, 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
(or whatever your hex file is called), 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!
- You can now close the programming dialog if you'd like. If you're frequently reprogramming the target, you may want to leave this open.
=== Programming the XMEGA Target === 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 XMEGA target, 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
(or whatever your hex file is called), 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!
- You can now close the programming dialog if you'd like. If you're frequently reprogramming the target, you may want to leave this open.
Programming the STM32F3 (CW303 Arm) Target
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 CW303 Arm target, 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_stm32f_aes.py" file:
You'll notice the contents of the script contain the following setup: 1 scope.gain.gain = 45
2 scope.adc.samples = 5000
3 scope.adc.offset = 0
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"
11
12 target.baud=38400
Line 1: Sets the input ADC gain
Line 2: Sets the number of samples to record as 5000 samples long (this is normally used for the AES algorithm).
Line 3: Sets an offset of 0 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 ARM target expects.
Line 9: Configures GPIO2 as the TX (Output). This is what the ARM target expects.
Line 10: Sets the "High-Speed 2" (HS2) pin as having the 7.37MHz clock output.
Line 12: Sets the serial communication speed with the target at 38400 baud.You can now program the ARM device! To do so, open the STM32F 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-CWLITEARM.hex
(or whatever your binary is called), 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:If the software freezes and the verification fails after a long period of time, set the Read Block Size to 64 instead of 256.
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!If you'd like, you can close the STM32F programmer dialog. If you frequently reprogram the target, you may want to leave it open.
=== Programming the STM32F3 (CW303 Arm) Target === 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 CW303 Arm target, 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_stm32f_aes.py" file:
You'll notice the contents of the script contain the following setup: 1 scope.gain.gain = 45
2 scope.adc.samples = 5000
3 scope.adc.offset = 0
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"
11
12 target.baud=38400
Line 1: Sets the input ADC gain
Line 2: Sets the number of samples to record as 5000 samples long (this is normally used for the AES algorithm).
Line 3: Sets an offset of 0 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 ARM target expects.
Line 9: Configures GPIO2 as the TX (Output). This is what the ARM target expects.
Line 10: Sets the "High-Speed 2" (HS2) pin as having the 7.37MHz clock output.
Line 12: Sets the serial communication speed with the target at 38400 baud.You can now program the ARM device! To do so, open the STM32F 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-CWLITEARM.hex
(or whatever your binary is called), 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:If the software freezes and the verification fails after a long period of time, set the Read Block Size to 64 instead of 256.
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!If you'd like, you can close the STM32F programmer dialog. If you frequently reprogram the target, you may want to leave it open.
Programming Other Targets
Programming other targets typically requires additional tools, such as a target specific programmer or debugger. Please see the wiki page for your target for additional details. Additionally, you should run connect_simpleserial.py and the associated setup_*.py script before moving on to the rest of the tutorial.
=== Programming Other Targets === Programming other targets typically requires additional tools, such as a target specific programmer or debugger. Please see the wiki page for your target for additional details. Additionally, you should run connect_simpleserial.py and the associated setup_*.py script before moving on to the rest of the tutorial.
Capturing Power Traces
The basic steps to connect to the ChipWhisperer device are described in Tutorial_B1_Building_a_SimpleSerial_Project. They are repeated here as well, however see Tutorial_B1_Building_a_SimpleSerial_Project for pictures & mode details.
- Start ChipWhisperer-Capture
- Under the Python Console tab, find the connect_cwlite_simpleserial.py script and double-click.
- Check there are no errors on the connection.
- Under the Python Console tab, find the relevant setup script for your target (such as setup_cwlite_xmega.py) and double-click.
- Both the Target & Scope should switch to CON and be green circles.
- Open the status monitor (Tools > Encryption Status Monitor).
- 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. The Text In and Text Out aren't actually used in this example, so you can close the Monitor dialog.
At this point you've completed the same amount of information as the previous tutorial. The following section describes how to setup the analog capture hardware, which is new (to you). The following is entirely done in the Scope Settings tab:
- The ADC Freq should show 4x the clock speed of your device (typically 29.5MHz), and the DCM Locked checkbox __MUST__ be checked. If the DCM Locked checkbox is NOT checked, try hitting the Reset ADC DCM button again.
At this point you can hit the Capture 1 button, and see if the system works! You should end up with a window looking like this:
Whilst there is a waveform, you need to adjust the capture settings. There are two main settings of importance, the analog gain and number of samples to capture.
- Under Gain Setting set the Mode to high. Increase the Gain Setting to about 25. You'll be able to adjust this further during experimentation; you may need to increase this depending on your hardware and target device.
- Under Trigger Setup set the Total Samples to 500.
- Try a few more Capture 1 traces, and you should see a 'zoomed-in' waveform.
Modifying the Target
Background on Setup (XMEGA)
The rest of this tutorial will focus on AtXMEGA128D4 (the CW303 XMEGA target), since correlating instructions to power consumption is typically simpler on it. We are comparing the power consumption of two different instructions, the MUL
(multiply) instruction and the NOP
(no operation) instruction. Some information on these two instructions:
- mul
- Multiples two 8-bit numbers together.
- Takes 2 clock cycles to complete
- Intuitively expect fairly large power consumption due to complexity of operation required
- nop
- Does nothing
- Takes 1 clock cycle to complete
- Intuitively expect low power consumption due to core doing nothing
Note that the capture clock is running at 4x the device clock. Thus a single mul
instruction should span 8 samples on our output graph, since it takes 4 samples to cover a complete clock cycle.
Initial Code
The initial code has a power signature something like this (yours will vary based on various physical considerations, and depending if you are using an XMEGA or AVR device):
Note that the 10 mul
instructions would be expected to take 80 samples to complete, and the 10 nop
instructions should take 40 samples to complete. By modifying the code we can determine exactly which portion of the trace is corresponding to which operations.
Increase number of NOPs
We will then modify the code to have twenty NOP operations in a row instead of ten. The modified code looks like this:
/********************************** * Start user-specific code here. */ trigger_high(); asm volatile( "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" :: ); asm volatile( "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" :: ); asm volatile( "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" :: ); trigger_low(); /* End user-specific code here. * ********************************/
Note that the mul
operation takes 2 clock cycles on the AVR, and the nop
operation takes 1 clock cycles. Thus we expect to now see two areas of the power trace which appear to take approximately the same time. The resulting power trace looks like this:
Pay particular attention to the section between sample number 0 & sample number 180. It is in this section we can compare the two power graphs to see the modified code. We can actually 'see' the change in operation of the device! It would appear the nop
is occuring from approximately 10-90, and the mul
occuring from 90-170.
Add NOP loop after MUL
Finally, we will add 10 more NOPs after the 10 MULs. The code should look something like this:
/********************************** * Start user-specific code here. */ trigger_high(); asm volatile( "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" :: ); asm volatile( "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" :: ); asm volatile( "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" "mul r0,r1" "\n\t" :: ); asm volatile( "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" :: ); trigger_low(); /* End user-specific code here. * ********************************/
With an output graph that looks like this:
Background on Setup (Arm)
For the rest of this tutorial, we'll be focusing on the STM32F3, which is the microcontroller on the CW303 Arm target (though other targets should demonstrate the same principles). Since the STM32F3 is an Arm Cortex M4 device, we'll need to refer to the Cortex M4 Instruction Set and the Cortex M4 Instruction Set Summary.
The first thing we'll do is replace the nop
instructions, since from it's documentation page we can see the processor may not execute them. Instead, let's add some add.w
(which is the 32 bit wide version of the add instruction) instructions. We'll be doing this since the mul
instruction is always 32 bits wide and the 16 bit thumb instruction has a different power profile than the 32 bit Arm instruction. From the earlier links, we can see that both add and mul take 1 cycle each to complete.
add.w
instructions and 10 mul
instructions:trigger_high();
asm volatile(
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
::
);
asm volatile(
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
::
);
trigger_low();
We can see the add.w
and mul
instructions near the beginning, staring about 10 samples in and ending about 90 samples in. There's not really any difference that we can see between the two, but we can see that they take up about 80 samples (20 microcontroller clock cycles) as we expect.
udiv
instructions. From the Cortex M4 Instruction Set Summary, we can see that udiv
(unsigned divide) instructions take between 2 and 12 cycles to complete (effectively depending on how big the numbers we're dividing are). We'll be dividing r0
by r0
, meaning we expect that every instruction after the first should take 2 cycles. It should have higher power consumption too, since dividing is typically a fairly complex operation:trigger_high();
asm volatile(
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
"add.w r0, r0" "\n\t"
::
);
asm volatile(
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
"mul r0, r0" "\n\t"
::
);
asm volatile(
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
"udiv r0, r0" "\n\t"
::
);
trigger_low();
As we expected, we can see periods of high power consumption measuring about 80 samples in total right after the add.w
and mul
instructions. Interestingly, the udiv
instructions seem to be split into 2 sets of operations. As a final check, we can add some more mul
instructions and see the udiv
instructions move down (and also break into more sections):
Clock Phase Adjustment
A final area of interest is the clock phase adjustment. The clock phase adjustment is used to shift the ADC sample clock from the actual device clock by small amounts. This will affect the appearance of the captured waveform, and in more advanced methods is used to improve the measurement.
The phase adjustment is found under the Phase Adjust option of the ADC Clock setting:
To see the effect this has, first consider an image of the power measured by a regular oscilloscope (at 1.25GS/s):
And the resulting waveforms for a variety of different phase shift settings:
The specifics of the capture are highly dependent on each ChipWhisperer board & target platform. The phase shift allows customization of the capture waveform for optimum performance, however what constitutes 'optimum performance' is highly dependent on the specifics of your algorithm.
Conclusion
In this tutorial you have learned how power analysis can tell you the operations being performed on a microcontroller. In future work we will move towards using this for breaking various forms of security on devices. In particular, Tutorial B3-1 Timing Analysis with Power for Password Bypass will examine how we can use this information to exploit a password check.