| 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 * Copyright (c) 2001-2006, Cisco Systems, Inc. | |
| 13 * All rights reserved. | |
| 14 * | |
| 15 * Redistribution and use in source and binary forms, with or without | |
| 16 * modification, are permitted provided that the following conditions | |
| 17 * are met: | |
| 18 * | |
| 19 * Redistributions of source code must retain the above copyright | |
| 20 * notice, this list of conditions and the following disclaimer. | |
| 21 * | |
| 22 * Redistributions in binary form must reproduce the above | |
| 23 * copyright notice, this list of conditions and the following | |
| 24 * disclaimer in the documentation and/or other materials provided | |
| 25 * with the distribution. | |
| 26 * | |
| 27 * Neither the name of the Cisco Systems, Inc. nor the names of its | |
| 28 * contributors may be used to endorse or promote products derived | |
| 29 * from this software without specific prior written permission. | |
| 30 * | |
| 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 35 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 42 * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 43 * | |
| 44 */ | |
| 45 | |
| 46 /* | |
| 47 | |
| 48 Example usage (with first NIST FIPS 197 test case): | |
| 49 | |
| 50 [sh]$ test/aes_calc 000102030405060708090a0b0c0d0e0f 00112233445566778899aabbccd
deeff -v | |
| 51 plaintext: 00112233445566778899aabbccddeeff | |
| 52 key: 000102030405060708090a0b0c0d0e0f | |
| 53 ciphertext: 69c4e0d86a7b0430d8cdb78070b4c55a | |
| 54 | |
| 55 */ | |
| 56 | |
| 57 #ifdef HAVE_CONFIG_H | |
| 58 #include <config.h> | |
| 59 #endif | |
| 60 | |
| 61 #include "aes.h" | |
| 62 #include <stdio.h> | |
| 63 #include <string.h> | |
| 64 | |
| 65 void | |
| 66 usage(char *prog_name) { | |
| 67 printf("usage: %s <key> <plaintext> [-v]\n", prog_name); | |
| 68 exit(255); | |
| 69 } | |
| 70 | |
| 71 #define AES_MAX_KEY_LEN 32 | |
| 72 | |
| 73 int | |
| 74 main (int argc, char *argv[]) { | |
| 75 v128_t data; | |
| 76 uint8_t key[AES_MAX_KEY_LEN]; | |
| 77 aes_expanded_key_t exp_key; | |
| 78 int key_len, len; | |
| 79 int verbose = 0; | |
| 80 err_status_t status; | |
| 81 | |
| 82 if (argc == 3) { | |
| 83 /* we're not in verbose mode */ | |
| 84 verbose = 0; | |
| 85 } else if (argc == 4) { | |
| 86 if (strncmp(argv[3], "-v", 2) == 0) { | |
| 87 /* we're in verbose mode */ | |
| 88 verbose = 1; | |
| 89 } else { | |
| 90 /* unrecognized flag, complain and exit */ | |
| 91 usage(argv[0]); | |
| 92 } | |
| 93 } else { | |
| 94 /* we've been fed the wrong number of arguments - compain and exit */ | |
| 95 usage(argv[0]); | |
| 96 } | |
| 97 | |
| 98 /* read in key, checking length */ | |
| 99 if (strlen(argv[1]) > AES_MAX_KEY_LEN*2) { | |
| 100 fprintf(stderr, | |
| 101 "error: too many digits in key " | |
| 102 "(should be at most %d hexadecimal digits, found %u)\n", | |
| 103 AES_MAX_KEY_LEN*2, (unsigned)strlen(argv[1])); | |
| 104 exit(1); | |
| 105 } | |
| 106 len = hex_string_to_octet_string((char*)key, argv[1], AES_MAX_KEY_LEN*2); | |
| 107 /* check that hex string is the right length */ | |
| 108 if (len != 32 && len != 48 && len != 64) { | |
| 109 fprintf(stderr, | |
| 110 "error: bad number of digits in key " | |
| 111 "(should be 32/48/64 hexadecimal digits, found %d)\n", | |
| 112 len); | |
| 113 exit(1); | |
| 114 } | |
| 115 key_len = len/2; | |
| 116 | |
| 117 /* read in plaintext, checking length */ | |
| 118 if (strlen(argv[2]) > 16*2) { | |
| 119 fprintf(stderr, | |
| 120 "error: too many digits in plaintext " | |
| 121 "(should be %d hexadecimal digits, found %u)\n", | |
| 122 16*2, (unsigned)strlen(argv[2])); | |
| 123 exit(1); | |
| 124 } | |
| 125 len = hex_string_to_octet_string((char *)(&data), argv[2], 16*2); | |
| 126 /* check that hex string is the right length */ | |
| 127 if (len < 16*2) { | |
| 128 fprintf(stderr, | |
| 129 "error: too few digits in plaintext " | |
| 130 "(should be %d hexadecimal digits, found %d)\n", | |
| 131 16*2, len); | |
| 132 exit(1); | |
| 133 } | |
| 134 | |
| 135 if (verbose) { | |
| 136 /* print out plaintext */ | |
| 137 printf("plaintext:\t%s\n", octet_string_hex_string((uint8_t *)&data, 16)); | |
| 138 } | |
| 139 | |
| 140 /* encrypt plaintext */ | |
| 141 status = aes_expand_encryption_key(key, key_len, &exp_key); | |
| 142 if (status) { | |
| 143 fprintf(stderr, | |
| 144 "error: AES key expansion failed.\n"); | |
| 145 exit(1); | |
| 146 } | |
| 147 | |
| 148 aes_encrypt(&data, &exp_key); | |
| 149 | |
| 150 /* write ciphertext to output */ | |
| 151 if (verbose) { | |
| 152 printf("key:\t\t%s\n", octet_string_hex_string(key, key_len)); | |
| 153 printf("ciphertext:\t"); | |
| 154 } | |
| 155 printf("%s\n", v128_hex_string(&data)); | |
| 156 | |
| 157 return 0; | |
| 158 } | |
| 159 | |
| OLD | NEW |