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

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: removed JWK AES alg key length validation, replaced with TODO 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 WebKit::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
17 WebKit::WebCryptoAlgorithmId aes_alg_id,
18 unsigned short length) {
19 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
20 aes_alg_id, new WebKit::WebCryptoAesKeyGenParams(length));
21 }
22
23 } // namespace
24
25 const uint8* Start(const std::vector<uint8>& data) {
26 if (data.empty())
27 return NULL;
28 return &data[0];
29 }
30
31 void ShrinkBuffer(WebKit::WebArrayBuffer* buffer, unsigned new_size) {
32 DCHECK_LE(new_size, buffer->byteLength());
33
34 if (new_size == buffer->byteLength())
35 return;
36
37 WebKit::WebArrayBuffer new_buffer =
38 WebKit::WebArrayBuffer::create(new_size, 1);
39 DCHECK(!new_buffer.isNull());
40 memcpy(new_buffer.data(), buffer->data(), new_size);
41 *buffer = new_buffer;
42 }
43
44 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) {
45 std::string base64EncodedText(input);
46 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+');
47 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/');
48 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '=');
49 return base::Base64Decode(base64EncodedText, output);
50 }
51
52 WebKit::WebCryptoAlgorithm GetInnerHashAlgorithm(
53 const WebKit::WebCryptoAlgorithm& algorithm) {
54 if (algorithm.hmacParams())
55 return algorithm.hmacParams()->hash();
56 if (algorithm.hmacKeyParams())
57 return algorithm.hmacKeyParams()->hash();
58 if (algorithm.rsaSsaParams())
59 return algorithm.rsaSsaParams()->hash();
60 if (algorithm.rsaOaepParams())
61 return algorithm.rsaOaepParams()->hash();
62 return WebKit::WebCryptoAlgorithm::createNull();
63 }
64
65 WebKit::WebCryptoAlgorithm CreateAlgorithm(WebKit::WebCryptoAlgorithmId id) {
66 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL);
67 }
68
69 WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByDigestLen(
70 unsigned short digest_length_bits) {
71 WebKit::WebCryptoAlgorithmId hash_id;
72 switch (digest_length_bits) {
73 case 160:
74 hash_id = WebKit::WebCryptoAlgorithmIdSha1;
75 break;
76 case 224:
77 hash_id = WebKit::WebCryptoAlgorithmIdSha224;
78 break;
79 case 256:
80 hash_id = WebKit::WebCryptoAlgorithmIdSha256;
81 break;
82 case 384:
83 hash_id = WebKit::WebCryptoAlgorithmIdSha384;
84 break;
85 case 512:
86 hash_id = WebKit::WebCryptoAlgorithmIdSha512;
87 break;
88 default:
89 NOTREACHED();
90 return WebKit::WebCryptoAlgorithm::createNull();
91 }
92 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
93 WebKit::WebCryptoAlgorithmIdHmac,
94 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id)));
95 }
96
97 WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByHashId(
98 WebKit::WebCryptoAlgorithmId hash_id) {
99 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
eroman 2013/11/07 20:54:37 [optional] I propose adding DCHECK(IsHashAlgorithm
padolph 2013/11/09 00:33:38 Done.
100 WebKit::WebCryptoAlgorithmIdHmac,
101 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id)));
102 }
103
104 WebKit::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm(
105 WebKit::WebCryptoAlgorithmId hash_id,
106 unsigned hash_length) {
107 // hash_length < 0 means unspecified
108 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
eroman 2013/11/07 20:54:37 [optional] I propose adding DCHECK(IsHashAlgorithm
padolph 2013/11/09 00:33:38 Done.
109 WebKit::WebCryptoAlgorithmIdHmac,
110 new WebKit::WebCryptoHmacKeyParams(
111 CreateAlgorithm(hash_id), (hash_length != 0), hash_length));
eroman 2013/11/07 20:54:37 'hash_length" isn't the right name, this is more l
padolph 2013/11/09 00:33:38 Done.
112 }
113
114 WebKit::WebCryptoAlgorithm CreateRsaSsaAlgorithm(
115 WebKit::WebCryptoAlgorithmId hash_algorithm_id) {
eroman 2013/11/07 20:54:37 hash_id
padolph 2013/11/09 00:33:38 Done.
116 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
eroman 2013/11/07 20:54:37 [optional] I propose adding DCHECK(IsHashAlgorithm
padolph 2013/11/09 00:33:38 Done.
117 WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
118 new WebKit::WebCryptoRsaSsaParams(CreateAlgorithm(hash_algorithm_id)));
119 }
120
121 WebKit::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
122 WebKit::WebCryptoAlgorithmId hash_algorithm_id) {
eroman 2013/11/07 20:54:37 hash_id
padolph 2013/11/09 00:33:38 Done.
123 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
eroman 2013/11/07 20:54:37 [optional] I propose adding DCHECK(IsHashAlgorithm
padolph 2013/11/09 00:33:38 Done.
124 WebKit::WebCryptoAlgorithmIdRsaOaep,
125 new WebKit::WebCryptoRsaOaepParams(
126 CreateAlgorithm(hash_algorithm_id), false, NULL, 0));
127 }
128
129 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) {
130 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
131 WebKit::WebCryptoAlgorithmIdAesCbc,
132 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size()));
133 }
134
135 WebKit::WebCryptoAlgorithm CreateAesGcmAlgorithm(
136 const std::vector<uint8>& iv,
137 const std::vector<uint8>& additional_data,
138 unsigned char tag_length) {
139 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
140 WebKit::WebCryptoAlgorithmIdAesCbc,
141 new WebKit::WebCryptoAesGcmParams(Start(iv),
142 iv.size(),
143 additional_data.size(),
144 Start(additional_data),
145 additional_data.size(),
146 tag_length != 0,
147 tag_length));
148 }
149
150 WebKit::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(unsigned short length) {
151 return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc, length);
152 }
153
154 WebKit::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(unsigned short length) {
155 return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesGcm, length);
156 }
157
158 } // 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