blob: 3ca6d3e773a1d4f055aa2500a5c0a39e5177b482 [file] [log] [blame]
[email protected]38409aec2014-07-19 00:54:511// 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
erg56f12322015-04-17 00:51:485#include "components/webcrypto/algorithm_registry.h"
[email protected]38409aec2014-07-19 00:54:516
7#include "base/lazy_instance.h"
erg56f12322015-04-17 00:51:488#include "components/webcrypto/algorithm_implementation.h"
eromand62cb472015-09-18 18:24:239#include "components/webcrypto/algorithm_implementations.h"
erg56f12322015-04-17 00:51:4810#include "components/webcrypto/status.h"
eromand62cb472015-09-18 18:24:2311#include "crypto/openssl_util.h"
[email protected]38409aec2014-07-19 00:54:5112
13namespace webcrypto {
14
15namespace {
16
eromancd1425982014-08-27 18:52:3417// This class is used as a singleton. All methods must be threadsafe.
[email protected]38409aec2014-07-19 00:54:5118class AlgorithmRegistry {
19 public:
20 AlgorithmRegistry()
eromand62cb472015-09-18 18:24:2321 : 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]38409aec2014-07-19 00:54:5135 }
36
37 const AlgorithmImplementation* GetAlgorithm(
38 blink::WebCryptoAlgorithmId id) const {
39 switch (id) {
Blink Reformat1c4d759e2017-04-09 16:34:5440 case blink::kWebCryptoAlgorithmIdSha1:
41 case blink::kWebCryptoAlgorithmIdSha256:
42 case blink::kWebCryptoAlgorithmIdSha384:
43 case blink::kWebCryptoAlgorithmIdSha512:
[email protected]38409aec2014-07-19 00:54:5144 return sha_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5445 case blink::kWebCryptoAlgorithmIdAesGcm:
[email protected]38409aec2014-07-19 00:54:5146 return aes_gcm_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5447 case blink::kWebCryptoAlgorithmIdAesCbc:
[email protected]38409aec2014-07-19 00:54:5148 return aes_cbc_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5449 case blink::kWebCryptoAlgorithmIdAesCtr:
eroman4d7a0e02014-08-27 00:30:3350 return aes_ctr_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5451 case blink::kWebCryptoAlgorithmIdAesKw:
[email protected]38409aec2014-07-19 00:54:5152 return aes_kw_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5453 case blink::kWebCryptoAlgorithmIdHmac:
[email protected]38409aec2014-07-19 00:54:5154 return hmac_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5455 case blink::kWebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
[email protected]38409aec2014-07-19 00:54:5156 return rsa_ssa_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5457 case blink::kWebCryptoAlgorithmIdRsaOaep:
[email protected]38409aec2014-07-19 00:54:5158 return rsa_oaep_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5459 case blink::kWebCryptoAlgorithmIdRsaPss:
eroman8793ece2014-10-20 20:47:1560 return rsa_pss_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5461 case blink::kWebCryptoAlgorithmIdEcdsa:
eromanb2ead6d2014-11-14 02:26:1462 return ecdsa_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5463 case blink::kWebCryptoAlgorithmIdEcdh:
eromaned48e812014-11-28 19:59:1364 return ecdh_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5465 case blink::kWebCryptoAlgorithmIdHkdf:
nharper651031792015-01-13 18:10:3966 return hkdf_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5467 case blink::kWebCryptoAlgorithmIdPbkdf2:
xun.sun22a80e72015-01-21 13:57:1968 return pbkdf2_.get();
[email protected]38409aec2014-07-19 00:54:5169 default:
Ivan Kotenkov75b1c3a2017-10-24 14:47:2470 return nullptr;
[email protected]38409aec2014-07-19 00:54:5171 }
72 }
73
74 private:
dcheng7036d1e52016-04-21 23:13:0375 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]38409aec2014-07-19 00:54:5188};
89
90} // namespace
91
92base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
93 LAZY_INSTANCE_INITIALIZER;
94
95Status 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