2 * Demo on how to use /dev/crypto device for ciphering.
4 * Placed under public domain.
12 #include <sys/ioctl.h>
13 #include <crypto/cryptodev.h>
15 #define DATA_SIZE 8*1024
22 char plaintext_raw
[DATA_SIZE
+ 63], *plaintext
;
23 char ciphertext_raw
[DATA_SIZE
+ 63], *ciphertext
;
27 struct session_op sess
;
30 memset(&sess
, 0, sizeof(sess
));
31 memset(&cryp
, 0, sizeof(cryp
));
33 memset(key
, 0x33, sizeof(key
));
34 memset(iv
, 0x03, sizeof(iv
));
36 /* Get crypto session for AES128 */
37 sess
.cipher
= CRYPTO_AES_CBC
;
38 sess
.keylen
= KEY_SIZE
;
40 if (ioctl(cfd
, CIOCGSESSION
, &sess
)) {
41 perror("ioctl(CIOCGSESSION)");
45 plaintext
= (char *)(((unsigned long)plaintext_raw
+ sess
.alignmask
) & ~sess
.alignmask
);
46 ciphertext
= (char *)(((unsigned long)ciphertext_raw
+ sess
.alignmask
) & ~sess
.alignmask
);
47 memset(plaintext
, 0x15, DATA_SIZE
);
49 /* Encrypt data.in to data.encrypted */
53 cryp
.dst
= ciphertext
;
55 cryp
.op
= COP_ENCRYPT
;
56 if (ioctl(cfd
, CIOCCRYPT
, &cryp
)) {
57 perror("ioctl(CIOCCRYPT)");
61 if (ioctl(cfd
, CIOCFSESSION
, &sess
.ses
)) {
62 perror("ioctl(CIOCFSESSION)");
66 if (ioctl(cfd
, CIOCGSESSION
, &sess
)) {
67 perror("ioctl(CIOCGSESSION)");
71 /* Decrypt data.encrypted to data.decrypted */
74 cryp
.src
= ciphertext
;
75 cryp
.dst
= ciphertext
;
77 cryp
.op
= COP_DECRYPT
;
78 if (ioctl(cfd
, CIOCCRYPT
, &cryp
)) {
79 perror("ioctl(CIOCCRYPT)");
83 /* Verify the result */
84 if (memcmp(plaintext
, ciphertext
, DATA_SIZE
) != 0) {
87 "FAIL: Decrypted data are different from the input data.\n");
89 for (i
= 0; i
< DATA_SIZE
; i
++) {
92 printf("%02x ", plaintext
[i
]);
94 printf("ciphertext:");
95 for (i
= 0; i
< DATA_SIZE
; i
++) {
98 printf("%02x ", ciphertext
[i
]);
103 printf("Test passed\n");
105 /* Finish crypto session */
106 if (ioctl(cfd
, CIOCFSESSION
, &sess
.ses
)) {
107 perror("ioctl(CIOCFSESSION)");
114 static int test_aes(int cfd
)
116 char plaintext1_raw
[BLOCK_SIZE
+ 63], *plaintext1
;
117 char ciphertext1
[BLOCK_SIZE
] = { 0xdf, 0x55, 0x6a, 0x33, 0x43, 0x8d, 0xb8, 0x7b, 0xc4, 0x1b, 0x17, 0x52, 0xc5, 0x5e, 0x5e, 0x49 };
118 char iv1
[BLOCK_SIZE
];
119 char key1
[KEY_SIZE
] = { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
120 char plaintext2_data
[BLOCK_SIZE
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
121 char plaintext2_raw
[BLOCK_SIZE
+ 63], *plaintext2
;
122 char ciphertext2
[BLOCK_SIZE
] = { 0xb7, 0x97, 0x2b, 0x39, 0x41, 0xc4, 0x4b, 0x90, 0xaf, 0xa7, 0xb2, 0x64, 0xbf, 0xba, 0x73, 0x87 };
123 char iv2
[BLOCK_SIZE
];
126 struct session_op sess
;
127 struct crypt_op cryp
;
129 memset(&sess
, 0, sizeof(sess
));
130 memset(&cryp
, 0, sizeof(cryp
));
132 /* Get crypto session for AES128 */
133 sess
.cipher
= CRYPTO_AES_CBC
;
134 sess
.keylen
= KEY_SIZE
;
136 if (ioctl(cfd
, CIOCGSESSION
, &sess
)) {
137 perror("ioctl(CIOCGSESSION)");
141 plaintext1
= (char *)(((unsigned long)plaintext1_raw
+ sess
.alignmask
) & ~sess
.alignmask
);
143 memset(plaintext1
, 0x0, BLOCK_SIZE
);
144 memset(iv1
, 0x0, sizeof(iv1
));
146 /* Encrypt data.in to data.encrypted */
148 cryp
.len
= BLOCK_SIZE
;
149 cryp
.src
= plaintext1
;
150 cryp
.dst
= plaintext1
;
152 cryp
.op
= COP_ENCRYPT
;
153 if (ioctl(cfd
, CIOCCRYPT
, &cryp
)) {
154 perror("ioctl(CIOCCRYPT)");
158 /* Verify the result */
159 if (memcmp(plaintext1
, ciphertext1
, BLOCK_SIZE
) != 0) {
161 "FAIL: Decrypted data are different from the input data.\n");
167 memset(key2
, 0x0, sizeof(key2
));
168 memset(iv2
, 0x0, sizeof(iv2
));
170 /* Get crypto session for AES128 */
171 sess
.cipher
= CRYPTO_AES_CBC
;
172 sess
.keylen
= KEY_SIZE
;
174 if (ioctl(cfd
, CIOCGSESSION
, &sess
)) {
175 perror("ioctl(CIOCGSESSION)");
179 plaintext2
= (char *)(((unsigned long)plaintext2_raw
+ sess
.alignmask
) & ~sess
.alignmask
);
180 memcpy(plaintext2
, plaintext2_data
, BLOCK_SIZE
);
182 /* Encrypt data.in to data.encrypted */
184 cryp
.len
= BLOCK_SIZE
;
185 cryp
.src
= plaintext2
;
186 cryp
.dst
= plaintext2
;
188 cryp
.op
= COP_ENCRYPT
;
189 if (ioctl(cfd
, CIOCCRYPT
, &cryp
)) {
190 perror("ioctl(CIOCCRYPT)");
194 /* Verify the result */
195 if (memcmp(plaintext2
, ciphertext2
, BLOCK_SIZE
) != 0) {
198 "FAIL: Decrypted data are different from the input data.\n");
199 printf("plaintext:");
200 for (i
= 0; i
< BLOCK_SIZE
; i
++) {
203 printf("%02x ", plaintext2
[i
]);
205 printf("ciphertext:");
206 for (i
= 0; i
< BLOCK_SIZE
; i
++) {
209 printf("%02x ", ciphertext2
[i
]);
215 printf("AES Test passed\n");
217 /* Finish crypto session */
218 if (ioctl(cfd
, CIOCFSESSION
, &sess
.ses
)) {
219 perror("ioctl(CIOCFSESSION)");
229 int fd
= -1, cfd
= -1;
231 /* Open the crypto device */
232 fd
= open("/dev/crypto", O_RDWR
, 0);
234 perror("open(/dev/crypto)");
238 /* Clone file descriptor */
239 if (ioctl(fd
, CRIOGET
, &cfd
)) {
240 perror("ioctl(CRIOGET)");
244 /* Set close-on-exec (not really neede here) */
245 if (fcntl(cfd
, F_SETFD
, 1) == -1) {
246 perror("fcntl(F_SETFD)");
250 /* Run the test itself */
254 if (test_crypto(cfd
))
257 /* Close cloned descriptor */
259 perror("close(cfd)");
263 /* Close the original descriptor */