[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" |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 9 | #include "components/webcrypto/algorithm_implementations.h" |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 10 | #include "components/webcrypto/status.h" |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 11 | #include "crypto/openssl_util.h" |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 12 | |
| 13 | namespace webcrypto { |
| 14 | |
| 15 | namespace { |
| 16 | |
eroman | cd142598 | 2014-08-27 18:52:34 | [diff] [blame] | 17 | // This class is used as a singleton. All methods must be threadsafe. |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 18 | class AlgorithmRegistry { |
| 19 | public: |
| 20 | AlgorithmRegistry() |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 21 | : sha_(CreateShaImplementation()), |
| 22 | aes_gcm_(CreateAesGcmImplementation()), |
| 23 | aes_cbc_(CreateAesCbcImplementation()), |
| 24 | aes_ctr_(CreateAesCtrImplementation()), |
| 25 | aes_kw_(CreateAesKwImplementation()), |
| 26 | hmac_(CreateHmacImplementation()), |
| 27 | rsa_ssa_(CreateRsaSsaImplementation()), |
| 28 | rsa_oaep_(CreateRsaOaepImplementation()), |
| 29 | rsa_pss_(CreateRsaPssImplementation()), |
| 30 | ecdsa_(CreateEcdsaImplementation()), |
| 31 | ecdh_(CreateEcdhImplementation()), |
| 32 | hkdf_(CreateHkdfImplementation()), |
| 33 | pbkdf2_(CreatePbkdf2Implementation()) { |
| 34 | crypto::EnsureOpenSSLInit(); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | const AlgorithmImplementation* GetAlgorithm( |
| 38 | blink::WebCryptoAlgorithmId id) const { |
| 39 | switch (id) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 40 | case blink::kWebCryptoAlgorithmIdSha1: |
| 41 | case blink::kWebCryptoAlgorithmIdSha256: |
| 42 | case blink::kWebCryptoAlgorithmIdSha384: |
| 43 | case blink::kWebCryptoAlgorithmIdSha512: |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 44 | return sha_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 45 | case blink::kWebCryptoAlgorithmIdAesGcm: |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 46 | return aes_gcm_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 47 | case blink::kWebCryptoAlgorithmIdAesCbc: |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 48 | return aes_cbc_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 49 | case blink::kWebCryptoAlgorithmIdAesCtr: |
eroman | 4d7a0e0 | 2014-08-27 00:30:33 | [diff] [blame] | 50 | return aes_ctr_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 51 | case blink::kWebCryptoAlgorithmIdAesKw: |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 52 | return aes_kw_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 53 | case blink::kWebCryptoAlgorithmIdHmac: |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 54 | return hmac_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 55 | case blink::kWebCryptoAlgorithmIdRsaSsaPkcs1v1_5: |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 56 | return rsa_ssa_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 57 | case blink::kWebCryptoAlgorithmIdRsaOaep: |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 58 | return rsa_oaep_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 59 | case blink::kWebCryptoAlgorithmIdRsaPss: |
eroman | 8793ece | 2014-10-20 20:47:15 | [diff] [blame] | 60 | return rsa_pss_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 61 | case blink::kWebCryptoAlgorithmIdEcdsa: |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 62 | return ecdsa_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 63 | case blink::kWebCryptoAlgorithmIdEcdh: |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 64 | return ecdh_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 65 | case blink::kWebCryptoAlgorithmIdHkdf: |
nharper | 65103179 | 2015-01-13 18:10:39 | [diff] [blame] | 66 | return hkdf_.get(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 67 | case blink::kWebCryptoAlgorithmIdPbkdf2: |
xun.sun | 22a80e7 | 2015-01-21 13:57:19 | [diff] [blame] | 68 | return pbkdf2_.get(); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 69 | default: |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 70 | return nullptr; |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | private: |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 75 | const std::unique_ptr<AlgorithmImplementation> sha_; |
| 76 | const std::unique_ptr<AlgorithmImplementation> aes_gcm_; |
| 77 | const std::unique_ptr<AlgorithmImplementation> aes_cbc_; |
| 78 | const std::unique_ptr<AlgorithmImplementation> aes_ctr_; |
| 79 | const std::unique_ptr<AlgorithmImplementation> aes_kw_; |
| 80 | const std::unique_ptr<AlgorithmImplementation> hmac_; |
| 81 | const std::unique_ptr<AlgorithmImplementation> rsa_ssa_; |
| 82 | const std::unique_ptr<AlgorithmImplementation> rsa_oaep_; |
| 83 | const std::unique_ptr<AlgorithmImplementation> rsa_pss_; |
| 84 | const std::unique_ptr<AlgorithmImplementation> ecdsa_; |
| 85 | const std::unique_ptr<AlgorithmImplementation> ecdh_; |
| 86 | const std::unique_ptr<AlgorithmImplementation> hkdf_; |
| 87 | const std::unique_ptr<AlgorithmImplementation> pbkdf2_; |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry = |
| 93 | LAZY_INSTANCE_INITIALIZER; |
| 94 | |
| 95 | Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id, |
| 96 | const AlgorithmImplementation** impl) { |
| 97 | *impl = g_algorithm_registry.Get().GetAlgorithm(id); |
| 98 | if (*impl) |
| 99 | return Status::Success(); |
| 100 | return Status::ErrorUnsupported(); |
| 101 | } |
| 102 | |
| 103 | } // namespace webcrypto |