Added support SHA224
[cryptodev-linux.git] / tests / cipher_comp.c
blobb2bc5afc198833d184da9de848680009487751e6
1 /*
2 * Compare encryption 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 KEY_SIZE 16
21 #define MAX_DATALEN (64 * 1024)
24 static int
25 test_crypto(int cfd, struct session_op *sess, int datalen)
27 char *data, *encrypted;
28 char *encrypted_comp;
30 char iv_in[BLOCK_SIZE];
31 char iv[BLOCK_SIZE];
32 char iv_comp[BLOCK_SIZE];
34 struct crypt_op cryp;
36 int ret = 0, fail = 0;
38 data = malloc(datalen);
39 encrypted = malloc(datalen);
40 encrypted_comp = malloc(datalen);
41 memset(data, datalen & 0xff, datalen);
42 memset(encrypted, 0x27, datalen);
43 memset(encrypted_comp, 0x41, datalen);
45 memset(iv_in, 0x23, sizeof(iv_in));
46 memcpy(iv, iv_in, sizeof(iv));
47 memcpy(iv_comp, iv_in, sizeof(iv_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.dst = encrypted;
56 cryp.iv = iv;
57 cryp.op = COP_ENCRYPT;
58 cryp.flags = COP_FLAG_WRITE_IV;
59 if ((ret = ioctl(cfd, CIOCCRYPT, &cryp))) {
60 perror("ioctl(CIOCCRYPT)");
61 goto out;
64 cryp.dst = encrypted_comp;
65 cryp.iv = iv_comp;
67 if ((ret = openssl_cioccrypt(sess, &cryp))) {
68 fprintf(stderr, "openssl_cioccrypt() failed!\n");
69 goto out;
72 if ((ret = memcmp(encrypted, encrypted_comp, cryp.len))) {
73 printf("fail for datalen %d, cipher texts do not match!\n", datalen);
75 if ((ret = memcmp(iv, iv_comp, BLOCK_SIZE))) {
76 printf("fail for datalen %d, IVs do not match!\n", datalen);
78 out:
79 free(data);
80 free(encrypted);
81 free(encrypted_comp);
82 return ret;
85 #define max(a, b) ((a) > (b) ? (a) : (b))
86 #define min(a, b) ((a) < (b) ? (a) : (b))
88 int
89 main(int argc, char **argv)
91 int fd;
92 struct session_op sess;
93 unsigned char key[KEY_SIZE];
94 int datalen = BLOCK_SIZE;
95 int datalen_end = MAX_DATALEN;
96 int i;
98 if (argc > 1) {
99 datalen = min(max(atoi(argv[1]), BLOCK_SIZE), MAX_DATALEN);
100 datalen_end = datalen;
102 if (argc > 2) {
103 datalen_end = min(atoi(argv[2]), MAX_DATALEN);
104 if (datalen_end < datalen)
105 datalen_end = datalen;
108 /* Open the crypto device */
109 fd = open("/dev/crypto", O_RDWR, 0);
110 if (fd < 0) {
111 perror("open(/dev/crypto)");
112 return 1;
115 for (i = 0; i < KEY_SIZE; i++)
116 key[i] = i & 0xff;
117 memset(&sess, 0, sizeof(sess));
119 /* encryption test */
120 sess.cipher = CRYPTO_AES_CBC;
121 sess.keylen = KEY_SIZE;
122 sess.key = key;
123 if (ioctl(fd, CIOCGSESSION, &sess)) {
124 perror("ioctl(CIOCGSESSION)");
125 return 1;
128 #ifdef CIOCGSESSINFO
130 struct session_info_op siop = {
131 .ses = sess.ses,
134 if (ioctl(fd, CIOCGSESSINFO, &siop)) {
135 perror("ioctl(CIOCGSESSINFO)");
136 } else {
137 printf("requested cipher CRYPTO_AES_CBC and mac CRYPTO_SHA1_HMAC,"
138 " got cipher %s with driver %s and hash %s with driver %s\n",
139 siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name,
140 siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
143 #endif
145 for (; datalen <= datalen_end; datalen += BLOCK_SIZE) {
146 if (test_crypto(fd, &sess, datalen)) {
147 printf("test_crypto() failed for datalen of %d\n", datalen);
148 return 1;
152 /* Finish crypto session */
153 if (ioctl(fd, CIOCFSESSION, &sess.ses)) {
154 perror("ioctl(CIOCFSESSION)");
157 close(fd);
158 return 0;