Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 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 | |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <vector> |
| 20 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 21 | #include <openssl/aes.h> |
| 22 | #include <openssl/crypto.h> |
| 23 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 24 | #include "../test/file_test.h" |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 25 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 26 | |
| 27 | static bool TestRaw(FileTest *t) { |
| 28 | std::vector<uint8_t> key, plaintext, ciphertext; |
| 29 | if (!t->GetBytes(&key, "Key") || |
| 30 | !t->GetBytes(&plaintext, "Plaintext") || |
| 31 | !t->GetBytes(&ciphertext, "Ciphertext")) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | if (plaintext.size() != AES_BLOCK_SIZE || |
| 36 | ciphertext.size() != AES_BLOCK_SIZE) { |
| 37 | t->PrintLine("Plaintext or Ciphertext not a block size."); |
| 38 | return false; |
| 39 | } |
| 40 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 41 | AES_KEY aes_key; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 42 | if (AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key) != 0) { |
| 43 | t->PrintLine("AES_set_encrypt_key failed."); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
| 47 | // Test encryption. |
| 48 | uint8_t block[AES_BLOCK_SIZE]; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 49 | AES_encrypt(plaintext.data(), block, &aes_key); |
| 50 | if (!t->ExpectBytesEqual(block, AES_BLOCK_SIZE, ciphertext.data(), |
| 51 | ciphertext.size())) { |
| 52 | t->PrintLine("AES_encrypt gave the wrong output."); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // Test in-place encryption. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 57 | memcpy(block, plaintext.data(), AES_BLOCK_SIZE); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 58 | AES_encrypt(block, block, &aes_key); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 59 | if (!t->ExpectBytesEqual(block, AES_BLOCK_SIZE, ciphertext.data(), |
| 60 | ciphertext.size())) { |
| 61 | t->PrintLine("In-place AES_encrypt gave the wrong output."); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 65 | if (AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key) != 0) { |
| 66 | t->PrintLine("AES_set_decrypt_key failed."); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // Test decryption. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 71 | AES_decrypt(ciphertext.data(), block, &aes_key); |
| 72 | if (!t->ExpectBytesEqual(block, AES_BLOCK_SIZE, plaintext.data(), |
| 73 | plaintext.size())) { |
| 74 | t->PrintLine("AES_decrypt gave the wrong output."); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
| 78 | // Test in-place decryption. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 79 | memcpy(block, ciphertext.data(), AES_BLOCK_SIZE); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 80 | AES_decrypt(block, block, &aes_key); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 81 | if (!t->ExpectBytesEqual(block, AES_BLOCK_SIZE, plaintext.data(), |
| 82 | plaintext.size())) { |
| 83 | t->PrintLine("In-place AES_decrypt gave the wrong output."); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 84 | return false; |
| 85 | } |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 86 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 90 | static bool TestKeyWrap(FileTest *t) { |
| 91 | // All test vectors use the default IV, so test both with implicit and |
| 92 | // explicit IV. |
| 93 | // |
| 94 | // TODO(davidben): Find test vectors that use a different IV. |
| 95 | static const uint8_t kDefaultIV[] = { |
| 96 | 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, |
| 97 | }; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 98 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 99 | std::vector<uint8_t> key, plaintext, ciphertext; |
| 100 | if (!t->GetBytes(&key, "Key") || |
| 101 | !t->GetBytes(&plaintext, "Plaintext") || |
| 102 | !t->GetBytes(&ciphertext, "Ciphertext")) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 106 | if (plaintext.size() + 8 != ciphertext.size()) { |
| 107 | t->PrintLine("Invalid Plaintext and Ciphertext lengths."); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | AES_KEY aes_key; |
| 112 | if (AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key) != 0) { |
| 113 | t->PrintLine("AES_set_encrypt_key failed."); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | std::unique_ptr<uint8_t[]> buf(new uint8_t[ciphertext.size()]); |
| 118 | if (AES_wrap_key(&aes_key, nullptr /* iv */, buf.get(), plaintext.data(), |
| 119 | plaintext.size()) != static_cast<int>(ciphertext.size()) || |
| 120 | !t->ExpectBytesEqual(buf.get(), ciphertext.size(), ciphertext.data(), |
| 121 | ciphertext.size())) { |
| 122 | t->PrintLine("AES_wrap_key with implicit IV failed."); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | memset(buf.get(), 0, ciphertext.size()); |
| 127 | if (AES_wrap_key(&aes_key, kDefaultIV, buf.get(), plaintext.data(), |
| 128 | plaintext.size()) != static_cast<int>(ciphertext.size()) || |
| 129 | !t->ExpectBytesEqual(buf.get(), ciphertext.size(), ciphertext.data(), |
| 130 | ciphertext.size())) { |
| 131 | t->PrintLine("AES_wrap_key with explicit IV failed."); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | if (AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key) != 0) { |
| 136 | t->PrintLine("AES_set_decrypt_key failed."); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | buf.reset(new uint8_t[plaintext.size()]); |
| 141 | if (AES_unwrap_key(&aes_key, nullptr /* iv */, buf.get(), ciphertext.data(), |
| 142 | ciphertext.size()) != static_cast<int>(plaintext.size()) || |
| 143 | !t->ExpectBytesEqual(buf.get(), plaintext.size(), plaintext.data(), |
| 144 | plaintext.size())) { |
| 145 | t->PrintLine("AES_unwrap_key with implicit IV failed."); |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | memset(buf.get(), 0, plaintext.size()); |
| 150 | if (AES_unwrap_key(&aes_key, kDefaultIV, buf.get(), ciphertext.data(), |
| 151 | ciphertext.size()) != static_cast<int>(plaintext.size()) || |
| 152 | !t->ExpectBytesEqual(buf.get(), plaintext.size(), plaintext.data(), |
| 153 | plaintext.size())) { |
| 154 | t->PrintLine("AES_unwrap_key with explicit IV failed."); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | static bool TestAES(FileTest *t, void *arg) { |
| 162 | if (t->GetParameter() == "Raw") { |
| 163 | return TestRaw(t); |
| 164 | } |
| 165 | if (t->GetParameter() == "KeyWrap") { |
| 166 | return TestKeyWrap(t); |
| 167 | } |
| 168 | |
| 169 | t->PrintLine("Unknown mode '%s'.", t->GetParameter().c_str()); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | int main(int argc, char **argv) { |
| 174 | CRYPTO_library_init(); |
| 175 | |
| 176 | if (argc != 2) { |
| 177 | fprintf(stderr, "%s <test file.txt>\n", argv[0]); |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | return FileTestMain(TestAES, nullptr, argv[1]); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 182 | } |