diff options
author | eroman <eroman@chromium.org> | 2015-03-05 10:12:24 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-05 18:13:24 +0000 |
commit | aab7aa9fb1703f5f36737dc05c3365a6c346a554 (patch) | |
tree | 2d01aefdb3f987d07e74a3a2ebac08f8ea784e09 | |
parent | 1b292c8b201c2777fc206c5ecf5049f77241f30a (diff) | |
download | chromium_src-aab7aa9fb1703f5f36737dc05c3365a6c346a554.zip chromium_src-aab7aa9fb1703f5f36737dc05c3365a6c346a554.tar.gz chromium_src-aab7aa9fb1703f5f36737dc05c3365a6c346a554.tar.bz2 |
Use vector_as_array() consistently throughout WebCrypto code.
In some cases this simplifies code by not explicitly handling the empty case.
Before the code was inconsistently using either:
* vector_as_array()
* ternery operator
* &v[0]
* v->front().
Once we have C++11 library support these will simply become v.data().
BUG=None
Review URL: https://codereview.chromium.org/974183006
Cr-Commit-Position: refs/heads/master@{#319285}
-rw-r--r-- | content/child/webcrypto/crypto_data.cc | 3 | ||||
-rw-r--r-- | content/child/webcrypto/openssl/aes_ctr_openssl.cc | 2 | ||||
-rw-r--r-- | content/child/webcrypto/openssl/ec_algorithm_openssl.cc | 4 | ||||
-rw-r--r-- | content/child/webcrypto/openssl/ecdh_openssl.cc | 8 | ||||
-rw-r--r-- | content/child/webcrypto/openssl/ecdsa_openssl.cc | 13 | ||||
-rw-r--r-- | content/child/webcrypto/openssl/hkdf_openssl.cc | 11 | ||||
-rw-r--r-- | content/child/webcrypto/openssl/pbkdf2_openssl.cc | 16 | ||||
-rw-r--r-- | content/child/webcrypto/openssl/rsa_sign_openssl.cc | 3 | ||||
-rw-r--r-- | content/child/webcrypto/test/sha_unittest.cc | 2 |
9 files changed, 31 insertions, 31 deletions
diff --git a/content/child/webcrypto/crypto_data.cc b/content/child/webcrypto/crypto_data.cc index 73bfc18..c80503c 100644 --- a/content/child/webcrypto/crypto_data.cc +++ b/content/child/webcrypto/crypto_data.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "content/child/webcrypto/crypto_data.h" +#include "base/stl_util.h" namespace content { @@ -16,7 +17,7 @@ CryptoData::CryptoData(const unsigned char* bytes, unsigned int byte_length) } CryptoData::CryptoData(const std::vector<unsigned char>& bytes) - : bytes_(bytes.size() ? &bytes[0] : NULL), byte_length_(bytes.size()) { + : bytes_(vector_as_array(&bytes)), byte_length_(bytes.size()) { } CryptoData::CryptoData(const std::string& bytes) diff --git a/content/child/webcrypto/openssl/aes_ctr_openssl.cc b/content/child/webcrypto/openssl/aes_ctr_openssl.cc index 43a1837..89e99ee 100644 --- a/content/child/webcrypto/openssl/aes_ctr_openssl.cc +++ b/content/child/webcrypto/openssl/aes_ctr_openssl.cc @@ -108,7 +108,7 @@ crypto::ScopedBIGNUM GetCounter(const CryptoData& counter_block, counter[0] &= ~(0xFF << counter_length_remainder_bits); return crypto::ScopedBIGNUM( - BN_bin2bn(&counter.front(), counter.size(), NULL)); + BN_bin2bn(vector_as_array(&counter), counter.size(), NULL)); } // Returns a counter block with the counter bits all set all zero. diff --git a/content/child/webcrypto/openssl/ec_algorithm_openssl.cc b/content/child/webcrypto/openssl/ec_algorithm_openssl.cc index b248235..7909fd9 100644 --- a/content/child/webcrypto/openssl/ec_algorithm_openssl.cc +++ b/content/child/webcrypto/openssl/ec_algorithm_openssl.cc @@ -161,8 +161,10 @@ Status WritePaddedBIGNUM(const std::string& member_name, size_t padded_length, JwkWriter* jwk) { std::vector<uint8_t> padded_bytes(padded_length); - if (!BN_bn2bin_padded(&padded_bytes.front(), padded_bytes.size(), value)) + if (!BN_bn2bin_padded(vector_as_array(&padded_bytes), padded_bytes.size(), + value)) { return Status::OperationError(); + } jwk->SetBytes(member_name, CryptoData(padded_bytes)); return Status::Success(); } diff --git a/content/child/webcrypto/openssl/ecdh_openssl.cc b/content/child/webcrypto/openssl/ecdh_openssl.cc index 9499a25..e4fa01b 100644 --- a/content/child/webcrypto/openssl/ecdh_openssl.cc +++ b/content/child/webcrypto/openssl/ecdh_openssl.cc @@ -7,6 +7,7 @@ #include <openssl/evp.h> #include "base/logging.h" +#include "base/stl_util.h" #include "content/child/webcrypto/algorithm_implementation.h" #include "content/child/webcrypto/crypto_data.h" #include "content/child/webcrypto/generate_key_result.h" @@ -99,8 +100,9 @@ class EcdhImplementation : public EcAlgorithm { unsigned int length_bits = has_optional_length_bits ? optional_length_bits : field_size_bytes * 8; - // Handle the empty length case now to avoid calling an undefined - // |&derived_bytes->front()| later. + // Short-circuit when deriving an empty key. + // TODO(eroman): ECDH_compute_key() is not happy when given a NULL output. + // http://crbug.com/464194. if (length_bits == 0) { derived_bytes->clear(); return Status::Success(); @@ -114,7 +116,7 @@ class EcdhImplementation : public EcAlgorithm { derived_bytes->resize(NumBitsToBytes(length_bits)); int result = - ECDH_compute_key(&derived_bytes->front(), derived_bytes->size(), + ECDH_compute_key(vector_as_array(derived_bytes), derived_bytes->size(), public_key_point, private_key_ec.get(), 0); if (result < 0 || static_cast<size_t>(result) != derived_bytes->size()) return Status::OperationError(); diff --git a/content/child/webcrypto/openssl/ecdsa_openssl.cc b/content/child/webcrypto/openssl/ecdsa_openssl.cc index a42b211..49585ee 100644 --- a/content/child/webcrypto/openssl/ecdsa_openssl.cc +++ b/content/child/webcrypto/openssl/ecdsa_openssl.cc @@ -7,6 +7,7 @@ #include <openssl/evp.h> #include "base/logging.h" +#include "base/stl_util.h" #include "content/child/webcrypto/algorithm_implementation.h" #include "content/child/webcrypto/crypto_data.h" #include "content/child/webcrypto/generate_key_result.h" @@ -68,14 +69,14 @@ Status ConvertDerSignatureToWebCryptoSignature( std::vector<uint8_t>* signature) { crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); - const unsigned char* der_data = &signature->front(); + const unsigned char* der_data = vector_as_array(signature); crypto::ScopedECDSA_SIG ecdsa_sig( d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(signature->size()))); if (!ecdsa_sig.get()) return Status::ErrorUnexpected(); // |der_data| is updated to point to past the end of the DER structure. - if (der_data != (&signature->front()) + signature->size()) + if (der_data != vector_as_array(signature) + signature->size()) return Status::ErrorUnexpected(); // Determine the maximum length of r and s. @@ -86,7 +87,7 @@ Status ConvertDerSignatureToWebCryptoSignature( signature->resize(order_size_bytes * 2); - if (!BN_bn2bin_padded(&signature->front(), order_size_bytes, + if (!BN_bn2bin_padded(vector_as_array(signature), order_size_bytes, ecdsa_sig.get()->r)) { return Status::ErrorUnexpected(); } @@ -151,7 +152,7 @@ Status ConvertWebCryptoSignatureToDerSignature( // DER-encode the signature. der_signature->resize(der_encoding_size); - uint8_t* result = &der_signature->front(); + uint8_t* result = vector_as_array(der_signature); if (0 > i2d_ECDSA_SIG(ecdsa_sig.get(), &result)) return Status::OperationError(); @@ -207,7 +208,7 @@ class EcdsaImplementation : public EcAlgorithm { } buffer->resize(sig_len); - if (!EVP_DigestSignFinal(ctx.get(), &buffer->front(), &sig_len)) + if (!EVP_DigestSignFinal(ctx.get(), vector_as_array(buffer), &sig_len)) return Status::OperationError(); buffer->resize(sig_len); @@ -252,7 +253,7 @@ class EcdsaImplementation : public EcAlgorithm { } *signature_match = - 1 == EVP_DigestVerifyFinal(ctx.get(), &der_signature.front(), + 1 == EVP_DigestVerifyFinal(ctx.get(), vector_as_array(&der_signature), der_signature.size()); return Status::Success(); } diff --git a/content/child/webcrypto/openssl/hkdf_openssl.cc b/content/child/webcrypto/openssl/hkdf_openssl.cc index 7a33b96..13005ba 100644 --- a/content/child/webcrypto/openssl/hkdf_openssl.cc +++ b/content/child/webcrypto/openssl/hkdf_openssl.cc @@ -72,13 +72,10 @@ class HkdfImplementation : public AlgorithmImplementation { // |algorithm|. const std::vector<uint8_t>& raw_key = SymKeyOpenSsl::Cast(base_key)->raw_key_data(); - const uint8_t* raw_key_ptr = raw_key.empty() ? NULL : &raw_key.front(); - uint8_t* derived_bytes_ptr = - derived_bytes->empty() ? NULL : &derived_bytes->front(); - if (!HKDF(derived_bytes_ptr, derived_bytes_len, digest_algorithm, - raw_key_ptr, raw_key.size(), params->salt().data(), - params->salt().size(), params->info().data(), - params->info().size())) { + if (!HKDF(vector_as_array(derived_bytes), derived_bytes_len, + digest_algorithm, vector_as_array(&raw_key), raw_key.size(), + params->salt().data(), params->salt().size(), + params->info().data(), params->info().size())) { uint32_t error = ERR_get_error(); if (ERR_GET_LIB(error) == ERR_LIB_HKDF && ERR_GET_REASON(error) == HKDF_R_OUTPUT_TOO_LARGE) { diff --git a/content/child/webcrypto/openssl/pbkdf2_openssl.cc b/content/child/webcrypto/openssl/pbkdf2_openssl.cc index d7ce485..5c7bf1a 100644 --- a/content/child/webcrypto/openssl/pbkdf2_openssl.cc +++ b/content/child/webcrypto/openssl/pbkdf2_openssl.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/stl_util.h" #include "content/child/webcrypto/algorithm_implementation.h" #include "content/child/webcrypto/crypto_data.h" #include "content/child/webcrypto/openssl/key_openssl.h" @@ -75,16 +76,11 @@ class Pbkdf2Implementation : public AlgorithmImplementation { const std::vector<uint8_t>& password = SymKeyOpenSsl::Cast(base_key)->raw_key_data(); - if (keylen_bytes == 0) - return Status::Success(); - - const char* password_ptr = - password.empty() ? NULL : reinterpret_cast<const char*>(&password[0]); - - if (!PKCS5_PBKDF2_HMAC(password_ptr, password.size(), params->salt().data(), - params->salt().size(), params->iterations(), - digest_algorithm, keylen_bytes, - &derived_bytes->front())) { + if (!PKCS5_PBKDF2_HMAC( + reinterpret_cast<const char*>(vector_as_array(&password)), + password.size(), params->salt().data(), params->salt().size(), + params->iterations(), digest_algorithm, keylen_bytes, + vector_as_array(derived_bytes))) { return Status::OperationError(); } return Status::Success(); diff --git a/content/child/webcrypto/openssl/rsa_sign_openssl.cc b/content/child/webcrypto/openssl/rsa_sign_openssl.cc index f24432e..be4449c 100644 --- a/content/child/webcrypto/openssl/rsa_sign_openssl.cc +++ b/content/child/webcrypto/openssl/rsa_sign_openssl.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/numerics/safe_math.h" +#include "base/stl_util.h" #include "content/child/webcrypto/crypto_data.h" #include "content/child/webcrypto/openssl/key_openssl.h" #include "content/child/webcrypto/openssl/rsa_sign_openssl.h" @@ -103,7 +104,7 @@ Status RsaSign(const blink::WebCryptoKey& key, } buffer->resize(sig_len); - if (!EVP_DigestSignFinal(ctx.get(), &buffer->front(), &sig_len)) + if (!EVP_DigestSignFinal(ctx.get(), vector_as_array(buffer), &sig_len)) return Status::OperationError(); buffer->resize(sig_len); diff --git a/content/child/webcrypto/test/sha_unittest.cc b/content/child/webcrypto/test/sha_unittest.cc index 65c19c0..50d58fc 100644 --- a/content/child/webcrypto/test/sha_unittest.cc +++ b/content/child/webcrypto/test/sha_unittest.cc @@ -69,7 +69,7 @@ TEST(WebCryptoShaTest, DigestSampleSetsInChunks) { size_t chunk_length = std::min(kChunkSizeBytes, length - chunk_index); std::vector<uint8_t> chunk(begin, begin + chunk_length); ASSERT_TRUE(chunk.size() > 0); - EXPECT_TRUE(digestor->consume(&chunk.front(), chunk.size())); + EXPECT_TRUE(digestor->consume(vector_as_array(&chunk), chunk.size())); chunk_index = chunk_index + chunk_length; begin = begin + chunk_length; } |