ANSSI Masked aes firmware problem

Hi
I am trying to compile ANSSI masked aes implementation given in chipwhisperer library. I ran the program as below

SCOPETYPE = 'OPENADC'
PLATFORM='CW308_STM32F3'
CRYPTO_TARGET='MASKEDAES'
CRYPTO_OPTIONs='ANSSI'
SS_VER='SS_VER_2_1'

%%bash -s "$PLATFORM" "$CRYPTO_TARGET" "$SS_VER" "$CRYPTO_OPTIONs"
cd firmware/mcu/simpleserial-aes
make PLATFORM=$1 CRYPTO_TARGET=$2 CRYPTO_OPTIONS=$4 -j

and i got following error

Building for platform CW308_STM32F3 with CRYPTO_TARGET=MASKEDAES
SS_VER set to SS_VER_1_1
SS_VER set to SS_VER_1_1
Impletementation=ANSSI
Options=
.
arm-none-eabi-gcc (15:10.3-2021.07-4) 10.3.1 20210621 (release)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Welcome to another exciting ChipWhisperer target build!!
.
+--------------------------------------------------------
LINKING:
+ Built for platform CW308T: STM32F3 Target with:
-en     simpleserial-aes-CW308_STM32F3.elf ...
+ CRYPTO_TARGET = MASKEDAES
+ CRYPTO_OPTIONS = ANSSI
arm-none-eabi-gcc: error: objdir-CW308_STM32F3/platform.o: No such file or directory
arm-none-eabi-gcc: error: objdir-CW308_STM32F3/affine_aes.o: No such file or directory
+--------------------------------------------------------
make: *** [.././Makefile.inc:494: simpleserial-aes-CW308_STM32F3.elf] Error 1

And when i went through forum i got j-michel’s ready to use files. But my confusion is do they support configurable masks via simpleserial?
thanks

According to the error message, I’d say you haven’t pulled the submodules.
Try running git submodule update --init and that should solve this compilation error.

The firmware is using SimpleSerial 1.x protocol and supports setting the masks through the m command. Note that this command is a variable length command because you can set half mask (protecting the encryption) or full mask (i.e. also masking the key schedule) so it expects the size to be encoded.
m<LL><mask1>[mask2] where LL is the hex-encoded length of the mask you’re setting. mask1 and mask2 are also hex-encoded, mask2 is optional.
Example: m1300112233445566778899aabbccddeeff001122 will set a mask of 19 bytes (0x13) that is going to be 00112233445566778899aabbccddeeff001122.

Regarding the length of the masks, I don’t remember their exact size but they should each contain 16-bytes mask and 2 or 3 bytes meta-data (operation shuffling, delays, etc.).

Hope that helps

thanks @jmichel for the help…

  1. compilation problem

I uninstalled the whole chipwhisperer software and installed it again. this time run the command u said above. then i tried to compile ANSSI AES and it gave an error saying

Building for platform CW308_STM32F3 with CRYPTO_TARGET=MASKEDAES
SS_VER set to SS_VER_1_1
SS_VER set to SS_VER_1_1
Impletementation=ANSSI
Options=
.
arm-none-eabi-gcc (15:10.3-2021.07-4) 10.3.1 20210621 (release)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Welcome to another exciting ChipWhisperer target build!!
.
Compiling:
+--------------------------------------------------------
-en     .././crypto/aes-independant.c ...
+ Built for platform CW308T: STM32F3 Target with:
+ CRYPTO_TARGET = MASKEDAES
+ CRYPTO_OPTIONS = ANSSI
+--------------------------------------------------------
.././crypto/aes-independant.c:285:10: fatal error: platform.h: No such file or directory
  285 | #include "platform.h"
      |          ^~~~~~~~~~~~
compilation terminated.
make: *** [.././Makefile.inc:503: objdir-CW308_STM32F3/aes-independant.o] Error 1
`
but when i tried for `TINYAES128C` it just compiled successfully. After this i tried `maskedaes` again. this time the error was 
`Building for platform CW308_STM32F3 with CRYPTO_TARGET=MASKEDAES
SS_VER set to SS_VER_1_1
SS_VER set to SS_VER_1_1
Impletementation=ANSSI
Options=
.
arm-none-eabi-gcc (15:10.3-2021.07-4) 10.3.1 20210621 (release)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Welcome to another exciting ChipWhisperer target build!!
.
+--------------------------------------------------------
LINKING:
+ Built for platform CW308T: STM32F3 Target with:
-en     simpleserial-aes-CW308_STM32F3.elf ...
+ CRYPTO_TARGET = MASKEDAES
+ CRYPTO_OPTIONS = ANSSI
+--------------------------------------------------------
arm-none-eabi-gcc: error: objdir-CW308_STM32F3/platform.o: No such file or directory
arm-none-eabi-gcc: error: objdir-CW308_STM32F3/affine_aes.o: No such file or directory
make: *** [.././Makefile.inc:494: simpleserial-aes-CW308_STM32F3.elf] Error 1

but when i tried using chipwhisperer release 5.5 which was initial release that contains anssi implementation, it just compiled successfully.

  1. sending mask values

I got this point. i was trying to check with some dummy mask value and tried following.


Then I tried using target.simpleserial_write('m',b'0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 23 36 54 47','\n'). Then even if i sent more than the length size of mask value it just accepted and no error thrown. Now I am confused whether i used that api correctly or not.
please confirm whether this is a correct way or not.
(sorry for this silly question)

3. About the implementation
I tried using ur ready to use ANSSI MASKEDAES STM32F3 Hex files from this repository and successfully got traces. My goal is to validate chipwhisperer setup that I have by reproducing ASCAD v2 dataset. That dataset have been taken for entire AES encryption, but not limited to a single round or key loading in that implementation. Now i wonder whether ur implementation does the same or it just collects samples for a particular round or what?
(of course i am using just the target board and UFO board as standalone and use oscilloscope with the same parameters as authors used and capture board for sending plaintext, key and mask values)

Thanks

target.simpleserial_write("m", bytes.fromhex("0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 23 36 54 47")) should be the correct way to call it I think.
IIRC setting the first bytes to 0x00 means that you’re effectively disabling many protections. But you’ll have to look at the source code of the firmware to double check that.

Regarding the capture, there are multiple options for the firmware, combining loop unrolling and including or not the keyschedule in the trace.
Like any other firmware, trigger line will go high when you need to start the capture and will go low when the operation is complete. The amount you capture is then completely us to you and the way you setup your oscilloscope :person_shrugging:

@jmichel thanks for the help on simpleserial.
But my compilation issue has not been resolved yet. by any chance did you look at the error ?

Hi, I’m also facing an issue with masked AES (ANSSI Version 1) on XMEGA.
Here is jupyter code:

for i in tnrange(num_traces, desc='Capturing traces'):
    scope.arm()
    key, text = ktp.next()
    key2 = bytearray([0xaf,0xed,0x20,0x46,0x47,0xf5,0x35,0xf7,0xf2,0x96,0xe7,0x63,0xac,0xf6,0x64,0xd2])
    # Generate the 18-byte random mask
    random_part = os.urandom(18)
    # Write plaintext to the target
    target.simpleserial_write('p', text)
    target.simpleserial_write('k', key2)
    # Write mask data (binary)
    mask_data = b'\x12' + random_part
    target.simpleserial_write("m", mask_data)
    
    # Capture trace
    ret = scope.capture()
    if ret:
        print("Target timed out!")
        continue
    
    # Read the response
    response = target.simpleserial_read('r', target.output_len)
    
    # Append trace and text input to arrays
    trace_array.append(scope.get_last_trace())
    textin_array.append(text)
    masks_aray.append(mask_data)
    textout_array.append(response)
    #textout_array.append(cw.CWbytearray(response))

I’ve successfully sent the random plaintexts, fixed key, and random masks (18 byte) to the target, and verified that AES-128 encryption is working correctly, with the following results:

  • Key: afed204647f535f7f296e763acf664d2
  • Text Mask (Hex): 123e3f5682fd4fd37a880f0216e71e271dd503
  • Text Input (Hex): c3fb7fb7d3f670873e7d7a51ad7704f9
  • Text Output (Hex): 1dd1b4af8aae8fa63907a330fb43f6b8

However, when I performed CPA on the 500 collected power traces, the secret key was successfully revealed, indicating that the masking did not work as expected. Does anyone have any insight into why this might be happening?"

This is a plot of power trace (I have eliminate the preprocessing step, self.adc.offset =16000):


And here is attack result:

The compilation error is simply due to the fact that the github submodule has been removed by the maintainers. No source code → compilation error :slight_smile:

A workaround would be, as a one-time operation:

$ cd firmware/mcu/crypto
$ git clone https://github.com/ANSSI-FR/SecAESSTM32

From here, compilation should work as before.