2 * Demo on how to use /dev/crypto device for ciphering.
4 * Placed under public domain.
11 #include <sys/ioctl.h>
12 #include <crypto/cryptodev.h>
14 #include "benchmark.h"
16 int hash_ctx_init(struct cryptodev_ctx
* ctx
, int hash
, int cfd
)
19 struct session_info_op siop
;
22 memset(ctx
, 0, sizeof(*ctx
));
27 if (ioctl(ctx
->cfd
, CIOCGSESSION
, &ctx
->sess
)) {
28 perror("ioctl(CIOCGSESSION)");
33 memset(&siop
, 0, sizeof(siop
));
34 siop
.ses
= ctx
->sess
.ses
;
35 if (ioctl(ctx
->cfd
, CIOCGSESSINFO
, &siop
)) {
36 perror("ioctl(CIOCGSESSINFO)");
40 printf("Got %s with driver %s\n",
41 siop
.hash_info
.cra_name
, siop
.hash_info
.cra_driver_name
);
43 /*printf("Alignmask is %x\n", (unsigned int)siop.alignmask);*/
44 ctx
->alignmask
= siop
.alignmask
;
49 void hash_ctx_deinit(struct cryptodev_ctx
* ctx
)
51 if (ioctl(ctx
->cfd
, CIOCFSESSION
, &ctx
->sess
.ses
)) {
52 perror("ioctl(CIOCFSESSION)");
57 hash(struct cryptodev_ctx
* ctx
, const void* text
, size_t size
, void* digest
)
61 memset(&cryp
, 0, sizeof(cryp
));
63 /* Encrypt data.in to data.encrypted */
64 cryp
.ses
= ctx
->sess
.ses
;
66 cryp
.src
= (void*)text
;
68 if (ioctl(ctx
->cfd
, CIOCCRYPT
, &cryp
)) {
69 perror("ioctl(CIOCCRYPT)");
76 static const int sizes
[] = {64, 256, 512, 1024, 4096, 16*1024};
78 /* Worst case running time: around 1.2 secs
80 int hash_test(int algo
, void (*user_hash
)(void* text
, int size
, void* res
))
83 struct cryptodev_ctx ctx
;
84 uint8_t digest
[AALG_MAX_RESULT_LEN
];
86 unsigned long elapsed
, counted
;
88 struct benchmark_st bst
;
90 /* Open the crypto device */
91 cfd
= open("/dev/crypto", O_RDWR
, 0);
93 perror("open(/dev/crypto)");
97 hash_ctx_init(&ctx
, algo
, cfd
);
99 for (i
=0;i
<sizeof(sizes
)/sizeof(sizes
[0]);i
++) {
101 ret
= start_benchmark(&bst
);
108 hash(&ctx
, text
, sizes
[i
], digest
);
110 } while(benchmark_must_finish
==0);
112 ret
= stop_benchmark(&bst
, &elapsed
);
117 t1
= (double)counted
/(double)elapsed
;
119 /* now check the user function */
121 ret
= start_benchmark(&bst
);
129 user_hash(text
, sizes
[i
], digest
);
131 } while(benchmark_must_finish
==0);
133 ret
= stop_benchmark(&bst
, &elapsed
);
139 t2
= (double)counted
/(double)elapsed
;
146 printf("%d: kernel: %.4f bytes/msec, user: %.4f bytes/msec\n", sizes
[i
], t1
, t2
);
152 hash_ctx_deinit(&ctx
);
154 /* Close the original descriptor */
156 perror("close(cfd)");