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