Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: net/quic/crypto/crypto_secret_boxer.cc

Issue 213473003: This change introduces a way to tie source address token keys to specific QUIC server configs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge internal change: 65382861 Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/memory/scoped_ptr.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;
23 31
24 // static 32 // static
25 size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; } 33 size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; }
wtc 2014/04/22 19:12:10 Please move the definition of this static method a
ramant (doing other things) 2014/04/25 23:40:58 Done.
26 34
35 CryptoSecretBoxer::CryptoSecretBoxer()
36 : encrypter_(QuicEncrypter::Create(kAESG)),
37 decrypter_(QuicDecrypter::Create(kAESG)) {
38 }
39
40 CryptoSecretBoxer::~CryptoSecretBoxer() {}
41
27 void CryptoSecretBoxer::SetKey(StringPiece key) { 42 void CryptoSecretBoxer::SetKey(StringPiece key) {
28 DCHECK_EQ(static_cast<size_t>(kKeySize), key.size()); 43 DCHECK_EQ(static_cast<size_t>(kKeySize), key.size());
29 key_ = key.as_string(); 44 key_ = key.as_string();
30 } 45 }
31 46
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 { 47 string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const {
48 if (!encrypter_->SetKey(key_)) {
49 DLOG(DFATAL) << "CryptoSecretBoxer's SetKey failed.";
50 return string();
51 }
wtc 2014/04/22 19:12:10 Lines 48-51 should be deleted. Then, change SetKey
ramant (doing other things) 2014/04/25 23:40:58 Done.
52 size_t ciphertext_size = encrypter_->GetCiphertextSize(plaintext.length());
53
35 string ret; 54 string ret;
36 const size_t len = kBoxNonceSize + plaintext.size() + crypto::kSHA256Length; 55 const size_t len = kBoxNonceSize + ciphertext_size;
37 ret.resize(len); 56 ret.resize(len);
38 char* data = &ret[0]; 57 char* data = &ret[0];
39 58
40 // Generate nonce. 59 // Generate nonce.
41 rand->RandBytes(data, kBoxNonceSize); 60 rand->RandBytes(data, kBoxNonceSize);
42 memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size()); 61 memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size());
43 62
44 // Compute sha256 for nonce + plaintext. 63 if (!encrypter_->Encrypt(StringPiece(data, kBoxNonceSize), StringPiece(),
45 scoped_ptr<crypto::SecureHash> sha256(crypto::SecureHash::Create( 64 plaintext, reinterpret_cast<unsigned char*>(
46 crypto::SecureHash::SHA256)); 65 data + kBoxNonceSize))) {
47 sha256->Update(data, kBoxNonceSize + plaintext.size()); 66 DLOG(DFATAL) << "CryptoSecretBoxer's Encrypt failed.";
48 sha256->Finish(data + kBoxNonceSize + plaintext.size(), 67 return string();
49 crypto::kSHA256Length); 68 }
50 69
51 return ret; 70 return ret;
52 } 71 }
53 72
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, 73 bool CryptoSecretBoxer::Unbox(StringPiece ciphertext,
57 string* out_storage, 74 string* out_storage,
58 StringPiece* out) const { 75 StringPiece* out) const {
59 if (ciphertext.size() < kBoxNonceSize + crypto::kSHA256Length) { 76 if (ciphertext.size() < kBoxNonceSize) {
60 return false; 77 return false;
61 } 78 }
62 79
63 const size_t plaintext_len = 80 char nonce[kBoxNonceSize];
64 ciphertext.size() - kBoxNonceSize - crypto::kSHA256Length; 81 memcpy(nonce, ciphertext.data(), kBoxNonceSize);
65 out_storage->resize(plaintext_len); 82 ciphertext.remove_prefix(kBoxNonceSize);
83
84 size_t len = ciphertext.size();
85 out_storage->resize(len);
66 char* data = const_cast<char*>(out_storage->data()); 86 char* data = const_cast<char*>(out_storage->data());
67 87
68 // Copy plaintext from ciphertext. 88 decrypter_->SetKey(key_);
wtc 2014/04/22 19:12:10 As noted above, remove this line. decrypter_->SetK
ramant (doing other things) 2014/04/25 23:40:58 Done.
69 if (plaintext_len != 0) { 89 if (!decrypter_->Decrypt(StringPiece(nonce, kBoxNonceSize), StringPiece(),
70 memcpy(data, ciphertext.data() + kBoxNonceSize, plaintext_len); 90 ciphertext, reinterpret_cast<unsigned char*>(data),
71 } 91 &len)) {
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; 92 return false;
84 } 93 }
85 94
86 out->set(data, plaintext_len); 95 out->set(data, len);
87 return true; 96 return true;
88 } 97 }
89 98
90 } // namespace net 99 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698