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 B2 Viewing Instruction Power Differences"

From ChipWhisperer Wiki
Jump to: navigation, search
(Background on Setup)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Warningbox|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.}}
+
{{Warningbox|This tutorial has been updated for ChipWhisperer 5 release. If you are using 4.x.x or 3.x.x see the "V4" or "V3" link in the sidebar.}}
  
 
{{Infobox tutorial
 
{{Infobox tutorial
Line 13: Line 13:
 
}}
 
}}
  
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 a 'add' instruction and a 'mul' instruction.
+
<!-- To edit this, edit Template:Tutorial_boilerplate -->
 +
{{Tutorial boilerplate}}
  
== Prerequisites ==
+
* Jupyter file: '''PA_Intro_2-Instruction_Differences.ipynb'''
  
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 ==
+
== XMEGA Target ==
  
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.
+
See the following for using:
{{CollapsibleSection
+
* ChipWhisperer-Lite Classic (XMEGA)
|intro = === Building for CWLite with XMEGA Target ===
+
* ChipWhisperer-Lite Capture + XMEGA Target on UFO Board (including NAE-SCAPACK-L1/L2 users)
|content= Building for XMEGA}}
+
* ChipWhisperer-Pro + XMEGA Target on UFO Board
  
{{CollapsibleSection
+
https://chipwhisperer.readthedocs.io/en/latest/tutorials/pa_intro_2-openadc-cwlitexmega.html#tutorial-pa-intro-2-openadc-cwlitexmega
|intro = === Building for CWLite with Arm Target ===
+
|content= Building for Arm}}
+
  
{{CollapsibleSection
+
== ChipWhisperer-Lite ARM / STM32F3 Target ==
|intro = === Building for Other Targets ===
+
|content= Building for Other Targets}}
+
  
<h2> Modifying the Basic Example </h2>
+
See the following for using:
<ol style="list-style-type: decimal;">
+
* ChipWhisperer-Lite 32-bit (STM32F3 Target)
<li><p>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 <code>simpleserial-base.c</code> with a text editor such as Programmer's Notepad (which ships with WinAVR).</p></li>
+
* ChipWhisperer-Lite Capture + STM32F3 Target on UFO Board (including NAE-SCAPACK-L1/L2 users)
<li><p>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]].</p>
+
* ChipWhisperer-Pro + STM32F3 Target on UFO Board
<source lang="c">/**********************************
+
* Start user-specific code here. */
+
trigger_high();
+
  
//16 hex bytes held in 'pt' were sent
+
https://chipwhisperer.readthedocs.io/en/latest/tutorials/pa_intro_2-openadc-cwlitearm.html#tutorial-pa-intro-2-openadc-cwlitearm
//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();
+
== ChipWhisperer Nano Target ==
/* End user-specific code here. *
+
********************************/</source></li>
+
<li><p>Modify it to do some work with no-ops and multiplication instructions:</p>
+
<source lang="c">/**********************************
+
* Start user-specific code here. */
+
trigger_high();
+
  
//16 hex bytes held in 'pt' were sent
+
See the following for using:
//from the computer. Store your response
+
* ChipWhisperer-Nano
//back into 'pt', which will send 16 bytes
+
//back to computer. Can ignore of course if
+
//not needed
+
  
asm volatile(
+
https://chipwhisperer.readthedocs.io/en/latest/tutorials/pa_intro_2-cwnano-cwnano.html#tutorial-pa-intro-2-cwnano-cwnano
"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. *
+
********************************/</source></li>
+
<li><p>Change the terminal to the directory with your source, and run the same <code>make</code> 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:</p></ol>
+
 
+
== Hardware Setup ==
+
The hardware setup is the same as in [[Tutorial B1 Building a SimpleSerial Project|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.
+
{{CollapsibleSection
+
|intro = === CW1173 (Lite) Hardware Setup ===
+
|content= CWLite HW Setup}}
+
 
+
{{CollapsibleSection
+
|intro = === CW1200 (Pro) Hardware Setup ===
+
|content= CW1200 HW Setup}}
+
 
+
{{CollapsibleSection
+
|intro = === CW308 (UFO) Hardware Setup ===
+
|content= CW308 HW Setup}}
+
 
+
== Programming the Target ==
+
Programming the target is the same as in previous tutorials. The steps are repeated in the drop down menus below.
+
 
+
{{CollapsibleSection
+
|intro = === Programming the XMEGA Target ===
+
|content= Programming XMEGA}}
+
 
+
{{CollapsibleSection
+
|intro = === Programming the STM32F3 (CW303 Arm) Target ===
+
|content= Programming Arm}}
+
 
+
{{CollapsibleSection
+
|intro = === Programming Other Targets ===
+
|content= Programming Other}}
+
 
+
 
+
== 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 &amp; mode details.
+
 
+
<ol style="list-style-type: decimal;">
+
<li>Start ChipWhisperer-Capture</li>
+
<li>Under the ''Python Console'' tab, find the ''connect_cwlite_simpleserial.py'' script and double-click.</li>
+
<li>Check there are no errors on the connection.</li>
+
<li>Under the ''Python Console'' tab, find the relevant setup script for your target (such as setup_cwlite_xmega.py) and double-click.</li>
+
<li>Both the Target &amp; Scope should switch to ''CON'' and be green circles.</li>
+
<li>Open the status monitor (<i>Tools > Encryption Status Monitor</i>).</li>
+
<li>Hit the ''Run 1'' [[File:Capture One Button.PNG|image]] 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.</li>
+
 
+
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:
+
 
+
[[File:04_ADC_Clock_2_1.png|image]]</ol>
+
 
+
<ol start="8" style="list-style-type: decimal;">
+
<li>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.</li>
+
<li><p>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:</p>
+
<p>[[File:05_Low_Gain.PNG|image|1250px]]</p>
+
<p>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.</p></li>
+
 
+
[[File:06_high_gain.PNG|image|1250px]]</ol>
+
 
+
<ol start="16" style="list-style-type: decimal;">
+
<li>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.</li>
+
<li>Under ''Trigger Setup'' set the ''Total Samples'' to ''500''.</li>
+
<li>Try a few more ''Capture 1'' traces, and you should see a 'zoomed-in' waveform.</li></ol>
+
 
+
== 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 <code>MUL</code> (multiply) instruction and the <code>NOP</code> (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 <code>mul</code> 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):
+
 
+
[[File:cap_nop_mul.png|image]]
+
 
+
Note that the 10 <code>mul</code> instructions would be expected to take 80 samples to complete, and the 10 <code>nop</code> 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:
+
 
+
<blockquote><source lang="c">/**********************************
+
* 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. *
+
********************************/</source></blockquote>
+
Note that the <code>mul</code> operation takes 2 clock cycles on the AVR, and the <code>nop</code> 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:
+
 
+
[[File:cap_doublenop_mul.png|image]]
+
 
+
Pay particular attention to the section between sample number 0 &amp; 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 <code>nop</code> is occuring from approximately 10-90, and the <code>mul</code> 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:
+
 
+
<blockquote><source lang="c">/**********************************
+
* 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. *
+
********************************/</source></blockquote>
+
With an output graph that looks like this:
+
 
+
<blockquote>[[File:cap_doublenop_mul_nop.png|image]]
+
</blockquote>
+
 
+
==== Comparison of All Three ====
+
 
+
The following graph lines the three options up. One can see where adding loops of different operations shows up in the power consumption.
+
 
+
<blockquote>[[File:nop_mul_comparison.png|image]]
+
</blockquote>
+
 
+
=== Background on Setup (Arm) ===
+
While this tutorial can be performed on any supported target, results will vary between targets. Arm targets, for example, have pipelining and much smaller differences in power consumption between instructions compared to the XMEGA target. Instead of trying to spot the differences ourselves, we'll be using the persistence feature of ChipWhisperer Capture to compare two traces. The instructions we'll be focusing on here are <code>nop</code> and <code>mul</code>.
+
 
+
First, clear the display of the current trace (if you have one):
+
 
+
[[File:Clear Display.png|frameless|336x336px]]
+
 
+
Next, enable persistence:
+
 
+
[[File:Persistence.png|frameless|336x336px]]
+
 
+
Now hit the ''Run 1'' [[File:Capture One Button.PNG|image]] button and capture a single trace. It's recommended that you only capture a single trace. You may also want to change the colour of the next trace to make differentiating them easier:
+
 
+
[[File:Set Colour.png|frameless|336x336px]]
+
 
+
You should now have something that looks like this:
+
 
+
[[File:B2 cap1.PNG|frameless|1139x1139px]]
+
 
+
==== Swapping NOP and MUL ====
+
Next, we're going to swap when the nop and mul instructions happen to see if we can identify which is which.:<syntaxhighlight lang="c">
+
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"
+
::
+
);
+
</syntaxhighlight>Build and program the new firmware onto the device. Then capture another trace. You should have something that looks like:
+
 
+
[[File:B2 cap2.PNG|frameless|1149x1149px]]
+
 
+
We can spot some areas with differences, but it's tough to see where the different instructions are happening. Let's zoom in (click and drag over the area you want to zoom into, right click > View All to go back to the original view):
+
 
+
[[File:B2 cap3.PNG|frameless|1126x1126px]]
+
 
+
As you can see, there's still very little difference between the two, but the differences are there!
+
 
+
== 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:
+
 
+
<blockquote>[[File:phasesetting.png|image]]
+
</blockquote>
+
To see the effect this has, first consider an image of the power measured by a regular oscilloscope (at 1.25GS/s):
+
 
+
<blockquote>[[File:scope_real.png|image]]
+
</blockquote>
+
And the resulting waveforms for a variety of different phase shift settings:
+
 
+
[[File:phase_differences.png|image]]
+
 
+
The specifics of the capture are highly dependent on each ChipWhisperer board &amp; 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.
+
 
+
== Links ==
+
 
+
{{Template:Tutorials}}
+
[[Category:Tutorials]]
+

Latest revision as of 05:19, 29 July 2019

This tutorial has been updated for ChipWhisperer 5 release. If you are using 4.x.x or 3.x.x see the "V4" or "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. In ChipWhisperer 5 Release, the software documentation is now held outside the wiki. See links below.

To see background on the tutorials see the Tutorial Introduction on ReadTheDocs, which explains what the links below mean. These wiki pages (that you are reading right now) only hold the hardware setup required, and you have to run the Tutorial via the Jupyter notebook itself. The links below take you to the expected Jupyter output from each tutorial, so you can compare your results to the expected/known-good results.

Running the tutorial uses the referenced Jupyter notebook file.

  • Jupyter file: PA_Intro_2-Instruction_Differences.ipynb


XMEGA Target

See the following for using:

  • ChipWhisperer-Lite Classic (XMEGA)
  • ChipWhisperer-Lite Capture + XMEGA Target on UFO Board (including NAE-SCAPACK-L1/L2 users)
  • ChipWhisperer-Pro + XMEGA Target on UFO Board

https://chipwhisperer.readthedocs.io/en/latest/tutorials/pa_intro_2-openadc-cwlitexmega.html#tutorial-pa-intro-2-openadc-cwlitexmega

ChipWhisperer-Lite ARM / STM32F3 Target

See the following for using:

  • ChipWhisperer-Lite 32-bit (STM32F3 Target)
  • ChipWhisperer-Lite Capture + STM32F3 Target on UFO Board (including NAE-SCAPACK-L1/L2 users)
  • ChipWhisperer-Pro + STM32F3 Target on UFO Board

https://chipwhisperer.readthedocs.io/en/latest/tutorials/pa_intro_2-openadc-cwlitearm.html#tutorial-pa-intro-2-openadc-cwlitearm