static PyObject *init(PyObject *self, PyObject *args) { const char *filename=NULL; if (!PyArg_ParseTuple(args, "|s", &filename)) { PyErr_SetString(PyExc_Exception, "Optional argument should be a string specifying the vcd trace filename."); return NULL; } tb = new VUXN1330_tb("tb"); // Create instance of module Verilated::debug(0); #if VM_TRACE Verilated::traceEverOn(true); // Verilator must compute traced signals tfp = new VerilatedVcdC; tb->trace (tfp, 99); // Trace 99 levels of hierarchy trace = false; if(filename) { open_trace(filename); } #endif // pull fx3 out of reset advance_clk(50); Py_RETURN_NONE; }
static PyObject *start_tracing(PyObject *self, PyObject *args) { #if VM_TRACE const char *filename=NULL; if (!PyArg_ParseTuple(args, "s", &filename)) { PyErr_SetString(PyExc_Exception, "Must provide vcd filename as argument"); return NULL; } if(trace) { PyErr_SetString(PyExc_Exception, "Tracing is already enabled."); return NULL; } open_trace(filename); #endif Py_RETURN_NONE; }
bool io_exec(struct io *io, enum io_type type, const char *dir, char * const env[], const char *argv[], int custom) { int pipefds[2] = { -1, -1 }; bool read_from_stdin = type == IO_RD && (custom & IO_RD_FORWARD_STDIN); bool read_with_stderr = type == IO_RD && (custom & IO_RD_WITH_STDERR); io_init(io); if (dir && !strcmp(dir, argv[0])) return io_open(io, "%s%s", dir, argv[1]); if ((type == IO_RD || type == IO_WR) && pipe(pipefds) < 0) { io->error = errno; return FALSE; } else if (type == IO_AP) { pipefds[1] = custom; } if ((io->pid = fork())) { if (io->pid == -1) io->error = errno; if (pipefds[!(type == IO_WR)] != -1) close(pipefds[!(type == IO_WR)]); if (io->pid != -1) { io->pipe = pipefds[!!(type == IO_WR)]; return TRUE; } } else { if (type != IO_FG) { int devnull = open("/dev/null", O_RDWR); int readfd = type == IO_WR ? pipefds[0] : devnull; int writefd = (type == IO_RD || type == IO_AP) ? pipefds[1] : devnull; int errorfd = open_trace(devnull, argv); /* Inject stdin given on the command line. */ if (read_from_stdin) readfd = dup(STDIN_FILENO); dup2(readfd, STDIN_FILENO); dup2(writefd, STDOUT_FILENO); if (read_with_stderr) dup2(writefd, STDERR_FILENO); else dup2(errorfd, STDERR_FILENO); if (devnull != errorfd) close(errorfd); close(devnull); if (pipefds[0] != -1) close(pipefds[0]); if (pipefds[1] != -1) close(pipefds[1]); } if (dir && *dir && chdir(dir) == -1) exit(errno); if (env) { int i; for (i = 0; env[i]; i++) if (*env[i]) putenv(env[i]); } execvp(argv[0], (char *const*) argv); exit(errno); } if (pipefds[!!(type == IO_WR)] != -1) close(pipefds[!!(type == IO_WR)]); return FALSE; }
int main(int argc, char **argv) { int trace=0; char file[512], cryptdata[128]; mf_t trace_mf; dpa_t *dpa=NULL; uint8_t hypotheses[1024], key[16], plain[16]; int hypos=0; correl_t *results=NULL; time_t start=0, runtime; int cnt; int last_cnt=0; FILE *fl, *ptxts; float signif; int best_keybyte=0; float max_correl=0; hypo_template_t *hypo_templates; if(argc<=2) { printf("see the README\n"); return 0; } sscanf(argv[1],"%2s:%d",file,&cnt); assert((fl=fopen("hypo.txt","w"))); if(!(strcmp(file,"hd"))) { printf("Hamming-Distance keybyte %d\n",cnt); fprintf(fl,"sbox_out %d&ff sbox_in %d&ff 0 0\n",cnt,cnt); } else if(!(strcmp(file,"hw"))) { printf("Hamming-Weight keybyte %d\n",cnt); fprintf(fl,"sbox_out %d&ff null 0&ff 0 0\n",cnt); } else { printf("usage: hd:<keybyte> for hamming dist or hw:<keybyte> for hamming weight\n"); return 0; } fclose(fl); // don't ask... assert((hypos = hypo_templ_gen("hypo.txt", &hypo_templates, NULL))); hypos *= 256; sprintf(file,"%s/aes.log",argv[2]); assert((ptxts = fopen(file, "r"))); while(fgets(cryptdata, 512, ptxts)) { if(!(trace%20)) printf("trace %d\n",trace); assert(parse_hex(cryptdata, plain, 16) == (cryptdata+32)); sprintf(file,"%s/%06d.dat",argv[2],trace); assert(!(open_trace(&trace_mf, file))); // generate hypotheses for(cnt=0; cnt<256; cnt++) { memset(key, cnt&0xff, 16); hypo_gen(plain, key, hypo_templates, hypotheses+cnt); } if(!dpa) { dpa = dpa_init(hypos, trace_mf.len); assert((results = malloc(sizeof(correl_t)*(dpa->tracelen)))); start = time(NULL); } dpa_add(dpa, trace_mf.ptr, hypotheses); trace++; last_cnt = trace; signif = 1.3*(4/sqrt((float)trace)); max_correl = 0; best_keybyte = 0; if(!(trace%100)) { for(cnt=0; cnt<hypos; cnt++) { float max; dpa_get_results(dpa, cnt, results, &max); if(ABS(max) > ABS(max_correl)) { max_correl = max; best_keybyte = cnt; } } printf("key guess 0x%02x correl: %f (signifcant: >=%f)\n",best_keybyte,max_correl,signif); } } runtime = time(NULL); runtime -= start; dpa_speedinfo(dpa, runtime); // get results dpa_get_results(dpa, best_keybyte, results, NULL); assert((fl=fopen("results.txt","w"))); for(cnt=0; cnt < dpa->tracelen; cnt++) fprintf(fl,"%d: %f\n",cnt,results[cnt]); fclose(fl); free(results); dpa_destroy(&dpa); return 0; }
int main(int argc, char **argv) { int trace = 0; char buf[512], cryptdata[128]; mf_t trace_mf; dpa_t *dpa = NULL; uint8_t hypotheses[1024], key[16], plain[16]; int hypos = 16; correl_t *results = NULL; time_t start = 0, runtime; int res, cnt; int last_cnt = 0; FILE *fl_align, *fl_iod; float signif; int best_keybyte = 0; float max_correl = 0; hypo_template_t *hypo_templates; int trace_ofs = (372*12*280)/16; // 78k int trace_len = 100000; assert(argc>=3); fl_iod = fopen(argv[1], "r"); assert(fl_iod); fl_align = fopen(argv[2],"r"); assert(fl_align); while (fgets(buf, 512, fl_iod)) { uint8_t rand[16]; uint8_t sres_kc[12]; char *p = parse_hex(buf+9, rand, 16); assert(p == buf+9+32); p = parse_hex(buf+9+32+1, sres_kc, 12); assert(p == buf+9+32+1+24); int ofs; float diff; res = fscanf(fl_align, "%s %i %f",buf,&ofs,&diff); assert(res == 3); res = open_trace(&trace_mf, buf); assert(!res); for(cnt=0; cnt<16; cnt++) { hypotheses[cnt] = hamming_weight(rand[cnt]); } if (!dpa) { dpa = dpa_init(hypos, trace_len); assert((results = malloc(sizeof(correl_t) * (dpa->tracelen)))); start = time(NULL); } assert(ofs <= trace_ofs); dpa_add(dpa, trace_mf.ptr + trace_ofs - ofs, hypotheses); trace++; last_cnt = trace; if(!(trace%32)) printf("%d\n",trace); } fclose(fl_iod); fclose(fl_align); runtime = time(NULL); runtime -= start; dpa_speedinfo(dpa, runtime); signif = 1.3 * (4 / sqrt((float)trace)); printf("signif: %f\n", signif); for (cnt = 0; cnt < hypos; cnt++) { float max; FILE *res_fl; int i; dpa_get_results(dpa, cnt, results, &max); sprintf(buf,"dpa-%d.txt",cnt); res_fl = fopen(buf,"w"); assert(res_fl); for(i=0;i<dpa->tracelen;i++) fprintf(res_fl,"%d %f\n",i+trace_ofs,results[i]); fclose(res_fl); } free(results); dpa_destroy(&dpa); return 0; }