2 * Compare encryption 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"
21 #define MAX_DATALEN (64 * 1024)
25 test_crypto(int cfd
, struct session_op
*sess
, int datalen
)
27 char *data
, *encrypted
;
30 char iv_in
[BLOCK_SIZE
];
32 char iv_comp
[BLOCK_SIZE
];
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 */
57 cryp
.op
= COP_ENCRYPT
;
58 cryp
.flags
= COP_FLAG_WRITE_IV
;
59 if ((ret
= ioctl(cfd
, CIOCCRYPT
, &cryp
))) {
60 perror("ioctl(CIOCCRYPT)");
64 cryp
.dst
= encrypted_comp
;
67 if ((ret
= openssl_cioccrypt(sess
, &cryp
))) {
68 fprintf(stderr
, "openssl_cioccrypt() failed!\n");
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
);
85 #define max(a, b) ((a) > (b) ? (a) : (b))
86 #define min(a, b) ((a) < (b) ? (a) : (b))
89 main(int argc
, char **argv
)
92 struct session_op sess
;
93 unsigned char key
[KEY_SIZE
];
94 int datalen
= BLOCK_SIZE
;
95 int datalen_end
= MAX_DATALEN
;
99 datalen
= min(max(atoi(argv
[1]), BLOCK_SIZE
), MAX_DATALEN
);
100 datalen_end
= datalen
;
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);
111 perror("open(/dev/crypto)");
115 for (i
= 0; i
< KEY_SIZE
; i
++)
117 memset(&sess
, 0, sizeof(sess
));
119 /* encryption test */
120 sess
.cipher
= CRYPTO_AES_CBC
;
121 sess
.keylen
= KEY_SIZE
;
123 if (ioctl(fd
, CIOCGSESSION
, &sess
)) {
124 perror("ioctl(CIOCGSESSION)");
130 struct session_info_op siop
= {
134 if (ioctl(fd
, CIOCGSESSINFO
, &siop
)) {
135 perror("ioctl(CIOCGSESSINFO)");
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
);
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
);
152 /* Finish crypto session */
153 if (ioctl(fd
, CIOCFSESSION
, &sess
.ses
)) {
154 perror("ioctl(CIOCFSESSION)");