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"
18 // This class is used as a singleton. All methods must be threadsafe.
19 class 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()) {
38 const AlgorithmImplementation
* GetAlgorithm(
39 blink::WebCryptoAlgorithmId id
) const {
41 case blink::WebCryptoAlgorithmIdSha1
:
42 case blink::WebCryptoAlgorithmIdSha256
:
43 case blink::WebCryptoAlgorithmIdSha384
:
44 case blink::WebCryptoAlgorithmIdSha512
:
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
:
54 case blink::WebCryptoAlgorithmIdHmac
:
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
:
64 case blink::WebCryptoAlgorithmIdEcdh
:
66 case blink::WebCryptoAlgorithmIdHkdf
:
68 case blink::WebCryptoAlgorithmIdPbkdf2
:
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_
;
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
);
100 return Status::Success();
101 return Status::ErrorUnsupported();
104 } // namespace webcrypto
106 } // namespace content