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"
17 // This class is used as a singleton. All methods must be threadsafe.
18 class 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 {
40 case blink::WebCryptoAlgorithmIdSha1
:
41 case blink::WebCryptoAlgorithmIdSha256
:
42 case blink::WebCryptoAlgorithmIdSha384
:
43 case blink::WebCryptoAlgorithmIdSha512
:
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
:
53 case blink::WebCryptoAlgorithmIdHmac
:
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
:
63 case blink::WebCryptoAlgorithmIdEcdh
:
65 case blink::WebCryptoAlgorithmIdHkdf
:
67 case blink::WebCryptoAlgorithmIdPbkdf2
:
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_
;
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
);
99 return Status::Success();
100 return Status::ErrorUnsupported();
103 } // namespace webcrypto