]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/SecDigest.c
Security-59306.11.20.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 if (length < 0 || length > INT32_MAX || data == NULL) {
44 return NULL; /* guard against passing bad values to digest function */
45 }
46 CFMutableDataRef digest = CFDataCreateMutable(allocator,
47 CC_SHA1_DIGEST_LENGTH);
48 CFDataSetLength(digest, CC_SHA1_DIGEST_LENGTH);
49 CCDigest(kCCDigestSHA1, data, length, CFDataGetMutableBytePtr(digest));
50 return digest;
51 }
52
53 CFDataRef SecSHA256DigestCreate(CFAllocatorRef allocator,
54 const UInt8 *data, CFIndex length) {
55 if (length < 0 || length > INT32_MAX || data == NULL) {
56 return NULL; /* guard against passing bad values to digest function */
57 }
58 CFMutableDataRef digest = CFDataCreateMutable(allocator,
59 CC_SHA256_DIGEST_LENGTH);
60 CFDataSetLength(digest, CC_SHA256_DIGEST_LENGTH);
61 CCDigest(kCCDigestSHA256, data, length, CFDataGetMutableBytePtr(digest));
62 return digest;
63 }
64
65 CFDataRef SecSHA256DigestCreateFromData(CFAllocatorRef allocator, CFDataRef data) {
66 CFMutableDataRef digest = CFDataCreateMutable(allocator,
67 CC_SHA256_DIGEST_LENGTH);
68 CFDataSetLength(digest, CC_SHA256_DIGEST_LENGTH);
69 CCDigest(kCCDigestSHA256, CFDataGetBytePtr(data), CFDataGetLength(data), CFDataGetMutableBytePtr(digest));
70 return digest;
71 }
72
73 CFDataRef SecDigestCreate(CFAllocatorRef allocator,
74 const SecAsn1Oid *algorithm, const SecAsn1Item *params,
75 const UInt8 *data, CFIndex length) {
76 unsigned char *(*digestFcn)(const void *data, CC_LONG len, unsigned char *md);
77 CFIndex digestLen;
78
79 if (length < 0 || length > INT32_MAX || data == NULL)
80 return NULL; /* guard against passing bad values to digest function */
81
82 if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA1)) {
83 digestFcn = CC_SHA1;
84 digestLen = CC_SHA1_DIGEST_LENGTH;
85 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA224)) {
86 digestFcn = CC_SHA224;
87 digestLen = CC_SHA224_DIGEST_LENGTH;
88 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA256)) {
89 digestFcn = CC_SHA256;
90 digestLen = CC_SHA256_DIGEST_LENGTH;
91 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA384)) {
92 digestFcn = CC_SHA384;
93 digestLen = CC_SHA384_DIGEST_LENGTH;
94 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA512)) {
95 digestFcn = CC_SHA512;
96 digestLen = CC_SHA512_DIGEST_LENGTH;
97 } else {
98 return NULL;
99 }
100
101 CFMutableDataRef digest = CFDataCreateMutable(allocator, digestLen);
102 CFDataSetLength(digest, digestLen);
103
104 digestFcn(data, (CC_LONG)length, CFDataGetMutableBytePtr(digest));
105 return digest;
106 }