blob: 4fb3a31f5d41596a2b0275c642b7d83b5af6bab6 [file] [log] [blame]
Kenny Rootb8494592015-09-25 02:29:14 +00001/* 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 Valdezbb1ceac2016-10-07 10:34:51 -040018#include <memory>
19#include <vector>
20
Kenny Rootb8494592015-09-25 02:29:14 +000021#include <openssl/aes.h>
22#include <openssl/crypto.h>
23
Steven Valdezbb1ceac2016-10-07 10:34:51 -040024#include "../test/file_test.h"
Kenny Rootb8494592015-09-25 02:29:14 +000025
Steven Valdezbb1ceac2016-10-07 10:34:51 -040026
27static 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 Rootb8494592015-09-25 02:29:14 +000041 AES_KEY aes_key;
Steven Valdezbb1ceac2016-10-07 10:34:51 -040042 if (AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key) != 0) {
43 t->PrintLine("AES_set_encrypt_key failed.");
Kenny Rootb8494592015-09-25 02:29:14 +000044 return false;
45 }
46
47 // Test encryption.
48 uint8_t block[AES_BLOCK_SIZE];
Steven Valdezbb1ceac2016-10-07 10:34:51 -040049 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 Rootb8494592015-09-25 02:29:14 +000053 return false;
54 }
55
56 // Test in-place encryption.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040057 memcpy(block, plaintext.data(), AES_BLOCK_SIZE);
Kenny Rootb8494592015-09-25 02:29:14 +000058 AES_encrypt(block, block, &aes_key);
Steven Valdezbb1ceac2016-10-07 10:34:51 -040059 if (!t->ExpectBytesEqual(block, AES_BLOCK_SIZE, ciphertext.data(),
60 ciphertext.size())) {
61 t->PrintLine("In-place AES_encrypt gave the wrong output.");
Kenny Rootb8494592015-09-25 02:29:14 +000062 return false;
63 }
64
Steven Valdezbb1ceac2016-10-07 10:34:51 -040065 if (AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key) != 0) {
66 t->PrintLine("AES_set_decrypt_key failed.");
Kenny Rootb8494592015-09-25 02:29:14 +000067 return false;
68 }
69
70 // Test decryption.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040071 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 Rootb8494592015-09-25 02:29:14 +000075 return false;
76 }
77
78 // Test in-place decryption.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040079 memcpy(block, ciphertext.data(), AES_BLOCK_SIZE);
Kenny Rootb8494592015-09-25 02:29:14 +000080 AES_decrypt(block, block, &aes_key);
Steven Valdezbb1ceac2016-10-07 10:34:51 -040081 if (!t->ExpectBytesEqual(block, AES_BLOCK_SIZE, plaintext.data(),
82 plaintext.size())) {
83 t->PrintLine("In-place AES_decrypt gave the wrong output.");
Kenny Rootb8494592015-09-25 02:29:14 +000084 return false;
85 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -040086
Kenny Rootb8494592015-09-25 02:29:14 +000087 return true;
88}
89
Steven Valdezbb1ceac2016-10-07 10:34:51 -040090static 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 Rootb8494592015-09-25 02:29:14 +000098
Steven Valdezbb1ceac2016-10-07 10:34:51 -040099 std::vector<uint8_t> key, plaintext, ciphertext;
100 if (!t->GetBytes(&key, "Key") ||
101 !t->GetBytes(&plaintext, "Plaintext") ||
102 !t->GetBytes(&ciphertext, "Ciphertext")) {
Kenny Rootb8494592015-09-25 02:29:14 +0000103 return false;
104 }
105
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400106 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
161static 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
173int 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 Rootb8494592015-09-25 02:29:14 +0000182}