Пример #1
0
def simulate_traces_random(args, train=True):
    """
    Untested. Simulate traces randomly, without artificial noise. Interesting for seeing true effect of random keys, but slow.
    :param args:
    :return:
    """
    specs = get_algorithm_specs(args.algorithm)
    key = random_bytes(specs.key_len)
    if train is False:
        print("Test set key: " + bytearray(key).hex())

    for i in range(0, args.num_trace_sets):
        traces = []
        print("\rSimulating trace set %d/%d...                          " %
              (i, args.num_trace_sets),
              end='')
        for j in range(0, args.num_traces_per_set):
            if train:
                key = random_bytes(specs.key_len)
            plaintext = random_bytes(specs.plaintext_len)
            key_string = binascii.hexlify(key).decode('utf-8')
            plaintext_string = binascii.hexlify(plaintext).decode('utf-8')

            sim = ProgramSimulation(specs.executable,
                                    (key_string, plaintext_string),
                                    specs.method,
                                    REGS_TO_CHECK,
                                    args=args)
            signal = sim.run()

            t = Trace(signal=signal,
                      plaintext=plaintext,
                      ciphertext=None,
                      key=key,
                      mask=None)
            traces.append(t)

        # Make TraceSet
        ts = TraceSet(name="sim-%s-%d" % (args.algorithm, i))
        ts.set_traces(traces)
        dataset_name = "sim-%s-%s" % (args.algorithm, args.mode)
        ts.save(join(args.output_directory, dataset_name + args.suffix))
Пример #2
0
def simulate_traces_noisy(args):
    specs = get_algorithm_specs(args.algorithm)

    key = random_bytes(specs.key_len)
    for i in range(0, 256):
        print("\rSimulating noisy trace sets for key %d...      " % i, end='')
        key[2] = i
        plaintext = random_bytes(specs.plaintext_len)
        key_string = binascii.hexlify(key).decode('utf-8')
        plaintext_string = binascii.hexlify(plaintext).decode('utf-8')

        sim = ProgramSimulation(specs.executable,
                                (key_string, plaintext_string),
                                specs.method,
                                REGS_TO_CHECK,
                                args=args)
        signal = sim.run()

        traces = []
        for j in range(0, args.num_traces_per_set):
            mod_signal = signal + np.random.normal(args.mu, args.sigma,
                                                   len(signal))
            t = Trace(signal=mod_signal,
                      plaintext=plaintext,
                      ciphertext=None,
                      key=key,
                      mask=None)
            traces.append(t)

            # Debug
            if args.debug:
                plt.plot(mod_signal)
                plt.show()

        # Make TraceSet
        ts = TraceSet(name="sim-noisy-%s-%d" % (args.algorithm, i))
        ts.set_traces(traces)
        dataset_name = "sim-noisy-%s" % args.algorithm
        ts.save(join(args.output_directory, dataset_name + args.suffix))