| 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 "components/webcrypto/crypto_data.h" | 12 #include "components/webcrypto/crypto_data.h" |
| 13 #include "components/webcrypto/status.h" | 13 #include "components/webcrypto/status.h" |
| 14 #include "crypto/openssl_util.h" | 14 #include "crypto/openssl_util.h" |
| 15 #include "crypto/scoped_openssl_types.h" | |
| 16 | 15 |
| 17 namespace webcrypto { | 16 namespace webcrypto { |
| 18 | 17 |
| 19 const EVP_MD* GetDigest(const blink::WebCryptoAlgorithm& hash_algorithm) { | 18 const EVP_MD* GetDigest(const blink::WebCryptoAlgorithm& hash_algorithm) { |
| 20 return GetDigest(hash_algorithm.id()); | 19 return GetDigest(hash_algorithm.id()); |
| 21 } | 20 } |
| 22 | 21 |
| 23 const EVP_MD* GetDigest(blink::WebCryptoAlgorithmId id) { | 22 const EVP_MD* GetDigest(blink::WebCryptoAlgorithmId id) { |
| 24 switch (id) { | 23 switch (id) { |
| 25 case blink::WebCryptoAlgorithmIdSha1: | 24 case blink::WebCryptoAlgorithmIdSha1: |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 67 |
| 69 Status AeadEncryptDecrypt(EncryptOrDecrypt mode, | 68 Status AeadEncryptDecrypt(EncryptOrDecrypt mode, |
| 70 const std::vector<uint8_t>& raw_key, | 69 const std::vector<uint8_t>& raw_key, |
| 71 const CryptoData& data, | 70 const CryptoData& data, |
| 72 unsigned int tag_length_bytes, | 71 unsigned int tag_length_bytes, |
| 73 const CryptoData& iv, | 72 const CryptoData& iv, |
| 74 const CryptoData& additional_data, | 73 const CryptoData& additional_data, |
| 75 const EVP_AEAD* aead_alg, | 74 const EVP_AEAD* aead_alg, |
| 76 std::vector<uint8_t>* buffer) { | 75 std::vector<uint8_t>* buffer) { |
| 77 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 76 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 78 EVP_AEAD_CTX ctx; | 77 bssl::ScopedEVP_AEAD_CTX ctx; |
| 79 | 78 |
| 80 if (!aead_alg) | 79 if (!aead_alg) |
| 81 return Status::ErrorUnexpected(); | 80 return Status::ErrorUnexpected(); |
| 82 | 81 |
| 83 if (!EVP_AEAD_CTX_init(&ctx, aead_alg, raw_key.data(), raw_key.size(), | 82 if (!EVP_AEAD_CTX_init(ctx.get(), aead_alg, raw_key.data(), raw_key.size(), |
| 84 tag_length_bytes, NULL)) { | 83 tag_length_bytes, NULL)) { |
| 85 return Status::OperationError(); | 84 return Status::OperationError(); |
| 86 } | 85 } |
| 87 | 86 |
| 88 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup> ctx_cleanup(&ctx); | |
| 89 | |
| 90 size_t len; | 87 size_t len; |
| 91 int ok; | 88 int ok; |
| 92 | 89 |
| 93 if (mode == DECRYPT) { | 90 if (mode == DECRYPT) { |
| 94 if (data.byte_length() < tag_length_bytes) | 91 if (data.byte_length() < tag_length_bytes) |
| 95 return Status::ErrorDataTooSmall(); | 92 return Status::ErrorDataTooSmall(); |
| 96 | 93 |
| 97 buffer->resize(data.byte_length() - tag_length_bytes); | 94 buffer->resize(data.byte_length() - tag_length_bytes); |
| 98 | 95 |
| 99 ok = EVP_AEAD_CTX_open(&ctx, buffer->data(), &len, buffer->size(), | 96 ok = EVP_AEAD_CTX_open(ctx.get(), buffer->data(), &len, buffer->size(), |
| 100 iv.bytes(), iv.byte_length(), data.bytes(), | 97 iv.bytes(), iv.byte_length(), data.bytes(), |
| 101 data.byte_length(), additional_data.bytes(), | 98 data.byte_length(), additional_data.bytes(), |
| 102 additional_data.byte_length()); | 99 additional_data.byte_length()); |
| 103 } else { | 100 } else { |
| 104 // No need to check for unsigned integer overflow here (seal fails if | 101 // No need to check for unsigned integer overflow here (seal fails if |
| 105 // the output buffer is too small). | 102 // the output buffer is too small). |
| 106 buffer->resize(data.byte_length() + EVP_AEAD_max_overhead(aead_alg)); | 103 buffer->resize(data.byte_length() + EVP_AEAD_max_overhead(aead_alg)); |
| 107 | 104 |
| 108 ok = EVP_AEAD_CTX_seal(&ctx, buffer->data(), &len, buffer->size(), | 105 ok = EVP_AEAD_CTX_seal(ctx.get(), buffer->data(), &len, buffer->size(), |
| 109 iv.bytes(), iv.byte_length(), data.bytes(), | 106 iv.bytes(), iv.byte_length(), data.bytes(), |
| 110 data.byte_length(), additional_data.bytes(), | 107 data.byte_length(), additional_data.bytes(), |
| 111 additional_data.byte_length()); | 108 additional_data.byte_length()); |
| 112 } | 109 } |
| 113 | 110 |
| 114 if (!ok) | 111 if (!ok) |
| 115 return Status::OperationError(); | 112 return Status::OperationError(); |
| 116 buffer->resize(len); | 113 buffer->resize(len); |
| 117 return Status::Success(); | 114 return Status::Success(); |
| 118 } | 115 } |
| 119 | 116 |
| 120 } // namespace webcrypto | 117 } // namespace webcrypto |
| OLD | NEW |