1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/multidevice/fake_secure_message_delegate.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash::multidevice {
namespace {
const char kTestPublicKey[] = "the private key is in another castle";
const char kPayload[] = "500 tons of uranium";
const char kSymmetricKey[] = "hunter2";
const char kPublicMetadata[] = "brought to you by our sponsors";
const char kAssociatedData[] = "save 20% bytes on your nonce insurance";
const char kVerificationKeyId[] = "the one with the red stripes";
const char kDecryptionKeyId[] = "it's in your pocket somewhere";
// Callback for saving the result of GenerateKeys().
void SaveKeyPair(std::string* private_key_out,
std::string* public_key_out,
const std::string& private_key,
const std::string& public_key) {
*private_key_out = private_key;
*public_key_out = public_key;
}
// Callback for saving the result of DeriveKey() and CreateSecureMessage().
void SaveString(std::string* out, const std::string& value) {
*out = value;
}
// Callback for saving the result of UnwrapSecureMessage().
void SaveUnwrapResults(std::string* payload_out,
securemessage::Header* header_out,
bool verified,
const std::string& payload,
const securemessage::Header& header) {
ASSERT_TRUE(verified);
*payload_out = payload;
*header_out = header;
}
// Returns the CreateOptions struct to create the test message.
SecureMessageDelegate::CreateOptions GetCreateOptions(
securemessage::EncScheme encryption_scheme,
securemessage::SigScheme signature_scheme) {
SecureMessageDelegate::CreateOptions create_options;
create_options.encryption_scheme = encryption_scheme;
create_options.signature_scheme = signature_scheme;
create_options.public_metadata = kPublicMetadata;
create_options.associated_data = kAssociatedData;
create_options.verification_key_id = kVerificationKeyId;
create_options.decryption_key_id = kDecryptionKeyId;
return create_options;
}
// Returns the UnwrapOptions struct to unwrap the test message.
SecureMessageDelegate::UnwrapOptions GetUnwrapOptions(
securemessage::EncScheme encryption_scheme,
securemessage::SigScheme signature_scheme) {
SecureMessageDelegate::UnwrapOptions unwrap_options;
unwrap_options.encryption_scheme = encryption_scheme;
unwrap_options.signature_scheme = signature_scheme;
unwrap_options.associated_data = kAssociatedData;
return unwrap_options;
}
void CheckSerializedSecureMessage(
const std::string& serialized_message,
const SecureMessageDelegate::CreateOptions& create_options) {
securemessage::SecureMessage secure_message;
ASSERT_TRUE(secure_message.ParseFromString(serialized_message));
securemessage::HeaderAndBody header_and_body;
ASSERT_TRUE(
header_and_body.ParseFromString(secure_message.header_and_body()));
const securemessage::Header& header = header_and_body.header();
EXPECT_EQ(create_options.signature_scheme, header.signature_scheme());
EXPECT_EQ(create_options.encryption_scheme, header.encryption_scheme());
EXPECT_EQ(create_options.verification_key_id, header.verification_key_id());
EXPECT_EQ(create_options.decryption_key_id, header.decryption_key_id());
EXPECT_EQ(create_options.public_metadata, header.public_metadata());
}
} // namespace
class CryptAuthFakeSecureMessageDelegateTest : public testing::Test {
public:
CryptAuthFakeSecureMessageDelegateTest(
const CryptAuthFakeSecureMessageDelegateTest&) = delete;
CryptAuthFakeSecureMessageDelegateTest& operator=(
const CryptAuthFakeSecureMessageDelegateTest&) = delete;
protected:
CryptAuthFakeSecureMessageDelegateTest() {}
FakeSecureMessageDelegate delegate_;
};
TEST_F(CryptAuthFakeSecureMessageDelegateTest, GenerateKeyPair) {
std::string public_key1, private_key1;
delegate_.GenerateKeyPair(
base::BindOnce(&SaveKeyPair, &public_key1, &private_key1));
EXPECT_NE(private_key1, public_key1);
std::string public_key2, private_key2;
delegate_.GenerateKeyPair(
base::BindOnce(&SaveKeyPair, &public_key2, &private_key2));
EXPECT_NE(private_key2, public_key2);
EXPECT_NE(public_key1, public_key2);
EXPECT_NE(private_key1, private_key2);
delegate_.set_next_public_key(kTestPublicKey);
std::string public_key3, private_key3;
delegate_.GenerateKeyPair(
base::BindOnce(&SaveKeyPair, &public_key3, &private_key3));
EXPECT_EQ(kTestPublicKey, public_key3);
EXPECT_NE(private_key3, public_key3);
EXPECT_NE(public_key1, public_key3);
EXPECT_NE(private_key1, private_key3);
}
TEST_F(CryptAuthFakeSecureMessageDelegateTest, DeriveKey) {
delegate_.set_next_public_key("key_pair_1");
std::string public_key1, private_key1;
delegate_.GenerateKeyPair(
base::BindOnce(&SaveKeyPair, &public_key1, &private_key1));
delegate_.set_next_public_key("key_pair_2");
std::string public_key2, private_key2;
delegate_.GenerateKeyPair(
base::BindOnce(&SaveKeyPair, &public_key2, &private_key2));
std::string symmetric_key1, symmetric_key2;
delegate_.DeriveKey(private_key1, public_key2,
base::BindOnce(&SaveString, &symmetric_key1));
delegate_.DeriveKey(private_key2, public_key1,
base::BindOnce(&SaveString, &symmetric_key2));
EXPECT_EQ(symmetric_key1, symmetric_key2);
}
TEST_F(CryptAuthFakeSecureMessageDelegateTest,
CreateAndUnwrapWithSymmetricKey) {
// Create SecureMessage using symmetric key.
SecureMessageDelegate::CreateOptions create_options =
GetCreateOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256);
std::string serialized_message;
delegate_.CreateSecureMessage(
kPayload, kSymmetricKey, create_options,
base::BindOnce(&SaveString, &serialized_message));
CheckSerializedSecureMessage(serialized_message, create_options);
// Unwrap SecureMessage using symmetric key.
SecureMessageDelegate::UnwrapOptions unwrap_options =
GetUnwrapOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256);
std::string payload;
securemessage::Header header;
delegate_.UnwrapSecureMessage(
serialized_message, kSymmetricKey, unwrap_options,
base::BindOnce(&SaveUnwrapResults, &payload, &header));
EXPECT_EQ(kPayload, payload);
}
TEST_F(CryptAuthFakeSecureMessageDelegateTest,
CreateAndUnwrapWithAsymmetricKey) {
delegate_.set_next_public_key(kTestPublicKey);
std::string public_key, private_key;
delegate_.GenerateKeyPair(
base::BindOnce(&SaveKeyPair, &public_key, &private_key));
// Create SecureMessage using asymmetric key.
SecureMessageDelegate::CreateOptions create_options =
GetCreateOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256);
std::string serialized_message;
delegate_.CreateSecureMessage(
kPayload, private_key, create_options,
base::BindOnce(&SaveString, &serialized_message));
CheckSerializedSecureMessage(serialized_message, create_options);
// Unwrap SecureMessage using symmetric key.
SecureMessageDelegate::UnwrapOptions unwrap_options =
GetUnwrapOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256);
std::string payload;
securemessage::Header header;
delegate_.UnwrapSecureMessage(
serialized_message, public_key, unwrap_options,
base::BindOnce(&SaveUnwrapResults, &payload, &header));
EXPECT_EQ(kPayload, payload);
}
TEST_F(CryptAuthFakeSecureMessageDelegateTest, GetPrivateKeyForPublicKey) {
delegate_.set_next_public_key(kTestPublicKey);
std::string public_key, private_key;
delegate_.GenerateKeyPair(
base::BindOnce(&SaveKeyPair, &public_key, &private_key));
EXPECT_EQ(kTestPublicKey, public_key);
EXPECT_EQ(private_key, delegate_.GetPrivateKeyForPublicKey(kTestPublicKey));
}
} // namespace ash::multidevice
|