| 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/algorithm_registry.h" | 5 #include "content/child/webcrypto/algorithm_registry.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "content/child/webcrypto/algorithm_implementation.h" | 8 #include "content/child/webcrypto/algorithm_implementation.h" |
| 9 #include "content/child/webcrypto/platform_crypto.h" | 9 #include "content/child/webcrypto/platform_crypto.h" |
| 10 #include "content/child/webcrypto/status.h" | 10 #include "content/child/webcrypto/status.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 namespace webcrypto { | 14 namespace webcrypto { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // This class is used as a singleton. All methods must be threadsafe. | 18 // This class is used as a singleton. All methods must be threadsafe. |
| 19 class AlgorithmRegistry { | 19 class AlgorithmRegistry { |
| 20 public: | 20 public: |
| 21 AlgorithmRegistry() | 21 AlgorithmRegistry() |
| 22 : sha_(CreatePlatformShaImplementation()), | 22 : sha_(CreatePlatformShaImplementation()), |
| 23 aes_gcm_(CreatePlatformAesGcmImplementation()), | 23 aes_gcm_(CreatePlatformAesGcmImplementation()), |
| 24 aes_cbc_(CreatePlatformAesCbcImplementation()), | 24 aes_cbc_(CreatePlatformAesCbcImplementation()), |
| 25 aes_ctr_(CreatePlatformAesCtrImplementation()), | 25 aes_ctr_(CreatePlatformAesCtrImplementation()), |
| 26 aes_kw_(CreatePlatformAesKwImplementation()), | 26 aes_kw_(CreatePlatformAesKwImplementation()), |
| 27 hmac_(CreatePlatformHmacImplementation()), | 27 hmac_(CreatePlatformHmacImplementation()), |
| 28 rsa_ssa_(CreatePlatformRsaSsaImplementation()), | 28 rsa_ssa_(CreatePlatformRsaSsaImplementation()), |
| 29 rsa_oaep_(CreatePlatformRsaOaepImplementation()), | 29 rsa_oaep_(CreatePlatformRsaOaepImplementation()), |
| 30 rsa_pss_(CreatePlatformRsaPssImplementation()) { | 30 rsa_pss_(CreatePlatformRsaPssImplementation()), |
| 31 ecdsa_(CreatePlatformEcdsaImplementation()) { |
| 31 PlatformInit(); | 32 PlatformInit(); |
| 32 } | 33 } |
| 33 | 34 |
| 34 const AlgorithmImplementation* GetAlgorithm( | 35 const AlgorithmImplementation* GetAlgorithm( |
| 35 blink::WebCryptoAlgorithmId id) const { | 36 blink::WebCryptoAlgorithmId id) const { |
| 36 switch (id) { | 37 switch (id) { |
| 37 case blink::WebCryptoAlgorithmIdSha1: | 38 case blink::WebCryptoAlgorithmIdSha1: |
| 38 case blink::WebCryptoAlgorithmIdSha256: | 39 case blink::WebCryptoAlgorithmIdSha256: |
| 39 case blink::WebCryptoAlgorithmIdSha384: | 40 case blink::WebCryptoAlgorithmIdSha384: |
| 40 case blink::WebCryptoAlgorithmIdSha512: | 41 case blink::WebCryptoAlgorithmIdSha512: |
| 41 return sha_.get(); | 42 return sha_.get(); |
| 42 case blink::WebCryptoAlgorithmIdAesGcm: | 43 case blink::WebCryptoAlgorithmIdAesGcm: |
| 43 return aes_gcm_.get(); | 44 return aes_gcm_.get(); |
| 44 case blink::WebCryptoAlgorithmIdAesCbc: | 45 case blink::WebCryptoAlgorithmIdAesCbc: |
| 45 return aes_cbc_.get(); | 46 return aes_cbc_.get(); |
| 46 case blink::WebCryptoAlgorithmIdAesCtr: | 47 case blink::WebCryptoAlgorithmIdAesCtr: |
| 47 return aes_ctr_.get(); | 48 return aes_ctr_.get(); |
| 48 case blink::WebCryptoAlgorithmIdAesKw: | 49 case blink::WebCryptoAlgorithmIdAesKw: |
| 49 return aes_kw_.get(); | 50 return aes_kw_.get(); |
| 50 case blink::WebCryptoAlgorithmIdHmac: | 51 case blink::WebCryptoAlgorithmIdHmac: |
| 51 return hmac_.get(); | 52 return hmac_.get(); |
| 52 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: | 53 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: |
| 53 return rsa_ssa_.get(); | 54 return rsa_ssa_.get(); |
| 54 case blink::WebCryptoAlgorithmIdRsaOaep: | 55 case blink::WebCryptoAlgorithmIdRsaOaep: |
| 55 return rsa_oaep_.get(); | 56 return rsa_oaep_.get(); |
| 56 case blink::WebCryptoAlgorithmIdRsaPss: | 57 case blink::WebCryptoAlgorithmIdRsaPss: |
| 57 return rsa_pss_.get(); | 58 return rsa_pss_.get(); |
| 59 case blink::WebCryptoAlgorithmIdEcdsa: |
| 60 return ecdsa_.get(); |
| 58 default: | 61 default: |
| 59 return NULL; | 62 return NULL; |
| 60 } | 63 } |
| 61 } | 64 } |
| 62 | 65 |
| 63 private: | 66 private: |
| 64 const scoped_ptr<AlgorithmImplementation> sha_; | 67 const scoped_ptr<AlgorithmImplementation> sha_; |
| 65 const scoped_ptr<AlgorithmImplementation> aes_gcm_; | 68 const scoped_ptr<AlgorithmImplementation> aes_gcm_; |
| 66 const scoped_ptr<AlgorithmImplementation> aes_cbc_; | 69 const scoped_ptr<AlgorithmImplementation> aes_cbc_; |
| 67 const scoped_ptr<AlgorithmImplementation> aes_ctr_; | 70 const scoped_ptr<AlgorithmImplementation> aes_ctr_; |
| 68 const scoped_ptr<AlgorithmImplementation> aes_kw_; | 71 const scoped_ptr<AlgorithmImplementation> aes_kw_; |
| 69 const scoped_ptr<AlgorithmImplementation> hmac_; | 72 const scoped_ptr<AlgorithmImplementation> hmac_; |
| 70 const scoped_ptr<AlgorithmImplementation> rsa_ssa_; | 73 const scoped_ptr<AlgorithmImplementation> rsa_ssa_; |
| 71 const scoped_ptr<AlgorithmImplementation> rsa_oaep_; | 74 const scoped_ptr<AlgorithmImplementation> rsa_oaep_; |
| 72 const scoped_ptr<AlgorithmImplementation> rsa_pss_; | 75 const scoped_ptr<AlgorithmImplementation> rsa_pss_; |
| 76 const scoped_ptr<AlgorithmImplementation> ecdsa_; |
| 73 }; | 77 }; |
| 74 | 78 |
| 75 } // namespace | 79 } // namespace |
| 76 | 80 |
| 77 base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry = | 81 base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry = |
| 78 LAZY_INSTANCE_INITIALIZER; | 82 LAZY_INSTANCE_INITIALIZER; |
| 79 | 83 |
| 80 Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id, | 84 Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id, |
| 81 const AlgorithmImplementation** impl) { | 85 const AlgorithmImplementation** impl) { |
| 82 *impl = g_algorithm_registry.Get().GetAlgorithm(id); | 86 *impl = g_algorithm_registry.Get().GetAlgorithm(id); |
| 83 if (*impl) | 87 if (*impl) |
| 84 return Status::Success(); | 88 return Status::Success(); |
| 85 return Status::ErrorUnsupported(); | 89 return Status::ErrorUnsupported(); |
| 86 } | 90 } |
| 87 | 91 |
| 88 } // namespace webcrypto | 92 } // namespace webcrypto |
| 89 | 93 |
| 90 } // namespace content | 94 } // namespace content |
| OLD | NEW |