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-2017, 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 \
51 00112233445566778899aabbccddeeff -v
52
53 plaintext: 00112233445566778899aabbccddeeff
54 key: 000102030405060708090a0b0c0d0e0f
55 ciphertext: 69c4e0d86a7b0430d8cdb78070b4c55a
56
57 */
58
59 #ifdef HAVE_CONFIG_H
60 #include <config.h>
61 #endif
62
63 #include "aes.h"
64 #include <stdio.h>
65 #include <string.h>
66 #include "util.h"
67
usage(char * prog_name)68 void usage(char *prog_name)
69 {
70 printf("usage: %s <key> <plaintext> [-v]\n", prog_name);
71 exit(255);
72 }
73
74 #define AES_MAX_KEY_LEN 32
75
main(int argc,char * argv[])76 int main(int argc, char *argv[])
77 {
78 v128_t data;
79 uint8_t key[AES_MAX_KEY_LEN];
80 srtp_aes_expanded_key_t exp_key;
81 int key_len, len;
82 int verbose = 0;
83 srtp_err_status_t status;
84
85 if (argc == 3) {
86 /* we're not in verbose mode */
87 verbose = 0;
88 } else if (argc == 4) {
89 if (strncmp(argv[3], "-v", 2) == 0) {
90 /* we're in verbose mode */
91 verbose = 1;
92 } else {
93 /* unrecognized flag, complain and exit */
94 usage(argv[0]);
95 }
96 } else {
97 /* we've been fed the wrong number of arguments - compain and exit */
98 usage(argv[0]);
99 }
100
101 /* read in key, checking length */
102 if (strlen(argv[1]) > AES_MAX_KEY_LEN * 2) {
103 fprintf(stderr, "error: too many digits in key "
104 "(should be at most %d hexadecimal digits, found %u)\n",
105 AES_MAX_KEY_LEN * 2, (unsigned)strlen(argv[1]));
106 exit(1);
107 }
108 len = hex_string_to_octet_string((char *)key, argv[1], AES_MAX_KEY_LEN * 2);
109 /* check that hex string is the right length */
110 if (len != 32 && len != 48 && len != 64) {
111 fprintf(stderr, "error: bad number of digits in key "
112 "(should be 32/48/64 hexadecimal digits, found %d)\n",
113 len);
114 exit(1);
115 }
116 key_len = len / 2;
117
118 /* read in plaintext, checking length */
119 if (strlen(argv[2]) > 16 * 2) {
120 fprintf(stderr, "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, "error: too few digits in plaintext "
129 "(should be %d hexadecimal digits, found %d)\n",
130 16 * 2, len);
131 exit(1);
132 }
133
134 if (verbose) {
135 /* print out plaintext */
136 printf("plaintext:\t%s\n",
137 octet_string_hex_string((uint8_t *)&data, 16));
138 }
139
140 /* encrypt plaintext */
141 status = srtp_aes_expand_encryption_key(key, key_len, &exp_key);
142 if (status) {
143 fprintf(stderr, "error: AES key expansion failed.\n");
144 exit(1);
145 }
146
147 srtp_aes_encrypt(&data, &exp_key);
148
149 /* write ciphertext to output */
150 if (verbose) {
151 printf("key:\t\t%s\n", octet_string_hex_string(key, key_len));
152 printf("ciphertext:\t");
153 }
154 printf("%s\n", v128_hex_string(&data));
155
156 return 0;
157 }
158