blob: 5828a0c8d9bdce0e44bdee97f2535db27693c429 [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
5#include "content/child/webcrypto/algorithm_registry.h"
6
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"
11
12namespace content {
13
14namespace webcrypto {
15
16namespace {
17
eromancd1425982014-08-27 18:52:3418// This class is used as a singleton. All methods must be threadsafe.
[email protected]38409aec2014-07-19 00:54:5119class AlgorithmRegistry {
20 public:
21 AlgorithmRegistry()
22 : sha_(CreatePlatformShaImplementation()),
23 aes_gcm_(CreatePlatformAesGcmImplementation()),
24 aes_cbc_(CreatePlatformAesCbcImplementation()),
eroman4d7a0e02014-08-27 00:30:3325 aes_ctr_(CreatePlatformAesCtrImplementation()),
[email protected]38409aec2014-07-19 00:54:5126 aes_kw_(CreatePlatformAesKwImplementation()),
27 hmac_(CreatePlatformHmacImplementation()),
28 rsa_ssa_(CreatePlatformRsaSsaImplementation()),
eroman8793ece2014-10-20 20:47:1529 rsa_oaep_(CreatePlatformRsaOaepImplementation()),
eromanb2ead6d2014-11-14 02:26:1430 rsa_pss_(CreatePlatformRsaPssImplementation()),
eromaned48e812014-11-28 19:59:1331 ecdsa_(CreatePlatformEcdsaImplementation()),
nharper651031792015-01-13 18:10:3932 ecdh_(CreatePlatformEcdhImplementation()),
xun.sun22a80e72015-01-21 13:57:1933 hkdf_(CreatePlatformHkdfImplementation()),
34 pbkdf2_(CreatePlatformPbkdf2Implementation()) {
[email protected]38409aec2014-07-19 00:54:5135 PlatformInit();
36 }
37
38 const AlgorithmImplementation* GetAlgorithm(
39 blink::WebCryptoAlgorithmId id) const {
40 switch (id) {
41 case blink::WebCryptoAlgorithmIdSha1:
42 case blink::WebCryptoAlgorithmIdSha256:
43 case blink::WebCryptoAlgorithmIdSha384:
44 case blink::WebCryptoAlgorithmIdSha512:
45 return sha_.get();
46 case blink::WebCryptoAlgorithmIdAesGcm:
47 return aes_gcm_.get();
48 case blink::WebCryptoAlgorithmIdAesCbc:
49 return aes_cbc_.get();
eroman4d7a0e02014-08-27 00:30:3350 case blink::WebCryptoAlgorithmIdAesCtr:
51 return aes_ctr_.get();
[email protected]38409aec2014-07-19 00:54:5152 case blink::WebCryptoAlgorithmIdAesKw:
53 return aes_kw_.get();
54 case blink::WebCryptoAlgorithmIdHmac:
55 return hmac_.get();
56 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
57 return rsa_ssa_.get();
58 case blink::WebCryptoAlgorithmIdRsaOaep:
59 return rsa_oaep_.get();
eroman8793ece2014-10-20 20:47:1560 case blink::WebCryptoAlgorithmIdRsaPss:
61 return rsa_pss_.get();
eromanb2ead6d2014-11-14 02:26:1462 case blink::WebCryptoAlgorithmIdEcdsa:
63 return ecdsa_.get();
eromaned48e812014-11-28 19:59:1364 case blink::WebCryptoAlgorithmIdEcdh:
65 return ecdh_.get();
nharper651031792015-01-13 18:10:3966 case blink::WebCryptoAlgorithmIdHkdf:
67 return hkdf_.get();
xun.sun22a80e72015-01-21 13:57:1968 case blink::WebCryptoAlgorithmIdPbkdf2:
69 return pbkdf2_.get();
[email protected]38409aec2014-07-19 00:54:5170 default:
71 return NULL;
72 }
73 }
74
75 private:
eromancd1425982014-08-27 18:52:3476 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_;
eroman8793ece2014-10-20 20:47:1584 const scoped_ptr<AlgorithmImplementation> rsa_pss_;
eromanb2ead6d2014-11-14 02:26:1485 const scoped_ptr<AlgorithmImplementation> ecdsa_;
eromaned48e812014-11-28 19:59:1386 const scoped_ptr<AlgorithmImplementation> ecdh_;
nharper651031792015-01-13 18:10:3987 const scoped_ptr<AlgorithmImplementation> hkdf_;
xun.sun22a80e72015-01-21 13:57:1988 const scoped_ptr<AlgorithmImplementation> pbkdf2_;
[email protected]38409aec2014-07-19 00:54:5189};
90
91} // namespace
92
93base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
94 LAZY_INSTANCE_INITIALIZER;
95
96Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id,
97 const AlgorithmImplementation** impl) {
98 *impl = g_algorithm_registry.Get().GetAlgorithm(id);
99 if (*impl)
100 return Status::Success();
101 return Status::ErrorUnsupported();
102}
103
104} // namespace webcrypto
105
106} // namespace content