added previous page count to log. minor formatting.
[cryptodev-linux.git] / lib / hash.c
bloba90e2262cba5b7a2e741befde35226fe2ee1ad05
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 "hash.h"
14 #include "benchmark.h"
16 int hash_ctx_init(struct cryptodev_ctx* ctx, int hash, 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;
27 if (ioctl(ctx->cfd, CIOCGSESSION, &ctx->sess)) {
28 perror("ioctl(CIOCGSESSION)");
29 return -1;
32 #ifdef CIOCGSESSINFO
33 memset(&siop, 0, sizeof(siop));
34 siop.ses = ctx->sess.ses;
35 if (ioctl(ctx->cfd, CIOCGSESSINFO, &siop)) {
36 perror("ioctl(CIOCGSESSINFO)");
37 return -1;
39 #ifdef DEBUG
40 printf("Got %s with driver %s\n",
41 siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
42 #endif
43 /*printf("Alignmask is %x\n", (unsigned int)siop.alignmask);*/
44 ctx->alignmask = siop.alignmask;
45 #endif
46 return 0;
49 void hash_ctx_deinit(struct cryptodev_ctx* ctx)
51 if (ioctl(ctx->cfd, CIOCFSESSION, &ctx->sess.ses)) {
52 perror("ioctl(CIOCFSESSION)");
56 int
57 hash(struct cryptodev_ctx* ctx, const void* text, size_t size, void* digest)
59 struct crypt_op cryp;
61 memset(&cryp, 0, sizeof(cryp));
63 /* Encrypt data.in to data.encrypted */
64 cryp.ses = ctx->sess.ses;
65 cryp.len = size;
66 cryp.src = (void*)text;
67 cryp.mac = digest;
68 if (ioctl(ctx->cfd, CIOCCRYPT, &cryp)) {
69 perror("ioctl(CIOCCRYPT)");
70 return -1;
73 return 0;
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))
82 int cfd = -1, i, ret;
83 struct cryptodev_ctx ctx;
84 uint8_t digest[AALG_MAX_RESULT_LEN];
85 char text[16*1024];
86 unsigned long elapsed, counted;
87 double t1, t2;
88 struct benchmark_st bst;
90 /* Open the crypto device */
91 cfd = open("/dev/crypto", O_RDWR, 0);
92 if (cfd < 0) {
93 perror("open(/dev/crypto)");
94 return -1;
97 hash_ctx_init(&ctx, algo, cfd);
99 for (i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
100 counted = 0;
101 ret = start_benchmark(&bst);
102 if (ret < 0) {
103 ret = -1;
104 goto finish;
107 do {
108 hash(&ctx, text, sizes[i], digest);
109 counted += sizes[i];
110 } while(benchmark_must_finish==0);
112 ret = stop_benchmark(&bst, &elapsed);
113 if (ret < 0) {
114 ret = -1;
115 goto finish;
117 t1 = (double)counted/(double)elapsed;
119 /* now check the user function */
120 counted = 0;
121 ret = start_benchmark(&bst);
122 if (ret < 0) {
123 ret = -1;
124 goto finish;
128 do {
129 user_hash(text, sizes[i], digest);
130 counted += sizes[i];
131 } while(benchmark_must_finish==0);
133 ret = stop_benchmark(&bst, &elapsed);
134 if (ret < 0) {
135 ret = -1;
136 goto finish;
139 t2 = (double)counted/(double)elapsed;
141 if (t1 > t2) {
142 ret = sizes[i];
143 goto finish;
145 #ifdef DEBUG
146 printf("%d: kernel: %.4f bytes/msec, user: %.4f bytes/msec\n", sizes[i], t1, t2);
147 #endif
150 ret = -1;
151 finish:
152 hash_ctx_deinit(&ctx);
154 /* Close the original descriptor */
155 if (close(cfd)) {
156 perror("close(cfd)");
157 return 1;
159 return ret;