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..26e464177fb86f14f1a6aba450ae0e899f60de5c |
--- /dev/null |
+++ b/content/renderer/webcrypto/webcrypto_util.cc |
@@ -0,0 +1,158 @@ |
+// 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)); |
+} |
+ |
+} // 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 CreateHmacAlgorithmByDigestLen( |
+ unsigned short digest_length_bits) { |
+ WebKit::WebCryptoAlgorithmId hash_id; |
+ switch (digest_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) { |
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
eroman
2013/11/07 20:54:37
[optional] I propose adding DCHECK(IsHashAlgorithm
padolph
2013/11/09 00:33:38
Done.
|
+ WebKit::WebCryptoAlgorithmIdHmac, |
+ new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( |
+ WebKit::WebCryptoAlgorithmId hash_id, |
+ unsigned hash_length) { |
+ // hash_length < 0 means unspecified |
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
eroman
2013/11/07 20:54:37
[optional] I propose adding DCHECK(IsHashAlgorithm
padolph
2013/11/09 00:33:38
Done.
|
+ WebKit::WebCryptoAlgorithmIdHmac, |
+ new WebKit::WebCryptoHmacKeyParams( |
+ CreateAlgorithm(hash_id), (hash_length != 0), hash_length)); |
eroman
2013/11/07 20:54:37
'hash_length" isn't the right name, this is more l
padolph
2013/11/09 00:33:38
Done.
|
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateRsaSsaAlgorithm( |
+ WebKit::WebCryptoAlgorithmId hash_algorithm_id) { |
eroman
2013/11/07 20:54:37
hash_id
padolph
2013/11/09 00:33:38
Done.
|
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
eroman
2013/11/07 20:54:37
[optional] I propose adding DCHECK(IsHashAlgorithm
padolph
2013/11/09 00:33:38
Done.
|
+ WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
+ new WebKit::WebCryptoRsaSsaParams(CreateAlgorithm(hash_algorithm_id))); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateRsaOaepAlgorithm( |
+ WebKit::WebCryptoAlgorithmId hash_algorithm_id) { |
eroman
2013/11/07 20:54:37
hash_id
padolph
2013/11/09 00:33:38
Done.
|
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
eroman
2013/11/07 20:54:37
[optional] I propose adding DCHECK(IsHashAlgorithm
padolph
2013/11/09 00:33:38
Done.
|
+ WebKit::WebCryptoAlgorithmIdRsaOaep, |
+ new WebKit::WebCryptoRsaOaepParams( |
+ CreateAlgorithm(hash_algorithm_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, |
+ unsigned char tag_length) { |
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
+ WebKit::WebCryptoAlgorithmIdAesCbc, |
+ new WebKit::WebCryptoAesGcmParams(Start(iv), |
+ iv.size(), |
+ additional_data.size(), |
+ Start(additional_data), |
+ additional_data.size(), |
+ tag_length != 0, |
+ tag_length)); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(unsigned short length) { |
+ return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc, length); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(unsigned short length) { |
+ return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesGcm, length); |
+} |
+ |
+} // namespace content |