diff options
Diffstat (limited to 'examples/speed.c')
-rw-r--r-- | examples/speed.c | 183 |
1 files changed, 69 insertions, 114 deletions
diff --git a/examples/speed.c b/examples/speed.c index 1cdd3af..70a2ed8 100644 --- a/examples/speed.c +++ b/examples/speed.c @@ -17,6 +17,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include <fcntl.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -24,11 +25,15 @@ #include <sys/time.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/socket.h> #include <signal.h> #include <unistd.h> -#include "../cryptodev.h" +#include <linux/netlink.h> #include "../ncr.h" +#define ALIGN_NL __attribute__((aligned(NLA_ALIGNTO))) +#define ALG_AES_CBC "cbc(aes)" + static double udifftimeval(struct timeval start, struct timeval end) { return (double)(end.tv_usec - start.tv_usec) + @@ -68,76 +73,7 @@ static void value2human(double bytes, double time, double* data, double* speed,c } -int encrypt_data(int cipher, int fdc, int chunksize) -{ - struct crypt_op cop; - char *buffer, iv[32]; - static int val = 23; - struct timeval start, end; - double total = 0; - double secs, ddata, dspeed; - struct session_op sess; - char metric[16]; - char keybuf[16]; - int keylen; - - memset(keybuf, 0x42, 16); - if (cipher == CRYPTO_NULL) - keylen = 0; - else keylen = 16; - - buffer = malloc(chunksize); - 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(&sess, 0, sizeof(sess)); - sess.cipher = cipher; - sess.keylen = keylen; - sess.key = (unsigned char *)keybuf; - if (ioctl(fdc, CIOCGSESSION, &sess)) { - perror("ioctl(CIOCGSESSION)"); - return 1; - } - - memset(&cop, 0, sizeof(cop)); - cop.ses = sess.ses; - cop.len = chunksize; - cop.iv = (unsigned char *)iv; - cop.op = COP_ENCRYPT; - cop.flags = 0; - cop.src = cop.dst = (unsigned char *)buffer; - - if (ioctl(fdc, CIOCCRYPT, &cop)) { - perror("ioctl(CIOCCRYPT)"); - return 1; - } - - ioctl(fdc, CIOCFSESSION, &sess.ses); - - total+=chunksize; - } while(must_finish==0); - 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); - - return 0; -} - - -int encrypt_data_ncr_direct(int cfd, int algo, int chunksize) +int encrypt_data_ncr_direct(int cfd, const char *algo, int chunksize) { char *buffer, iv[32]; static int val = 23; @@ -146,23 +82,48 @@ int encrypt_data_ncr_direct(int cfd, int algo, int chunksize) double secs, ddata, dspeed; char metric[16]; ncr_key_t key; - struct ncr_key_generate_st kgen; - struct ncr_session_once_op_st nop; - - if (ioctl(cfd, NCRIO_KEY_INIT, &key)) { + struct __attribute__((packed)) { + struct ncr_key_generate f; + struct nlattr algo_head ALIGN_NL; + char algo[sizeof(ALG_AES_CBC)] ALIGN_NL; + struct nlattr bits_head ALIGN_NL; + uint32_t bits ALIGN_NL; + } kgen; + struct __attribute__((packed)) { + struct ncr_session_once f; + struct nlattr key_head ALIGN_NL; + uint32_t key ALIGN_NL; + struct nlattr input_head ALIGN_NL; + struct ncr_session_input_data input ALIGN_NL; + struct nlattr output_head ALIGN_NL; + struct ncr_session_output_buffer output ALIGN_NL; + struct nlattr iv_head ALIGN_NL; + struct nlattr algo_head ALIGN_NL; + char algo[128] ALIGN_NL; + } op; + size_t algo_size; + + algo_size = strlen(algo) + 1; + key = ioctl(cfd, NCRIO_KEY_INIT); + if (key == -1) { 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 */ - + memset(&kgen.f, 0, sizeof(kgen.f)); + kgen.f.input_size = sizeof(kgen); + kgen.f.key = key; + kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo); + kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM; + strcpy(kgen.algo, ALG_AES_CBC); + kgen.bits_head.nla_len = NLA_HDRLEN + sizeof(kgen.bits); + kgen.bits_head.nla_type = NCR_ATTR_SECRET_KEY_BITS; + kgen.bits = 128; /* 16 bytes */ + if (ioctl(cfd, NCRIO_KEY_GENERATE, &kgen)) { fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); - perror("ioctl(NCRIO_KEY_IMPORT)"); + perror("ioctl(NCRIO_KEY_GENERATE)"); return 1; } @@ -180,17 +141,30 @@ int encrypt_data_ncr_direct(int cfd, int algo, int chunksize) gettimeofday(&start, NULL); do { - memset(&nop, 0, sizeof(nop)); - nop.init.algorithm = algo; - nop.init.key = key; - nop.init.op = NCR_OP_ENCRYPT; - nop.op.data.udata.input = buffer; - nop.op.data.udata.input_size = chunksize; - nop.op.data.udata.output = buffer; - nop.op.data.udata.output_size = chunksize; - nop.op.type = NCR_DIRECT_DATA; - - if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) { + size_t output_size; + + memset(&op.f, 0, sizeof(op.f)); + op.f.op = NCR_OP_ENCRYPT; + op.key_head.nla_len = NLA_HDRLEN + sizeof(op.key); + op.key_head.nla_type = NCR_ATTR_KEY; + op.key = key; + op.input_head.nla_len = NLA_HDRLEN + sizeof(op.input); + op.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA; + op.input.data = buffer; + op.input.data_size = chunksize; + op.output_head.nla_len = NLA_HDRLEN + sizeof(op.output); + op.output_head.nla_type = NCR_ATTR_UPDATE_OUTPUT_BUFFER; + op.output.buffer = buffer; + op.output.buffer_size = chunksize; + op.output.result_size_ptr = &output_size; + op.iv_head.nla_len = NLA_HDRLEN + 0; + op.iv_head.nla_type = NCR_ATTR_IV; + op.algo_head.nla_len = NLA_HDRLEN + algo_size; + op.algo_head.nla_type = NCR_ATTR_ALGORITHM; + memcpy(op.algo, algo, algo_size); + op.f.input_size = op.algo + algo_size - (char *)&op; + + if (ioctl(cfd, NCRIO_SESSION_ONCE, &op)) { fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); perror("ioctl(NCRIO_SESSION_ONCE)"); return 1; @@ -211,7 +185,7 @@ int encrypt_data_ncr_direct(int cfd, int algo, int chunksize) int main(void) { - int fd, i, fdc = -1; + int fd, i; signal(SIGALRM, alarm_handler); @@ -219,39 +193,20 @@ int main(void) perror("open()"); return 1; } - if (ioctl(fd, CRIOGET, &fdc)) { - perror("ioctl(CRIOGET)"); - return 1; - } - - fprintf(stderr, "Testing NULL cipher: \n"); - - for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(CRYPTO_NULL, fdc, i)) - break; - } fprintf(stderr, "\nTesting NCR-DIRECT with NULL cipher: \n"); for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data_ncr_direct(fdc, NCR_ALG_NULL, i)) + if (encrypt_data_ncr_direct(fd, "ecb(cipher_null)", i)) break; } - fprintf(stderr, "\nTesting AES-128-CBC cipher: \n"); - - for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(CRYPTO_AES_CBC, fdc, i)) - break; - } fprintf(stderr, "\nTesting NCR-DIRECT with AES-128-CBC cipher: \n"); for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data_ncr_direct(fdc, NCR_ALG_AES_CBC, i)) + if (encrypt_data_ncr_direct(fd, "cbc(aes)", i)) break; } - - close(fdc); close(fd); return 0; } |