4
* A simple AES calculator for generating AES encryption values
12
Example usage (with first NIST FIPS 197 test case):
14
[sh]$ test/aes_calc 000102030405060708090a0b0c0d0e0f 00112233445566778899aabbccddeeff -v
15
plaintext: 00112233445566778899aabbccddeeff
16
key: 000102030405060708090a0b0c0d0e0f
17
ciphertext: 69c4e0d86a7b0430d8cdb78070b4c55a
26
usage(char *prog_name) {
27
printf("usage: %s <key> <plaintext> [-v]\n", prog_name);
31
#define AES_KEY_LEN 16
34
main (int argc, char *argv[]) {
36
aes_expanded_key_t exp_key;
41
/* we're not in verbose mode */
43
} else if (argc == 4) {
44
if (strncmp(argv[3], "-v", 2) == 0) {
45
/* we're in verbose mode */
48
/* unrecognized flag, complain and exit */
52
/* we've been fed the wrong number of arguments - compain and exit */
56
/* read in key, checking length */
57
if (strlen(argv[1]) > AES_KEY_LEN*2) {
59
"error: too many digits in key "
60
"(should be %d hexadecimal digits, found %u)\n",
61
AES_KEY_LEN*2, (unsigned)strlen(argv[1]));
64
len = hex_string_to_octet_string((char *)&key, argv[1], AES_KEY_LEN*2);
65
/* check that hex string is the right length */
66
if (len < AES_KEY_LEN*2) {
68
"error: too few digits in key "
69
"(should be %d hexadecimal digits, found %d)\n",
74
/* read in plaintext, checking length */
75
if (strlen(argv[2]) > 16*2) {
77
"error: too many digits in plaintext "
78
"(should be %d hexadecimal digits, found %u)\n",
79
16*2, (unsigned)strlen(argv[2]));
82
len = hex_string_to_octet_string((char *)(&data), argv[2], 16*2);
83
/* check that hex string is the right length */
86
"error: too few digits in plaintext "
87
"(should be %d hexadecimal digits, found %d)\n",
93
/* print out plaintext */
94
printf("plaintext:\t%s\n", octet_string_hex_string((uint8_t *)&data, 16));
97
/* encrypt plaintext */
98
aes_expand_encryption_key(&key, exp_key);
100
aes_encrypt(&data, exp_key);
102
/* write ciphertext to output */
104
printf("key:\t\t%s\n", v128_hex_string(&key));
105
printf("ciphertext:\t");
107
printf("%s\n", v128_hex_string(&data));