| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/proximity_auth/cryptauth/fake_secure_message_delegate.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/md5.h" | |
| 13 #include "base/strings/string_util.h" | |
| 14 | |
| 15 namespace proximity_auth { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const char kKeyPrefix[] = "fake_key_"; | |
| 20 const char kPrivateKeyPrefix[] = "private_"; | |
| 21 | |
| 22 // Encrypts the |plaintext| with the |key| using the |encryption_scheme| and | |
| 23 // returns the ciphertext. | |
| 24 std::string Encrypt(const std::string& plaintext, | |
| 25 const std::string& key, | |
| 26 const securemessage::EncScheme& encryption_scheme) { | |
| 27 if (encryption_scheme == securemessage::NONE) | |
| 28 return plaintext; | |
| 29 | |
| 30 // Encrypt with a Vigenere cipher. | |
| 31 std::string ciphertext; | |
| 32 ciphertext.resize(plaintext.size()); | |
| 33 for (size_t i = 0; i < plaintext.size(); ++i) { | |
| 34 unsigned char plaintext_char = plaintext[i]; | |
| 35 unsigned char key_char = key[i % key.size()]; | |
| 36 ciphertext[i] = plaintext_char + key_char; | |
| 37 } | |
| 38 return ciphertext; | |
| 39 } | |
| 40 | |
| 41 // Decrypts the |ciphertext| with the |key| using the |encryption_scheme| and | |
| 42 // returns the plaintext. | |
| 43 std::string Decrypt(const std::string& ciphertext, | |
| 44 const std::string& key, | |
| 45 const securemessage::EncScheme& encryption_scheme) { | |
| 46 if (encryption_scheme == securemessage::NONE) | |
| 47 return ciphertext; | |
| 48 | |
| 49 // Decrypt with a Vigenere cipher. | |
| 50 std::string plaintext; | |
| 51 plaintext.resize(ciphertext.size()); | |
| 52 for (size_t i = 0; i < ciphertext.size(); ++i) { | |
| 53 unsigned char ciphertext_char = ciphertext[i]; | |
| 54 unsigned char key_char = key[i % key.size()]; | |
| 55 plaintext[i] = ciphertext_char - key_char; | |
| 56 } | |
| 57 return plaintext; | |
| 58 } | |
| 59 | |
| 60 // Signs the |payload| and |associated_data| with the |key| using the | |
| 61 // |signature_scheme| and returns the signature. | |
| 62 std::string Sign(const std::string& payload, | |
| 63 const std::string& associated_data, | |
| 64 const std::string& key) { | |
| 65 return base::MD5String(payload + "|" + associated_data + "|" + key); | |
| 66 } | |
| 67 | |
| 68 // Verifies that the |signature| for the the |payload| and |associated_data| is | |
| 69 // valid for the given the |key| and |signature_scheme|. | |
| 70 bool Verify(const std::string& signature, | |
| 71 const std::string& payload, | |
| 72 const std::string& associated_data, | |
| 73 const std::string& key, | |
| 74 const securemessage::SigScheme& signature_scheme) { | |
| 75 std::string signing_key; | |
| 76 | |
| 77 if (signature_scheme == securemessage::HMAC_SHA256) { | |
| 78 signing_key = key; | |
| 79 } else { | |
| 80 std::string prefix = kPrivateKeyPrefix; | |
| 81 bool is_private_key = | |
| 82 base::StartsWith(key, prefix, base::CompareCase::SENSITIVE); | |
| 83 signing_key = is_private_key ? key.substr(prefix.size()) : prefix + key; | |
| 84 } | |
| 85 | |
| 86 std::string expected_signature = Sign(payload, associated_data, signing_key); | |
| 87 return signature == expected_signature; | |
| 88 } | |
| 89 | |
| 90 } // namespace | |
| 91 | |
| 92 FakeSecureMessageDelegate::FakeSecureMessageDelegate() | |
| 93 : next_public_key_(std::string(kKeyPrefix) + "0") { | |
| 94 } | |
| 95 | |
| 96 FakeSecureMessageDelegate::~FakeSecureMessageDelegate() { | |
| 97 } | |
| 98 | |
| 99 void FakeSecureMessageDelegate::GenerateKeyPair( | |
| 100 const GenerateKeyPairCallback& callback) { | |
| 101 std::string public_key = next_public_key_; | |
| 102 | |
| 103 // The private key is simply the public key prepended with "private_". | |
| 104 std::string private_key(kPrivateKeyPrefix + public_key); | |
| 105 | |
| 106 next_public_key_ = std::string(kKeyPrefix) + base::MD5String(public_key); | |
| 107 | |
| 108 callback.Run(public_key, private_key); | |
| 109 } | |
| 110 | |
| 111 void FakeSecureMessageDelegate::DeriveKey(const std::string& private_key, | |
| 112 const std::string& public_key, | |
| 113 const DeriveKeyCallback& callback) { | |
| 114 // To ensure that the same symmetric key is derived for DeriveKey(private1, | |
| 115 // public2) and DeriveKey(private2, public1), we remove the prefix from the | |
| 116 // private key so it is equal to its corresponding public key. | |
| 117 std::string prefix(kPrivateKeyPrefix); | |
| 118 std::string normalized_private_key = | |
| 119 base::StartsWith(private_key, prefix, base::CompareCase::SENSITIVE) | |
| 120 ? private_key.substr(prefix.size()) | |
| 121 : private_key; | |
| 122 | |
| 123 std::vector<std::string> keys; | |
| 124 keys.push_back(normalized_private_key); | |
| 125 keys.push_back(public_key); | |
| 126 std::sort(keys.begin(), keys.end()); | |
| 127 callback.Run(base::MD5String(keys[0] + "|" + keys[1])); | |
| 128 } | |
| 129 | |
| 130 void FakeSecureMessageDelegate::CreateSecureMessage( | |
| 131 const std::string& payload, | |
| 132 const std::string& key, | |
| 133 const CreateOptions& create_options, | |
| 134 const CreateSecureMessageCallback& callback) { | |
| 135 securemessage::Header header; | |
| 136 header.set_signature_scheme(create_options.signature_scheme); | |
| 137 header.set_encryption_scheme(create_options.encryption_scheme); | |
| 138 if (!create_options.public_metadata.empty()) | |
| 139 header.set_public_metadata(create_options.public_metadata); | |
| 140 if (!create_options.verification_key_id.empty()) | |
| 141 header.set_verification_key_id(create_options.verification_key_id); | |
| 142 if (!create_options.decryption_key_id.empty()) | |
| 143 header.set_decryption_key_id(create_options.decryption_key_id); | |
| 144 | |
| 145 securemessage::HeaderAndBody header_and_body; | |
| 146 header_and_body.mutable_header()->CopyFrom(header); | |
| 147 header_and_body.set_body( | |
| 148 Encrypt(payload, key, create_options.encryption_scheme)); | |
| 149 std::string serialized_header_and_body; | |
| 150 header_and_body.SerializeToString(&serialized_header_and_body); | |
| 151 | |
| 152 securemessage::SecureMessage secure_message; | |
| 153 secure_message.set_header_and_body(serialized_header_and_body); | |
| 154 secure_message.set_signature( | |
| 155 Sign(payload, create_options.associated_data, key)); | |
| 156 | |
| 157 std::string serialized_secure_message; | |
| 158 secure_message.SerializeToString(&serialized_secure_message); | |
| 159 callback.Run(serialized_secure_message); | |
| 160 } | |
| 161 | |
| 162 void FakeSecureMessageDelegate::UnwrapSecureMessage( | |
| 163 const std::string& serialized_message, | |
| 164 const std::string& key, | |
| 165 const UnwrapOptions& unwrap_options, | |
| 166 const UnwrapSecureMessageCallback& callback) { | |
| 167 securemessage::SecureMessage secure_message; | |
| 168 if (!secure_message.ParseFromString(serialized_message)) { | |
| 169 LOG(ERROR) << "Failed to parse SecureMessage."; | |
| 170 callback.Run(false, std::string(), securemessage::Header()); | |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 securemessage::HeaderAndBody header_and_body; | |
| 175 if (!header_and_body.ParseFromString(secure_message.header_and_body())) { | |
| 176 LOG(ERROR) << "Failed to parse secure message HeaderAndBody."; | |
| 177 callback.Run(false, std::string(), securemessage::Header()); | |
| 178 return; | |
| 179 } | |
| 180 | |
| 181 const securemessage::Header& header = header_and_body.header(); | |
| 182 std::string payload = | |
| 183 Decrypt(header_and_body.body(), key, unwrap_options.encryption_scheme); | |
| 184 | |
| 185 bool verified = Verify(secure_message.signature(), payload, | |
| 186 unwrap_options.associated_data, key, | |
| 187 unwrap_options.signature_scheme); | |
| 188 if (verified) { | |
| 189 callback.Run(true, payload, header); | |
| 190 } else { | |
| 191 callback.Run(false, std::string(), securemessage::Header()); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 std::string FakeSecureMessageDelegate::GetPrivateKeyForPublicKey( | |
| 196 const std::string& public_key) { | |
| 197 return kPrivateKeyPrefix + public_key; | |
| 198 } | |
| 199 | |
| 200 } // namespace proximity_auth | |
| OLD | NEW |