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 #include "net/quic/crypto/crypto_secret_boxer.h" | 5 #include "net/quic/crypto/crypto_secret_boxer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "net/quic/crypto/crypto_protocol.h" |
9 #include "crypto/secure_hash.h" | |
10 #include "crypto/sha2.h" | |
11 #include "net/quic/crypto/quic_random.h" | 9 #include "net/quic/crypto/quic_random.h" |
12 | 10 |
13 using base::StringPiece; | 11 using base::StringPiece; |
14 using std::string; | 12 using std::string; |
15 | 13 |
16 namespace net { | 14 namespace net { |
17 | 15 |
18 // Defined kKeySize for GetKeySize() and SetKey(). | 16 // Defined kKeySize for GetKeySize() and SetKey(). |
19 static const size_t kKeySize = 16; | 17 static const size_t kKeySize = 16; |
20 | 18 |
21 // kBoxNonceSize contains the number of bytes of nonce that we use in each box. | 19 // kBoxNonceSize contains the number of bytes of nonce that we use in each box. |
22 static const size_t kBoxNonceSize = 16; | 20 // TODO(rtenneti): Add support for kBoxNonceSize to be 16 bytes. |
21 // | |
22 // From agl@: | |
23 // 96-bit nonces are on the edge. An attacker who can collect 2^41 | |
24 // source-address tokens has a 1% chance of finding a duplicate. | |
25 // | |
26 // The "average" DDoS is now 32.4M PPS. That's 2^25 source-address tokens | |
27 // per second. So one day of that DDoS botnot would reach the 1% mark. | |
28 // | |
29 // It's not terrible, but it's not a "forget about it" margin. | |
30 static const size_t kBoxNonceSize = 12; | |
31 | |
32 CryptoSecretBoxer::CryptoSecretBoxer() | |
33 : encrypter_(QuicEncrypter::Create(kAESG)), | |
34 decrypter_(QuicDecrypter::Create(kAESG)) { | |
35 } | |
36 | |
37 CryptoSecretBoxer::~CryptoSecretBoxer() {} | |
23 | 38 |
24 // static | 39 // static |
25 size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; } | 40 size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; } |
26 | 41 |
27 void CryptoSecretBoxer::SetKey(StringPiece key) { | 42 bool CryptoSecretBoxer::SetKey(StringPiece key) { |
28 DCHECK_EQ(static_cast<size_t>(kKeySize), key.size()); | 43 DCHECK_EQ(static_cast<size_t>(kKeySize), key.size()); |
wtc
2014/04/28 18:44:58
Please remove the static_cast. kKeySize is already
ramant (doing other things)
2014/04/29 00:31:18
Done.
| |
29 key_ = key.as_string(); | 44 string key_string = key.as_string(); |
wtc
2014/04/28 18:44:58
It is not necessary to convert |key| to a string.
ramant (doing other things)
2014/04/29 00:31:18
Done.
| |
45 if (!encrypter_->SetKey(key_string)) { | |
46 DLOG(DFATAL) << "CryptoSecretBoxer's encrypter_->SetKey failed."; | |
47 return false; | |
48 } | |
49 if (!decrypter_->SetKey(key_string)) { | |
50 DLOG(DFATAL) << "CryptoSecretBoxer's decrypter_->SetKey failed."; | |
51 return false; | |
52 } | |
53 return true; | |
30 } | 54 } |
31 | 55 |
32 // TODO(rtenneti): Delete sha256 based code. Use Aes128Gcm12Encrypter to Box the | |
33 // plaintext. This is temporary solution for tests to pass. | |
34 string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const { | 56 string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const { |
57 DCHECK_EQ(kKeySize, encrypter_->GetKeySize()); | |
wtc
2014/04/28 18:44:58
I think this DCHECK can be deleted. Alternatively,
ramant (doing other things)
2014/04/29 00:31:18
Done.
Deleted the DECHEK_EQ.
Wanted to make sur
| |
58 size_t ciphertext_size = encrypter_->GetCiphertextSize(plaintext.length()); | |
59 | |
35 string ret; | 60 string ret; |
36 const size_t len = kBoxNonceSize + plaintext.size() + crypto::kSHA256Length; | 61 const size_t len = kBoxNonceSize + ciphertext_size; |
37 ret.resize(len); | 62 ret.resize(len); |
38 char* data = &ret[0]; | 63 char* data = &ret[0]; |
39 | 64 |
40 // Generate nonce. | 65 // Generate nonce. |
41 rand->RandBytes(data, kBoxNonceSize); | 66 rand->RandBytes(data, kBoxNonceSize); |
42 memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size()); | 67 memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size()); |
43 | 68 |
44 // Compute sha256 for nonce + plaintext. | 69 if (!encrypter_->Encrypt(StringPiece(data, kBoxNonceSize), StringPiece(), |
45 scoped_ptr<crypto::SecureHash> sha256(crypto::SecureHash::Create( | 70 plaintext, reinterpret_cast<unsigned char*>( |
46 crypto::SecureHash::SHA256)); | 71 data + kBoxNonceSize))) { |
47 sha256->Update(data, kBoxNonceSize + plaintext.size()); | 72 DLOG(DFATAL) << "CryptoSecretBoxer's Encrypt failed."; |
48 sha256->Finish(data + kBoxNonceSize + plaintext.size(), | 73 return string(); |
49 crypto::kSHA256Length); | 74 } |
50 | 75 |
51 return ret; | 76 return ret; |
52 } | 77 } |
53 | 78 |
54 // TODO(rtenneti): Delete sha256 based code. Use Aes128Gcm12Decrypter to Unbox | |
55 // the plaintext. This is temporary solution for tests to pass. | |
56 bool CryptoSecretBoxer::Unbox(StringPiece ciphertext, | 79 bool CryptoSecretBoxer::Unbox(StringPiece ciphertext, |
57 string* out_storage, | 80 string* out_storage, |
58 StringPiece* out) const { | 81 StringPiece* out) const { |
59 if (ciphertext.size() < kBoxNonceSize + crypto::kSHA256Length) { | 82 if (ciphertext.size() < kBoxNonceSize) { |
60 return false; | 83 return false; |
61 } | 84 } |
62 | 85 |
63 const size_t plaintext_len = | 86 char nonce[kBoxNonceSize]; |
64 ciphertext.size() - kBoxNonceSize - crypto::kSHA256Length; | 87 memcpy(nonce, ciphertext.data(), kBoxNonceSize); |
65 out_storage->resize(plaintext_len); | 88 ciphertext.remove_prefix(kBoxNonceSize); |
89 | |
90 size_t len = ciphertext.size(); | |
91 out_storage->resize(len); | |
66 char* data = const_cast<char*>(out_storage->data()); | 92 char* data = const_cast<char*>(out_storage->data()); |
67 | 93 |
68 // Copy plaintext from ciphertext. | 94 if (!decrypter_->Decrypt(StringPiece(nonce, kBoxNonceSize), StringPiece(), |
69 if (plaintext_len != 0) { | 95 ciphertext, reinterpret_cast<unsigned char*>(data), |
70 memcpy(data, ciphertext.data() + kBoxNonceSize, plaintext_len); | 96 &len)) { |
71 } | |
72 | |
73 // Compute sha256 for nonce + plaintext. | |
74 scoped_ptr<crypto::SecureHash> sha256(crypto::SecureHash::Create( | |
75 crypto::SecureHash::SHA256)); | |
76 sha256->Update(ciphertext.data(), ciphertext.size() - crypto::kSHA256Length); | |
77 char sha256_bytes[crypto::kSHA256Length]; | |
78 sha256->Finish(sha256_bytes, sizeof(sha256_bytes)); | |
79 | |
80 // Verify sha256. | |
81 if (0 != memcmp(ciphertext.data() + ciphertext.size() - crypto::kSHA256Length, | |
82 sha256_bytes, crypto::kSHA256Length)) { | |
83 return false; | 97 return false; |
84 } | 98 } |
85 | 99 |
86 out->set(data, plaintext_len); | 100 out->set(data, len); |
87 return true; | 101 return true; |
88 } | 102 } |
89 | 103 |
90 } // namespace net | 104 } // namespace net |
OLD | NEW |