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

Side by Side Diff: crypto/p224_spake.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
« no previous file with comments | « crypto/p224_spake.h ('k') | crypto/p224_spake_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/p224_spake.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 P224EncryptedKeyExchange::P224EncryptedKeyExchange(
99 PeerType peer_type,
100 const base::StringPiece& password,
101 const base::StringPiece& session)
102 : state_(kStateInitial),
103 is_server_(peer_type == kPeerTypeServer) {
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();
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& P224EncryptedKeyExchange::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(FATAL) << "P224EncryptedKeyExchange::GetMessage called in"
154 " bad state " << state_;
155 next_message_ = "";
156 return next_message_;
157 }
158
159 P224EncryptedKeyExchange::Result P224EncryptedKeyExchange::ProcessMessage(
160 const base::StringPiece& message) {
161 if (state_ == kStateRecvHash) {
162 // This is the final state of the protocol: we are reading the peer's
163 // authentication hash and checking that it matches the one that we expect.
164 if (message.size() != sizeof(expected_authenticator_)) {
165 error_ = "peer's hash had an incorrect size";
166 return kResultFailed;
167 }
168 if (!SecureMemEqual(message.data(), expected_authenticator_,
169 message.size())) {
170 error_ = "peer's hash had incorrect value";
171 return kResultFailed;
172 }
173 state_ = kStateDone;
174 return kResultSuccess;
175 }
176
177 if (state_ != kStateRecvDH) {
178 LOG(FATAL) << "P224EncryptedKeyExchange::ProcessMessage called in"
179 " bad state " << state_;
180 error_ = "internal error";
181 return kResultFailed;
182 }
183
184 // Y* is the other party's masked, Diffie-Hellman value.
185 p224::Point Ystar;
186 if (!Ystar.SetFromString(message)) {
187 error_ = "failed to parse peer's masked Diffie-Hellman value";
188 return kResultFailed;
189 }
190
191 // We calculate the mask value: (N|M)**pw
192 p224::Point MNpw, minus_MNpw, Y, k;
193 p224::ScalarMult(is_server_ ? kM : kN, pw_, &MNpw);
194 p224::Negate(MNpw, &minus_MNpw);
195
196 // Y = Y* - (N|M)**pw
197 p224::Add(Ystar, minus_MNpw, &Y);
198
199 // K = Y**x_
200 p224::ScalarMult(Y, x_, &k);
201
202 // If everything worked out, then K is the same for both parties.
203 std::string k_str = k.ToString();
204
205 std::string client_masked_dh, server_masked_dh;
206 if (is_server_) {
207 client_masked_dh = message.as_string();
208 server_masked_dh = next_message_;
209 } else {
210 client_masked_dh = next_message_;
211 server_masked_dh = message.as_string();
212 }
213
214 // Now we calculate the hashes that each side will use to prove to the other
215 // that they derived the correct value for K.
216 uint8 client_hash[kSHA256Length], server_hash[kSHA256Length];
217 CalculateHash(kPeerTypeClient, client_masked_dh, server_masked_dh, k_str,
218 client_hash);
219 CalculateHash(kPeerTypeServer, client_masked_dh, server_masked_dh, k_str,
220 server_hash);
221
222 const uint8* my_hash = is_server_ ? server_hash : client_hash;
223 const uint8* their_hash = is_server_ ? client_hash : server_hash;
224
225 next_message_ =
226 std::string(reinterpret_cast<const char*>(my_hash), kSHA256Length);
227 memcpy(expected_authenticator_, their_hash, kSHA256Length);
228 state_ = kStateSendHash;
229 return kResultPending;
230 }
231
232 void P224EncryptedKeyExchange::CalculateHash(
233 PeerType peer_type,
234 const std::string& client_masked_dh,
235 const std::string& server_masked_dh,
236 const std::string& k,
237 uint8* out_digest) {
238 std::string hash_contents;
239
240 if (peer_type == kPeerTypeServer) {
241 hash_contents = "server";
242 } else {
243 hash_contents = "client";
244 }
245
246 hash_contents += client_masked_dh;
247 hash_contents += server_masked_dh;
248 hash_contents +=
249 std::string(reinterpret_cast<const char *>(pw_), sizeof(pw_));
250 hash_contents += k;
251
252 SHA256HashString(hash_contents, out_digest, kSHA256Length);
253 }
254
255 const std::string& P224EncryptedKeyExchange::error() const {
256 return error_;
257 }
258
259 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/p224_spake.h ('k') | crypto/p224_spake_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698