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 |
11 #include "base/crypto/cssm_init.h" | 11 #include "base/crypto/cssm_init.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 CSSM_KEY_TYPE CheckKeyParams(base::SymmetricKey::Algorithm algorithm, | 17 CSSM_KEY_TYPE CheckKeyParams(base::SymmetricKey::Algorithm algorithm, |
18 size_t key_size_in_bits) { | 18 size_t key_size_in_bits) { |
19 if (algorithm == base::SymmetricKey::AES) { | 19 if (algorithm == base::SymmetricKey::AES) { |
20 CHECK(key_size_in_bits == 128 || | 20 CHECK(key_size_in_bits == 128 || |
21 key_size_in_bits == 192 || | 21 key_size_in_bits == 192 || |
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 // FIPS 198 Section 3 requires the a SHA-1 derived key to be at least | |
wtc
2010/04/27 18:38:06
Nit: SHA-1 derived key => HMAC-SHA-1 key
| |
27 // (SHA-1 output size / 2) to be compliant. Since the ouput size of SHA-1 is | |
28 // 160 bits, we require at least 80 bits here. | |
26 CHECK(algorithm == base::SymmetricKey::HMAC_SHA1); | 29 CHECK(algorithm == base::SymmetricKey::HMAC_SHA1); |
27 CHECK(key_size_in_bits >= 64 && (key_size_in_bits % 8) == 0) | 30 CHECK(key_size_in_bits >= 80 && (key_size_in_bits % 8) == 0) |
28 << "Invalid key size " << key_size_in_bits << " bits"; | 31 << "Invalid key size " << key_size_in_bits << " bits"; |
29 return CSSM_ALGID_SHA1HMAC_LEGACY; | 32 return CSSM_ALGID_SHA1HMAC_LEGACY; |
30 } | 33 } |
31 } | 34 } |
32 | 35 |
33 void* CreateRandomBytes(size_t size) { | 36 void* CreateRandomBytes(size_t size) { |
34 CSSM_RETURN err; | 37 CSSM_RETURN err; |
35 CSSM_CC_HANDLE ctx; | 38 CSSM_CC_HANDLE ctx; |
36 err = CSSM_CSP_CreateRandomGenContext(base::GetSharedCSPHandle(), | 39 err = CSSM_CSP_CreateRandomGenContext(base::GetSharedCSPHandle(), |
37 CSSM_ALGID_APPLE_YARROW, | 40 CSSM_ALGID_APPLE_YARROW, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 141 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
139 *raw_key = key_; | 142 *raw_key = key_; |
140 return true; | 143 return true; |
141 } | 144 } |
142 | 145 |
143 CSSM_DATA SymmetricKey::cssm_data() const { | 146 CSSM_DATA SymmetricKey::cssm_data() const { |
144 return StringToData(key_); | 147 return StringToData(key_); |
145 } | 148 } |
146 | 149 |
147 } // namespace base | 150 } // namespace base |
OLD | NEW |