As of August 2020 the site you are on (wiki.newae.com) is deprecated, and content is now at rtfm.newae.com.

Changes

Jump to: navigation, search

Tutorial B1 Building a SimpleSerial Project

18,953 bytes removed, 13:05, 29 July 2019
no edit summary
{{Warningbox|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 has been updated for ChipWhisperer software5 release. This can be used for building your own system which If you wish to 'break'are using 4.x.x or 3.x.x see the "V4" or "V3" link in the sidebar.}}
{{Infobox tutorial|name = What is B1: Building a SimpleSerial Project|image =|caption = |software versions =|capture hardware = CW-Lite, CW-Lite 2-Part, CW-Pro|Target Device = |Target Architecture = XMEGA/ARM/Other|Hardware Crypto = No|Purchase Hardware = }}<!-- To edit this, edit Template:Tutorial_boilerplate -->{{Tutorial boilerplate}}
SimpleSerial is the communications protocol used for almost all of the ChipWhisperer demo project. It* Jupyter file: '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''PA_Intro_1-N-1Firmware_Build_Setup.ipynb'''
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.== XMEGA Target ==
The See the following message types are definedfor 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
; <code>x</code>https: 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.; <code>k00112233445566778899AABBCCDDEEFF\\n</code>: Loads the encryption key <code>00112233445566778899AABBCCDDEEFF</code> into the systemchipwhisperer. If not called the system may use some default keyreadthedocs.; <code>pAABBCCDDEEFF00112233445566778899\\n<io/code>: Encrypts the data <code>AABBCCDDEEFF00112233445566778899<en/code> with the key loaded with the 'k' command. The system will respond with a string starting with r, as shown next.; <code>rCBBD4A2B34F2571758FF6A797E09859D\\n<latest/code>: 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 <code>cbbd4a2b34f2571758ff6a797e09859d<tutorials/code>pa_intro_1-openadc-cwlitexmega.html#tutorial-pa-intro-1-openadc-cwlitexmega
= Building the Basic Example = ChipWhisperer-Lite ARM / STM32F3 Target ==
You'll need to have installed avr-gcc and avr-libc. You may have already done this by following See the installation guide, or if following for using the :* ChipWhisperer-VM it comes prepared with avrLite 32-gcc already setup. See the [[Installing_ChipWhisperer]] guide for details.bit (STM32F3 Target)* ChipWhisperer-Lite Capture + STM32F3 Target on UFO Board (including NAE-SCAPACK-L1/L2 users)* ChipWhisperer-Pro + STM32F3 Target on UFO Board
Once you have a working compiler (check by typing 'avrhttps://chipwhisperer.readthedocs.io/en/latest/tutorials/pa_intro_1-gcc' at the command line openadc- if using Windows you may need to setup a special batch file to provide you with a avr-gcc command prompt)cwlitearm.html#tutorial-pa-intro-1-openadc-cwlitearm
<ol style="list-style-type: decimal;"><li>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 <code>simpleserial-base</code> which is found at <code>chipwhisperer\hardware\victims\firmware\</code> of the chipwhisperer release to a new directory called <code>simpleserial-base-lab1</code>. You must keep it in the same directory, as it will reference other files within that directory for the build process.</li><li><dl><dt>Open a terminal with avr-gcc in the path. If using Windows the sidebar on the [[Installing_ChipWhisperer]] page - you can either</dt><dd><p>add WinAVR to your system path, or you can run the 'winavr.bat' file suggested.</p></dd></dl></li><li><p>Change the terminal to the newly copied directory. For example:</p><pre>cd c:\chipwhisperer\hardware\victims\firmware\simpleserial-base-lab1</pre></li><li><p>Then run <code>make</code> to build the system:</p><pre>make</pre><p>Which should have the following output:</p><pre>...Bunch of lines removed...Creating Extended Listing: simpleserial-base.lssavr-objdump -h -S -z simpleserial-base.elf &gt; simpleserial-base.lss= ChipWhisperer Nano Target ==
Creating Symbol TableSee the following for using: simpleserial-base.symavr-nm -n simpleserial-base.elf &gt; simpleserial* ChipWhisperer-base.symNano
Size afterhttps: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 --------</pre></li><li><p>Pay special attention to the &quot;Build platform&quot; in that outputchipwhisperer. Ensure this matches your desired target - if not, open the <code>makefile</code> located in the same directory you ran <code>make</code> fromreadthedocs. If using Windows you will need a code editor - if you installed WinAVR, it would have installed ''Programmer's Notepad'' which is an excellent choice. Scrolling down in the makefile you should see the following lines:<io/p><pre>######### Specify your platform here #Multi-Target Board, AVR Device (ATMega328P)#PLATFORM = CW301_AVR #Multi-Target Board, XMEGA Device#PLATFORM = CW301_XMEGA #CW-Lite XMEGA Target Device (XMEGA128D4)PLATFORM = CW303 #NOTDUINO Kit (ATMega328P)#PLATFORM = CW304<en/pre><p>Adjust the target platform by uncommenting (comments are lines starting with <code>#<latest/code>) the appropriate platform, and commenting out the other choices.</p></li><li>Run <code>make</code> again, and ensure the correct plaform is now printed.</li></ol> = 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 <code>simpleserial-base.c</code> with a code editor such as ''Programmer's Notepad'' (which ships with WinAVR). <ol style="list-style-type: decimal;"><li><p>Find the following code block towards the end of the file:</p><syntaxhighlight lang="c">/********************************** * 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. * ********************************/</syntaxhighlight></li><li><p>Modify it to increment the value of each sent data byte:</p><syntaxhighlight lang="c">/********************************** * 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. * ********************************/</syntaxhighlight></li><li>Rebuild the example using the <code>make</code> command. Remember you can press the up arrow on the keyboard to get recently typed commands in most OSes.</li></ol> = Completing Tutorial with CW1002 (ChipWhisperer Capture Rev2) = Note the following is only applicable to the use of the ChipWhisperer-Capture Rev 2 hardware (aka the CW1002). See [[#Completing_Tutorial_with_CW1173_(ChipWhisperer-Lite_Board)]] if you are not using this hardware. == Setting up the Hardware == This tutorial uses the [[CW1002_ChipWhisperer_Capture_Rev2]] hardware along with the [[CW301_Multi-Target]] board. This hardware is the standard setup for all basic tutorials. This example uses the Atmel AVR in 28-pin DIP programmed with a ''simpleserial'' communications protocol. This is the default firmware programmed into the devices, so you shouldn't need to do anything. If you've erased the device, you can see programming instructions in the [[Installing_ChipWhisperer]] section. The Multi-Target board should be plugged into the ChipWhisperer Capture Rev2 via the 20-pin target cable. The ''VOUT'' SMA connector is wired to the ''LNA'' input on the ChipWhisperer-Capture Rev2 front panel. The general hardware setup is as follows: <blockquote>[[File:hw-1.jpg|image]] # 20-Pin Header connects Multi-Target to Capture Hardware# VOUT Connects to SMA Cable# SMA Cable connects to 'LNA' on CHA input# USB-Mini connects to side (NB: Confirm jumper settings in next section first)</blockquote>Jumpers on the Multipa_intro_1-Target Victim board are as follows: <blockquote>[[File:hwcwnano-2cwnano.jpg|600px|image]] html# NO jumpers mounted in XMEGA Portion or SmartCard Portion (JP10-JP15, JP19, JP7-JP8, JP17)# 3.3V IO Level (JP20 set to INT.)# The 7.37 MHz oscillator is selected as the CLKOSC source (JP18)# The CLKOSC is connected to the AVR CLock Network, along with connected to the FPGAIN pin (JP4)# The TXD &amp; RXD jumpers are set (JP5, JP6)# Power measurement taken from VCC shunt (JP1)# The TRIG jumper is set (JP28) (NOTE: Early revisions of the multi-target board do not have the TRIG jumper and you can ingore this). For more information on these jumper settings see [[CW301_Multi-Target]] .</blockquote> == Setting up the Software == 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 Capture Rev2 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 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 [[Tutorial_B5_Breaking_AES_(Straightforward)]].   == Programming the Example == <ol style="list-style-type: decimal;"><li>We assume the hardware is already connected as at the beginning of this tutorial. If not go back &amp; confirm this hardware setup.</li><li>Next, you will need to program the AVR itself. On Windows we will make use of the free [http://www.atmel.com/tools/STUDIOARCHIVE.aspx Atmel Studio 4.19]. You can find a direct link here: [http://www.atmel.com/System/BaseForm.aspx?target=tcm:26-17924 Direct Link to Atmel Studio 4.19 Binary] which will require you to enter an email address to receive the download link. Note it is possible to use <code>avrdude</code>, a commandpa-line program which is part of WinAVR instead if you wish. However since many people find the graphical interface of AVRStudio easier, this guide will use AVRStudio. Be sure to install the USB drivers as part of the package.</li><li>Plug in the USBintro-A Connector on the rear side of the ChipWhisperer Rev2. This should trigger the driver installation, which will detect the device as a ''AVR-ISP MK2''. You can leave the USB-Mini cable plugged in during this operation, or unplug the USB-Mini cable from the ChipWhisperer &amp; plug in the USB-A cable.</li><li><p>Once AVR Studio is installed, open the main window. From the toolbar select either the ''Con'' or ''AVR'' icon, and select the ''AVR-ISP MK-II'' Device:</p><p>[[File:studio4-connect.png|image]]</p></li><li><p>In the window that opens, select the ''Main'' tab. Select the device type as ''AtMega328P'', and hit ''Read Signature''. You should get an indication that the device signature was successfully read!</p><p>[[File:studio4-read-signature.png|image]]</p></li><li><p>Finally we can program the chip. To do so switch to the ''Program'' tab, select the <code>simpleserial_nocrypto.hex</code> file that was generated in Step 4, and hit ''Program''. If it's successful you should see some output data saying so.</p><p>[[File:studio4-program.png|image]]</p></li></ol> <blockquote>'''warning''' Be sure to select the correct .hex file! Otherwise the system won't work &amp; it can be frusturating to troubleshoot. It's a good idea to look at the 'last modified' date which shows up when you go to select the file. Check that it roughly corresponds to when you compiled the file.</blockquote>That's it! You've now built a custom application &amp; programmed it into the AVR. We haven't yet verified it's working, which is the next step. == Communicating from CW-Capture Software == Next, open the CW-Capture software. Then perform the following steps: [[File:genericsetup.png|image]] # Switch to the ''General Settings'' tab# As the ''Scope Module'', select the ''ChipWhisperer/OpenADC'' option# As the ''Target Module'', select the ''Simple Serial'' option Next, you'll have to configure the target module: [[File:targetsetup.png|image]] <ol start="4" style="list-style-type: decimal;"><li>Switch to the ''Target Settings'' tab</li><li>As the ''connection'', select the ''ChipWhisperer'' option</li></ol> Now, download the FPGA Firmware: [[File:downloadfpga.png|image]] <ol start="6" style="list-style-type: decimal;"><li>Optional: Run the ''Download CW Firmware'' tool. You should have configured this already before. Note that from release 0.09 of ChipWhisperer the FPGA is automatically programmed when you attempt to connect, so you can skip this step if using a recent release (i.e. any release in 2015 or later).</li><li>If you switch to the ''Debug Logging'' output, you should see an indication the FPGA was programmed. If you were already using the device, it will skip the download. Normally you can skip steps 6 &amp; 7 if you've already performed the FPGA download once since powering on the device. Note that</li></ol> [[File:connect.png|image]] <ol start="8" style="list-style-type: decimal;"><li>Press the button labeled ''Master: DIS'', where DIS has a circle around it. If it works, it will switch to green and say ''CON''.</li></ol> [[File:connect_run.png|image]] <ol start="9" style="list-style-type: decimal;"><li>Switch to the ''General Settings'' tab.</li><li>Hit the ''Open Monitor'' button.</li><li>Resize the monitor window. The monitor will show sent &amp; received data to the target.</li><li>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.</li></ol> = Completing Tutorial with CW1173 (ChipWhisperer-Lite Board) = Note the following is only applicable to the use of the ChipWhisperercwnano-Lite hardware (aka the CW1173). See [[#Completing_Tutorial_with_CW1002_(ChipWhisperer_Capture_Rev2)]] if you are not using this hardware. == Setting up the Hardware == This tutorial uses the [[CW1173_ChipWhisperer-Lite]] hardware. No hardware setup is required normally, simply plug in the micro-USB cable: [[File:cw1173_microusb.jpg|image]] Note that under ''no circumstances'' as part of the setup should you use the CW1173 device to hold up furniture: [[File:nofurniture.jpg|image]] == Setting up the Software == 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: [[File:genericsetup.png|image]] # Switch to the ''General Settings'' tab.# As the ''Scope Module'', select the ''ChipWhisperer/OpenADC'' option.# As the ''Target Module'', select the ''Simple Serial'' option. Next, you'll have to configure the scope device in use. [[File:cw1173_scopesetup.png|image]] <ol start="4" style="list-style-type: decimal;"><li>Switch to the ''Scope Settings'' tab.</li><li>As the ''connection'', select the ''ChipWhisperer Lite'' protocol.</li></ol> Next, you'll have to configure the target module: [[File:cw1173_targetsetup.png|image]] <ol start="6" style="list-style-type: decimal;"><li>Switch to the ''Target Settings'' tab</li><li>As the ''connection'', select the ''ChipWhisperer-Lite'' option</li></ol> [[File:connect.png|image]] <ol start="8" style="list-style-type: decimal;"><li>Press the button labeled ''Master: DIS'', where DIS has a circle around it. If it works, it will switch to green and say ''CON''.</li></ol> We will now need to configure the clock to the XMEGA target device. [[File:cw1173_freqoutput1.png|image]] <ol start="9" style="list-style-type: decimal;"><li>Switch to the ''Scope Settings'' tab.</li><li><p>Scroll down to the CLKGEN section, and enter the ''Desired Frequency'' as ''7.37 MHz''. Hit <code>&lt;enter&gt;</code> and you should see the multiply and divide settings change. Be careful of the units - you may need to click just on the numbers and enter that. You can also check the frequency settings using the ''Frequency Counter'' - to use this switch the ''Freq Counter Src'' to be ''CLKGEN''. It should read 7.37 MHz.</p><p>We now need to feed this generated clock to an output pin.</p></li></ol> [[File:cw1173_freqoutput2.png|image]] <ol start="11" style="list-style-type: decimal;"><li><p>Scroll further down until you find the ''Target HS IO-Out'' function, set this to ''CLKGEN''. The clock is now being sent on the ''Target-IO HS2'' pin, which is used by the XMEGA as the input clock.</p><p>For dumb historical reasons the XMEGA and AVR have opposite TX/RX pinouts (on the original multi-target board, the AVR and XMEGA are designed such they can communicate, which required opposite TX/RX connections). Anyway you now need to switch the IO1 and IO2 functions from default:</p></li></ol> [[File:cw1173_txrx.png|image]] <ol start="12" style="list-style-type: decimal;"><li>Change the ''Target IO1'' function to be ''RXD''.</li><li>Change the ''Target IO2'' function to be ''TXD''.</li><li><p>You can now program the XMEGA device! To do so, open the XMEGA Programmer from the ''Tools'' menu:</p><p>[[File:cw1173_xmegaproga.png|image]]</p></li><li><p>Hit the ''Check Signature'' button and confirm the device is detected. If not you may have issues with the clock setup.</p><p>[[File:cw1173_xmegaprogb.png|image]]</p></li><li><p>Using the ''Find'' button, navigate to the <code>simpleserial-base.hex</code> which you built earlier with the <code>make</code> command. You can then press the ''Erase/Program/Verify'' button, and confirm the file is programmed into the XMEGA device:</p><p>[[File:cw1173_xmegaprogc.png|image]]</p><p>Note the programmer dialog not only shows the successful programming status, but also shows when the <code>.hex</code> 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!</p> <p>Finally we can check communications with the programmed file:</p></li></ol> [[File:Cw1173_connectrun.png|image]] <ol start="17" style="list-style-type: decimal;"><li>Close the XMEGA programmer dialog.</li><li>Open the status monitor under <i>Tools > Encryption Status Monitor</i>.</li><li>Resize the monitor window. The monitor will show sent &amp; received data to the target.</li><li>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.</li></ol> = 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: <blockquote><ol style="list-style-type: decimal;"><li><p>You may have to generate the .dep and objdir directories manually before make will work:</p><pre>mkdir .depmkdir objdir</pre></li><li>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 <code>C:\WinAVR-20100110\utils\bin</code>, replacing the existing file.</li><li>For the AVR Studio USB Drivers, you'll need to download a [https://gallery.atmel.com/Products/Details/004ccabd-e18e-431a-8557-83deaea23341 Special Update] from Atmel.</li><li>You may wish to use the &quot;ChipWhisperer Virtual Machine&quot; on newer Windows systems, which does not require any of the above setup.</li></ol></blockquote> {{Template:Tutorials}}[[Category:Tutorials]]cwnano
Approved_users, administrator
366
edits

Navigation menu