QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2013 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 QUICHE_QUIC_CORE_CRYPTO_CRYPTO_SECRET_BOXER_H_ |
| 6 | #define QUICHE_QUIC_CORE_CRYPTO_CRYPTO_SECRET_BOXER_H_ |
| 7 | |
| 8 | #include <cstddef> |
| 9 | #include <memory> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/macros.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_mutex.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_string.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 17 | |
| 18 | namespace quic { |
| 19 | |
| 20 | class QuicRandom; |
| 21 | |
| 22 | // CryptoSecretBoxer encrypts small chunks of plaintext (called 'boxing') and |
| 23 | // then, later, can authenticate+decrypt the resulting boxes. This object is |
| 24 | // thread-safe. |
| 25 | class QUIC_EXPORT_PRIVATE CryptoSecretBoxer { |
| 26 | public: |
| 27 | CryptoSecretBoxer(); |
| 28 | CryptoSecretBoxer(const CryptoSecretBoxer&) = delete; |
| 29 | CryptoSecretBoxer& operator=(const CryptoSecretBoxer&) = delete; |
| 30 | ~CryptoSecretBoxer(); |
| 31 | |
| 32 | // GetKeySize returns the number of bytes in a key. |
| 33 | static size_t GetKeySize(); |
| 34 | |
| 35 | // SetKeys sets a list of encryption keys. The first key in the list will be |
| 36 | // used by |Box|, but all supplied keys will be tried by |Unbox|, to handle |
| 37 | // key skew across the fleet. This must be called before |Box| or |Unbox|. |
| 38 | // Keys must be |GetKeySize()| bytes long. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 39 | void SetKeys(const std::vector<std::string>& keys); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | |
| 41 | // Box encrypts |plaintext| using a random nonce generated from |rand| and |
| 42 | // returns the resulting ciphertext. Since an authenticator and nonce are |
| 43 | // included, the result will be slightly larger than |plaintext|. The first |
| 44 | // key in the vector supplied to |SetKeys| will be used. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 45 | std::string Box(QuicRandom* rand, QuicStringPiece plaintext) const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 46 | |
| 47 | // Unbox takes the result of a previous call to |Box| in |ciphertext| and |
| 48 | // authenticates+decrypts it. If |ciphertext| cannot be decrypted with any of |
| 49 | // the supplied keys, the function returns false. Otherwise, |out_storage| is |
| 50 | // used to store the result and |out| is set to point into |out_storage| and |
| 51 | // contains the original plaintext. |
| 52 | bool Unbox(QuicStringPiece ciphertext, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 53 | std::string* out_storage, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | QuicStringPiece* out) const; |
| 55 | |
| 56 | private: |
| 57 | struct State; |
| 58 | |
| 59 | mutable QuicMutex lock_; |
| 60 | |
| 61 | // state_ is an opaque pointer to whatever additional state the concrete |
| 62 | // implementation of CryptoSecretBoxer requires. |
| 63 | std::unique_ptr<State> state_ GUARDED_BY(lock_); |
| 64 | }; |
| 65 | |
| 66 | } // namespace quic |
| 67 | |
| 68 | #endif // QUICHE_QUIC_CORE_CRYPTO_CRYPTO_SECRET_BOXER_H_ |