blob: f9a44e70e17a3a1cbbf6d63b052c7368b44a7fba [file] [log] [blame]
[email protected]cf211882012-07-11 07:19:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d9a262d2011-11-22 01:29:372// 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]d9a262d2011-11-22 01:29:377
[email protected]d9a262d2011-11-22 01:29:378#include <crypto/p224.h>
9#include <crypto/sha2.h>
avidd373b8b2015-12-21 21:34:4310#include <stdint.h>
11
12#include "base/gtest_prod_util.h"
13#include "base/strings/string_piece.h"
[email protected]d9a262d2011-11-22 01:29:3714
15namespace crypto {
16
[email protected]78df46a2011-12-13 07:00:1917// P224EncryptedKeyExchange implements SPAKE2, a variant of Encrypted
18// Key Exchange. It allows two parties that have a secret common
19// password to establish a common secure key by exchanging messages
Vitaly Bukabd85b5692014-12-10 07:54:1520// over an insecure channel without disclosing the password.
[email protected]d9a262d2011-11-22 01:29:3721//
22// The password can be low entropy as authenticating with an attacker only
23// gives the attacker a one-shot password oracle. No other information about
24// the password is leaked. (However, you must be sure to limit the number of
25// permitted authentication attempts otherwise they get many one-shot oracles.)
26//
27// The protocol requires several RTTs (actually two, but you shouldn't assume
vitalybukad1203602015-01-29 20:49:4928// that.) To use the object, call GetNextMessage() and pass that message to the
[email protected]d9a262d2011-11-22 01:29:3729// peer. Get a message from the peer and feed it into ProcessMessage. Then
30// examine the return value of ProcessMessage:
vitalybukad1203602015-01-29 20:49:4931// kResultPending: Another round is required. Call GetNextMessage and repeat.
[email protected]d9a262d2011-11-22 01:29:3732// kResultFailed: The authentication has failed. You can get a human readable
33// error message by calling error().
34// kResultSuccess: The authentication was successful.
35//
36// In each exchange, each peer always sends a message.
37class CRYPTO_EXPORT P224EncryptedKeyExchange {
38 public:
39 enum Result {
40 kResultPending,
41 kResultFailed,
42 kResultSuccess,
43 };
44
45 // PeerType's values are named client and server due to convention. But
46 // they could be called "A" and "B" as far as the protocol is concerned so
47 // long as the two parties don't both get the same label.
48 enum PeerType {
49 kPeerTypeClient,
50 kPeerTypeServer,
51 };
52
53 // peer_type: the type of the local authentication party.
[email protected]78df46a2011-12-13 07:00:1954 // password: secret session password. Both parties to the
55 // authentication must pass the same value. For the case of a
56 // TLS connection, see RFC 5705.
[email protected]d9a262d2011-11-22 01:29:3757 P224EncryptedKeyExchange(PeerType peer_type,
[email protected]78df46a2011-12-13 07:00:1958 const base::StringPiece& password);
[email protected]d9a262d2011-11-22 01:29:3759
vitalybukad1203602015-01-29 20:49:4960 // GetNextMessage returns a byte string which must be passed to the other
61 // party in the authentication.
62 const std::string& GetNextMessage();
[email protected]d9a262d2011-11-22 01:29:3763
64 // ProcessMessage processes a message which must have been generated by a
vitalybukad1203602015-01-29 20:49:4965 // call to GetNextMessage() by the other party.
[email protected]d9a262d2011-11-22 01:29:3766 Result ProcessMessage(const base::StringPiece& message);
67
68 // In the event that ProcessMessage() returns kResultFailed, error will
69 // return a human readable error message.
70 const std::string& error() const;
71
[email protected]78df46a2011-12-13 07:00:1972 // The key established as result of the key exchange. Must be called
73 // at then end after ProcessMessage() returns kResultSuccess.
Vitaly Bukafb2ccf62014-12-04 17:15:2074 const std::string& GetKey() const;
75
76 // The key established as result of the key exchange. Can be called after
77 // the first ProcessMessage()
78 const std::string& GetUnverifiedKey() const;
[email protected]78df46a2011-12-13 07:00:1979
[email protected]d9a262d2011-11-22 01:29:3780 private:
81 // The authentication state machine is very simple and each party proceeds
82 // through each of these states, in order.
83 enum State {
84 kStateInitial,
85 kStateRecvDH,
86 kStateSendHash,
87 kStateRecvHash,
88 kStateDone,
89 };
90
Vitaly Bukabd85b5692014-12-10 07:54:1591 FRIEND_TEST_ALL_PREFIXES(MutualAuth, ExpectedValues);
92
93 void Init();
94
95 // Sets internal random scalar. Should be used by tests only.
96 void SetXForTesting(const std::string& x);
97
[email protected]d9a262d2011-11-22 01:29:3798 State state_;
99 const bool is_server_;
vitalybukad1203602015-01-29 20:49:49100 // next_message_ contains a value for GetNextMessage() to return.
[email protected]d9a262d2011-11-22 01:29:37101 std::string next_message_;
102 std::string error_;
103
104 // CalculateHash computes the verification hash for the given peer and writes
105 // |kSHA256Length| bytes at |out_digest|.
avidd373b8b2015-12-21 21:34:43106 void CalculateHash(PeerType peer_type,
107 const std::string& client_masked_dh,
108 const std::string& server_masked_dh,
109 const std::string& k,
110 uint8_t* out_digest);
[email protected]d9a262d2011-11-22 01:29:37111
112 // x_ is the secret Diffie-Hellman exponent (see paper referenced in .cc
113 // file).
avidd373b8b2015-12-21 21:34:43114 uint8_t x_[p224::kScalarBytes];
115 // pw_ is SHA256(P(password), P(session))[:28] where P() prepends a uint32_t,
Vitaly Bukabd85b5692014-12-10 07:54:15116 // big-endian length prefix (see paper referenced in .cc file).
avidd373b8b2015-12-21 21:34:43117 uint8_t pw_[p224::kScalarBytes];
[email protected]d9a262d2011-11-22 01:29:37118 // expected_authenticator_ is used to store the hash value expected from the
119 // other party.
avidd373b8b2015-12-21 21:34:43120 uint8_t expected_authenticator_[kSHA256Length];
[email protected]78df46a2011-12-13 07:00:19121
122 std::string key_;
[email protected]d9a262d2011-11-22 01:29:37123};
124
125} // namespace crypto
126
127#endif // CRYPTO_P224_SPAKE_H_