]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/SecDigest.c
Security-58286.70.7.tar.gz
[apple/security.git] / OSX / sec / Security / SecDigest.c
1 /*
2 * Copyright (c) 2006-2010,2012-2015 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 #ifdef STANDALONE
25 /* Allows us to build genanchors against the BaseSDK. */
26 #undef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
27 #undef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
28 #endif
29
30 #include "SecFramework.h"
31 #include <dispatch/dispatch.h>
32 #include <CommonCrypto/CommonDigest.h>
33 #include <CommonCrypto/CommonDigestSPI.h>
34 #include <Security/SecAsn1Coder.h>
35 #include <Security/oidsalg.h>
36 #include <utilities/SecCFWrappers.h>
37 #include <Security/SecBase.h>
38 #include <inttypes.h>
39
40 /* Return the SHA1 digest of a chunk of data as newly allocated CFDataRef. */
41 CFDataRef SecSHA1DigestCreate(CFAllocatorRef allocator,
42 const UInt8 *data, CFIndex length) {
43 CFMutableDataRef digest = CFDataCreateMutable(allocator,
44 CC_SHA1_DIGEST_LENGTH);
45 CFDataSetLength(digest, CC_SHA1_DIGEST_LENGTH);
46 CCDigest(kCCDigestSHA1, data, length, CFDataGetMutableBytePtr(digest));
47 return digest;
48 }
49
50 CFDataRef SecSHA256DigestCreate(CFAllocatorRef allocator,
51 const UInt8 *data, CFIndex length) {
52 CFMutableDataRef digest = CFDataCreateMutable(allocator,
53 CC_SHA256_DIGEST_LENGTH);
54 CFDataSetLength(digest, CC_SHA256_DIGEST_LENGTH);
55 CCDigest(kCCDigestSHA256, data, length, CFDataGetMutableBytePtr(digest));
56 return digest;
57 }
58
59 CFDataRef SecSHA256DigestCreateFromData(CFAllocatorRef allocator, CFDataRef data) {
60 CFMutableDataRef digest = CFDataCreateMutable(allocator,
61 CC_SHA256_DIGEST_LENGTH);
62 CFDataSetLength(digest, CC_SHA256_DIGEST_LENGTH);
63 CCDigest(kCCDigestSHA256, CFDataGetBytePtr(data), CFDataGetLength(data), CFDataGetMutableBytePtr(digest));
64 return digest;
65 }
66
67 CFDataRef SecDigestCreate(CFAllocatorRef allocator,
68 const SecAsn1Oid *algorithm, const SecAsn1Item *params,
69 const UInt8 *data, CFIndex length) {
70 unsigned char *(*digestFcn)(const void *data, CC_LONG len, unsigned char *md);
71 CFIndex digestLen;
72
73 if (length > INT32_MAX)
74 return NULL;
75
76 if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA1)) {
77 digestFcn = CC_SHA1;
78 digestLen = CC_SHA1_DIGEST_LENGTH;
79 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA224)) {
80 digestFcn = CC_SHA224;
81 digestLen = CC_SHA224_DIGEST_LENGTH;
82 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA256)) {
83 digestFcn = CC_SHA256;
84 digestLen = CC_SHA256_DIGEST_LENGTH;
85 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA384)) {
86 digestFcn = CC_SHA384;
87 digestLen = CC_SHA384_DIGEST_LENGTH;
88 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA512)) {
89 digestFcn = CC_SHA512;
90 digestLen = CC_SHA512_DIGEST_LENGTH;
91 } else {
92 return NULL;
93 }
94
95 CFMutableDataRef digest = CFDataCreateMutable(allocator, digestLen);
96 CFDataSetLength(digest, digestLen);
97
98 digestFcn(data, (CC_LONG)length, CFDataGetMutableBytePtr(digest));
99 return digest;
100 }