blob: 28e926f6c3fcdccf52115cb7c344298087edcd0f [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()),
29 rsa_oaep_(CreatePlatformRsaOaepImplementation()) {
30 PlatformInit();
31 }
32
33 const AlgorithmImplementation* GetAlgorithm(
34 blink::WebCryptoAlgorithmId id) const {
35 switch (id) {
36 case blink::WebCryptoAlgorithmIdSha1:
37 case blink::WebCryptoAlgorithmIdSha256:
38 case blink::WebCryptoAlgorithmIdSha384:
39 case blink::WebCryptoAlgorithmIdSha512:
40 return sha_.get();
41 case blink::WebCryptoAlgorithmIdAesGcm:
42 return aes_gcm_.get();
43 case blink::WebCryptoAlgorithmIdAesCbc:
44 return aes_cbc_.get();
eroman4d7a0e02014-08-27 00:30:3345 case blink::WebCryptoAlgorithmIdAesCtr:
46 return aes_ctr_.get();
[email protected]38409aec2014-07-19 00:54:5147 case blink::WebCryptoAlgorithmIdAesKw:
48 return aes_kw_.get();
49 case blink::WebCryptoAlgorithmIdHmac:
50 return hmac_.get();
51 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
52 return rsa_ssa_.get();
53 case blink::WebCryptoAlgorithmIdRsaOaep:
54 return rsa_oaep_.get();
55 default:
56 return NULL;
57 }
58 }
59
60 private:
eromancd1425982014-08-27 18:52:3461 const scoped_ptr<AlgorithmImplementation> sha_;
62 const scoped_ptr<AlgorithmImplementation> aes_gcm_;
63 const scoped_ptr<AlgorithmImplementation> aes_cbc_;
64 const scoped_ptr<AlgorithmImplementation> aes_ctr_;
65 const scoped_ptr<AlgorithmImplementation> aes_kw_;
66 const scoped_ptr<AlgorithmImplementation> hmac_;
67 const scoped_ptr<AlgorithmImplementation> rsa_ssa_;
68 const scoped_ptr<AlgorithmImplementation> rsa_oaep_;
[email protected]38409aec2014-07-19 00:54:5169};
70
71} // namespace
72
73base::LazyInstance<AlgorithmRegistry>::Leaky g_algorithm_registry =
74 LAZY_INSTANCE_INITIALIZER;
75
76Status GetAlgorithmImplementation(blink::WebCryptoAlgorithmId id,
77 const AlgorithmImplementation** impl) {
78 *impl = g_algorithm_registry.Get().GetAlgorithm(id);
79 if (*impl)
80 return Status::Success();
81 return Status::ErrorUnsupported();
82}
83
84} // namespace webcrypto
85
86} // namespace content