[email protected] | cf21188 | 2012-07-11 07:19:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [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 | #ifndef CRYPTO_P224_SPAKE_H_ |
| 6 | #define CRYPTO_P224_SPAKE_H_ |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 7 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
davidben | 6004dc5 | 2017-02-03 04:15:29 | [diff] [blame] | 10 | #include <string> |
| 11 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
| 13 | #include "base/strings/string_piece.h" |
davidben | 6004dc5 | 2017-02-03 04:15:29 | [diff] [blame] | 14 | #include "crypto/p224.h" |
| 15 | #include "crypto/sha2.h" |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 16 | |
| 17 | namespace crypto { |
| 18 | |
[email protected] | 78df46a | 2011-12-13 07:00:19 | [diff] [blame] | 19 | // P224EncryptedKeyExchange implements SPAKE2, a variant of Encrypted |
| 20 | // Key Exchange. It allows two parties that have a secret common |
| 21 | // password to establish a common secure key by exchanging messages |
Vitaly Buka | bd85b569 | 2014-12-10 07:54:15 | [diff] [blame] | 22 | // over an insecure channel without disclosing the password. |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 23 | // |
| 24 | // The password can be low entropy as authenticating with an attacker only |
| 25 | // gives the attacker a one-shot password oracle. No other information about |
| 26 | // the password is leaked. (However, you must be sure to limit the number of |
| 27 | // permitted authentication attempts otherwise they get many one-shot oracles.) |
| 28 | // |
| 29 | // The protocol requires several RTTs (actually two, but you shouldn't assume |
vitalybuka | d120360 | 2015-01-29 20:49:49 | [diff] [blame] | 30 | // that.) To use the object, call GetNextMessage() and pass that message to the |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 31 | // peer. Get a message from the peer and feed it into ProcessMessage. Then |
| 32 | // examine the return value of ProcessMessage: |
vitalybuka | d120360 | 2015-01-29 20:49:49 | [diff] [blame] | 33 | // kResultPending: Another round is required. Call GetNextMessage and repeat. |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 34 | // kResultFailed: The authentication has failed. You can get a human readable |
| 35 | // error message by calling error(). |
| 36 | // kResultSuccess: The authentication was successful. |
| 37 | // |
| 38 | // In each exchange, each peer always sends a message. |
| 39 | class CRYPTO_EXPORT P224EncryptedKeyExchange { |
| 40 | public: |
| 41 | enum Result { |
| 42 | kResultPending, |
| 43 | kResultFailed, |
| 44 | kResultSuccess, |
| 45 | }; |
| 46 | |
| 47 | // PeerType's values are named client and server due to convention. But |
| 48 | // they could be called "A" and "B" as far as the protocol is concerned so |
| 49 | // long as the two parties don't both get the same label. |
| 50 | enum PeerType { |
| 51 | kPeerTypeClient, |
| 52 | kPeerTypeServer, |
| 53 | }; |
| 54 | |
| 55 | // peer_type: the type of the local authentication party. |
[email protected] | 78df46a | 2011-12-13 07:00:19 | [diff] [blame] | 56 | // password: secret session password. Both parties to the |
| 57 | // authentication must pass the same value. For the case of a |
| 58 | // TLS connection, see RFC 5705. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame] | 59 | P224EncryptedKeyExchange(PeerType peer_type, base::StringPiece password); |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 60 | |
vitalybuka | d120360 | 2015-01-29 20:49:49 | [diff] [blame] | 61 | // GetNextMessage returns a byte string which must be passed to the other |
| 62 | // party in the authentication. |
| 63 | const std::string& GetNextMessage(); |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 64 | |
| 65 | // ProcessMessage processes a message which must have been generated by a |
vitalybuka | d120360 | 2015-01-29 20:49:49 | [diff] [blame] | 66 | // call to GetNextMessage() by the other party. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame] | 67 | Result ProcessMessage(base::StringPiece message); |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 68 | |
| 69 | // In the event that ProcessMessage() returns kResultFailed, error will |
| 70 | // return a human readable error message. |
| 71 | const std::string& error() const; |
| 72 | |
[email protected] | 78df46a | 2011-12-13 07:00:19 | [diff] [blame] | 73 | // The key established as result of the key exchange. Must be called |
| 74 | // at then end after ProcessMessage() returns kResultSuccess. |
Vitaly Buka | fb2ccf6 | 2014-12-04 17:15:20 | [diff] [blame] | 75 | const std::string& GetKey() const; |
| 76 | |
| 77 | // The key established as result of the key exchange. Can be called after |
| 78 | // the first ProcessMessage() |
| 79 | const std::string& GetUnverifiedKey() const; |
[email protected] | 78df46a | 2011-12-13 07:00:19 | [diff] [blame] | 80 | |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 81 | private: |
| 82 | // The authentication state machine is very simple and each party proceeds |
| 83 | // through each of these states, in order. |
| 84 | enum State { |
| 85 | kStateInitial, |
| 86 | kStateRecvDH, |
| 87 | kStateSendHash, |
| 88 | kStateRecvHash, |
| 89 | kStateDone, |
| 90 | }; |
| 91 | |
Vitaly Buka | bd85b569 | 2014-12-10 07:54:15 | [diff] [blame] | 92 | FRIEND_TEST_ALL_PREFIXES(MutualAuth, ExpectedValues); |
| 93 | |
| 94 | void Init(); |
| 95 | |
| 96 | // Sets internal random scalar. Should be used by tests only. |
| 97 | void SetXForTesting(const std::string& x); |
| 98 | |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 99 | State state_; |
| 100 | const bool is_server_; |
vitalybuka | d120360 | 2015-01-29 20:49:49 | [diff] [blame] | 101 | // next_message_ contains a value for GetNextMessage() to return. |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 102 | std::string next_message_; |
| 103 | std::string error_; |
| 104 | |
| 105 | // CalculateHash computes the verification hash for the given peer and writes |
| 106 | // |kSHA256Length| bytes at |out_digest|. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 107 | void CalculateHash(PeerType peer_type, |
| 108 | const std::string& client_masked_dh, |
| 109 | const std::string& server_masked_dh, |
| 110 | const std::string& k, |
| 111 | uint8_t* out_digest); |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 112 | |
| 113 | // x_ is the secret Diffie-Hellman exponent (see paper referenced in .cc |
| 114 | // file). |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 115 | uint8_t x_[p224::kScalarBytes]; |
| 116 | // pw_ is SHA256(P(password), P(session))[:28] where P() prepends a uint32_t, |
Vitaly Buka | bd85b569 | 2014-12-10 07:54:15 | [diff] [blame] | 117 | // big-endian length prefix (see paper referenced in .cc file). |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 118 | uint8_t pw_[p224::kScalarBytes]; |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 119 | // expected_authenticator_ is used to store the hash value expected from the |
| 120 | // other party. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 121 | uint8_t expected_authenticator_[kSHA256Length]; |
[email protected] | 78df46a | 2011-12-13 07:00:19 | [diff] [blame] | 122 | |
| 123 | std::string key_; |
[email protected] | d9a262d | 2011-11-22 01:29:37 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // namespace crypto |
| 127 | |
| 128 | #endif // CRYPTO_P224_SPAKE_H_ |