| /* |
| * Copyright 2014 Google Inc. All rights reserved. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include "securemessage/crypto_ops.h" |
| |
| #include <gtest/gtest.h> |
| #include <gtest/gtest-message.h> |
| #include <gtest/internal/gtest-internal.h> |
| #include <stddef.h> |
| |
| using std::unique_ptr; |
| |
| namespace securemessage { |
| |
| // |
| // Class for exposing CryptOps private helper functions for testing purposes |
| // |
| class CryptoOpsTest { |
| public: |
| static const uint8_t* Salt() { return CryptoOps::kSalt; } |
| |
| static unsigned int SaltSize() { return CryptoOps::kSaltSize; } |
| |
| static unsigned int AesKeySize() { return CryptoOps::kAesKeySize; } |
| |
| static unique_ptr<ByteBuffer> Sha256(ByteBuffer message) { |
| return CryptoOps::Sha256(message); |
| } |
| |
| static unique_ptr<ByteBuffer> HkdfSha256Extract(ByteBuffer key, |
| ByteBuffer salt) { |
| return CryptoOps::HkdfSha256Extract(key, salt); |
| } |
| |
| static unique_ptr<ByteBuffer> Aes256CBCDecrypt( |
| CryptoOps::SecretKey decryptionKey, ByteBuffer iv, |
| ByteBuffer ciphertext) { |
| return CryptoOps::Aes256CBCDecrypt(decryptionKey, iv, ciphertext); |
| } |
| |
| static unique_ptr<ByteBuffer> Aes256CBCEncrypt( |
| CryptoOps::SecretKey encryptionKey, ByteBuffer iv, ByteBuffer plaintext) { |
| return CryptoOps::Aes256CBCEncrypt(encryptionKey, iv, plaintext); |
| } |
| |
| static string GetPurpose(CryptoOps::SigType sigType) { |
| return CryptoOps::GetPurpose(sigType); |
| } |
| |
| static string GetPurpose(CryptoOps::EncType encType) { |
| return CryptoOps::GetPurpose(encType); |
| } |
| |
| static string Int32BytesToString(int32_t value) { |
| return CryptoOps::Int32BytesToString(value); |
| } |
| |
| static bool StringToInt32Bytes(const string& value, int32_t* result) { |
| return CryptoOps::StringToInt32Bytes(value, result); |
| } |
| }; |
| } // namespace securemessage |
| |
| namespace securemessage { |
| namespace unittesting { |
| namespace { |
| |
| // HKDF Test Case 1 IKM from RFC 5869 |
| static unsigned const int kHkdfCase1IkmLength = 22; |
| static unsigned const char kHkdfCase1Ikm[kHkdfCase1IkmLength] = { |
| 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
| |
| // HKDF Test Case 1 salt from RFC 5869 |
| static unsigned const int kHkdfCase1SaltLength = 13; |
| static unsigned const char kHkdfCase1Salt[kHkdfCase1SaltLength] = { |
| 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c}; |
| |
| // HKDF Test Case 1 info from RFC 5869 |
| static unsigned const int kHkdfCase1InfoLength = 10; |
| static unsigned const char kHkdfCase1Info[kHkdfCase1InfoLength] = { |
| 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9}; |
| |
| // First 32 bytes of HKDF Test Case 1 OKM (output) from RFC 5869 |
| static unsigned const int kHkdfCase1OkmLength = 32; |
| static unsigned const char kHkdfCase1Okm[kHkdfCase1OkmLength] = { |
| 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, |
| 0x64, 0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, |
| 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf}; |
| |
| // Expected results (based on Java SecureMessage codebase) |
| static unsigned const int kHkdfExtractExpectedLength = 32; |
| static unsigned const char kHkdfExtractExpected[kHkdfExtractExpectedLength] = { |
| 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, 0x0d, 0xdc, 0x3f, |
| 0x0d, 0xc4, 0x7b, 0xba, 0x63, 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, |
| 0x9c, 0x31, 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5}; |
| |
| // AES Test Vectors |
| static unsigned const char kAesCiphertext[32] = { |
| 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, |
| 0xfb, 0x5f, 0x7b, 0xfb, 0xd6, 0x48, 0x5a, 0x5c, 0x81, 0x51, 0x9c, |
| 0xf3, 0x78, 0xfa, 0x36, 0xd4, 0x2b, 0x85, 0x47, 0xed, 0xc0}; |
| |
| static unsigned const char kAesPlaintext[16] = { |
| 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
| 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}; |
| |
| static unsigned const int kAesIvLength = 16; |
| static unsigned const char KAesIv[kAesIvLength] = { |
| 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; |
| |
| static unsigned const char kAesKey[32] = { |
| 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, |
| 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, |
| 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4}; |
| |
| TEST(CryptoOpsTest, TestSalt) { |
| const uint8_t* salt = CryptoOpsTest::Salt(); |
| unique_ptr<ByteBuffer> expected_salt = |
| CryptoOpsTest::Sha256(ByteBuffer("SecureMessage")); |
| |
| EXPECT_EQ(expected_salt->size(), CryptoOpsTest::SaltSize()); |
| EXPECT_TRUE(expected_salt->Equals(ByteBuffer(salt, CryptoOpsTest::SaltSize()))); |
| } |
| |
| TEST(CryptoOpsTest, TestHkdfExtract) { |
| EXPECT_EQ(kHkdfCase1IkmLength, sizeof(kHkdfCase1Ikm)); |
| EXPECT_EQ(kHkdfCase1SaltLength, sizeof(kHkdfCase1Salt)); |
| |
| ByteBuffer key_data(kHkdfCase1Ikm, kHkdfCase1IkmLength); |
| ByteBuffer salt(kHkdfCase1Salt, kHkdfCase1SaltLength); |
| |
| unique_ptr<ByteBuffer> result = |
| CryptoOpsTest::HkdfSha256Extract(key_data, salt); |
| |
| EXPECT_NE(nullptr, result); |
| EXPECT_EQ(kHkdfExtractExpectedLength, result->size()); |
| EXPECT_TRUE(result->Equals( |
| ByteBuffer(kHkdfExtractExpected, kHkdfExtractExpectedLength))); |
| } |
| |
| TEST(CryptoOpsTest, TestHkdf) { |
| EXPECT_EQ(kHkdfCase1IkmLength, sizeof(kHkdfCase1Ikm)); |
| EXPECT_EQ(kHkdfCase1SaltLength, sizeof(kHkdfCase1Salt)); |
| EXPECT_EQ(kHkdfCase1InfoLength, sizeof(kHkdfCase1Info)); |
| |
| ByteBuffer key_data(kHkdfCase1Ikm, kHkdfCase1IkmLength); |
| ByteBuffer salt(kHkdfCase1Salt, kHkdfCase1SaltLength); |
| ByteBuffer info(kHkdfCase1Info, kHkdfCase1InfoLength); |
| |
| unique_ptr<string> result = |
| CryptoOps::Hkdf(key_data.String(), salt.String(), info.String()); |
| |
| EXPECT_NE(nullptr, result); |
| EXPECT_EQ(kHkdfCase1OkmLength, result->length()); |
| EXPECT_TRUE(ByteBuffer(kHkdfCase1Okm, kHkdfCase1OkmLength) |
| .Equals(ByteBuffer(*result))); |
| } |
| |
| TEST(CryptoOpsTest, TestDeriveAes256KeyFor) { |
| CryptoOps::SecretKey aesKey1( |
| string(CryptoOpsTest::AesKeySize(), static_cast<char>(1)), |
| CryptoOps::AES_256_KEY); |
| CryptoOps::SecretKey aesKey2( |
| string(CryptoOpsTest::AesKeySize(), static_cast<char>(2)), |
| CryptoOps::AES_256_KEY); |
| |
| // Test that deriving with the same key and purpose twice is deterministic |
| EXPECT_EQ(CryptoOps::DeriveAes256KeyFor(aesKey1, "A")->data().String(), |
| CryptoOps::DeriveAes256KeyFor(aesKey1, "A")->data().String()); |
| |
| // Test that derived keys with different purposes differ |
| EXPECT_NE(CryptoOps::DeriveAes256KeyFor(aesKey1, "A")->data().String(), |
| CryptoOps::DeriveAes256KeyFor(aesKey1, "B")->data().String()); |
| |
| // Test that derived keys with the same purpose but different master keys |
| // differ |
| EXPECT_NE(CryptoOps::DeriveAes256KeyFor(aesKey1, "A")->data().String(), |
| CryptoOps::DeriveAes256KeyFor(aesKey2, "A")->data().String()); |
| } |
| |
| TEST(CryptoOpsTest, TestAes256CbcEncryptDecrypt) { |
| string plaintext(16, static_cast<char>(1)); |
| string iv(16, static_cast<char>(2)); |
| string key(32, static_cast<char>(3)); |
| CryptoOps::SecretKey aesKey(key, CryptoOps::AES_256_KEY); |
| |
| unique_ptr<ByteBuffer> ciphertext_ptr = CryptoOpsTest::Aes256CBCEncrypt( |
| aesKey, ByteBuffer(iv), ByteBuffer(plaintext)); |
| EXPECT_NE(nullptr, ciphertext_ptr); |
| EXPECT_NE(ciphertext_ptr->String(), plaintext); |
| |
| unique_ptr<ByteBuffer> plaintext_ptr = |
| CryptoOpsTest::Aes256CBCDecrypt(aesKey, ByteBuffer(iv), *ciphertext_ptr); |
| EXPECT_NE(nullptr, plaintext_ptr); |
| EXPECT_EQ(plaintext, plaintext_ptr->String()); |
| } |
| |
| TEST(CryptoOpsTest, TestAes256CbcEncryptDecrypt2) { |
| // First test the encrypt portion using known vectors |
| ByteBuffer plaintext_string(kAesPlaintext, sizeof(kAesPlaintext)); |
| ByteBuffer iv_string(KAesIv, sizeof(KAesIv)); |
| ByteBuffer key_string(kAesKey, sizeof(kAesKey)); |
| CryptoOps::SecretKey aesKey(key_string.String(), CryptoOps::AES_256_KEY); |
| |
| unique_ptr<ByteBuffer> ciphertext_string = |
| CryptoOpsTest::Aes256CBCEncrypt(aesKey, iv_string, plaintext_string); |
| |
| EXPECT_NE(nullptr, ciphertext_string); |
| EXPECT_TRUE(ByteBuffer(kAesCiphertext, sizeof(kAesCiphertext)) |
| .Equals(*ciphertext_string)); |
| |
| // Now test the decrypt portion |
| unique_ptr<ByteBuffer> plaintext_ptr = |
| CryptoOpsTest::Aes256CBCDecrypt(aesKey, iv_string, *ciphertext_string); |
| |
| EXPECT_NE(nullptr, plaintext_ptr); |
| EXPECT_TRUE( |
| plaintext_ptr->Equals(ByteBuffer(kAesPlaintext, sizeof(kAesPlaintext)))); |
| } |
| |
| TEST(CryptoOpsTest, TestEncryptDecrypt) { |
| ByteBuffer plaintext("Hello World!"); |
| ByteBuffer key("5uper 5ecret"); |
| |
| // Create a bogus iv with 16 bytes of the number 42. |
| ByteBuffer iv(string(16, static_cast<char>(42))); |
| |
| CryptoOps::SecretKey aesKey(key.String(), CryptoOps::AES_256_KEY); |
| unique_ptr<string> ciphertext_ptr = CryptoOps::Encrypt( |
| aesKey, CryptoOps::AES_256_CBC, iv.String(), plaintext.String()); |
| |
| EXPECT_NE(nullptr, ciphertext_ptr); |
| EXPECT_NE(plaintext.String(), *ciphertext_ptr); |
| |
| iv = ByteBuffer(string(16, 42)); |
| unique_ptr<string> plaintext_ptr = CryptoOps::Decrypt( |
| aesKey, CryptoOps::AES_256_CBC, iv.String(), *ciphertext_ptr); |
| EXPECT_NE(nullptr, plaintext_ptr); |
| EXPECT_EQ(plaintext.String(), *plaintext_ptr); |
| } |
| |
| TEST(CryptoOpsTest, TestEncryptDecryptEmpty) { |
| ByteBuffer plaintext; |
| ByteBuffer key("5uper 5ecret"); |
| ByteBuffer iv(string(kAesIvLength, static_cast<char>(42))); |
| |
| CryptoOps::SecretKey aesKey(key.String(), CryptoOps::AES_256_KEY); |
| unique_ptr<string> ciphertext_ptr = CryptoOps::Encrypt( |
| aesKey, CryptoOps::AES_256_CBC, iv.String(), plaintext.String()); |
| |
| EXPECT_NE(nullptr, ciphertext_ptr); |
| EXPECT_NE(plaintext.String(), *ciphertext_ptr); |
| |
| iv = ByteBuffer(string(kAesIvLength, static_cast<char>(42))); |
| unique_ptr<string> plaintext_ptr = CryptoOps::Decrypt( |
| aesKey, CryptoOps::AES_256_CBC, iv.String(), *ciphertext_ptr); |
| EXPECT_NE(nullptr, plaintext_ptr); |
| EXPECT_EQ((size_t)0, plaintext_ptr->size()); |
| } |
| |
| TEST(CryptoOpsTest, TestGenerateIV) { |
| unique_ptr<string> iv1 = CryptoOps::GenerateIv(CryptoOps::AES_256_CBC); |
| unique_ptr<string> iv2 = CryptoOps::GenerateIv(CryptoOps::AES_256_CBC); |
| unique_ptr<string> iv3 = CryptoOps::GenerateIv(CryptoOps::NONE); |
| |
| EXPECT_NE(nullptr, iv1); |
| EXPECT_NE(nullptr, iv2); |
| EXPECT_EQ(nullptr, iv3); |
| |
| EXPECT_EQ(kAesIvLength, iv1->length()); |
| EXPECT_EQ(kAesIvLength, iv2->length()); |
| |
| EXPECT_NE(0, iv1->compare(*iv2)); |
| } |
| |
| TEST(CryptoOpsTest, TestNoPurposeConflicts) { |
| // Ensure that signature algorithms and encryption algorithms are not given |
| // identical purposes (this prevents confusion of derived keys). |
| for (int sig_type = 0; sig_type < CryptoOps::SigType::SIG_TYPE_END; |
| sig_type++) { |
| for (int enc_type = 0; enc_type < CryptoOps::EncType::ENC_TYPE_END; |
| enc_type++) { |
| EXPECT_FALSE(CryptoOpsTest::GetPurpose((CryptoOps::SigType)sig_type) == |
| CryptoOpsTest::GetPurpose((CryptoOps::EncType)enc_type)); |
| } |
| } |
| } |
| |
| TEST(CryptoOpsTest, TestSignVerifyHmacSha256) { |
| // Test Case 1 from RFC4231 |
| // Note that we can't just use the standard expected signature since our |
| // sign() function internally derives a key from the original key. So |
| // instead, we're testing the output against a known good output from the |
| // Java implementation of securemessage. |
| string key_string(20, static_cast<char>(0x0b)); |
| string data_string("Hi There"); |
| unsigned char expected_signature[32] = { |
| 0x3b, 0x14, 0x7b, 0x0f, 0xe6, 0x6a, 0x00, 0x47, 0xa2, 0x60, 0x4c, |
| 0xf2, 0x64, 0x29, 0xad, 0x07, 0x5d, 0x86, 0x8b, 0x01, 0xdb, 0x11, |
| 0xef, 0x6f, 0x4e, 0xc2, 0x2d, 0x8b, 0xdb, 0x66, 0xf1, 0x8c}; |
| CryptoOps::SecretKey secret_key(key_string, |
| CryptoOps::KeyAlgorithm::AES_256_KEY); |
| unique_ptr<string> signature = |
| CryptoOps::Sign(CryptoOps::SigType::HMAC_SHA256, secret_key, data_string); |
| EXPECT_NE(nullptr, signature); |
| EXPECT_EQ((size_t)32, signature->length()); |
| EXPECT_TRUE( |
| ByteBuffer(expected_signature, 32).Equals(ByteBuffer(*signature))); |
| |
| EXPECT_TRUE(CryptoOps::Verify(CryptoOps::SigType::HMAC_SHA256, secret_key, |
| *signature, data_string)); |
| } |
| |
| TEST(CryptoOpsTest, TestSignVerifyEcdsaP256sha256) { |
| // We don't really test anything other than a signature is generated |
| // and verified with no errors |
| // We use sample pregenerated keys. Below are their byte representations in |
| // PKCS8 format |
| unsigned const char public_key_bytes[335] = { |
| 0x30, 0x82, 0x01, 0x4b, 0x30, 0x82, 0x01, 0x03, 0x06, 0x07, 0x2a, 0x86, |
| 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30, 0x81, 0xf7, 0x02, 0x01, 0x01, 0x30, |
| 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, |
| 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x30, 0x5b, 0x04, |
| 0x20, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x04, 0x20, 0x5a, |
| 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd, 0x55, 0x76, |
| 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6, 0x3b, |
| 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b, 0x03, 0x15, 0x00, 0xc4, 0x9d, |
| 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78, 0xe1, 0x13, 0x9d, |
| 0x26, 0xb7, 0x81, 0x9f, 0x7e, 0x90, 0x04, 0x41, 0x04, 0x6b, 0x17, 0xd1, |
| 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, |
| 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, |
| 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, |
| 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, |
| 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, |
| 0xf5, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad, |
| 0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51, |
| 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04, 0xa9, 0xa3, 0x76, 0xc2, 0xb0, |
| 0x0d, 0xa1, 0x4a, 0xe8, 0x60, 0x1f, 0x19, 0xc7, 0xbf, 0x1f, 0x6c, 0x34, |
| 0xfd, 0x7b, 0xe1, 0x20, 0x37, 0x49, 0x20, 0x4a, 0xcd, 0xec, 0x1d, 0x4a, |
| 0xc3, 0x71, 0xa1, 0xbb, 0x2f, 0xdf, 0x5d, 0x03, 0xcd, 0xe4, 0x25, 0x33, |
| 0xd3, 0x6e, 0xc3, 0xf9, 0xe4, 0x92, 0xf5, 0x21, 0x9b, 0x41, 0xf1, 0xb3, |
| 0x61, 0x5a, 0xa0, 0xc4, 0xe8, 0xf7, 0x67, 0x22, 0xac, 0x44, 0xcb}; |
| |
| unsigned const char private_key_bytes[381] = { |
| 0x30, 0x82, 0x01, 0x79, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x03, 0x06, |
| 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30, 0x81, 0xf7, 0x02, |
| 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, |
| 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 0x30, 0x5b, 0x04, 0x20, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, |
| 0x04, 0x20, 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, |
| 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, |
| 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b, 0x03, 0x15, |
| 0x00, 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78, |
| 0xe1, 0x13, 0x9d, 0x26, 0xb7, 0x81, 0x9f, 0x7e, 0x90, 0x04, 0x41, 0x04, |
| 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, |
| 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, |
| 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2, |
| 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, |
| 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, |
| 0x37, 0xbf, 0x51, 0xf5, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, |
| 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, |
| 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, |
| 0x63, 0x25, 0x51, 0x02, 0x01, 0x01, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, |
| 0x01, 0x04, 0x20, 0x1a, 0xf4, 0x39, 0x88, 0x75, 0x66, 0x91, 0xef, 0xbe, |
| 0x57, 0xfa, 0xf5, 0x75, 0xd4, 0xc1, 0xc8, 0xbb, 0x8e, 0xb2, 0x0a, 0x59, |
| 0xc3, 0x89, 0xd1, 0x8b, 0xc6, 0x2b, 0xa5, 0x0c, 0x98, 0xc7, 0x48, 0xa1, |
| 0x44, 0x03, 0x42, 0x00, 0x04, 0xa9, 0xa3, 0x76, 0xc2, 0xb0, 0x0d, 0xa1, |
| 0x4a, 0xe8, 0x60, 0x1f, 0x19, 0xc7, 0xbf, 0x1f, 0x6c, 0x34, 0xfd, 0x7b, |
| 0xe1, 0x20, 0x37, 0x49, 0x20, 0x4a, 0xcd, 0xec, 0x1d, 0x4a, 0xc3, 0x71, |
| 0xa1, 0xbb, 0x2f, 0xdf, 0x5d, 0x03, 0xcd, 0xe4, 0x25, 0x33, 0xd3, 0x6e, |
| 0xc3, 0xf9, 0xe4, 0x92, 0xf5, 0x21, 0x9b, 0x41, 0xf1, 0xb3, 0x61, 0x5a, |
| 0xa0, 0xc4, 0xe8, 0xf7, 0x67, 0x22, 0xac, 0x44, 0xcb}; |
| |
| string data_string("Hi There"); |
| |
| // Test Signing |
| CryptoOps::PrivateKey private_key(string((const char*)private_key_bytes, 381), |
| CryptoOps::KeyAlgorithm::ECDSA_KEY); |
| unique_ptr<string> signature = CryptoOps::Sign( |
| CryptoOps::SigType::ECDSA_P256_SHA256, private_key, data_string); |
| EXPECT_NE(nullptr, signature); |
| |
| // Test Verifying |
| CryptoOps::PublicKey public_key(string((const char*)public_key_bytes, 335), |
| CryptoOps::KeyAlgorithm::ECDSA_KEY); |
| bool signature_verifies = |
| CryptoOps::Verify(CryptoOps::SigType::ECDSA_P256_SHA256, public_key, |
| *signature, data_string); |
| EXPECT_TRUE(signature_verifies); |
| } |
| |
| TEST(CryptoOpsTest, TestSignVerifyRsa2048Sha256) { |
| // We don't really test anything other than a signature is generated |
| // and no error occurs |
| |
| // We use a valid pre-computed keys |
| const uint8_t private_key_bytes[1193] = { |
| 0x30, 0x82, 0x04, 0xa5, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, |
| 0x99, 0xa5, 0x48, 0x16, 0x40, 0x19, 0x1e, 0xcc, 0x78, 0xcf, 0x53, 0xd0, |
| 0x61, 0x83, 0x3b, 0xfc, 0x5d, 0xb9, 0x2e, 0x9a, 0xfa, 0x04, 0x10, 0xfb, |
| 0x65, 0xd1, 0xef, 0x7b, 0x2c, 0x74, 0x25, 0xf3, 0x79, 0x6a, 0xf6, 0x3f, |
| 0xe7, 0x36, 0xbd, 0x55, 0xb4, 0x96, 0x0b, 0x95, 0x8e, 0x07, 0x1a, 0xbd, |
| 0x35, 0x44, 0xe5, 0xf8, 0x6f, 0xd5, 0xa7, 0x85, 0x77, 0x43, 0xf9, 0xa4, |
| 0x1a, 0x78, 0xb8, 0xcf, 0x62, 0xc8, 0x2a, 0x88, 0x47, 0x3a, 0x4f, 0xd6, |
| 0x3b, 0x21, 0x2b, 0xb2, 0x6e, 0xea, 0x13, 0xdd, 0xde, 0xf4, 0x55, 0xf4, |
| 0x4b, 0x57, 0x11, 0xf3, 0xb3, 0x92, 0xd6, 0xce, 0x57, 0x28, 0x9e, 0x85, |
| 0xba, 0x79, 0x3b, 0xbb, 0x0b, 0x35, 0x7a, 0xa4, 0x69, 0x84, 0x1f, 0xbe, |
| 0x89, 0x35, 0xbd, 0x02, 0x25, 0x28, 0xec, 0x6f, 0xeb, 0x3d, 0x0a, 0xb0, |
| 0x02, 0x6b, 0x4b, 0xdb, 0xd2, 0x7c, 0x4e, 0xed, 0x44, 0x4f, 0xa1, 0xe6, |
| 0xd9, 0xc4, 0xf7, 0xe7, 0x7d, 0x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0x3b, |
| 0x76, 0x02, 0xd6, 0xfd, 0xa3, 0x26, 0xe5, 0xdd, 0x9d, 0x80, 0xcd, 0x7e, |
| 0xf9, 0x81, 0x87, 0xb0, 0xaf, 0x1e, 0x8c, 0xbc, 0xae, 0xc2, 0x0a, 0x47, |
| 0xb3, 0x9f, 0x29, 0x9c, 0x69, 0x8b, 0x02, 0x81, 0x80, 0x6f, 0xa1, 0xb9, |
| 0xeb, 0x3c, 0xea, 0xed, 0x30, 0x1f, 0x39, 0x3b, 0x36, 0x38, 0x41, 0x74, |
| 0xad, 0xae, 0x9e, 0x40, 0x3e, 0x45, 0xc2, 0x4a, 0x47, 0x9b, 0x71, 0x5a, |
| 0xab, 0xb0, 0x97, 0x98, 0x90, 0x22, 0x35, 0x87, 0x56, 0xcf, 0x0a, 0xf9, |
| 0x52, 0x4b, 0x30, 0x90, 0x03, 0x77, 0x5c, 0x6c, 0x9a, 0x9e, 0x77, 0xf9, |
| 0x6e, 0x87, 0x3a, 0xb5, 0x18, 0x23, 0xcb, 0xde, 0xec, 0xe3, 0xa2, 0xb3, |
| 0x57, 0xb7, 0xd1, 0xd1, 0xb9, 0x3d, 0xaa, 0x33, 0xee, 0x38, 0x88, 0xf5, |
| 0x03, 0x47, 0xde, 0x57, 0xd7, 0x43, 0xa8, 0x28, 0x24, 0xd5, 0xab, 0x19, |
| 0xf1, 0x80, 0xf9, 0x84, 0x42, 0xc8, 0xa7, 0xe9, 0xd0, 0xf3, 0xb0, 0x4d, |
| 0xd4, 0x06, 0xb9, 0xcb, 0x22, 0x2a, 0x37, 0x98, 0xc0, 0x40, 0x2b, 0x6b, |
| 0x3b, 0xef, 0x86, 0xcc, 0x30, 0x2d, 0xbc, 0xae, 0x4c, 0x82, 0x6f, 0xd2, |
| 0x28, 0x4b, 0xba, 0xb4, 0xfb}; |
| |
| const uint8_t public_key_bytes[294] = { |
| 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 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x35, 0x5a, 0xad, 0x0f, 0x1a, |
| 0x9c, 0x70, 0x98, 0xcb, 0xfc, 0x48, 0xab, 0xf5, 0xc6, 0xa4, 0x94, 0xa6, |
| 0x9b, 0x64, 0x06, 0x0e, 0xb9, 0x38, 0xa9, 0x84, 0x32, 0x38, 0x13, 0x84, |
| 0x70, 0x99, 0x79, 0x25, 0xfe, 0x2c, 0xc0, 0xde, 0xec, 0x9f, 0x99, 0x51, |
| 0x4f, 0xfc, 0xac, 0xe8, 0x0d, 0x87, 0x6d, 0x43, 0x8b, 0x25, 0xa7, 0x54, |
| 0x47, 0x42, 0xfe, 0xe2, 0x67, 0xff, 0xc2, 0x8f, 0x1b, 0x0e, 0x1a, 0x24, |
| 0xdf, 0xa7, 0x40, 0xcd, 0xa4, 0xa2, 0xb4, 0xad, 0x43, 0x72, 0xbc, 0x8d, |
| 0xc0, 0x1d, 0xe0, 0x77, 0x76, 0x4b, 0xe0, 0xc2, 0x32, 0x7a, 0x57, 0x33, |
| 0xee, 0xa8, 0x9c, 0xfa, 0x49, 0x67, 0x42, 0x15, 0x31, 0x0e, 0xd0, 0xc2, |
| 0x7b, 0x02, 0x03, 0x01, 0x00, 0x01}; |
| |
| string data_string("Hi There"); |
| |
| // Test Signing |
| CryptoOps::PrivateKey private_key( |
| ByteBuffer(private_key_bytes, 1193).String(), |
| CryptoOps::KeyAlgorithm::RSA_KEY); |
| unique_ptr<string> signature = CryptoOps::Sign( |
| CryptoOps::SigType::RSA2048_SHA256, private_key, data_string); |
| EXPECT_NE(nullptr, signature); |
| |
| // Test Verifying |
| CryptoOps::PublicKey public_key(ByteBuffer(public_key_bytes, 294).String(), |
| CryptoOps::KeyAlgorithm::RSA_KEY); |
| bool signature_verifies = CryptoOps::Verify( |
| CryptoOps::SigType::RSA2048_SHA256, public_key, *signature, data_string); |
| EXPECT_TRUE(signature_verifies); |
| } |
| |
| TEST(CryptoOpsTest, TestEcKeyAgreement) { |
| unique_ptr<CryptoOps::KeyPair> client_key_pair = |
| CryptoOps::GenerateEcP256KeyPair(); |
| unique_ptr<CryptoOps::KeyPair> server_key_pair = |
| CryptoOps::GenerateEcP256KeyPair(); |
| |
| string client_y, server_y; |
| bool success; |
| success = CryptoOps::ExportEcP256Key(*(client_key_pair->public_key), |
| NULL, &client_y); |
| ASSERT_TRUE(success); |
| success = CryptoOps::ExportEcP256Key(*(server_key_pair->public_key), |
| NULL, &server_y); |
| ASSERT_TRUE(success); |
| // Public Keys should not be equal. |
| ASSERT_NE(client_y, server_y); |
| |
| // Run client side of the key exchange |
| unique_ptr<CryptoOps::SecretKey> client_secret = |
| CryptoOps::KeyAgreementSha256(*(client_key_pair->private_key), |
| *(server_key_pair->public_key)); |
| ASSERT_NE(client_secret, nullptr); |
| |
| // Run the server side of the key exchange |
| unique_ptr<CryptoOps::SecretKey> server_secret = |
| CryptoOps::KeyAgreementSha256(*(server_key_pair->private_key), |
| *(client_key_pair->public_key)); |
| ASSERT_NE(server_secret, nullptr); |
| |
| ASSERT_TRUE(client_secret->data().Equals(server_secret->data())); |
| } |
| |
| |
| TEST(CryptoOpsTest, TestGenerateAes256SecretKey) { |
| unique_ptr<CryptoOps::SecretKey> key = CryptoOps::GenerateAes256SecretKey(); |
| EXPECT_NE(key, nullptr); |
| EXPECT_EQ(CryptoOpsTest::AesKeySize(), key->data().size()); |
| } |
| |
| TEST(CryptoOpsTest, TestGenerateEcP256KeyPair) { |
| unique_ptr<CryptoOps::KeyPair> key_pair = CryptoOps::GenerateEcP256KeyPair(); |
| EXPECT_NE(key_pair, nullptr); |
| EXPECT_GT(key_pair->private_key->data().size(), 0U); |
| EXPECT_GT(key_pair->public_key->data().size(), 0U); |
| } |
| |
| TEST(CryptoOpsTest, TestGenerateRsa2048KeyPair) { |
| unique_ptr<CryptoOps::KeyPair> key_pair = CryptoOps::GenerateRsa2048KeyPair(); |
| EXPECT_NE(key_pair, nullptr); |
| EXPECT_GT(key_pair->private_key->data().size(), 0U); |
| EXPECT_GT(key_pair->public_key->data().size(), 0U); |
| } |
| |
| TEST(CryptoOpsTest, TestInt32BytesToString) { |
| EXPECT_EQ(string("\x05\xF2\x23\x00", 4), |
| CryptoOpsTest::Int32BytesToString(0x05F22300)); |
| |
| // All zero bytes. |
| EXPECT_EQ(string("\x0\x0\x0\x0", 4), CryptoOpsTest::Int32BytesToString(0)); |
| |
| // Leading 0 byte. |
| EXPECT_EQ(string("\x00\x00\x93\x06", 4), |
| CryptoOpsTest::Int32BytesToString(0x00009306)); |
| |
| // Negative value. |
| EXPECT_EQ(string("\xE0\x00\x00\x00", 4), |
| CryptoOpsTest::Int32BytesToString(0xE0000000)); |
| |
| // Negative non-leading bytes. |
| EXPECT_EQ(string("\x0F\x81\xA3\x99", 4), |
| CryptoOpsTest::Int32BytesToString(0x0F81A399)); |
| } |
| |
| TEST(CryptoOpsTest, TestStringToInt32Bytes) { |
| // Initially set to an arbitrary value; |
| int32_t result = 0x34F4EE21; |
| |
| // Empty string. |
| EXPECT_TRUE(CryptoOpsTest::StringToInt32Bytes(string(), &result)); |
| EXPECT_EQ(0, result); |
| |
| // All zero bytes. |
| EXPECT_TRUE(CryptoOpsTest::StringToInt32Bytes(string("\x00\x00\x00\x00", 4), |
| &result)); |
| EXPECT_EQ(0, result); |
| |
| // Positive value. |
| EXPECT_TRUE(CryptoOpsTest::StringToInt32Bytes(string("\x05\xF2\x23\x00", 4), |
| &result)); |
| EXPECT_EQ(0x05F22300, result); |
| |
| // Leading 0 byte. |
| EXPECT_TRUE(CryptoOpsTest::StringToInt32Bytes(string("\x00\x00\x93\x06", 4), |
| &result)); |
| EXPECT_EQ(0x00009306, result); |
| |
| // Negative value. |
| EXPECT_TRUE(CryptoOpsTest::StringToInt32Bytes(string("\xE0\x00\x00\x00", 4), |
| &result)); |
| EXPECT_EQ(0xE0000000, result); |
| |
| // Negative non-leading bytes. |
| EXPECT_TRUE(CryptoOpsTest::StringToInt32Bytes(string("\x0F\x81\xA3\x99", 4), |
| &result)); |
| EXPECT_EQ(0x0F81A399, result); |
| |
| // String shorter than 4 bytes. |
| EXPECT_TRUE(CryptoOpsTest::StringToInt32Bytes(string("\x81\xA3\x99", 3), |
| &result)); |
| EXPECT_EQ(0x0081A399, result); |
| |
| // String longer than 4 bytes (invalid value). |
| EXPECT_FALSE( |
| CryptoOpsTest::StringToInt32Bytes(string("\x00\x00\x81\xA3\x99", 5), |
| &result)); |
| } |
| |
| } // namespace |
| } // namespace unittesting |
| } // namespace securemessage |