Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 1 | // Copyright 2012 The Chromium OS Authors. All rights reserved. |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 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 variant of EKE: |
| 6 | // http://www.di.ens.fr/~pointche/pub.php?reference=AbPo04 |
| 7 | |
Vitaly Buka | 9e5b683 | 2015-10-14 15:57:14 -0700 | [diff] [blame] | 8 | #include "third_party/chromium/crypto/p224_spake.h" |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 9 | |
| 10 | #include <algorithm> |
| 11 | |
| 12 | #include <base/logging.h> |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 13 | #include <base/rand_util.h> |
| 14 | |
Vitaly Buka | 9e5b683 | 2015-10-14 15:57:14 -0700 | [diff] [blame] | 15 | #include "third_party/chromium/crypto/p224.h" |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 16 | |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 17 | namespace crypto { |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // The following two points (M and N in the protocol) are verifiable random |
| 22 | // points on the curve and can be generated with the following code: |
| 23 | |
| 24 | // #include <stdint.h> |
| 25 | // #include <stdio.h> |
| 26 | // #include <string.h> |
| 27 | // |
| 28 | // #include <openssl/ec.h> |
| 29 | // #include <openssl/obj_mac.h> |
| 30 | // #include <openssl/sha.h> |
| 31 | // |
| 32 | // static const char kSeed1[] = "P224 point generation seed (M)"; |
| 33 | // static const char kSeed2[] = "P224 point generation seed (N)"; |
| 34 | // |
| 35 | // void find_seed(const char* seed) { |
| 36 | // SHA256_CTX sha256; |
| 37 | // uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 38 | // |
| 39 | // SHA256_Init(&sha256); |
| 40 | // SHA256_Update(&sha256, seed, strlen(seed)); |
| 41 | // SHA256_Final(digest, &sha256); |
| 42 | // |
| 43 | // BIGNUM x, y; |
| 44 | // EC_GROUP* p224 = EC_GROUP_new_by_curve_name(NID_secp224r1); |
| 45 | // EC_POINT* p = EC_POINT_new(p224); |
| 46 | // |
| 47 | // for (unsigned i = 0;; i++) { |
| 48 | // BN_init(&x); |
| 49 | // BN_bin2bn(digest, 28, &x); |
| 50 | // |
| 51 | // if (EC_POINT_set_compressed_coordinates_GFp( |
| 52 | // p224, p, &x, digest[28] & 1, NULL)) { |
| 53 | // BN_init(&y); |
| 54 | // EC_POINT_get_affine_coordinates_GFp(p224, p, &x, &y, NULL); |
| 55 | // char* x_str = BN_bn2hex(&x); |
| 56 | // char* y_str = BN_bn2hex(&y); |
| 57 | // printf("Found after %u iterations:\n%s\n%s\n", i, x_str, y_str); |
| 58 | // OPENSSL_free(x_str); |
| 59 | // OPENSSL_free(y_str); |
| 60 | // BN_free(&x); |
| 61 | // BN_free(&y); |
| 62 | // break; |
| 63 | // } |
| 64 | // |
| 65 | // SHA256_Init(&sha256); |
| 66 | // SHA256_Update(&sha256, digest, sizeof(digest)); |
| 67 | // SHA256_Final(digest, &sha256); |
| 68 | // |
| 69 | // BN_free(&x); |
| 70 | // } |
| 71 | // |
| 72 | // EC_POINT_free(p); |
| 73 | // EC_GROUP_free(p224); |
| 74 | // } |
| 75 | // |
| 76 | // int main() { |
| 77 | // find_seed(kSeed1); |
| 78 | // find_seed(kSeed2); |
| 79 | // return 0; |
| 80 | // } |
| 81 | |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 82 | const p224::Point kM = { |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 83 | {174237515, 77186811, 235213682, 33849492, |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 84 | 33188520, 48266885, 177021753, 81038478}, |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 85 | {104523827, 245682244, 266509668, 236196369, |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 86 | 28372046, 145351378, 198520366, 113345994}, |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 87 | {1, 0, 0, 0, 0, 0, 0, 0}, |
| 88 | }; |
| 89 | |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 90 | const p224::Point kN = { |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 91 | {136176322, 263523628, 251628795, 229292285, |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 92 | 5034302, 185981975, 171998428, 11653062}, |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 93 | {197567436, 51226044, 60372156, 175772188, |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 94 | 42075930, 8083165, 160827401, 65097570}, |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 95 | {1, 0, 0, 0, 0, 0, 0, 0}, |
| 96 | }; |
| 97 | |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 98 | // Performs a constant-time comparison of two strings, returning true if the |
| 99 | // strings are equal. |
| 100 | // |
| 101 | // For cryptographic operations, comparison functions such as memcmp() may |
| 102 | // expose side-channel information about input, allowing an attacker to |
| 103 | // perform timing analysis to determine what the expected bits should be. In |
| 104 | // order to avoid such attacks, the comparison must execute in constant time, |
| 105 | // so as to not to reveal to the attacker where the difference(s) are. |
| 106 | // For an example attack, see |
| 107 | // http://groups.google.com/group/keyczar-discuss/browse_thread/thread/5571eca0948b2a13 |
| 108 | bool SecureMemEqual(const uint8_t* s1_ptr, const uint8_t* s2_ptr, size_t n) { |
| 109 | uint8_t tmp = 0; |
| 110 | for (size_t i = 0; i < n; ++i, ++s1_ptr, ++s2_ptr) |
| 111 | tmp |= *s1_ptr ^ *s2_ptr; |
| 112 | return (tmp == 0); |
| 113 | } |
| 114 | |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 115 | } // anonymous namespace |
| 116 | |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 117 | P224EncryptedKeyExchange::P224EncryptedKeyExchange(PeerType peer_type, |
| 118 | const std::string& password) |
| 119 | : state_(kStateInitial), is_server_(peer_type == kPeerTypeServer) { |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 120 | memset(&x_, 0, sizeof(x_)); |
| 121 | memset(&expected_authenticator_, 0, sizeof(expected_authenticator_)); |
| 122 | |
| 123 | // x_ is a random scalar. |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 124 | base::RandBytes(x_, sizeof(x_)); |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 125 | |
| 126 | // Calculate |password| hash to get SPAKE password value. |
| 127 | SHA256HashString(std::string(password.data(), password.length()), |
| 128 | pw_, sizeof(pw_)); |
| 129 | |
| 130 | Init(); |
| 131 | } |
| 132 | |
| 133 | void P224EncryptedKeyExchange::Init() { |
| 134 | // X = g**x_ |
| 135 | p224::Point X; |
| 136 | p224::ScalarBaseMult(x_, &X); |
| 137 | |
| 138 | // The client masks the Diffie-Hellman value, X, by adding M**pw and the |
| 139 | // server uses N**pw. |
| 140 | p224::Point MNpw; |
| 141 | p224::ScalarMult(is_server_ ? kN : kM, pw_, &MNpw); |
| 142 | |
| 143 | // X* = X + (N|M)**pw |
| 144 | p224::Point Xstar; |
| 145 | p224::Add(X, MNpw, &Xstar); |
| 146 | |
| 147 | next_message_ = Xstar.ToString(); |
| 148 | } |
| 149 | |
| 150 | const std::string& P224EncryptedKeyExchange::GetNextMessage() { |
| 151 | if (state_ == kStateInitial) { |
| 152 | state_ = kStateRecvDH; |
| 153 | return next_message_; |
| 154 | } else if (state_ == kStateSendHash) { |
| 155 | state_ = kStateRecvHash; |
| 156 | return next_message_; |
| 157 | } |
| 158 | |
| 159 | LOG(FATAL) << "P224EncryptedKeyExchange::GetNextMessage called in" |
| 160 | " bad state " << state_; |
| 161 | next_message_ = ""; |
| 162 | return next_message_; |
| 163 | } |
| 164 | |
| 165 | P224EncryptedKeyExchange::Result P224EncryptedKeyExchange::ProcessMessage( |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 166 | const std::string& message) { |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 167 | if (state_ == kStateRecvHash) { |
| 168 | // This is the final state of the protocol: we are reading the peer's |
| 169 | // authentication hash and checking that it matches the one that we expect. |
| 170 | if (message.size() != sizeof(expected_authenticator_)) { |
| 171 | error_ = "peer's hash had an incorrect size"; |
| 172 | return kResultFailed; |
| 173 | } |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 174 | if (!SecureMemEqual(reinterpret_cast<const uint8_t*>(message.data()), |
| 175 | expected_authenticator_, message.size())) { |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 176 | error_ = "peer's hash had incorrect value"; |
| 177 | return kResultFailed; |
| 178 | } |
| 179 | state_ = kStateDone; |
| 180 | return kResultSuccess; |
| 181 | } |
| 182 | |
| 183 | if (state_ != kStateRecvDH) { |
| 184 | LOG(FATAL) << "P224EncryptedKeyExchange::ProcessMessage called in" |
| 185 | " bad state " << state_; |
| 186 | error_ = "internal error"; |
| 187 | return kResultFailed; |
| 188 | } |
| 189 | |
| 190 | // Y* is the other party's masked, Diffie-Hellman value. |
| 191 | p224::Point Ystar; |
| 192 | if (!Ystar.SetFromString(message)) { |
| 193 | error_ = "failed to parse peer's masked Diffie-Hellman value"; |
| 194 | return kResultFailed; |
| 195 | } |
| 196 | |
| 197 | // We calculate the mask value: (N|M)**pw |
| 198 | p224::Point MNpw, minus_MNpw, Y, k; |
| 199 | p224::ScalarMult(is_server_ ? kM : kN, pw_, &MNpw); |
| 200 | p224::Negate(MNpw, &minus_MNpw); |
| 201 | |
| 202 | // Y = Y* - (N|M)**pw |
| 203 | p224::Add(Ystar, minus_MNpw, &Y); |
| 204 | |
| 205 | // K = Y**x_ |
| 206 | p224::ScalarMult(Y, x_, &k); |
| 207 | |
| 208 | // If everything worked out, then K is the same for both parties. |
| 209 | key_ = k.ToString(); |
| 210 | |
| 211 | std::string client_masked_dh, server_masked_dh; |
| 212 | if (is_server_) { |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 213 | client_masked_dh = message; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 214 | server_masked_dh = next_message_; |
| 215 | } else { |
| 216 | client_masked_dh = next_message_; |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 217 | server_masked_dh = message; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Now we calculate the hashes that each side will use to prove to the other |
| 221 | // that they derived the correct value for K. |
| 222 | uint8 client_hash[kSHA256Length], server_hash[kSHA256Length]; |
| 223 | CalculateHash(kPeerTypeClient, client_masked_dh, server_masked_dh, key_, |
| 224 | client_hash); |
| 225 | CalculateHash(kPeerTypeServer, client_masked_dh, server_masked_dh, key_, |
| 226 | server_hash); |
| 227 | |
| 228 | const uint8* my_hash = is_server_ ? server_hash : client_hash; |
| 229 | const uint8* their_hash = is_server_ ? client_hash : server_hash; |
| 230 | |
| 231 | next_message_ = |
| 232 | std::string(reinterpret_cast<const char*>(my_hash), kSHA256Length); |
| 233 | memcpy(expected_authenticator_, their_hash, kSHA256Length); |
| 234 | state_ = kStateSendHash; |
| 235 | return kResultPending; |
| 236 | } |
| 237 | |
| 238 | void P224EncryptedKeyExchange::CalculateHash( |
| 239 | PeerType peer_type, |
| 240 | const std::string& client_masked_dh, |
| 241 | const std::string& server_masked_dh, |
| 242 | const std::string& k, |
| 243 | uint8* out_digest) { |
| 244 | std::string hash_contents; |
| 245 | |
| 246 | if (peer_type == kPeerTypeServer) { |
| 247 | hash_contents = "server"; |
| 248 | } else { |
| 249 | hash_contents = "client"; |
| 250 | } |
| 251 | |
| 252 | hash_contents += client_masked_dh; |
| 253 | hash_contents += server_masked_dh; |
| 254 | hash_contents += |
| 255 | std::string(reinterpret_cast<const char *>(pw_), sizeof(pw_)); |
| 256 | hash_contents += k; |
| 257 | |
| 258 | SHA256HashString(hash_contents, out_digest, kSHA256Length); |
| 259 | } |
| 260 | |
| 261 | const std::string& P224EncryptedKeyExchange::error() const { |
| 262 | return error_; |
| 263 | } |
| 264 | |
| 265 | const std::string& P224EncryptedKeyExchange::GetKey() const { |
| 266 | DCHECK_EQ(state_, kStateDone); |
| 267 | return GetUnverifiedKey(); |
| 268 | } |
| 269 | |
| 270 | const std::string& P224EncryptedKeyExchange::GetUnverifiedKey() const { |
| 271 | // Key is already final when state is kStateSendHash. Subsequent states are |
| 272 | // used only for verification of the key. Some users may combine verification |
| 273 | // with sending verifiable data instead of |expected_authenticator_|. |
| 274 | DCHECK_GE(state_, kStateSendHash); |
| 275 | return key_; |
| 276 | } |
| 277 | |
| 278 | void P224EncryptedKeyExchange::SetXForTesting(const std::string& x) { |
| 279 | memset(&x_, 0, sizeof(x_)); |
| 280 | memcpy(&x_, x.data(), std::min(x.size(), sizeof(x_))); |
| 281 | Init(); |
| 282 | } |
| 283 | |
| 284 | } // namespace crypto |