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

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

Issue 189893002: Add ChaCha20Poly1305Encrypter, based on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Move GCM-specific code back to the Aes128Gcm12En/Decrypter classes 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_encrypter.h" 5 #include "net/quic/crypto/aead_base_encrypter.h"
6 6
7 #include <nss.h>
8 #include <pk11pub.h> 7 #include <pk11pub.h>
9 #include <secerr.h>
10 8
11 #include "base/lazy_instance.h"
12 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
13 #include "crypto/ghash.h"
14 #include "crypto/scoped_nss_types.h" 10 #include "crypto/scoped_nss_types.h"
15 11
16 #if defined(USE_NSS)
17 #include <dlfcn.h>
18 #endif
19
20 using base::StringPiece; 12 using base::StringPiece;
21 13
22 namespace net { 14 namespace net {
23 15
24 namespace { 16 AeadBaseEncrypter::AeadBaseEncrypter(CK_MECHANISM_TYPE aead_mechanism,
25 17 PK11_EncryptFunction pk11_encrypt,
26 // The pkcs11t.h header in NSS versions older than 3.14 does not have the CTR 18 size_t key_size,
27 // and GCM types, so define them here. 19 size_t auth_tag_size,
28 #if !defined(CKM_AES_CTR) 20 size_t nonce_prefix_size)
29 #define CKM_AES_CTR 0x00001086 21 : aead_mechanism_(aead_mechanism),
30 #define CKM_AES_GCM 0x00001087 22 pk11_encrypt_(pk11_encrypt),
31 23 key_size_(key_size),
32 struct CK_AES_CTR_PARAMS { 24 auth_tag_size_(auth_tag_size),
33 CK_ULONG ulCounterBits; 25 nonce_prefix_size_(nonce_prefix_size) {
34 CK_BYTE cb[16]; 26 DCHECK_LE(key_size_, sizeof(key_));
35 }; 27 DCHECK_LE(nonce_prefix_size_, sizeof(nonce_prefix_));
36
37 struct CK_GCM_PARAMS {
38 CK_BYTE_PTR pIv;
39 CK_ULONG ulIvLen;
40 CK_BYTE_PTR pAAD;
41 CK_ULONG ulAADLen;
42 CK_ULONG ulTagBits;
43 };
44 #endif // CKM_AES_CTR
45
46 typedef SECStatus
47 (*PK11_EncryptFunction)(
48 PK11SymKey* symKey, CK_MECHANISM_TYPE mechanism, SECItem* param,
49 unsigned char* out, unsigned int* outLen, unsigned int maxLen,
50 const unsigned char* data, unsigned int dataLen);
51
52 // On Linux, dynamically link against the system version of libnss3.so. In
53 // order to continue working on systems without up-to-date versions of NSS,
54 // lookup PK11_Encrypt with dlsym.
55
56 // GcmSupportChecker is a singleton which caches the results of runtime symbol
57 // resolution of PK11_Encrypt.
58 class GcmSupportChecker {
59 public:
60 static PK11_EncryptFunction pk11_encrypt_func() {
61 return pk11_encrypt_func_;
62 }
63
64 static CK_MECHANISM_TYPE aes_key_mechanism() {
65 return aes_key_mechanism_;
66 }
67
68 private:
69 friend struct base::DefaultLazyInstanceTraits<GcmSupportChecker>;
70
71 GcmSupportChecker() {
72 #if !defined(USE_NSS)
73 // Using a bundled version of NSS that is guaranteed to have this symbol.
74 pk11_encrypt_func_ = PK11_Encrypt;
75 #else
76 // Using system NSS libraries and PCKS #11 modules, which may not have the
77 // necessary function (PK11_Encrypt) or mechanism support (CKM_AES_GCM).
78
79 // If PK11_Encrypt() was successfully resolved, then NSS will support
80 // AES-GCM directly. This was introduced in NSS 3.15.
81 pk11_encrypt_func_ = (PK11_EncryptFunction)dlsym(RTLD_DEFAULT,
82 "PK11_Encrypt");
83 if (pk11_encrypt_func_ == NULL) {
84 aes_key_mechanism_ = CKM_AES_ECB;
85 }
86 #endif
87 }
88
89 // |pk11_encrypt_func_| stores the runtime symbol resolution of PK11_Encrypt.
90 static PK11_EncryptFunction pk11_encrypt_func_;
91
92 // The correct value for |aes_key_mechanism_| is CKM_AES_GCM, but because of
93 // NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=853285 (to be fixed in
94 // NSS 3.15), use CKM_AES_ECB for NSS versions older than 3.15.
95 static CK_MECHANISM_TYPE aes_key_mechanism_;
96 };
97
98 // static
99 PK11_EncryptFunction GcmSupportChecker::pk11_encrypt_func_ = NULL;
100
101 // static
102 CK_MECHANISM_TYPE GcmSupportChecker::aes_key_mechanism_ = CKM_AES_GCM;
103
104 base::LazyInstance<GcmSupportChecker>::Leaky g_gcm_support_checker =
105 LAZY_INSTANCE_INITIALIZER;
106
107 const size_t kKeySize = 16;
108 const size_t kNoncePrefixSize = 4;
109 const size_t kAESNonceSize = 12;
110
111 // Calls PK11_Encrypt if it's available. Otherwise, emulates CKM_AES_GCM using
112 // CKM_AES_CTR and the GaloisHash class.
113 SECStatus My_Encrypt(PK11SymKey* key,
114 CK_MECHANISM_TYPE mechanism,
115 SECItem* param,
116 unsigned char* out,
117 unsigned int* out_len,
118 unsigned int max_len,
119 const unsigned char* data,
120 unsigned int data_len) {
121 // If PK11_Encrypt() was successfully resolved or if bundled version of NSS is
122 // being used, then NSS will support AES-GCM directly.
123 PK11_EncryptFunction pk11_encrypt_func =
124 GcmSupportChecker::pk11_encrypt_func();
125 if (pk11_encrypt_func != NULL) {
126 return pk11_encrypt_func(key, mechanism, param, out, out_len, max_len, data,
127 data_len);
128 }
129
130 // Otherwise, the user has an older version of NSS. Regrettably, NSS 3.14.x
131 // has a bug in the AES GCM code
132 // (https://bugzilla.mozilla.org/show_bug.cgi?id=853285), as well as missing
133 // the PK11_Encrypt function
134 // (https://bugzilla.mozilla.org/show_bug.cgi?id=854063), both of which are
135 // resolved in NSS 3.15.
136
137 DCHECK_EQ(mechanism, static_cast<CK_MECHANISM_TYPE>(CKM_AES_GCM));
138 DCHECK_EQ(param->len, sizeof(CK_GCM_PARAMS));
139
140 if (max_len < static_cast<unsigned int>(Aes128Gcm12Encrypter::kAuthTagSize)) {
141 DVLOG(1) << "max_len is less than kAuthTagSize";
142 PORT_SetError(SEC_ERROR_OUTPUT_LEN);
143 return SECFailure;
144 }
145
146 const CK_GCM_PARAMS* gcm_params =
147 reinterpret_cast<CK_GCM_PARAMS*>(param->data);
148
149 DCHECK_EQ(gcm_params->ulTagBits,
150 static_cast<CK_ULONG>(Aes128Gcm12Encrypter::kAuthTagSize * 8));
151 if (gcm_params->ulIvLen != 12u) {
152 DVLOG(1) << "ulIvLen is not equal to 12";
153 PORT_SetError(SEC_ERROR_INPUT_LEN);
154 return SECFailure;
155 }
156
157 SECItem my_param = { siBuffer, NULL, 0 };
158
159 // Step 1. Let H = CIPH_K(128 '0' bits).
160 unsigned char ghash_key[16] = {0};
161 crypto::ScopedPK11Context ctx(PK11_CreateContextBySymKey(
162 CKM_AES_ECB, CKA_ENCRYPT, key, &my_param));
163 if (!ctx) {
164 DVLOG(1) << "PK11_CreateContextBySymKey failed";
165 return SECFailure;
166 }
167 int output_len;
168 if (PK11_CipherOp(ctx.get(), ghash_key, &output_len, sizeof(ghash_key),
169 ghash_key, sizeof(ghash_key)) != SECSuccess) {
170 DVLOG(1) << "PK11_CipherOp failed";
171 return SECFailure;
172 }
173
174 PK11_Finalize(ctx.get());
175
176 if (output_len != sizeof(ghash_key)) {
177 DVLOG(1) << "Wrong output length";
178 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
179 return SECFailure;
180 }
181
182 // Step 2. If len(IV)=96, then let J0 = IV || 31 '0' bits || 1.
183 CK_AES_CTR_PARAMS ctr_params = {0};
184 ctr_params.ulCounterBits = 32;
185 memcpy(ctr_params.cb, gcm_params->pIv, gcm_params->ulIvLen);
186 ctr_params.cb[12] = 0;
187 ctr_params.cb[13] = 0;
188 ctr_params.cb[14] = 0;
189 ctr_params.cb[15] = 1;
190
191 my_param.type = siBuffer;
192 my_param.data = reinterpret_cast<unsigned char*>(&ctr_params);
193 my_param.len = sizeof(ctr_params);
194
195 ctx.reset(PK11_CreateContextBySymKey(CKM_AES_CTR, CKA_ENCRYPT, key,
196 &my_param));
197 if (!ctx) {
198 DVLOG(1) << "PK11_CreateContextBySymKey failed";
199 return SECFailure;
200 }
201
202 // Step 6. Calculate the encryption mask of GCTR_K(J0, ...).
203 unsigned char tag_mask[16] = {0};
204 if (PK11_CipherOp(ctx.get(), tag_mask, &output_len, sizeof(tag_mask),
205 tag_mask, sizeof(tag_mask)) != SECSuccess) {
206 DVLOG(1) << "PK11_CipherOp failed";
207 return SECFailure;
208 }
209 if (output_len != sizeof(tag_mask)) {
210 DVLOG(1) << "Wrong output length";
211 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
212 return SECFailure;
213 }
214
215 // The const_cast for |data| can be removed if system NSS libraries are
216 // NSS 3.14.1 or later (NSS bug
217 // https://bugzilla.mozilla.org/show_bug.cgi?id=808218).
218 if (PK11_CipherOp(ctx.get(), out, &output_len, max_len,
219 const_cast<unsigned char*>(data), data_len) != SECSuccess) {
220 DVLOG(1) << "PK11_CipherOp failed";
221 return SECFailure;
222 }
223
224 PK11_Finalize(ctx.get());
225
226 if (static_cast<unsigned int>(output_len) != data_len) {
227 DVLOG(1) << "Wrong output length";
228 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
229 return SECFailure;
230 }
231
232 if ((max_len - Aes128Gcm12Encrypter::kAuthTagSize) <
233 static_cast<unsigned int>(output_len)) {
234 DVLOG(1) << "(max_len - kAuthTagSize) is less than output_len";
235 PORT_SetError(SEC_ERROR_OUTPUT_LEN);
236 return SECFailure;
237 }
238
239 crypto::GaloisHash ghash(ghash_key);
240 ghash.UpdateAdditional(gcm_params->pAAD, gcm_params->ulAADLen);
241 ghash.UpdateCiphertext(out, output_len);
242 ghash.Finish(out + output_len, Aes128Gcm12Encrypter::kAuthTagSize);
243 for (unsigned int i = 0; i < Aes128Gcm12Encrypter::kAuthTagSize; i++) {
244 out[output_len + i] ^= tag_mask[i];
245 }
246
247 *out_len = output_len + Aes128Gcm12Encrypter::kAuthTagSize;
248 return SECSuccess;
249 } 28 }
250 29
251 } // namespace 30 AeadBaseEncrypter::~AeadBaseEncrypter() {}
252 31
253 Aes128Gcm12Encrypter::Aes128Gcm12Encrypter() { 32 bool AeadBaseEncrypter::SetKey(StringPiece key) {
254 ignore_result(g_gcm_support_checker.Get()); 33 DCHECK_EQ(key.size(), key_size_);
255 } 34 if (key.size() != key_size_) {
256
257 Aes128Gcm12Encrypter::~Aes128Gcm12Encrypter() {}
258
259 bool Aes128Gcm12Encrypter::SetKey(StringPiece key) {
260 DCHECK_EQ(key.size(), sizeof(key_));
261 if (key.size() != sizeof(key_)) {
262 return false; 35 return false;
263 } 36 }
264 memcpy(key_, key.data(), key.size()); 37 memcpy(key_, key.data(), key.size());
265 return true; 38 return true;
266 } 39 }
267 40
268 bool Aes128Gcm12Encrypter::SetNoncePrefix(StringPiece nonce_prefix) { 41 bool AeadBaseEncrypter::SetNoncePrefix(StringPiece nonce_prefix) {
269 DCHECK_EQ(nonce_prefix.size(), kNoncePrefixSize); 42 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_);
270 if (nonce_prefix.size() != kNoncePrefixSize) { 43 if (nonce_prefix.size() != nonce_prefix_size_) {
271 return false; 44 return false;
272 } 45 }
273 COMPILE_ASSERT(sizeof(nonce_prefix_) == kNoncePrefixSize, bad_nonce_length);
274 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); 46 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size());
275 return true; 47 return true;
276 } 48 }
277 49
278 bool Aes128Gcm12Encrypter::Encrypt(StringPiece nonce, 50 bool AeadBaseEncrypter::Encrypt(StringPiece nonce,
279 StringPiece associated_data, 51 StringPiece associated_data,
280 StringPiece plaintext, 52 StringPiece plaintext,
281 unsigned char* output) { 53 unsigned char* output) {
282 if (nonce.size() != kNoncePrefixSize + sizeof(QuicPacketSequenceNumber)) { 54 if (nonce.size() != nonce_prefix_size_ + sizeof(QuicPacketSequenceNumber)) {
283 return false; 55 return false;
284 } 56 }
285 57
286 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); 58 size_t ciphertext_size = GetCiphertextSize(plaintext.length());
287 59
288 // Import key_ into NSS. 60 // Import key_ into NSS.
289 SECItem key_item; 61 SECItem key_item;
290 key_item.type = siBuffer; 62 key_item.type = siBuffer;
291 key_item.data = key_; 63 key_item.data = key_;
292 key_item.len = sizeof(key_); 64 key_item.len = key_size_;
293 PK11SlotInfo* slot = PK11_GetInternalSlot(); 65 PK11SlotInfo* slot = PK11_GetInternalSlot();
66
67 // TODO(wtc): For an AES-GCM key, the correct value for |key_mechanism| is
68 // CKM_AES_GCM, but because of NSS bug
69 // https://bugzilla.mozilla.org/show_bug.cgi?id=853285, use CKM_AES_ECB as a
70 // workaround. Remove this when we require NSS 3.15.
71 CK_MECHANISM_TYPE key_mechanism = aead_mechanism_;
72 if (key_mechanism == CKM_AES_GCM) {
73 key_mechanism = CKM_AES_ECB;
74 }
75
294 // The exact value of the |origin| argument doesn't matter to NSS as long as 76 // The exact value of the |origin| argument doesn't matter to NSS as long as
295 // it's not PK11_OriginFortezzaHack, so we pass PK11_OriginUnwrap as a 77 // it's not PK11_OriginFortezzaHack, so we pass PK11_OriginUnwrap as a
296 // placeholder. 78 // placeholder.
297 crypto::ScopedPK11SymKey aes_key(PK11_ImportSymKey( 79 crypto::ScopedPK11SymKey aead_key(PK11_ImportSymKey(
298 slot, GcmSupportChecker::aes_key_mechanism(), PK11_OriginUnwrap, 80 slot, key_mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &key_item, NULL));
299 CKA_ENCRYPT, &key_item, NULL));
300 PK11_FreeSlot(slot); 81 PK11_FreeSlot(slot);
301 slot = NULL; 82 slot = NULL;
302 if (!aes_key) { 83 if (!aead_key) {
303 DVLOG(1) << "PK11_ImportSymKey failed"; 84 DVLOG(1) << "PK11_ImportSymKey failed";
304 return false; 85 return false;
305 } 86 }
306 87
307 CK_GCM_PARAMS gcm_params = {0}; 88 AeadParams aead_params = {0};
308 gcm_params.pIv = 89 FillAeadParams(nonce, associated_data, auth_tag_size_, &aead_params);
309 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data()));
310 gcm_params.ulIvLen = nonce.size();
311 gcm_params.pAAD =
312 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data()));
313 gcm_params.ulAADLen = associated_data.size();
314 gcm_params.ulTagBits = kAuthTagSize * 8;
315 90
316 SECItem param; 91 SECItem param;
317 param.type = siBuffer; 92 param.type = siBuffer;
318 param.data = reinterpret_cast<unsigned char*>(&gcm_params); 93 param.data = reinterpret_cast<unsigned char*>(&aead_params.data);
319 param.len = sizeof(gcm_params); 94 param.len = aead_params.len;
320 95
321 unsigned int output_len; 96 unsigned int output_len;
322 if (My_Encrypt(aes_key.get(), CKM_AES_GCM, &param, 97 if (pk11_encrypt_(aead_key.get(), aead_mechanism_, &param,
323 output, &output_len, ciphertext_size, 98 output, &output_len, ciphertext_size,
324 reinterpret_cast<const unsigned char*>(plaintext.data()), 99 reinterpret_cast<const unsigned char*>(plaintext.data()),
325 plaintext.size()) != SECSuccess) { 100 plaintext.size()) != SECSuccess) {
326 DVLOG(1) << "My_Encrypt failed"; 101 DVLOG(1) << "pk11_encrypt_ failed";
327 return false; 102 return false;
328 } 103 }
329 104
330 if (output_len != ciphertext_size) { 105 if (output_len != ciphertext_size) {
331 DVLOG(1) << "Wrong output length"; 106 DVLOG(1) << "Wrong output length";
332 return false; 107 return false;
333 } 108 }
334 109
335 return true; 110 return true;
336 } 111 }
337 112
338 QuicData* Aes128Gcm12Encrypter::EncryptPacket( 113 QuicData* AeadBaseEncrypter::EncryptPacket(
339 QuicPacketSequenceNumber sequence_number, 114 QuicPacketSequenceNumber sequence_number,
340 StringPiece associated_data, 115 StringPiece associated_data,
341 StringPiece plaintext) { 116 StringPiece plaintext) {
342 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); 117 size_t ciphertext_size = GetCiphertextSize(plaintext.length());
343 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); 118 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]);
344 119
345 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the 120 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the
346 // same sequence number twice. 121 // same sequence number twice.
347 uint8 nonce[kNoncePrefixSize + sizeof(sequence_number)]; 122 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)];
348 COMPILE_ASSERT(sizeof(nonce) == kAESNonceSize, bad_sequence_number_size); 123 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number);
349 memcpy(nonce, nonce_prefix_, kNoncePrefixSize); 124 DCHECK_LE(nonce_size, sizeof(nonce));
350 memcpy(nonce + kNoncePrefixSize, &sequence_number, sizeof(sequence_number)); 125 memcpy(nonce, nonce_prefix_, nonce_prefix_size_);
351 if (!Encrypt(StringPiece(reinterpret_cast<char*>(nonce), sizeof(nonce)), 126 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number));
127 if (!Encrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size),
352 associated_data, plaintext, 128 associated_data, plaintext,
353 reinterpret_cast<unsigned char*>(ciphertext.get()))) { 129 reinterpret_cast<unsigned char*>(ciphertext.get()))) {
354 return NULL; 130 return NULL;
355 } 131 }
356 132
357 return new QuicData(ciphertext.release(), ciphertext_size, true); 133 return new QuicData(ciphertext.release(), ciphertext_size, true);
358 } 134 }
359 135
360 size_t Aes128Gcm12Encrypter::GetKeySize() const { return kKeySize; } 136 size_t AeadBaseEncrypter::GetKeySize() const { return key_size_; }
361 137
362 size_t Aes128Gcm12Encrypter::GetNoncePrefixSize() const { 138 size_t AeadBaseEncrypter::GetNoncePrefixSize() const {
363 return kNoncePrefixSize; 139 return nonce_prefix_size_;
364 } 140 }
365 141
366 size_t Aes128Gcm12Encrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { 142 size_t AeadBaseEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const {
367 return ciphertext_size - kAuthTagSize; 143 return ciphertext_size - auth_tag_size_;
368 } 144 }
369 145
370 // An AEAD_AES_128_GCM_12 ciphertext is exactly 12 bytes longer than its 146 size_t AeadBaseEncrypter::GetCiphertextSize(size_t plaintext_size) const {
371 // corresponding plaintext. 147 return plaintext_size + auth_tag_size_;
372 size_t Aes128Gcm12Encrypter::GetCiphertextSize(size_t plaintext_size) const {
373 return plaintext_size + kAuthTagSize;
374 } 148 }
375 149
376 StringPiece Aes128Gcm12Encrypter::GetKey() const { 150 StringPiece AeadBaseEncrypter::GetKey() const {
377 return StringPiece(reinterpret_cast<const char*>(key_), sizeof(key_)); 151 return StringPiece(reinterpret_cast<const char*>(key_), key_size_);
378 } 152 }
379 153
380 StringPiece Aes128Gcm12Encrypter::GetNoncePrefix() const { 154 StringPiece AeadBaseEncrypter::GetNoncePrefix() const {
155 if (nonce_prefix_size_ == 0) {
156 return StringPiece();
157 }
381 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), 158 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_),
382 kNoncePrefixSize); 159 nonce_prefix_size_);
383 } 160 }
384 161
385 } // namespace net 162 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698