blob: 73ae2555d5a2154d8146d54dcf817986608ab8b1 [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
Robert Sloan8ff03552017-06-14 12:40:58 -070021#include <gtest/gtest.h>
22
Kenny Rootb8494592015-09-25 02:29:14 +000023#include <openssl/aes.h>
Kenny Rootb8494592015-09-25 02:29:14 +000024
Robert Sloan572a4e22017-04-17 10:52:19 -070025#include "../../internal.h"
26#include "../../test/file_test.h"
Robert Sloan8ff03552017-06-14 12:40:58 -070027#include "../../test/test_util.h"
Kenny Rootb8494592015-09-25 02:29:14 +000028
Steven Valdezbb1ceac2016-10-07 10:34:51 -040029
Robert Sloan8ff03552017-06-14 12:40:58 -070030static void TestRaw(FileTest *t) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -040031 std::vector<uint8_t> key, plaintext, ciphertext;
Robert Sloan8ff03552017-06-14 12:40:58 -070032 ASSERT_TRUE(t->GetBytes(&key, "Key"));
33 ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext"));
34 ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext"));
Steven Valdezbb1ceac2016-10-07 10:34:51 -040035
Robert Sloan8ff03552017-06-14 12:40:58 -070036 ASSERT_EQ(static_cast<unsigned>(AES_BLOCK_SIZE), plaintext.size());
37 ASSERT_EQ(static_cast<unsigned>(AES_BLOCK_SIZE), ciphertext.size());
Steven Valdezbb1ceac2016-10-07 10:34:51 -040038
Kenny Rootb8494592015-09-25 02:29:14 +000039 AES_KEY aes_key;
Robert Sloan8ff03552017-06-14 12:40:58 -070040 ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key));
Kenny Rootb8494592015-09-25 02:29:14 +000041
42 // Test encryption.
43 uint8_t block[AES_BLOCK_SIZE];
Steven Valdezbb1ceac2016-10-07 10:34:51 -040044 AES_encrypt(plaintext.data(), block, &aes_key);
Robert Sloan8ff03552017-06-14 12:40:58 -070045 EXPECT_EQ(Bytes(ciphertext), Bytes(block));
Kenny Rootb8494592015-09-25 02:29:14 +000046
47 // Test in-place encryption.
Robert Sloan69939df2017-01-09 10:53:07 -080048 OPENSSL_memcpy(block, plaintext.data(), AES_BLOCK_SIZE);
Kenny Rootb8494592015-09-25 02:29:14 +000049 AES_encrypt(block, block, &aes_key);
Robert Sloan8ff03552017-06-14 12:40:58 -070050 EXPECT_EQ(Bytes(ciphertext), Bytes(block));
Kenny Rootb8494592015-09-25 02:29:14 +000051
Robert Sloan8ff03552017-06-14 12:40:58 -070052 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key));
Kenny Rootb8494592015-09-25 02:29:14 +000053
54 // Test decryption.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040055 AES_decrypt(ciphertext.data(), block, &aes_key);
Robert Sloan8ff03552017-06-14 12:40:58 -070056 EXPECT_EQ(Bytes(plaintext), Bytes(block));
Kenny Rootb8494592015-09-25 02:29:14 +000057
58 // Test in-place decryption.
Robert Sloan69939df2017-01-09 10:53:07 -080059 OPENSSL_memcpy(block, ciphertext.data(), AES_BLOCK_SIZE);
Kenny Rootb8494592015-09-25 02:29:14 +000060 AES_decrypt(block, block, &aes_key);
Robert Sloan8ff03552017-06-14 12:40:58 -070061 EXPECT_EQ(Bytes(plaintext), Bytes(block));
Kenny Rootb8494592015-09-25 02:29:14 +000062}
63
Robert Sloan8ff03552017-06-14 12:40:58 -070064static void TestKeyWrap(FileTest *t) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -040065 // All test vectors use the default IV, so test both with implicit and
66 // explicit IV.
67 //
68 // TODO(davidben): Find test vectors that use a different IV.
69 static const uint8_t kDefaultIV[] = {
70 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6,
71 };
Kenny Rootb8494592015-09-25 02:29:14 +000072
Steven Valdezbb1ceac2016-10-07 10:34:51 -040073 std::vector<uint8_t> key, plaintext, ciphertext;
Robert Sloan8ff03552017-06-14 12:40:58 -070074 ASSERT_TRUE(t->GetBytes(&key, "Key"));
75 ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext"));
76 ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext"));
Kenny Rootb8494592015-09-25 02:29:14 +000077
Robert Sloan8ff03552017-06-14 12:40:58 -070078 ASSERT_EQ(plaintext.size() + 8, ciphertext.size())
79 << "Invalid Plaintext and Ciphertext lengths.";
Steven Valdezbb1ceac2016-10-07 10:34:51 -040080
Robert Sloan8ff03552017-06-14 12:40:58 -070081 // Test encryption.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040082 AES_KEY aes_key;
Robert Sloan8ff03552017-06-14 12:40:58 -070083 ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key));
Steven Valdezbb1ceac2016-10-07 10:34:51 -040084
Robert Sloan8ff03552017-06-14 12:40:58 -070085 // Test with implicit IV.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040086 std::unique_ptr<uint8_t[]> buf(new uint8_t[ciphertext.size()]);
Robert Sloan8ff03552017-06-14 12:40:58 -070087 int len = AES_wrap_key(&aes_key, nullptr /* iv */, buf.get(),
88 plaintext.data(), plaintext.size());
89 ASSERT_GE(len, 0);
90 EXPECT_EQ(Bytes(ciphertext), Bytes(buf.get(), static_cast<size_t>(len)));
Steven Valdezbb1ceac2016-10-07 10:34:51 -040091
Robert Sloan8ff03552017-06-14 12:40:58 -070092 // Test with explicit IV.
Robert Sloan69939df2017-01-09 10:53:07 -080093 OPENSSL_memset(buf.get(), 0, ciphertext.size());
Robert Sloan8ff03552017-06-14 12:40:58 -070094 len = AES_wrap_key(&aes_key, kDefaultIV, buf.get(), plaintext.data(),
95 plaintext.size());
96 ASSERT_GE(len, 0);
97 EXPECT_EQ(Bytes(ciphertext), Bytes(buf.get(), static_cast<size_t>(len)));
Steven Valdezbb1ceac2016-10-07 10:34:51 -040098
Robert Sloan8ff03552017-06-14 12:40:58 -070099 // Test decryption.
100 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key));
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400101
Robert Sloan8ff03552017-06-14 12:40:58 -0700102 // Test with implicit IV.
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400103 buf.reset(new uint8_t[plaintext.size()]);
Robert Sloan8ff03552017-06-14 12:40:58 -0700104 len = AES_unwrap_key(&aes_key, nullptr /* iv */, buf.get(), ciphertext.data(),
105 ciphertext.size());
106 ASSERT_GE(len, 0);
107 EXPECT_EQ(Bytes(plaintext), Bytes(buf.get(), static_cast<size_t>(len)));
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400108
Robert Sloan8ff03552017-06-14 12:40:58 -0700109 // Test with explicit IV.
Robert Sloan69939df2017-01-09 10:53:07 -0800110 OPENSSL_memset(buf.get(), 0, plaintext.size());
Robert Sloan8ff03552017-06-14 12:40:58 -0700111 len = AES_unwrap_key(&aes_key, kDefaultIV, buf.get(), ciphertext.data(),
112 ciphertext.size());
113 ASSERT_GE(len, 0);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400114
Robert Sloan8ff03552017-06-14 12:40:58 -0700115 // Test corrupted ciphertext.
Robert Sloan69939df2017-01-09 10:53:07 -0800116 ciphertext[0] ^= 1;
Robert Sloan8ff03552017-06-14 12:40:58 -0700117 EXPECT_EQ(-1, AES_unwrap_key(&aes_key, nullptr /* iv */, buf.get(),
118 ciphertext.data(), ciphertext.size()));
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400119}
120
Robert Sloan8ff03552017-06-14 12:40:58 -0700121TEST(AESTest, TestVectors) {
122 FileTestGTest("crypto/fipsmodule/aes/aes_tests.txt", [](FileTest *t) {
123 if (t->GetParameter() == "Raw") {
124 TestRaw(t);
125 } else if (t->GetParameter() == "KeyWrap") {
126 TestKeyWrap(t);
127 } else {
128 ADD_FAILURE() << "Unknown mode " << t->GetParameter();
129 }
130 });
Kenny Rootb8494592015-09-25 02:29:14 +0000131}