Version is shown on module load.
[cryptodev-linux.git] / examples / cipher.c
blobd7982ae580e88fb7df07e66dd900f021a28cc3c4
1 /*
2 * Demo on how to use /dev/crypto device for ciphering.
4 * Placed under public domain.
6 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <fcntl.h>
12 #include <sys/ioctl.h>
13 #include <crypto/cryptodev.h>
15 #define DATA_SIZE 4096
16 #define BLOCK_SIZE 16
17 #define KEY_SIZE 16
19 static int
20 test_crypto(int cfd)
22 char plaintext[DATA_SIZE];
23 char ciphertext[DATA_SIZE];
24 char iv[BLOCK_SIZE];
25 char key[KEY_SIZE];
27 struct session_op sess;
28 struct crypt_op cryp;
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;
40 sess.key = key;
41 if (ioctl(cfd, CIOCGSESSION, &sess)) {
42 perror("ioctl(CIOCGSESSION)");
43 return 1;
46 /* Encrypt data.in to data.encrypted */
47 cryp.ses = sess.ses;
48 cryp.len = sizeof(plaintext);
49 cryp.src = plaintext;
50 cryp.dst = ciphertext;
51 cryp.iv = iv;
52 cryp.op = COP_ENCRYPT;
53 if (ioctl(cfd, CIOCCRYPT, &cryp)) {
54 perror("ioctl(CIOCCRYPT)");
55 return 1;
58 if (ioctl(cfd, CIOCGSESSION, &sess)) {
59 perror("ioctl(CIOCGSESSION)");
60 return 1;
63 /* Decrypt data.encrypted to data.decrypted */
64 cryp.ses = sess.ses;
65 cryp.len = sizeof(plaintext);
66 cryp.src = ciphertext;
67 cryp.dst = ciphertext;
68 cryp.iv = iv;
69 cryp.op = COP_DECRYPT;
70 if (ioctl(cfd, CIOCCRYPT, &cryp)) {
71 perror("ioctl(CIOCCRYPT)");
72 return 1;
75 /* Verify the result */
76 if (memcmp(plaintext, ciphertext, sizeof(plaintext)) != 0) {
77 fprintf(stderr,
78 "FAIL: Decrypted data are different from the input data.\n");
79 return 1;
80 } else
81 printf("Test passed\n");
83 /* Finish crypto session */
84 if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
85 perror("ioctl(CIOCFSESSION)");
86 return 1;
89 return 0;
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 };
96 char iv1[BLOCK_SIZE];
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];
101 char key2[KEY_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;
115 sess.key = key1;
116 if (ioctl(cfd, CIOCGSESSION, &sess)) {
117 perror("ioctl(CIOCGSESSION)");
118 return 1;
121 /* Encrypt data.in to data.encrypted */
122 cryp.ses = sess.ses;
123 cryp.len = sizeof(plaintext1);
124 cryp.src = plaintext1;
125 cryp.dst = plaintext1;
126 cryp.iv = iv1;
127 cryp.op = COP_ENCRYPT;
128 if (ioctl(cfd, CIOCCRYPT, &cryp)) {
129 perror("ioctl(CIOCCRYPT)");
130 return 1;
133 /* Verify the result */
134 if (memcmp(plaintext1, ciphertext1, sizeof(plaintext1)) != 0) {
135 fprintf(stderr,
136 "FAIL: Decrypted data are different from the input data.\n");
137 return 1;
140 /* Test 2 */
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;
148 sess.key = key2;
149 if (ioctl(cfd, CIOCGSESSION, &sess)) {
150 perror("ioctl(CIOCGSESSION)");
151 return 1;
154 /* Encrypt data.in to data.encrypted */
155 cryp.ses = sess.ses;
156 cryp.len = sizeof(plaintext2);
157 cryp.src = plaintext2;
158 cryp.dst = plaintext2;
159 cryp.iv = iv2;
160 cryp.op = COP_ENCRYPT;
161 if (ioctl(cfd, CIOCCRYPT, &cryp)) {
162 perror("ioctl(CIOCCRYPT)");
163 return 1;
166 /* Verify the result */
167 if (memcmp(plaintext2, ciphertext2, sizeof(plaintext2)) != 0) {
168 fprintf(stderr,
169 "FAIL: Decrypted data are different from the input data.\n");
170 return 1;
173 printf("AES Test passed\n");
175 /* Finish crypto session */
176 if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
177 perror("ioctl(CIOCFSESSION)");
178 return 1;
181 return 0;
185 main()
187 int fd = -1, cfd = -1;
189 /* Open the crypto device */
190 fd = open("/dev/crypto", O_RDWR, 0);
191 if (fd < 0) {
192 perror("open(/dev/crypto)");
193 return 1;
196 /* Clone file descriptor */
197 if (ioctl(fd, CRIOGET, &cfd)) {
198 perror("ioctl(CRIOGET)");
199 return 1;
202 /* Set close-on-exec (not really neede here) */
203 if (fcntl(cfd, F_SETFD, 1) == -1) {
204 perror("fcntl(F_SETFD)");
205 return 1;
208 /* Run the test itself */
209 if (test_aes(cfd))
210 return 1;
212 if (test_crypto(cfd))
213 return 1;
215 /* Close cloned descriptor */
216 if (close(cfd)) {
217 perror("close(cfd)");
218 return 1;
221 /* Close the original descriptor */
222 if (close(fd)) {
223 perror("close(fd)");
224 return 1;
227 return 0;