blob: b82737e732b29a930111e6e07f86282e87577f0e [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) {
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();
eroman4d7a0e02014-08-27 00:30:3349 case blink::WebCryptoAlgorithmIdAesCtr:
50 return aes_ctr_.get();
[email protected]38409aec2014-07-19 00:54:5151 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();
eroman8793ece2014-10-20 20:47:1559 case blink::WebCryptoAlgorithmIdRsaPss:
60 return rsa_pss_.get();
eromanb2ead6d2014-11-14 02:26:1461 case blink::WebCryptoAlgorithmIdEcdsa:
62 return ecdsa_.get();
eromaned48e812014-11-28 19:59:1363 case blink::WebCryptoAlgorithmIdEcdh:
64 return ecdh_.get();
nharper651031792015-01-13 18:10:3965 case blink::WebCryptoAlgorithmIdHkdf:
66 return hkdf_.get();
xun.sun22a80e72015-01-21 13:57:1967 case blink::WebCryptoAlgorithmIdPbkdf2:
68 return pbkdf2_.get();
[email protected]38409aec2014-07-19 00:54:5169 default:
70 return NULL;
71 }
72 }
73
74 private:
eromancd1425982014-08-27 18:52:3475 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_;
eroman8793ece2014-10-20 20:47:1583 const scoped_ptr<AlgorithmImplementation> rsa_pss_;
eromanb2ead6d2014-11-14 02:26:1484 const scoped_ptr<AlgorithmImplementation> ecdsa_;
eromaned48e812014-11-28 19:59:1385 const scoped_ptr<AlgorithmImplementation> ecdh_;
nharper651031792015-01-13 18:10:3986 const scoped_ptr<AlgorithmImplementation> hkdf_;
xun.sun22a80e72015-01-21 13:57:1987 const scoped_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