1 /* Copyright (c) 2015, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include <openssl/aes.h>
19 #include <openssl/crypto.h>
20 
21 
TestAES(const uint8_t * key,size_t key_len,const uint8_t plaintext[AES_BLOCK_SIZE],const uint8_t ciphertext[AES_BLOCK_SIZE])22 static bool TestAES(const uint8_t *key, size_t key_len,
23                     const uint8_t plaintext[AES_BLOCK_SIZE],
24                     const uint8_t ciphertext[AES_BLOCK_SIZE]) {
25   AES_KEY aes_key;
26   if (AES_set_encrypt_key(key, key_len * 8, &aes_key) != 0) {
27     fprintf(stderr, "AES_set_encrypt_key failed\n");
28     return false;
29   }
30 
31   // Test encryption.
32   uint8_t block[AES_BLOCK_SIZE];
33   AES_encrypt(plaintext, block, &aes_key);
34   if (memcmp(block, ciphertext, AES_BLOCK_SIZE) != 0) {
35     fprintf(stderr, "AES_encrypt gave the wrong output\n");
36     return false;
37   }
38 
39   // Test in-place encryption.
40   memcpy(block, plaintext, AES_BLOCK_SIZE);
41   AES_encrypt(block, block, &aes_key);
42   if (memcmp(block, ciphertext, AES_BLOCK_SIZE) != 0) {
43     fprintf(stderr, "AES_encrypt gave the wrong output\n");
44     return false;
45   }
46 
47   if (AES_set_decrypt_key(key, key_len * 8, &aes_key) != 0) {
48     fprintf(stderr, "AES_set_decrypt_key failed\n");
49     return false;
50   }
51 
52   // Test decryption.
53   AES_decrypt(ciphertext, block, &aes_key);
54   if (memcmp(block, plaintext, AES_BLOCK_SIZE) != 0) {
55     fprintf(stderr, "AES_decrypt gave the wrong output\n");
56     return false;
57   }
58 
59   // Test in-place decryption.
60   memcpy(block, ciphertext, AES_BLOCK_SIZE);
61   AES_decrypt(block, block, &aes_key);
62   if (memcmp(block, plaintext, AES_BLOCK_SIZE) != 0) {
63     fprintf(stderr, "AES_decrypt gave the wrong output\n");
64     return false;
65   }
66   return true;
67 }
68 
main()69 int main() {
70   CRYPTO_library_init();
71 
72   // Test vectors from FIPS-197, Appendix C.
73   if (!TestAES((const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
74                                 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
75                128 / 8,
76                (const uint8_t *)"\x00\x11\x22\x33\x44\x55\x66\x77"
77                                 "\x88\x99\xaa\xbb\xcc\xdd\xee\xff",
78                (const uint8_t *)"\x69\xc4\xe0\xd8\x6a\x7b\x04\x30"
79                                 "\xd8\xcd\xb7\x80\x70\xb4\xc5\x5a") ||
80       !TestAES((const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
81                                 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
82                                 "\x10\x11\x12\x13\x14\x15\x16\x17",
83                192 / 8,
84                (const uint8_t *)"\x00\x11\x22\x33\x44\x55\x66\x77"
85                                 "\x88\x99\xaa\xbb\xcc\xdd\xee\xff",
86                (const uint8_t *)"\xdd\xa9\x7c\xa4\x86\x4c\xdf\xe0"
87                                 "\x6e\xaf\x70\xa0\xec\x0d\x71\x91") ||
88       !TestAES((const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
89                                 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
90                                 "\x10\x11\x12\x13\x14\x15\x16\x17"
91                                 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
92                256 / 8,
93                (const uint8_t *)"\x00\x11\x22\x33\x44\x55\x66\x77"
94                                 "\x88\x99\xaa\xbb\xcc\xdd\xee\xff",
95                (const uint8_t *)"\x8e\xa2\xb7\xca\x51\x67\x45\xbf"
96                                 "\xea\xfc\x49\x90\x4b\x49\x60\x89")) {
97     return false;
98   }
99 
100   printf("PASS\n");
101   return 0;
102 }
103