| 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/cipher.h> |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/numerics/safe_math.h" | 12 #include "base/numerics/safe_math.h" |
| 13 #include "components/webcrypto/algorithms/aes.h" | 13 #include "components/webcrypto/algorithms/aes.h" |
| 14 #include "components/webcrypto/algorithms/util.h" | 14 #include "components/webcrypto/algorithms/util.h" |
| 15 #include "components/webcrypto/blink_key_handle.h" | 15 #include "components/webcrypto/blink_key_handle.h" |
| 16 #include "components/webcrypto/crypto_data.h" | 16 #include "components/webcrypto/crypto_data.h" |
| 17 #include "components/webcrypto/status.h" | 17 #include "components/webcrypto/status.h" |
| 18 #include "crypto/openssl_util.h" | 18 #include "crypto/openssl_util.h" |
| 19 #include "crypto/scoped_openssl_types.h" | |
| 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 21 | 20 |
| 22 namespace webcrypto { | 21 namespace webcrypto { |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 const EVP_CIPHER* GetAESCipherByKeyLength(size_t key_length_bytes) { | 25 const EVP_CIPHER* GetAESCipherByKeyLength(size_t key_length_bytes) { |
| 27 // 192-bit AES is intentionally unsupported (http://crbug.com/533699). | 26 // 192-bit AES is intentionally unsupported (http://crbug.com/533699). |
| 28 switch (key_length_bytes) { | 27 switch (key_length_bytes) { |
| 29 case 16: | 28 case 16: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 56 if (!output_max_len.IsValid()) | 55 if (!output_max_len.IsValid()) |
| 57 return Status::ErrorDataTooLarge(); | 56 return Status::ErrorDataTooLarge(); |
| 58 | 57 |
| 59 const unsigned remainder = output_max_len.ValueOrDie() % AES_BLOCK_SIZE; | 58 const unsigned remainder = output_max_len.ValueOrDie() % AES_BLOCK_SIZE; |
| 60 if (remainder != 0) | 59 if (remainder != 0) |
| 61 output_max_len += AES_BLOCK_SIZE - remainder; | 60 output_max_len += AES_BLOCK_SIZE - remainder; |
| 62 if (!output_max_len.IsValid()) | 61 if (!output_max_len.IsValid()) |
| 63 return Status::ErrorDataTooLarge(); | 62 return Status::ErrorDataTooLarge(); |
| 64 | 63 |
| 65 // Note: PKCS padding is enabled by default | 64 // Note: PKCS padding is enabled by default |
| 66 crypto::ScopedOpenSSL<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> context( | |
| 67 EVP_CIPHER_CTX_new()); | |
| 68 | |
| 69 if (!context.get()) | |
| 70 return Status::OperationError(); | |
| 71 | |
| 72 const EVP_CIPHER* const cipher = GetAESCipherByKeyLength(raw_key.size()); | 65 const EVP_CIPHER* const cipher = GetAESCipherByKeyLength(raw_key.size()); |
| 73 DCHECK(cipher); | 66 DCHECK(cipher); |
| 74 | 67 |
| 68 bssl::ScopedEVP_CIPHER_CTX context; |
| 75 if (!EVP_CipherInit_ex(context.get(), cipher, NULL, &raw_key[0], | 69 if (!EVP_CipherInit_ex(context.get(), cipher, NULL, &raw_key[0], |
| 76 params->iv().data(), cipher_operation)) { | 70 params->iv().data(), cipher_operation)) { |
| 77 return Status::OperationError(); | 71 return Status::OperationError(); |
| 78 } | 72 } |
| 79 | 73 |
| 80 buffer->resize(output_max_len.ValueOrDie()); | 74 buffer->resize(output_max_len.ValueOrDie()); |
| 81 | 75 |
| 82 int output_len = 0; | 76 int output_len = 0; |
| 83 if (!EVP_CipherUpdate(context.get(), buffer->data(), &output_len, | 77 if (!EVP_CipherUpdate(context.get(), buffer->data(), &output_len, |
| 84 data.bytes(), data.byte_length())) { | 78 data.bytes(), data.byte_length())) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 } | 112 } |
| 119 }; | 113 }; |
| 120 | 114 |
| 121 } // namespace | 115 } // namespace |
| 122 | 116 |
| 123 std::unique_ptr<AlgorithmImplementation> CreateAesCbcImplementation() { | 117 std::unique_ptr<AlgorithmImplementation> CreateAesCbcImplementation() { |
| 124 return base::WrapUnique(new AesCbcImplementation); | 118 return base::WrapUnique(new AesCbcImplementation); |
| 125 } | 119 } |
| 126 | 120 |
| 127 } // namespace webcrypto | 121 } // namespace webcrypto |
| OLD | NEW |