]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/otr/otr-packetdata.c
Security-57740.20.22.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / otr / otr-packetdata.c
1 /*
2 * Copyright (c) 2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include <CoreFoundation/CFData.h>
26 #include "SecOTRPacketData.h"
27
28 #include <corecrypto/ccn.h>
29
30 #include "security_regressions.h"
31
32 static bool CFDataMatches(CFDataRef data, size_t amount, const uint8_t* bytes)
33 {
34 if ((size_t) CFDataGetLength(data) != amount)
35 return false;
36
37 return 0 == memcmp(CFDataGetBytePtr(data), bytes, amount);
38
39 }
40
41
42 static void testAppendShort()
43 {
44 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
45
46 const uint8_t firstResult[] = { 1, 2 };
47 AppendShort(result, 0x0102);
48 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "Didn't insert correctly");
49
50 const uint8_t secondResult[] = { 1, 2, 3, 4};
51 AppendShort(result, 0x0304);
52 ok(CFDataMatches(result, sizeof(secondResult), secondResult), "Didn't append!");
53
54 CFRelease(result);
55
56 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
57
58 const uint8_t thirdResult[] = { 0, 0 };
59 AppendShort(result, 0x0);
60 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "Didn't insert zero");
61
62 CFRelease(result);
63
64 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
65
66 const uint8_t fourthResult[] = { 0xFF, 0xFF};
67 AppendShort(result, 0xFFFF);
68 ok(CFDataMatches(result, sizeof(fourthResult), fourthResult), "Didn't insert 0xFFFFFFFF");
69
70 CFRelease(result);
71 }
72
73 static void testAppendLong()
74 {
75 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
76
77 const uint8_t firstResult[] = { 1, 2, 3, 4 };
78 AppendLong(result, 0x01020304);
79 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "Didn't insert correctly");
80
81 const uint8_t secondResult[] = { 1, 2, 3, 4, 5, 6, 7, 8};
82 AppendLong(result, 0x05060708);
83 ok(CFDataMatches(result, sizeof(secondResult), secondResult), "Didn't append!");
84
85 CFRelease(result);
86
87 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
88
89 const uint8_t thirdResult[] = { 0, 0, 0, 0 };
90 AppendLong(result, 0x0);
91 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "Didn't insert zero");
92
93 CFRelease(result);
94
95 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
96
97 const uint8_t fourthResult[] = { 0xFF, 0xFF, 0xFF, 0xFF };
98 AppendLong(result, 0xFFFFFFFF);
99 ok(CFDataMatches(result, sizeof(fourthResult), fourthResult), "Didn't insert 0xFFFFFFFF");
100
101 CFRelease(result);
102 }
103
104 static void testAppendLongLong()
105 {
106 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
107
108 const uint8_t firstResult[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
109 AppendLongLong(result, 0x0102030405060708);
110 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "insert correctly");
111
112 const uint8_t secondResult[] = { 1, 2, 3, 4, 5, 6, 7, 8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x10};
113 AppendLongLong(result, 0x090A0B0C0D0E0F10);
114 ok(CFDataMatches(result, sizeof(secondResult), secondResult), "append!");
115
116 CFRelease(result);
117
118 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
119
120 const uint8_t thirdResult[] = { 0, 0, 0, 0, 0, 0, 0, 0};
121 AppendLongLong(result, 0x0);
122 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "insert zero");
123
124 CFRelease(result);
125
126 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
127
128 const uint8_t fourthResult[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
129 AppendLongLong(result, 0xFFFFFFFFFFFFFFFF);
130 ok(CFDataMatches(result, sizeof(fourthResult), fourthResult), "insert 0xFFFFFFFFFFFFFFF");
131
132 CFRelease(result);
133 }
134
135 static void testAppendLongLongCompact()
136 {
137 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
138
139 const uint64_t firstValue = 0x000001FC07F;
140 const uint8_t firstResult[] = { 0xFF,0x80,0x7F };
141 AppendLongLongCompact(result, firstValue);
142 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "insert correctly");
143
144 uint64_t readback;
145 const uint8_t *readPtr = firstResult;
146 size_t size = sizeof(firstResult);
147 ReadLongLongCompact(&readPtr, &size, &readback);
148
149 is(readback, firstValue, "read back");
150
151 const uint8_t secondResult[] = { 0xFF,0x80,0x7F, 0x82, 0x80, 0x81, 0x80, 0xff, 0x80, 0x7f };
152 AppendLongLongCompact(result, 0x800101FC07F);
153 ok(CFDataMatches(result, sizeof(secondResult), secondResult), "append!");
154
155 CFRelease(result);
156
157 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
158
159 const uint8_t thirdResult[] = { 0 };
160 AppendLongLongCompact(result, 0x0);
161 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "insert zero");
162
163 readPtr = thirdResult;
164 size = sizeof(thirdResult);
165 ReadLongLongCompact(&readPtr, &size, &readback);
166 is(readback, 0ULL, "read back");
167
168 CFRelease(result);
169
170 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
171
172 const uint8_t fourthResult[] = { 0x81, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F };
173 AppendLongLongCompact(result, 0xFFFFFFFFFFFFFFFF);
174 ok(CFDataMatches(result, sizeof(fourthResult), fourthResult), "insert 0xFFFFFFFFFFFFFFF");
175
176 readPtr = fourthResult;
177 size = sizeof(fourthResult);
178 ReadLongLongCompact(&readPtr, &size, &readback);
179 is(readback, 0xFFFFFFFFFFFFFFFF, "read back");
180
181 CFRelease(result);
182 }
183
184 static void testAppendData()
185 {
186 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
187
188 const uint8_t firstResult[] = { 0, 0, 0, 4, 1, 2, 3, 4 };
189 AppendDATA(result, sizeof(firstResult) - 4, firstResult + 4);
190 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "Didn't insert correctly");
191
192 const uint8_t secondResult[] = { 0, 0, 0, 4, 1, 2, 3, 4, 0, 0, 0, 1, 0 };
193 AppendDATA(result, sizeof(secondResult) - 12, secondResult + 12);
194 ok(CFDataMatches(result, sizeof(secondResult), secondResult), "Didn't append!");
195
196 CFRelease(result);
197
198 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
199
200 const uint8_t thirdResult[] = { 0, 0, 0, 2, 0, 0 };
201 AppendDATA(result, sizeof(thirdResult) - 4, thirdResult + 4);
202 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "Didn't insert correctly");
203
204 CFRelease(result);
205
206 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
207
208 const uint8_t fourthResult[] = { 0, 0, 0, 5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
209 AppendDATA(result, sizeof(fourthResult) - 4, fourthResult + 4);
210 ok(CFDataMatches(result, sizeof(fourthResult), fourthResult), "Didn't insert correctly");
211
212 CFRelease(result);
213 }
214
215
216 static void testAppendMPI()
217 {
218 const size_t kUnitBufferN = 1024;
219 cc_unit unitBuffer[1024];
220
221 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
222
223 const uint8_t firstResult[] = { 0, 0, 0, 2, 1, 2 };
224 ccn_read_uint(kUnitBufferN, unitBuffer, sizeof(firstResult) - 4, firstResult + 4);
225 AppendMPI(result, kUnitBufferN, unitBuffer);
226
227 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "Didn't insert zero");
228
229 const uint8_t secondResult[] = { 0, 0, 0, 2, 1, 2, 0, 0, 0, 3, 5, 6, 7 };
230 ccn_read_uint(kUnitBufferN, unitBuffer, sizeof(secondResult) - 10, secondResult + 10);
231 AppendMPI(result, kUnitBufferN, unitBuffer);
232
233 ok(CFDataMatches(result, sizeof(secondResult), secondResult), "Didn't append zero");
234
235 CFRelease(result);
236
237 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
238
239 const uint8_t thirdResult[] = { 0, 0, 0, 1, 1 };
240 ccn_read_uint(kUnitBufferN, unitBuffer, sizeof(thirdResult) - 4, thirdResult + 4);
241 AppendMPI(result, kUnitBufferN, unitBuffer);
242
243 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "Didn't insert zero");
244
245 CFRelease(result);
246
247 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
248
249 const uint8_t fourthResult[] = { 0, 0, 0, 7, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
250 ccn_read_uint(kUnitBufferN, unitBuffer, sizeof(fourthResult) - 4, fourthResult + 4);
251 AppendMPI(result, kUnitBufferN, unitBuffer);
252
253 ok(CFDataMatches(result, sizeof(fourthResult), fourthResult), "Didn't insert zero");
254
255 CFRelease(result);
256
257 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
258
259 const uint8_t paddedData[] = { 0, 0xCC, 0xDD, 0xEE, 0xFF, 0xAA };
260 const uint8_t shortenedResult[] = { 0, 0, 0, 5, 0xCC, 0xDD, 0xEE, 0xFF, 0xAA };
261 ccn_read_uint(kUnitBufferN, unitBuffer, sizeof(paddedData), paddedData);
262 AppendMPI(result, kUnitBufferN, unitBuffer);
263
264 ok(CFDataMatches(result, sizeof(shortenedResult), shortenedResult), "Didn't insert zero");
265
266 CFRelease(result);
267
268 }
269
270 int otr_packetdata(int argc, char *const * argv)
271 {
272 plan_tests(28);
273
274 testAppendShort();
275 testAppendLong();
276 testAppendLongLong();
277 testAppendLongLongCompact();
278 testAppendData();
279 testAppendMPI();
280
281 return 0;
282 }
283