| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/http/des.h" | 5 #include "net/http/des.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 #if defined(USE_OPENSSL) | 9 #if defined(USE_OPENSSL) |
| 10 #include <openssl/des.h> | 10 #include <openssl/des.h> |
| 11 #include "base/openssl_util.h" | 11 #include "crypto/openssl_util.h" |
| 12 #elif defined(USE_NSS) | 12 #elif defined(USE_NSS) |
| 13 #include <nss.h> | 13 #include <nss.h> |
| 14 #include <pk11pub.h> | 14 #include <pk11pub.h> |
| 15 #include "base/nss_util.h" | 15 #include "crypto/nss_util.h" |
| 16 #elif defined(OS_MACOSX) | 16 #elif defined(OS_MACOSX) |
| 17 #include <CommonCrypto/CommonCryptor.h> | 17 #include <CommonCrypto/CommonCryptor.h> |
| 18 #elif defined(OS_WIN) | 18 #elif defined(OS_WIN) |
| 19 #include <windows.h> | 19 #include <windows.h> |
| 20 #include <wincrypt.h> | 20 #include <wincrypt.h> |
| 21 #include "base/crypto/scoped_capi_types.h" | 21 #include "crypto/scoped_capi_types.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 // The Mac and Windows (CryptoAPI) versions of DESEncrypt are our own code. | 24 // The Mac and Windows (CryptoAPI) versions of DESEncrypt are our own code. |
| 25 // DESSetKeyParity, DESMakeKey, and the Linux (NSS) version of DESEncrypt are | 25 // DESSetKeyParity, DESMakeKey, and the Linux (NSS) version of DESEncrypt are |
| 26 // based on mozilla/security/manager/ssl/src/nsNTLMAuthModule.cpp, | 26 // based on mozilla/security/manager/ssl/src/nsNTLMAuthModule.cpp, |
| 27 // CVS rev. 1.14. | 27 // CVS rev. 1.14. |
| 28 | 28 |
| 29 /* ***** BEGIN LICENSE BLOCK ***** | 29 /* ***** BEGIN LICENSE BLOCK ***** |
| 30 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 30 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 31 * | 31 * |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 key[3] = DESSetKeyParity((raw[2] << 5) | (raw[3] >> 3)); | 83 key[3] = DESSetKeyParity((raw[2] << 5) | (raw[3] >> 3)); |
| 84 key[4] = DESSetKeyParity((raw[3] << 4) | (raw[4] >> 4)); | 84 key[4] = DESSetKeyParity((raw[3] << 4) | (raw[4] >> 4)); |
| 85 key[5] = DESSetKeyParity((raw[4] << 3) | (raw[5] >> 5)); | 85 key[5] = DESSetKeyParity((raw[4] << 3) | (raw[5] >> 5)); |
| 86 key[6] = DESSetKeyParity((raw[5] << 2) | (raw[6] >> 6)); | 86 key[6] = DESSetKeyParity((raw[5] << 2) | (raw[6] >> 6)); |
| 87 key[7] = DESSetKeyParity((raw[6] << 1)); | 87 key[7] = DESSetKeyParity((raw[6] << 1)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 #if defined(USE_OPENSSL) | 90 #if defined(USE_OPENSSL) |
| 91 | 91 |
| 92 void DESEncrypt(const uint8* key, const uint8* src, uint8* hash) { | 92 void DESEncrypt(const uint8* key, const uint8* src, uint8* hash) { |
| 93 base::EnsureOpenSSLInit(); | 93 crypto::EnsureOpenSSLInit(); |
| 94 | 94 |
| 95 DES_key_schedule ks; | 95 DES_key_schedule ks; |
| 96 DES_set_key_unchecked( | 96 DES_set_key_unchecked( |
| 97 reinterpret_cast<const_DES_cblock*>(const_cast<uint8*>(key)), &ks); | 97 reinterpret_cast<const_DES_cblock*>(const_cast<uint8*>(key)), &ks); |
| 98 | 98 |
| 99 DES_ecb_encrypt(reinterpret_cast<const_DES_cblock*>(const_cast<uint8*>(src)), | 99 DES_ecb_encrypt(reinterpret_cast<const_DES_cblock*>(const_cast<uint8*>(src)), |
| 100 reinterpret_cast<DES_cblock*>(hash), &ks, DES_ENCRYPT); | 100 reinterpret_cast<DES_cblock*>(hash), &ks, DES_ENCRYPT); |
| 101 } | 101 } |
| 102 | 102 |
| 103 #elif defined(USE_NSS) | 103 #elif defined(USE_NSS) |
| 104 | 104 |
| 105 void DESEncrypt(const uint8* key, const uint8* src, uint8* hash) { | 105 void DESEncrypt(const uint8* key, const uint8* src, uint8* hash) { |
| 106 CK_MECHANISM_TYPE cipher_mech = CKM_DES_ECB; | 106 CK_MECHANISM_TYPE cipher_mech = CKM_DES_ECB; |
| 107 PK11SlotInfo* slot = NULL; | 107 PK11SlotInfo* slot = NULL; |
| 108 PK11SymKey* symkey = NULL; | 108 PK11SymKey* symkey = NULL; |
| 109 PK11Context* ctxt = NULL; | 109 PK11Context* ctxt = NULL; |
| 110 SECItem key_item; | 110 SECItem key_item; |
| 111 SECItem* param = NULL; | 111 SECItem* param = NULL; |
| 112 SECStatus rv; | 112 SECStatus rv; |
| 113 unsigned int n; | 113 unsigned int n; |
| 114 | 114 |
| 115 base::EnsureNSSInit(); | 115 crypto::EnsureNSSInit(); |
| 116 | 116 |
| 117 slot = PK11_GetBestSlot(cipher_mech, NULL); | 117 slot = PK11_GetBestSlot(cipher_mech, NULL); |
| 118 if (!slot) | 118 if (!slot) |
| 119 goto done; | 119 goto done; |
| 120 | 120 |
| 121 key_item.data = const_cast<uint8*>(key); | 121 key_item.data = const_cast<uint8*>(key); |
| 122 key_item.len = 8; | 122 key_item.len = 8; |
| 123 symkey = PK11_ImportSymKey(slot, cipher_mech, | 123 symkey = PK11_ImportSymKey(slot, cipher_mech, |
| 124 PK11_OriginUnwrap, CKA_ENCRYPT, | 124 PK11_OriginUnwrap, CKA_ENCRYPT, |
| 125 &key_item, NULL); | 125 &key_item, NULL); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 size_t data_out_moved = 0; | 164 size_t data_out_moved = 0; |
| 165 status = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, | 165 status = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, |
| 166 key, 8, NULL, src, 8, hash, 8, &data_out_moved); | 166 key, 8, NULL, src, 8, hash, 8, &data_out_moved); |
| 167 DCHECK(status == kCCSuccess); | 167 DCHECK(status == kCCSuccess); |
| 168 DCHECK(data_out_moved == 8); | 168 DCHECK(data_out_moved == 8); |
| 169 } | 169 } |
| 170 | 170 |
| 171 #elif defined(OS_WIN) | 171 #elif defined(OS_WIN) |
| 172 | 172 |
| 173 void DESEncrypt(const uint8* key, const uint8* src, uint8* hash) { | 173 void DESEncrypt(const uint8* key, const uint8* src, uint8* hash) { |
| 174 base::ScopedHCRYPTPROV provider; | 174 crypto::ScopedHCRYPTPROV provider; |
| 175 if (!CryptAcquireContext(provider.receive(), NULL, NULL, PROV_RSA_FULL, | 175 if (!CryptAcquireContext(provider.receive(), NULL, NULL, PROV_RSA_FULL, |
| 176 CRYPT_VERIFYCONTEXT)) | 176 CRYPT_VERIFYCONTEXT)) |
| 177 return; | 177 return; |
| 178 | 178 |
| 179 { | 179 { |
| 180 // Import the DES key. | 180 // Import the DES key. |
| 181 struct KeyBlob { | 181 struct KeyBlob { |
| 182 BLOBHEADER header; | 182 BLOBHEADER header; |
| 183 DWORD key_size; | 183 DWORD key_size; |
| 184 BYTE key_data[8]; | 184 BYTE key_data[8]; |
| 185 }; | 185 }; |
| 186 KeyBlob key_blob; | 186 KeyBlob key_blob; |
| 187 key_blob.header.bType = PLAINTEXTKEYBLOB; | 187 key_blob.header.bType = PLAINTEXTKEYBLOB; |
| 188 key_blob.header.bVersion = CUR_BLOB_VERSION; | 188 key_blob.header.bVersion = CUR_BLOB_VERSION; |
| 189 key_blob.header.reserved = 0; | 189 key_blob.header.reserved = 0; |
| 190 key_blob.header.aiKeyAlg = CALG_DES; | 190 key_blob.header.aiKeyAlg = CALG_DES; |
| 191 key_blob.key_size = 8; // 64 bits | 191 key_blob.key_size = 8; // 64 bits |
| 192 memcpy(key_blob.key_data, key, 8); | 192 memcpy(key_blob.key_data, key, 8); |
| 193 | 193 |
| 194 base::ScopedHCRYPTKEY key; | 194 crypto::ScopedHCRYPTKEY key; |
| 195 BOOL import_ok = CryptImportKey(provider, | 195 BOOL import_ok = CryptImportKey(provider, |
| 196 reinterpret_cast<BYTE*>(&key_blob), | 196 reinterpret_cast<BYTE*>(&key_blob), |
| 197 sizeof key_blob, 0, 0, key.receive()); | 197 sizeof key_blob, 0, 0, key.receive()); |
| 198 // Destroy the copy of the key. | 198 // Destroy the copy of the key. |
| 199 SecureZeroMemory(key_blob.key_data, sizeof key_blob.key_data); | 199 SecureZeroMemory(key_blob.key_data, sizeof key_blob.key_data); |
| 200 if (!import_ok) | 200 if (!import_ok) |
| 201 return; | 201 return; |
| 202 | 202 |
| 203 // No initialization vector required. | 203 // No initialization vector required. |
| 204 DWORD cipher_mode = CRYPT_MODE_ECB; | 204 DWORD cipher_mode = CRYPT_MODE_ECB; |
| 205 if (!CryptSetKeyParam(key, KP_MODE, reinterpret_cast<BYTE*>(&cipher_mode), | 205 if (!CryptSetKeyParam(key, KP_MODE, reinterpret_cast<BYTE*>(&cipher_mode), |
| 206 0)) | 206 0)) |
| 207 return; | 207 return; |
| 208 | 208 |
| 209 // CryptoAPI requires us to copy the plaintext to the output buffer first. | 209 // CryptoAPI requires us to copy the plaintext to the output buffer first. |
| 210 CopyMemory(hash, src, 8); | 210 CopyMemory(hash, src, 8); |
| 211 // Pass a 'Final' of FALSE, otherwise CryptEncrypt appends one additional | 211 // Pass a 'Final' of FALSE, otherwise CryptEncrypt appends one additional |
| 212 // block of padding to the data. | 212 // block of padding to the data. |
| 213 DWORD hash_len = 8; | 213 DWORD hash_len = 8; |
| 214 CryptEncrypt(key, 0, FALSE, 0, hash, &hash_len, 8); | 214 CryptEncrypt(key, 0, FALSE, 0, hash, &hash_len, 8); |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 #endif | 218 #endif |
| 219 | 219 |
| 220 } // namespace net | 220 } // namespace net |
| OLD | NEW |