Exemplo n.º 1
0
def cpa_attack_trace_set(trace_set, result, conf):
    if result.guess_sample_score_matrix is None:
        raise EMMAException("Score matrix is not initialized.")

    if not trace_set.windowed:
        logger.warning("Trace set not windowed. Skipping attack.")
        return

    if trace_set.num_traces <= 0:
        logger.warning("Skipping empty trace set.")
        return

    hypotheses = np.empty([256, trace_set.num_traces])

    # 1. Build hypotheses for all 256 possibilities of the key and all traces
    leakage_model = LeakageModel(conf)
    for subkey_guess in range(0, 256):
        for i in range(0, trace_set.num_traces):
            hypotheses[subkey_guess, i] = leakage_model.get_trace_leakages(
                trace=trace_set.traces[i],
                subkey_start_index=conf.subkey,
                key_hypothesis=subkey_guess)

    # 2. Given point j of trace i, calculate the correlation between all hypotheses
    for j in range(0, trace_set.window.size):
        # Get measurements (columns) from all traces
        measurements = np.empty(trace_set.num_traces)
        for i in range(0, trace_set.num_traces):
            measurements[i] = trace_set.traces[i].signal[j]

        # Correlate measurements with 256 hypotheses
        for subkey_guess in range(0, 256):
            # Update correlation
            result.guess_sample_score_matrix.update(
                (subkey_guess, j), hypotheses[subkey_guess, :], measurements)
Exemplo n.º 2
0
    def test_hmac_hw(self):
        fake_pt  = np.array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], dtype=np.uint8)
        fake_key = np.array([0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80], dtype=np.uint8)
        trace = Trace(signal=[], plaintext=fake_pt, ciphertext=[], key=fake_key, mask=[])

        lm = LeakageModel(Namespace(leakage_model="hmac_hw", key_low=0, key_high=1))
        self.assertEqual(lm.subkey_size, 4)  # HMAC uses subkeys of size 4
        leakage_subkey_0 = lm.get_trace_leakages(trace, 0)  # hw32(0x40302010)
        leakage_subkey_1 = lm.get_trace_leakages(trace, 1)  # hw32(0x80706050)
        self.assertEqual(leakage_subkey_0[0], hw32(0x40302010))
        self.assertEqual(leakage_subkey_1[0], hw32(0x80706050))
        self.assertEqual(leakage_subkey_0[1], hw32(0x40302010 ^ 0x36363636))
        self.assertEqual(leakage_subkey_1[1], hw32(0x80706050 ^ 0x36363636))
        self.assertEqual(leakage_subkey_0[2], hw32(0x40302010 ^ 0x5c5c5c5c))
        self.assertEqual(leakage_subkey_1[2], hw32(0x80706050 ^ 0x5c5c5c5c))
Exemplo n.º 3
0
class SignalLeakageAIInput(AIInput):
    input_type = AIInputType.SIGNAL_LEAKAGE

    def __init__(self, conf):
        super().__init__(conf)
        self.leakage_model = LeakageModel(conf)

    def get_trace_inputs(self, trace):
        leakages = []

        for k in range(16):
            leakage = self.leakage_model.get_trace_leakages(trace, k)
            if isinstance(leakage, list) or isinstance(leakage, np.ndarray):
                leakages.extend(list(leakage))
            else:
                leakages.append(leakage)
        leakages = np.array(leakages)

        return np.concatenate((trace.signal, leakages))