blob: e45ec59902e069ffdebc97bef7ca649a95fb424d [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()),
33 hkdf_(CreatePlatformHkdfImplementation()) {
[email protected]38409aec2014-07-19 00:54:5134 PlatformInit();
35 }
36
37 const AlgorithmImplementation* GetAlgorithm(
38 blink::WebCryptoAlgorithmId id) const {
39 switch (id) {
40 case blink::WebCryptoAlgorithmIdSha1:
41 case blink::WebCryptoAlgorithmIdSha256:
42 case blink::WebCryptoAlgorithmIdSha384:
43 case blink::WebCryptoAlgorithmIdSha512:
44 return sha_.get();
45 case blink::WebCryptoAlgorithmIdAesGcm:
46 return aes_gcm_.get();
47 case blink::WebCryptoAlgorithmIdAesCbc:
48 return aes_cbc_.get();
eroman4d7a0e02014-08-27 00:30:3349 case blink::WebCryptoAlgorithmIdAesCtr:
50 return aes_ctr_.get();
[email protected]38409aec2014-07-19 00:54:5151 case blink::WebCryptoAlgorithmIdAesKw:
52 return aes_kw_.get();
53 case blink::WebCryptoAlgorithmIdHmac:
54 return hmac_.get();
55 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
56 return rsa_ssa_.get();
57 case blink::WebCryptoAlgorithmIdRsaOaep:
58 return rsa_oaep_.get();
eroman8793ece2014-10-20 20:47:1559 case blink::WebCryptoAlgorithmIdRsaPss:
60 return rsa_pss_.get();
eromanb2ead6d2014-11-14 02:26:1461 case blink::WebCryptoAlgorithmIdEcdsa:
62 return ecdsa_.get();
eromaned48e812014-11-28 19:59:1363 case blink::WebCryptoAlgorithmIdEcdh:
64 return ecdh_.get();
nharper651031792015-01-13 18:10:3965 case blink::WebCryptoAlgorithmIdHkdf:
66 return hkdf_.get();
[email protected]38409aec2014-07-19 00:54:5167 default:
68 return NULL;
69 }
70 }
71
72 private:
eromancd1425982014-08-27 18:52:3473 const scoped_ptr<AlgorithmImplementation> sha_;
74 const scoped_ptr<AlgorithmImplementation> aes_gcm_;
75 const scoped_ptr<AlgorithmImplementation> aes_cbc_;
76 const scoped_ptr<AlgorithmImplementation> aes_ctr_;
77 const scoped_ptr<AlgorithmImplementation> aes_kw_;
78 const scoped_ptr<AlgorithmImplementation> hmac_;
79 const scoped_ptr<AlgorithmImplementation> rsa_ssa_;
80 const scoped_ptr<AlgorithmImplementation> rsa_oaep_;
eroman8793ece2014-10-20 20:47:1581 const scoped_ptr<AlgorithmImplementation> rsa_pss_;
eromanb2ead6d2014-11-14 02:26:1482 const scoped_ptr<AlgorithmImplementation> ecdsa_;
eromaned48e812014-11-28 19:59:1383 const scoped_ptr<AlgorithmImplementation> ecdh_;
nharper651031792015-01-13 18:10:3984 const scoped_ptr<AlgorithmImplementation> hkdf_;
[email protected]38409aec2014-07-19 00:54:5185};
86
87} // namespace
88
89base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
90 LAZY_INSTANCE_INITIALIZER;
91
92Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id,
93 const AlgorithmImplementation** impl) {
94 *impl = g_algorithm_registry.Get().GetAlgorithm(id);
95 if (*impl)
96 return Status::Success();
97 return Status::ErrorUnsupported();
98}
99
100} // namespace webcrypto
101
102} // namespace content