Commit 4c1ae71b authored by eroman@chromium.org's avatar eroman@chromium.org

[refactor] Replace custom utility function with base's "vector_as_array()"

BUG=395823
R=rsleevi@chromium.org

Review URL: https://codereview.chromium.org/410743002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284984 0039d316-1c4b-4281-b951-d872f2087c98
parent fb08c7a4
......@@ -5,6 +5,7 @@
#include <cryptohi.h>
#include "base/numerics/safe_math.h"
#include "base/stl_util.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/nss/aes_key_nss.h"
#include "content/child/webcrypto/nss/key_nss.h"
......@@ -72,7 +73,7 @@ Status AesCbcEncryptDecrypt(EncryptOrDecrypt mode,
// encryption, and can be smaller for decryption.
buffer->resize(output_max_len.ValueOrDie());
unsigned char* buffer_data = Uint8VectorStart(buffer);
unsigned char* buffer_data = vector_as_array(buffer);
int output_len;
if (SECSuccess != PK11_CipherOp(context.get(),
......
......@@ -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/nss/aes_key_nss.h"
#include "content/child/webcrypto/nss/key_nss.h"
......@@ -112,7 +113,7 @@ Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode,
// WebCryptoArrayBuffer is expensive that hack may be worth looking into.
buffer->resize(buffer_size.ValueOrDie());
unsigned char* buffer_data = Uint8VectorStart(buffer);
unsigned char* buffer_data = vector_as_array(buffer);
PK11_EncryptDecryptFunction encrypt_or_decrypt_func =
(mode == ENCRYPT) ? NssRuntimeSupport::Get()->pk11_encrypt_func()
......
......@@ -8,6 +8,7 @@
#include <sechash.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/jwk.h"
......@@ -191,7 +192,7 @@ class HmacImplementation : public AlgorithmImplementation {
DCHECK_NE(0u, signature_item.len);
buffer->resize(signature_item.len);
signature_item.data = Uint8VectorStart(buffer);
signature_item.data = vector_as_array(buffer);
if (PK11_SignWithSymKey(
sym_key, mechanism, &param_item, &signature_item, &data_item) !=
......@@ -216,7 +217,7 @@ class HmacImplementation : public AlgorithmImplementation {
// Do not allow verification of truncated MACs.
*signature_match = result.size() == signature.byte_length() &&
crypto::SecureMemEqual(Uint8VectorStart(result),
crypto::SecureMemEqual(vector_as_array(&result),
signature.bytes(),
signature.byte_length());
......
......@@ -8,6 +8,7 @@
#include <secerr.h>
#include <sechash.h>
#include "base/stl_util.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/nss/key_nss.h"
#include "content/child/webcrypto/nss/rsa_key_nss.h"
......@@ -96,7 +97,7 @@ Status EncryptRsaOaep(SECKEYPublicKey* key,
param.len = sizeof(oaep_params);
buffer->resize(SECKEY_PublicKeyStrength(key));
unsigned char* buffer_data = Uint8VectorStart(buffer);
unsigned char* buffer_data = vector_as_array(buffer);
unsigned int output_len;
if (NssRuntimeSupport::Get()->pk11_pub_encrypt_func()(key,
CKM_RSA_PKCS_OAEP,
......@@ -139,7 +140,7 @@ Status DecryptRsaOaep(SECKEYPrivateKey* key,
buffer->resize(modulus_length_bytes);
unsigned char* buffer_data = Uint8VectorStart(buffer);
unsigned char* buffer_data = vector_as_array(buffer);
unsigned int output_len;
if (NssRuntimeSupport::Get()->pk11_priv_decrypt_func()(key,
CKM_RSA_PKCS_OAEP,
......
......@@ -5,6 +5,7 @@
#include <sechash.h>
#include <vector>
#include "base/stl_util.h"
#include "content/child/webcrypto/algorithm_implementation.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/nss/util_nss.h"
......@@ -85,7 +86,7 @@ class DigestorNSS : public blink::WebCryptoDigestor {
unsigned int result_length = HASH_ResultLenContext(hash_context_);
result->resize(result_length);
unsigned char* digest = Uint8VectorStart(result);
unsigned char* digest = vector_as_array(result);
unsigned int digest_size; // ignored
return FinishInternal(digest, &digest_size);
}
......
......@@ -7,6 +7,7 @@
#include "base/logging.h"
#include "base/numerics/safe_math.h"
#include "base/stl_util.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/openssl/aes_key_openssl.h"
#include "content/child/webcrypto/openssl/key_openssl.h"
......@@ -83,7 +84,7 @@ Status AesCbcEncryptDecrypt(CipherOperation cipher_operation,
buffer->resize(output_max_len.ValueOrDie());
unsigned char* const buffer_data = Uint8VectorStart(buffer);
unsigned char* const buffer_data = vector_as_array(buffer);
int output_len = 0;
if (!EVP_CipherUpdate(context.get(),
......
......@@ -6,6 +6,7 @@
#include <openssl/evp.h>
#include "base/logging.h"
#include "base/stl_util.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/openssl/aes_key_openssl.h"
#include "content/child/webcrypto/openssl/key_openssl.h"
......@@ -62,7 +63,7 @@ Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode,
if (!EVP_AEAD_CTX_init(&ctx,
aead_alg,
Uint8VectorStart(raw_key),
vector_as_array(&raw_key),
raw_key.size(),
tag_length_bytes,
NULL)) {
......@@ -82,7 +83,7 @@ Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode,
buffer->resize(data.byte_length() - tag_length_bytes);
ok = EVP_AEAD_CTX_open(&ctx,
Uint8VectorStart(buffer),
vector_as_array(buffer),
&len,
buffer->size(),
iv.bytes(),
......@@ -97,7 +98,7 @@ Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode,
buffer->resize(data.byte_length() + tag_length_bytes);
ok = EVP_AEAD_CTX_seal(&ctx,
Uint8VectorStart(buffer),
vector_as_array(buffer),
&len,
buffer->size(),
iv.bytes(),
......
......@@ -5,6 +5,7 @@
#include <openssl/hmac.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/jwk.h"
......@@ -48,7 +49,7 @@ Status SignHmac(const std::vector<uint8_t>& raw_key,
buffer->resize(hmac_expected_length);
crypto::ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> hmac_result(
Uint8VectorStart(buffer), hmac_expected_length);
vector_as_array(buffer), hmac_expected_length);
unsigned int hmac_actual_length;
unsigned char* const success = HMAC(digest_algorithm,
......@@ -193,7 +194,7 @@ class HmacImplementation : public AlgorithmImplementation {
// Do not allow verification of truncated MACs.
*signature_match = result.size() == signature.byte_length() &&
crypto::SecureMemEqual(Uint8VectorStart(result),
crypto::SecureMemEqual(vector_as_array(&result),
signature.bytes(),
signature.byte_length());
......
......@@ -7,6 +7,7 @@
#include <openssl/sha.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/openssl/util_openssl.h"
......@@ -60,7 +61,7 @@ class DigestorOpenSsl : public blink::WebCryptoDigestor {
Status FinishWithVectorAndStatus(std::vector<uint8_t>* result) {
const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get());
result->resize(hash_expected_size);
unsigned char* const hash_buffer = Uint8VectorStart(result);
unsigned char* const hash_buffer = vector_as_array(result);
unsigned int hash_buffer_size; // ignored
return FinishInternal(hash_buffer, &hash_buffer_size);
}
......
......@@ -13,6 +13,7 @@
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
......@@ -159,7 +160,7 @@ blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm(
new blink::WebCryptoRsaHashedKeyGenParams(
CreateAlgorithm(hash_id),
modulus_length,
webcrypto::Uint8VectorStart(public_exponent),
vector_as_array(&public_exponent),
public_exponent.size()));
}
......@@ -169,7 +170,7 @@ blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
blink::WebCryptoAlgorithmIdRsaOaep,
new blink::WebCryptoRsaOaepParams(
!label.empty(), Uint8VectorStart(label), label.size()));
!label.empty(), vector_as_array(&label), label.size()));
}
// Creates an AES-CBC algorithm.
......@@ -177,7 +178,7 @@ blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(
const std::vector<uint8_t>& iv) {
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
blink::WebCryptoAlgorithmIdAesCbc,
new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size()));
new blink::WebCryptoAesCbcParams(vector_as_array(&iv), iv.size()));
}
// Creates an AES-GCM algorithm.
......@@ -188,10 +189,10 @@ blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
EXPECT_TRUE(SupportsAesGcm());
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
blink::WebCryptoAlgorithmIdAesGcm,
new blink::WebCryptoAesGcmParams(Uint8VectorStart(iv),
new blink::WebCryptoAesGcmParams(vector_as_array(&iv),
iv.size(),
true,
Uint8VectorStart(additional_data),
vector_as_array(&additional_data),
additional_data.size(),
true,
tag_length_bits));
......@@ -601,7 +602,7 @@ Status ImportKeyJwkFromDict(const base::DictionaryValue& dict,
scoped_ptr<base::DictionaryValue> GetJwkDictionary(
const std::vector<uint8_t>& json) {
base::StringPiece json_string(
reinterpret_cast<const char*>(Uint8VectorStart(json)), json.size());
reinterpret_cast<const char*>(vector_as_array(&json)), json.size());
base::Value* value = base::JSONReader::Read(json_string);
EXPECT_TRUE(value);
base::DictionaryValue* dict_value = NULL;
......@@ -903,7 +904,7 @@ TEST_F(SharedCryptoTest, HMACSampleSets) {
EXPECT_EQ(Status::Success(),
Verify(algorithm,
key,
CryptoData(Uint8VectorStart(output), output.size() - 1),
CryptoData(vector_as_array(&output), output.size() - 1),
CryptoData(test_message),
&signature_match));
EXPECT_FALSE(signature_match);
......@@ -2439,7 +2440,6 @@ TEST_F(SharedCryptoTest, MAYBE(ImportRsaPrivateKeyJwkIncorrectOptionalEmpty)) {
true,
blink::WebCryptoKeyUsageSign,
&key));
}
TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsa)) {
......@@ -2683,8 +2683,7 @@ TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsaBadExponent)) {
blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(),
GenerateKeyPair(
algorithm, true, 0, &public_key, &private_key));
GenerateKeyPair(algorithm, true, 0, &public_key, &private_key));
}
}
......@@ -2724,7 +2723,7 @@ TEST_F(SharedCryptoTest, RsaSsaSignVerifyFailures) {
Status::Success(),
Verify(algorithm,
public_key,
CryptoData(Uint8VectorStart(signature), signature.size() - 1),
CryptoData(vector_as_array(&signature), signature.size() - 1),
CryptoData(data),
&signature_match));
EXPECT_FALSE(signature_match);
......@@ -3430,15 +3429,15 @@ TEST_F(SharedCryptoTest, MAYBE(UnwrapAesCbc192)) {
blink::WebCryptoKeyUsageUnwrapKey);
blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
ASSERT_EQ(Status::ErrorAes192BitUnsupported(),
UnwrapKey(blink::WebCryptoKeyFormatRaw,
CryptoData(wrapped_key),
wrapping_key,
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw),
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
true,
blink::WebCryptoKeyUsageEncrypt,
&unwrapped_key));
ASSERT_EQ(Status::ErrorAes192BitUnsupported(),
UnwrapKey(blink::WebCryptoKeyFormatRaw,
CryptoData(wrapped_key),
wrapping_key,
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw),
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
true,
blink::WebCryptoKeyUsageEncrypt,
&unwrapped_key));
}
class SharedCryptoRsaOaepTest : public ::testing::Test {
......
......@@ -10,6 +10,7 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/sequenced_worker_pool.h"
......@@ -119,8 +120,7 @@ void CompleteWithBufferOrError(const Status& status,
// theoretically this could overflow.
CompleteWithError(Status::ErrorUnexpected(), result);
} else {
result->completeWithBuffer(webcrypto::Uint8VectorStart(buffer),
buffer.size());
result->completeWithBuffer(vector_as_array(&buffer), buffer.size());
}
}
}
......@@ -481,8 +481,7 @@ void DoExportKeyReply(scoped_ptr<ExportKeyState> state) {
CompleteWithError(state->status, &state->result);
} else {
state->result.completeWithJson(
reinterpret_cast<const char*>(
webcrypto::Uint8VectorStart(&state->buffer)),
reinterpret_cast<const char*>(vector_as_array(&state->buffer)),
state->buffer.size());
}
}
......
......@@ -6,6 +6,7 @@
#include "base/base64.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "content/child/webcrypto/status.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
......@@ -16,18 +17,6 @@ namespace content {
namespace webcrypto {
const uint8_t* Uint8VectorStart(const std::vector<uint8_t>& data) {
if (data.empty())
return NULL;
return &data[0];
}
uint8_t* Uint8VectorStart(std::vector<uint8_t>* data) {
if (data->empty())
return NULL;
return &(*data)[0];
}
// This function decodes unpadded 'base64url' encoded data, as described in
// RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first
// change the incoming data to 'base64' encoding by applying the appropriate
......@@ -54,7 +43,7 @@ std::string Base64EncodeUrlSafe(const base::StringPiece& input) {
std::string Base64EncodeUrlSafe(const std::vector<uint8_t>& input) {
const base::StringPiece string_piece(
reinterpret_cast<const char*>(Uint8VectorStart(input)), input.size());
reinterpret_cast<const char*>(vector_as_array(&input)), input.size());
return Base64EncodeUrlSafe(string_piece);
}
......
......@@ -21,13 +21,6 @@ namespace webcrypto {
class Status;
// Returns a pointer to the start of |data|, or NULL if it is empty. This is a
// convenience function for getting the pointer, and should not be used beyond
// the expected lifetime of |data|.
CONTENT_EXPORT const uint8_t* Uint8VectorStart(
const std::vector<uint8_t>& data);
CONTENT_EXPORT uint8_t* Uint8VectorStart(std::vector<uint8_t>* data);
// This function decodes unpadded 'base64url' encoded data, as described in
// RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5.
// In Web Crypto, this type of encoding is only used inside JWK.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment