Clarify and update GN build flags docs.
[chromium-blink-merge.git] / components / webcrypto / algorithm_registry.cc
blobea4799e70e9b5c1d939c379ef287040143a85058
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/platform_crypto.h"
10 #include "components/webcrypto/status.h"
12 namespace webcrypto {
14 namespace {
16 // This class is used as a singleton. All methods must be threadsafe.
17 class AlgorithmRegistry {
18 public:
19 AlgorithmRegistry()
20 : sha_(CreatePlatformShaImplementation()),
21 aes_gcm_(CreatePlatformAesGcmImplementation()),
22 aes_cbc_(CreatePlatformAesCbcImplementation()),
23 aes_ctr_(CreatePlatformAesCtrImplementation()),
24 aes_kw_(CreatePlatformAesKwImplementation()),
25 hmac_(CreatePlatformHmacImplementation()),
26 rsa_ssa_(CreatePlatformRsaSsaImplementation()),
27 rsa_oaep_(CreatePlatformRsaOaepImplementation()),
28 rsa_pss_(CreatePlatformRsaPssImplementation()),
29 ecdsa_(CreatePlatformEcdsaImplementation()),
30 ecdh_(CreatePlatformEcdhImplementation()),
31 hkdf_(CreatePlatformHkdfImplementation()),
32 pbkdf2_(CreatePlatformPbkdf2Implementation()) {
33 PlatformInit();
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();
48 case blink::WebCryptoAlgorithmIdAesCtr:
49 return aes_ctr_.get();
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();
58 case blink::WebCryptoAlgorithmIdRsaPss:
59 return rsa_pss_.get();
60 case blink::WebCryptoAlgorithmIdEcdsa:
61 return ecdsa_.get();
62 case blink::WebCryptoAlgorithmIdEcdh:
63 return ecdh_.get();
64 case blink::WebCryptoAlgorithmIdHkdf:
65 return hkdf_.get();
66 case blink::WebCryptoAlgorithmIdPbkdf2:
67 return pbkdf2_.get();
68 default:
69 return NULL;
73 private:
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_;
82 const scoped_ptr<AlgorithmImplementation> rsa_pss_;
83 const scoped_ptr<AlgorithmImplementation> ecdsa_;
84 const scoped_ptr<AlgorithmImplementation> ecdh_;
85 const scoped_ptr<AlgorithmImplementation> hkdf_;
86 const scoped_ptr<AlgorithmImplementation> pbkdf2_;
89 } // namespace
91 base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
92 LAZY_INSTANCE_INITIALIZER;
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();
102 } // namespace webcrypto