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.
4
5#ifndef CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_IMPL_H_
6#define CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_IMPL_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "third_party/WebKit/public/platform/WebCrypto.h"
11#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
12#include "third_party/WebKit/public/platform/WebVector.h"
13
14namespace content {
15
16// Wrapper around the Blink WebCrypto asynchronous interface, which forwards to
17// the synchronous platform (NSS or OpenSSL) implementation.
18//
19// WebCryptoImpl is threadsafe.
20//
21// EnsureInit() must be called prior to using methods on WebCryptoImpl().
22class WebCryptoImpl : public blink::WebCrypto {
23 public:
24  WebCryptoImpl();
25
26  virtual ~WebCryptoImpl();
27
28  virtual void encrypt(const blink::WebCryptoAlgorithm& algorithm,
29                       const blink::WebCryptoKey& key,
30                       const unsigned char* data,
31                       unsigned int data_size,
32                       blink::WebCryptoResult result);
33  virtual void decrypt(const blink::WebCryptoAlgorithm& algorithm,
34                       const blink::WebCryptoKey& key,
35                       const unsigned char* data,
36                       unsigned int data_size,
37                       blink::WebCryptoResult result);
38  virtual void digest(const blink::WebCryptoAlgorithm& algorithm,
39                      const unsigned char* data,
40                      unsigned int data_size,
41                      blink::WebCryptoResult result);
42  virtual void generateKey(const blink::WebCryptoAlgorithm& algorithm,
43                           bool extractable,
44                           blink::WebCryptoKeyUsageMask usage_mask,
45                           blink::WebCryptoResult result);
46  virtual void importKey(blink::WebCryptoKeyFormat format,
47                         const unsigned char* key_data,
48                         unsigned int key_data_size,
49                         const blink::WebCryptoAlgorithm& algorithm,
50                         bool extractable,
51                         blink::WebCryptoKeyUsageMask usage_mask,
52                         blink::WebCryptoResult result);
53  virtual void exportKey(blink::WebCryptoKeyFormat format,
54                         const blink::WebCryptoKey& key,
55                         blink::WebCryptoResult result);
56  virtual void sign(const blink::WebCryptoAlgorithm& algorithm,
57                    const blink::WebCryptoKey& key,
58                    const unsigned char* data,
59                    unsigned int data_size,
60                    blink::WebCryptoResult result);
61  virtual void verifySignature(const blink::WebCryptoAlgorithm& algorithm,
62                               const blink::WebCryptoKey& key,
63                               const unsigned char* signature,
64                               unsigned int signature_size,
65                               const unsigned char* data,
66                               unsigned int data_size,
67                              blink::WebCryptoResult result);
68  virtual void wrapKey(blink::WebCryptoKeyFormat format,
69                       const blink::WebCryptoKey& key,
70                       const blink::WebCryptoKey& wrapping_key,
71                       const blink::WebCryptoAlgorithm& wrap_algorithm,
72                       blink::WebCryptoResult result);
73  virtual void unwrapKey(
74      blink::WebCryptoKeyFormat format,
75      const unsigned char* wrapped_key,
76      unsigned wrapped_key_size,
77      const blink::WebCryptoKey& wrapping_key,
78      const blink::WebCryptoAlgorithm& unwrap_algorithm,
79      const blink::WebCryptoAlgorithm& unwrapped_key_algorithm,
80      bool extractable,
81      blink::WebCryptoKeyUsageMask usages,
82      blink::WebCryptoResult result);
83
84  // This method returns a digestor object that can be used to synchronously
85  // compute a digest one chunk at a time. Thus, the consume does not need to
86  // hold onto a large buffer with all the data to digest. Chunks can be given
87  // one at a time and the digest will be computed piecemeal. The allocated
88  // WebCrytpoDigestor that is returned by createDigestor must be freed by the
89  // caller.
90  virtual blink::WebCryptoDigestor* createDigestor(
91      blink::WebCryptoAlgorithmId algorithm_id);
92
93  virtual bool deserializeKeyForClone(
94      const blink::WebCryptoKeyAlgorithm& algorithm,
95      blink::WebCryptoKeyType type,
96      bool extractable,
97      blink::WebCryptoKeyUsageMask usages,
98      const unsigned char* key_data,
99      unsigned key_data_size,
100      blink::WebCryptoKey& key);
101
102  virtual bool serializeKeyForClone(const blink::WebCryptoKey& key,
103                                    blink::WebVector<unsigned char>& key_data);
104
105 private:
106  DISALLOW_COPY_AND_ASSIGN(WebCryptoImpl);
107};
108
109}  // namespace content
110
111#endif  // CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_IMPL_H_
112