whitespace and indenting cleanup
[cryptodev-linux.git] / lib / combo.c
blobb26da0a584eb51b1d4e749720a210056ecab3eae
1 /*
2 * Demo on how to use /dev/crypto device for ciphering.
4 * Placed under public domain.
6 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <sys/ioctl.h>
12 #include <crypto/cryptodev.h>
13 #include "benchmark.h"
14 #include "hash.h"
16 int aead_ctx_init(struct cryptodev_ctx* ctx, int cipher, int hash, void* key, int key_size, int cfd)
18 #ifdef CIOCGSESSINFO
19 struct session_info_op siop;
20 #endif
22 memset(ctx, 0, sizeof(*ctx));
23 ctx->cfd = cfd;
25 ctx->sess.mac = hash;
26 ctx->sess.cipher = cipher;
27 ctx->sess.key = key;
28 ctx->sess.keylen = key_size;
30 if (ioctl(ctx->cfd, CIOCGSESSION, &ctx->sess)) {
31 perror("ioctl(CIOCGSESSION)");
32 return -1;
35 #ifdef CIOCGSESSINFO
36 memset(&siop, 0, sizeof(siop));
37 siop.ses = ctx->sess.ses;
38 if (ioctl(ctx->cfd, CIOCGSESSINFO, &siop)) {
39 perror("ioctl(CIOCGSESSINFO)");
40 return -1;
42 #ifdef DEBUG
43 printf("Got %s-%s with drivers %s and %s\n",
44 siop.cipher_info.cra_name, siop.hash_info.cra_name,
45 siop.cipher_info.cra_driver_name, siop.hash_info.cra_driver_name);
46 #endif
47 /*printf("Alignmask is %x\n", (unsigned int)siop.alignmask);*/
48 ctx->alignmask = siop.alignmask;
49 #endif
50 return 0;
53 void aead_ctx_deinit(struct cryptodev_ctx* ctx)
55 if (ioctl(ctx->cfd, CIOCFSESSION, &ctx->sess.ses)) {
56 perror("ioctl(CIOCFSESSION)");
60 int
61 aead_encrypt(struct cryptodev_ctx* ctx, const void* iv, const void* plaintext, void* ciphertext, size_t size, void* digest)
63 struct crypt_auth_op cryp;
65 memset(&cryp, 0, sizeof(cryp));
67 /* Encrypt data.in to data.encrypted */
68 cryp.ses = ctx->sess.ses;
69 cryp.len = size;
70 cryp.iv = (void*)iv;
71 cryp.iv_len = 16;
72 cryp.src = (void*)plaintext;
73 cryp.dst = (void*)ciphertext;
74 cryp.flags = COP_FLAG_AEAD_TLS_TYPE;
76 if (ioctl(ctx->cfd, CIOCAUTHCRYPT, &cryp)) {
77 perror("ioctl(CIOCAUTHCRYPT)");
78 return -1;
81 return 0;
84 static const int sizes[] = {64, 256, 512, 1024, 4096, 16*1024};
87 int aead_test(int cipher, int mac, void* ukey, int ukey_size,
88 void* user_ctx, void (*user_combo)(void* user_ctx, void* plaintext, void* ciphertext, int size, void* res))
90 int cfd = -1, i, ret;
91 struct cryptodev_ctx ctx;
92 uint8_t digest[AALG_MAX_RESULT_LEN];
93 char text[16*1024];
94 char ctext[16*1024];
95 char iv[16];
96 unsigned long elapsed, counted;
97 double t1, t2;
98 struct benchmark_st bst;
100 /* Open the crypto device */
101 cfd = open("/dev/crypto", O_RDWR, 0);
102 if (cfd < 0) {
103 perror("open(/dev/crypto)");
104 return -1;
107 aead_ctx_init(&ctx, cipher, mac, ukey, ukey_size, cfd);
109 for (i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
110 counted = 0;
111 ret = start_benchmark(&bst);
112 if (ret < 0) {
113 ret = -1;
114 goto finish;
117 do {
118 if (aead_encrypt(&ctx, iv, text, text, sizes[i], digest) < 0)
119 return -2;
120 counted += sizes[i];
121 } while(benchmark_must_finish==0);
123 ret = stop_benchmark(&bst, &elapsed);
124 if (ret < 0) {
125 ret = -1;
126 goto finish;
129 t1 = (double)counted/(double)elapsed;
131 /* now check the user function */
132 counted = 0;
133 ret = start_benchmark(&bst);
134 if (ret < 0) {
135 ret = -1;
136 goto finish;
139 do {
140 user_combo(user_ctx, text, ctext, sizes[i], digest);
141 counted += sizes[i];
142 } while(benchmark_must_finish==0);
144 ret = stop_benchmark(&bst, &elapsed);
145 if (ret < 0) {
146 ret = -1;
147 goto finish;
150 t2 = (double)counted/(double)elapsed;
152 #ifdef DEBUG
153 printf("%d: kernel: %.4f bytes/msec, user: %.4f bytes/msec\n", sizes[i], t1, t2);
154 #endif
155 if (t1 > t2) {
156 ret = sizes[i];
157 goto finish;
161 ret = -1;
162 finish:
163 aead_ctx_deinit(&ctx);
165 /* Close the original descriptor */
166 if (close(cfd)) {
167 perror("close(cfd)");
168 return 1;
170 return ret;