Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[capture] Add uJSON support #218

Merged
merged 2 commits into from
Dec 5, 2023
Merged

Conversation

nasahlpa
Copy link
Member

@nasahlpa nasahlpa commented Nov 15, 2023

This PR enables communication with the OT AES over uJSON.

How to use:

  • In capture/configs/aes_sca_cw310.yaml target change:
    • protocol: "ujson"
    • port: "/dev/ttyACM4" (adapt the port accordingly, needs to be UART of CW310)
    • fw_bin: "../objs/aes_ujson_fpga_cw310.bin"

Device code is located in PR#20386.

@nasahlpa nasahlpa force-pushed the usjon_commands branch 3 times, most recently from ddac48f to de8bebd Compare November 16, 2023 18:15
def _ujson_aes_sca_cmd(self):
# TODO: without the delay, the device uJSON command handler program
# does not recognize the commands.
time.sleep(0.2)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@milesdai is there a way to avoid this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick update: I'm working on reproducing this bug. Should I be running something like ./capture_aes.py -c configs/aes_sca_cw310.yaml -p test_project to test this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactely. Please change the configs/aes_sca_cw310.yaml as mentioned above, build the firmware from the PR linked above, and run ./capture_aes.py -c configs/aes_sca_cw310.yaml -p test_project. However, if you don't have a ChipWhisperer Husky, the capture script fails.
I'll create a small POC script for you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a small POC script:

#!/usr/bin/env python3
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

from lib.ot_communication import OTAES, OTPRNG, OTUART

# Establish UART for uJSON command interface. Returns None for simpleserial.
ot_uart = OTUART(protocol="ujson", port="/dev/ttyACM4")

# Create communication interface to OT AES.
ot_aes = OTAES(target=None, protocol="ujson", port=ot_uart.uart)

# Set the key.
key = [0x81, 0x1E, 0x37, 0x31, 0xB0, 0x12, 0x0A, 0x78, 0x42, 0x78, 0x1E, 0x22, 0xB2, 0x5C, 0xDD, 0xF9]
ot_aes.key_set(key)

# Start encryption.
text = [0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA]
ot_aes.single_encrypt(text)

# Read ciphertext.
print(ot_aes.read_ciphertext(16))

Please place this script into the capture/ folder, transfer the firmware to the device, adapt the port (e.g., "/dev/ttyACM4") in the script, and start it. You should receive the ciphertext.

When I now remove all time.sleep commands in ot_communication.py, I do not receive a ciphertext. I did some printf debugging on the device, it seems that when sending two commands subsequently, the latter is not received by the command handler.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the test script! I'm able to reproduce the problem you're describing. I'm still looking into this, but so far, this sounds a lot like a flow control issue. The firmware enables software-based flow control through OTTF, so I've tried enabling SW flow control on the Python side as well with the xonxoff=True flag, but no luck so far.

I'm going to take a closer look at this, but in the meanwhile, I've found that I can reduce the sleep time to about 10 ms instead of 200. If we don't want to block the PR on this, maybe that can help your throughput a bit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @milesdai! I'll decrease the timeout and merge the PR for now.
Please let me know if I can help you with debugging, for us a fast communication is really important as we want to trigger AES executions as fast as possible to achieve high capture rates.

@nasahlpa
Copy link
Member Author

nasahlpa commented Nov 29, 2023

As discussed with @milesdai , capture rate is still quite low as there seems to be an issue with the communication protocol. Miles will be working on a fix.
To not block progress on this repo, could others (@vogelpi @johannheyszl @vrozic ...) please review this PR?

Capture rates for AES random batch drops from 934.27 traces/s to 350.40 traces/s.

@nasahlpa nasahlpa force-pushed the usjon_commands branch 8 times, most recently from 21dfe61 to 69e60c4 Compare November 29, 2023 15:20
Copy link
Collaborator

@vogelpi vogelpi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nasahlpa , the PR looks mostly good but especially in data_generator.py there seem to be changes not related to this PR.

capture/configs/aes_sca_cw310.yaml Outdated Show resolved Hide resolved
capture/configs/aes_sca_cw310.yaml Outdated Show resolved Hide resolved
util/data_generator.py Outdated Show resolved Hide resolved
@nasahlpa nasahlpa force-pushed the usjon_commands branch 4 times, most recently from 2c69265 to 080026e Compare December 4, 2023 08:39
@nasahlpa
Copy link
Member Author

nasahlpa commented Dec 4, 2023

Thanks for the feedback @vogelpi, I've addressed your comments.

This PR enables communication with the OT AES over uJSON.

Signed-off-by: Pascal Nasahl <[email protected]>
The new uJSOn based communication interfaces requires that the crypto
material is handed over in plain. Conversion from plain data to bytes
now happens directly before sending data over simpleserial.
This PR changes the data format from bytes to plain in the data generator.

Signed-off-by: Pascal Nasahl <[email protected]>
@vrozic
Copy link
Contributor

vrozic commented Dec 4, 2023

Capture rates for AES random batch drops from 934.27 traces/s to 350.40 traces/s.

This is probably not a problem. If we increase num_segments to e.g. 160, the capture rate goes back up to ~2500, which is still pretty high. I think that for these measurement on Husky we can use up to 190 segments.


def fvsr_key_set(self, key):
def fvsr_key_set(self, key, key_length: Optional[int] = 16):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this function is not used anywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. However, as the AES SCA uJSON handler at the OT device implements the FvsrKeySet command, the handler is provided here. If we don't need the FvsrKeySet command at the OT device, we should remove it there and also here.

Copy link
Contributor

@vrozic vrozic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this PR and all captures work well with my setup.

Copy link
Collaborator

@vogelpi vogelpi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@nasahlpa nasahlpa merged commit 19a084e into lowRISC:master Dec 5, 2023
5 checks passed
@nasahlpa nasahlpa deleted the usjon_commands branch December 5, 2023 09:46
@vogelpi
Copy link
Collaborator

vogelpi commented Dec 5, 2023

Capture rates for AES random batch drops from 934.27 traces/s to 350.40 traces/s.

This is probably not a problem. If we increase num_segments to e.g. 160, the capture rate goes back up to ~2500, which is still pretty high. I think that for these measurement on Husky we can use up to 190 segments.

Related to that, aren't the number of segments automatically determined by Husky? I thought this was required to always completely fill the sample memory in Husky before reading it out in batch mode. Do we manually set the number of segments somewhere now?

@vrozic
Copy link
Contributor

vrozic commented Dec 5, 2023

Related to that, aren't the number of segments automatically determined by Husky?

That used to be the case. Now it's just specified in the configuration file. For this reason we also sometimes get more measurements than we ask for (e.g. if num_segments = 40, num_traces = 100, the script will take 120 measurements)

@vogelpi
Copy link
Collaborator

vogelpi commented Dec 5, 2023

Related to that, aren't the number of segments automatically determined by Husky?

That used to be the case. Now it's just specified in the configuration file. For this reason we also sometimes get more measurements than we ask for (e.g. if num_segments = 40, num_traces = 100, the script will take 120 measurements)

Ah, thanks for getting back! This makes sense. We should probably add a check in case num_segements * num_samples is bigger than the sample memory. Pascal said to take care of this.

nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 5, 2023
Similar to lowRISC#218, this PR enables uJSON support for capturing KMAC
traces. The device command handler code can be found in #20563.

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 5, 2023
Similar to lowRISC#218, this PR enables uJSON support for capturing KMAC
traces. The device command handler code can be found in #20563.

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 5, 2023
Similar to lowRISC#218, this PR enables uJSON support for capturing KMAC
traces. The device command handler code can be found in #20563.

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 5, 2023
Similar to lowRISC#218, this PR enables uJSON support for capturing KMAC
traces. The device command handler code can be found in #20563.
Closes lowRISC#238

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 6, 2023
Similar to lowRISC#218, this PR enables uJSON support for capturing KMAC
traces. The device command handler code can be found in #20563.
Closes lowRISC#238

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 6, 2023
Similar to lowRISC#218, this PR enables uJSON support for capturing KMAC
traces. The device command handler code can be found in #20563.
Closes lowRISC#238

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 6, 2023
Similar to lowRISC#218, this PR enables uJSON support for capturing KMAC
traces. The device command handler code can be found in #20563.
Closes lowRISC#238

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit that referenced this pull request Dec 6, 2023
Similar to #218, this PR enables uJSON support for capturing KMAC
traces. The device command handler code can be found in #20563.
Closes #238

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 8, 2023
Similar to lowRISC#218 , this PR enables uJSON support for capturing SHA3
traces. The device command handler code can be found in #20593.

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit to nasahlpa/ot-sca that referenced this pull request Dec 11, 2023
Similar to lowRISC#218 , this PR enables uJSON support for capturing SHA3
traces. The device command handler code can be found in #20593.

Signed-off-by: Pascal Nasahl <[email protected]>
nasahlpa added a commit that referenced this pull request Dec 11, 2023
Similar to #218 , this PR enables uJSON support for capturing SHA3
traces. The device command handler code can be found in #20593.

Signed-off-by: Pascal Nasahl <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants