OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ | 5 #ifndef NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ |
6 #define NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ | 6 #define NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
| 11 #include <vector> |
11 | 12 |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 15 #include "base/synchronization/lock.h" |
14 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
15 | 17 |
16 namespace net { | 18 namespace net { |
17 | 19 |
18 class QuicRandom; | 20 class QuicRandom; |
19 | 21 |
20 // CryptoSecretBoxer encrypts small chunks of plaintext (called 'boxing') and | 22 // CryptoSecretBoxer encrypts small chunks of plaintext (called 'boxing') and |
21 // then, later, can authenticate+decrypt the resulting boxes. This object is | 23 // then, later, can authenticate+decrypt the resulting boxes. This object is |
22 // thread-safe. | 24 // thread-safe. |
23 class NET_EXPORT_PRIVATE CryptoSecretBoxer { | 25 class NET_EXPORT_PRIVATE CryptoSecretBoxer { |
24 public: | 26 public: |
25 CryptoSecretBoxer() {} | 27 CryptoSecretBoxer(); |
| 28 ~CryptoSecretBoxer(); |
26 | 29 |
27 // GetKeySize returns the number of bytes in a key. | 30 // GetKeySize returns the number of bytes in a key. |
28 static size_t GetKeySize(); | 31 static size_t GetKeySize(); |
29 | 32 |
30 // SetKey sets the key for this object. This must be done before |Box| or | 33 // SetKeys sets a std::list of encryption keys. The first key in the std::list |
31 // |Unbox| are called. |key| must be |GetKeySize()| bytes long. | 34 // will be used by |Box|, but all supplied keys will be tried by |Unbox|, to |
32 void SetKey(base::StringPiece key); | 35 // handle key skew across the fleet. This must be called before |Box| or |
| 36 // |Unbox|. Keys must be |GetKeySize()| bytes long. |
| 37 void SetKeys(const std::vector<std::string>& keys); |
33 | 38 |
34 // Box encrypts |plaintext| using a random nonce generated from |rand| and | 39 // Box encrypts |plaintext| using a random nonce generated from |rand| and |
35 // returns the resulting ciphertext. Since an authenticator and nonce are | 40 // returns the resulting ciphertext. Since an authenticator and nonce are |
36 // included, the result will be slightly larger than |plaintext|. | 41 // included, the result will be slightly larger than |plaintext|. The first |
| 42 // key in the std::vector supplied to |SetKeys| will be used. |
37 std::string Box(QuicRandom* rand, base::StringPiece plaintext) const; | 43 std::string Box(QuicRandom* rand, base::StringPiece plaintext) const; |
38 | 44 |
39 // Unbox takes the result of a previous call to |Box| in |ciphertext| and | 45 // Unbox takes the result of a previous call to |Box| in |ciphertext| and |
40 // authenticates+decrypts it. If |ciphertext| is not authentic then it | 46 // authenticates+decrypts it. If |ciphertext| cannot be decrypted with any of |
41 // returns false. Otherwise, |out_storage| is used to store the result and | 47 // the supplied keys, the function returns false. Otherwise, |out_storage| is |
42 // |out| is set to point into |out_storage| and contains the original | 48 // used to store the result and |out| is set to point into |out_storage| and |
43 // plaintext. | 49 // contains the original plaintext. |
44 bool Unbox(base::StringPiece ciphertext, | 50 bool Unbox(base::StringPiece ciphertext, |
45 std::string* out_storage, | 51 std::string* out_storage, |
46 base::StringPiece* out) const; | 52 base::StringPiece* out) const; |
47 | 53 |
48 private: | 54 private: |
49 std::string key_; | 55 mutable base::Lock lock_; |
| 56 // GUARDED_BY(lock_). |
| 57 std::vector<std::string> keys_; |
50 | 58 |
51 DISALLOW_COPY_AND_ASSIGN(CryptoSecretBoxer); | 59 DISALLOW_COPY_AND_ASSIGN(CryptoSecretBoxer); |
52 }; | 60 }; |
53 | 61 |
54 } // namespace net | 62 } // namespace net |
55 | 63 |
56 #endif // NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ | 64 #endif // NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ |
OLD | NEW |