| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 key_size_in_bits == 256) | 22 key_size_in_bits == 256) |
| 23 << "Invalid key size " << key_size_in_bits << " bits"; | 23 << "Invalid key size " << key_size_in_bits << " bits"; |
| 24 return CSSM_ALGID_AES; | 24 return CSSM_ALGID_AES; |
| 25 } else { | 25 } else { |
| 26 CHECK(algorithm == base::SymmetricKey::HMAC_SHA1); | 26 CHECK(algorithm == base::SymmetricKey::HMAC_SHA1); |
| 27 CHECK(key_size_in_bits >= 80 && (key_size_in_bits % 8) == 0) | 27 CHECK(key_size_in_bits >= 80 && (key_size_in_bits % 8) == 0) |
| 28 << "Invalid key size " << key_size_in_bits << " bits"; | 28 << "Invalid key size " << key_size_in_bits << " bits"; |
| 29 return CSSM_ALGID_SHA1HMAC_LEGACY; | 29 return CSSM_ALGID_SHA1HMAC_LEGACY; |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 void* CreateRandomBytes(size_t size) { | 33 void* CreateRandomBytes(size_t size) { |
| 34 CSSM_RETURN err; | 34 CSSM_RETURN err; |
| 35 CSSM_CC_HANDLE ctx; | 35 CSSM_CC_HANDLE ctx; |
| 36 err = CSSM_CSP_CreateRandomGenContext(base::GetSharedCSPHandle(), | 36 err = CSSM_CSP_CreateRandomGenContext(base::GetSharedCSPHandle(), |
| 37 CSSM_ALGID_APPLE_YARROW, | 37 CSSM_ALGID_APPLE_YARROW, |
| 38 NULL, | 38 NULL, |
| 39 size, &ctx); | 39 size, &ctx); |
| 40 if (err) { | 40 if (err) { |
| 41 base::LogCSSMError("CSSM_CSP_CreateRandomGenContext", err); | 41 base::LogCSSMError("CSSM_CSP_CreateRandomGenContext", err); |
| 42 return NULL; | 42 return NULL; |
| 43 } | 43 } |
| 44 CSSM_DATA random_data = {}; | 44 CSSM_DATA random_data = {}; |
| 45 err = CSSM_GenerateRandom(ctx, &random_data); | 45 err = CSSM_GenerateRandom(ctx, &random_data); |
| 46 if (err) { | 46 if (err) { |
| 47 base::LogCSSMError("CSSM_GenerateRandom", err); | 47 base::LogCSSMError("CSSM_GenerateRandom", err); |
| 48 random_data.Data = NULL; | 48 random_data.Data = NULL; |
| 49 } | 49 } |
| 50 CSSM_DeleteContext(ctx); | 50 CSSM_DeleteContext(ctx); |
| 51 return random_data.Data; // Caller responsible for freeing this | 51 return random_data.Data; // Caller responsible for freeing this |
| 52 } | 52 } |
| 53 | 53 |
| 54 inline CSSM_DATA StringToData(const std::string& str) { | 54 inline CSSM_DATA StringToData(const std::string& str) { |
| 55 CSSM_DATA data = { | 55 CSSM_DATA data = { |
| 56 str.size(), | 56 str.size(), |
| 57 reinterpret_cast<uint8_t*>(const_cast<char*>(str.data())) | 57 reinterpret_cast<uint8_t*>(const_cast<char*>(str.data())) |
| 58 }; | 58 }; |
| 59 return data; | 59 return data; |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 namespace base { | 64 namespace base { |
| 65 | 65 |
| 66 SymmetricKey::~SymmetricKey() {} |
| 67 |
| 66 // static | 68 // static |
| 67 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, | 69 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
| 68 size_t key_size_in_bits) { | 70 size_t key_size_in_bits) { |
| 69 CheckKeyParams(algorithm, key_size_in_bits); | 71 CheckKeyParams(algorithm, key_size_in_bits); |
| 70 void* random_bytes = CreateRandomBytes((key_size_in_bits + 7) / 8); | 72 void* random_bytes = CreateRandomBytes((key_size_in_bits + 7) / 8); |
| 71 if (!random_bytes) | 73 if (!random_bytes) |
| 72 return NULL; | 74 return NULL; |
| 73 SymmetricKey *key = new SymmetricKey(random_bytes, key_size_in_bits); | 75 SymmetricKey *key = new SymmetricKey(random_bytes, key_size_in_bits); |
| 74 free(random_bytes); | 76 free(random_bytes); |
| 75 return key; | 77 return key; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 NULL, | 118 NULL, |
| 117 &cssm_key); | 119 &cssm_key); |
| 118 if (err) { | 120 if (err) { |
| 119 LogCSSMError("CSSM_DeriveKey", err); | 121 LogCSSMError("CSSM_DeriveKey", err); |
| 120 goto exit; | 122 goto exit; |
| 121 } | 123 } |
| 122 | 124 |
| 123 DCHECK_EQ(cssm_key.KeyData.Length, key_size_in_bits / 8); | 125 DCHECK_EQ(cssm_key.KeyData.Length, key_size_in_bits / 8); |
| 124 derived_key = new SymmetricKey(cssm_key.KeyData.Data, key_size_in_bits); | 126 derived_key = new SymmetricKey(cssm_key.KeyData.Data, key_size_in_bits); |
| 125 | 127 |
| 126 exit: | 128 exit: |
| 127 CSSM_DeleteContext(ctx); | 129 CSSM_DeleteContext(ctx); |
| 128 CSSM_FreeKey(GetSharedCSPHandle(), &credentials, &cssm_key, false); | 130 CSSM_FreeKey(GetSharedCSPHandle(), &credentials, &cssm_key, false); |
| 129 return derived_key; | 131 return derived_key; |
| 130 } | 132 } |
| 131 | 133 |
| 132 SymmetricKey::SymmetricKey(const void *key_data, size_t key_size_in_bits) | 134 SymmetricKey::SymmetricKey(const void *key_data, size_t key_size_in_bits) |
| 133 : key_(reinterpret_cast<const char*>(key_data), | 135 : key_(reinterpret_cast<const char*>(key_data), |
| 134 key_size_in_bits / 8) {} | 136 key_size_in_bits / 8) {} |
| 135 | 137 |
| 136 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 138 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
| 137 *raw_key = key_; | 139 *raw_key = key_; |
| 138 return true; | 140 return true; |
| 139 } | 141 } |
| 140 | 142 |
| 141 CSSM_DATA SymmetricKey::cssm_data() const { | 143 CSSM_DATA SymmetricKey::cssm_data() const { |
| 142 return StringToData(key_); | 144 return StringToData(key_); |
| 143 } | 145 } |
| 144 | 146 |
| 145 } // namespace base | 147 } // namespace base |
| OLD | NEW |