netwerk/srtp/src/crypto/test/aes_calc.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/srtp/src/crypto/test/aes_calc.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,154 @@
     1.4 +/*
     1.5 + * aes_calc.c
     1.6 + * 
     1.7 + * A simple AES calculator for generating AES encryption values
     1.8 + *
     1.9 + * David A. McGrew
    1.10 + * Cisco Systems, Inc.
    1.11 + */
    1.12 +/*
    1.13 + *	
    1.14 + * Copyright (c) 2001-2006, Cisco Systems, Inc.
    1.15 + * All rights reserved.
    1.16 + * 
    1.17 + * Redistribution and use in source and binary forms, with or without
    1.18 + * modification, are permitted provided that the following conditions
    1.19 + * are met:
    1.20 + * 
    1.21 + *   Redistributions of source code must retain the above copyright
    1.22 + *   notice, this list of conditions and the following disclaimer.
    1.23 + * 
    1.24 + *   Redistributions in binary form must reproduce the above
    1.25 + *   copyright notice, this list of conditions and the following
    1.26 + *   disclaimer in the documentation and/or other materials provided
    1.27 + *   with the distribution.
    1.28 + * 
    1.29 + *   Neither the name of the Cisco Systems, Inc. nor the names of its
    1.30 + *   contributors may be used to endorse or promote products derived
    1.31 + *   from this software without specific prior written permission.
    1.32 + * 
    1.33 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.34 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.35 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    1.36 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    1.37 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    1.38 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.39 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.40 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.41 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.42 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.43 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.44 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.45 + *
    1.46 + */
    1.47 +
    1.48 +/*
    1.49 +  
    1.50 + Example usage (with first NIST FIPS 197 test case):
    1.51 + 
    1.52 +[sh]$ test/aes_calc 000102030405060708090a0b0c0d0e0f 00112233445566778899aabbccddeeff -v
    1.53 + plaintext:      00112233445566778899aabbccddeeff
    1.54 + key:            000102030405060708090a0b0c0d0e0f
    1.55 + ciphertext:     69c4e0d86a7b0430d8cdb78070b4c55a
    1.56 +
    1.57 + */
    1.58 +
    1.59 +#include "aes.h"
    1.60 +#include <stdio.h>
    1.61 +#include <string.h>
    1.62 +
    1.63 +void
    1.64 +usage(char *prog_name) {
    1.65 +  printf("usage: %s <key> <plaintext> [-v]\n", prog_name);
    1.66 +  exit(255);
    1.67 +}
    1.68 +
    1.69 +#define AES_MAX_KEY_LEN 32
    1.70 +
    1.71 +int
    1.72 +main (int argc, char *argv[]) {
    1.73 +  v128_t data;
    1.74 +  uint8_t key[AES_MAX_KEY_LEN];
    1.75 +  aes_expanded_key_t exp_key;
    1.76 +  int key_len, len;
    1.77 +  int verbose;
    1.78 +  err_status_t status;
    1.79 +
    1.80 +  if (argc == 3) {
    1.81 +    /* we're not in verbose mode */
    1.82 +    verbose = 0;
    1.83 +  } else if (argc == 4) {
    1.84 +    if (strncmp(argv[3], "-v", 2) == 0) {
    1.85 +      /* we're in verbose mode */
    1.86 +      verbose = 1;
    1.87 +    } else {
    1.88 +      /* unrecognized flag, complain and exit */
    1.89 +      usage(argv[0]);
    1.90 +    }
    1.91 +  } else {
    1.92 +    /* we've been fed the wrong number of arguments - compain and exit */
    1.93 +    usage(argv[0]);
    1.94 +  }
    1.95 +  
    1.96 +  /* read in key, checking length */
    1.97 +  if (strlen(argv[1]) > AES_MAX_KEY_LEN*2) {
    1.98 +    fprintf(stderr, 
    1.99 +	    "error: too many digits in key "
   1.100 +	    "(should be at most %d hexadecimal digits, found %u)\n",
   1.101 +	    AES_MAX_KEY_LEN*2, (unsigned)strlen(argv[1]));
   1.102 +    exit(1);    
   1.103 +  }
   1.104 +  len = hex_string_to_octet_string((char*)key, argv[1], AES_MAX_KEY_LEN*2);
   1.105 +  /* check that hex string is the right length */
   1.106 +  if (len != 32 && len != 48 && len != 64) {
   1.107 +    fprintf(stderr, 
   1.108 +	    "error: bad number of digits in key "
   1.109 +	    "(should be 32/48/64 hexadecimal digits, found %d)\n",
   1.110 +	    len);
   1.111 +    exit(1);    
   1.112 +  } 
   1.113 +  key_len = len/2;
   1.114 +      
   1.115 +  /* read in plaintext, checking length */
   1.116 +  if (strlen(argv[2]) > 16*2) {
   1.117 +    fprintf(stderr, 
   1.118 +	    "error: too many digits in plaintext "
   1.119 +	    "(should be %d hexadecimal digits, found %u)\n",
   1.120 +	    16*2, (unsigned)strlen(argv[2]));
   1.121 +    exit(1);    
   1.122 +  }
   1.123 +  len = hex_string_to_octet_string((char *)(&data), argv[2], 16*2);
   1.124 +  /* check that hex string is the right length */
   1.125 +  if (len < 16*2) {
   1.126 +    fprintf(stderr, 
   1.127 +	    "error: too few digits in plaintext "
   1.128 +	    "(should be %d hexadecimal digits, found %d)\n",
   1.129 +	    16*2, len);
   1.130 +    exit(1);    
   1.131 +  }
   1.132 +
   1.133 +  if (verbose) {
   1.134 +    /* print out plaintext */
   1.135 +    printf("plaintext:\t%s\n", octet_string_hex_string((uint8_t *)&data, 16));
   1.136 +  }
   1.137 +
   1.138 +  /* encrypt plaintext */
   1.139 +  status = aes_expand_encryption_key(key, key_len, &exp_key);
   1.140 +  if (status) {
   1.141 +    fprintf(stderr,
   1.142 +	    "error: AES key expansion failed.\n");
   1.143 +    exit(1);
   1.144 +  }
   1.145 +
   1.146 +  aes_encrypt(&data, &exp_key);
   1.147 +
   1.148 +  /* write ciphertext to output */
   1.149 +  if (verbose) {
   1.150 +    printf("key:\t\t%s\n", octet_string_hex_string(key, key_len));
   1.151 +    printf("ciphertext:\t");
   1.152 +  }
   1.153 +  printf("%s\n", v128_hex_string(&data));
   1.154 +
   1.155 +  return 0;
   1.156 +}
   1.157 +

mercurial