Can I put my own crypto code into chipwhisperer for analysis?

Hi Alex

Thank you very much for your help.
Here is my makefile (based on the makefile in your simpleserial-base).


#Target file name (without extension).
#This is the name of the compiled .hex file.
TARGET = simpleserial-base_kyber512

#List C source files here.
#Header files (.h) are automatically pulled in.
SRC += simpleserial-base_kyber512.c

#add library files here
SRC += indcpa.c
SRC += cbd.c
SRC += kem.c
SRC += ntt.c
SRC += poly.c
SRC += polyvec.c
SRC += symmetric-fips202.c
SRC += verify.c
SRC += fastaddsub.S
SRC += fastbasemul.S
SRC += fastinvntt.S
SRC += fastntt.S
SRC += reduce.S

#SRC += macros.i
#ASRC += macros.i
ASRC += ./crypto_kem/kyber512/m4/macros.i
#directories where source files are located
VPATH += :crypto_kem/kyber512/m4
EXTRAINCDIRS += crypto_kem/kyber512/m4
VPATH += :mupq/pqclean/common
EXTRAINCDIRS += mupq/pqclean/common

ifeq ($(CRYPTO_OPTIONS),)
CRYPTO_OPTIONS = AES128C
endif

#Add simpleserial project to build
include …/simpleserial/Makefile.simpleserial

FIRMWAREPATH = …/.
include $(FIRMWAREPATH)/Makefile.inc


I use ASRC += macros.i as your suggestion in the makefile (either line 24, 25, or 26) but it makes no change in the error. Am I wrong with the SRC and the makefile you mentioned?

Thank you very much.
Best regards,
Tuan

Try adding all of the .S files to ASRC as well.

Alex

Hi Alex
I added other .S files from pqclean (beside macro and .S files from pqm4) to the makefile like


#add assembly and macro codes from pqm4
#ASRC += ./crypto_kem/kyber512/m4/macros.i
ASRC += fastaddsub.S
ASRC += fastbasemul.S
ASRC += fastinvntt.S
ASRC += fastntt.S
ASRC += reduce.S
ASRC += macros.i

#add assembly codes from pqclean
ASRC += basemul.S

#directories where source files are located
VPATH += :crypto_kem/kyber512/m4
EXTRAINCDIRS += crypto_kem/kyber512/m4
#directories for assembly codes of pqclean
VPATH += :mupq/pqclean/common
EXTRAINCDIRS += mupq/pqclean/common
VPATH += mupq/pqclean/crypto_kem/kyber512/clean
EXTRAINCDIRS += mupq/pqclean/crypto_kem/kyber512/clean
VPATH += mupq/pqclean/crypto_kem/kyber512/avx2
EXTRAINCDIRS += mupq/pqclean/crypto_kem/kyber512/avx2


However, adding any other .S file from pqclean (e.g in line 11 above ASRC += basemul.S) makes following error


mupq/pqclean/crypto_kem/kyber512/avx2/basemul.S: Assembler messages:
mupq/pqclean/crypto_kem/kyber512/avx2/basemul.S:90: Error: ARM register expected – mov %rsp,%r8' mupq/pqclean/crypto_kem/kyber512/avx2/basemul.S:91: Error: ARM register expected -- and $-32,%rsp’
mupq/pqclean/crypto_kem/kyber512/avx2/basemul.S:92: Error: ARM register expected – sub $32,%rsp' mupq/pqclean/crypto_kem/kyber512/avx2/basemul.S:94: Error: bad instruction lea (160+176)2(%rcx),%r9’
mupq/pqclean/crypto_kem/kyber512/avx2/basemul.S:95: Error: bad instruction `vmovdqa 16
2(%rcx),%ymm0’


the codes in pqclean are for avx2.
Do you think that I will need those codes compiled also and if so, how can I make it compiled?

Thank you very much.
Best regards,
Tuan

Hi Tuan,

I’m guessing pqclean is all x86_64 since it’s got avx instructions, so I don’t think you should include any assembly files from there. I’m not familiar with this project, but pqclean might just be there for some .c/.h files.

Alex

Hi Alex and Tuan,

I am currently working on the same project. Has this issue been fixed?
Is there a complete tutorial on how to apply Kyber to the CW?

Would really appreciate any help.

Best regards,

Mathias

Hi Mathias,

We don’t have any plans to add Kyber as a crypto library to ChipWhisperer. That being said, I’m happy to answer questions about the build system if you want to try adding it in yourself.

Alex

Hi Alex,

Thanks for the reply.
So I am currently using the pqm4 library. I have went through several of the guides from the chipwhisperer github repo, and have done a report on the CW and how a CPA works and can be constructed.

My first intention for the Master´s is simply to be able to run a single instance of Kyber (using the pqm4 library) and experiment with where I can put the triggers to capture the traces. I am fully able to run the tests from pqm4 (python files) and it seems to work properly.

Is there a way to use Kyber512 instead of AES in this guide chipwhisperer-jupyter/PA_CPA_1-Using_CW-Analyzer_for_CPA_Attack.ipynb at 95c7cc2391d224d13ebc807a6665e1672c62d9bf · newaetech/chipwhisperer-jupyter · GitHub ?

Mathias

Hi Mathias,

You should be able to stitch together a full trace based on smaller traces of varying offsets (this is done for the Lite in our ECC example on the F3: chipwhisperer-jupyter/uecc.ipynb at master · newaetech/chipwhisperer-jupyter · GitHub). Of course, you can also move trigger_high() around in the code to help you understand which parts of the power trace correspond to which parts of the code.

We don’t have any analysis support for Kyber either as papers on side channel analysis for it is quite recent and we don’t have anyone familiar with the algorithm. To get started with capturing traces should be pretty easy. If you look at the capture code in chipwhisperer-jupyter/Lab 4_2 - CPA on Firmware Implementation of AES (HARDWARE).ipynb at master · newaetech/chipwhisperer-jupyter · GitHub, what you need to do is

  1. Setup your ChipWhisperer. Typically connecting and running scope.default_setup() gets you good settings to work off of
  2. Make sure your target is programmed
  3. Optionally, set a key on the target
  4. Arm the scope (scope.arm())
  5. Trigger your algorithm (in this case Kyber) to run, which in turn causes a rising edge on TIO4. In our default firmware, this is done via a serial message
  6. Capture the trace (scope.capture())
  7. Get the trace data (scope.get_last_trace())
  8. Optionally, read the result of the encryption back

Alex

Hi Alex,

Thank you for your reply.
I have gotten further in my research, and the pieces are slowing coming together.

I have now managed to create a .hex-file with Kyber1024 ready to be tested. My next milestone would therefore be to implement the triggers around appropriate functions.

I am currently not working in the Simpleserial repo, so I do not have the same header files in my project. Is there a way to be able to use the trigger_high() and trigger_low() functions in my own project?

Mathias

Hi,
I’m attempting to implement Kyber512 from the pqm4 library, but I have some issues. I have done some debugging through gdb, and I’m getting the error “[stm32f3x.cpu] clearing lockup after double fault” when reaching the assembly code “vpush.w {s16-s24}”. The code should be compatible as my target is the CW308-STM32F3 which the pqm4 library is designed for. I have attempted changing some gcc flags according to the pqm4 build process as “-mfloat-abi=hard -mfpu=fpv4-sp-d16 -ggdb3 -MD” without success. Any help is greatly appreciated.
Erlend

Hi,
Have you succeeded in debugging this issue? I am also trying to test Kyber512 from pqm4 on a STM32F3 CW Lite and, after finally compiling the firmware and programming the target, the program is stuck on any asm function. Didn’t get to find the root cause, the script just tries to run a key_gen() and get’s blocked/unresponsive. I tried with various mfpus, but for my board it only seems to compile a with a soft vfp. Any help or advice would be much appreciated.

Will this answer be helpful?

1 Like

I cannot provide more suggestion wwithout further information.

However, the hard fpu is available in the STM32F3 board, have you tried to modify the compilation script in “Makefile.inc” and “hal/stm32f3/Makefile.hal” ?

This thread was exactly what I needed. I added the missing code to hal/stm32f3/stm32f3_hal.c and recompiled the firmware. Works like a charm now. Thank you so much!

Hello, may I ask if MAX_SS_CMDS has a maximum value? My current main function can only add 34 simpleserial_addcmd commands, and the following simpleserial_addcmd will no longer respond.

I really appreciate you being able to answer my questions.

The hardware I use is cw-lite, cw308 and stm32f405.

Hi Alex and Tuan,
I wrote ‘ASRC += /path/to/macros.i’ but it still cannot be recognized. I tried adding rules to convert .i files to .s and .o, but they did not work. The error is as follows:
crypto_kem/kyber768/m4/macros.i:1:1: error: stray ‘#’ in program
1 | #ifndef MACROS_I
| ^
crypto_kem/kyber768/m4/macros.i:1:2: error: unknown type name ‘ifndef’
1 | #ifndef MACROS_I
| ^~~~~~
crypto_kem/kyber768/m4/macros.i:4:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
4 | .macro load a, a0, a1, a2, a3, mem0, mem1, mem2, mem3
| ^
crypto_kem/kyber768/m4/macros.i:5:9: error: stray '' in program
5 | ldr.w \a0, [\a, \mem0]
| ^
crypto_kem/kyber768/m4/macros.i:5:15: error: stray '' in program
5 | ldr.w \a0, [\a, \mem0]
| ^
crypto_kem/kyber768/m4/macros.i:5:19: error: stray '' in program
5 | ldr.w \a0, [\a, \mem0]
| ^
crypto_kem/kyber768/m4/macros.i:6:9: error: stray '' in program
6 | ldr.w \a1, [\a, \mem1]

The command field itself is limited to 1 byte, so 255 would be the hard maximum (see here for the SimpleSerial protocol definition).

Perhaps you’ve run out of memory due to the code associated with the extra commands you’ve added.

Hi there,
I had a similar problem when adding the .I files to the ASRC, namely “file format not recognized treating as linker script” + syntax error. I tried ignoring the files first and then changing their extension to .S in order to be recognized and both methods seem to work, as I get the firmware to compile and run Kyber procedures on the device.

However, I am not entirely sure this is ideal, nor if it fully works, because I found out that I can’t perform more than 40 kem procedures (keygen, enc, dec), after which the board becomes unresponsive. Other operations work smoothly, so the problem might very well be linked to this issue.

If anyone has any insight on this, it would be really helpful.