Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1703)

Side by Side Diff: content/renderer/webcrypto/webcrypto_util.cc

Issue 25906002: [webcrypto] Add JWK import for HMAC and AES-CBC key. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes for eroman Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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
5 #include "content/renderer/webcrypto/webcrypto_util.h"
6
7 #include "base/base64.h"
8 #include "base/logging.h"
9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
11
12 namespace content {
13
14 namespace {
15
16 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
17 blink::WebCryptoAlgorithmId aes_alg_id,
18 unsigned short length) {
19 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
20 aes_alg_id, new blink::WebCryptoAesKeyGenParams(length));
21 }
22
23 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) {
24 return alg_id == blink::WebCryptoAlgorithmIdSha1 ||
25 alg_id == blink::WebCryptoAlgorithmIdSha224 ||
26 alg_id == blink::WebCryptoAlgorithmIdSha256 ||
27 alg_id == blink::WebCryptoAlgorithmIdSha384 ||
28 alg_id == blink::WebCryptoAlgorithmIdSha512;
29 }
30
31 } // namespace
32
33 const uint8* Start(const std::vector<uint8>& data) {
34 if (data.empty())
35 return NULL;
36 return &data[0];
37 }
38
39 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) {
40 DCHECK_LE(new_size, buffer->byteLength());
41
42 if (new_size == buffer->byteLength())
43 return;
44
45 blink::WebArrayBuffer new_buffer =
46 blink::WebArrayBuffer::create(new_size, 1);
47 DCHECK(!new_buffer.isNull());
48 memcpy(new_buffer.data(), buffer->data(), new_size);
49 *buffer = new_buffer;
50 }
51
52 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) {
53 std::string base64EncodedText(input);
54 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+');
55 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/');
56 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '=');
57 return base::Base64Decode(base64EncodedText, output);
58 }
59
60 blink::WebCryptoAlgorithm GetInnerHashAlgorithm(
61 const blink::WebCryptoAlgorithm& algorithm) {
62 if (algorithm.hmacParams())
63 return algorithm.hmacParams()->hash();
64 if (algorithm.hmacKeyParams())
65 return algorithm.hmacKeyParams()->hash();
66 if (algorithm.rsaSsaParams())
67 return algorithm.rsaSsaParams()->hash();
68 if (algorithm.rsaOaepParams())
69 return algorithm.rsaOaepParams()->hash();
70 return blink::WebCryptoAlgorithm::createNull();
71 }
72
73 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) {
74 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL);
75 }
76
77 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashOutputLen(
78 unsigned short hash_output_length_bits) {
79 blink::WebCryptoAlgorithmId hash_id;
80 switch (hash_output_length_bits) {
81 case 160:
82 hash_id = blink::WebCryptoAlgorithmIdSha1;
83 break;
84 case 224:
85 hash_id = blink::WebCryptoAlgorithmIdSha224;
86 break;
87 case 256:
88 hash_id = blink::WebCryptoAlgorithmIdSha256;
89 break;
90 case 384:
91 hash_id = blink::WebCryptoAlgorithmIdSha384;
92 break;
93 case 512:
94 hash_id = blink::WebCryptoAlgorithmIdSha512;
95 break;
96 default:
97 NOTREACHED();
98 return blink::WebCryptoAlgorithm::createNull();
99 }
100 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
101 blink::WebCryptoAlgorithmIdHmac,
102 new blink::WebCryptoHmacParams(CreateAlgorithm(hash_id)));
103 }
104
105 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId(
106 blink::WebCryptoAlgorithmId hash_id) {
107 DCHECK(IsHashAlgorithm(hash_id));
108 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
109 blink::WebCryptoAlgorithmIdHmac,
110 new blink::WebCryptoHmacParams(CreateAlgorithm(hash_id)));
111 }
112
113 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm(
114 blink::WebCryptoAlgorithmId hash_id,
115 unsigned key_length_bytes) {
116 DCHECK(IsHashAlgorithm(hash_id));
117 // key_length_bytes == 0 means unspecified
118 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
119 blink::WebCryptoAlgorithmIdHmac,
120 new blink::WebCryptoHmacKeyParams(
121 CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes));
122 }
123
124 blink::WebCryptoAlgorithm CreateRsaSsaAlgorithm(
125 blink::WebCryptoAlgorithmId hash_id) {
126 DCHECK(IsHashAlgorithm(hash_id));
127 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
128 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
129 new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id)));
130 }
131
132 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
133 blink::WebCryptoAlgorithmId hash_id) {
134 DCHECK(IsHashAlgorithm(hash_id));
135 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
136 blink::WebCryptoAlgorithmIdRsaOaep,
137 new blink::WebCryptoRsaOaepParams(
138 CreateAlgorithm(hash_id), false, NULL, 0));
139 }
140
141 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) {
142 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
143 blink::WebCryptoAlgorithmIdAesCbc,
144 new blink::WebCryptoAesCbcParams(Start(iv), iv.size()));
145 }
146
147 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
148 const std::vector<uint8>& iv,
149 const std::vector<uint8>& additional_data,
150 uint8 tag_length_bytes) {
151 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
152 blink::WebCryptoAlgorithmIdAesCbc,
153 new blink::WebCryptoAesGcmParams(Start(iv),
154 iv.size(),
155 additional_data.size(),
156 Start(additional_data),
157 additional_data.size(),
158 tag_length_bytes != 0,
159 tag_length_bytes));
160 }
161
162 blink::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(
163 unsigned short key_length_bits) {
164 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesCbc,
165 key_length_bits);
166 }
167
168 blink::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(
169 unsigned short key_length_bits) {
170 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesGcm,
171 key_length_bits);
172 }
173
174 } // namespace content
OLDNEW
« content/renderer/webcrypto/webcrypto_util.h ('K') | « content/renderer/webcrypto/webcrypto_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698