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 "base/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" | 11 #include "base/crypto/cssm_init.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
25 // FIPS 198 Section 3 requires a HMAC-SHA-1 derived keys to be at least | 25 // FIPS 198 Section 3 requires a HMAC-SHA-1 derived keys to be at least |
26 // (HMAC-SHA-1 output size / 2) to be compliant. Since the ouput size of | 26 // (HMAC-SHA-1 output size / 2) to be compliant. Since the ouput size of |
27 // HMAC-SHA-1 is 160 bits, we require at least 80 bits here. | 27 // HMAC-SHA-1 is 160 bits, we require at least 80 bits here. |
28 CHECK(algorithm == base::SymmetricKey::HMAC_SHA1); | 28 CHECK(algorithm == base::SymmetricKey::HMAC_SHA1); |
29 CHECK(key_size_in_bits >= 80 && (key_size_in_bits % 8) == 0) | 29 CHECK(key_size_in_bits >= 80 && (key_size_in_bits % 8) == 0) |
30 << "Invalid key size " << key_size_in_bits << " bits"; | 30 << "Invalid key size " << key_size_in_bits << " bits"; |
31 return CSSM_ALGID_SHA1HMAC_LEGACY; | 31 return CSSM_ALGID_SHA1HMAC_LEGACY; |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 void* CreateRandomBytes(size_t size) { | 35 StringToData(const std::string& str) { |
36 CSSM_DATA data = { | |
37 str.size(), | |
38 reinterpret_cast<uint8_t*>(const_cast<char*>(str.data())) | |
39 }; | |
40 return data; | |
41 } | |
42 | |
43 } // namespace | |
44 | |
45 namespace base { | |
46 | |
47 SymmetricKey::~SymmetricKey() {} | |
48 | |
49 // static | |
50 bool SymmetricKey::GenerateRandomBytes(size_t num_bytes, uint8* out) { | |
51 if (num_bytes == 0) | |
52 return true; | |
53 if (out == NULL) | |
54 return false; | |
55 | |
36 CSSM_RETURN err; | 56 CSSM_RETURN err; |
37 CSSM_CC_HANDLE ctx; | 57 CSSM_CC_HANDLE ctx; |
38 err = CSSM_CSP_CreateRandomGenContext(base::GetSharedCSPHandle(), | 58 err = CSSM_CSP_CreateRandomGenContext(base::GetSharedCSPHandle(), |
39 CSSM_ALGID_APPLE_YARROW, | 59 CSSM_ALGID_APPLE_YARROW, |
40 NULL, | 60 NULL, |
41 size, &ctx); | 61 num_bytes, &ctx); |
42 if (err) { | 62 if (err) { |
43 base::LogCSSMError("CSSM_CSP_CreateRandomGenContext", err); | 63 base::LogCSSMError("CSSM_CSP_CreateRandomGenContext", err); |
44 return NULL; | 64 return NULL; |
45 } | 65 } |
46 CSSM_DATA random_data = {}; | 66 CSSM_DATA random_data = {}; |
47 err = CSSM_GenerateRandom(ctx, &random_data); | 67 err = CSSM_GenerateRandom(ctx, &random_data); |
48 if (err) { | 68 if (err) { |
49 base::LogCSSMError("CSSM_GenerateRandom", err); | 69 base::LogCSSMError("CSSM_GenerateRandom", err); |
50 random_data.Data = NULL; | 70 random_data.Data = NULL; |
51 } | 71 } |
52 CSSM_DeleteContext(ctx); | 72 CSSM_DeleteContext(ctx); |
53 return random_data.Data; // Caller responsible for freeing this | 73 std::copy(random_data.Data, random_data.Data + num_bytes, out); |
zel
2011/03/29 05:31:34
you should avoid data copy here
change the functi
Denis Lagno
2011/04/04 18:18:02
mac was the only platform that performed this copy
| |
74 delete random_data.Data; | |
75 return true; | |
54 } | 76 } |
55 | 77 |
56 inline CSSM_DATA StringToData(const std::string& str) { | |
57 CSSM_DATA data = { | |
58 str.size(), | |
59 reinterpret_cast<uint8_t*>(const_cast<char*>(str.data())) | |
60 }; | |
61 return data; | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 namespace base { | |
67 | |
68 SymmetricKey::~SymmetricKey() {} | |
69 | |
70 // static | 78 // static |
71 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, | 79 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
72 size_t key_size_in_bits) { | 80 size_t key_size_in_bits) { |
73 CheckKeyParams(algorithm, key_size_in_bits); | 81 CheckKeyParams(algorithm, key_size_in_bits); |
74 void* random_bytes = CreateRandomBytes((key_size_in_bits + 7) / 8); | 82 std::vector<uint8> random_bytes((key_size_in_bits + 7) / 8); |
75 if (!random_bytes) | 83 if (!GenerateRandomBytes(random_bytes.size(), &random_bytes[0])) |
76 return NULL; | 84 return NULL; |
77 SymmetricKey *key = new SymmetricKey(random_bytes, key_size_in_bits); | 85 SymmetricKey *key = new SymmetricKey(&random_bytes[0], key_size_in_bits); |
78 free(random_bytes); | 86 std::fill(random_bytes.begin(), random_bytes.end(), 0u); |
zel
2011/03/29 05:31:34
why cleaning here? the data is already in memory s
Denis Lagno
2011/04/04 18:18:02
yes, in memory somewhere else. But looking into i
| |
79 return key; | 87 return key; |
80 } | 88 } |
81 | 89 |
82 // static | 90 // static |
83 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, | 91 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, |
84 const std::string& password, | 92 const std::string& password, |
85 const std::string& salt, | 93 const std::string& salt, |
86 size_t iterations, | 94 size_t iterations, |
87 size_t key_size_in_bits) { | 95 size_t key_size_in_bits) { |
88 // Derived (haha) from cdsaDeriveKey() in Apple's CryptoSample. | 96 // Derived (haha) from cdsaDeriveKey() in Apple's CryptoSample. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 154 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
147 *raw_key = key_; | 155 *raw_key = key_; |
148 return true; | 156 return true; |
149 } | 157 } |
150 | 158 |
151 CSSM_DATA SymmetricKey::cssm_data() const { | 159 CSSM_DATA SymmetricKey::cssm_data() const { |
152 return StringToData(key_); | 160 return StringToData(key_); |
153 } | 161 } |
154 | 162 |
155 } // namespace base | 163 } // namespace base |
OLD | NEW |