Skip to content

Commit

Permalink
[tvla] Adjust kmac and sha3 capture and config for tvla plotting
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Wettermann <[email protected]>
  • Loading branch information
wettermo committed Jan 8, 2024
1 parent 20deeda commit 645b703
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
5 changes: 4 additions & 1 deletion analysis/configs/tvla_cfg_kmac.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_file: projects/opentitan_simple_kmac.cwp
project_file: ../capture/projects/simple_capture_kmac_sca
trace_file: null
trace_start: null
trace_end: null
Expand All @@ -14,3 +14,6 @@ ttest_step_file: null
plot_figures: true
general_test: true
mode: kmac
filter_traces: true
trace_threshold: 1000
trace_db: ot_trace_library
5 changes: 4 additions & 1 deletion analysis/configs/tvla_cfg_sha3.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_file: projects/opentitan_simple_sha3.cwp
project_file: ../capture/projects/simple_capture_sha3_sca
trace_file: null
trace_start: null
trace_end: null
Expand All @@ -14,3 +14,6 @@ ttest_step_file: null
plot_figures: true
general_test: true
mode: sha3
filter_traces: true
trace_threshold: 1000
trace_db: ot_trace_library
62 changes: 38 additions & 24 deletions analysis/tvla.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

import inspect
import logging as log
import math
import multiprocessing
import os
import sys
from pathlib import Path
from types import SimpleNamespace

import matplotlib.pyplot as plt
import math
import numpy as np
import typer
import yaml
Expand Down Expand Up @@ -163,7 +163,8 @@ def compute_histograms_aes(trace_resolution, rnd_list, byte_list, traces, leakag
return histograms


def tvla_plotting_fnc(axs, num_orders, ttest_trace, single_trace, threshold, num_samples, sample_start, metadata):
def tvla_plotting_fnc(axs, num_orders, ttest_trace, single_trace, threshold,
num_samples, sample_start, metadata):
"""Plotting trace in different colors, depending on where the trigger is high
"""
c = np.ones(num_samples)
Expand Down Expand Up @@ -204,17 +205,17 @@ def tvla_plotting_fnc(axs, num_orders, ttest_trace, single_trace, threshold, num
axs[1 + i_order].plot(xaxs, c * threshold, "r")
axs[1 + i_order].plot(xaxs, -threshold * c, "r")
if trigger_high > 0:
axs[1 + i_order].plot(
xaxs[:trigger_high],
ttest_trace[i_order, 0, 0][:trigger_high], "grey")
axs[1 + i_order].plot(xaxs[:trigger_high],
ttest_trace[i_order, 0,
0][:trigger_high], "grey")
if trigger_low > trigger_high:
axs[1 + i_order].plot(
xaxs[trigger_high:trigger_low],
ttest_trace[i_order, 0, 0][trigger_high:trigger_low], "k")
if trigger_low < num_samples:
axs[1 + i_order].plot(
xaxs[trigger_low:],
ttest_trace[i_order, 0, 0][trigger_low:], "grey")
axs[1 + i_order].plot(xaxs[trigger_low:],
ttest_trace[i_order, 0,
0][trigger_low:], "grey")

return axs

Expand Down Expand Up @@ -524,7 +525,10 @@ def run_tvla(ctx: typer.Context):
# Eventually, we can drop this.
if i_step == 0:
if OTTraceLib:
keys_nparrays = project.get_keys()
if cfg["mode"] == "sha3":
keys_nparrays = project.get_plaintexts()
else:
keys_nparrays = project.get_keys()
else:
# Convert all keys from the project file to numpy
# arrays once.
Expand Down Expand Up @@ -758,23 +762,27 @@ def run_tvla(ctx: typer.Context):
# Catch case where certain metadata isn't saved to project file (e.g. older measurement)
try:
sampling_rate = float(metadata['sampling_rate']) / 1e6
textbox = textbox + "Sample rate:\n" + str(math.floor(sampling_rate)) + " MS/s\n\n"
textbox = textbox + "Sample rate:\n" + str(
math.floor(sampling_rate)) + " MS/s\n\n"
except KeyError:
textbox = textbox
try:
textbox = textbox + "Masks off:\n" + metadata['masks_off'] + "\n\n"
except KeyError:
textbox = textbox
try:
textbox = textbox + "Samples:\n" + str(metadata['num_samples']) + "\n\n"
textbox = textbox + "Samples:\n" + str(
metadata['num_samples']) + "\n\n"
except KeyError:
textbox = textbox
try:
textbox = textbox + "Offset:\n" + str(metadata['offset_samples']) + "\n\n"
textbox = textbox + "Offset:\n" + str(
metadata['offset_samples']) + "\n\n"
except KeyError:
textbox = textbox
try:
textbox = textbox + "Scope gain:\n" + str(metadata['scope_gain']) + "\n\n"
textbox = textbox + "Scope gain:\n" + str(
metadata['scope_gain']) + "\n\n"
except KeyError:
textbox = textbox
try:
Expand All @@ -793,9 +801,13 @@ def run_tvla(ctx: typer.Context):
for i_rnd in range(num_rnds):
for i_byte in range(num_bytes):

axs = tvla_plotting_fnc(axs, num_orders, ttest_trace, single_trace, threshold, num_samples, sample_start, metadata)
axs = tvla_plotting_fnc(axs, num_orders, ttest_trace,
single_trace, threshold,
num_samples, sample_start,
metadata)

# Catch case where datetime data isn't saved to project file (e.g. older measurement)
# Catch case where datetime data isn't saved
# to project file (e.g. older measurement)
try:
axs[0].set_title("TVLA of " + "aes_t_test_round_" +
str(rnd_list[i_rnd]) + "_byte_" +
Expand All @@ -813,14 +825,14 @@ def run_tvla(ctx: typer.Context):
right = left + width
top = bottom + height
plt.gcf().text(0.5 * (left + right),
0.5 * (bottom + top),
textbox,
fontsize=9,
horizontalalignment='center',
verticalalignment='center',
bbox=dict(boxstyle='round',
facecolor='w',
linewidth=0.6))
0.5 * (bottom + top),
textbox,
fontsize=9,
horizontalalignment='center',
verticalalignment='center',
bbox=dict(boxstyle='round',
facecolor='w',
linewidth=0.6))
plt.subplots_adjust(right=0.84)
plt.xlabel("time [samples]")

Expand All @@ -833,7 +845,9 @@ def run_tvla(ctx: typer.Context):
plt.close()

else:
axs = tvla_plotting_fnc(axs, num_orders, ttest_trace, single_trace, threshold, num_samples, sample_start, metadata)
axs = tvla_plotting_fnc(axs, num_orders, ttest_trace, single_trace,
threshold, num_samples, sample_start,
metadata)

# Catch case where datetime data isn't saved to project file (e.g. older measurement)
try:
Expand Down
2 changes: 2 additions & 0 deletions capture/capture_kmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ def main(argv=None):
metadata["cfg"] = cfg
metadata["num_samples"] = scope.scope_cfg.num_samples
metadata["offset_samples"] = scope.scope_cfg.offset_samples
metadata["sampling_rate"] = scope.scope_cfg.sampling_rate
metadata["num_traces"] = capture_cfg.num_traces
metadata["scope_gain"] = scope.scope_cfg.scope_gain
metadata["cfg_file"] = str(args.cfg)
# Store bitstream information.
Expand Down
3 changes: 2 additions & 1 deletion capture/capture_otbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,8 @@ def main(argv=None):
metadata["offset_samples"] = scope.scope_cfg.offset_samples
metadata["scope_gain"] = scope.scope_cfg.scope_gain
if cfg["capture"]["scope_select"] == "husky":
metadata["sampling_rate"] = scope.scope.scope.clock.adc_freq / scope.scope.scope.adc.decimate
metadata[
"sampling_rate"] = scope.scope.scope.clock.adc_freq / scope.scope.scope.adc.decimate
metadata["samples_trigger_high"] = scope.scope.scope.adc.trig_count
else:
metadata["sampling_rate"] = scope.scope_cfg.sampling_rate
Expand Down
2 changes: 2 additions & 0 deletions capture/capture_sha3.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ def main(argv=None):
metadata["cfg"] = cfg
metadata["num_samples"] = scope.scope_cfg.num_samples
metadata["offset_samples"] = scope.scope_cfg.offset_samples
metadata["sampling_rate"] = scope.scope_cfg.sampling_rate
metadata["num_traces"] = capture_cfg.num_traces
metadata["scope_gain"] = scope.scope_cfg.scope_gain
metadata["cfg_file"] = str(args.cfg)
# Store bitstream information.
Expand Down

0 comments on commit 645b703

Please sign in to comment.