OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/rsa_private_key.h" | 5 #include "crypto/rsa_private_key.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 } | 181 } |
182 | 182 |
183 void PrivateKeyInfoCodec::PrependIntegerImpl(uint8* val, | 183 void PrivateKeyInfoCodec::PrependIntegerImpl(uint8* val, |
184 int num_bytes, | 184 int num_bytes, |
185 std::list<uint8>* data, | 185 std::list<uint8>* data, |
186 bool big_endian) { | 186 bool big_endian) { |
187 // Reverse input if little-endian. | 187 // Reverse input if little-endian. |
188 std::vector<uint8> tmp; | 188 std::vector<uint8> tmp; |
189 if (!big_endian) { | 189 if (!big_endian) { |
190 tmp.assign(val, val + num_bytes); | 190 tmp.assign(val, val + num_bytes); |
191 reverse(tmp.begin(), tmp.end()); | 191 std::reverse(tmp.begin(), tmp.end()); |
192 val = &tmp.front(); | 192 val = &tmp.front(); |
193 } | 193 } |
194 | 194 |
195 // ASN.1 integers are unpadded byte arrays, so skip any null padding bytes | 195 // ASN.1 integers are unpadded byte arrays, so skip any null padding bytes |
196 // from the most-significant end of the integer. | 196 // from the most-significant end of the integer. |
197 int start = 0; | 197 int start = 0; |
198 while (start < (num_bytes - 1) && val[start] == 0x00) { | 198 while (start < (num_bytes - 1) && val[start] == 0x00) { |
199 start++; | 199 start++; |
200 num_bytes--; | 200 num_bytes--; |
201 } | 201 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 } | 239 } |
240 | 240 |
241 while (pad) { | 241 while (pad) { |
242 out->push_back(0x00); | 242 out->push_back(0x00); |
243 pad--; | 243 pad--; |
244 } | 244 } |
245 out->insert(out->end(), temp.begin(), temp.end()); | 245 out->insert(out->end(), temp.begin(), temp.end()); |
246 | 246 |
247 // Reverse output if little-endian. | 247 // Reverse output if little-endian. |
248 if (!big_endian_) | 248 if (!big_endian_) |
249 reverse(out->begin(), out->end()); | 249 std::reverse(out->begin(), out->end()); |
250 return true; | 250 return true; |
251 } | 251 } |
252 | 252 |
253 bool PrivateKeyInfoCodec::ReadIntegerImpl(uint8** pos, | 253 bool PrivateKeyInfoCodec::ReadIntegerImpl(uint8** pos, |
254 uint8* end, | 254 uint8* end, |
255 std::vector<uint8>* out, | 255 std::vector<uint8>* out, |
256 bool big_endian) { | 256 bool big_endian) { |
257 uint32 length = 0; | 257 uint32 length = 0; |
258 if (!ReadTypeHeaderAndLength(pos, end, kIntegerTag, &length) || !length) | 258 if (!ReadTypeHeaderAndLength(pos, end, kIntegerTag, &length) || !length) |
259 return false; | 259 return false; |
260 | 260 |
261 // The first byte can be zero to force positiveness. We can ignore this. | 261 // The first byte can be zero to force positiveness. We can ignore this. |
262 if (**pos == 0x00) { | 262 if (**pos == 0x00) { |
263 ++(*pos); | 263 ++(*pos); |
264 --length; | 264 --length; |
265 } | 265 } |
266 | 266 |
267 if (length) | 267 if (length) |
268 out->insert(out->end(), *pos, (*pos) + length); | 268 out->insert(out->end(), *pos, (*pos) + length); |
269 | 269 |
270 (*pos) += length; | 270 (*pos) += length; |
271 | 271 |
272 // Reverse output if little-endian. | 272 // Reverse output if little-endian. |
273 if (!big_endian) | 273 if (!big_endian) |
274 reverse(out->begin(), out->end()); | 274 std::reverse(out->begin(), out->end()); |
275 return true; | 275 return true; |
276 } | 276 } |
277 | 277 |
278 void PrivateKeyInfoCodec::PrependBytes(uint8* val, | 278 void PrivateKeyInfoCodec::PrependBytes(uint8* val, |
279 int start, | 279 int start, |
280 int num_bytes, | 280 int num_bytes, |
281 std::list<uint8>* data) { | 281 std::list<uint8>* data) { |
282 while (num_bytes > 0) { | 282 while (num_bytes > 0) { |
283 --num_bytes; | 283 --num_bytes; |
284 data->push_front(val[start + num_bytes]); | 284 data->push_front(val[start + num_bytes]); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 // The version should be zero. | 381 // The version should be zero. |
382 for (uint32 i = 0; i < length; ++i) { | 382 for (uint32 i = 0; i < length; ++i) { |
383 READ_ASSERT(**pos == 0x00); | 383 READ_ASSERT(**pos == 0x00); |
384 (*pos)++; | 384 (*pos)++; |
385 } | 385 } |
386 | 386 |
387 return true; | 387 return true; |
388 } | 388 } |
389 | 389 |
390 } // namespace crypto | 390 } // namespace crypto |
OLD | NEW |