int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask) { struct crypt_op cop; char *buffer, iv[32]; static int val = 23; struct timeval start, end; double total = 0; double secs, ddata, dspeed; char metric[16]; if (alignmask) { if (posix_memalign((void **)&buffer, MAX(alignmask + 1, sizeof(void*)), chunksize)) { printf("posix_memalign() failed! (mask %x, size: %d)\n", alignmask+1, chunksize); return 1; } } else { if (!(buffer = malloc(chunksize))) { perror("malloc()"); return 1; } } memset(iv, 0x23, 32); printf("\tEncrypting in chunks of %d bytes: ", chunksize); fflush(stdout); memset(buffer, val++, chunksize); must_finish = 0; alarm(5); gettimeofday(&start, NULL); do { memset(&cop, 0, sizeof(cop)); cop.ses = sess->ses; cop.len = chunksize; cop.iv = (unsigned char *)iv; cop.op = COP_ENCRYPT; cop.src = cop.dst = (unsigned char *)buffer; if (ioctl(fdc, CIOCCRYPT, &cop)) { perror("ioctl(CIOCCRYPT)"); return 1; } total+=chunksize; } while(must_finish==0); gettimeofday(&end, NULL); secs = udifftimeval(start, end)/ 1000000.0; value2human(si, total, secs, &ddata, &dspeed, metric); printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs); printf ("%.2f %s/sec\n", dspeed, metric); free(buffer); return 0; }
static void mac_bench (int algo, int size) { void *_key; struct timespec start, stop; double secs; double data_size = 0; double ddata, dspeed; int blocksize = gnutls_hmac_get_len (algo); char metric[16]; _key = malloc (blocksize); if (_key == NULL) return; memset (_key, 0xf0, blocksize); printf ("Checking %s (%dkb payload)... ", gnutls_mac_get_name (algo), size); fflush (stdout); must_finish = 0; alarm (5); gettime (&start); do { gnutls_hmac_fast (algo, _key, blocksize, data, size * 1024, _key); data_size += size * 1024; } while (must_finish == 0); gettime (&stop); secs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) - (start.tv_sec * 1000 + start.tv_nsec / (1000 * 1000))); secs /= 1000; value2human (data_size, secs, &ddata, &dspeed, metric); printf ("Hashed %.2f %s in %.2f secs: ", ddata, metric, secs); printf ("%.2f %s/sec\n", dspeed, metric); free (_key); }
/* returns the elapsed time */ double stop_benchmark(struct benchmark_st * st, const char* metric) { double secs; unsigned long lsecs; struct timespec stop; double dspeed, ddata; char imetric[16]; #if defined(_WIN32) if (st->wtimer != NULL) CloseHandle (st->wtimer); if (st->wthread != NULL) CloseHandle (st->wthread); #else signal(SIGALRM, st->old_handler); #endif gettime (&stop); lsecs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) - (st->start.tv_sec * 1000 + st->start.tv_nsec / (1000 * 1000))); secs = lsecs; secs /= 1000; if (metric == NULL) { /* assume bytes/sec */ value2human (st->size, secs, &ddata, &dspeed, imetric); printf (" Processed %.2f %s in %.2f secs: ", ddata, imetric, secs); printf ("%.2f %s/sec\n", dspeed, imetric); } else { ddata = (double) st->size; dspeed = ddata / secs; printf (" Processed %.2f %s in %.2f secs: ", ddata, metric, secs); printf ("%.2f %s/sec\n", dspeed, metric); } return secs; }
int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask) { struct crypt_op cop; char *buffer[64], iv[32]; static int val = 23; struct timeval start, end; double total = 0; double secs, ddata, dspeed; char metric[16]; int rc, wqueue = 0, bufidx = 0; memset(iv, 0x23, 32); printf("\tEncrypting in chunks of %d bytes: ", chunksize); fflush(stdout); for (rc = 0; rc < 64; rc++) { if (alignmask) { if (posix_memalign((void **)(buffer + rc), alignmask + 1, chunksize)) { printf("posix_memalign() failed!\n"); return 1; } } else { if (!(buffer[rc] = malloc(chunksize))) { perror("malloc()"); return 1; } } memset(buffer[rc], val++, chunksize); } pfd.fd = fdc; pfd.events = POLLOUT | POLLIN; must_finish = 0; alarm(5); gettimeofday(&start, NULL); do { if ((rc = poll(&pfd, 1, 100)) < 0) { if (errno & (ERESTART | EINTR)) continue; fprintf(stderr, "errno = %d ", errno); perror("poll()"); return 1; } if (pfd.revents & POLLOUT) { memset(&cop, 0, sizeof(cop)); cop.ses = sess->ses; cop.len = chunksize; cop.iv = (unsigned char *)iv; cop.op = COP_ENCRYPT; cop.src = cop.dst = (unsigned char *)buffer[bufidx]; bufidx = (bufidx + 1) % 64; if (ioctl(fdc, CIOCASYNCCRYPT, &cop)) { perror("ioctl(CIOCASYNCCRYPT)"); return 1; } wqueue++; } if (pfd.revents & POLLIN) { if (ioctl(fdc, CIOCASYNCFETCH, &cop)) { perror("ioctl(CIOCASYNCFETCH)"); return 1; } wqueue--; total += cop.len; } } while(!must_finish || wqueue); gettimeofday(&end, NULL); secs = udifftimeval(start, end)/ 1000000.0; value2human(total, secs, &ddata, &dspeed, metric); printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs); printf ("%.2f %s/sec\n", dspeed, metric); for (rc = 0; rc < 64; rc++) free(buffer[rc]); return 0; }
static void cipher_bench (int algo, int size) { int ret; gnutls_cipher_hd_t ctx; void *_key, *_iv; gnutls_datum_t key, iv; struct timespec start, stop; double secs; double data_size = 0; double dspeed, ddata; int blocksize = gnutls_cipher_get_block_size (algo); int keysize = gnutls_cipher_get_key_size (algo); char metric[16]; _key = malloc (keysize); if (_key == NULL) return; memset (_key, 0xf0, keysize); _iv = malloc (blocksize); if (_iv == NULL) return; memset (_iv, 0xf0, blocksize); iv.data = _iv; iv.size = blocksize; key.data = _key; key.size = keysize; printf ("Checking %s (%dkb payload)... ", gnutls_cipher_get_name (algo), size); fflush (stdout); must_finish = 0; alarm (5); gettime (&start); ret = gnutls_cipher_init (&ctx, algo, &key, &iv); if (ret < 0) { fprintf (stderr, "error: %s\n", gnutls_strerror (ret)); goto leave; } do { gnutls_cipher_encrypt (ctx, data, size * 1024); data_size += size * 1024; } while (must_finish == 0); gnutls_cipher_deinit (ctx); gettime (&stop); secs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) - (start.tv_sec * 1000 + start.tv_nsec / (1000 * 1000))); secs /= 1000; value2human (data_size, secs, &ddata, &dspeed, metric); printf ("Encrypted %.2f %s in %.2f secs: ", ddata, metric, secs); printf ("%.2f %s/sec\n", dspeed, metric); leave: free (_key); free (_iv); }