OLD | NEW |
1 // Copyright (c) 2010 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 "base/crypto/symmetric_key.h" | 5 #include "crypto/symmetric_key.h" |
6 | 6 |
7 #include <CommonCrypto/CommonCryptor.h> | 7 #include <CommonCrypto/CommonCryptor.h> |
8 #include <CoreFoundation/CFString.h> | 8 #include <CoreFoundation/CFString.h> |
9 #include <Security/cssm.h> | 9 #include <Security/cssm.h> |
10 | 10 |
11 #include "base/crypto/cssm_init.h" | |
12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "crypto/cssm_init.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 CSSM_KEY_TYPE CheckKeyParams(base::SymmetricKey::Algorithm algorithm, | 16 CSSM_KEY_TYPE CheckKeyParams(base::SymmetricKey::Algorithm algorithm, |
17 size_t key_size_in_bits) { | 17 size_t key_size_in_bits) { |
18 if (algorithm == base::SymmetricKey::AES) { | 18 if (algorithm == base::SymmetricKey::AES) { |
19 CHECK(key_size_in_bits == 128 || | 19 CHECK(key_size_in_bits == 128 || |
20 key_size_in_bits == 192 || | 20 key_size_in_bits == 192 || |
21 key_size_in_bits == 256) | 21 key_size_in_bits == 256) |
22 << "Invalid key size " << key_size_in_bits << " bits"; | 22 << "Invalid key size " << key_size_in_bits << " bits"; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 inline CSSM_DATA StringToData(const std::string& str) { | 56 inline CSSM_DATA StringToData(const std::string& str) { |
57 CSSM_DATA data = { | 57 CSSM_DATA data = { |
58 str.size(), | 58 str.size(), |
59 reinterpret_cast<uint8_t*>(const_cast<char*>(str.data())) | 59 reinterpret_cast<uint8_t*>(const_cast<char*>(str.data())) |
60 }; | 60 }; |
61 return data; | 61 return data; |
62 } | 62 } |
63 | 63 |
64 } // namespace | 64 } // namespace |
65 | 65 |
66 namespace base { | 66 namespace crypto { |
67 | 67 |
68 SymmetricKey::~SymmetricKey() {} | 68 SymmetricKey::~SymmetricKey() {} |
69 | 69 |
70 // static | 70 // static |
71 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, | 71 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
72 size_t key_size_in_bits) { | 72 size_t key_size_in_bits) { |
73 CheckKeyParams(algorithm, key_size_in_bits); | 73 CheckKeyParams(algorithm, key_size_in_bits); |
74 void* random_bytes = CreateRandomBytes((key_size_in_bits + 7) / 8); | 74 void* random_bytes = CreateRandomBytes((key_size_in_bits + 7) / 8); |
75 if (!random_bytes) | 75 if (!random_bytes) |
76 return NULL; | 76 return NULL; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 145 |
146 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 146 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
147 *raw_key = key_; | 147 *raw_key = key_; |
148 return true; | 148 return true; |
149 } | 149 } |
150 | 150 |
151 CSSM_DATA SymmetricKey::cssm_data() const { | 151 CSSM_DATA SymmetricKey::cssm_data() const { |
152 return StringToData(key_); | 152 return StringToData(key_); |
153 } | 153 } |
154 | 154 |
155 } // namespace base | 155 } // namespace crypto |
OLD | NEW |