blob: 32c0433597e706172482cd8ff56acb9cc4a68739 [file] [log] [blame]
[email protected]408699c2013-07-17 21:23:161// Copyright (c) 2013 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
[email protected]3da6b212013-09-27 05:02:365#include "content/renderer/webcrypto/webcrypto_impl.h"
[email protected]408699c2013-07-17 21:23:166
[email protected]1c879bc92013-09-18 07:45:327#include "base/memory/scoped_ptr.h"
[email protected]e5b794b2013-08-30 01:32:548#include "third_party/WebKit/public/platform/WebArrayBuffer.h"
[email protected]1c879bc92013-09-18 07:45:329#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
10#include "third_party/WebKit/public/platform/WebCryptoKey.h"
[email protected]408699c2013-07-17 21:23:1611
12namespace content {
13
[email protected]7e4c36f2013-09-12 06:10:1914WebCryptoImpl::WebCryptoImpl() {
15 Init();
16}
17
[email protected]a2a06c732013-09-27 10:50:5418void WebCryptoImpl::encrypt(
19 const WebKit::WebCryptoAlgorithm& algorithm,
20 const WebKit::WebCryptoKey& key,
21 const unsigned char* data,
22 unsigned data_size,
23 WebKit::WebCryptoResult result) {
24 WebKit::WebArrayBuffer buffer;
25 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) {
26 result.completeWithError();
27 } else {
28 result.completeWithBuffer(buffer);
29 }
30}
31
[email protected]e5b794b2013-08-30 01:32:5432void WebCryptoImpl::digest(
33 const WebKit::WebCryptoAlgorithm& algorithm,
34 const unsigned char* data,
[email protected]ed80a092013-09-10 16:36:3835 unsigned data_size,
[email protected]e5b794b2013-08-30 01:32:5436 WebKit::WebCryptoResult result) {
37 WebKit::WebArrayBuffer buffer;
[email protected]ed80a092013-09-10 16:36:3838 if (!DigestInternal(algorithm, data, data_size, &buffer)) {
[email protected]e5b794b2013-08-30 01:32:5439 result.completeWithError();
40 } else {
41 result.completeWithBuffer(buffer);
[email protected]408699c2013-07-17 21:23:1642 }
43}
44
[email protected]1c879bc92013-09-18 07:45:3245void WebCryptoImpl::importKey(
46 WebKit::WebCryptoKeyFormat format,
47 const unsigned char* key_data,
48 unsigned key_data_size,
49 const WebKit::WebCryptoAlgorithm& algorithm,
50 bool extractable,
51 WebKit::WebCryptoKeyUsageMask usage_mask,
52 WebKit::WebCryptoResult result) {
53 WebKit::WebCryptoKeyType type;
54 scoped_ptr<WebKit::WebCryptoKeyHandle> handle;
55
56 if (!ImportKeyInternal(format,
57 key_data,
58 key_data_size,
59 algorithm,
60 usage_mask,
61 &handle,
62 &type)) {
63 result.completeWithError();
64 return;
65 }
66
67 WebKit::WebCryptoKey key(
68 WebKit::WebCryptoKey::create(
69 handle.release(), type, extractable, algorithm, usage_mask));
70
71 result.completeWithKey(key);
72}
73
74void WebCryptoImpl::sign(
75 const WebKit::WebCryptoAlgorithm& algorithm,
76 const WebKit::WebCryptoKey& key,
77 const unsigned char* data,
78 unsigned data_size,
79 WebKit::WebCryptoResult result) {
80 WebKit::WebArrayBuffer buffer;
81 if (!SignInternal(algorithm, key, data, data_size, &buffer)) {
82 result.completeWithError();
83 } else {
84 result.completeWithBuffer(buffer);
85 }
86}
87
[email protected]3ed00262013-09-26 22:28:1088void WebCryptoImpl::verifySignature(
89 const WebKit::WebCryptoAlgorithm& algorithm,
90 const WebKit::WebCryptoKey& key,
91 const unsigned char* signature,
92 unsigned signature_size,
93 const unsigned char* data,
94 unsigned data_size,
95 WebKit::WebCryptoResult result) {
96 bool signature_match = false;
97 if (!VerifySignatureInternal(algorithm,
98 key,
99 signature,
100 signature_size,
101 data,
102 data_size,
103 &signature_match)) {
104 result.completeWithError();
105 } else {
106 result.completeWithBoolean(signature_match);
107 }
108}
109
[email protected]408699c2013-07-17 21:23:16110} // namespace content