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 (digest
&& SOSDigestVectorEnsureCapacity(dv
, dv
->count
+ 1)) {
65 memcpy(dv
->digest
[dv
->count
++], digest
, SOSDigestSize
);
69 void SOSDigestVectorAppend(struct SOSDigestVector
*dv
, const uint8_t *digest
)
71 SOSDigestVectorAppendOrdered(dv
, digest
);
75 static int SOSDigestCompare(const void *a
, const void *b
)
77 return memcmp(a
, b
, SOSDigestSize
);
80 // Remove duplicates from sorted manifest using minimal memmove() calls
81 static void SOSDigestVectorUnique(struct SOSDigestVector
*dv
) {
82 if (dv
->count
< 2 || dv
->digest
== NULL
)
85 const uint8_t *prev
= dv
->digest
[0];
86 uint8_t *dest
= dv
->digest
[1];
87 const uint8_t *end
= dv
->digest
[dv
->count
];
88 const uint8_t *source
= dest
;
89 for (const uint8_t *cur
= source
; cur
< end
; cur
+= SOSDigestSize
) {
90 int delta
= SOSDigestCompare(prev
, cur
);
92 // Found a properly sorted element
93 // 1) Extend the current region (prev is end of region pointer)
96 } else if (delta
> 0) {
97 // DigestVector wasn't sorted!
100 // Found a duplicate element
101 // 1) Finish copy for current region up to previous element
102 prev
+= SOSDigestSize
;
104 memmove(dest
, source
, prev
- source
);
105 dest
+= prev
- source
;
106 // 2) Skip remaining dupes
108 cur
+= SOSDigestSize
;
110 int delta
= SOSDigestCompare(prev
, cur
);
115 cur
+= SOSDigestSize
;
118 // cur now points to the first new element that hasn't yet been copied
119 // 3) Set start of next region
125 // Copy remainder of final region
127 prev
+= SOSDigestSize
;
129 memmove(dest
, source
, prev
- source
);
130 dest
+= prev
- source
;
132 dv
->count
= (dest
- dv
->digest
[0]) / SOSDigestSize
;
136 void SOSDigestVectorSort(struct SOSDigestVector
*dv
)
138 if (dv
->unsorted
&& dv
->digest
) {
139 qsort(dv
->digest
, dv
->count
, sizeof(*dv
->digest
), SOSDigestCompare
);
140 dv
->unsorted
= false;
141 SOSDigestVectorUnique(dv
);
145 void SOSDigestVectorUniqueSorted(struct SOSDigestVector
*dv
)
147 // Uniqify in place (sort does this now for safety)
149 SOSDigestVectorSort(dv
);
152 void SOSDigestVectorSwap(struct SOSDigestVector
*dva
, struct SOSDigestVector
*dvb
)
154 struct SOSDigestVector dv
;
160 bool SOSDigestVectorContainsSorted(const struct SOSDigestVector
*dv
, const uint8_t *digest
)
162 return SOSDigestVectorIndexOfSorted(dv
, digest
) != (size_t)-1;
165 bool SOSDigestVectorContains(struct SOSDigestVector
*dv
, const uint8_t *digest
)
168 SOSDigestVectorSort(dv
);
169 return SOSDigestVectorContainsSorted(dv
, digest
);
172 size_t SOSDigestVectorIndexOfSorted(const struct SOSDigestVector
*dv
, const uint8_t *digest
)
175 const void *pos
= bsearch(digest
, dv
->digest
, dv
->count
, sizeof(*dv
->digest
), SOSDigestCompare
);
176 return pos
? ((size_t)(pos
- (void *)dv
->digest
)) / SOSDigestSize
: ((size_t)-1);
182 size_t SOSDigestVectorIndexOf(struct SOSDigestVector
*dv
, const uint8_t *digest
)
185 SOSDigestVectorSort(dv
);
186 return SOSDigestVectorIndexOfSorted(dv
, digest
);
189 void SOSDigestVectorFree(struct SOSDigestVector
*dv
)
195 dv
->unsorted
= false;
198 void SOSDigestVectorApplySorted(const struct SOSDigestVector
*dv
, SOSDigestVectorApplyBlock with
)
201 for (size_t ix
= 0; !stop
&& ix
< dv
->count
&& dv
->digest
; ++ix
) {
202 with(dv
->digest
[ix
], &stop
);
206 void SOSDigestVectorApply(struct SOSDigestVector
*dv
, SOSDigestVectorApplyBlock with
)
209 SOSDigestVectorSort(dv
);
210 SOSDigestVectorApplySorted(dv
, with
);
213 // TODO: Check for NDEBUG to disable skip dupes are release time.
214 //#define SOSDVSKIPDUPES 0
215 #define SOSDVSKIPDUPES 1
218 #define SOSDVINCRIX(dv,ix) (SOSDigestVectorIncrementAndSkipDupes(dv,ix))
220 static size_t SOSIncrementAndSkipDupes(const uint8_t *digests
, size_t count
, const size_t ix
) {
222 if (digests
&& new_ix
< count
) {
223 while (++new_ix
< count
) {
224 int delta
= SOSDigestCompare(digests
+ ix
* SOSDigestSize
, digests
+ new_ix
* SOSDigestSize
);
233 static size_t SOSDigestVectorIncrementAndSkipDupes(const struct SOSDigestVector
*dv
, const size_t ix
) {
234 return SOSIncrementAndSkipDupes((const uint8_t *)dv
->digest
, dv
->count
, ix
);
237 void SOSDigestVectorAppendMultipleOrdered(struct SOSDigestVector
*dv
,
238 size_t count
, const uint8_t *digests
) {
241 SOSDigestVectorAppendOrdered(dv
, digests
+ (ix
* SOSDigestSize
));
242 ix
= SOSIncrementAndSkipDupes(digests
, count
, ix
);
246 #else /* !SOSDVSKIPDUPES */
248 #define SOSDVINCRIX(dv,ix) (ix + 1)
250 void SOSDigestVectorAppendMultipleOrdered(struct SOSDigestVector
*dv
,
251 size_t count
, const uint8_t *digests
) {
253 if (SOSDigestVectorEnsureCapacity(dv
, dv
->count
+ count
))
254 memcpy(dv
->digest
[dv
->count
], digests
, count
* SOSDigestSize
);
259 #endif /* !SOSDVSKIPDUPES */
261 void SOSDigestVectorIntersectSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
262 struct SOSDigestVector
*dvintersect
)
264 /* dvintersect should be empty to start. */
265 assert(dvintersect
->count
== 0);
266 size_t i1
= 0, i2
= 0;
267 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
268 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
270 SOSDigestVectorAppendOrdered(dvintersect
, dv1
->digest
[i1
]);
271 i1
= SOSDVINCRIX(dv1
, i1
);
272 i2
= SOSDVINCRIX(dv2
, i2
);
273 } else if (delta
< 0) {
274 i1
= SOSDVINCRIX(dv1
, i1
);
276 i2
= SOSDVINCRIX(dv2
, i2
);
281 void SOSDigestVectorUnionSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
282 struct SOSDigestVector
*dvunion
)
284 /* dvunion should be empty to start. */
285 assert(dvunion
->count
== 0);
286 size_t i1
= 0, i2
= 0;
287 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
288 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
290 SOSDigestVectorAppendOrdered(dvunion
, dv1
->digest
[i1
]);
291 i1
= SOSDVINCRIX(dv1
, i1
);
292 i2
= SOSDVINCRIX(dv2
, i2
);
293 } else if (delta
< 0) {
294 SOSDigestVectorAppendOrdered(dvunion
, dv1
->digest
[i1
]);
295 i1
= SOSDVINCRIX(dv1
, i1
);
297 SOSDigestVectorAppendOrdered(dvunion
, dv2
->digest
[i2
]);
298 i2
= SOSDVINCRIX(dv2
, i2
);
301 SOSDigestVectorAppendMultipleOrdered(dvunion
, dv1
->count
- i1
, dv1
->digest
[i1
]);
302 SOSDigestVectorAppendMultipleOrdered(dvunion
, dv2
->count
- i2
, dv2
->digest
[i2
]);
305 void SOSDigestVectorDiffSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
306 struct SOSDigestVector
*dv1_2
, struct SOSDigestVector
*dv2_1
)
308 /* dv1_2 and dv2_1 should be empty to start. */
309 assert(dv1_2
->count
== 0);
310 assert(dv2_1
->count
== 0);
312 size_t i1
= 0, i2
= 0;
313 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
314 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
316 i1
= SOSDVINCRIX(dv1
, i1
);
317 i2
= SOSDVINCRIX(dv2
, i2
);
318 } else if (delta
< 0) {
319 SOSDigestVectorAppendOrdered(dv1_2
, dv1
->digest
[i1
]);
320 i1
= SOSDVINCRIX(dv1
, i1
);
322 SOSDigestVectorAppendOrdered(dv2_1
, dv2
->digest
[i2
]);
323 i2
= SOSDVINCRIX(dv2
, i2
);
326 SOSDigestVectorAppendMultipleOrdered(dv1_2
, dv1
->count
- i1
, dv1
->digest
[i1
]);
327 SOSDigestVectorAppendMultipleOrdered(dv2_1
, dv2
->count
- i2
, dv2
->digest
[i2
]);
330 void SOSDigestVectorDiff(struct SOSDigestVector
*dv1
, struct SOSDigestVector
*dv2
,
331 struct SOSDigestVector
*dv1_2
, struct SOSDigestVector
*dv2_1
)
333 if (dv1
->unsorted
) SOSDigestVectorSort(dv1
);
334 if (dv2
->unsorted
) SOSDigestVectorSort(dv2
);
335 SOSDigestVectorDiffSorted(dv1
, dv2
, dv1_2
, dv2_1
);
339 If A and B are sets, then the relative complement of A in B, also termed the set-theoretic difference of B and A,
340 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
341 sometimes written B − A
343 The common case for us will be Removals\Additions
346 static void SOSDigestVectorAppendComplementAtIndex(size_t a_ix
, const struct SOSDigestVector
*dvA
, size_t b_ix
, const struct SOSDigestVector
*dvB
,
347 struct SOSDigestVector
*dvcomplement
)
349 assert(a_ix
<= dvA
->count
&& b_ix
<= dvB
->count
);
350 while (a_ix
< dvA
->count
&& b_ix
< dvB
->count
&& dvA
->digest
&& dvB
->digest
) {
351 int delta
= SOSDigestCompare(dvA
->digest
[a_ix
], dvB
->digest
[b_ix
]);
353 a_ix
= SOSDVINCRIX(dvA
, a_ix
);
354 b_ix
= SOSDVINCRIX(dvB
, b_ix
);
355 } else if (delta
< 0) {
356 a_ix
= SOSDVINCRIX(dvA
, a_ix
);
358 SOSDigestVectorAppendOrdered(dvcomplement
, dvB
->digest
[b_ix
]);
359 b_ix
= SOSDVINCRIX(dvB
, b_ix
);
363 SOSDigestVectorAppendMultipleOrdered(dvcomplement
, dvB
->count
- b_ix
, dvB
->digest
[b_ix
]);
367 void SOSDigestVectorComplementSorted(const struct SOSDigestVector
*dvA
, const struct SOSDigestVector
*dvB
,
368 struct SOSDigestVector
*dvcomplement
)
370 /* dvcomplement should be empty to start. */
371 assert(dvcomplement
->count
== 0);
372 assert(!dvA
->unsorted
);
373 assert(!dvB
->unsorted
);
375 SOSDigestVectorAppendComplementAtIndex(0, dvA
, 0, dvB
, dvcomplement
);
380 For each item in base
382 one way to do would be to define SOSDigestVectorComplementSorted
384 For removals, if removal value is less than base, increment until GEQ
386 bool SOSDigestVectorPatchSorted(const struct SOSDigestVector
*base
, const struct SOSDigestVector
*removals
,
387 const struct SOSDigestVector
*additions
, struct SOSDigestVector
*dv
,
390 /* dv should be empty to start. */
391 assert(dv
->count
== 0);
392 assert(!base
->unsorted
);
393 assert(!removals
->unsorted
);
394 assert(!additions
->unsorted
);
396 size_t i1
= 0, i2
= 0, i3
= 0;
397 while (i1
< base
->count
&& i2
< additions
->count
) {
398 // Pick the smaller of base->digest[i1] and additions->digest[i2] as a
399 // candidate to be put into the output vector. If udelta positive, addition is smaller
400 int udelta
= SOSDigestCompare(base
->digest
[i1
], additions
->digest
[i2
]);
401 const uint8_t *candidate
= udelta
< 0 ? base
->digest
[i1
] : additions
->digest
[i2
];
403 // ddelta > 0 means rem > candidate
405 while (i3
< removals
->count
) {
406 ddelta
= SOSDigestCompare(removals
->digest
[i3
], candidate
);
408 i3
= SOSDVINCRIX(removals
, i3
);
411 i3
= SOSDVINCRIX(removals
, i3
);
416 SOSDigestVectorAppendOrdered(dv
, candidate
);
418 // Point to next (different) candidate
420 i1
= SOSDVINCRIX(base
, i1
);
421 i2
= SOSDVINCRIX(additions
, i2
);
422 } else if (udelta
< 0) {
423 i1
= SOSDVINCRIX(base
, i1
);
425 i2
= SOSDVINCRIX(additions
, i2
);
428 SOSDigestVectorAppendComplementAtIndex(i3
, removals
, i1
, base
, dv
);
429 SOSDigestVectorAppendComplementAtIndex(i3
, removals
, i2
, additions
, dv
);
434 bool SOSDigestVectorPatch(struct SOSDigestVector
*base
, struct SOSDigestVector
*removals
,
435 struct SOSDigestVector
*additions
, struct SOSDigestVector
*dv
,
438 if (base
->unsorted
) SOSDigestVectorSort(base
);
439 if (removals
->unsorted
) SOSDigestVectorSort(removals
);
440 if (additions
->unsorted
) SOSDigestVectorSort(additions
);
441 return SOSDigestVectorPatchSorted(base
, removals
, additions
, dv
, error
);