blob: ab441b7946676f3ffaf6f2d07032d38c9def51af [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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.
4
5#include "net/third_party/quiche/src/quic/core/crypto/aead_base_decrypter.h"
6
7#include <cstdint>
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009
10#include "third_party/boringssl/src/include/openssl/crypto.h"
11#include "third_party/boringssl/src/include/openssl/err.h"
12#include "third_party/boringssl/src/include/openssl/evp.h"
13#include "net/third_party/quiche/src/quic/core/quic_utils.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
15#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
16#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050017
18namespace quic {
19
20namespace {
21
22// Clear OpenSSL error stack.
23void ClearOpenSslErrors() {
24 while (ERR_get_error()) {
25 }
26}
27
28// In debug builds only, log OpenSSL error stack. Then clear OpenSSL error
29// stack.
30void DLogOpenSslErrors() {
31#ifdef NDEBUG
32 ClearOpenSslErrors();
33#else
34 while (uint32_t error = ERR_get_error()) {
35 char buf[120];
36 ERR_error_string_n(error, buf, QUIC_ARRAYSIZE(buf));
37 QUIC_DLOG(ERROR) << "OpenSSL error: " << buf;
38 }
39#endif
40}
41
42const EVP_AEAD* InitAndCall(const EVP_AEAD* (*aead_getter)()) {
43 // Ensure BoringSSL is initialized before calling |aead_getter|. In Chromium,
44 // the static initializer is disabled.
45 CRYPTO_library_init();
46 return aead_getter();
47}
48
49} // namespace
50
51AeadBaseDecrypter::AeadBaseDecrypter(const EVP_AEAD* (*aead_getter)(),
52 size_t key_size,
53 size_t auth_tag_size,
54 size_t nonce_size,
55 bool use_ietf_nonce_construction)
56 : aead_alg_(InitAndCall(aead_getter)),
57 key_size_(key_size),
58 auth_tag_size_(auth_tag_size),
59 nonce_size_(nonce_size),
60 use_ietf_nonce_construction_(use_ietf_nonce_construction),
61 have_preliminary_key_(false) {
62 DCHECK_GT(256u, key_size);
63 DCHECK_GT(256u, auth_tag_size);
64 DCHECK_GT(256u, nonce_size);
65 DCHECK_LE(key_size_, sizeof(key_));
66 DCHECK_LE(nonce_size_, sizeof(iv_));
67}
68
69AeadBaseDecrypter::~AeadBaseDecrypter() {}
70
71bool AeadBaseDecrypter::SetKey(QuicStringPiece key) {
72 DCHECK_EQ(key.size(), key_size_);
73 if (key.size() != key_size_) {
74 return false;
75 }
76 memcpy(key_, key.data(), key.size());
77
78 EVP_AEAD_CTX_cleanup(ctx_.get());
79 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, auth_tag_size_,
80 nullptr)) {
81 DLogOpenSslErrors();
82 return false;
83 }
84
85 return true;
86}
87
88bool AeadBaseDecrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) {
89 if (use_ietf_nonce_construction_) {
90 QUIC_BUG << "Attempted to set nonce prefix on IETF QUIC crypter";
91 return false;
92 }
93 DCHECK_EQ(nonce_prefix.size(), nonce_size_ - sizeof(QuicPacketNumber));
94 if (nonce_prefix.size() != nonce_size_ - sizeof(QuicPacketNumber)) {
95 return false;
96 }
97 memcpy(iv_, nonce_prefix.data(), nonce_prefix.size());
98 return true;
99}
100
101bool AeadBaseDecrypter::SetIV(QuicStringPiece iv) {
102 if (!use_ietf_nonce_construction_) {
103 QUIC_BUG << "Attempted to set IV on Google QUIC crypter";
104 return false;
105 }
106 DCHECK_EQ(iv.size(), nonce_size_);
107 if (iv.size() != nonce_size_) {
108 return false;
109 }
110 memcpy(iv_, iv.data(), iv.size());
111 return true;
112}
113
114bool AeadBaseDecrypter::SetPreliminaryKey(QuicStringPiece key) {
115 DCHECK(!have_preliminary_key_);
116 SetKey(key);
117 have_preliminary_key_ = true;
118
119 return true;
120}
121
122bool AeadBaseDecrypter::SetDiversificationNonce(
123 const DiversificationNonce& nonce) {
124 if (!have_preliminary_key_) {
125 return true;
126 }
127
vasilvvc48c8712019-03-11 13:38:16 -0700128 std::string key, nonce_prefix;
nharperc1bbfe62019-09-27 16:48:40 -0700129 size_t prefix_size = nonce_size_;
130 if (!use_ietf_nonce_construction_) {
131 prefix_size -= sizeof(QuicPacketNumber);
132 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500133 DiversifyPreliminaryKey(
134 QuicStringPiece(reinterpret_cast<const char*>(key_), key_size_),
135 QuicStringPiece(reinterpret_cast<const char*>(iv_), prefix_size), nonce,
136 key_size_, prefix_size, &key, &nonce_prefix);
137
nharperc1bbfe62019-09-27 16:48:40 -0700138 if (!SetKey(key) ||
139 (!use_ietf_nonce_construction_ && !SetNoncePrefix(nonce_prefix)) ||
140 (use_ietf_nonce_construction_ && !SetIV(nonce_prefix))) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500141 DCHECK(false);
142 return false;
143 }
144
145 have_preliminary_key_ = false;
146 return true;
147}
148
149bool AeadBaseDecrypter::DecryptPacket(uint64_t packet_number,
150 QuicStringPiece associated_data,
151 QuicStringPiece ciphertext,
152 char* output,
153 size_t* output_length,
154 size_t max_output_length) {
155 if (ciphertext.length() < auth_tag_size_) {
156 return false;
157 }
158
159 if (have_preliminary_key_) {
160 QUIC_BUG << "Unable to decrypt while key diversification is pending";
161 return false;
162 }
163
164 uint8_t nonce[kMaxNonceSize];
165 memcpy(nonce, iv_, nonce_size_);
166 size_t prefix_len = nonce_size_ - sizeof(packet_number);
167 if (use_ietf_nonce_construction_) {
168 for (size_t i = 0; i < sizeof(packet_number); ++i) {
169 nonce[prefix_len + i] ^=
170 (packet_number >> ((sizeof(packet_number) - i - 1) * 8)) & 0xff;
171 }
172 } else {
173 memcpy(nonce + prefix_len, &packet_number, sizeof(packet_number));
174 }
175 if (!EVP_AEAD_CTX_open(
176 ctx_.get(), reinterpret_cast<uint8_t*>(output), output_length,
177 max_output_length, reinterpret_cast<const uint8_t*>(nonce),
178 nonce_size_, reinterpret_cast<const uint8_t*>(ciphertext.data()),
179 ciphertext.size(),
180 reinterpret_cast<const uint8_t*>(associated_data.data()),
181 associated_data.size())) {
182 // Because QuicFramer does trial decryption, decryption errors are expected
183 // when encryption level changes. So we don't log decryption errors.
184 ClearOpenSslErrors();
185 return false;
186 }
187 return true;
188}
189
190size_t AeadBaseDecrypter::GetKeySize() const {
191 return key_size_;
192}
193
nharper965e5922019-09-23 22:33:54 -0700194size_t AeadBaseDecrypter::GetNoncePrefixSize() const {
195 return nonce_size_ - sizeof(QuicPacketNumber);
196}
197
QUICHE teama6ef0a62019-03-07 20:34:33 -0500198size_t AeadBaseDecrypter::GetIVSize() const {
199 return nonce_size_;
200}
201
202QuicStringPiece AeadBaseDecrypter::GetKey() const {
203 return QuicStringPiece(reinterpret_cast<const char*>(key_), key_size_);
204}
205
206QuicStringPiece AeadBaseDecrypter::GetNoncePrefix() const {
207 return QuicStringPiece(reinterpret_cast<const char*>(iv_),
208 nonce_size_ - sizeof(QuicPacketNumber));
209}
210
211} // namespace quic