Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / components / webcrypto / algorithm_registry.cc
blobb82737e732b29a930111e6e07f86282e87577f0e
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.
5 #include "components/webcrypto/algorithm_registry.h"
7 #include "base/lazy_instance.h"
8 #include "components/webcrypto/algorithm_implementation.h"
9 #include "components/webcrypto/algorithm_implementations.h"
10 #include "components/webcrypto/status.h"
11 #include "crypto/openssl_util.h"
13 namespace webcrypto {
15 namespace {
17 // This class is used as a singleton. All methods must be threadsafe.
18 class AlgorithmRegistry {
19 public:
20 AlgorithmRegistry()
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();
37 const AlgorithmImplementation* GetAlgorithm(
38 blink::WebCryptoAlgorithmId id) const {
39 switch (id) {
40 case blink::WebCryptoAlgorithmIdSha1:
41 case blink::WebCryptoAlgorithmIdSha256:
42 case blink::WebCryptoAlgorithmIdSha384:
43 case blink::WebCryptoAlgorithmIdSha512:
44 return sha_.get();
45 case blink::WebCryptoAlgorithmIdAesGcm:
46 return aes_gcm_.get();
47 case blink::WebCryptoAlgorithmIdAesCbc:
48 return aes_cbc_.get();
49 case blink::WebCryptoAlgorithmIdAesCtr:
50 return aes_ctr_.get();
51 case blink::WebCryptoAlgorithmIdAesKw:
52 return aes_kw_.get();
53 case blink::WebCryptoAlgorithmIdHmac:
54 return hmac_.get();
55 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
56 return rsa_ssa_.get();
57 case blink::WebCryptoAlgorithmIdRsaOaep:
58 return rsa_oaep_.get();
59 case blink::WebCryptoAlgorithmIdRsaPss:
60 return rsa_pss_.get();
61 case blink::WebCryptoAlgorithmIdEcdsa:
62 return ecdsa_.get();
63 case blink::WebCryptoAlgorithmIdEcdh:
64 return ecdh_.get();
65 case blink::WebCryptoAlgorithmIdHkdf:
66 return hkdf_.get();
67 case blink::WebCryptoAlgorithmIdPbkdf2:
68 return pbkdf2_.get();
69 default:
70 return NULL;
74 private:
75 const scoped_ptr<AlgorithmImplementation> sha_;
76 const scoped_ptr<AlgorithmImplementation> aes_gcm_;
77 const scoped_ptr<AlgorithmImplementation> aes_cbc_;
78 const scoped_ptr<AlgorithmImplementation> aes_ctr_;
79 const scoped_ptr<AlgorithmImplementation> aes_kw_;
80 const scoped_ptr<AlgorithmImplementation> hmac_;
81 const scoped_ptr<AlgorithmImplementation> rsa_ssa_;
82 const scoped_ptr<AlgorithmImplementation> rsa_oaep_;
83 const scoped_ptr<AlgorithmImplementation> rsa_pss_;
84 const scoped_ptr<AlgorithmImplementation> ecdsa_;
85 const scoped_ptr<AlgorithmImplementation> ecdh_;
86 const scoped_ptr<AlgorithmImplementation> hkdf_;
87 const scoped_ptr<AlgorithmImplementation> pbkdf2_;
90 } // namespace
92 base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
93 LAZY_INSTANCE_INITIALIZER;
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();
103 } // namespace webcrypto