Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: chromeos/dbus/fake_easy_unlock_client.cc

Issue 513013003: Add unit tests for easyUnlockPrivate API - crypto methods (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/json/json_string_value_serializer.h"
6 #include "base/strings/stringprintf.h"
5 #include "chromeos/dbus/fake_easy_unlock_client.h" 7 #include "chromeos/dbus/fake_easy_unlock_client.h"
6 8
9 namespace {
10
11 // Keys generated using |GenerateEcP256KeyPair| are in the following format:
12 // "{<key_type>: <key_pair_index>}".
13 // <key_pair_index> is an integer identifying
14 // the key pair.
15 // <key_type> specifies whether the key is public or private. It can have one
16 // of the following valies:
17 const char kEc256PrivateKeyKey[] = "ec_p256_private_key";
18 const char kEc256PublicKeyKey[] = "ec_p256_public_key";
19
20 // Extracts key pair index from a key in format "<key_type>: <key_pair_index>}".
21 int ExtractKeyPairIndexFromKey(const std::string& key,
22 const std::string& key_type) {
23 JSONStringValueSerializer serializer(key);
24 scoped_ptr<base::Value> json_value(serializer.Deserialize(NULL, NULL));
25 if (!json_value)
26 return -1;
27
28 base::DictionaryValue* json_dictionary = NULL;
29 if (!json_value->GetAsDictionary(&json_dictionary))
30 return -1;
31
32 int key_pair_index = -1;
33 if (!json_dictionary->GetInteger(key_type, &key_pair_index))
34 return -1;
35 return key_pair_index;
36 }
37
38 } // namespace
39
7 namespace chromeos { 40 namespace chromeos {
8 41
9 FakeEasyUnlockClient::FakeEasyUnlockClient() {} 42 // static
43 bool FakeEasyUnlockClient::IsEcP256KeyPair(const std::string& private_key,
44 const std::string& public_key) {
45 int private_key_index =
46 ExtractKeyPairIndexFromKey(private_key, kEc256PrivateKeyKey);
47 int public_key_index =
48 ExtractKeyPairIndexFromKey(public_key, kEc256PublicKeyKey);
49
50 return private_key_index > 0 && public_key_index == private_key_index;
51 }
52
53 FakeEasyUnlockClient::FakeEasyUnlockClient() : generated_keys_count_(0) {}
10 54
11 FakeEasyUnlockClient::~FakeEasyUnlockClient() {} 55 FakeEasyUnlockClient::~FakeEasyUnlockClient() {}
12 56
13 void FakeEasyUnlockClient::Init(dbus::Bus* bus) {} 57 void FakeEasyUnlockClient::Init(dbus::Bus* bus) {}
14 58
15 void FakeEasyUnlockClient::GenerateEcP256KeyPair( 59 void FakeEasyUnlockClient::GenerateEcP256KeyPair(
16 const KeyPairCallback& callback) { 60 const KeyPairCallback& callback) {
61 ++generated_keys_count_;
62
63 callback.Run(
64 base::StringPrintf("{\"%s\": %d}",
65 kEc256PrivateKeyKey,
66 generated_keys_count_),
67 base::StringPrintf("{\"%s\": %d}",
68 kEc256PublicKeyKey,
69 generated_keys_count_));
17 } 70 }
18 71
19 void FakeEasyUnlockClient::PerformECDHKeyAgreement( 72 void FakeEasyUnlockClient::PerformECDHKeyAgreement(
20 const std::string& private_key, 73 const std::string& private_key,
21 const std::string& public_key, 74 const std::string& public_key,
22 const DataCallback& callback) { 75 const DataCallback& callback) {
76 int private_key_index =
77 ExtractKeyPairIndexFromKey(private_key, kEc256PrivateKeyKey);
78 int public_key_index =
79 ExtractKeyPairIndexFromKey(public_key, kEc256PublicKeyKey);
80 if (private_key_index < 0 || public_key_index < 0) {
81 callback.Run("");
82 return;
83 }
84
85 // The secret key should stay the same if key pairs to which private and
86 // public key belong to are changed. To achieve this, identify the key by
87 // sum and product of key pairs.
88 callback.Run(base::StringPrintf(
89 "{\"secret_key\": [%d, %d]}",
Tim Song 2014/08/29 17:38:58 Why not just [private_key, public_key]? Is the ECD
tbarzic 2014/08/29 17:43:39 Yes, in respect to the key pairs the used keys com
Tim Song 2014/08/29 17:46:00 Good to know. Can you mention this explicitly in t
tbarzic 2014/08/29 18:29:46 Done.
90 private_key_index + public_key_index,
91 private_key_index * public_key_index));
23 } 92 }
24 93
25 void FakeEasyUnlockClient::CreateSecureMessage( 94 void FakeEasyUnlockClient::CreateSecureMessage(
26 const std::string& payload, 95 const std::string& payload,
27 const std::string& secret_key, 96 const std::string& key,
28 const std::string& associated_data, 97 const std::string& associated_data,
29 const std::string& public_metadata, 98 const std::string& public_metadata,
30 const std::string& verification_key_id, 99 const std::string& verification_key_id,
31 const std::string& encryption_type, 100 const std::string& encryption_type,
32 const std::string& signature_type, 101 const std::string& signature_type,
33 const DataCallback& callback) { 102 const DataCallback& callback) {
103 callback.Run(base::StringPrintf(
104 "{\"securemessage\": {"
105 "\"payload\": \"%s\","
106 "\"key\": \"%s\","
107 "\"associated_data\": \"%s\","
108 "\"public_metadata\": \"%s\","
109 "\"verification_key_id\": \"%s\","
110 "\"encryption_type\": \"%s\","
111 "\"signature_type\": \"%s\""
112 "}}",
113 payload.c_str(),
114 key.c_str(),
115 associated_data.c_str(),
116 public_metadata.c_str(),
117 verification_key_id.c_str(),
118 encryption_type.c_str(),
119 signature_type.c_str()));
34 } 120 }
35 121
36 void FakeEasyUnlockClient::UnwrapSecureMessage( 122 void FakeEasyUnlockClient::UnwrapSecureMessage(
37 const std::string& message, 123 const std::string& message,
38 const std::string& secret_key, 124 const std::string& key,
39 const std::string& associated_data, 125 const std::string& associated_data,
40 const std::string& encryption_type, 126 const std::string& encryption_type,
41 const std::string& signature_type, 127 const std::string& signature_type,
42 const DataCallback& callback) { 128 const DataCallback& callback) {
129 // TODO(tbarzic): Verify that |message| is in the format returned by
130 // |CreateSecureMessage| and extract payload, metadata and
131 // verification_key_id from there.
132 callback.Run(base::StringPrintf(
133 "{\"unwrapped_securemessage\": {"
134 "\"message\": \"%s\","
135 "\"key\": \"%s\","
136 "\"associated_data\": \"%s\","
137 "\"encryption_type\": \"%s\","
138 "\"signature_type\": \"%s\""
139 "}}",
140 message.c_str(),
141 key.c_str(),
142 associated_data.c_str(),
143 encryption_type.c_str(),
144 signature_type.c_str()));
43 } 145 }
44 146
45 } // namespace chromeos 147 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/fake_easy_unlock_client.h ('k') | chromeos/dbus/fake_easy_unlock_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698