blob: 0095214c46be14b5e0b42a0bd821ef383c87e8af [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()),
Qingsi Wang14d7f0e2020-02-28 02:10:3033 pbkdf2_(CreatePbkdf2Implementation()),
34 ed25519_(CreateEd25519Implementation()),
35 x25519_(CreateX25519Implementation()) {
eromand62cb472015-09-18 18:24:2336 crypto::EnsureOpenSSLInit();
[email protected]38409aec2014-07-19 00:54:5137 }
38
39 const AlgorithmImplementation* GetAlgorithm(
40 blink::WebCryptoAlgorithmId id) const {
41 switch (id) {
Blink Reformat1c4d759e2017-04-09 16:34:5442 case blink::kWebCryptoAlgorithmIdSha1:
43 case blink::kWebCryptoAlgorithmIdSha256:
44 case blink::kWebCryptoAlgorithmIdSha384:
45 case blink::kWebCryptoAlgorithmIdSha512:
[email protected]38409aec2014-07-19 00:54:5146 return sha_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5447 case blink::kWebCryptoAlgorithmIdAesGcm:
[email protected]38409aec2014-07-19 00:54:5148 return aes_gcm_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5449 case blink::kWebCryptoAlgorithmIdAesCbc:
[email protected]38409aec2014-07-19 00:54:5150 return aes_cbc_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5451 case blink::kWebCryptoAlgorithmIdAesCtr:
eroman4d7a0e02014-08-27 00:30:3352 return aes_ctr_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5453 case blink::kWebCryptoAlgorithmIdAesKw:
[email protected]38409aec2014-07-19 00:54:5154 return aes_kw_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5455 case blink::kWebCryptoAlgorithmIdHmac:
[email protected]38409aec2014-07-19 00:54:5156 return hmac_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5457 case blink::kWebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
[email protected]38409aec2014-07-19 00:54:5158 return rsa_ssa_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5459 case blink::kWebCryptoAlgorithmIdRsaOaep:
[email protected]38409aec2014-07-19 00:54:5160 return rsa_oaep_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5461 case blink::kWebCryptoAlgorithmIdRsaPss:
eroman8793ece2014-10-20 20:47:1562 return rsa_pss_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5463 case blink::kWebCryptoAlgorithmIdEcdsa:
eromanb2ead6d2014-11-14 02:26:1464 return ecdsa_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5465 case blink::kWebCryptoAlgorithmIdEcdh:
eromaned48e812014-11-28 19:59:1366 return ecdh_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5467 case blink::kWebCryptoAlgorithmIdHkdf:
nharper651031792015-01-13 18:10:3968 return hkdf_.get();
Blink Reformat1c4d759e2017-04-09 16:34:5469 case blink::kWebCryptoAlgorithmIdPbkdf2:
xun.sun22a80e72015-01-21 13:57:1970 return pbkdf2_.get();
Qingsi Wang14d7f0e2020-02-28 02:10:3071 case blink::kWebCryptoAlgorithmIdEd25519:
72 return ed25519_.get();
73 case blink::kWebCryptoAlgorithmIdX25519:
74 return x25519_.get();
[email protected]38409aec2014-07-19 00:54:5175 default:
Ivan Kotenkov75b1c3a2017-10-24 14:47:2476 return nullptr;
[email protected]38409aec2014-07-19 00:54:5177 }
78 }
79
80 private:
dcheng7036d1e52016-04-21 23:13:0381 const std::unique_ptr<AlgorithmImplementation> sha_;
82 const std::unique_ptr<AlgorithmImplementation> aes_gcm_;
83 const std::unique_ptr<AlgorithmImplementation> aes_cbc_;
84 const std::unique_ptr<AlgorithmImplementation> aes_ctr_;
85 const std::unique_ptr<AlgorithmImplementation> aes_kw_;
86 const std::unique_ptr<AlgorithmImplementation> hmac_;
87 const std::unique_ptr<AlgorithmImplementation> rsa_ssa_;
88 const std::unique_ptr<AlgorithmImplementation> rsa_oaep_;
89 const std::unique_ptr<AlgorithmImplementation> rsa_pss_;
90 const std::unique_ptr<AlgorithmImplementation> ecdsa_;
91 const std::unique_ptr<AlgorithmImplementation> ecdh_;
92 const std::unique_ptr<AlgorithmImplementation> hkdf_;
93 const std::unique_ptr<AlgorithmImplementation> pbkdf2_;
Qingsi Wang14d7f0e2020-02-28 02:10:3094 const std::unique_ptr<AlgorithmImplementation> ed25519_;
95 const std::unique_ptr<AlgorithmImplementation> x25519_;
[email protected]38409aec2014-07-19 00:54:5196};
97
98} // namespace
99
100base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
101 LAZY_INSTANCE_INITIALIZER;
102
103Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id,
104 const AlgorithmImplementation** impl) {
105 *impl = g_algorithm_registry.Get().GetAlgorithm(id);
106 if (*impl)
107 return Status::Success();
108 return Status::ErrorUnsupported();
109}
110
111} // namespace webcrypto