]>
git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/Regressions/sc-150-backupkeyderivation.c
2 // sc-150-backupkeyderivation.c
10 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
12 * @APPLE_LICENSE_HEADER_START@
14 * This file contains Original Code and/or Modifications of Original Code
15 * as defined in and that are subject to the Apple Public Source License
16 * Version 2.0 (the 'License'). You may not use this file except in
17 * compliance with the License. Please obtain a copy of the License at
18 * http://www.opensource.apple.com/apsl/ and read it before using this
21 * The Original Code and all software distributed under the License are
22 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
23 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
24 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
26 * Please see the License for the specific language governing rights and
27 * limitations under the License.
29 * @APPLE_LICENSE_HEADER_END@
32 #include <AssertMacros.h>
34 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
35 #include "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
36 #include <utilities/SecCFWrappers.h>
37 #include <Security/SecRandom.h>
39 #include "SOSCircle_regressions.h"
40 #include "SOSRegressionUtilities.h"
41 #include "keychain/SecureObjectSync/SOSInternal.h"
46 static inline CFMutableDataRef
CFDataCreateMutableWithRandom(CFAllocatorRef allocator
, CFIndex size
) {
47 CFMutableDataRef result
= NULL
;
48 CFMutableDataRef data
= CFDataCreateMutableWithScratch(allocator
, size
);
50 require_quiet(errSecSuccess
== SecRandomCopyBytes(kSecRandomDefault
, size
, CFDataGetMutableBytePtr(data
)), fail
);
52 CFTransferRetained(result
, data
);
61 static const uint8_t sEntropy1
[] = { 0xc4, 0xb9, 0xa6, 0x6e, 0xeb, 0x56, 0xa1, 0x5c, 0x1d, 0x30, 0x09, 0x40,
62 0x41, 0xe9, 0x68, 0xb4, 0x12, 0xe0, 0xc6, 0x69, 0xfb, 0xdf, 0xcb, 0xe0,
63 0x27, 0x4b, 0x54, 0xf0, 0xdd, 0x62, 0x10, 0x78
66 static const uint8_t sEntropy2
[] = { 0xef, 0xbd, 0x72, 0x57, 0x02, 0xe6, 0xbd, 0x0a, 0x22, 0x6e, 0x77, 0x93,
67 0x17, 0xb3, 0x27, 0x12, 0x1b, 0x1f, 0xdf, 0xa0, 0x5b, 0xc6, 0x66, 0x54,
68 0x3a, 0x91, 0x0d, 0xc1, 0x5f, 0x57, 0x98, 0x44
71 static const uint8_t sEntropy3
[] = { 0xea, 0x06, 0x34, 0x93, 0xd7, 0x8b, 0xd6, 0x0d, 0xce, 0x83, 0x00 };
73 static void tests(void)
75 ccec_const_cp_t cp
= SOSGetBackupKeyCurveParameters();
76 CFErrorRef error
= NULL
;
77 CFDataRef entropy1
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, sEntropy1
, sizeof(sEntropy1
), kCFAllocatorNull
);
78 CFDataRef entropy2
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, sEntropy2
, sizeof(sEntropy2
), kCFAllocatorNull
);
79 CFDataRef entropy3
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, sEntropy3
, sizeof(sEntropy3
), kCFAllocatorNull
);
81 ccec_full_ctx_decl_cp(cp
, fullKey1
);
82 ccec_full_ctx_decl_cp(cp
, fullKey1a
);
83 ccec_full_ctx_decl_cp(cp
, fullKey2
);
84 ccec_full_ctx_decl_cp(cp
, fullKey3
);
86 ok(SOSGenerateDeviceBackupFullKey(fullKey1
, cp
, entropy1
, &error
), "Generate key 1 (%@)", error
);
89 ok(SOSGenerateDeviceBackupFullKey(fullKey1a
, cp
, entropy1
, &error
), "Generate key 1a (%@)", error
);
92 ok(SOSGenerateDeviceBackupFullKey(fullKey2
, cp
, entropy2
, &error
), "Generate key 2 (%@)", error
);
95 ok(SOSGenerateDeviceBackupFullKey(fullKey3
, cp
, entropy3
, &error
), "Generate key 3 (%@)", error
);
98 size_t ex_size
= ccec_x963_export_size(true, ccec_ctx_pub(fullKey1
));
99 uint8_t buf1
[ex_size
];
100 ccec_x963_export(true, buf1
, fullKey1
);
101 uint8_t buf1a
[ex_size
];
102 ccec_x963_export(true, buf1a
, fullKey1a
);
104 ok(0 == memcmp(buf1
, buf1a
, ex_size
), "Two derivations match");
106 CFDataRef publicKeyData
= SOSCopyDeviceBackupPublicKey(entropy1
, &error
);
107 ok(publicKeyData
, "Public key copy");
108 CFReleaseNull(error
);
110 CFReleaseNull(publicKeyData
);
111 CFReleaseNull(entropy1
);
112 CFReleaseNull(entropy2
);
113 CFReleaseNull(entropy3
);
118 int sc_150_backupkeyderivation(int argc
, char *const *argv
)