Added real usage examples, and moved test code to tests/
[cryptodev-linux.git] / examples / aes.c
blob0f154d8fc97f427d7a7b0a16c9622f72b50cbf8c
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>
11 #include <sys/ioctl.h>
12 #include <crypto/cryptodev.h>
13 #include "aes.h"
15 #define KEY_SIZE 16
18 int aes_ctx_init(struct cryptodev_ctx* ctx, int cfd, const uint8_t *key, unsigned int key_size)
20 #ifdef CIOCGSESSINFO
21 struct session_info_op siop;
22 #endif
24 memset(ctx, 0, sizeof(*ctx));
25 ctx->cfd = cfd;
27 ctx->sess.cipher = CRYPTO_AES_CBC;
28 ctx->sess.keylen = key_size;
29 ctx->sess.key = (void*)key;
30 if (ioctl(ctx->cfd, CIOCGSESSION, &ctx->sess)) {
31 perror("ioctl(CIOCGSESSION)");
32 return -1;
35 #ifdef CIOCGSESSINFO
36 siop.ses = ctx->sess.ses;
37 if (ioctl(ctx->cfd, CIOCGSESSINFO, &siop)) {
38 perror("ioctl(CIOCGSESSINFO)");
39 return -1;
41 printf("requested cipher CRYPTO_AES_CBC, got %s with driver %s\n",
42 siop.cipher_info.cra_name, siop.cipher_info.cra_driver_name);
43 /*printf("Alignmask is %x\n", (unsigned int)siop.alignmask); */
44 ctx->alignmask = siop.alignmask;
45 #endif
46 return 0;
49 void aes_ctx_deinit(struct cryptodev_ctx* ctx)
51 if (ioctl(ctx->cfd, CIOCFSESSION, &ctx->sess.ses)) {
52 perror("ioctl(CIOCFSESSION)");
56 int
57 aes_encrypt(struct cryptodev_ctx* ctx, const void* iv, const void* plaintext, void* ciphertext, size_t size)
59 struct crypt_op cryp;
60 void* p;
62 /* check plaintext and ciphertext alignment */
63 if (ctx->alignmask) {
64 p = (void*)(((unsigned long)plaintext + ctx->alignmask) & ~ctx->alignmask);
65 if (plaintext != p) {
66 fprintf(stderr, "plaintext is not aligned\n");
67 return -1;
70 p = (void*)(((unsigned long)ciphertext + ctx->alignmask) & ~ctx->alignmask);
71 if (ciphertext != p) {
72 fprintf(stderr, "ciphertext is not aligned\n");
73 return -1;
77 memset(&cryp, 0, sizeof(cryp));
79 /* Encrypt data.in to data.encrypted */
80 cryp.ses = ctx->sess.ses;
81 cryp.len = size;
82 cryp.src = (void*)plaintext;
83 cryp.dst = ciphertext;
84 cryp.iv = (void*)iv;
85 cryp.op = COP_ENCRYPT;
86 if (ioctl(ctx->cfd, CIOCCRYPT, &cryp)) {
87 perror("ioctl(CIOCCRYPT)");
88 return -1;
91 return 0;
94 int
95 aes_decrypt(struct cryptodev_ctx* ctx, const void* iv, const void* ciphertext, void* plaintext, size_t size)
97 struct crypt_op cryp;
98 void* p;
100 /* check plaintext and ciphertext alignment */
101 if (ctx->alignmask) {
102 p = (void*)(((unsigned long)plaintext + ctx->alignmask) & ~ctx->alignmask);
103 if (plaintext != p) {
104 fprintf(stderr, "plaintext is not aligned\n");
105 return -1;
108 p = (void*)(((unsigned long)ciphertext + ctx->alignmask) & ~ctx->alignmask);
109 if (ciphertext != p) {
110 fprintf(stderr, "ciphertext is not aligned\n");
111 return -1;
115 memset(&cryp, 0, sizeof(cryp));
117 /* Encrypt data.in to data.encrypted */
118 cryp.ses = ctx->sess.ses;
119 cryp.len = size;
120 cryp.src = (void*)ciphertext;
121 cryp.dst = plaintext;
122 cryp.iv = (void*)iv;
123 cryp.op = COP_DECRYPT;
124 if (ioctl(ctx->cfd, CIOCCRYPT, &cryp)) {
125 perror("ioctl(CIOCCRYPT)");
126 return -1;
129 return 0;
132 static int test_aes(int cfd)
134 char plaintext1_raw[AES_BLOCK_SIZE + 63], *plaintext1;
135 char ciphertext1[AES_BLOCK_SIZE] = { 0xdf, 0x55, 0x6a, 0x33, 0x43, 0x8d, 0xb8, 0x7b, 0xc4, 0x1b, 0x17, 0x52, 0xc5, 0x5e, 0x5e, 0x49 };
136 char iv1[AES_BLOCK_SIZE];
137 uint8_t key1[KEY_SIZE] = { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
138 char plaintext2_data[AES_BLOCK_SIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
139 char plaintext2_raw[AES_BLOCK_SIZE + 63], *plaintext2;
140 char ciphertext2[AES_BLOCK_SIZE] = { 0xb7, 0x97, 0x2b, 0x39, 0x41, 0xc4, 0x4b, 0x90, 0xaf, 0xa7, 0xb2, 0x64, 0xbf, 0xba, 0x73, 0x87 };
141 char iv2[AES_BLOCK_SIZE];
142 uint8_t key2[KEY_SIZE];
143 struct cryptodev_ctx ctx;
145 aes_ctx_init(&ctx, cfd, key1, sizeof(key1));
147 if (ctx.alignmask)
148 plaintext1 = (char *)(((unsigned long)plaintext1_raw + ctx.alignmask) & ~ctx.alignmask);
149 else
150 plaintext1 = plaintext1_raw;
152 memset(plaintext1, 0x0, AES_BLOCK_SIZE);
153 memset(iv1, 0x0, sizeof(iv1));
155 aes_encrypt(&ctx, iv1, plaintext1, plaintext1, AES_BLOCK_SIZE);
157 /* Verify the result */
158 if (memcmp(plaintext1, ciphertext1, AES_BLOCK_SIZE) != 0) {
159 fprintf(stderr,
160 "FAIL: Decrypted data are different from the input data.\n");
161 return -1;
164 aes_ctx_deinit(&ctx);
166 /* Test 2 */
168 memset(key2, 0x0, sizeof(key2));
169 memset(iv2, 0x0, sizeof(iv2));
171 aes_ctx_init(&ctx, cfd, key2, sizeof(key2));
173 if (ctx.alignmask) {
174 plaintext2 = (char *)(((unsigned long)plaintext2_raw + ctx.alignmask) & ~ctx.alignmask);
175 } else {
176 plaintext2 = plaintext2_raw;
178 memcpy(plaintext2, plaintext2_data, AES_BLOCK_SIZE);
180 /* Encrypt data.in to data.encrypted */
181 aes_encrypt(&ctx, iv2, plaintext2, plaintext2, AES_BLOCK_SIZE);
183 /* Verify the result */
184 if (memcmp(plaintext2, ciphertext2, AES_BLOCK_SIZE) != 0) {
185 int i;
186 fprintf(stderr,
187 "FAIL: Decrypted data are different from the input data.\n");
188 printf("plaintext:");
189 for (i = 0; i < AES_BLOCK_SIZE; i++) {
190 printf("%02x ", plaintext2[i]);
192 printf("ciphertext:");
193 for (i = 0; i < AES_BLOCK_SIZE; i++) {
194 printf("%02x ", ciphertext2[i]);
196 printf("\n");
197 return 1;
200 aes_ctx_deinit(&ctx);
202 printf("AES Test passed\n");
204 return 0;
208 main()
210 int cfd = -1;
212 /* Open the crypto device */
213 cfd = open("/dev/crypto", O_RDWR, 0);
214 if (cfd < 0) {
215 perror("open(/dev/crypto)");
216 return 1;
219 /* Set close-on-exec (not really neede here) */
220 if (fcntl(cfd, F_SETFD, 1) == -1) {
221 perror("fcntl(F_SETFD)");
222 return 1;
225 /* Run the test itself */
226 if (test_aes(cfd))
227 return 1;
229 /* Close the original descriptor */
230 if (close(cfd)) {
231 perror("close(cfd)");
232 return 1;
235 return 0;