2 * Copyright (c) 2013-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@
25 #include <Security/SecureObjectSync/SOSDigestVector.h>
26 #include <utilities/SecCFError.h>
27 #include <utilities/SecCFWrappers.h>
28 #include <dispatch/dispatch.h>
31 CFStringRef kSOSDigestVectorErrorDomain
= CFSTR("com.apple.security.sos.digestvector.error");
33 /* SOSDigestVector code. */
35 const size_t kMaxDVCapacity
= (1024*1024L); // roughly based on KVS limit, helps avoid integer overflow issues
37 static bool SOSDigestVectorEnsureCapacity(struct SOSDigestVector
*dv
, size_t count
) {
38 // Note that capacity is the number of digests it can hold, not the size in bytes
39 // Already big enough.
40 if (count
<= dv
->capacity
)
43 if (count
> kMaxDVCapacity
) {
44 secerror("Requesting too much space for digest vectors: %ld", count
);
48 size_t capacity
= (dv
->capacity
+ 16) * 3 / 2;
49 size_t digestSize
= sizeof(*(dv
->digest
));
52 dv
->digest
= reallocf(dv
->digest
, digestSize
* capacity
);
53 if (dv
->digest
== NULL
) {
55 secerror("reallocf failed requesting space for digest vectors: %ld (bytes)", digestSize
* capacity
);
58 dv
->capacity
= capacity
;
62 static void SOSDigestVectorAppendOrdered(struct SOSDigestVector
*dv
, const uint8_t *digest
)
64 if (SOSDigestVectorEnsureCapacity(dv
, dv
->count
+ 1))
65 memcpy(dv
->digest
[dv
->count
++], digest
, SOSDigestSize
);
68 void SOSDigestVectorAppend(struct SOSDigestVector
*dv
, const uint8_t *digest
)
70 SOSDigestVectorAppendOrdered(dv
, digest
);
74 static int SOSDigestCompare(const void *a
, const void *b
)
76 return memcmp(a
, b
, SOSDigestSize
);
79 // Remove duplicates from sorted manifest using minimal memmove() calls
80 static void SOSDigestVectorUnique(struct SOSDigestVector
*dv
) {
81 if (dv
->count
< 2 || dv
->digest
== NULL
)
84 const uint8_t *prev
= dv
->digest
[0];
85 uint8_t *dest
= dv
->digest
[1];
86 const uint8_t *end
= dv
->digest
[dv
->count
];
87 const uint8_t *source
= dest
;
88 for (const uint8_t *cur
= source
; cur
< end
; cur
+= SOSDigestSize
) {
89 int delta
= SOSDigestCompare(prev
, cur
);
91 // Found a properly sorted element
92 // 1) Extend the current region (prev is end of region pointer)
95 } else if (delta
> 0) {
96 // DigestVector wasn't sorted!
99 // Found a duplicate element
100 // 1) Finish copy for current region up to previous element
101 prev
+= SOSDigestSize
;
103 memmove(dest
, source
, prev
- source
);
104 dest
+= prev
- source
;
105 // 2) Skip remaining dupes
107 cur
+= SOSDigestSize
;
109 int delta
= SOSDigestCompare(prev
, cur
);
114 cur
+= SOSDigestSize
;
117 // cur now points to the first new element that hasn't yet been copied
118 // 3) Set start of next region
124 // Copy remainder of final region
126 prev
+= SOSDigestSize
;
128 memmove(dest
, source
, prev
- source
);
129 dest
+= prev
- source
;
131 dv
->count
= (dest
- dv
->digest
[0]) / SOSDigestSize
;
135 void SOSDigestVectorSort(struct SOSDigestVector
*dv
)
137 if (dv
->unsorted
&& dv
->digest
) {
138 qsort(dv
->digest
, dv
->count
, sizeof(*dv
->digest
), SOSDigestCompare
);
139 dv
->unsorted
= false;
140 SOSDigestVectorUnique(dv
);
144 void SOSDigestVectorUniqueSorted(struct SOSDigestVector
*dv
)
146 // Uniqify in place (sort does this now for safety)
148 SOSDigestVectorSort(dv
);
151 void SOSDigestVectorSwap(struct SOSDigestVector
*dva
, struct SOSDigestVector
*dvb
)
153 struct SOSDigestVector dv
;
159 bool SOSDigestVectorContainsSorted(const struct SOSDigestVector
*dv
, const uint8_t *digest
)
161 return SOSDigestVectorIndexOfSorted(dv
, digest
) != (size_t)-1;
164 bool SOSDigestVectorContains(struct SOSDigestVector
*dv
, const uint8_t *digest
)
167 SOSDigestVectorSort(dv
);
168 return SOSDigestVectorContainsSorted(dv
, digest
);
171 size_t SOSDigestVectorIndexOfSorted(const struct SOSDigestVector
*dv
, const uint8_t *digest
)
174 const void *pos
= bsearch(digest
, dv
->digest
, dv
->count
, sizeof(*dv
->digest
), SOSDigestCompare
);
175 return pos
? ((size_t)(pos
- (void *)dv
->digest
)) / SOSDigestSize
: ((size_t)-1);
181 size_t SOSDigestVectorIndexOf(struct SOSDigestVector
*dv
, const uint8_t *digest
)
184 SOSDigestVectorSort(dv
);
185 return SOSDigestVectorIndexOfSorted(dv
, digest
);
188 void SOSDigestVectorFree(struct SOSDigestVector
*dv
)
194 dv
->unsorted
= false;
197 void SOSDigestVectorApplySorted(const struct SOSDigestVector
*dv
, SOSDigestVectorApplyBlock with
)
200 for (size_t ix
= 0; !stop
&& ix
< dv
->count
&& dv
->digest
; ++ix
) {
201 with(dv
->digest
[ix
], &stop
);
205 void SOSDigestVectorApply(struct SOSDigestVector
*dv
, SOSDigestVectorApplyBlock with
)
208 SOSDigestVectorSort(dv
);
209 SOSDigestVectorApplySorted(dv
, with
);
212 // TODO: Check for NDEBUG to disable skip dupes are release time.
213 //#define SOSDVSKIPDUPES 0
214 #define SOSDVSKIPDUPES 1
217 #define SOSDVINCRIX(dv,ix) (SOSDigestVectorIncrementAndSkipDupes(dv,ix))
219 static size_t SOSIncrementAndSkipDupes(const uint8_t *digests
, size_t count
, const size_t ix
) {
221 if (digests
&& new_ix
< count
) {
222 while (++new_ix
< count
) {
223 int delta
= SOSDigestCompare(digests
+ ix
* SOSDigestSize
, digests
+ new_ix
* SOSDigestSize
);
232 static size_t SOSDigestVectorIncrementAndSkipDupes(const struct SOSDigestVector
*dv
, const size_t ix
) {
233 return SOSIncrementAndSkipDupes((const uint8_t *)dv
->digest
, dv
->count
, ix
);
236 void SOSDigestVectorAppendMultipleOrdered(struct SOSDigestVector
*dv
,
237 size_t count
, const uint8_t *digests
) {
240 SOSDigestVectorAppendOrdered(dv
, digests
+ (ix
* SOSDigestSize
));
241 ix
= SOSIncrementAndSkipDupes(digests
, count
, ix
);
245 #else /* !SOSDVSKIPDUPES */
247 #define SOSDVINCRIX(dv,ix) (ix + 1)
249 void SOSDigestVectorAppendMultipleOrdered(struct SOSDigestVector
*dv
,
250 size_t count
, const uint8_t *digests
) {
252 if (SOSDigestVectorEnsureCapacity(dv
, dv
->count
+ count
))
253 memcpy(dv
->digest
[dv
->count
], digests
, count
* SOSDigestSize
);
258 #endif /* !SOSDVSKIPDUPES */
260 void SOSDigestVectorIntersectSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
261 struct SOSDigestVector
*dvintersect
)
263 /* dvintersect should be empty to start. */
264 assert(dvintersect
->count
== 0);
265 size_t i1
= 0, i2
= 0;
266 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
267 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
269 SOSDigestVectorAppendOrdered(dvintersect
, dv1
->digest
[i1
]);
270 i1
= SOSDVINCRIX(dv1
, i1
);
271 i2
= SOSDVINCRIX(dv2
, i2
);
272 } else if (delta
< 0) {
273 i1
= SOSDVINCRIX(dv1
, i1
);
275 i2
= SOSDVINCRIX(dv2
, i2
);
280 void SOSDigestVectorUnionSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
281 struct SOSDigestVector
*dvunion
)
283 /* dvunion should be empty to start. */
284 assert(dvunion
->count
== 0);
285 size_t i1
= 0, i2
= 0;
286 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
287 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
289 SOSDigestVectorAppendOrdered(dvunion
, dv1
->digest
[i1
]);
290 i1
= SOSDVINCRIX(dv1
, i1
);
291 i2
= SOSDVINCRIX(dv2
, i2
);
292 } else if (delta
< 0) {
293 SOSDigestVectorAppendOrdered(dvunion
, dv1
->digest
[i1
]);
294 i1
= SOSDVINCRIX(dv1
, i1
);
296 SOSDigestVectorAppendOrdered(dvunion
, dv2
->digest
[i2
]);
297 i2
= SOSDVINCRIX(dv2
, i2
);
300 SOSDigestVectorAppendMultipleOrdered(dvunion
, dv1
->count
- i1
, dv1
->digest
[i1
]);
301 SOSDigestVectorAppendMultipleOrdered(dvunion
, dv2
->count
- i2
, dv2
->digest
[i2
]);
304 void SOSDigestVectorDiffSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
305 struct SOSDigestVector
*dv1_2
, struct SOSDigestVector
*dv2_1
)
307 /* dv1_2 and dv2_1 should be empty to start. */
308 assert(dv1_2
->count
== 0);
309 assert(dv2_1
->count
== 0);
311 size_t i1
= 0, i2
= 0;
312 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
313 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
315 i1
= SOSDVINCRIX(dv1
, i1
);
316 i2
= SOSDVINCRIX(dv2
, i2
);
317 } else if (delta
< 0) {
318 SOSDigestVectorAppendOrdered(dv1_2
, dv1
->digest
[i1
]);
319 i1
= SOSDVINCRIX(dv1
, i1
);
321 SOSDigestVectorAppendOrdered(dv2_1
, dv2
->digest
[i2
]);
322 i2
= SOSDVINCRIX(dv2
, i2
);
325 SOSDigestVectorAppendMultipleOrdered(dv1_2
, dv1
->count
- i1
, dv1
->digest
[i1
]);
326 SOSDigestVectorAppendMultipleOrdered(dv2_1
, dv2
->count
- i2
, dv2
->digest
[i2
]);
329 void SOSDigestVectorDiff(struct SOSDigestVector
*dv1
, struct SOSDigestVector
*dv2
,
330 struct SOSDigestVector
*dv1_2
, struct SOSDigestVector
*dv2_1
)
332 if (dv1
->unsorted
) SOSDigestVectorSort(dv1
);
333 if (dv2
->unsorted
) SOSDigestVectorSort(dv2
);
334 SOSDigestVectorDiffSorted(dv1
, dv2
, dv1_2
, dv2_1
);
338 If A and B are sets, then the relative complement of A in B, also termed the set-theoretic difference of B and A,
339 is the set of elements in B, but not in A. The relative complement of A in B is denoted B ∖ A according to the ISO 31-11 standard
340 sometimes written B − A
342 The common case for us will be Removals\Additions
345 static void SOSDigestVectorAppendComplementAtIndex(size_t a_ix
, const struct SOSDigestVector
*dvA
, size_t b_ix
, const struct SOSDigestVector
*dvB
,
346 struct SOSDigestVector
*dvcomplement
)
348 assert(a_ix
<= dvA
->count
&& b_ix
<= dvB
->count
);
349 while (a_ix
< dvA
->count
&& b_ix
< dvB
->count
&& dvA
->digest
&& dvB
->digest
) {
350 int delta
= SOSDigestCompare(dvA
->digest
[a_ix
], dvB
->digest
[b_ix
]);
352 a_ix
= SOSDVINCRIX(dvA
, a_ix
);
353 b_ix
= SOSDVINCRIX(dvB
, b_ix
);
354 } else if (delta
< 0) {
355 a_ix
= SOSDVINCRIX(dvA
, a_ix
);
357 SOSDigestVectorAppendOrdered(dvcomplement
, dvB
->digest
[b_ix
]);
358 b_ix
= SOSDVINCRIX(dvB
, b_ix
);
362 SOSDigestVectorAppendMultipleOrdered(dvcomplement
, dvB
->count
- b_ix
, dvB
->digest
[b_ix
]);
366 void SOSDigestVectorComplementSorted(const struct SOSDigestVector
*dvA
, const struct SOSDigestVector
*dvB
,
367 struct SOSDigestVector
*dvcomplement
)
369 /* dvcomplement should be empty to start. */
370 assert(dvcomplement
->count
== 0);
371 assert(!dvA
->unsorted
);
372 assert(!dvB
->unsorted
);
374 SOSDigestVectorAppendComplementAtIndex(0, dvA
, 0, dvB
, dvcomplement
);
379 For each item in base
381 one way to do would be to define SOSDigestVectorComplementSorted
383 For removals, if removal value is less than base, increment until GEQ
385 bool SOSDigestVectorPatchSorted(const struct SOSDigestVector
*base
, const struct SOSDigestVector
*removals
,
386 const struct SOSDigestVector
*additions
, struct SOSDigestVector
*dv
,
389 /* dv should be empty to start. */
390 assert(dv
->count
== 0);
391 assert(!base
->unsorted
);
392 assert(!removals
->unsorted
);
393 assert(!additions
->unsorted
);
395 size_t i1
= 0, i2
= 0, i3
= 0;
396 while (i1
< base
->count
&& i2
< additions
->count
) {
397 // Pick the smaller of base->digest[i1] and additions->digest[i2] as a
398 // candidate to be put into the output vector. If udelta positive, addition is smaller
399 int udelta
= SOSDigestCompare(base
->digest
[i1
], additions
->digest
[i2
]);
400 const uint8_t *candidate
= udelta
< 0 ? base
->digest
[i1
] : additions
->digest
[i2
];
402 // ddelta > 0 means rem > candidate
404 while (i3
< removals
->count
) {
405 ddelta
= SOSDigestCompare(removals
->digest
[i3
], candidate
);
407 i3
= SOSDVINCRIX(removals
, i3
);
410 i3
= SOSDVINCRIX(removals
, i3
);
415 SOSDigestVectorAppendOrdered(dv
, candidate
);
417 // Point to next (different) candidate
419 i1
= SOSDVINCRIX(base
, i1
);
420 i2
= SOSDVINCRIX(additions
, i2
);
421 } else if (udelta
< 0) {
422 i1
= SOSDVINCRIX(base
, i1
);
424 i2
= SOSDVINCRIX(additions
, i2
);
427 SOSDigestVectorAppendComplementAtIndex(i3
, removals
, i1
, base
, dv
);
428 SOSDigestVectorAppendComplementAtIndex(i3
, removals
, i2
, additions
, dv
);
433 bool SOSDigestVectorPatch(struct SOSDigestVector
*base
, struct SOSDigestVector
*removals
,
434 struct SOSDigestVector
*additions
, struct SOSDigestVector
*dv
,
437 if (base
->unsorted
) SOSDigestVectorSort(base
);
438 if (removals
->unsorted
) SOSDigestVectorSort(removals
);
439 if (additions
->unsorted
) SOSDigestVectorSort(additions
);
440 return SOSDigestVectorPatchSorted(base
, removals
, additions
, dv
, error
);