]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-150-backupkeyderivation.c
Security-58286.260.20.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Regressions / sc-150-backupkeyderivation.c
1 //
2 // sc-150-backupkeyderivation.c
3 // sec
4 //
5 //
6
7 #include <stdio.h>
8
9 /*
10 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
11 *
12 * @APPLE_LICENSE_HEADER_START@
13 *
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
19 * file.
20 *
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.
28 *
29 * @APPLE_LICENSE_HEADER_END@
30 */
31
32 #include <AssertMacros.h>
33
34 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
35 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
36 #include <utilities/SecCFWrappers.h>
37 #include <Security/SecRandom.h>
38
39 #include "SOSCircle_regressions.h"
40 #include "SOSRegressionUtilities.h"
41 #include "SOSInternal.h"
42
43 #if 0
44 static inline CFMutableDataRef CFDataCreateMutableWithRandom(CFAllocatorRef allocator, CFIndex size) {
45 CFMutableDataRef result = NULL;
46 CFMutableDataRef data = CFDataCreateMutableWithScratch(allocator, size);
47
48 require_quiet(errSecSuccess == SecRandomCopyBytes(kSecRandomDefault, size, CFDataGetMutableBytePtr(data)), fail);
49
50 CFTransferRetained(result, data);
51
52 fail:
53 CFReleaseNull(data);
54
55 return result;
56 }
57 #endif
58
59 static const uint8_t sEntropy1[] = { 0xc4, 0xb9, 0xa6, 0x6e, 0xeb, 0x56, 0xa1, 0x5c, 0x1d, 0x30, 0x09, 0x40,
60 0x41, 0xe9, 0x68, 0xb4, 0x12, 0xe0, 0xc6, 0x69, 0xfb, 0xdf, 0xcb, 0xe0,
61 0x27, 0x4b, 0x54, 0xf0, 0xdd, 0x62, 0x10, 0x78
62 };
63
64 static const uint8_t sEntropy2[] = { 0xef, 0xbd, 0x72, 0x57, 0x02, 0xe6, 0xbd, 0x0a, 0x22, 0x6e, 0x77, 0x93,
65 0x17, 0xb3, 0x27, 0x12, 0x1b, 0x1f, 0xdf, 0xa0, 0x5b, 0xc6, 0x66, 0x54,
66 0x3a, 0x91, 0x0d, 0xc1, 0x5f, 0x57, 0x98, 0x44
67 };
68
69 static const uint8_t sEntropy3[] = { 0xea, 0x06, 0x34, 0x93, 0xd7, 0x8b, 0xd6, 0x0d, 0xce, 0x83, 0x00 };
70
71
72 #define tests_count (6)
73 static void tests(void)
74 {
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);
80
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);
85
86 ok(SOSGenerateDeviceBackupFullKey(fullKey1, cp, entropy1, &error), "Generate key 1 (%@)", error);
87 CFReleaseNull(error);
88
89 ok(SOSGenerateDeviceBackupFullKey(fullKey1a, cp, entropy1, &error), "Generate key 1a (%@)", error);
90 CFReleaseNull(error);
91
92 ok(SOSGenerateDeviceBackupFullKey(fullKey2, cp, entropy2, &error), "Generate key 2 (%@)", error);
93 CFReleaseNull(error);
94
95 ok(SOSGenerateDeviceBackupFullKey(fullKey3, cp, entropy3, &error), "Generate key 3 (%@)", error);
96 CFReleaseNull(error);
97
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);
103
104 ok(0 == memcmp(buf1, buf1a, ex_size), "Two derivations match");
105
106 CFDataRef publicKeyData = SOSCopyDeviceBackupPublicKey(entropy1, &error);
107 ok(publicKeyData, "Public key copy");
108 CFReleaseNull(error);
109
110 CFReleaseNull(publicKeyData);
111 CFReleaseNull(entropy1);
112 CFReleaseNull(entropy2);
113 CFReleaseNull(entropy3);
114 }
115
116 static int kTestTestCount = tests_count;
117
118 int sc_150_backupkeyderivation(int argc, char *const *argv)
119 {
120 plan_tests(kTestTestCount);
121
122 tests();
123
124 return 0;
125 }