aes_calc.c 4.8 KB

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