Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / content / child / webcrypto / algorithm_registry.cc
blob5828a0c8d9bdce0e44bdee97f2535db27693c429
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 "content/child/webcrypto/algorithm_registry.h"
7 #include "base/lazy_instance.h"
8 #include "content/child/webcrypto/algorithm_implementation.h"
9 #include "content/child/webcrypto/platform_crypto.h"
10 #include "content/child/webcrypto/status.h"
12 namespace content {
14 namespace webcrypto {
16 namespace {
18 // This class is used as a singleton. All methods must be threadsafe.
19 class AlgorithmRegistry {
20 public:
21 AlgorithmRegistry()
22 : sha_(CreatePlatformShaImplementation()),
23 aes_gcm_(CreatePlatformAesGcmImplementation()),
24 aes_cbc_(CreatePlatformAesCbcImplementation()),
25 aes_ctr_(CreatePlatformAesCtrImplementation()),
26 aes_kw_(CreatePlatformAesKwImplementation()),
27 hmac_(CreatePlatformHmacImplementation()),
28 rsa_ssa_(CreatePlatformRsaSsaImplementation()),
29 rsa_oaep_(CreatePlatformRsaOaepImplementation()),
30 rsa_pss_(CreatePlatformRsaPssImplementation()),
31 ecdsa_(CreatePlatformEcdsaImplementation()),
32 ecdh_(CreatePlatformEcdhImplementation()),
33 hkdf_(CreatePlatformHkdfImplementation()),
34 pbkdf2_(CreatePlatformPbkdf2Implementation()) {
35 PlatformInit();
38 const AlgorithmImplementation* GetAlgorithm(
39 blink::WebCryptoAlgorithmId id) const {
40 switch (id) {
41 case blink::WebCryptoAlgorithmIdSha1:
42 case blink::WebCryptoAlgorithmIdSha256:
43 case blink::WebCryptoAlgorithmIdSha384:
44 case blink::WebCryptoAlgorithmIdSha512:
45 return sha_.get();
46 case blink::WebCryptoAlgorithmIdAesGcm:
47 return aes_gcm_.get();
48 case blink::WebCryptoAlgorithmIdAesCbc:
49 return aes_cbc_.get();
50 case blink::WebCryptoAlgorithmIdAesCtr:
51 return aes_ctr_.get();
52 case blink::WebCryptoAlgorithmIdAesKw:
53 return aes_kw_.get();
54 case blink::WebCryptoAlgorithmIdHmac:
55 return hmac_.get();
56 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
57 return rsa_ssa_.get();
58 case blink::WebCryptoAlgorithmIdRsaOaep:
59 return rsa_oaep_.get();
60 case blink::WebCryptoAlgorithmIdRsaPss:
61 return rsa_pss_.get();
62 case blink::WebCryptoAlgorithmIdEcdsa:
63 return ecdsa_.get();
64 case blink::WebCryptoAlgorithmIdEcdh:
65 return ecdh_.get();
66 case blink::WebCryptoAlgorithmIdHkdf:
67 return hkdf_.get();
68 case blink::WebCryptoAlgorithmIdPbkdf2:
69 return pbkdf2_.get();
70 default:
71 return NULL;
75 private:
76 const scoped_ptr<AlgorithmImplementation> sha_;
77 const scoped_ptr<AlgorithmImplementation> aes_gcm_;
78 const scoped_ptr<AlgorithmImplementation> aes_cbc_;
79 const scoped_ptr<AlgorithmImplementation> aes_ctr_;
80 const scoped_ptr<AlgorithmImplementation> aes_kw_;
81 const scoped_ptr<AlgorithmImplementation> hmac_;
82 const scoped_ptr<AlgorithmImplementation> rsa_ssa_;
83 const scoped_ptr<AlgorithmImplementation> rsa_oaep_;
84 const scoped_ptr<AlgorithmImplementation> rsa_pss_;
85 const scoped_ptr<AlgorithmImplementation> ecdsa_;
86 const scoped_ptr<AlgorithmImplementation> ecdh_;
87 const scoped_ptr<AlgorithmImplementation> hkdf_;
88 const scoped_ptr<AlgorithmImplementation> pbkdf2_;
91 } // namespace
93 base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
94 LAZY_INSTANCE_INITIALIZER;
96 Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id,
97 const AlgorithmImplementation** impl) {
98 *impl = g_algorithm_registry.Get().GetAlgorithm(id);
99 if (*impl)
100 return Status::Success();
101 return Status::ErrorUnsupported();
104 } // namespace webcrypto
106 } // namespace content