diff options
| author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2010-07-19 10:32:30 +0200 |
|---|---|---|
| committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2010-07-19 10:32:30 +0200 |
| commit | d4dc626658726f9c09619924dd6808f47f411db1 (patch) | |
| tree | 5bd7d178355414cd99227419eb9cceb6fa1ea8c7 /examples | |
| parent | 13629ab760d95f17aa1ebc36d1870d6b044c3acc (diff) | |
| download | cryptodev-linux-d4dc626658726f9c09619924dd6808f47f411db1.tar.gz cryptodev-linux-d4dc626658726f9c09619924dd6808f47f411db1.tar.xz cryptodev-linux-d4dc626658726f9c09619924dd6808f47f411db1.zip | |
Added NULL cipher and a speed program to compare old cryptodev operations and ncr ones.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/speed.c | 125 |
1 files changed, 120 insertions, 5 deletions
diff --git a/examples/speed.c b/examples/speed.c index bf19863..99dc09e 100644 --- a/examples/speed.c +++ b/examples/speed.c @@ -27,6 +27,7 @@ #include <signal.h> #include <unistd.h> #include "../cryptodev.h" +#include "../ncr.h" static double udifftimeval(struct timeval start, struct timeval end) { @@ -67,7 +68,7 @@ static void value2human(double bytes, double time, double* data, double* speed,c } -int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags) +int encrypt_data(struct session_op *sess, int fdc, int chunksize) { struct crypt_op cop; char *buffer, iv[32]; @@ -95,7 +96,7 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags) cop.len = chunksize; cop.iv = (unsigned char *)iv; cop.op = COP_ENCRYPT; - cop.flags = flags; + cop.flags = 0; cop.src = cop.dst = (unsigned char *)buffer; if (ioctl(fdc, CIOCCRYPT, &cop)) { @@ -115,6 +116,107 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int flags) return 0; } +int encrypt_data_ncr(int cfd, int algo, int chunksize) +{ + char *buffer, iv[32]; + static int val = 23; + struct timeval start, end; + double total = 0; + double secs, ddata, dspeed; + char metric[16]; + ncr_key_t key; + struct ncr_key_generate_st kgen; + struct ncr_data_init_st dinit; + struct ncr_data_st kdata; + struct ncr_session_once_op_st nop; + ncr_data_t dd; + + if (ioctl(cfd, NCRIO_KEY_INIT, &key)) { + fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); + perror("ioctl(NCRIO_KEY_INIT)"); + return 1; + } + + kgen.desc = key; + kgen.params.algorithm = NCR_ALG_AES_CBC; + kgen.params.keyflags = NCR_KEY_FLAG_EXPORTABLE; + kgen.params.params.secret.bits = 128; /* 16 bytes */ + + if (ioctl(cfd, NCRIO_KEY_GENERATE, &kgen)) { + fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); + perror("ioctl(NCRIO_KEY_IMPORT)"); + return 1; + } + + + buffer = malloc(chunksize); + memset(iv, 0x23, 32); + + dinit.max_object_size = chunksize; + dinit.flags = NCR_DATA_FLAG_EXPORTABLE; + dinit.initial_data = buffer; + dinit.initial_data_size = chunksize; + + if (ioctl(cfd, NCRIO_DATA_INIT, &dinit)) { + fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); + perror("ioctl(NCRIO_DATA_INIT)"); + return 1; + } + dd = dinit.desc; + + printf("\tEncrypting in chunks of %d bytes: ", chunksize); + fflush(stdout); + + memset(buffer, val++, chunksize); + + must_finish = 0; + alarm(5); + + gettimeofday(&start, NULL); + do { + kdata.data = buffer; + kdata.data_size = chunksize; + kdata.desc = dd; + kdata.append_flag = 0; + + if (ioctl(cfd, NCRIO_DATA_SET, &kdata)) { + fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); + perror("ioctl(NCRIO_DATA_INIT)"); + return 1; + } + + memset(&nop, 0, sizeof(nop)); + nop.init.algorithm = algo; + nop.init.params.key = key; + nop.init.op = NCR_OP_ENCRYPT; + nop.op.data.cipher.plaintext = dd; + nop.op.data.cipher.ciphertext = dd; + + if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) { + fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); + perror("ioctl(NCRIO_SESSION_ONCE)"); + return 1; + } + + total+=chunksize; + } while(must_finish==0); + gettimeofday(&end, NULL); + + if (ioctl(cfd, NCRIO_DATA_DEINIT, &dd)) { + fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); + perror("ioctl(NCRIO_DATA_INIT)"); + return 1; + } + + 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); + + return 0; +} + int main(void) { int fd, i, fdc = -1; @@ -131,7 +233,7 @@ int main(void) perror("ioctl(CRIOGET)"); return 1; } - + fprintf(stderr, "Testing NULL cipher: \n"); memset(&sess, 0, sizeof(sess)); sess.cipher = CRYPTO_NULL; @@ -143,7 +245,7 @@ int main(void) } for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(&sess, fdc, i, 0)) + if (encrypt_data(&sess, fdc, i)) break; } @@ -159,10 +261,23 @@ int main(void) } for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(&sess, fdc, i, 0)) + if (encrypt_data(&sess, fdc, i)) + break; + } + + fprintf(stderr, "\nTesting NCR with NULL cipher: \n"); + for (i = 256; i <= (64 * 1024); i *= 2) { + if (encrypt_data_ncr(fdc, NCR_ALG_NULL, i)) + break; + } + + fprintf(stderr, "\nTesting NCR with AES-128-CBC cipher: \n"); + for (i = 256; i <= (64 * 1024); i *= 2) { + if (encrypt_data_ncr(fdc, NCR_ALG_AES_CBC, i)) break; } + close(fdc); close(fd); return 0; |
