OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/webcrypto/webcrypto_util.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/logging.h" |
| 9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 namespace { |
| 15 |
| 16 WebKit::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( |
| 17 WebKit::WebCryptoAlgorithmId aes_alg_id, |
| 18 unsigned short length) { |
| 19 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 20 aes_alg_id, new WebKit::WebCryptoAesKeyGenParams(length)); |
| 21 } |
| 22 |
| 23 bool IsHashAlgorithm(WebKit::WebCryptoAlgorithmId alg_id) { |
| 24 return alg_id == WebKit::WebCryptoAlgorithmIdSha1 || |
| 25 alg_id == WebKit::WebCryptoAlgorithmIdSha224 || |
| 26 alg_id == WebKit::WebCryptoAlgorithmIdSha256 || |
| 27 alg_id == WebKit::WebCryptoAlgorithmIdSha384 || |
| 28 alg_id == WebKit::WebCryptoAlgorithmIdSha512; |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 const uint8* Start(const std::vector<uint8>& data) { |
| 34 if (data.empty()) |
| 35 return NULL; |
| 36 return &data[0]; |
| 37 } |
| 38 |
| 39 void ShrinkBuffer(WebKit::WebArrayBuffer* buffer, unsigned new_size) { |
| 40 DCHECK_LE(new_size, buffer->byteLength()); |
| 41 |
| 42 if (new_size == buffer->byteLength()) |
| 43 return; |
| 44 |
| 45 WebKit::WebArrayBuffer new_buffer = |
| 46 WebKit::WebArrayBuffer::create(new_size, 1); |
| 47 DCHECK(!new_buffer.isNull()); |
| 48 memcpy(new_buffer.data(), buffer->data(), new_size); |
| 49 *buffer = new_buffer; |
| 50 } |
| 51 |
| 52 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
| 53 std::string base64EncodedText(input); |
| 54 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); |
| 55 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); |
| 56 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); |
| 57 return base::Base64Decode(base64EncodedText, output); |
| 58 } |
| 59 |
| 60 WebKit::WebCryptoAlgorithm GetInnerHashAlgorithm( |
| 61 const WebKit::WebCryptoAlgorithm& algorithm) { |
| 62 if (algorithm.hmacParams()) |
| 63 return algorithm.hmacParams()->hash(); |
| 64 if (algorithm.hmacKeyParams()) |
| 65 return algorithm.hmacKeyParams()->hash(); |
| 66 if (algorithm.rsaSsaParams()) |
| 67 return algorithm.rsaSsaParams()->hash(); |
| 68 if (algorithm.rsaOaepParams()) |
| 69 return algorithm.rsaOaepParams()->hash(); |
| 70 return WebKit::WebCryptoAlgorithm::createNull(); |
| 71 } |
| 72 |
| 73 WebKit::WebCryptoAlgorithm CreateAlgorithm(WebKit::WebCryptoAlgorithmId id) { |
| 74 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
| 75 } |
| 76 |
| 77 WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByHashOutputLen( |
| 78 unsigned short hash_output_length_bits) { |
| 79 WebKit::WebCryptoAlgorithmId hash_id; |
| 80 switch (hash_output_length_bits) { |
| 81 case 160: |
| 82 hash_id = WebKit::WebCryptoAlgorithmIdSha1; |
| 83 break; |
| 84 case 224: |
| 85 hash_id = WebKit::WebCryptoAlgorithmIdSha224; |
| 86 break; |
| 87 case 256: |
| 88 hash_id = WebKit::WebCryptoAlgorithmIdSha256; |
| 89 break; |
| 90 case 384: |
| 91 hash_id = WebKit::WebCryptoAlgorithmIdSha384; |
| 92 break; |
| 93 case 512: |
| 94 hash_id = WebKit::WebCryptoAlgorithmIdSha512; |
| 95 break; |
| 96 default: |
| 97 NOTREACHED(); |
| 98 return WebKit::WebCryptoAlgorithm::createNull(); |
| 99 } |
| 100 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 101 WebKit::WebCryptoAlgorithmIdHmac, |
| 102 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
| 103 } |
| 104 |
| 105 WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( |
| 106 WebKit::WebCryptoAlgorithmId hash_id) { |
| 107 DCHECK(IsHashAlgorithm(hash_id)); |
| 108 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 109 WebKit::WebCryptoAlgorithmIdHmac, |
| 110 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
| 111 } |
| 112 |
| 113 WebKit::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( |
| 114 WebKit::WebCryptoAlgorithmId hash_id, |
| 115 unsigned key_length_bytes) { |
| 116 DCHECK(IsHashAlgorithm(hash_id)); |
| 117 // key_length_bytes == 0 means unspecified |
| 118 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 119 WebKit::WebCryptoAlgorithmIdHmac, |
| 120 new WebKit::WebCryptoHmacKeyParams( |
| 121 CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes)); |
| 122 } |
| 123 |
| 124 WebKit::WebCryptoAlgorithm CreateRsaSsaAlgorithm( |
| 125 WebKit::WebCryptoAlgorithmId hash_id) { |
| 126 DCHECK(IsHashAlgorithm(hash_id)); |
| 127 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 128 WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 129 new WebKit::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id))); |
| 130 } |
| 131 |
| 132 WebKit::WebCryptoAlgorithm CreateRsaOaepAlgorithm( |
| 133 WebKit::WebCryptoAlgorithmId hash_id) { |
| 134 DCHECK(IsHashAlgorithm(hash_id)); |
| 135 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 136 WebKit::WebCryptoAlgorithmIdRsaOaep, |
| 137 new WebKit::WebCryptoRsaOaepParams( |
| 138 CreateAlgorithm(hash_id), false, NULL, 0)); |
| 139 } |
| 140 |
| 141 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { |
| 142 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 143 WebKit::WebCryptoAlgorithmIdAesCbc, |
| 144 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size())); |
| 145 } |
| 146 |
| 147 WebKit::WebCryptoAlgorithm CreateAesGcmAlgorithm( |
| 148 const std::vector<uint8>& iv, |
| 149 const std::vector<uint8>& additional_data, |
| 150 uint8 tag_length_bytes) { |
| 151 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 152 WebKit::WebCryptoAlgorithmIdAesCbc, |
| 153 new WebKit::WebCryptoAesGcmParams(Start(iv), |
| 154 iv.size(), |
| 155 additional_data.size(), |
| 156 Start(additional_data), |
| 157 additional_data.size(), |
| 158 tag_length_bytes != 0, |
| 159 tag_length_bytes)); |
| 160 } |
| 161 |
| 162 WebKit::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm( |
| 163 unsigned short key_length_bits) { |
| 164 return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc, |
| 165 key_length_bits); |
| 166 } |
| 167 |
| 168 WebKit::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm( |
| 169 unsigned short key_length_bits) { |
| 170 return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesGcm, |
| 171 key_length_bits); |
| 172 } |
| 173 |
| 174 } // namespace content |
OLD | NEW |