You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aes_test.cc 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <openssl/aes.h>
  17. #include <openssl/crypto.h>
  18. static bool TestAES(const uint8_t *key, size_t key_len,
  19. const uint8_t plaintext[AES_BLOCK_SIZE],
  20. const uint8_t ciphertext[AES_BLOCK_SIZE]) {
  21. AES_KEY aes_key;
  22. if (AES_set_encrypt_key(key, key_len * 8, &aes_key) != 0) {
  23. fprintf(stderr, "AES_set_encrypt_key failed\n");
  24. return false;
  25. }
  26. // Test encryption.
  27. uint8_t block[AES_BLOCK_SIZE];
  28. AES_encrypt(plaintext, block, &aes_key);
  29. if (memcmp(block, ciphertext, AES_BLOCK_SIZE) != 0) {
  30. fprintf(stderr, "AES_encrypt gave the wrong output\n");
  31. return false;
  32. }
  33. // Test in-place encryption.
  34. memcpy(block, plaintext, AES_BLOCK_SIZE);
  35. AES_encrypt(block, block, &aes_key);
  36. if (memcmp(block, ciphertext, AES_BLOCK_SIZE) != 0) {
  37. fprintf(stderr, "AES_encrypt gave the wrong output\n");
  38. return false;
  39. }
  40. if (AES_set_decrypt_key(key, key_len * 8, &aes_key) != 0) {
  41. fprintf(stderr, "AES_set_decrypt_key failed\n");
  42. return false;
  43. }
  44. // Test decryption.
  45. AES_decrypt(ciphertext, block, &aes_key);
  46. if (memcmp(block, plaintext, AES_BLOCK_SIZE) != 0) {
  47. fprintf(stderr, "AES_decrypt gave the wrong output\n");
  48. return false;
  49. }
  50. // Test in-place decryption.
  51. memcpy(block, ciphertext, AES_BLOCK_SIZE);
  52. AES_decrypt(block, block, &aes_key);
  53. if (memcmp(block, plaintext, AES_BLOCK_SIZE) != 0) {
  54. fprintf(stderr, "AES_decrypt gave the wrong output\n");
  55. return false;
  56. }
  57. return true;
  58. }
  59. int main() {
  60. CRYPTO_library_init();
  61. // Test vectors from FIPS-197, Appendix C.
  62. if (!TestAES((const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
  63. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
  64. 128 / 8,
  65. (const uint8_t *)"\x00\x11\x22\x33\x44\x55\x66\x77"
  66. "\x88\x99\xaa\xbb\xcc\xdd\xee\xff",
  67. (const uint8_t *)"\x69\xc4\xe0\xd8\x6a\x7b\x04\x30"
  68. "\xd8\xcd\xb7\x80\x70\xb4\xc5\x5a") ||
  69. !TestAES((const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
  70. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  71. "\x10\x11\x12\x13\x14\x15\x16\x17",
  72. 192 / 8,
  73. (const uint8_t *)"\x00\x11\x22\x33\x44\x55\x66\x77"
  74. "\x88\x99\xaa\xbb\xcc\xdd\xee\xff",
  75. (const uint8_t *)"\xdd\xa9\x7c\xa4\x86\x4c\xdf\xe0"
  76. "\x6e\xaf\x70\xa0\xec\x0d\x71\x91") ||
  77. !TestAES((const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
  78. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  79. "\x10\x11\x12\x13\x14\x15\x16\x17"
  80. "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
  81. 256 / 8,
  82. (const uint8_t *)"\x00\x11\x22\x33\x44\x55\x66\x77"
  83. "\x88\x99\xaa\xbb\xcc\xdd\xee\xff",
  84. (const uint8_t *)"\x8e\xa2\xb7\xca\x51\x67\x45\xbf"
  85. "\xea\xfc\x49\x90\x4b\x49\x60\x89")) {
  86. return false;
  87. }
  88. printf("PASS\n");
  89. return 0;
  90. }