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
m (fix line break)
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.}}
 +
 +
{{Infobox tutorial
 +
|name                  = B2: Viewing Instruction Power Differences
 +
|image                  =
 +
|caption                =
 +
|software versions      =
 +
|capture hardware      = CW-Lite, CW-Lite 2-Part, CW-Pro
 +
|Target Device          =
 +
|Target Architecture    = XMEGA
 +
|Hardware Crypto        = No
 +
|Purchase Hardware      =
 +
}}
 +
 
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.
 
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.
  

Revision as of 15:03, 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.

B2: Viewing Instruction Power Differences
Target Architecture XMEGA
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 a 'add' instruction and a 'mul' instruction.

Prerequisites

You should have already completed Tutorial_B1_Building_a_SimpleSerial_Project. This tutorial assumes you are capable of building a new AVR/XMEGA code, programming the code, and connecting to the ChipWhisperer.

Setting Up the Example

  1. Copy the directory simpleserial-base which is found at chipwhisperer\hardware\victims\firmware\ of the chipwhisperer release to a new directory called simpleserial-base-lab2. You must keep it in the same directory, as it will reference other files within that directory for the build process.

    If you just completed Tutorial_B1_Building_a_SimpleSerial_Project, you can simply reuse that code (this builds upon it).

    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).

  2. 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. *
     ********************************/
    
  3. 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. *
     ********************************/
    
  4. Change the terminal to the directory with your source, and run make to build the system. Remember you can press the up arrow on the keyboard to get recently typed commands in most OSes:

    make

    Which should have the following output, you will want to check the build platform is what you expect:

    ...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:    1568 bytes (1.1% Full)
    (.text + .data + .bootloader)
    
    Data:        224 bytes (2.7% Full)
    (.data + .bss + .noinit)
    
    
    Built for platform CW-Lite XMEGA
    
    -------- end --------
  5. Following the instructions given in Tutorial_B1_Building_a_SimpleSerial_Project, program the AVR or XMEGA with your new code. Note you __do not__ need to close the programming window. If you will be doing frequent modifications to the source code, this can simplify your life since you only need to hit the Program button in AVRStudio to download new code.
  6. Ensure the hardware is setup as in Tutorial_B1_Building_a_SimpleSerial_Project. If using the CW1002, ensure the SMA cable is also connected as described in the previous 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.

  1. Start ChipWhisperer-Capture
  2. Under Generic Settings tab, as the Scope Module, select the ChipWhisperer/OpenADC option.
  3. Under Generic Settings tab, as the Target Module, select the Simple Serial option.
  4. Switch to the Target Settings tab, and as the connection, select the ChipWhisperer (for CW1002) or NewAE USB (CWLite/CW1200) (for ChipWhisperer Lite/Pro).
  5. Switch to the Scope Settings tab, and ensure the connection is set to either ChipWhisperer (for CW1002) or NewAE USB (CWLite/CW1200) (for ChipWhisperer Lite/Pro).
  6. Run the master connect (click the button labeled Master: DIS). Both the Target & Scope should switch to CON and be green circles.
  7. For the CW1173/CW1180/Cw1200 (ChipWhisperer-Lite/Pro based systems), perform the following:

    1. Set the CLKGEN frequency to 7.37 MHz
    2. Set the Target HS-IO Out as CLKGEN
  8. If targetting an XMEGA board (either the ChipWhisperer-Lite XMEGA default target, or the XMEGA on the multi-target board), perform the following:
    1. Set Target IO1 as Serial RXD
    2. Set Target IO2 as Serial TXD
  9. Open the status monitor (Tools > Encryption Status Monitor).
10. Hit the Run 1 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.

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:

image

  1. Under Trigger Setup set the Mode to rising edge. This means the system will trigger on a rising edge logic level:

image

Beginning of a hardware specific section for CW1002

  1. For the CW1002 (ChipWhisperer Capture Rev 2) hardware only, perform the following:
    1. Under the Trigger Pins unselect the Front Panel A as an option, and select Target IO4 (Trigger Line). This will mean only the trigger pin coming from the AVR target is used to trigger the capture.
    2. In the same area, select the Clock Source as being from Target IO-IN

      image

    3. You can monitor the Freq Counter option, which measures the frequency being used on the EXTCLK input. This should be 7.37 MHz, which is the oscillator on the multi-target board.
    4. Change the ADC Clock source as being EXTCLK x4 via DCM. This routes the external clock through a 4x multiplier, and routes it to the ADC.

End of CW1002 (ChipWhisperer Capture Rev 2) specific section


  1. For the CW1173/CW1180/Cw1200 (ChipWhisperer-Lite/Pro based hardware), perform the following:
  1. Change the ADC Clock source as being CLKGEN x4 via DCM. This routes the device clock through a 4x multiplier, and routes it to the ADC.
  1. Hit the Reset ADC DCM button.
  2. image
  1. The ADC Freq should show 29.5 MHz (which is 4x 7.37 MHz), and the DCM Locked checkbox __MUST__ be checked. If the DCM Locked checkbox is NOT checked, try hitting the Reset ADC DCM button again.
  2. 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:

    image

    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.

  3. image
  1. Under Gain Setting set the Mode to high. Increase the Gain Setting to about 25. You'll be able to adjust this further during experimentations, you may need to increase this depending on your hardware and target device. For the multi-target board with the CW1002 you will probably need to set this around 40.
  2. Under Trigger Setup set the Total Samples to 500.
  3. Try a few more Capture 1 traces, and you should see a 'zoomed-in' waveform.

Modifying the Target

Background on Setup

This tutorial is using either an AtMega328p which is an Atmel AVR device, or AtXMEGA128D4 which is an Atmel XMEGA device. 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):

image

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:

image

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:

image

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.

image

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:

image

To see the effect this has, first consider an image of the power measured by a regular oscilloscope (at 1.25GS/s):

image

And the resulting waveforms for a variety of different phase shift settings:

image

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.