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>
13 #include "benchmark.h"
16 int aead_ctx_init(struct cryptodev_ctx
* ctx
, int cipher
, int hash
, void* key
, int key_size
, int cfd
)
19 struct session_info_op siop
;
22 memset(ctx
, 0, sizeof(*ctx
));
26 ctx
->sess
.cipher
= cipher
;
28 ctx
->sess
.keylen
= key_size
;
30 if (ioctl(ctx
->cfd
, CIOCGSESSION
, &ctx
->sess
)) {
31 perror("ioctl(CIOCGSESSION)");
36 memset(&siop
, 0, sizeof(siop
));
37 siop
.ses
= ctx
->sess
.ses
;
38 if (ioctl(ctx
->cfd
, CIOCGSESSINFO
, &siop
)) {
39 perror("ioctl(CIOCGSESSINFO)");
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
);
47 /*printf("Alignmask is %x\n", (unsigned int)siop.alignmask);*/
48 ctx
->alignmask
= siop
.alignmask
;
53 void aead_ctx_deinit(struct cryptodev_ctx
* ctx
)
55 if (ioctl(ctx
->cfd
, CIOCFSESSION
, &ctx
->sess
.ses
)) {
56 perror("ioctl(CIOCFSESSION)");
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
;
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)");
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
))
91 struct cryptodev_ctx ctx
;
92 uint8_t digest
[AALG_MAX_RESULT_LEN
];
96 unsigned long elapsed
, counted
;
98 struct benchmark_st bst
;
100 /* Open the crypto device */
101 cfd
= open("/dev/crypto", O_RDWR
, 0);
103 perror("open(/dev/crypto)");
107 aead_ctx_init(&ctx
, cipher
, mac
, ukey
, ukey_size
, cfd
);
109 for (i
=0;i
<sizeof(sizes
)/sizeof(sizes
[0]);i
++) {
111 ret
= start_benchmark(&bst
);
118 if (aead_encrypt(&ctx
, iv
, text
, text
, sizes
[i
], digest
) < 0)
121 } while(benchmark_must_finish
==0);
123 ret
= stop_benchmark(&bst
, &elapsed
);
129 t1
= (double)counted
/(double)elapsed
;
131 /* now check the user function */
133 ret
= start_benchmark(&bst
);
140 user_combo(user_ctx
, text
, ctext
, sizes
[i
], digest
);
142 } while(benchmark_must_finish
==0);
144 ret
= stop_benchmark(&bst
, &elapsed
);
150 t2
= (double)counted
/(double)elapsed
;
153 printf("%d: kernel: %.4f bytes/msec, user: %.4f bytes/msec\n", sizes
[i
], t1
, t2
);
163 aead_ctx_deinit(&ctx
);
165 /* Close the original descriptor */
167 perror("close(cfd)");