2 * Compare digest results with ones from openssl.
4 * Placed under public domain.
14 #include <sys/ioctl.h>
15 #include <crypto/cryptodev.h>
17 #include "openssl_wrapper.h"
20 #define MAX_DATALEN (64 * 1024)
22 static void printhex(unsigned char *buf
, int buflen
)
24 while (buflen
-- > 0) {
25 printf("\\x%.2x", *(buf
++));
31 test_crypto(int cfd
, struct session_op
*sess
, int datalen
)
35 unsigned char mac
[AALG_MAX_RESULT_LEN
];
37 unsigned char mac_comp
[AALG_MAX_RESULT_LEN
];
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 */
56 cryp
.op
= COP_ENCRYPT
;
57 if ((ret
= ioctl(cfd
, CIOCCRYPT
, &cryp
))) {
58 perror("ioctl(CIOCCRYPT)");
64 if ((ret
= openssl_cioccrypt(sess
, &cryp
))) {
65 fprintf(stderr
, "openssl_cioccrypt() failed!\n");
69 if (memcmp(mac
, mac_comp
, AALG_MAX_RESULT_LEN
)) {
70 printf("fail for datalen %d, MACs do not match!\n", datalen
);
72 printf("wrong mac: ");
74 printf("right mac: ");
75 printhex(mac_comp
, 20);
83 #define max(a, b) ((a) > (b) ? (a) : (b))
84 #define min(a, b) ((a) < (b) ? (a) : (b))
87 main(int argc
, char **argv
)
90 struct session_op sess
;
91 int datalen
= BLOCK_SIZE
;
92 int datalen_end
= MAX_DATALEN
;
96 datalen
= min(max(atoi(argv
[1]), BLOCK_SIZE
), MAX_DATALEN
);
97 datalen_end
= datalen
;
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);
108 perror("open(/dev/crypto)");
112 memset(&sess
, 0, sizeof(sess
));
115 sess
.mac
= CRYPTO_SHA1
;
116 if (ioctl(fd
, CIOCGSESSION
, &sess
)) {
117 perror("ioctl(CIOCGSESSION)");
123 struct session_info_op siop
= {
127 if (ioctl(fd
, CIOCGSESSINFO
, &siop
)) {
128 perror("ioctl(CIOCGSESSINFO)");
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
);
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
);
143 /* Finish crypto session */
144 if (ioctl(fd
, CIOCFSESSION
, &sess
.ses
)) {
145 perror("ioctl(CIOCFSESSION)");