Home | History | Annotate | Download | only in test
      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 00112233445566778899aabbccddeeff -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_KEY_LEN 16
     32 
     33 int
     34 main (int argc, char *argv[]) {
     35   v128_t data, key;
     36   aes_expanded_key_t exp_key;
     37   int len;
     38   int verbose;
     39 
     40   if (argc == 3) {
     41     /* we're not in verbose mode */
     42     verbose = 0;
     43   } else if (argc == 4) {
     44     if (strncmp(argv[3], "-v", 2) == 0) {
     45       /* we're in verbose mode */
     46       verbose = 1;
     47     } else {
     48       /* unrecognized flag, complain and exit */
     49       usage(argv[0]);
     50     }
     51   } else {
     52     /* we've been fed the wrong number of arguments - compain and exit */
     53     usage(argv[0]);
     54   }
     55 
     56   /* read in key, checking length */
     57   if (strlen(argv[1]) > AES_KEY_LEN*2) {
     58     fprintf(stderr,
     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]));
     62     exit(1);
     63   }
     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) {
     67     fprintf(stderr,
     68 	    "error: too few digits in key "
     69 	    "(should be %d hexadecimal digits, found %d)\n",
     70 	    AES_KEY_LEN*2, len);
     71     exit(1);
     72   }
     73 
     74   /* read in plaintext, checking length */
     75   if (strlen(argv[2]) > 16*2) {
     76     fprintf(stderr,
     77 	    "error: too many digits in plaintext "
     78 	    "(should be %d hexadecimal digits, found %u)\n",
     79 	    16*2, (unsigned)strlen(argv[2]));
     80     exit(1);
     81   }
     82   len = hex_string_to_octet_string((char *)(&data), argv[2], 16*2);
     83   /* check that hex string is the right length */
     84   if (len < 16*2) {
     85     fprintf(stderr,
     86 	    "error: too few digits in plaintext "
     87 	    "(should be %d hexadecimal digits, found %d)\n",
     88 	    16*2, len);
     89     exit(1);
     90   }
     91 
     92   if (verbose) {
     93     /* print out plaintext */
     94     printf("plaintext:\t%s\n", octet_string_hex_string((uint8_t *)&data, 16));
     95   }
     96 
     97   /* encrypt plaintext */
     98   aes_expand_encryption_key(&key, exp_key);
     99 
    100   aes_encrypt(&data, exp_key);
    101 
    102   /* write ciphertext to output */
    103   if (verbose) {
    104     printf("key:\t\t%s\n", v128_hex_string(&key));
    105     printf("ciphertext:\t");
    106   }
    107   printf("%s\n", v128_hex_string(&data));
    108 
    109   return 0;
    110 }
    111 
    112