| 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 "components/webcrypto/algorithms/util.h" | 5 #include "components/webcrypto/algorithms/util.h" |
| 6 | 6 |
| 7 #include <openssl/aead.h> | 7 #include <openssl/aead.h> |
| 8 #include <openssl/bn.h> | 8 #include <openssl/bn.h> |
| 9 #include <openssl/digest.h> | 9 #include <openssl/digest.h> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/stl_util.h" | |
| 13 #include "components/webcrypto/crypto_data.h" | 12 #include "components/webcrypto/crypto_data.h" |
| 14 #include "components/webcrypto/status.h" | 13 #include "components/webcrypto/status.h" |
| 15 #include "crypto/openssl_util.h" | 14 #include "crypto/openssl_util.h" |
| 16 #include "crypto/scoped_openssl_types.h" | 15 #include "crypto/scoped_openssl_types.h" |
| 17 | 16 |
| 18 namespace webcrypto { | 17 namespace webcrypto { |
| 19 | 18 |
| 20 const EVP_MD* GetDigest(const blink::WebCryptoAlgorithm& hash_algorithm) { | 19 const EVP_MD* GetDigest(const blink::WebCryptoAlgorithm& hash_algorithm) { |
| 21 return GetDigest(hash_algorithm.id()); | 20 return GetDigest(hash_algorithm.id()); |
| 22 } | 21 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 const CryptoData& iv, | 79 const CryptoData& iv, |
| 81 const CryptoData& additional_data, | 80 const CryptoData& additional_data, |
| 82 const EVP_AEAD* aead_alg, | 81 const EVP_AEAD* aead_alg, |
| 83 std::vector<uint8_t>* buffer) { | 82 std::vector<uint8_t>* buffer) { |
| 84 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 83 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 85 EVP_AEAD_CTX ctx; | 84 EVP_AEAD_CTX ctx; |
| 86 | 85 |
| 87 if (!aead_alg) | 86 if (!aead_alg) |
| 88 return Status::ErrorUnexpected(); | 87 return Status::ErrorUnexpected(); |
| 89 | 88 |
| 90 if (!EVP_AEAD_CTX_init(&ctx, aead_alg, vector_as_array(&raw_key), | 89 if (!EVP_AEAD_CTX_init(&ctx, aead_alg, raw_key.data(), raw_key.size(), |
| 91 raw_key.size(), tag_length_bytes, NULL)) { | 90 tag_length_bytes, NULL)) { |
| 92 return Status::OperationError(); | 91 return Status::OperationError(); |
| 93 } | 92 } |
| 94 | 93 |
| 95 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup> ctx_cleanup(&ctx); | 94 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup> ctx_cleanup(&ctx); |
| 96 | 95 |
| 97 size_t len; | 96 size_t len; |
| 98 int ok; | 97 int ok; |
| 99 | 98 |
| 100 if (mode == DECRYPT) { | 99 if (mode == DECRYPT) { |
| 101 if (data.byte_length() < tag_length_bytes) | 100 if (data.byte_length() < tag_length_bytes) |
| 102 return Status::ErrorDataTooSmall(); | 101 return Status::ErrorDataTooSmall(); |
| 103 | 102 |
| 104 buffer->resize(data.byte_length() - tag_length_bytes); | 103 buffer->resize(data.byte_length() - tag_length_bytes); |
| 105 | 104 |
| 106 ok = EVP_AEAD_CTX_open(&ctx, vector_as_array(buffer), &len, buffer->size(), | 105 ok = EVP_AEAD_CTX_open(&ctx, buffer->data(), &len, buffer->size(), |
| 107 iv.bytes(), iv.byte_length(), data.bytes(), | 106 iv.bytes(), iv.byte_length(), data.bytes(), |
| 108 data.byte_length(), additional_data.bytes(), | 107 data.byte_length(), additional_data.bytes(), |
| 109 additional_data.byte_length()); | 108 additional_data.byte_length()); |
| 110 } else { | 109 } else { |
| 111 // No need to check for unsigned integer overflow here (seal fails if | 110 // No need to check for unsigned integer overflow here (seal fails if |
| 112 // the output buffer is too small). | 111 // the output buffer is too small). |
| 113 buffer->resize(data.byte_length() + EVP_AEAD_max_overhead(aead_alg)); | 112 buffer->resize(data.byte_length() + EVP_AEAD_max_overhead(aead_alg)); |
| 114 | 113 |
| 115 ok = EVP_AEAD_CTX_seal(&ctx, vector_as_array(buffer), &len, buffer->size(), | 114 ok = EVP_AEAD_CTX_seal(&ctx, buffer->data(), &len, buffer->size(), |
| 116 iv.bytes(), iv.byte_length(), data.bytes(), | 115 iv.bytes(), iv.byte_length(), data.bytes(), |
| 117 data.byte_length(), additional_data.bytes(), | 116 data.byte_length(), additional_data.bytes(), |
| 118 additional_data.byte_length()); | 117 additional_data.byte_length()); |
| 119 } | 118 } |
| 120 | 119 |
| 121 if (!ok) | 120 if (!ok) |
| 122 return Status::OperationError(); | 121 return Status::OperationError(); |
| 123 buffer->resize(len); | 122 buffer->resize(len); |
| 124 return Status::Success(); | 123 return Status::Success(); |
| 125 } | 124 } |
| 126 | 125 |
| 127 } // namespace webcrypto | 126 } // namespace webcrypto |
| OLD | NEW |