| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/webcrypto/webcrypto_util.h" | 5 #include "content/child/webcrypto/webcrypto_util.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "content/child/webcrypto/status.h" | 10 #include "content/child/webcrypto/status.h" |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( | 168 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( |
| 169 blink::WebCryptoAlgorithmId id, | 169 blink::WebCryptoAlgorithmId id, |
| 170 blink::WebCryptoAlgorithmId hash_id) { | 170 blink::WebCryptoAlgorithmId hash_id) { |
| 171 DCHECK(IsHashAlgorithm(hash_id)); | 171 DCHECK(IsHashAlgorithm(hash_id)); |
| 172 DCHECK(id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 172 DCHECK(id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 173 id == blink::WebCryptoAlgorithmIdRsaOaep); | 173 id == blink::WebCryptoAlgorithmIdRsaOaep); |
| 174 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 174 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 175 id, new blink::WebCryptoRsaHashedImportParams(CreateAlgorithm(hash_id))); | 175 id, new blink::WebCryptoRsaHashedImportParams(CreateAlgorithm(hash_id))); |
| 176 } | 176 } |
| 177 | 177 |
| 178 blink::WebCryptoAlgorithm CreateRsaSsaImportAlgorithm( | |
| 179 blink::WebCryptoAlgorithmId hash_id) { | |
| 180 return CreateRsaHashedImportAlgorithm( | |
| 181 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, hash_id); | |
| 182 } | |
| 183 | |
| 184 blink::WebCryptoAlgorithm CreateRsaOaepImportAlgorithm( | |
| 185 blink::WebCryptoAlgorithmId hash_id) { | |
| 186 return CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep, | |
| 187 hash_id); | |
| 188 } | |
| 189 | |
| 190 unsigned int ShaBlockSizeBytes(blink::WebCryptoAlgorithmId hash_id) { | |
| 191 switch (hash_id) { | |
| 192 case blink::WebCryptoAlgorithmIdSha1: | |
| 193 case blink::WebCryptoAlgorithmIdSha256: | |
| 194 return 64; | |
| 195 case blink::WebCryptoAlgorithmIdSha384: | |
| 196 case blink::WebCryptoAlgorithmIdSha512: | |
| 197 return 128; | |
| 198 default: | |
| 199 NOTREACHED(); | |
| 200 return 0; | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 bool CreateSecretKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm, | 178 bool CreateSecretKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm, |
| 205 unsigned int keylen_bytes, | 179 unsigned int keylen_bytes, |
| 206 blink::WebCryptoKeyAlgorithm* key_algorithm) { | 180 blink::WebCryptoKeyAlgorithm* key_algorithm) { |
| 207 switch (algorithm.id()) { | 181 switch (algorithm.id()) { |
| 208 case blink::WebCryptoAlgorithmIdHmac: { | 182 case blink::WebCryptoAlgorithmIdHmac: { |
| 209 blink::WebCryptoAlgorithm hash = GetInnerHashAlgorithm(algorithm); | 183 blink::WebCryptoAlgorithm hash = GetInnerHashAlgorithm(algorithm); |
| 210 if (hash.isNull()) | 184 if (hash.isNull()) |
| 211 return false; | 185 return false; |
| 212 if (keylen_bytes > UINT_MAX / 8) | 186 if (keylen_bytes > UINT_MAX / 8) |
| 213 return false; | 187 return false; |
| 214 *key_algorithm = | 188 *key_algorithm = |
| 215 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bytes * 8); | 189 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bytes * 8); |
| 216 return true; | 190 return true; |
| 217 } | 191 } |
| 218 case blink::WebCryptoAlgorithmIdAesKw: | 192 case blink::WebCryptoAlgorithmIdAesKw: |
| 219 case blink::WebCryptoAlgorithmIdAesCbc: | 193 case blink::WebCryptoAlgorithmIdAesCbc: |
| 220 case blink::WebCryptoAlgorithmIdAesCtr: | 194 case blink::WebCryptoAlgorithmIdAesCtr: |
| 221 case blink::WebCryptoAlgorithmIdAesGcm: | 195 case blink::WebCryptoAlgorithmIdAesGcm: |
| 222 *key_algorithm = blink::WebCryptoKeyAlgorithm::createAes( | 196 *key_algorithm = blink::WebCryptoKeyAlgorithm::createAes( |
| 223 algorithm.id(), keylen_bytes * 8); | 197 algorithm.id(), keylen_bytes * 8); |
| 224 return true; | 198 return true; |
| 225 default: | 199 default: |
| 226 return false; | 200 return false; |
| 227 } | 201 } |
| 228 } | 202 } |
| 229 | 203 |
| 230 } // namespace webcrypto | 204 } // namespace webcrypto |
| 231 | 205 |
| 232 } // namespace content | 206 } // namespace content |
| OLD | NEW |