| Index: content/renderer/webcrypto/webcrypto_util.cc
|
| diff --git a/content/renderer/webcrypto/webcrypto_util.cc b/content/renderer/webcrypto/webcrypto_util.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..0fe413e0b5f35571d448a6c2526e31ebc8137783
|
| --- /dev/null
|
| +++ b/content/renderer/webcrypto/webcrypto_util.cc
|
| @@ -0,0 +1,174 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/renderer/webcrypto/webcrypto_util.h"
|
| +
|
| +#include "base/base64.h"
|
| +#include "base/logging.h"
|
| +#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
|
| +#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
|
| + WebKit::WebCryptoAlgorithmId aes_alg_id,
|
| + unsigned short length) {
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
|
| + aes_alg_id, new WebKit::WebCryptoAesKeyGenParams(length));
|
| +}
|
| +
|
| +bool IsHashAlgorithm(WebKit::WebCryptoAlgorithmId alg_id) {
|
| + return alg_id == WebKit::WebCryptoAlgorithmIdSha1 ||
|
| + alg_id == WebKit::WebCryptoAlgorithmIdSha224 ||
|
| + alg_id == WebKit::WebCryptoAlgorithmIdSha256 ||
|
| + alg_id == WebKit::WebCryptoAlgorithmIdSha384 ||
|
| + alg_id == WebKit::WebCryptoAlgorithmIdSha512;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +const uint8* Start(const std::vector<uint8>& data) {
|
| + if (data.empty())
|
| + return NULL;
|
| + return &data[0];
|
| +}
|
| +
|
| +void ShrinkBuffer(WebKit::WebArrayBuffer* buffer, unsigned new_size) {
|
| + DCHECK_LE(new_size, buffer->byteLength());
|
| +
|
| + if (new_size == buffer->byteLength())
|
| + return;
|
| +
|
| + WebKit::WebArrayBuffer new_buffer =
|
| + WebKit::WebArrayBuffer::create(new_size, 1);
|
| + DCHECK(!new_buffer.isNull());
|
| + memcpy(new_buffer.data(), buffer->data(), new_size);
|
| + *buffer = new_buffer;
|
| +}
|
| +
|
| +bool Base64DecodeUrlSafe(const std::string& input, std::string* output) {
|
| + std::string base64EncodedText(input);
|
| + std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+');
|
| + std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/');
|
| + base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '=');
|
| + return base::Base64Decode(base64EncodedText, output);
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm GetInnerHashAlgorithm(
|
| + const WebKit::WebCryptoAlgorithm& algorithm) {
|
| + if (algorithm.hmacParams())
|
| + return algorithm.hmacParams()->hash();
|
| + if (algorithm.hmacKeyParams())
|
| + return algorithm.hmacKeyParams()->hash();
|
| + if (algorithm.rsaSsaParams())
|
| + return algorithm.rsaSsaParams()->hash();
|
| + if (algorithm.rsaOaepParams())
|
| + return algorithm.rsaOaepParams()->hash();
|
| + return WebKit::WebCryptoAlgorithm::createNull();
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateAlgorithm(WebKit::WebCryptoAlgorithmId id) {
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL);
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByHashOutputLen(
|
| + unsigned short hash_output_length_bits) {
|
| + WebKit::WebCryptoAlgorithmId hash_id;
|
| + switch (hash_output_length_bits) {
|
| + case 160:
|
| + hash_id = WebKit::WebCryptoAlgorithmIdSha1;
|
| + break;
|
| + case 224:
|
| + hash_id = WebKit::WebCryptoAlgorithmIdSha224;
|
| + break;
|
| + case 256:
|
| + hash_id = WebKit::WebCryptoAlgorithmIdSha256;
|
| + break;
|
| + case 384:
|
| + hash_id = WebKit::WebCryptoAlgorithmIdSha384;
|
| + break;
|
| + case 512:
|
| + hash_id = WebKit::WebCryptoAlgorithmIdSha512;
|
| + break;
|
| + default:
|
| + NOTREACHED();
|
| + return WebKit::WebCryptoAlgorithm::createNull();
|
| + }
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
|
| + WebKit::WebCryptoAlgorithmIdHmac,
|
| + new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id)));
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByHashId(
|
| + WebKit::WebCryptoAlgorithmId hash_id) {
|
| + DCHECK(IsHashAlgorithm(hash_id));
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
|
| + WebKit::WebCryptoAlgorithmIdHmac,
|
| + new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id)));
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm(
|
| + WebKit::WebCryptoAlgorithmId hash_id,
|
| + unsigned key_length_bytes) {
|
| + DCHECK(IsHashAlgorithm(hash_id));
|
| + // key_length_bytes == 0 means unspecified
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
|
| + WebKit::WebCryptoAlgorithmIdHmac,
|
| + new WebKit::WebCryptoHmacKeyParams(
|
| + CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes));
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateRsaSsaAlgorithm(
|
| + WebKit::WebCryptoAlgorithmId hash_id) {
|
| + DCHECK(IsHashAlgorithm(hash_id));
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
|
| + WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
|
| + new WebKit::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id)));
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
|
| + WebKit::WebCryptoAlgorithmId hash_id) {
|
| + DCHECK(IsHashAlgorithm(hash_id));
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
|
| + WebKit::WebCryptoAlgorithmIdRsaOaep,
|
| + new WebKit::WebCryptoRsaOaepParams(
|
| + CreateAlgorithm(hash_id), false, NULL, 0));
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) {
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
|
| + WebKit::WebCryptoAlgorithmIdAesCbc,
|
| + new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size()));
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateAesGcmAlgorithm(
|
| + const std::vector<uint8>& iv,
|
| + const std::vector<uint8>& additional_data,
|
| + uint8 tag_length_bytes) {
|
| + return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
|
| + WebKit::WebCryptoAlgorithmIdAesCbc,
|
| + new WebKit::WebCryptoAesGcmParams(Start(iv),
|
| + iv.size(),
|
| + additional_data.size(),
|
| + Start(additional_data),
|
| + additional_data.size(),
|
| + tag_length_bytes != 0,
|
| + tag_length_bytes));
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(
|
| + unsigned short key_length_bits) {
|
| + return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc,
|
| + key_length_bits);
|
| +}
|
| +
|
| +WebKit::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(
|
| + unsigned short key_length_bits) {
|
| + return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesGcm,
|
| + key_length_bits);
|
| +}
|
| +
|
| +} // namespace content
|
|
|