[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame^] | 5 | #include "components/webcrypto/algorithm_registry.h" |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 6 | |
| 7 | #include "base/lazy_instance.h" |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame^] | 8 | #include "components/webcrypto/algorithm_implementation.h" |
| 9 | #include "components/webcrypto/platform_crypto.h" |
| 10 | #include "components/webcrypto/status.h" |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 11 | |
| 12 | namespace webcrypto { |
| 13 | |
| 14 | namespace { |
| 15 | |
eroman | cd142598 | 2014-08-27 18:52:34 | [diff] [blame] | 16 | // This class is used as a singleton. All methods must be threadsafe. |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 17 | class AlgorithmRegistry { |
| 18 | public: |
| 19 | AlgorithmRegistry() |
| 20 | : sha_(CreatePlatformShaImplementation()), |
| 21 | aes_gcm_(CreatePlatformAesGcmImplementation()), |
| 22 | aes_cbc_(CreatePlatformAesCbcImplementation()), |
eroman | 4d7a0e0 | 2014-08-27 00:30:33 | [diff] [blame] | 23 | aes_ctr_(CreatePlatformAesCtrImplementation()), |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 24 | aes_kw_(CreatePlatformAesKwImplementation()), |
| 25 | hmac_(CreatePlatformHmacImplementation()), |
| 26 | rsa_ssa_(CreatePlatformRsaSsaImplementation()), |
eroman | 8793ece | 2014-10-20 20:47:15 | [diff] [blame] | 27 | rsa_oaep_(CreatePlatformRsaOaepImplementation()), |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 28 | rsa_pss_(CreatePlatformRsaPssImplementation()), |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 29 | ecdsa_(CreatePlatformEcdsaImplementation()), |
nharper | 65103179 | 2015-01-13 18:10:39 | [diff] [blame] | 30 | ecdh_(CreatePlatformEcdhImplementation()), |
xun.sun | 22a80e7 | 2015-01-21 13:57:19 | [diff] [blame] | 31 | hkdf_(CreatePlatformHkdfImplementation()), |
| 32 | pbkdf2_(CreatePlatformPbkdf2Implementation()) { |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 33 | PlatformInit(); |
| 34 | } |
| 35 | |
| 36 | const AlgorithmImplementation* GetAlgorithm( |
| 37 | blink::WebCryptoAlgorithmId id) const { |
| 38 | switch (id) { |
| 39 | case blink::WebCryptoAlgorithmIdSha1: |
| 40 | case blink::WebCryptoAlgorithmIdSha256: |
| 41 | case blink::WebCryptoAlgorithmIdSha384: |
| 42 | case blink::WebCryptoAlgorithmIdSha512: |
| 43 | return sha_.get(); |
| 44 | case blink::WebCryptoAlgorithmIdAesGcm: |
| 45 | return aes_gcm_.get(); |
| 46 | case blink::WebCryptoAlgorithmIdAesCbc: |
| 47 | return aes_cbc_.get(); |
eroman | 4d7a0e0 | 2014-08-27 00:30:33 | [diff] [blame] | 48 | case blink::WebCryptoAlgorithmIdAesCtr: |
| 49 | return aes_ctr_.get(); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 50 | case blink::WebCryptoAlgorithmIdAesKw: |
| 51 | return aes_kw_.get(); |
| 52 | case blink::WebCryptoAlgorithmIdHmac: |
| 53 | return hmac_.get(); |
| 54 | case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: |
| 55 | return rsa_ssa_.get(); |
| 56 | case blink::WebCryptoAlgorithmIdRsaOaep: |
| 57 | return rsa_oaep_.get(); |
eroman | 8793ece | 2014-10-20 20:47:15 | [diff] [blame] | 58 | case blink::WebCryptoAlgorithmIdRsaPss: |
| 59 | return rsa_pss_.get(); |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 60 | case blink::WebCryptoAlgorithmIdEcdsa: |
| 61 | return ecdsa_.get(); |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 62 | case blink::WebCryptoAlgorithmIdEcdh: |
| 63 | return ecdh_.get(); |
nharper | 65103179 | 2015-01-13 18:10:39 | [diff] [blame] | 64 | case blink::WebCryptoAlgorithmIdHkdf: |
| 65 | return hkdf_.get(); |
xun.sun | 22a80e7 | 2015-01-21 13:57:19 | [diff] [blame] | 66 | case blink::WebCryptoAlgorithmIdPbkdf2: |
| 67 | return pbkdf2_.get(); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 68 | default: |
| 69 | return NULL; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private: |
eroman | cd142598 | 2014-08-27 18:52:34 | [diff] [blame] | 74 | const scoped_ptr<AlgorithmImplementation> sha_; |
| 75 | const scoped_ptr<AlgorithmImplementation> aes_gcm_; |
| 76 | const scoped_ptr<AlgorithmImplementation> aes_cbc_; |
| 77 | const scoped_ptr<AlgorithmImplementation> aes_ctr_; |
| 78 | const scoped_ptr<AlgorithmImplementation> aes_kw_; |
| 79 | const scoped_ptr<AlgorithmImplementation> hmac_; |
| 80 | const scoped_ptr<AlgorithmImplementation> rsa_ssa_; |
| 81 | const scoped_ptr<AlgorithmImplementation> rsa_oaep_; |
eroman | 8793ece | 2014-10-20 20:47:15 | [diff] [blame] | 82 | const scoped_ptr<AlgorithmImplementation> rsa_pss_; |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 83 | const scoped_ptr<AlgorithmImplementation> ecdsa_; |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 84 | const scoped_ptr<AlgorithmImplementation> ecdh_; |
nharper | 65103179 | 2015-01-13 18:10:39 | [diff] [blame] | 85 | const scoped_ptr<AlgorithmImplementation> hkdf_; |
xun.sun | 22a80e7 | 2015-01-21 13:57:19 | [diff] [blame] | 86 | const scoped_ptr<AlgorithmImplementation> pbkdf2_; |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace |
| 90 | |
| 91 | base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry = |
| 92 | LAZY_INSTANCE_INITIALIZER; |
| 93 | |
| 94 | Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id, |
| 95 | const AlgorithmImplementation** impl) { |
| 96 | *impl = g_algorithm_registry.Get().GetAlgorithm(id); |
| 97 | if (*impl) |
| 98 | return Status::Success(); |
| 99 | return Status::ErrorUnsupported(); |
| 100 | } |
| 101 | |
| 102 | } // namespace webcrypto |