Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1688)

Unified Diff: content/child/webcrypto/platform_crypto_openssl.cc

Issue 200763005: Support for the new WebCrypto digest by chunks API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated to latest WebCrypto API changes Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/child/webcrypto/platform_crypto_openssl.cc
diff --git a/content/child/webcrypto/platform_crypto_openssl.cc b/content/child/webcrypto/platform_crypto_openssl.cc
index 30825a89b219bee5ed0a51ee4d8e456af4096fa5..e2d03ae05482fd247ec1c2f8dd9e9d6b07e9f9ce 100644
--- a/content/child/webcrypto/platform_crypto_openssl.cc
+++ b/content/child/webcrypto/platform_crypto_openssl.cc
@@ -12,6 +12,7 @@
#include <openssl/sha.h>
#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/status.h"
#include "content/child/webcrypto/webcrypto_util.h"
@@ -151,6 +152,95 @@ Status AesCbcEncryptDecrypt(EncryptOrDecrypt mode,
} // namespace
+class DigestorOpenSSL : public blink::WebCryptoDigestor {
+ public:
+ DigestorOpenSSL(blink::WebCryptoAlgorithmId algorithm_id)
eroman 2014/03/25 23:26:23 explicit
jww 2014/03/26 00:42:31 Done.
+ : initialized_(false),
+ digest_context_(EVP_MD_CTX_create()),
+ algorithm_id_(algorithm_id) {}
+
+ virtual bool consume(const unsigned char* data, unsigned int size) OVERRIDE {
eroman 2014/03/25 23:26:23 ditto on OVERRIDE. I expect this will be pretty st
jww 2014/03/26 00:42:31 Done.
+ return consumeWithStatus(data, size).IsSuccess();
+ }
+
+ Status consumeWithStatus(const unsigned char* data, unsigned int size) {
eroman 2014/03/25 23:26:23 For ConsumeWithStatus (should use chromium style f
jww 2014/03/26 00:42:31 Done.
+ crypto::OpenSSLErrStackTracer(FROM_HERE);
+ Status error = init();
+ if (!error.IsSuccess())
+ return error;
+
+ if (!EVP_DigestUpdate(digest_context_.get(), data, size))
+ return Status::Error();
+
+ return Status::Success();
+ }
+
+ virtual bool finish(unsigned char*& result_data,
+ unsigned int& result_data_size) OVERRIDE {
+ Status error = finishInternal(result_, &result_data_size);
+ if (!error.IsSuccess())
+ return false;
+ result_data = result_;
+ return true;
+ }
+
+ virtual Status finishWithWebArrayAndStatus(blink::WebArrayBuffer& result) {
eroman 2014/03/25 23:26:23 Make this non-virtual. Also use chromium-style fun
jww 2014/03/26 00:42:31 See my question in platform_crypto_nss.cc about th
+ const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get());
+ result = blink::WebArrayBuffer::create(hash_expected_size, 1);
+ unsigned char* const hash_buffer =
+ static_cast<unsigned char* const>(result.data());
+ unsigned int hash_buffer_size; // ignored
+ Status error = finishInternal(hash_buffer, &hash_buffer_size);
+ if (!error.IsSuccess())
+ result.reset();
+ return error;
+ }
+
+ private:
+ bool initialized_;
+ crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context_;
+ blink::WebCryptoAlgorithmId algorithm_id_;
+ unsigned char result_[EVP_MAX_MD_SIZE];
+
+ DigestorOpenSSL() {}
eroman 2014/03/25 23:26:23 Is this necessary?
jww 2014/03/26 00:42:31 Removed.
+
+ Status init() {
+ if (initialized_)
+ return Status::Success();
+
+ const EVP_MD* digest_algorithm = GetDigest(algorithm_id_);
+ if (!digest_algorithm)
+ return Status::ErrorUnexpected();
+
+ if (!digest_context_.get())
+ return Status::Error();
+
+ if (!EVP_DigestInit_ex(digest_context_.get(), digest_algorithm, NULL))
+ return Status::Error();
+
+ initialized_ = true;
+ return Status::Success();
+ }
+
+ Status finishInternal(unsigned char* result, unsigned int* result_size) {
+ crypto::OpenSSLErrStackTracer(FROM_HERE);
+ Status error = init();
+ if (!error.IsSuccess())
+ return error;
+
+ const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get());
+ if (hash_expected_size <= 0)
+ return Status::ErrorUnexpected();
+ DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE);
+
+ if (!EVP_DigestFinal_ex(digest_context_.get(), result, result_size) ||
+ static_cast<int>(*result_size) != hash_expected_size)
+ return Status::Error();
+
+ return Status::Success();
+ }
+};
+
Status ExportKeyRaw(SymKey* key, blink::WebArrayBuffer* buffer) {
*buffer = CreateArrayBuffer(Uint8VectorStart(key->key()), key->key().size());
return Status::Success();
@@ -170,40 +260,16 @@ Status EncryptDecryptAesCbc(EncryptOrDecrypt mode,
Status DigestSha(blink::WebCryptoAlgorithmId algorithm,
const CryptoData& data,
blink::WebArrayBuffer* buffer) {
- crypto::OpenSSLErrStackTracer(FROM_HERE);
-
- const EVP_MD* digest_algorithm = GetDigest(algorithm);
- if (!digest_algorithm)
- return Status::ErrorUnexpected();
-
- crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context(
- EVP_MD_CTX_create());
- if (!digest_context.get())
- return Status::Error();
-
- if (!EVP_DigestInit_ex(digest_context.get(), digest_algorithm, NULL) ||
- !EVP_DigestUpdate(
- digest_context.get(), data.bytes(), data.byte_length())) {
- return Status::Error();
- }
-
- const int hash_expected_size = EVP_MD_CTX_size(digest_context.get());
- if (hash_expected_size <= 0)
- return Status::ErrorUnexpected();
- DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE);
-
- *buffer = blink::WebArrayBuffer::create(hash_expected_size, 1);
- unsigned char* const hash_buffer =
- reinterpret_cast<unsigned char* const>(buffer->data());
-
- unsigned int hash_size = 0;
- if (!EVP_DigestFinal_ex(digest_context.get(), hash_buffer, &hash_size) ||
- static_cast<int>(hash_size) != hash_expected_size) {
- buffer->reset();
- return Status::Error();
- }
+ DigestorOpenSSL digestor(algorithm);
+ Status error = digestor.consumeWithStatus(data.bytes(), data.byte_length());
+ if (!error.IsSuccess())
+ return error;
+ return digestor.finishWithWebArrayAndStatus(*buffer);
+}
- return Status::Success();
+blink::WebCryptoDigestor* CreateDigestor(
+ blink::WebCryptoAlgorithmId algorithm_id) {
+ return new DigestorOpenSSL(algorithm_id);
}
Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,

Powered by Google App Engine
This is Rietveld 408576698