| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <openssl/aes.h> | 5 #include <openssl/aes.h> |
| 6 #include <openssl/evp.h> | 6 #include <openssl/evp.h> |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/numerics/safe_math.h" | 9 #include "base/numerics/safe_math.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "content/child/webcrypto/crypto_data.h" | 11 #include "content/child/webcrypto/crypto_data.h" |
| 12 #include "content/child/webcrypto/openssl/aes_key_openssl.h" | 12 #include "content/child/webcrypto/openssl/aes_key_openssl.h" |
| 13 #include "content/child/webcrypto/openssl/key_openssl.h" | 13 #include "content/child/webcrypto/openssl/key_openssl.h" |
| 14 #include "content/child/webcrypto/openssl/util_openssl.h" |
| 14 #include "content/child/webcrypto/status.h" | 15 #include "content/child/webcrypto/status.h" |
| 15 #include "content/child/webcrypto/webcrypto_util.h" | 16 #include "content/child/webcrypto/webcrypto_util.h" |
| 16 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
| 17 #include "crypto/scoped_openssl_types.h" | 18 #include "crypto/scoped_openssl_types.h" |
| 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 namespace webcrypto { | 23 namespace webcrypto { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 const EVP_CIPHER* GetAESCipherByKeyLength(unsigned int key_length_bytes) { | 27 const EVP_CIPHER* GetAESCipherByKeyLength(unsigned int key_length_bytes) { |
| 27 // BoringSSL does not support 192-bit AES keys. | 28 // BoringSSL does not support 192-bit AES keys. |
| 28 switch (key_length_bytes) { | 29 switch (key_length_bytes) { |
| 29 case 16: | 30 case 16: |
| 30 return EVP_aes_128_cbc(); | 31 return EVP_aes_128_cbc(); |
| 31 case 32: | 32 case 32: |
| 32 return EVP_aes_256_cbc(); | 33 return EVP_aes_256_cbc(); |
| 33 default: | 34 default: |
| 34 return NULL; | 35 return NULL; |
| 35 } | 36 } |
| 36 } | 37 } |
| 37 | 38 |
| 38 // OpenSSL constants for EVP_CipherInit_ex(), do not change | 39 Status AesCbcEncryptDecrypt(EncryptOrDecrypt cipher_operation, |
| 39 enum CipherOperation { kDoDecrypt = 0, kDoEncrypt = 1 }; | |
| 40 | |
| 41 Status AesCbcEncryptDecrypt(CipherOperation cipher_operation, | |
| 42 const blink::WebCryptoAlgorithm& algorithm, | 40 const blink::WebCryptoAlgorithm& algorithm, |
| 43 const blink::WebCryptoKey& key, | 41 const blink::WebCryptoKey& key, |
| 44 const CryptoData& data, | 42 const CryptoData& data, |
| 45 std::vector<uint8_t>* buffer) { | 43 std::vector<uint8_t>* buffer) { |
| 46 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 44 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 47 | 45 |
| 48 const blink::WebCryptoAesCbcParams* params = algorithm.aesCbcParams(); | 46 const blink::WebCryptoAesCbcParams* params = algorithm.aesCbcParams(); |
| 49 const std::vector<uint8_t>& raw_key = | 47 const std::vector<uint8_t>& raw_key = |
| 50 SymKeyOpenSsl::Cast(key)->raw_key_data(); | 48 SymKeyOpenSsl::Cast(key)->raw_key_data(); |
| 51 | 49 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } | 110 } |
| 113 | 111 |
| 114 class AesCbcImplementation : public AesAlgorithm { | 112 class AesCbcImplementation : public AesAlgorithm { |
| 115 public: | 113 public: |
| 116 AesCbcImplementation() : AesAlgorithm("CBC") {} | 114 AesCbcImplementation() : AesAlgorithm("CBC") {} |
| 117 | 115 |
| 118 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | 116 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, |
| 119 const blink::WebCryptoKey& key, | 117 const blink::WebCryptoKey& key, |
| 120 const CryptoData& data, | 118 const CryptoData& data, |
| 121 std::vector<uint8_t>* buffer) const OVERRIDE { | 119 std::vector<uint8_t>* buffer) const OVERRIDE { |
| 122 return AesCbcEncryptDecrypt(kDoEncrypt, algorithm, key, data, buffer); | 120 return AesCbcEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer); |
| 123 } | 121 } |
| 124 | 122 |
| 125 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | 123 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, |
| 126 const blink::WebCryptoKey& key, | 124 const blink::WebCryptoKey& key, |
| 127 const CryptoData& data, | 125 const CryptoData& data, |
| 128 std::vector<uint8_t>* buffer) const OVERRIDE { | 126 std::vector<uint8_t>* buffer) const OVERRIDE { |
| 129 return AesCbcEncryptDecrypt(kDoDecrypt, algorithm, key, data, buffer); | 127 return AesCbcEncryptDecrypt(DECRYPT, algorithm, key, data, buffer); |
| 130 } | 128 } |
| 131 }; | 129 }; |
| 132 | 130 |
| 133 } // namespace | 131 } // namespace |
| 134 | 132 |
| 135 AlgorithmImplementation* CreatePlatformAesCbcImplementation() { | 133 AlgorithmImplementation* CreatePlatformAesCbcImplementation() { |
| 136 return new AesCbcImplementation; | 134 return new AesCbcImplementation; |
| 137 } | 135 } |
| 138 | 136 |
| 139 } // namespace webcrypto | 137 } // namespace webcrypto |
| 140 | 138 |
| 141 } // namespace content | 139 } // namespace content |
| OLD | NEW |