OLD | NEW |
(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 #ifndef CRYPTO_P224_SPAKE_H_ |
| 6 #define CRYPTO_P224_SPAKE_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <base/string_piece.h> |
| 10 #include <crypto/p224.h> |
| 11 #include <crypto/sha2.h> |
| 12 |
| 13 namespace crypto { |
| 14 |
| 15 // P224EncryptedKeyExchange provides a means to authenticate an |
| 16 // encrypted transport using a low-entropy, shared secret. |
| 17 // |
| 18 // You need a value derived from the master secret of the connection in order |
| 19 // to bind the authentication to the encrypted channel. It's the |session| |
| 20 // argument to the constructor and can be of any length. |
| 21 // |
| 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 |
| 28 // that.) To use the object, call GetMessage() and pass that message to the |
| 29 // peer. Get a message from the peer and feed it into ProcessMessage. Then |
| 30 // examine the return value of ProcessMessage: |
| 31 // kResultPending: Another round is required. Call GetMessage and repeat. |
| 32 // 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. |
| 37 class 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. |
| 54 // password: a, possibly low-entropy, mutually known password. |
| 55 // session: a value securely derived from the connection's master secret. |
| 56 // Both parties to the authentication must pass the same value. For the |
| 57 // case of a TLS connection, see RFC 5705. |
| 58 P224EncryptedKeyExchange(PeerType peer_type, |
| 59 const base::StringPiece& password, |
| 60 const base::StringPiece& session); |
| 61 |
| 62 // GetMessage returns a byte string which must be passed to the other party |
| 63 // in the authentication. |
| 64 const std::string& GetMessage(); |
| 65 |
| 66 // ProcessMessage processes a message which must have been generated by a |
| 67 // call to GetMessage() by the other party. |
| 68 Result ProcessMessage(const base::StringPiece& message); |
| 69 |
| 70 // In the event that ProcessMessage() returns kResultFailed, error will |
| 71 // return a human readable error message. |
| 72 const std::string& error() const; |
| 73 |
| 74 private: |
| 75 // The authentication state machine is very simple and each party proceeds |
| 76 // through each of these states, in order. |
| 77 enum State { |
| 78 kStateInitial, |
| 79 kStateRecvDH, |
| 80 kStateSendHash, |
| 81 kStateRecvHash, |
| 82 kStateDone, |
| 83 }; |
| 84 |
| 85 State state_; |
| 86 const bool is_server_; |
| 87 // next_message_ contains a value for GetMessage() to return. |
| 88 std::string next_message_; |
| 89 std::string error_; |
| 90 |
| 91 // CalculateHash computes the verification hash for the given peer and writes |
| 92 // |kSHA256Length| bytes at |out_digest|. |
| 93 void CalculateHash( |
| 94 PeerType peer_type, |
| 95 const std::string& client_masked_dh, |
| 96 const std::string& server_masked_dh, |
| 97 const std::string& k, |
| 98 uint8* out_digest); |
| 99 |
| 100 // x_ is the secret Diffie-Hellman exponent (see paper referenced in .cc |
| 101 // file). |
| 102 uint8 x_[p224::kScalarBytes]; |
| 103 // pw_ is SHA256(P(password), P(session))[:28] where P() prepends a uint32, |
| 104 // big-endian length prefix (see paper refereneced in .cc file). |
| 105 uint8 pw_[p224::kScalarBytes]; |
| 106 // expected_authenticator_ is used to store the hash value expected from the |
| 107 // other party. |
| 108 uint8 expected_authenticator_[kSHA256Length]; |
| 109 }; |
| 110 |
| 111 } // namespace crypto |
| 112 |
| 113 #endif // CRYPTO_P224_SPAKE_H_ |
OLD | NEW |