Added SIOP_FLAG_KERNEL_DRIVER_ONLY to correspond to CRYPTO_ALG_KERN_DRIVER_ONLY if...
[cryptodev-linux.git] / examples / sha.c
blob4f45a6b01a500168eb1a879fa7648d30977b93c3
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 "sha.h"
15 int sha_ctx_init(struct cryptodev_ctx* ctx, int cfd, const uint8_t *key, unsigned int key_size)
17 #ifdef CIOCGSESSINFO
18 struct session_info_op siop;
19 #endif
21 memset(ctx, 0, sizeof(*ctx));
22 ctx->cfd = cfd;
24 if (key == NULL)
25 ctx->sess.mac = CRYPTO_SHA1;
26 else {
27 ctx->sess.mac = CRYPTO_SHA1_HMAC;
28 ctx->sess.mackeylen = key_size;
29 ctx->sess.mackey = (void*)key;
31 if (ioctl(ctx->cfd, CIOCGSESSION, &ctx->sess)) {
32 perror("ioctl(CIOCGSESSION)");
33 return -1;
36 #ifdef CIOCGSESSINFO
37 siop.ses = ctx->sess.ses;
38 if (ioctl(ctx->cfd, CIOCGSESSINFO, &siop)) {
39 perror("ioctl(CIOCGSESSINFO)");
40 return -1;
42 printf("Got %s with driver %s\n",
43 siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
44 if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)) {
45 printf("Note: This is not an accelerated cipher\n");
47 /*printf("Alignmask is %x\n", (unsigned int)siop.alignmask);*/
48 ctx->alignmask = siop.alignmask;
49 #endif
50 return 0;
53 void sha_ctx_deinit(struct cryptodev_ctx* ctx)
55 if (ioctl(ctx->cfd, CIOCFSESSION, &ctx->sess.ses)) {
56 perror("ioctl(CIOCFSESSION)");
60 int
61 sha_hash(struct cryptodev_ctx* ctx, const void* text, size_t size, void* digest)
63 struct crypt_op cryp;
64 void* p;
66 /* check text and ciphertext alignment */
67 if (ctx->alignmask) {
68 p = (void*)(((unsigned long)text + ctx->alignmask) & ~ctx->alignmask);
69 if (text != p) {
70 fprintf(stderr, "text is not aligned\n");
71 return -1;
75 memset(&cryp, 0, sizeof(cryp));
77 /* Encrypt data.in to data.encrypted */
78 cryp.ses = ctx->sess.ses;
79 cryp.len = size;
80 cryp.src = (void*)text;
81 cryp.mac = digest;
82 if (ioctl(ctx->cfd, CIOCCRYPT, &cryp)) {
83 perror("ioctl(CIOCCRYPT)");
84 return -1;
87 return 0;
90 int
91 main()
93 int cfd = -1, i;
94 struct cryptodev_ctx ctx;
95 uint8_t digest[20];
96 char text[] = "The quick brown fox jumps over the lazy dog";
97 uint8_t expected[] = "\x2f\xd4\xe1\xc6\x7a\x2d\x28\xfc\xed\x84\x9e\xe1\xbb\x76\xe7\x39\x1b\x93\xeb\x12";
99 /* Open the crypto device */
100 cfd = open("/dev/crypto", O_RDWR, 0);
101 if (cfd < 0) {
102 perror("open(/dev/crypto)");
103 return 1;
106 /* Set close-on-exec (not really neede here) */
107 if (fcntl(cfd, F_SETFD, 1) == -1) {
108 perror("fcntl(F_SETFD)");
109 return 1;
112 sha_ctx_init(&ctx, cfd, NULL, 0);
114 sha_hash(&ctx, text, strlen(text), digest);
116 sha_ctx_deinit(&ctx);
118 printf("digest: ");
119 for (i = 0; i < 20; i++) {
120 printf("%02x:", digest[i]);
122 printf("\n");
124 if (memcmp(digest, expected, 20) != 0) {
125 fprintf(stderr, "SHA1 hashing failed\n");
126 return 1;
129 /* Close the original descriptor */
130 if (close(cfd)) {
131 perror("close(cfd)");
132 return 1;
135 return 0;