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

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

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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/aes_128_gcm_12_decrypter.h" 5 #include "net/quic/crypto/aes_128_gcm_12_decrypter.h"
6 6
7 #include <pk11pub.h> 7 #include <pk11pub.h>
8 #include <secerr.h> 8 #include <secerr.h>
9 9
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 14 matching lines...) Expand all
25 const size_t kNoncePrefixSize = 4; 25 const size_t kNoncePrefixSize = 4;
26 26
27 // On Linux, dynamically link against the system version of libnss3.so. In 27 // On Linux, dynamically link against the system version of libnss3.so. In
28 // order to continue working on systems without up-to-date versions of NSS, 28 // order to continue working on systems without up-to-date versions of NSS,
29 // lookup PK11_Decrypt with dlsym. 29 // lookup PK11_Decrypt with dlsym.
30 30
31 // GcmSupportChecker is a singleton which caches the results of runtime symbol 31 // GcmSupportChecker is a singleton which caches the results of runtime symbol
32 // resolution of PK11_Decrypt. 32 // resolution of PK11_Decrypt.
33 class GcmSupportChecker { 33 class GcmSupportChecker {
34 public: 34 public:
35 static PK11_DecryptFunction pk11_decrypt_func() { 35 static PK11_DecryptFunction pk11_decrypt_func() { return pk11_decrypt_func_; }
36 return pk11_decrypt_func_;
37 }
38 36
39 private: 37 private:
40 friend struct base::DefaultLazyInstanceTraits<GcmSupportChecker>; 38 friend struct base::DefaultLazyInstanceTraits<GcmSupportChecker>;
41 39
42 GcmSupportChecker() { 40 GcmSupportChecker() {
43 #if !defined(USE_NSS) 41 #if !defined(USE_NSS)
44 // Using a bundled version of NSS that is guaranteed to have this symbol. 42 // Using a bundled version of NSS that is guaranteed to have this symbol.
45 pk11_decrypt_func_ = PK11_Decrypt; 43 pk11_decrypt_func_ = PK11_Decrypt;
46 #else 44 #else
47 // Using system NSS libraries and PCKS #11 modules, which may not have the 45 // Using system NSS libraries and PCKS #11 modules, which may not have the
48 // necessary function (PK11_Decrypt) or mechanism support (CKM_AES_GCM). 46 // necessary function (PK11_Decrypt) or mechanism support (CKM_AES_GCM).
49 47
50 // If PK11_Decrypt() was successfully resolved, then NSS will support 48 // If PK11_Decrypt() was successfully resolved, then NSS will support
51 // AES-GCM directly. This was introduced in NSS 3.15. 49 // AES-GCM directly. This was introduced in NSS 3.15.
52 pk11_decrypt_func_ = (PK11_DecryptFunction)dlsym(RTLD_DEFAULT, 50 pk11_decrypt_func_ =
53 "PK11_Decrypt"); 51 (PK11_DecryptFunction)dlsym(RTLD_DEFAULT, "PK11_Decrypt");
54 #endif 52 #endif
55 } 53 }
56 54
57 // |pk11_decrypt_func_| stores the runtime symbol resolution of PK11_Decrypt. 55 // |pk11_decrypt_func_| stores the runtime symbol resolution of PK11_Decrypt.
58 static PK11_DecryptFunction pk11_decrypt_func_; 56 static PK11_DecryptFunction pk11_decrypt_func_;
59 }; 57 };
60 58
61 // static 59 // static
62 PK11_DecryptFunction GcmSupportChecker::pk11_decrypt_func_ = NULL; 60 PK11_DecryptFunction GcmSupportChecker::pk11_decrypt_func_ = NULL;
63 61
64 base::LazyInstance<GcmSupportChecker>::Leaky g_gcm_support_checker = 62 base::LazyInstance<GcmSupportChecker>::Leaky g_gcm_support_checker =
65 LAZY_INSTANCE_INITIALIZER; 63 LAZY_INSTANCE_INITIALIZER;
66 64
67 // Calls PK11_Decrypt if it's available. Otherwise, emulates CKM_AES_GCM using 65 // Calls PK11_Decrypt if it's available. Otherwise, emulates CKM_AES_GCM using
68 // CKM_AES_CTR and the GaloisHash class. 66 // CKM_AES_CTR and the GaloisHash class.
69 SECStatus My_Decrypt(PK11SymKey* key, 67 SECStatus My_Decrypt(PK11SymKey* key,
70 CK_MECHANISM_TYPE mechanism, 68 CK_MECHANISM_TYPE mechanism,
71 SECItem* param, 69 SECItem* param,
72 unsigned char* out, 70 unsigned char* out,
73 unsigned int* out_len, 71 unsigned int* out_len,
74 unsigned int max_len, 72 unsigned int max_len,
75 const unsigned char* enc, 73 const unsigned char* enc,
76 unsigned int enc_len) { 74 unsigned int enc_len) {
77 // If PK11_Decrypt() was successfully resolved or if bundled version of NSS is 75 // If PK11_Decrypt() was successfully resolved or if bundled version of NSS is
78 // being used, then NSS will support AES-GCM directly. 76 // being used, then NSS will support AES-GCM directly.
79 PK11_DecryptFunction pk11_decrypt_func = 77 PK11_DecryptFunction pk11_decrypt_func =
80 GcmSupportChecker::pk11_decrypt_func(); 78 GcmSupportChecker::pk11_decrypt_func();
81 if (pk11_decrypt_func != NULL) { 79 if (pk11_decrypt_func != NULL) {
82 return pk11_decrypt_func(key, mechanism, param, out, out_len, max_len, enc, 80 return pk11_decrypt_func(
83 enc_len); 81 key, mechanism, param, out, out_len, max_len, enc, enc_len);
84 } 82 }
85 83
86 // Otherwise, the user has an older version of NSS. Regrettably, NSS 3.14.x 84 // Otherwise, the user has an older version of NSS. Regrettably, NSS 3.14.x
87 // has a bug in the AES GCM code 85 // has a bug in the AES GCM code
88 // (https://bugzilla.mozilla.org/show_bug.cgi?id=853285), as well as missing 86 // (https://bugzilla.mozilla.org/show_bug.cgi?id=853285), as well as missing
89 // the PK11_Decrypt function 87 // the PK11_Decrypt function
90 // (https://bugzilla.mozilla.org/show_bug.cgi?id=854063), both of which are 88 // (https://bugzilla.mozilla.org/show_bug.cgi?id=854063), both of which are
91 // resolved in NSS 3.15. 89 // resolved in NSS 3.15.
92 90
93 DCHECK_EQ(mechanism, static_cast<CK_MECHANISM_TYPE>(CKM_AES_GCM)); 91 DCHECK_EQ(mechanism, static_cast<CK_MECHANISM_TYPE>(CKM_AES_GCM));
94 DCHECK_EQ(param->len, sizeof(CK_GCM_PARAMS)); 92 DCHECK_EQ(param->len, sizeof(CK_GCM_PARAMS));
95 93
96 const CK_GCM_PARAMS* gcm_params = 94 const CK_GCM_PARAMS* gcm_params =
97 reinterpret_cast<CK_GCM_PARAMS*>(param->data); 95 reinterpret_cast<CK_GCM_PARAMS*>(param->data);
98 96
99 DCHECK_EQ(gcm_params->ulTagBits, 97 DCHECK_EQ(gcm_params->ulTagBits,
100 static_cast<CK_ULONG>(Aes128Gcm12Decrypter::kAuthTagSize * 8)); 98 static_cast<CK_ULONG>(Aes128Gcm12Decrypter::kAuthTagSize * 8));
101 if (gcm_params->ulIvLen != 12u) { 99 if (gcm_params->ulIvLen != 12u) {
102 DVLOG(1) << "ulIvLen is not equal to 12"; 100 DVLOG(1) << "ulIvLen is not equal to 12";
103 PORT_SetError(SEC_ERROR_INPUT_LEN); 101 PORT_SetError(SEC_ERROR_INPUT_LEN);
104 return SECFailure; 102 return SECFailure;
105 } 103 }
106 104
107 SECItem my_param = { siBuffer, NULL, 0 }; 105 SECItem my_param = {siBuffer, NULL, 0};
108 106
109 // Step 2. Let H = CIPH_K(128 '0' bits). 107 // Step 2. Let H = CIPH_K(128 '0' bits).
110 unsigned char ghash_key[16] = {0}; 108 unsigned char ghash_key[16] = {0};
111 crypto::ScopedPK11Context ctx(PK11_CreateContextBySymKey( 109 crypto::ScopedPK11Context ctx(
112 CKM_AES_ECB, CKA_ENCRYPT, key, &my_param)); 110 PK11_CreateContextBySymKey(CKM_AES_ECB, CKA_ENCRYPT, key, &my_param));
113 if (!ctx) { 111 if (!ctx) {
114 DVLOG(1) << "PK11_CreateContextBySymKey failed"; 112 DVLOG(1) << "PK11_CreateContextBySymKey failed";
115 return SECFailure; 113 return SECFailure;
116 } 114 }
117 int output_len; 115 int output_len;
118 if (PK11_CipherOp(ctx.get(), ghash_key, &output_len, sizeof(ghash_key), 116 if (PK11_CipherOp(ctx.get(),
119 ghash_key, sizeof(ghash_key)) != SECSuccess) { 117 ghash_key,
118 &output_len,
119 sizeof(ghash_key),
120 ghash_key,
121 sizeof(ghash_key)) != SECSuccess) {
120 DVLOG(1) << "PK11_CipherOp failed"; 122 DVLOG(1) << "PK11_CipherOp failed";
121 return SECFailure; 123 return SECFailure;
122 } 124 }
123 125
124 PK11_Finalize(ctx.get()); 126 PK11_Finalize(ctx.get());
125 127
126 if (output_len != sizeof(ghash_key)) { 128 if (output_len != sizeof(ghash_key)) {
127 DVLOG(1) << "Wrong output length"; 129 DVLOG(1) << "Wrong output length";
128 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 130 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
129 return SECFailure; 131 return SECFailure;
130 } 132 }
131 133
132 // Step 3. If len(IV)=96, then let J0 = IV || 31 '0' bits || 1. 134 // Step 3. If len(IV)=96, then let J0 = IV || 31 '0' bits || 1.
133 CK_AES_CTR_PARAMS ctr_params = {0}; 135 CK_AES_CTR_PARAMS ctr_params = {0};
134 ctr_params.ulCounterBits = 32; 136 ctr_params.ulCounterBits = 32;
135 memcpy(ctr_params.cb, gcm_params->pIv, gcm_params->ulIvLen); 137 memcpy(ctr_params.cb, gcm_params->pIv, gcm_params->ulIvLen);
136 ctr_params.cb[12] = 0; 138 ctr_params.cb[12] = 0;
137 ctr_params.cb[13] = 0; 139 ctr_params.cb[13] = 0;
138 ctr_params.cb[14] = 0; 140 ctr_params.cb[14] = 0;
139 ctr_params.cb[15] = 1; 141 ctr_params.cb[15] = 1;
140 142
141 my_param.type = siBuffer; 143 my_param.type = siBuffer;
142 my_param.data = reinterpret_cast<unsigned char*>(&ctr_params); 144 my_param.data = reinterpret_cast<unsigned char*>(&ctr_params);
143 my_param.len = sizeof(ctr_params); 145 my_param.len = sizeof(ctr_params);
144 146
145 ctx.reset(PK11_CreateContextBySymKey(CKM_AES_CTR, CKA_ENCRYPT, key, 147 ctx.reset(
146 &my_param)); 148 PK11_CreateContextBySymKey(CKM_AES_CTR, CKA_ENCRYPT, key, &my_param));
147 if (!ctx) { 149 if (!ctx) {
148 DVLOG(1) << "PK11_CreateContextBySymKey failed"; 150 DVLOG(1) << "PK11_CreateContextBySymKey failed";
149 return SECFailure; 151 return SECFailure;
150 } 152 }
151 153
152 // Step 6. Calculate the encryption mask of GCTR_K(J0, ...). 154 // Step 6. Calculate the encryption mask of GCTR_K(J0, ...).
153 unsigned char tag_mask[16] = {0}; 155 unsigned char tag_mask[16] = {0};
154 if (PK11_CipherOp(ctx.get(), tag_mask, &output_len, sizeof(tag_mask), 156 if (PK11_CipherOp(ctx.get(),
155 tag_mask, sizeof(tag_mask)) != SECSuccess) { 157 tag_mask,
158 &output_len,
159 sizeof(tag_mask),
160 tag_mask,
161 sizeof(tag_mask)) != SECSuccess) {
156 DVLOG(1) << "PK11_CipherOp failed"; 162 DVLOG(1) << "PK11_CipherOp failed";
157 return SECFailure; 163 return SECFailure;
158 } 164 }
159 if (output_len != sizeof(tag_mask)) { 165 if (output_len != sizeof(tag_mask)) {
160 DVLOG(1) << "Wrong output length"; 166 DVLOG(1) << "Wrong output length";
161 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 167 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
162 return SECFailure; 168 return SECFailure;
163 } 169 }
164 170
165 if (enc_len < Aes128Gcm12Decrypter::kAuthTagSize) { 171 if (enc_len < Aes128Gcm12Decrypter::kAuthTagSize) {
166 PORT_SetError(SEC_ERROR_INPUT_LEN); 172 PORT_SetError(SEC_ERROR_INPUT_LEN);
167 return SECFailure; 173 return SECFailure;
168 } 174 }
169 175
170 // The const_cast for |enc| can be removed if system NSS libraries are 176 // The const_cast for |enc| can be removed if system NSS libraries are
171 // NSS 3.14.1 or later (NSS bug 177 // NSS 3.14.1 or later (NSS bug
172 // https://bugzilla.mozilla.org/show_bug.cgi?id=808218). 178 // https://bugzilla.mozilla.org/show_bug.cgi?id=808218).
173 if (PK11_CipherOp(ctx.get(), out, &output_len, max_len, 179 if (PK11_CipherOp(ctx.get(),
174 const_cast<unsigned char*>(enc), 180 out,
175 enc_len - Aes128Gcm12Decrypter::kAuthTagSize) != SECSuccess) { 181 &output_len,
182 max_len,
183 const_cast<unsigned char*>(enc),
184 enc_len - Aes128Gcm12Decrypter::kAuthTagSize) !=
185 SECSuccess) {
176 DVLOG(1) << "PK11_CipherOp failed"; 186 DVLOG(1) << "PK11_CipherOp failed";
177 return SECFailure; 187 return SECFailure;
178 } 188 }
179 189
180 PK11_Finalize(ctx.get()); 190 PK11_Finalize(ctx.get());
181 191
182 if (static_cast<unsigned int>(output_len) != 192 if (static_cast<unsigned int>(output_len) !=
183 enc_len - Aes128Gcm12Decrypter::kAuthTagSize) { 193 enc_len - Aes128Gcm12Decrypter::kAuthTagSize) {
184 DVLOG(1) << "Wrong output length"; 194 DVLOG(1) << "Wrong output length";
185 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 195 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
186 return SECFailure; 196 return SECFailure;
187 } 197 }
188 198
189 crypto::GaloisHash ghash(ghash_key); 199 crypto::GaloisHash ghash(ghash_key);
190 ghash.UpdateAdditional(gcm_params->pAAD, gcm_params->ulAADLen); 200 ghash.UpdateAdditional(gcm_params->pAAD, gcm_params->ulAADLen);
191 ghash.UpdateCiphertext(enc, output_len); 201 ghash.UpdateCiphertext(enc, output_len);
192 unsigned char auth_tag[Aes128Gcm12Decrypter::kAuthTagSize]; 202 unsigned char auth_tag[Aes128Gcm12Decrypter::kAuthTagSize];
193 ghash.Finish(auth_tag, Aes128Gcm12Decrypter::kAuthTagSize); 203 ghash.Finish(auth_tag, Aes128Gcm12Decrypter::kAuthTagSize);
194 for (unsigned int i = 0; i < Aes128Gcm12Decrypter::kAuthTagSize; i++) { 204 for (unsigned int i = 0; i < Aes128Gcm12Decrypter::kAuthTagSize; i++) {
195 auth_tag[i] ^= tag_mask[i]; 205 auth_tag[i] ^= tag_mask[i];
196 } 206 }
197 207
198 if (NSS_SecureMemcmp(auth_tag, enc + output_len, 208 if (NSS_SecureMemcmp(auth_tag,
209 enc + output_len,
199 Aes128Gcm12Decrypter::kAuthTagSize) != 0) { 210 Aes128Gcm12Decrypter::kAuthTagSize) != 0) {
200 PORT_SetError(SEC_ERROR_BAD_DATA); 211 PORT_SetError(SEC_ERROR_BAD_DATA);
201 return SECFailure; 212 return SECFailure;
202 } 213 }
203 214
204 *out_len = output_len; 215 *out_len = output_len;
205 return SECSuccess; 216 return SECSuccess;
206 } 217 }
207 218
208 } // namespace 219 } // namespace
209 220
210 Aes128Gcm12Decrypter::Aes128Gcm12Decrypter() 221 Aes128Gcm12Decrypter::Aes128Gcm12Decrypter()
211 : AeadBaseDecrypter(CKM_AES_GCM, My_Decrypt, kKeySize, kAuthTagSize, 222 : AeadBaseDecrypter(CKM_AES_GCM,
223 My_Decrypt,
224 kKeySize,
225 kAuthTagSize,
212 kNoncePrefixSize) { 226 kNoncePrefixSize) {
213 COMPILE_ASSERT(kKeySize <= kMaxKeySize, key_size_too_big); 227 COMPILE_ASSERT(kKeySize <= kMaxKeySize, key_size_too_big);
214 COMPILE_ASSERT(kNoncePrefixSize <= kMaxNoncePrefixSize, 228 COMPILE_ASSERT(kNoncePrefixSize <= kMaxNoncePrefixSize,
215 nonce_prefix_size_too_big); 229 nonce_prefix_size_too_big);
216 ignore_result(g_gcm_support_checker.Get()); 230 ignore_result(g_gcm_support_checker.Get());
217 } 231 }
218 232
219 Aes128Gcm12Decrypter::~Aes128Gcm12Decrypter() {} 233 Aes128Gcm12Decrypter::~Aes128Gcm12Decrypter() {
234 }
220 235
221 void Aes128Gcm12Decrypter::FillAeadParams(StringPiece nonce, 236 void Aes128Gcm12Decrypter::FillAeadParams(StringPiece nonce,
222 StringPiece associated_data, 237 StringPiece associated_data,
223 size_t auth_tag_size, 238 size_t auth_tag_size,
224 AeadParams* aead_params) const { 239 AeadParams* aead_params) const {
225 aead_params->len = sizeof(aead_params->data.gcm_params); 240 aead_params->len = sizeof(aead_params->data.gcm_params);
226 CK_GCM_PARAMS* gcm_params = &aead_params->data.gcm_params; 241 CK_GCM_PARAMS* gcm_params = &aead_params->data.gcm_params;
227 gcm_params->pIv = 242 gcm_params->pIv = reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data()));
228 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data()));
229 gcm_params->ulIvLen = nonce.size(); 243 gcm_params->ulIvLen = nonce.size();
230 gcm_params->pAAD = 244 gcm_params->pAAD =
231 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data())); 245 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data()));
232 gcm_params->ulAADLen = associated_data.size(); 246 gcm_params->ulAADLen = associated_data.size();
233 gcm_params->ulTagBits = auth_tag_size * 8; 247 gcm_params->ulTagBits = auth_tag_size * 8;
234 } 248 }
235 249
236 } // namespace net 250 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698