Added support SHA224
[cryptodev-linux.git] / tests / hash_comp.c
blob9e700a1b21824855c8b0ec757c347669b7c6ac3d
1 /*
2 * Compare digest results with ones from openssl.
4 * Placed under public domain.
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdint.h>
14 #include <sys/ioctl.h>
15 #include <crypto/cryptodev.h>
17 #include "openssl_wrapper.h"
19 #define BLOCK_SIZE 16
20 #define MAX_DATALEN (64 * 1024)
22 static void printhex(unsigned char *buf, int buflen)
24 while (buflen-- > 0) {
25 printf("\\x%.2x", *(buf++));
27 printf("\n");
30 static int
31 test_crypto(int cfd, struct session_op *sess, int datalen)
33 unsigned char *data;
35 unsigned char mac[AALG_MAX_RESULT_LEN];
37 unsigned char mac_comp[AALG_MAX_RESULT_LEN];
39 struct crypt_op cryp;
41 int ret = 0, fail = 0;
43 data = malloc(datalen);
44 memset(data, datalen & 0xff, datalen);
46 memset(mac, 0, sizeof(mac));
47 memset(mac_comp, 0, sizeof(mac_comp));
49 memset(&cryp, 0, sizeof(cryp));
51 /* Encrypt data.in to data.encrypted */
52 cryp.ses = sess->ses;
53 cryp.len = datalen;
54 cryp.src = data;
55 cryp.mac = mac;
56 cryp.op = COP_ENCRYPT;
57 if ((ret = ioctl(cfd, CIOCCRYPT, &cryp))) {
58 perror("ioctl(CIOCCRYPT)");
59 goto out;
62 cryp.mac = mac_comp;
64 if ((ret = openssl_cioccrypt(sess, &cryp))) {
65 fprintf(stderr, "openssl_cioccrypt() failed!\n");
66 goto out;
69 if (memcmp(mac, mac_comp, AALG_MAX_RESULT_LEN)) {
70 printf("fail for datalen %d, MACs do not match!\n", datalen);
71 fail = 1;
72 printf("wrong mac: ");
73 printhex(mac, 20);
74 printf("right mac: ");
75 printhex(mac_comp, 20);
78 out:
79 free(data);
80 return ret;
83 #define max(a, b) ((a) > (b) ? (a) : (b))
84 #define min(a, b) ((a) < (b) ? (a) : (b))
86 int
87 main(int argc, char **argv)
89 int fd;
90 struct session_op sess;
91 int datalen = BLOCK_SIZE;
92 int datalen_end = MAX_DATALEN;
93 int i;
95 if (argc > 1) {
96 datalen = min(max(atoi(argv[1]), BLOCK_SIZE), MAX_DATALEN);
97 datalen_end = datalen;
99 if (argc > 2) {
100 datalen_end = min(atoi(argv[2]), MAX_DATALEN);
101 if (datalen_end < datalen)
102 datalen_end = datalen;
105 /* Open the crypto device */
106 fd = open("/dev/crypto", O_RDWR, 0);
107 if (fd < 0) {
108 perror("open(/dev/crypto)");
109 return 1;
112 memset(&sess, 0, sizeof(sess));
114 /* Hash test */
115 sess.mac = CRYPTO_SHA1;
116 if (ioctl(fd, CIOCGSESSION, &sess)) {
117 perror("ioctl(CIOCGSESSION)");
118 return 1;
121 #ifdef CIOCGSESSINFO
123 struct session_info_op siop = {
124 .ses = sess.ses,
127 if (ioctl(fd, CIOCGSESSINFO, &siop)) {
128 perror("ioctl(CIOCGSESSINFO)");
129 } else {
130 printf("requested mac CRYPTO_SHA1, got hash %s with driver %s\n",
131 siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
134 #endif
136 for (; datalen <= datalen_end; datalen += BLOCK_SIZE) {
137 if (test_crypto(fd, &sess, datalen)) {
138 printf("test_crypto() failed for datalen of %d\n", datalen);
139 return 1;
143 /* Finish crypto session */
144 if (ioctl(fd, CIOCFSESSION, &sess.ses)) {
145 perror("ioctl(CIOCFSESSION)");
148 close(fd);
149 return 0;