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()) {
34 const AlgorithmImplementation
* GetAlgorithm(
35 blink::WebCryptoAlgorithmId id
) const {
37 case blink::WebCryptoAlgorithmIdSha1
:
38 case blink::WebCryptoAlgorithmIdSha256
:
39 case blink::WebCryptoAlgorithmIdSha384
:
40 case blink::WebCryptoAlgorithmIdSha512
:
42 case blink::WebCryptoAlgorithmIdAesGcm
:
43 return aes_gcm_
.get();
44 case blink::WebCryptoAlgorithmIdAesCbc
:
45 return aes_cbc_
.get();
46 case blink::WebCryptoAlgorithmIdAesCtr
:
47 return aes_ctr_
.get();
48 case blink::WebCryptoAlgorithmIdAesKw
:
50 case blink::WebCryptoAlgorithmIdHmac
:
52 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5
:
53 return rsa_ssa_
.get();
54 case blink::WebCryptoAlgorithmIdRsaOaep
:
55 return rsa_oaep_
.get();
56 case blink::WebCryptoAlgorithmIdRsaPss
:
57 return rsa_pss_
.get();
64 const scoped_ptr
<AlgorithmImplementation
> sha_
;
65 const scoped_ptr
<AlgorithmImplementation
> aes_gcm_
;
66 const scoped_ptr
<AlgorithmImplementation
> aes_cbc_
;
67 const scoped_ptr
<AlgorithmImplementation
> aes_ctr_
;
68 const scoped_ptr
<AlgorithmImplementation
> aes_kw_
;
69 const scoped_ptr
<AlgorithmImplementation
> hmac_
;
70 const scoped_ptr
<AlgorithmImplementation
> rsa_ssa_
;
71 const scoped_ptr
<AlgorithmImplementation
> rsa_oaep_
;
72 const scoped_ptr
<AlgorithmImplementation
> rsa_pss_
;
77 base::LazyInstance
<AlgorithmRegistry
>::Leaky g_algorithm_registry
=
78 LAZY_INSTANCE_INITIALIZER
;
80 Status
GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id
,
81 const AlgorithmImplementation
** impl
) {
82 *impl
= g_algorithm_registry
.Get().GetAlgorithm(id
);
84 return Status::Success();
85 return Status::ErrorUnsupported();
88 } // namespace webcrypto
90 } // namespace content