2 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #define __KEYCHAINCORE__ 1
26 #include <Foundation/Foundation.h>
27 #include <Security/SecBase.h>
28 #include <Security/SecBasePriv.h>
29 #include <Security/SecCFAllocator.h>
30 #include <corecrypto/ccpbkdf2.h>
31 #include <corecrypto/ccsha2.h>
32 #include <corecrypto/ccaes.h>
33 #include <corecrypto/ccmode.h>
34 #include <corecrypto/ccwrap.h>
36 #include <utilities/SecCFWrappers.h>
37 #include <AssertMacros.h>
39 #include "SecEMCSPriv.h"
41 static CFStringRef kiDMSSalt = CFSTR("salt");
42 static CFStringRef kiDMSIterrations = CFSTR("iter");
43 static CFStringRef kiDMSWrapEMCSKey = CFSTR("wkey");
45 #define MIN_ITERATIONS 1000
46 #define MIN_SALTLEN 16
54 CopyWrappedKey(CFDataRef wrappingKey, CFDataRef unwrappedKey)
56 const struct ccmode_ecb *ecb_mode = ccaes_ecb_encrypt_mode();
57 ccecb_ctx_decl(ccecb_context_size(ecb_mode), key);
58 CFMutableDataRef wrappedKey = NULL;
60 require(CFDataGetLength(wrappingKey) == KEY_LENGTH, out);
62 ccecb_init(ecb_mode, key, CFDataGetLength(wrappingKey), CFDataGetBytePtr(wrappingKey));
64 wrappedKey = CFDataCreateMutableWithScratch(NULL, ccwrap_wrapped_size(CFDataGetLength(unwrappedKey)));
65 require(wrappingKey, out);
68 int wrap_status = ccwrap_auth_encrypt(ecb_mode, key, CFDataGetLength(unwrappedKey), CFDataGetBytePtr(unwrappedKey),
69 &obytes, CFDataGetMutableBytePtr(wrappedKey));
70 if (wrap_status == 0) {
71 assert(obytes == (size_t)CFDataGetLength(wrappedKey));
73 CFReleaseNull(wrappedKey);
78 ccecb_ctx_clear(ccecb_context_size(ecb_mode), key);
83 CopyUnwrappedKey(CFDataRef wrappingKey, CFDataRef wrappedKey)
85 const struct ccmode_ecb *ecb_mode = ccaes_ecb_decrypt_mode();
86 ccecb_ctx_decl(ccecb_context_size(ecb_mode), key);
87 NSMutableData *unwrappedKey = NULL;
89 require(CFDataGetLength(wrappedKey) >= CCWRAP_SEMIBLOCK, out);
90 require(CFDataGetLength(wrappingKey) == KEY_LENGTH, out);
92 ccecb_init(ecb_mode, key, CFDataGetLength(wrappingKey), CFDataGetBytePtr(wrappingKey));
94 unwrappedKey = CFBridgingRelease(CFDataCreateMutableWithScratch(SecCFAllocatorZeroize(), ccwrap_unwrapped_size(CFDataGetLength(wrappedKey))));
95 require(unwrappedKey, out);
98 int unwrap_status = ccwrap_auth_decrypt(ecb_mode, key, CFDataGetLength(wrappedKey), CFDataGetBytePtr(wrappedKey),
99 &obytes, [unwrappedKey mutableBytes]);
100 if (unwrap_status == 0) {
101 assert(obytes == (size_t)[unwrappedKey length]);
108 ccecb_ctx_clear(ccecb_context_size(ecb_mode), key);
117 CreateDerivedKey(CFDataRef salt, long iterations, NSString *managedCredential)
119 if (iterations < MIN_ITERATIONS || CFDataGetLength(salt) < MIN_SALTLEN)
123 * Assume users use the same normalization rules always
126 CFMutableDataRef key = CFDataCreateMutable(SecCFAllocatorZeroize(), KEY_LENGTH);
131 CFDataSetLength(key, KEY_LENGTH);
134 ret = ccpbkdf2_hmac(ccsha256_di(),
135 strlen(managedCredential.UTF8String), managedCredential.UTF8String,
136 CFDataGetLength(salt), CFDataGetBytePtr(salt),
138 KEY_LENGTH, CFDataGetMutableBytePtr(key));
148 * Given a dictionary stored in iDMS and a passcode, return a crypto key
152 SecEMCSCreateDerivedEMCSKey(NSDictionary *iDMSData, NSString *managedCredential, NSError **error)
154 CFDataRef userDerivedKey = NULL, emcsKey = NULL;
155 CFNumberRef number = NULL;
156 CFDataRef salt = NULL;
160 salt = CFDictionaryGetValue((__bridge CFDictionaryRef)iDMSData, kiDMSSalt);
161 number = CFDictionaryGetValue((__bridge CFDictionaryRef)iDMSData, kiDMSIterrations);
162 emcsKey = CFDictionaryGetValue((__bridge CFDictionaryRef)iDMSData, kiDMSWrapEMCSKey);
164 /* validate parameters */
165 if (!isData(salt) || !isNumber(number) || !isData(emcsKey))
168 if (!CFNumberGetValue(number, kCFNumberLongType, &iterations))
171 userDerivedKey = CreateDerivedKey(salt, iterations, managedCredential);
172 if (userDerivedKey == NULL)
175 key = CopyUnwrappedKey(userDerivedKey, emcsKey);
176 CFRelease(userDerivedKey);
182 * Return a dictionary to be stored in iDMS
186 SecEMCSCreateNewiDMSKey(NSDictionary *options,
188 NSString *managedCredential,
192 CFMutableDataRef salt = NULL;
193 const long iter = MIN_ITERATIONS;
194 CFDataRef wrappedEMCSKey = NULL;
195 CFMutableDataRef localEmcsKey = NULL;
196 CFNumberRef iterations = NULL;
197 CFDataRef userDerivedKey = NULL;
198 CFDictionaryRef key = NULL;
204 if (CFGetTypeID((__bridge CFTypeRef)(oldEMCSKey)) != CFDataGetTypeID())
206 if (CFDataGetLength((__bridge CFDataRef)oldEMCSKey) != KEY_LENGTH)
210 salt = CFDataCreateMutableWithScratch(NULL, MIN_SALTLEN);
214 if (SecRandomCopyBytes(NULL, CFDataGetLength(salt), CFDataGetMutableBytePtr(salt)) != 0)
218 iterations = CFNumberCreate(NULL, kCFNumberLongType, &iter);
219 if (iterations == NULL)
223 localEmcsKey = CFDataCreateMutableCopy(SecCFAllocatorZeroize(), 0, (__bridge CFDataRef)oldEMCSKey);
225 localEmcsKey = CFDataCreateMutableWithScratch(SecCFAllocatorZeroize(), KEY_LENGTH);
226 if (localEmcsKey == NULL)
228 if (SecRandomCopyBytes(NULL, CFDataGetLength(localEmcsKey), CFDataGetMutableBytePtr(localEmcsKey)) != 0)
232 userDerivedKey = CreateDerivedKey(salt, iter, managedCredential);
233 if (userDerivedKey == NULL)
236 wrappedEMCSKey = CopyWrappedKey(userDerivedKey, localEmcsKey);
237 CFRelease(userDerivedKey);
238 if (wrappedEMCSKey == NULL)
241 const void *keys[] = {
246 const void *values[] = {
251 _Static_assert(sizeof(keys)/sizeof(keys[0]) == sizeof(values)/sizeof(values[0]), "keys != values");
253 key = CFDictionaryCreate(NULL, keys, values, sizeof(keys)/sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
255 *emcsKey = CFRetain(localEmcsKey);
259 CFReleaseNull(iterations);
260 CFReleaseNull(localEmcsKey);
261 CFReleaseNull(wrappedEMCSKey);
263 return (__bridge NSDictionary *)key;