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

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

Issue 189893002: Add ChaCha20Poly1305Encrypter, based on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Export AeadBase*crypter Created 6 years, 9 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/aes_128_gcm_12_decrypter.h" 5 #include "net/quic/crypto/aead_base_decrypter.h"
6 6
7 #include <openssl/err.h> 7 #include <openssl/err.h>
8 #include <openssl/evp.h> 8 #include <openssl/evp.h>
9 9
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 11
12 using base::StringPiece; 12 using base::StringPiece;
13 13
14 namespace net { 14 namespace net {
15 15
16 namespace { 16 namespace {
17 17
18 const size_t kNoncePrefixSize = 4; 18 // Clear OpenSSL error stack.
19 const size_t kAESNonceSize = 12; 19 void ClearOpenSslErrors() {
20 #ifdef NDEBUG
21 while (ERR_get_error()) {}
22 #else
23 while (unsigned long error = ERR_get_error()) {
24 char buf[120];
25 ERR_error_string_n(error, buf, arraysize(buf));
26 DLOG(ERROR) << "OpenSSL error: " << buf;
27 }
28 #endif
29 }
20 30
21 } // namespace 31 } // namespace
22 32
23 Aes128Gcm12Decrypter::Aes128Gcm12Decrypter() {} 33 AeadBaseDecrypter::AeadBaseDecrypter(const EVP_AEAD* aead_alg,
34 size_t key_size,
35 size_t auth_tag_size,
36 size_t nonce_prefix_size)
37 : aead_alg_(aead_alg),
38 key_size_(key_size),
39 auth_tag_size_(auth_tag_size),
40 nonce_prefix_size_(nonce_prefix_size) {
41 DCHECK_LE(key_size_, sizeof(key_));
42 DCHECK_LE(nonce_prefix_size_, sizeof(nonce_prefix_));
43 }
24 44
25 Aes128Gcm12Decrypter::~Aes128Gcm12Decrypter() {} 45 AeadBaseDecrypter::~AeadBaseDecrypter() {}
26 46
27 // static 47 bool AeadBaseDecrypter::SetKey(StringPiece key) {
28 bool Aes128Gcm12Decrypter::IsSupported() { return true; } 48 DCHECK_EQ(key.size(), key_size_);
29 49 if (key.size() != key_size_) {
30 bool Aes128Gcm12Decrypter::SetKey(StringPiece key) {
31 DCHECK_EQ(key.size(), sizeof(key_));
32 if (key.size() != sizeof(key_)) {
33 return false; 50 return false;
34 } 51 }
35 memcpy(key_, key.data(), key.size()); 52 memcpy(key_, key.data(), key.size());
36 53
37 EVP_AEAD_CTX_cleanup(ctx_.get()); 54 EVP_AEAD_CTX_cleanup(ctx_.get());
38 if (!EVP_AEAD_CTX_init(ctx_.get(), EVP_aead_aes_128_gcm(), key_, 55 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_,
39 sizeof(key_), kAuthTagSize, NULL)) { 56 auth_tag_size_, NULL)) {
40 // Clear OpenSSL error stack. 57 ClearOpenSslErrors();
41 while (ERR_get_error()) {}
42 return false; 58 return false;
43 } 59 }
44 60
45 return true; 61 return true;
46 } 62 }
47 63
48 bool Aes128Gcm12Decrypter::SetNoncePrefix(StringPiece nonce_prefix) { 64 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) {
49 DCHECK_EQ(nonce_prefix.size(), kNoncePrefixSize); 65 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_);
50 if (nonce_prefix.size() != kNoncePrefixSize) { 66 if (nonce_prefix.size() != nonce_prefix_size_) {
51 return false; 67 return false;
52 } 68 }
53 COMPILE_ASSERT(sizeof(nonce_prefix_) == kNoncePrefixSize, bad_nonce_length);
54 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); 69 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size());
55 return true; 70 return true;
56 } 71 }
57 72
58 bool Aes128Gcm12Decrypter::Decrypt(StringPiece nonce, 73 bool AeadBaseDecrypter::Decrypt(StringPiece nonce,
59 StringPiece associated_data, 74 StringPiece associated_data,
60 StringPiece ciphertext, 75 StringPiece ciphertext,
61 uint8* output, 76 uint8* output,
62 size_t* output_length) { 77 size_t* output_length) {
63 if (ciphertext.length() < kAuthTagSize || 78 if (ciphertext.length() < auth_tag_size_ ||
64 nonce.size() != kNoncePrefixSize + sizeof(QuicPacketSequenceNumber)) { 79 nonce.size() != nonce_prefix_size_ + sizeof(QuicPacketSequenceNumber)) {
65 return false; 80 return false;
66 } 81 }
67 82
68 ssize_t len = EVP_AEAD_CTX_open( 83 ssize_t len = EVP_AEAD_CTX_open(
69 ctx_.get(), output, ciphertext.size(), 84 ctx_.get(), output, ciphertext.size(),
70 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), 85 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(),
71 reinterpret_cast<const uint8_t*>(ciphertext.data()), ciphertext.size(), 86 reinterpret_cast<const uint8_t*>(ciphertext.data()), ciphertext.size(),
72 reinterpret_cast<const uint8_t*>(associated_data.data()), 87 reinterpret_cast<const uint8_t*>(associated_data.data()),
73 associated_data.size()); 88 associated_data.size());
74 89
75 if (len < 0) { 90 if (len < 0) {
76 // Clear OpenSSL error stack. 91 ClearOpenSslErrors();
77 while (ERR_get_error()) {}
78 return false; 92 return false;
79 } 93 }
80 94
81 *output_length = len; 95 *output_length = len;
82 return true; 96 return true;
83 } 97 }
84 98
85 QuicData* Aes128Gcm12Decrypter::DecryptPacket( 99 QuicData* AeadBaseDecrypter::DecryptPacket(
86 QuicPacketSequenceNumber sequence_number, 100 QuicPacketSequenceNumber sequence_number,
87 StringPiece associated_data, 101 StringPiece associated_data,
88 StringPiece ciphertext) { 102 StringPiece ciphertext) {
89 if (ciphertext.length() < kAuthTagSize) { 103 if (ciphertext.length() < auth_tag_size_) {
90 return NULL; 104 return NULL;
91 } 105 }
92 size_t plaintext_size = ciphertext.length(); 106 size_t plaintext_size = ciphertext.length();
93 scoped_ptr<char[]> plaintext(new char[plaintext_size]); 107 scoped_ptr<char[]> plaintext(new char[plaintext_size]);
94 108
95 uint8 nonce[kNoncePrefixSize + sizeof(sequence_number)]; 109 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)];
96 COMPILE_ASSERT(sizeof(nonce) == kAESNonceSize, bad_sequence_number_size); 110 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number);
97 memcpy(nonce, nonce_prefix_, kNoncePrefixSize); 111 DCHECK_LE(nonce_size, sizeof(nonce));
98 memcpy(nonce + kNoncePrefixSize, &sequence_number, sizeof(sequence_number)); 112 memcpy(nonce, nonce_prefix_, nonce_prefix_size_);
99 if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce), sizeof(nonce)), 113 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number));
114 if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size),
100 associated_data, ciphertext, 115 associated_data, ciphertext,
101 reinterpret_cast<uint8*>(plaintext.get()), 116 reinterpret_cast<uint8*>(plaintext.get()),
102 &plaintext_size)) { 117 &plaintext_size)) {
103 return NULL; 118 return NULL;
104 } 119 }
105 return new QuicData(plaintext.release(), plaintext_size, true); 120 return new QuicData(plaintext.release(), plaintext_size, true);
106 } 121 }
107 122
108 StringPiece Aes128Gcm12Decrypter::GetKey() const { 123 StringPiece AeadBaseDecrypter::GetKey() const {
109 return StringPiece(reinterpret_cast<const char*>(key_), sizeof(key_)); 124 return StringPiece(reinterpret_cast<const char*>(key_), key_size_);
110 } 125 }
111 126
112 StringPiece Aes128Gcm12Decrypter::GetNoncePrefix() const { 127 StringPiece AeadBaseDecrypter::GetNoncePrefix() const {
128 if (nonce_prefix_size_ == 0) {
129 return StringPiece();
130 }
113 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), 131 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_),
114 kNoncePrefixSize); 132 nonce_prefix_size_);
115 } 133 }
116 134
117 } // namespace net 135 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698