blob: ea4799e70e9b5c1d939c379ef287040143a85058 [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"
9#include "components/webcrypto/platform_crypto.h"
10#include "components/webcrypto/status.h"
[email protected]38409aec2014-07-19 00:54:5111
12namespace webcrypto {
13
14namespace {
15
eromancd1425982014-08-27 18:52:3416// This class is used as a singleton. All methods must be threadsafe.
[email protected]38409aec2014-07-19 00:54:5117class AlgorithmRegistry {
18 public:
19 AlgorithmRegistry()
20 : sha_(CreatePlatformShaImplementation()),
21 aes_gcm_(CreatePlatformAesGcmImplementation()),
22 aes_cbc_(CreatePlatformAesCbcImplementation()),
eroman4d7a0e02014-08-27 00:30:3323 aes_ctr_(CreatePlatformAesCtrImplementation()),
[email protected]38409aec2014-07-19 00:54:5124 aes_kw_(CreatePlatformAesKwImplementation()),
25 hmac_(CreatePlatformHmacImplementation()),
26 rsa_ssa_(CreatePlatformRsaSsaImplementation()),
eroman8793ece2014-10-20 20:47:1527 rsa_oaep_(CreatePlatformRsaOaepImplementation()),
eromanb2ead6d2014-11-14 02:26:1428 rsa_pss_(CreatePlatformRsaPssImplementation()),
eromaned48e812014-11-28 19:59:1329 ecdsa_(CreatePlatformEcdsaImplementation()),
nharper651031792015-01-13 18:10:3930 ecdh_(CreatePlatformEcdhImplementation()),
xun.sun22a80e72015-01-21 13:57:1931 hkdf_(CreatePlatformHkdfImplementation()),
32 pbkdf2_(CreatePlatformPbkdf2Implementation()) {
[email protected]38409aec2014-07-19 00:54:5133 PlatformInit();
34 }
35
36 const AlgorithmImplementation* GetAlgorithm(
37 blink::WebCryptoAlgorithmId id) const {
38 switch (id) {
39 case blink::WebCryptoAlgorithmIdSha1:
40 case blink::WebCryptoAlgorithmIdSha256:
41 case blink::WebCryptoAlgorithmIdSha384:
42 case blink::WebCryptoAlgorithmIdSha512:
43 return sha_.get();
44 case blink::WebCryptoAlgorithmIdAesGcm:
45 return aes_gcm_.get();
46 case blink::WebCryptoAlgorithmIdAesCbc:
47 return aes_cbc_.get();
eroman4d7a0e02014-08-27 00:30:3348 case blink::WebCryptoAlgorithmIdAesCtr:
49 return aes_ctr_.get();
[email protected]38409aec2014-07-19 00:54:5150 case blink::WebCryptoAlgorithmIdAesKw:
51 return aes_kw_.get();
52 case blink::WebCryptoAlgorithmIdHmac:
53 return hmac_.get();
54 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
55 return rsa_ssa_.get();
56 case blink::WebCryptoAlgorithmIdRsaOaep:
57 return rsa_oaep_.get();
eroman8793ece2014-10-20 20:47:1558 case blink::WebCryptoAlgorithmIdRsaPss:
59 return rsa_pss_.get();
eromanb2ead6d2014-11-14 02:26:1460 case blink::WebCryptoAlgorithmIdEcdsa:
61 return ecdsa_.get();
eromaned48e812014-11-28 19:59:1362 case blink::WebCryptoAlgorithmIdEcdh:
63 return ecdh_.get();
nharper651031792015-01-13 18:10:3964 case blink::WebCryptoAlgorithmIdHkdf:
65 return hkdf_.get();
xun.sun22a80e72015-01-21 13:57:1966 case blink::WebCryptoAlgorithmIdPbkdf2:
67 return pbkdf2_.get();
[email protected]38409aec2014-07-19 00:54:5168 default:
69 return NULL;
70 }
71 }
72
73 private:
eromancd1425982014-08-27 18:52:3474 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_;
eroman8793ece2014-10-20 20:47:1582 const scoped_ptr<AlgorithmImplementation> rsa_pss_;
eromanb2ead6d2014-11-14 02:26:1483 const scoped_ptr<AlgorithmImplementation> ecdsa_;
eromaned48e812014-11-28 19:59:1384 const scoped_ptr<AlgorithmImplementation> ecdh_;
nharper651031792015-01-13 18:10:3985 const scoped_ptr<AlgorithmImplementation> hkdf_;
xun.sun22a80e72015-01-21 13:57:1986 const scoped_ptr<AlgorithmImplementation> pbkdf2_;
[email protected]38409aec2014-07-19 00:54:5187};
88
89} // namespace
90
91base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
92 LAZY_INSTANCE_INITIALIZER;
93
94Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id,
95 const AlgorithmImplementation** impl) {
96 *impl = g_algorithm_registry.Get().GetAlgorithm(id);
97 if (*impl)
98 return Status::Success();
99 return Status::ErrorUnsupported();
100}
101
102} // namespace webcrypto