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..7c7e7ef3f9dbbdf3b88c237ed862c80579633684 |
--- /dev/null |
+++ b/content/renderer/webcrypto/webcrypto_util.cc |
@@ -0,0 +1,175 @@ |
+// 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 "base/base64.h" |
+#include "base/logging.h" |
+#include "content/renderer/webcrypto/webcrypto_util.h" |
eroman
2013/10/28 23:02:00
This header goes first, and then the rest (the hea
padolph
2013/10/29 02:25:40
Done.
|
+#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)); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateAlgorithmWithInnerHash( |
+ WebKit::WebCryptoAlgorithmId algorithm_id, |
+ unsigned short hash_key_length) { |
+ WebKit::WebCryptoAlgorithmId hash_id; |
+ switch (hash_key_length) { |
+ 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::WebCryptoAlgorithmIdSha384; |
+ break; |
+ default: |
+ NOTREACHED(); |
+ // FIXME WRONG TYPE |
+ return CreateAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc); |
+ // return WebKit::WebCryptoAlgorithm::createNull(); |
+ } |
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
+ algorithm_id, new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
+} |
+ |
+} |
eroman
2013/10/28 23:02:00
End with a comment " // namespace" to help matchi
padolph
2013/10/29 02:25:40
Done.
|
+ |
+// 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|. |
+const uint8* Start(const std::vector<uint8>& data) { |
+ if (data.empty()) |
+ return NULL; |
+ return &data[0]; |
+} |
+ |
+// TODO(eroman): This works by re-allocating a new buffer. It would be better if |
+// the WebArrayBuffer could just be truncated instead. |
+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; |
+} |
+ |
+// 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 |
+// transformation including adding padding if required, and then call a base64 |
+// decoder. |
+// In Web Crypto, this type of encoding is only used inside JWK. |
+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); |
+} |
+ |
+// *** Algorithm Factories *** |
+ |
+WebKit::WebCryptoAlgorithm CreateAlgorithm(WebKit::WebCryptoAlgorithmId id) { |
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
+} |
+ |
+// HMAC Operations Algorithm Factories |
+ |
+WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByKeyLen( |
+ unsigned short hash_key_length) { |
+ return CreateAlgorithmWithInnerHash(WebKit::WebCryptoAlgorithmIdHmac, |
+ hash_key_length); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( |
+ WebKit::WebCryptoAlgorithmId hashId) { |
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
+ WebKit::WebCryptoAlgorithmIdHmac, |
+ new WebKit::WebCryptoHmacParams(CreateAlgorithm(hashId))); |
+} |
+ |
+// HMAC Key Generation Algorithm Factories |
+ |
+WebKit::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( |
+ WebKit::WebCryptoAlgorithmId hashId, |
+ unsigned hash_length) { |
+ // hash_length < 0 means unspecified |
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
+ WebKit::WebCryptoAlgorithmIdHmac, |
+ new WebKit::WebCryptoHmacKeyParams( |
+ CreateAlgorithm(hashId), (hash_length != 0), hash_length)); |
+} |
+ |
+// RSA Operations Algorithm Factories |
+ |
+WebKit::WebCryptoAlgorithm CreateRsaEsAlgorithm() { |
+ return CreateAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateRsaSsaAlgorithmByKeyLen( |
+ unsigned short hash_key_length) { |
+ return CreateAlgorithmWithInnerHash( |
+ WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, hash_key_length); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateRsaOaepAlgorithmByKeyLen( |
+ unsigned short hash_key_length) { |
+ return CreateAlgorithmWithInnerHash(WebKit::WebCryptoAlgorithmIdRsaOaep, |
+ hash_key_length); |
+} |
+ |
+// AES Operations Algorithm Factories |
+ |
+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>& additionalData, |
+ unsigned char tagLength) { |
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
+ WebKit::WebCryptoAlgorithmIdAesCbc, |
+ new WebKit::WebCryptoAesGcmParams(Start(iv), |
+ iv.size(), |
+ additionalData.size(), |
+ Start(additionalData), |
+ additionalData.size(), |
+ tagLength != 0, |
+ tagLength)); |
+} |
+ |
+// AES Key Generation Algorithm Factories |
+ |
+WebKit::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(unsigned short length) { |
+ return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc, length); |
+} |
+ |
+WebKit::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(unsigned short length) { |
+ return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesGcm, length); |
+} |
+ |
+} // namespace content |