OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * aes_calc.c |
| 3 * |
| 4 * A simple AES calculator for generating AES encryption values |
| 5 * |
| 6 * David A. McGrew |
| 7 * Cisco Systems, Inc. |
| 8 */ |
| 9 |
| 10 /* |
| 11 |
| 12 Example usage (with first NIST FIPS 197 test case): |
| 13 |
| 14 [sh]$ test/aes_calc 000102030405060708090a0b0c0d0e0f 00112233445566778899aabbccd
deeff -v |
| 15 plaintext: 00112233445566778899aabbccddeeff |
| 16 key: 000102030405060708090a0b0c0d0e0f |
| 17 ciphertext: 69c4e0d86a7b0430d8cdb78070b4c55a |
| 18 |
| 19 */ |
| 20 |
| 21 #include "aes.h" |
| 22 #include <stdio.h> |
| 23 #include <string.h> |
| 24 |
| 25 void |
| 26 usage(char *prog_name) { |
| 27 printf("usage: %s <key> <plaintext> [-v]\n", prog_name); |
| 28 exit(255); |
| 29 } |
| 30 |
| 31 #define AES_MAX_KEY_LEN 32 |
| 32 |
| 33 int |
| 34 main (int argc, char *argv[]) { |
| 35 v128_t data; |
| 36 uint8_t key[AES_MAX_KEY_LEN]; |
| 37 aes_expanded_key_t exp_key; |
| 38 int key_len, len; |
| 39 int verbose; |
| 40 err_status_t status; |
| 41 |
| 42 if (argc == 3) { |
| 43 /* we're not in verbose mode */ |
| 44 verbose = 0; |
| 45 } else if (argc == 4) { |
| 46 if (strncmp(argv[3], "-v", 2) == 0) { |
| 47 /* we're in verbose mode */ |
| 48 verbose = 1; |
| 49 } else { |
| 50 /* unrecognized flag, complain and exit */ |
| 51 usage(argv[0]); |
| 52 } |
| 53 } else { |
| 54 /* we've been fed the wrong number of arguments - compain and exit */ |
| 55 usage(argv[0]); |
| 56 } |
| 57 |
| 58 /* read in key, checking length */ |
| 59 if (strlen(argv[1]) > AES_MAX_KEY_LEN*2) { |
| 60 fprintf(stderr, |
| 61 "error: too many digits in key " |
| 62 "(should be at most %d hexadecimal digits, found %u)\n", |
| 63 AES_MAX_KEY_LEN*2, (unsigned)strlen(argv[1])); |
| 64 exit(1); |
| 65 } |
| 66 len = hex_string_to_octet_string((char*)key, argv[1], AES_MAX_KEY_LEN*2); |
| 67 /* check that hex string is the right length */ |
| 68 if (len != 32 && len != 48 && len != 64) { |
| 69 fprintf(stderr, |
| 70 "error: bad number of digits in key " |
| 71 "(should be 32/48/64 hexadecimal digits, found %d)\n", |
| 72 len); |
| 73 exit(1); |
| 74 } |
| 75 key_len = len/2; |
| 76 |
| 77 /* read in plaintext, checking length */ |
| 78 if (strlen(argv[2]) > 16*2) { |
| 79 fprintf(stderr, |
| 80 "error: too many digits in plaintext " |
| 81 "(should be %d hexadecimal digits, found %u)\n", |
| 82 16*2, (unsigned)strlen(argv[2])); |
| 83 exit(1); |
| 84 } |
| 85 len = hex_string_to_octet_string((char *)(&data), argv[2], 16*2); |
| 86 /* check that hex string is the right length */ |
| 87 if (len < 16*2) { |
| 88 fprintf(stderr, |
| 89 "error: too few digits in plaintext " |
| 90 "(should be %d hexadecimal digits, found %d)\n", |
| 91 16*2, len); |
| 92 exit(1); |
| 93 } |
| 94 |
| 95 if (verbose) { |
| 96 /* print out plaintext */ |
| 97 printf("plaintext:\t%s\n", octet_string_hex_string((uint8_t *)&data, 16)); |
| 98 } |
| 99 |
| 100 /* encrypt plaintext */ |
| 101 status = aes_expand_encryption_key(key, key_len, &exp_key); |
| 102 if (status) { |
| 103 fprintf(stderr, |
| 104 "error: AES key expansion failed.\n"); |
| 105 exit(1); |
| 106 } |
| 107 |
| 108 aes_encrypt(&data, &exp_key); |
| 109 |
| 110 /* write ciphertext to output */ |
| 111 if (verbose) { |
| 112 printf("key:\t\t%s\n", octet_string_hex_string(key, key_len)); |
| 113 printf("ciphertext:\t"); |
| 114 } |
| 115 printf("%s\n", v128_hex_string(&data)); |
| 116 |
| 117 return 0; |
| 118 } |
| 119 |
OLD | NEW |