def process_trace_set_paths(result, trace_set_paths, conf, request_id=None, keep_trace_sets=False): num_todo = len(trace_set_paths) num_done = 0 for trace_set_path in trace_set_paths: # Get trace name from path # trace_set_name = basename(trace_set_path) logger.info("Processing '%s' (%d/%d)" % (trace_set_path, num_done, num_todo)) # Load trace trace_set = emio.get_trace_set(trace_set_path, conf.format, ignore_malformed=False, remote=False) if trace_set is None: logger.warning( "Failed to load trace set %s (got None). Skipping..." % trace_set_path) continue # Process trace set process_trace_set(result, trace_set, conf, request_id, keep_trace_sets) num_done += 1
def compress_trace_set(trace_set_path): if trace_set_path.endswith('.npy'): parent_dataset_path = os.path.dirname(trace_set_path) manifest_path = os.path.join(parent_dataset_path, 'manifest.emcap') if os.path.exists(manifest_path): # Open manifest with open(manifest_path, 'rb') as manifest_file: manifest = pickle.load(manifest_file) conf = manifest['conf'] # Load trace set trace_set = emio.get_trace_set(trace_set_path, 'cw', remote=False) conf_delete_action( conf, 'optimize_capture' ) # Make sure there is no optimize_capture action anymore # Add appropriate actions if 'pca' in manifest: conf.actions.append(Action('pca[%s]' % manifest_path)) elif 'autoenc' in manifest: conf.actions.append(Action('corrtest[autoenc]')) # Perform compression result = EMResult() ops.process_trace_set(result, trace_set, conf, keep_trace_sets=True) processed_trace_set = result.trace_sets[0] # Save compressed trace set processed_trace_set.save(os.path.abspath(parent_dataset_path), dry=False) else: raise EMMAException( "No manifest.emcap in %s, so don't know how to compress." % parent_dataset_path) else: raise EMMAException("Not a valid traceset_path in numpy format")
def remote_get_trace_set(trace_set_path, format, ignore_malformed): return emio.get_trace_set(trace_set_path, format, ignore_malformed, remote=False)
parser.add_argument("--limit", type=int, default=0, help="Limit number of trace sets (0=infinite)") args = parser.parse_args() dataset_name = args.dataset_name dataset_name_pca = dataset_name + "-pca" # Gather signals dataset = get_dataset(dataset_name, remote=False) all_signals = [] for count, trace_set_path in enumerate(dataset.trace_set_paths): print("\rGathering signal %d " % count) trace_set_path = os.path.join(dataset.root, trace_set_path) trace_set = get_trace_set(trace_set_path, "cw", remote=False) for trace in trace_set.traces: all_signals.append(trace.signal) if count + 1 == args.limit: break all_signals = np.array(all_signals) # Do PCA print("Performing PCA") pca = PCA() pca.fit(all_signals) # Save PCA model with open("pca-components-%s.p" % dataset_name, 'wb') as f: pickle.dump(pca, f) print("Dumped PCA model to pca-components-%s.p" % dataset_name)
plt.plot(signal) plt.show() if __name__ == "__main__": parser = argparse.ArgumentParser(description='Electromagnetic Mining Array (EMMA): Dataset visualization tool', formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('dataset', type=str, help='Identifier of dataset to use', nargs='+') parser.add_argument('--mean', default=False, action='store_true', help='Plot mean of signals') args, unknown = parser.parse_known_args() for dataset_name in args.dataset: dataset = emio.get_dataset(dataset_name, remote=False) print("Dataset: %s\nFormat: %s" % (dataset.id, dataset.format)) # Calculate mean for trace_set_path in dataset.trace_set_paths: trace_set = emio.get_trace_set(join(dataset.root, trace_set_path), dataset.format, ignore_malformed=False, remote=False) if args.mean: signals = [] for trace in trace_set.traces: signals.append(trace.signal) signals = np.array(signals) plot_signal(np.mean(signals, axis=0)) else: for trace in trace_set.traces: print("Key: %s" % numpy_to_hex(trace.key)) print("Plaintext: %s" % numpy_to_hex(trace.plaintext)) print("---------------------------------------") plot_signal(trace.signal)