OLD | NEW |
---|---|
(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 WebKit::WebCryptoAlgorithm CreateAlgorithmWithInnerHash( | |
24 WebKit::WebCryptoAlgorithmId algorithm_id, | |
25 unsigned short hash_key_length) { | |
26 WebKit::WebCryptoAlgorithmId hash_id; | |
27 switch (hash_key_length) { | |
28 case 160: | |
29 hash_id = WebKit::WebCryptoAlgorithmIdSha1; | |
30 break; | |
31 case 224: | |
32 hash_id = WebKit::WebCryptoAlgorithmIdSha224; | |
33 break; | |
34 case 256: | |
35 hash_id = WebKit::WebCryptoAlgorithmIdSha256; | |
36 break; | |
37 case 384: | |
38 hash_id = WebKit::WebCryptoAlgorithmIdSha384; | |
39 break; | |
40 case 512: | |
41 hash_id = WebKit::WebCryptoAlgorithmIdSha512; | |
42 break; | |
43 default: | |
44 NOTREACHED(); | |
45 return WebKit::WebCryptoAlgorithm::createNull(); | |
46 } | |
47 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
48 algorithm_id, new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id))); | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 const uint8* Start(const std::vector<uint8>& data) { | |
54 if (data.empty()) | |
55 return NULL; | |
56 return &data[0]; | |
57 } | |
58 | |
59 void ShrinkBuffer(WebKit::WebArrayBuffer* buffer, unsigned new_size) { | |
60 DCHECK_LE(new_size, buffer->byteLength()); | |
61 | |
62 if (new_size == buffer->byteLength()) | |
63 return; | |
64 | |
65 WebKit::WebArrayBuffer new_buffer = | |
66 WebKit::WebArrayBuffer::create(new_size, 1); | |
67 DCHECK(!new_buffer.isNull()); | |
68 memcpy(new_buffer.data(), buffer->data(), new_size); | |
69 *buffer = new_buffer; | |
70 } | |
71 | |
72 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { | |
73 std::string base64EncodedText(input); | |
74 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); | |
75 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); | |
76 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); | |
77 return base::Base64Decode(base64EncodedText, output); | |
78 } | |
79 | |
80 WebKit::WebCryptoAlgorithm GetInnerHashAlgorithm( | |
81 const WebKit::WebCryptoAlgorithm& algorithm) { | |
82 switch (algorithm.id()) { | |
83 case WebKit::WebCryptoAlgorithmIdHmac: { | |
84 const WebKit::WebCryptoHmacParams* const params = algorithm.hmacParams(); | |
eroman
2013/10/29 03:03:46
Cant get rid of the local variable, and consequent
padolph
2013/10/29 22:57:01
replaced function with your version below.
| |
85 return params->hash(); | |
86 } | |
87 case WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: { | |
88 const WebKit::WebCryptoRsaSsaParams* const params = | |
89 algorithm.rsaSsaParams(); | |
90 return params->hash(); | |
eroman
2013/10/29 03:03:46
WebCryptoAlgorithm can have different parameter ty
padolph
2013/10/29 22:57:01
Done.
| |
91 } | |
92 case WebKit::WebCryptoAlgorithmIdRsaOaep: { | |
93 const WebKit::WebCryptoRsaOaepParams* const params = | |
94 algorithm.rsaOaepParams(); | |
95 return params->hash(); | |
96 } | |
97 default: | |
98 return WebKit::WebCryptoAlgorithm::createNull(); | |
99 } | |
100 } | |
101 | |
102 WebKit::WebCryptoAlgorithm CreateAlgorithm(WebKit::WebCryptoAlgorithmId id) { | |
103 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); | |
104 } | |
105 | |
106 WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByKeyLen( | |
107 unsigned short hash_key_length) { | |
108 return CreateAlgorithmWithInnerHash(WebKit::WebCryptoAlgorithmIdHmac, | |
109 hash_key_length); | |
110 } | |
111 | |
112 WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( | |
113 WebKit::WebCryptoAlgorithmId hash_id) { | |
114 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
115 WebKit::WebCryptoAlgorithmIdHmac, | |
116 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id))); | |
117 } | |
118 | |
119 WebKit::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( | |
120 WebKit::WebCryptoAlgorithmId hash_id, | |
121 unsigned hash_length) { | |
122 // hash_length < 0 means unspecified | |
123 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
124 WebKit::WebCryptoAlgorithmIdHmac, | |
125 new WebKit::WebCryptoHmacKeyParams( | |
126 CreateAlgorithm(hash_id), (hash_length != 0), hash_length)); | |
127 } | |
128 | |
129 WebKit::WebCryptoAlgorithm CreateRsaEsAlgorithm() { | |
130 return CreateAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); | |
131 } | |
132 | |
133 WebKit::WebCryptoAlgorithm CreateRsaSsaAlgorithmByKeyLen( | |
Ryan Sleevi
2013/10/29 18:05:13
I don't understand why this function is calling Cr
padolph
2013/10/29 22:57:01
Sorry, I agree this is just wrong. Not sure what h
| |
134 unsigned short hash_key_length) { | |
135 return CreateAlgorithmWithInnerHash( | |
136 WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, hash_key_length); | |
137 } | |
138 | |
139 WebKit::WebCryptoAlgorithm CreateRsaOaepAlgorithmByKeyLen( | |
140 unsigned short hash_key_length) { | |
141 return CreateAlgorithmWithInnerHash(WebKit::WebCryptoAlgorithmIdRsaOaep, | |
142 hash_key_length); | |
143 } | |
144 | |
145 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { | |
146 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
147 WebKit::WebCryptoAlgorithmIdAesCbc, | |
148 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size())); | |
149 } | |
150 | |
151 WebKit::WebCryptoAlgorithm CreateAesGcmAlgorithm( | |
152 const std::vector<uint8>& iv, | |
153 const std::vector<uint8>& additional_data, | |
154 unsigned char tag_length) { | |
155 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
156 WebKit::WebCryptoAlgorithmIdAesCbc, | |
157 new WebKit::WebCryptoAesGcmParams(Start(iv), | |
158 iv.size(), | |
159 additional_data.size(), | |
160 Start(additional_data), | |
161 additional_data.size(), | |
162 tag_length != 0, | |
163 tag_length)); | |
164 } | |
165 | |
166 WebKit::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(unsigned short length) { | |
167 return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc, length); | |
168 } | |
169 | |
170 WebKit::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(unsigned short length) { | |
171 return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesGcm, length); | |
172 } | |
173 | |
174 } // namespace content | |
OLD | NEW |