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 4096
22 char plaintext
[DATA_SIZE
];
23 char ciphertext
[DATA_SIZE
];
27 struct session_op sess
;
30 memset(&sess
, 0, sizeof(sess
));
31 memset(&cryp
, 0, sizeof(cryp
));
33 memset(plaintext
, 0x15, sizeof(plaintext
));
34 memset(key
, 0x33, sizeof(key
));
35 memset(iv
, 0x03, sizeof(iv
));
37 /* Get crypto session for AES128 */
38 sess
.cipher
= CRYPTO_AES_CBC
;
39 sess
.keylen
= KEY_SIZE
;
41 if (ioctl(cfd
, CIOCGSESSION
, &sess
)) {
42 perror("ioctl(CIOCGSESSION)");
46 /* Encrypt data.in to data.encrypted */
48 cryp
.len
= sizeof(plaintext
);
50 cryp
.dst
= ciphertext
;
52 cryp
.op
= COP_ENCRYPT
;
53 if (ioctl(cfd
, CIOCCRYPT
, &cryp
)) {
54 perror("ioctl(CIOCCRYPT)");
58 if (ioctl(cfd
, CIOCGSESSION
, &sess
)) {
59 perror("ioctl(CIOCGSESSION)");
63 /* Decrypt data.encrypted to data.decrypted */
65 cryp
.len
= sizeof(plaintext
);
66 cryp
.src
= ciphertext
;
67 cryp
.dst
= ciphertext
;
69 cryp
.op
= COP_DECRYPT
;
70 if (ioctl(cfd
, CIOCCRYPT
, &cryp
)) {
71 perror("ioctl(CIOCCRYPT)");
75 /* Verify the result */
76 if (memcmp(plaintext
, ciphertext
, sizeof(plaintext
)) != 0) {
78 "FAIL: Decrypted data are different from the input data.\n");
81 printf("Test passed\n");
83 /* Finish crypto session */
84 if (ioctl(cfd
, CIOCFSESSION
, &sess
.ses
)) {
85 perror("ioctl(CIOCFSESSION)");
92 static int test_aes(int cfd
)
94 char plaintext1
[BLOCK_SIZE
];
95 char ciphertext1
[BLOCK_SIZE
] = { 0xdf, 0x55, 0x6a, 0x33, 0x43, 0x8d, 0xb8, 0x7b, 0xc4, 0x1b, 0x17, 0x52, 0xc5, 0x5e, 0x5e, 0x49 };
97 char key1
[KEY_SIZE
] = { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
98 char plaintext2
[BLOCK_SIZE
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
99 char ciphertext2
[BLOCK_SIZE
] = { 0xb7, 0x97, 0x2b, 0x39, 0x41, 0xc4, 0x4b, 0x90, 0xaf, 0xa7, 0xb2, 0x64, 0xbf, 0xba, 0x73, 0x87 };
100 char iv2
[BLOCK_SIZE
];
103 struct session_op sess
;
104 struct crypt_op cryp
;
106 memset(&sess
, 0, sizeof(sess
));
107 memset(&cryp
, 0, sizeof(cryp
));
109 memset(plaintext1
, 0x0, sizeof(plaintext1
));
110 memset(iv1
, 0x0, sizeof(iv1
));
112 /* Get crypto session for AES128 */
113 sess
.cipher
= CRYPTO_AES_CBC
;
114 sess
.keylen
= KEY_SIZE
;
116 if (ioctl(cfd
, CIOCGSESSION
, &sess
)) {
117 perror("ioctl(CIOCGSESSION)");
121 /* Encrypt data.in to data.encrypted */
123 cryp
.len
= sizeof(plaintext1
);
124 cryp
.src
= plaintext1
;
125 cryp
.dst
= plaintext1
;
127 cryp
.op
= COP_ENCRYPT
;
128 if (ioctl(cfd
, CIOCCRYPT
, &cryp
)) {
129 perror("ioctl(CIOCCRYPT)");
133 /* Verify the result */
134 if (memcmp(plaintext1
, ciphertext1
, sizeof(plaintext1
)) != 0) {
136 "FAIL: Decrypted data are different from the input data.\n");
142 memset(key2
, 0x0, sizeof(key2
));
143 memset(iv2
, 0x0, sizeof(iv2
));
145 /* Get crypto session for AES128 */
146 sess
.cipher
= CRYPTO_AES_CBC
;
147 sess
.keylen
= KEY_SIZE
;
149 if (ioctl(cfd
, CIOCGSESSION
, &sess
)) {
150 perror("ioctl(CIOCGSESSION)");
154 /* Encrypt data.in to data.encrypted */
156 cryp
.len
= sizeof(plaintext2
);
157 cryp
.src
= plaintext2
;
158 cryp
.dst
= plaintext2
;
160 cryp
.op
= COP_ENCRYPT
;
161 if (ioctl(cfd
, CIOCCRYPT
, &cryp
)) {
162 perror("ioctl(CIOCCRYPT)");
166 /* Verify the result */
167 if (memcmp(plaintext2
, ciphertext2
, sizeof(plaintext2
)) != 0) {
169 "FAIL: Decrypted data are different from the input data.\n");
173 printf("AES Test passed\n");
175 /* Finish crypto session */
176 if (ioctl(cfd
, CIOCFSESSION
, &sess
.ses
)) {
177 perror("ioctl(CIOCFSESSION)");
187 int fd
= -1, cfd
= -1;
189 /* Open the crypto device */
190 fd
= open("/dev/crypto", O_RDWR
, 0);
192 perror("open(/dev/crypto)");
196 /* Clone file descriptor */
197 if (ioctl(fd
, CRIOGET
, &cfd
)) {
198 perror("ioctl(CRIOGET)");
202 /* Set close-on-exec (not really neede here) */
203 if (fcntl(cfd
, F_SETFD
, 1) == -1) {
204 perror("fcntl(F_SETFD)");
208 /* Run the test itself */
212 if (test_crypto(cfd
))
215 /* Close cloned descriptor */
217 perror("close(cfd)");
221 /* Close the original descriptor */