]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-150-backupkeyderivation.c
Security-57337.50.23.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Regressions / sc-150-backupkeyderivation.c
1 //
2 // sc-150-backupkeyderivation.c
3 // sec
4 //
5 // Created by Mitch Adler on 4/7/15.
6 //
7 //
8
9 #include <stdio.h>
10
11 /*
12 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
13 *
14 * @APPLE_LICENSE_HEADER_START@
15 *
16 * This file contains Original Code and/or Modifications of Original Code
17 * as defined in and that are subject to the Apple Public Source License
18 * Version 2.0 (the 'License'). You may not use this file except in
19 * compliance with the License. Please obtain a copy of the License at
20 * http://www.opensource.apple.com/apsl/ and read it before using this
21 * file.
22 *
23 * The Original Code and all software distributed under the License are
24 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
25 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
26 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
28 * Please see the License for the specific language governing rights and
29 * limitations under the License.
30 *
31 * @APPLE_LICENSE_HEADER_END@
32 */
33
34 #include <AssertMacros.h>
35
36 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
37 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
38 #include <utilities/SecCFWrappers.h>
39 #include <Security/SecRandom.h>
40
41 #include "SOSCircle_regressions.h"
42 #include "SOSRegressionUtilities.h"
43 #include "SOSInternal.h"
44
45 #if 0
46 static inline CFMutableDataRef CFDataCreateMutableWithRandom(CFAllocatorRef allocator, CFIndex size) {
47 CFMutableDataRef result = NULL;
48 CFMutableDataRef data = CFDataCreateMutableWithScratch(allocator, size);
49
50 require_quiet(0 == SecRandomCopyBytes(kSecRandomDefault, size, CFDataGetMutableBytePtr(data)), fail);
51
52 CFTransferRetained(result, data);
53
54 fail:
55 CFReleaseNull(data);
56
57 return result;
58 }
59 #endif
60
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
64 };
65
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
69 };
70
71 static const uint8_t sEntropy3[] = { 0xea, 0x06, 0x34, 0x93, 0xd7, 0x8b, 0xd6, 0x0d, 0xce, 0x83, 0x00 };
72
73
74 #define tests_count (6)
75 static void tests(void)
76 {
77 ccec_const_cp_t cp = SOSGetBackupKeyCurveParameters();
78 CFErrorRef error = NULL;
79 CFDataRef entropy1 = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, sEntropy1, sizeof(sEntropy1), kCFAllocatorNull);
80 CFDataRef entropy2 = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, sEntropy2, sizeof(sEntropy2), kCFAllocatorNull);
81 CFDataRef entropy3 = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, sEntropy3, sizeof(sEntropy3), kCFAllocatorNull);
82
83 ccec_full_ctx_decl_cp(cp, fullKey1);
84 ccec_full_ctx_decl_cp(cp, fullKey1a);
85 ccec_full_ctx_decl_cp(cp, fullKey2);
86 ccec_full_ctx_decl_cp(cp, fullKey3);
87
88 ok(SOSGenerateDeviceBackupFullKey(fullKey1, cp, entropy1, &error), "Generate key 1 (%@)", error);
89 CFReleaseNull(error);
90
91 ok(SOSGenerateDeviceBackupFullKey(fullKey1a, cp, entropy1, &error), "Generate key 1a (%@)", error);
92 CFReleaseNull(error);
93
94 ok(SOSGenerateDeviceBackupFullKey(fullKey2, cp, entropy2, &error), "Generate key 2 (%@)", error);
95 CFReleaseNull(error);
96
97 ok(SOSGenerateDeviceBackupFullKey(fullKey3, cp, entropy3, &error), "Generate key 3 (%@)", error);
98 CFReleaseNull(error);
99
100 size_t comparisonSize = ccec_full_ctx_size(ccec_ccn_size(cp));
101
102 ok(memcmp(fullKey1, fullKey1a, comparisonSize), "Two derivations match");
103
104 CFDataRef publicKeyData = SOSCopyDeviceBackupPublicKey(entropy1, &error);
105 ok(publicKeyData, "Public key copy");
106 CFReleaseNull(error);
107
108 CFReleaseNull(publicKeyData);
109 CFReleaseNull(entropy1);
110 CFReleaseNull(entropy2);
111 CFReleaseNull(entropy3);
112 }
113
114 static int kTestTestCount = tests_count;
115
116 int sc_150_backupkeyderivation(int argc, char *const *argv)
117 {
118 plan_tests(kTestTestCount);
119
120 tests();
121
122 return 0;
123 }