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

Side by Side Diff: crypto/mutual_auth.cc

Issue 8499032: net: add low-entropy, shared secret authentication protocol. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 // This code implements SPAKE2, a varient of EKE:
6 // http://www.di.ens.fr/~pointche/pub.php?reference=AbPo04
7
8 #include <crypto/mutual_auth.h>
9
10 #include <base/logging.h>
11 #include <base/rand_util.h>
12 #include <crypto/p224.h>
13 #include <crypto/secure_util.h>
14
15 namespace {
16
17 // The following two points (M and N in the protocol) are verifiable random
18 // points on the curve and can be generated with the following code:
19
20 // #include <stdint.h>
21 // #include <stdio.h>
22 // #include <string.h>
23 //
24 // #include <openssl/ec.h>
25 // #include <openssl/obj_mac.h>
26 // #include <openssl/sha.h>
27 //
28 // static const char kSeed1[] = "P224 point generation seed (M)";
29 // static const char kSeed2[] = "P224 point generation seed (N)";
30 //
31 // void find_seed(const char* seed) {
32 // SHA256_CTX sha256;
33 // uint8_t digest[SHA256_DIGEST_LENGTH];
34 //
35 // SHA256_Init(&sha256);
36 // SHA256_Update(&sha256, seed, strlen(seed));
37 // SHA256_Final(digest, &sha256);
38 //
39 // BIGNUM x, y;
40 // EC_GROUP* p224 = EC_GROUP_new_by_curve_name(NID_secp224r1);
41 // EC_POINT* p = EC_POINT_new(p224);
42 //
43 // for (unsigned i = 0;; i++) {
44 // BN_init(&x);
45 // BN_bin2bn(digest, 28, &x);
46 //
47 // if (EC_POINT_set_compressed_coordinates_GFp(
48 // p224, p, &x, digest[28] & 1, NULL)) {
49 // BN_init(&y);
50 // EC_POINT_get_affine_coordinates_GFp(p224, p, &x, &y, NULL);
51 // char* x_str = BN_bn2hex(&x);
52 // char* y_str = BN_bn2hex(&y);
53 // printf("Found after %u iterations:\n%s\n%s\n", i, x_str, y_str);
54 // OPENSSL_free(x_str);
55 // OPENSSL_free(y_str);
56 // BN_free(&x);
57 // BN_free(&y);
58 // break;
59 // }
60 //
61 // SHA256_Init(&sha256);
62 // SHA256_Update(&sha256, digest, sizeof(digest));
63 // SHA256_Final(digest, &sha256);
64 //
65 // BN_free(&x);
66 // }
67 //
68 // EC_POINT_free(p);
69 // EC_GROUP_free(p224);
70 // }
71 //
72 // int main() {
73 // find_seed(kSeed1);
74 // find_seed(kSeed2);
75 // return 0;
76 // }
77
78 const crypto::p224::Point kM = {
79 {174237515, 77186811, 235213682, 33849492,
80 33188520, 48266885, 177021753, 81038478},
81 {104523827, 245682244, 266509668, 236196369,
82 28372046, 145351378, 198520366, 113345994},
83 {1, 0, 0, 0, 0, 0, 0},
84 };
85
86 const crypto::p224::Point kN = {
87 {136176322, 263523628, 251628795, 229292285,
88 5034302, 185981975, 171998428, 11653062},
89 {197567436, 51226044, 60372156, 175772188,
90 42075930, 8083165, 160827401, 65097570},
91 {1, 0, 0, 0, 0, 0, 0},
92 };
93
94 } // anonymous namespace
95
96 namespace crypto {
97
98 SharedSecretMutualAuthentication::SharedSecretMutualAuthentication(
99 PeerType peer_type,
100 const base::StringPiece& password,
101 const base::StringPiece& session)
102 : is_server_(peer_type == kServer),
103 state_(kStateInitial) {
104 // x_ is a random scalar.
105 base::RandBytes(x_, sizeof(x_));
106
107 // X = g**x_
108 p224::Point X;
109 p224::ScalarBaseMult(x_, &X);
110
111 // The "password" in the SPAKE2 protocol is
112 // SHA256(P(password) + P(session)) where P is function that prepends a
113 // uint32, big-endian length prefix.
114 uint8 password_length[4], session_length[4];
115 password_length[0] = password.size() >> 24;
116 password_length[1] = password.size() >> 16;
117 password_length[2] = password.size() >> 8;
118 password_length[3] = password.size();
119 session_length[0] = session.size() >> 24;
120 session_length[1] = session.size() >> 16;
121 session_length[2] = session.size() >> 8;
122 session_length[3] = session.size();
Wez 2011/11/09 22:43:46 nit: Do we not have helper functions in crypto/, o
agl 2011/11/10 17:18:19 In net/ we use htonl/ntohl and memcpy. For the sak
123 SHA256HashString(std::string(reinterpret_cast<const char *>(password_length),
124 sizeof(password_length)) +
125 password.as_string() +
126 std::string(reinterpret_cast<const char *>(session_length),
127 sizeof(session_length)) +
128 session.as_string(),
129 pw_,
130 sizeof(pw_));
131
132 // The client masks the Diffie-Hellman value, X, by adding M**pw and the
133 // server uses N**pw.
134 p224::Point MNpw;
135 p224::ScalarMult(is_server_ ? kN : kM, pw_, &MNpw);
136
137 // X* = X + (N|M)**pw
138 p224::Point Xstar;
139 p224::Add(X, MNpw, &Xstar);
140
141 next_message_ = Xstar.ToString();
142 }
143
144 const std::string& SharedSecretMutualAuthentication::GetMessage() {
145 if (state_ == kStateInitial) {
146 state_ = kStateRecvDH;
147 return next_message_;
148 } else if (state_ == kStateSendHash) {
149 state_ = kStateRecvHash;
150 return next_message_;
151 }
152
153 LOG(ERROR) << "SharedSecretMutualAuthentication::GetMessage called in"
154 " bad state " << state_;
Wez 2011/11/09 22:43:46 Is this actually a sufficiently severe failure of
agl 2011/11/10 17:18:19 You're the one writing the calling code so, if you
155 NOTREACHED();
156 next_message_ = "";
157 return next_message_;
158 }
159
160 SharedSecretMutualAuthentication::Result
161 SharedSecretMutualAuthentication::ProcessMessage(
162 const base::StringPiece& message) {
163 if (state_ == kStateRecvHash) {
164 // This is the final state of the protocol: we are reading the peer's
165 // authentication hash and checking that it matches the one that we expect.
166 if (message.size() != sizeof(expected_authenticator_)) {
167 error_ = "peer's hash had an incorrect size";
168 return kResultFailed;
169 }
170 if (!SecureMemEqual(message.data(), expected_authenticator_,
171 message.size())) {
172 error_ = "peer's hash had incorrect value";
173 return kResultFailed;
174 }
175 state_ = kStateDone;
176 return kResultSuccess;
177 }
178
179 if (state_ != kStateRecvDH) {
180 LOG(ERROR) << "SharedSecretMutualAuthentication::ProcessMessage called in"
181 " bad state " << state_;
182 error_ = "internal error";
183 return kResultFailed;
Wez 2011/11/09 22:43:46 Similarly, is this actually a potentially severe f
agl 2011/11/10 17:18:19 Ditto.
184 }
185
186 // Y* is the other party's masked, Diffie-Hellman value.
187 p224::Point Ystar;
188 if (!Ystar.SetFromString(message)) {
189 error_ = "failed to parse peer's masked Diffie-Hellman value";
190 return kResultFailed;
191 }
192
193 // We calculate the mask value: (N|M)**pw
194 p224::Point MNpw, minus_MNpw, Y, k;
195 p224::ScalarMult(is_server_ ? kM : kN, pw_, &MNpw);
196 p224::Negate(MNpw, &minus_MNpw);
197 // Y = Y* - (N|M)**pw
198 p224::Add(Ystar, minus_MNpw, &Y);
199 // K = Y**x_
200 p224::ScalarMult(Y, x_, &k);
Wez 2011/11/09 22:43:46 nit: As for the P224 impl, I find this easier to r
agl 2011/11/10 17:18:19 Done.
201 // If everything worked out, then K is the same for both parties.
202 std::string k_str = k.ToString();
203
204 std::string client_masked_dh, server_masked_dh;
Wez 2011/11/09 22:43:46 Add a comment here to indicate that we're just cal
agl 2011/11/10 17:18:19 Done.
205 if (is_server_) {
206 client_masked_dh = message.as_string();
207 server_masked_dh = next_message_;
208 } else {
209 client_masked_dh = next_message_;
210 server_masked_dh = message.as_string();
211 }
212
213 std::string client_hash_contents;
214 client_hash_contents = "client";
215 client_hash_contents += client_masked_dh;
216 client_hash_contents += server_masked_dh;
217 client_hash_contents += std::string(reinterpret_cast<const char *>(pw_),
218 sizeof(pw_));
219 client_hash_contents += k_str;
220
221 std::string server_hash_contents;
222 server_hash_contents = "server";
223 server_hash_contents += client_masked_dh;
224 server_hash_contents += server_masked_dh;
225 server_hash_contents += std::string(reinterpret_cast<const char *>(pw_),
226 sizeof(pw_));
227 server_hash_contents += k_str;
Wez 2011/11/09 22:43:46 Consider having a static function to do these, sin
agl 2011/11/10 17:18:19 Done.
228
229 uint8 client_hash[kSHA256Length], server_hash[kSHA256Length];
230 SHA256HashString(client_hash_contents, client_hash, sizeof(client_hash));
231 SHA256HashString(server_hash_contents, server_hash, sizeof(server_hash));
232
233 const uint8* my_hash = is_server_ ? server_hash : client_hash;
234 const uint8* their_hash = is_server_ ? client_hash : server_hash;
235
236 next_message_ =
237 std::string(reinterpret_cast<const char*>(my_hash), kSHA256Length);
238 memcpy(expected_authenticator_, their_hash, kSHA256Length);
239 state_ = kStateSendHash;
240 return kResultPending;
241 }
242
243 const std::string& SharedSecretMutualAuthentication::error() const {
244 return error_;
245 }
246
247 } // namespace crypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698