[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / quic / crypto / crypto_secret_boxer.cc
bloba7898d995a43e53cc8853a09bd7155dc6f1ebbba
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.
5 #include "net/quic/crypto/crypto_secret_boxer.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "net/quic/crypto/aes_128_gcm_12_decrypter.h"
10 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
11 #include "net/quic/crypto/crypto_protocol.h"
12 #include "net/quic/crypto/quic_decrypter.h"
13 #include "net/quic/crypto/quic_encrypter.h"
14 #include "net/quic/crypto/quic_random.h"
16 using base::StringPiece;
17 using std::string;
19 namespace net {
21 // Defined kKeySize for GetKeySize() and SetKey().
22 static const size_t kKeySize = 16;
24 // kBoxNonceSize contains the number of bytes of nonce that we use in each box.
25 // TODO(rtenneti): Add support for kBoxNonceSize to be 16 bytes.
27 // From agl@:
28 // 96-bit nonces are on the edge. An attacker who can collect 2^41
29 // source-address tokens has a 1% chance of finding a duplicate.
31 // The "average" DDoS is now 32.4M PPS. That's 2^25 source-address tokens
32 // per second. So one day of that DDoS botnot would reach the 1% mark.
34 // It's not terrible, but it's not a "forget about it" margin.
35 static const size_t kBoxNonceSize = 12;
37 // static
38 size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; }
40 void CryptoSecretBoxer::SetKey(StringPiece key) {
41 DCHECK_EQ(kKeySize, key.size());
42 key_ = key.as_string();
45 string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const {
46 scoped_ptr<Aes128Gcm12Encrypter> encrypter(new Aes128Gcm12Encrypter());
47 if (!encrypter->SetKey(key_)) {
48 DLOG(DFATAL) << "CryptoSecretBoxer's encrypter->SetKey failed.";
49 return string();
51 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length());
53 string ret;
54 const size_t len = kBoxNonceSize + ciphertext_size;
55 ret.resize(len);
56 char* data = &ret[0];
58 // Generate nonce.
59 rand->RandBytes(data, kBoxNonceSize);
60 memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size());
62 if (!encrypter->Encrypt(StringPiece(data, kBoxNonceSize), StringPiece(),
63 plaintext, reinterpret_cast<unsigned char*>(
64 data + kBoxNonceSize))) {
65 DLOG(DFATAL) << "CryptoSecretBoxer's Encrypt failed.";
66 return string();
69 return ret;
72 bool CryptoSecretBoxer::Unbox(StringPiece ciphertext,
73 string* out_storage,
74 StringPiece* out) const {
75 if (ciphertext.size() < kBoxNonceSize) {
76 return false;
79 StringPiece nonce(ciphertext.data(), kBoxNonceSize);
80 ciphertext.remove_prefix(kBoxNonceSize);
81 QuicPacketSequenceNumber sequence_number;
82 StringPiece nonce_prefix(nonce.data(),
83 nonce.size() - sizeof(sequence_number));
84 memcpy(&sequence_number, nonce.data() + nonce_prefix.size(),
85 sizeof(sequence_number));
87 scoped_ptr<Aes128Gcm12Decrypter> decrypter(new Aes128Gcm12Decrypter());
88 if (!decrypter->SetKey(key_)) {
89 DLOG(DFATAL) << "CryptoSecretBoxer's decrypter->SetKey failed.";
90 return false;
92 decrypter->SetNoncePrefix(nonce_prefix);
93 char plaintext[kMaxPacketSize];
94 size_t plaintext_length = 0;
95 const bool success = decrypter->DecryptPacket(
96 sequence_number, StringPiece() /* associated data */, ciphertext,
97 plaintext, &plaintext_length, kMaxPacketSize);
98 if (!success) {
99 return false;
102 out_storage->resize(plaintext_length);
103 out_storage->assign(plaintext, plaintext_length);
104 out->set(out_storage->data(), plaintext_length);
105 return true;
108 } // namespace net