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

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

Issue 257123002: CryptoSecretBoxer cleanup changes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with TOT Created 6 years, 7 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 "net/quic/crypto/crypto_protocol.h" 9 #include "net/quic/crypto/crypto_protocol.h"
10 #include "net/quic/crypto/quic_decrypter.h"
11 #include "net/quic/crypto/quic_encrypter.h"
9 #include "net/quic/crypto/quic_random.h" 12 #include "net/quic/crypto/quic_random.h"
10 13
11 using base::StringPiece; 14 using base::StringPiece;
12 using std::string; 15 using std::string;
13 16
14 namespace net { 17 namespace net {
15 18
16 // Defined kKeySize for GetKeySize() and SetKey(). 19 // Defined kKeySize for GetKeySize() and SetKey().
17 static const size_t kKeySize = 16; 20 static const size_t kKeySize = 16;
18 21
19 // kBoxNonceSize contains the number of bytes of nonce that we use in each box. 22 // kBoxNonceSize contains the number of bytes of nonce that we use in each box.
20 // TODO(rtenneti): Add support for kBoxNonceSize to be 16 bytes. 23 // TODO(rtenneti): Add support for kBoxNonceSize to be 16 bytes.
21 // 24 //
22 // From agl@: 25 // From agl@:
23 // 96-bit nonces are on the edge. An attacker who can collect 2^41 26 // 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. 27 // source-address tokens has a 1% chance of finding a duplicate.
25 // 28 //
26 // The "average" DDoS is now 32.4M PPS. That's 2^25 source-address tokens 29 // 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. 30 // per second. So one day of that DDoS botnot would reach the 1% mark.
28 // 31 //
29 // It's not terrible, but it's not a "forget about it" margin. 32 // It's not terrible, but it's not a "forget about it" margin.
30 static const size_t kBoxNonceSize = 12; 33 static const size_t kBoxNonceSize = 12;
31 34
32 CryptoSecretBoxer::CryptoSecretBoxer()
33 : encrypter_(QuicEncrypter::Create(kAESG)),
34 decrypter_(QuicDecrypter::Create(kAESG)) {
35 }
36
37 CryptoSecretBoxer::~CryptoSecretBoxer() {}
38
39 // static 35 // static
40 size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; } 36 size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; }
41 37
42 bool CryptoSecretBoxer::SetKey(StringPiece key) { 38 void CryptoSecretBoxer::SetKey(StringPiece key) {
43 DCHECK_EQ(static_cast<size_t>(kKeySize), key.size()); 39 DCHECK_EQ(kKeySize, key.size());
44 string key_string = key.as_string(); 40 key_ = key.as_string();
ramant (doing other things) 2014/04/29 00:41:49 SetKey code is similar to the internal source.
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;
54 } 41 }
55 42
56 string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const { 43 string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const {
57 DCHECK_EQ(kKeySize, encrypter_->GetKeySize()); 44 scoped_ptr<QuicEncrypter> encrypter(QuicEncrypter::Create(kAESG));
58 size_t ciphertext_size = encrypter_->GetCiphertextSize(plaintext.length()); 45 if (!encrypter->SetKey(key_)) {
46 DLOG(DFATAL) << "CryptoSecretBoxer's encrypter->SetKey failed.";
47 return string();
48 }
49 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length());
59 50
60 string ret; 51 string ret;
61 const size_t len = kBoxNonceSize + ciphertext_size; 52 const size_t len = kBoxNonceSize + ciphertext_size;
62 ret.resize(len); 53 ret.resize(len);
63 char* data = &ret[0]; 54 char* data = &ret[0];
64 55
65 // Generate nonce. 56 // Generate nonce.
66 rand->RandBytes(data, kBoxNonceSize); 57 rand->RandBytes(data, kBoxNonceSize);
67 memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size()); 58 memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size());
68 59
69 if (!encrypter_->Encrypt(StringPiece(data, kBoxNonceSize), StringPiece(), 60 if (!encrypter->Encrypt(StringPiece(data, kBoxNonceSize), StringPiece(),
70 plaintext, reinterpret_cast<unsigned char*>( 61 plaintext, reinterpret_cast<unsigned char*>(
71 data + kBoxNonceSize))) { 62 data + kBoxNonceSize))) {
72 DLOG(DFATAL) << "CryptoSecretBoxer's Encrypt failed."; 63 DLOG(DFATAL) << "CryptoSecretBoxer's Encrypt failed.";
73 return string(); 64 return string();
74 } 65 }
75 66
76 return ret; 67 return ret;
77 } 68 }
78 69
79 bool CryptoSecretBoxer::Unbox(StringPiece ciphertext, 70 bool CryptoSecretBoxer::Unbox(StringPiece ciphertext,
80 string* out_storage, 71 string* out_storage,
81 StringPiece* out) const { 72 StringPiece* out) const {
82 if (ciphertext.size() < kBoxNonceSize) { 73 if (ciphertext.size() < kBoxNonceSize) {
83 return false; 74 return false;
84 } 75 }
85 76
86 char nonce[kBoxNonceSize]; 77 char nonce[kBoxNonceSize];
87 memcpy(nonce, ciphertext.data(), kBoxNonceSize); 78 memcpy(nonce, ciphertext.data(), kBoxNonceSize);
88 ciphertext.remove_prefix(kBoxNonceSize); 79 ciphertext.remove_prefix(kBoxNonceSize);
89 80
90 size_t len = ciphertext.size(); 81 size_t len = ciphertext.size();
91 out_storage->resize(len); 82 out_storage->resize(len);
92 char* data = const_cast<char*>(out_storage->data()); 83 char* data = const_cast<char*>(out_storage->data());
93 84
94 if (!decrypter_->Decrypt(StringPiece(nonce, kBoxNonceSize), StringPiece(), 85 scoped_ptr<QuicDecrypter> decrypter(QuicDecrypter::Create(kAESG));
95 ciphertext, reinterpret_cast<unsigned char*>(data), 86 if (!decrypter->SetKey(key_)) {
96 &len)) { 87 DLOG(DFATAL) << "CryptoSecretBoxer's decrypter->SetKey failed.";
88 return false;
89 }
90 if (!decrypter->Decrypt(StringPiece(nonce, kBoxNonceSize), StringPiece(),
91 ciphertext, reinterpret_cast<unsigned char*>(data),
92 &len)) {
97 return false; 93 return false;
98 } 94 }
99 95
100 out->set(data, len); 96 out->set(data, len);
101 return true; 97 return true;
102 } 98 }
103 99
104 } // namespace net 100 } // namespace net
OLDNEW
« net/quic/crypto/crypto_secret_boxer.h ('K') | « net/quic/crypto/crypto_secret_boxer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698