190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved. 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file. 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <string> 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/strings/string_piece.h" 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "net/base/net_export.h" 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace net { 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class QuicRandom; 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// CryptoSecretBoxer encrypts small chunks of plaintext (called 'boxing') and 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// then, later, can authenticate+decrypt the resulting boxes. This object is 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// thread-safe. 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class NET_EXPORT_PRIVATE CryptoSecretBoxer { 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public: 225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu CryptoSecretBoxer() {} 235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // GetKeySize returns the number of bytes in a key. 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) static size_t GetKeySize(); 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // SetKey sets the key for this object. This must be done before |Box| or 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // |Unbox| are called. |key| must be |GetKeySize()| bytes long. 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void SetKey(base::StringPiece key); 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Box encrypts |plaintext| using a random nonce generated from |rand| and 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // returns the resulting ciphertext. Since an authenticator and nonce are 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // included, the result will be slightly larger than |plaintext|. 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::string Box(QuicRandom* rand, base::StringPiece plaintext) const; 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Unbox takes the result of a previous call to |Box| in |ciphertext| and 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // authenticates+decrypts it. If |ciphertext| is not authentic then it 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // returns false. Otherwise, |out_storage| is used to store the result and 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // |out| is set to point into |out_storage| and contains the original 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // plaintext. 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool Unbox(base::StringPiece ciphertext, 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::string* out_storage, 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::StringPiece* out) const; 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private: 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::string key_; 475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu DISALLOW_COPY_AND_ASSIGN(CryptoSecretBoxer); 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace net 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ 54