OLD | NEW |
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/aes_128_gcm_12_encrypter.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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 pk11_encrypt_func_ = (PK11_EncryptFunction)dlsym(RTLD_DEFAULT, | 52 pk11_encrypt_func_ = (PK11_EncryptFunction)dlsym(RTLD_DEFAULT, |
53 "PK11_Encrypt"); | 53 "PK11_Encrypt"); |
54 #endif | 54 #endif |
55 } | 55 } |
56 | 56 |
57 // |pk11_encrypt_func_| stores the runtime symbol resolution of PK11_Encrypt. | 57 // |pk11_encrypt_func_| stores the runtime symbol resolution of PK11_Encrypt. |
58 static PK11_EncryptFunction pk11_encrypt_func_; | 58 static PK11_EncryptFunction pk11_encrypt_func_; |
59 }; | 59 }; |
60 | 60 |
61 // static | 61 // static |
62 PK11_EncryptFunction GcmSupportChecker::pk11_encrypt_func_ = NULL; | 62 PK11_EncryptFunction GcmSupportChecker::pk11_encrypt_func_ = nullptr; |
63 | 63 |
64 base::LazyInstance<GcmSupportChecker>::Leaky g_gcm_support_checker = | 64 base::LazyInstance<GcmSupportChecker>::Leaky g_gcm_support_checker = |
65 LAZY_INSTANCE_INITIALIZER; | 65 LAZY_INSTANCE_INITIALIZER; |
66 | 66 |
67 // Calls PK11_Encrypt if it's available. Otherwise, emulates CKM_AES_GCM using | 67 // Calls PK11_Encrypt if it's available. Otherwise, emulates CKM_AES_GCM using |
68 // CKM_AES_CTR and the GaloisHash class. | 68 // CKM_AES_CTR and the GaloisHash class. |
69 SECStatus My_Encrypt(PK11SymKey* key, | 69 SECStatus My_Encrypt(PK11SymKey* key, |
70 CK_MECHANISM_TYPE mechanism, | 70 CK_MECHANISM_TYPE mechanism, |
71 SECItem* param, | 71 SECItem* param, |
72 unsigned char* out, | 72 unsigned char* out, |
73 unsigned int* out_len, | 73 unsigned int* out_len, |
74 unsigned int max_len, | 74 unsigned int max_len, |
75 const unsigned char* data, | 75 const unsigned char* data, |
76 unsigned int data_len) { | 76 unsigned int data_len) { |
77 // If PK11_Encrypt() was successfully resolved or if bundled version of NSS is | 77 // If PK11_Encrypt() was successfully resolved or if bundled version of NSS is |
78 // being used, then NSS will support AES-GCM directly. | 78 // being used, then NSS will support AES-GCM directly. |
79 PK11_EncryptFunction pk11_encrypt_func = | 79 PK11_EncryptFunction pk11_encrypt_func = |
80 GcmSupportChecker::pk11_encrypt_func(); | 80 GcmSupportChecker::pk11_encrypt_func(); |
81 if (pk11_encrypt_func != NULL) { | 81 if (pk11_encrypt_func != nullptr) { |
82 return pk11_encrypt_func(key, mechanism, param, out, out_len, max_len, data, | 82 return pk11_encrypt_func(key, mechanism, param, out, out_len, max_len, data, |
83 data_len); | 83 data_len); |
84 } | 84 } |
85 | 85 |
86 // Otherwise, the user has an older version of NSS. Regrettably, NSS 3.14.x | 86 // Otherwise, the user has an older version of NSS. Regrettably, NSS 3.14.x |
87 // has a bug in the AES GCM code | 87 // has a bug in the AES GCM code |
88 // (https://bugzilla.mozilla.org/show_bug.cgi?id=853285), as well as missing | 88 // (https://bugzilla.mozilla.org/show_bug.cgi?id=853285), as well as missing |
89 // the PK11_Encrypt function | 89 // the PK11_Encrypt function |
90 // (https://bugzilla.mozilla.org/show_bug.cgi?id=854063), both of which are | 90 // (https://bugzilla.mozilla.org/show_bug.cgi?id=854063), both of which are |
91 // resolved in NSS 3.15. | 91 // resolved in NSS 3.15. |
(...skipping 11 matching lines...) Expand all Loading... |
103 reinterpret_cast<CK_GCM_PARAMS*>(param->data); | 103 reinterpret_cast<CK_GCM_PARAMS*>(param->data); |
104 | 104 |
105 DCHECK_EQ(gcm_params->ulTagBits, | 105 DCHECK_EQ(gcm_params->ulTagBits, |
106 static_cast<CK_ULONG>(Aes128Gcm12Encrypter::kAuthTagSize * 8)); | 106 static_cast<CK_ULONG>(Aes128Gcm12Encrypter::kAuthTagSize * 8)); |
107 if (gcm_params->ulIvLen != 12u) { | 107 if (gcm_params->ulIvLen != 12u) { |
108 DVLOG(1) << "ulIvLen is not equal to 12"; | 108 DVLOG(1) << "ulIvLen is not equal to 12"; |
109 PORT_SetError(SEC_ERROR_INPUT_LEN); | 109 PORT_SetError(SEC_ERROR_INPUT_LEN); |
110 return SECFailure; | 110 return SECFailure; |
111 } | 111 } |
112 | 112 |
113 SECItem my_param = { siBuffer, NULL, 0 }; | 113 SECItem my_param = { siBuffer, nullptr, 0 }; |
114 | 114 |
115 // Step 1. Let H = CIPH_K(128 '0' bits). | 115 // Step 1. Let H = CIPH_K(128 '0' bits). |
116 unsigned char ghash_key[16] = {0}; | 116 unsigned char ghash_key[16] = {0}; |
117 crypto::ScopedPK11Context ctx(PK11_CreateContextBySymKey( | 117 crypto::ScopedPK11Context ctx(PK11_CreateContextBySymKey( |
118 CKM_AES_ECB, CKA_ENCRYPT, key, &my_param)); | 118 CKM_AES_ECB, CKA_ENCRYPT, key, &my_param)); |
119 if (!ctx) { | 119 if (!ctx) { |
120 DVLOG(1) << "PK11_CreateContextBySymKey failed"; | 120 DVLOG(1) << "PK11_CreateContextBySymKey failed"; |
121 return SECFailure; | 121 return SECFailure; |
122 } | 122 } |
123 int output_len; | 123 int output_len; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 gcm_params->pIv = | 226 gcm_params->pIv = |
227 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data())); | 227 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data())); |
228 gcm_params->ulIvLen = nonce.size(); | 228 gcm_params->ulIvLen = nonce.size(); |
229 gcm_params->pAAD = | 229 gcm_params->pAAD = |
230 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data())); | 230 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data())); |
231 gcm_params->ulAADLen = associated_data.size(); | 231 gcm_params->ulAADLen = associated_data.size(); |
232 gcm_params->ulTagBits = auth_tag_size * 8; | 232 gcm_params->ulTagBits = auth_tag_size * 8; |
233 } | 233 } |
234 | 234 |
235 } // namespace net | 235 } // namespace net |
OLD | NEW |