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"
16 // This class is used as a singleton. All methods must be threadsafe.
17 class 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()) {
36 const AlgorithmImplementation
* GetAlgorithm(
37 blink::WebCryptoAlgorithmId id
) const {
39 case blink::WebCryptoAlgorithmIdSha1
:
40 case blink::WebCryptoAlgorithmIdSha256
:
41 case blink::WebCryptoAlgorithmIdSha384
:
42 case blink::WebCryptoAlgorithmIdSha512
:
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
:
52 case blink::WebCryptoAlgorithmIdHmac
:
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
:
62 case blink::WebCryptoAlgorithmIdEcdh
:
64 case blink::WebCryptoAlgorithmIdHkdf
:
66 case blink::WebCryptoAlgorithmIdPbkdf2
:
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_
;
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
);
98 return Status::Success();
99 return Status::ErrorUnsupported();
102 } // namespace webcrypto