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 "keychain/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 secnotice("manifest", "Requesting too much space for digest vectors: %ld", count
);
48 size_t capacity
= MIN(count
+ 100, kMaxDVCapacity
);
49 dv
->digest
= reallocf(dv
->digest
, SOSDigestSize
* capacity
);
50 if (dv
->digest
== NULL
) {
53 secnotice("manifest", "reallocf failed requesting space for digest vectors: %ld (bytes)", SOSDigestSize
* capacity
);
56 dv
->capacity
= capacity
;
60 static void SOSDigestVectorAppendOrdered(struct SOSDigestVector
*dv
, const uint8_t *digest
)
62 if (digest
&& SOSDigestVectorEnsureCapacity(dv
, dv
->count
+ 1)) {
63 memcpy(dv
->digest
[dv
->count
++], digest
, SOSDigestSize
);
67 void SOSDigestVectorAppend(struct SOSDigestVector
*dv
, const uint8_t *digest
)
69 SOSDigestVectorAppendOrdered(dv
, digest
);
73 static int SOSDigestCompare(const void *a
, const void *b
)
75 return memcmp(a
, b
, SOSDigestSize
);
78 // Remove duplicates from sorted manifest using minimal memmove() calls
79 static void SOSDigestVectorUnique(struct SOSDigestVector
*dv
) {
80 if (dv
->count
< 2 || dv
->digest
== NULL
)
83 const uint8_t *prev
= dv
->digest
[0];
84 uint8_t *dest
= dv
->digest
[1];
85 const uint8_t *end
= dv
->digest
[dv
->count
];
86 const uint8_t *source
= dest
;
87 for (const uint8_t *cur
= source
; cur
< end
; cur
+= SOSDigestSize
) {
88 int delta
= SOSDigestCompare(prev
, cur
);
90 // Found a properly sorted element
91 // 1) Extend the current region (prev is end of region pointer)
94 } else if (delta
> 0) {
95 // DigestVector wasn't sorted!
98 // Found a duplicate element
99 // 1) Finish copy for current region up to previous element
100 prev
+= SOSDigestSize
;
102 memmove(dest
, source
, prev
- source
);
103 dest
+= prev
- source
;
104 // 2) Skip remaining dupes
106 cur
+= SOSDigestSize
;
108 int delta
= SOSDigestCompare(prev
, cur
);
113 cur
+= SOSDigestSize
;
116 // cur now points to the first new element that hasn't yet been copied
117 // 3) Set start of next region
123 // Copy remainder of final region
125 prev
+= SOSDigestSize
;
127 memmove(dest
, source
, prev
- source
);
128 dest
+= prev
- source
;
130 dv
->count
= (dest
- dv
->digest
[0]) / SOSDigestSize
;
134 void SOSDigestVectorSort(struct SOSDigestVector
*dv
)
136 if (dv
->unsorted
&& dv
->digest
) {
137 qsort(dv
->digest
, dv
->count
, sizeof(*dv
->digest
), SOSDigestCompare
);
138 dv
->unsorted
= false;
139 SOSDigestVectorUnique(dv
);
143 void SOSDigestVectorUniqueSorted(struct SOSDigestVector
*dv
)
145 // Uniqify in place (sort does this now for safety)
147 SOSDigestVectorSort(dv
);
150 void SOSDigestVectorSwap(struct SOSDigestVector
*dva
, struct SOSDigestVector
*dvb
)
152 struct SOSDigestVector dv
;
158 bool SOSDigestVectorContainsSorted(const struct SOSDigestVector
*dv
, const uint8_t *digest
)
160 return SOSDigestVectorIndexOfSorted(dv
, digest
) != (size_t)-1;
163 bool SOSDigestVectorContains(struct SOSDigestVector
*dv
, const uint8_t *digest
)
166 SOSDigestVectorSort(dv
);
167 return SOSDigestVectorContainsSorted(dv
, digest
);
170 size_t SOSDigestVectorIndexOfSorted(const struct SOSDigestVector
*dv
, const uint8_t *digest
)
173 const void *pos
= bsearch(digest
, dv
->digest
, dv
->count
, sizeof(*dv
->digest
), SOSDigestCompare
);
174 return pos
? ((size_t)(pos
- (void *)dv
->digest
)) / SOSDigestSize
: ((size_t)-1);
180 size_t SOSDigestVectorIndexOf(struct SOSDigestVector
*dv
, const uint8_t *digest
)
183 SOSDigestVectorSort(dv
);
184 return SOSDigestVectorIndexOfSorted(dv
, digest
);
187 void SOSDigestVectorFree(struct SOSDigestVector
*dv
)
193 dv
->unsorted
= false;
196 void SOSDigestVectorApplySorted(const struct SOSDigestVector
*dv
, SOSDigestVectorApplyBlock with
)
199 for (size_t ix
= 0; !stop
&& ix
< dv
->count
&& dv
->digest
; ++ix
) {
200 with(dv
->digest
[ix
], &stop
);
204 void SOSDigestVectorApply(struct SOSDigestVector
*dv
, SOSDigestVectorApplyBlock with
)
207 SOSDigestVectorSort(dv
);
208 SOSDigestVectorApplySorted(dv
, with
);
211 // TODO: Check for NDEBUG to disable skip dupes are release time.
212 //#define SOSDVSKIPDUPES 0
213 #define SOSDVSKIPDUPES 1
216 #define SOSDVINCRIX(dv,ix) (SOSDigestVectorIncrementAndSkipDupes(dv,ix))
218 static size_t SOSIncrementAndSkipDupes(const uint8_t *digests
, size_t count
, const size_t ix
) {
220 if (digests
&& new_ix
< count
) {
221 while (++new_ix
< count
) {
222 int delta
= SOSDigestCompare(digests
+ ix
* SOSDigestSize
, digests
+ new_ix
* SOSDigestSize
);
231 static size_t SOSDigestVectorIncrementAndSkipDupes(const struct SOSDigestVector
*dv
, const size_t ix
) {
232 return SOSIncrementAndSkipDupes((const uint8_t *)dv
->digest
, dv
->count
, ix
);
235 void SOSDigestVectorAppendMultipleOrdered(struct SOSDigestVector
*dv
,
236 size_t count
, const uint8_t *digests
) {
239 SOSDigestVectorAppendOrdered(dv
, digests
+ (ix
* SOSDigestSize
));
240 ix
= SOSIncrementAndSkipDupes(digests
, count
, ix
);
244 #else /* !SOSDVSKIPDUPES */
246 #define SOSDVINCRIX(dv,ix) (ix + 1)
248 void SOSDigestVectorAppendMultipleOrdered(struct SOSDigestVector
*dv
,
249 size_t count
, const uint8_t *digests
) {
251 if (SOSDigestVectorEnsureCapacity(dv
, dv
->count
+ count
))
252 memcpy(dv
->digest
[dv
->count
], digests
, count
* SOSDigestSize
);
257 #endif /* !SOSDVSKIPDUPES */
259 void SOSDigestVectorIntersectSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
260 struct SOSDigestVector
*dvintersect
)
262 /* dvintersect should be empty to start. */
263 assert(dvintersect
->count
== 0);
264 size_t i1
= 0, i2
= 0;
265 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
266 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
268 SOSDigestVectorAppendOrdered(dvintersect
, dv1
->digest
[i1
]);
269 i1
= SOSDVINCRIX(dv1
, i1
);
270 i2
= SOSDVINCRIX(dv2
, i2
);
271 } else if (delta
< 0) {
272 i1
= SOSDVINCRIX(dv1
, i1
);
274 i2
= SOSDVINCRIX(dv2
, i2
);
279 void SOSDigestVectorUnionSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
280 struct SOSDigestVector
*dvunion
)
282 /* dvunion should be empty to start. */
283 assert(dvunion
->count
== 0);
284 size_t i1
= 0, i2
= 0;
285 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
286 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
288 SOSDigestVectorAppendOrdered(dvunion
, dv1
->digest
[i1
]);
289 i1
= SOSDVINCRIX(dv1
, i1
);
290 i2
= SOSDVINCRIX(dv2
, i2
);
291 } else if (delta
< 0) {
292 SOSDigestVectorAppendOrdered(dvunion
, dv1
->digest
[i1
]);
293 i1
= SOSDVINCRIX(dv1
, i1
);
295 SOSDigestVectorAppendOrdered(dvunion
, dv2
->digest
[i2
]);
296 i2
= SOSDVINCRIX(dv2
, i2
);
299 SOSDigestVectorAppendMultipleOrdered(dvunion
, dv1
->count
- i1
, dv1
->digest
[i1
]);
300 SOSDigestVectorAppendMultipleOrdered(dvunion
, dv2
->count
- i2
, dv2
->digest
[i2
]);
303 void SOSDigestVectorDiffSorted(const struct SOSDigestVector
*dv1
, const struct SOSDigestVector
*dv2
,
304 struct SOSDigestVector
*dv1_2
, struct SOSDigestVector
*dv2_1
)
306 /* dv1_2 and dv2_1 should be empty to start. */
307 assert(dv1_2
->count
== 0);
308 assert(dv2_1
->count
== 0);
310 size_t i1
= 0, i2
= 0;
311 while (i1
< dv1
->count
&& i2
< dv2
->count
) {
312 int delta
= SOSDigestCompare(dv1
->digest
[i1
], dv2
->digest
[i2
]);
314 i1
= SOSDVINCRIX(dv1
, i1
);
315 i2
= SOSDVINCRIX(dv2
, i2
);
316 } else if (delta
< 0) {
317 SOSDigestVectorAppendOrdered(dv1_2
, dv1
->digest
[i1
]);
318 i1
= SOSDVINCRIX(dv1
, i1
);
320 SOSDigestVectorAppendOrdered(dv2_1
, dv2
->digest
[i2
]);
321 i2
= SOSDVINCRIX(dv2
, i2
);
324 SOSDigestVectorAppendMultipleOrdered(dv1_2
, dv1
->count
- i1
, dv1
->digest
[i1
]);
325 SOSDigestVectorAppendMultipleOrdered(dv2_1
, dv2
->count
- i2
, dv2
->digest
[i2
]);
328 void SOSDigestVectorDiff(struct SOSDigestVector
*dv1
, struct SOSDigestVector
*dv2
,
329 struct SOSDigestVector
*dv1_2
, struct SOSDigestVector
*dv2_1
)
331 if (dv1
->unsorted
) SOSDigestVectorSort(dv1
);
332 if (dv2
->unsorted
) SOSDigestVectorSort(dv2
);
333 SOSDigestVectorDiffSorted(dv1
, dv2
, dv1_2
, dv2_1
);
337 If A and B are sets, then the relative complement of A in B, also termed the set-theoretic difference of B and A,
338 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
339 sometimes written B − A
341 The common case for us will be Removals\Additions
344 static void SOSDigestVectorAppendComplementAtIndex(size_t a_ix
, const struct SOSDigestVector
*dvA
, size_t b_ix
, const struct SOSDigestVector
*dvB
,
345 struct SOSDigestVector
*dvcomplement
)
347 assert(a_ix
<= dvA
->count
&& b_ix
<= dvB
->count
);
348 while (a_ix
< dvA
->count
&& b_ix
< dvB
->count
&& dvA
->digest
&& dvB
->digest
) {
349 int delta
= SOSDigestCompare(dvA
->digest
[a_ix
], dvB
->digest
[b_ix
]);
351 a_ix
= SOSDVINCRIX(dvA
, a_ix
);
352 b_ix
= SOSDVINCRIX(dvB
, b_ix
);
353 } else if (delta
< 0) {
354 a_ix
= SOSDVINCRIX(dvA
, a_ix
);
356 SOSDigestVectorAppendOrdered(dvcomplement
, dvB
->digest
[b_ix
]);
357 b_ix
= SOSDVINCRIX(dvB
, b_ix
);
361 SOSDigestVectorAppendMultipleOrdered(dvcomplement
, dvB
->count
- b_ix
, dvB
->digest
[b_ix
]);
365 void SOSDigestVectorComplementSorted(const struct SOSDigestVector
*dvA
, const struct SOSDigestVector
*dvB
,
366 struct SOSDigestVector
*dvcomplement
)
368 /* dvcomplement should be empty to start. */
369 assert(dvcomplement
->count
== 0);
370 assert(!dvA
->unsorted
);
371 assert(!dvB
->unsorted
);
373 SOSDigestVectorAppendComplementAtIndex(0, dvA
, 0, dvB
, dvcomplement
);
378 For each item in base
380 one way to do would be to define SOSDigestVectorComplementSorted
382 For removals, if removal value is less than base, increment until GEQ
384 bool SOSDigestVectorPatchSorted(const struct SOSDigestVector
*base
, const struct SOSDigestVector
*removals
,
385 const struct SOSDigestVector
*additions
, struct SOSDigestVector
*dv
,
388 /* dv should be empty to start. */
389 assert(dv
->count
== 0);
390 assert(!base
->unsorted
);
391 assert(!removals
->unsorted
);
392 assert(!additions
->unsorted
);
394 size_t i1
= 0, i2
= 0, i3
= 0;
395 while (i1
< base
->count
&& i2
< additions
->count
) {
396 // Pick the smaller of base->digest[i1] and additions->digest[i2] as a
397 // candidate to be put into the output vector. If udelta positive, addition is smaller
398 int udelta
= SOSDigestCompare(base
->digest
[i1
], additions
->digest
[i2
]);
399 const uint8_t *candidate
= udelta
< 0 ? base
->digest
[i1
] : additions
->digest
[i2
];
401 // ddelta > 0 means rem > candidate
403 while (i3
< removals
->count
) {
404 ddelta
= SOSDigestCompare(removals
->digest
[i3
], candidate
);
406 i3
= SOSDVINCRIX(removals
, i3
);
409 i3
= SOSDVINCRIX(removals
, i3
);
414 SOSDigestVectorAppendOrdered(dv
, candidate
);
416 // Point to next (different) candidate
418 i1
= SOSDVINCRIX(base
, i1
);
419 i2
= SOSDVINCRIX(additions
, i2
);
420 } else if (udelta
< 0) {
421 i1
= SOSDVINCRIX(base
, i1
);
423 i2
= SOSDVINCRIX(additions
, i2
);
426 SOSDigestVectorAppendComplementAtIndex(i3
, removals
, i1
, base
, dv
);
427 SOSDigestVectorAppendComplementAtIndex(i3
, removals
, i2
, additions
, dv
);
432 bool SOSDigestVectorPatch(struct SOSDigestVector
*base
, struct SOSDigestVector
*removals
,
433 struct SOSDigestVector
*additions
, struct SOSDigestVector
*dv
,
436 if (base
->unsorted
) SOSDigestVectorSort(base
);
437 if (removals
->unsorted
) SOSDigestVectorSort(removals
);
438 if (additions
->unsorted
) SOSDigestVectorSort(additions
);
439 return SOSDigestVectorPatchSorted(base
, removals
, additions
, dv
, error
);