]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_transform/lib/Digest_block.c
Security-57337.40.85.tar.gz
[apple/security.git] / OSX / libsecurity_transform / lib / Digest_block.c
1 /*
2 * Copyright (c) 2010-2011,2014 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
25 #include "Digest_block.h"
26 #include "SecCustomTransform.h"
27 #include <CommonCrypto/CommonDigest.h>
28 #include "Utilities.h"
29
30 extern const CFStringRef kSecDigestMD2, kSecDigestMD4, kSecDigestMD5, kSecDigestSHA1, kSecDigestSHA2;
31 extern const CFStringRef kSecDigestTypeAttribute, kSecDigestLengthAttribute;
32
33
34 static CFStringRef kCustomDigestTransformName = CFSTR("com.apple.security.digest");
35
36 SecTransformRef SecDigestTransformCreate_block(SecProviderRef device, CFTypeRef digestType, CFIndex digestLength, CFErrorRef* error) {
37 static dispatch_once_t inited;
38 __block CFStringRef current_algo;
39 __block CFIndex current_length;
40
41 dispatch_once(&inited, ^{
42 SecTransformRegister(kCustomDigestTransformName, ^(CFStringRef name, SecTransformRebindActionBlock rebind) {
43 rebind(kSecTransformActionProcessData, NULL, ^(SecTransformRef tr, CFDataRef d, SecTransformSendAttribute set) {
44 set(kSecDigestTypeAttribute, kSecDigestSHA2);
45 });
46 rebind(SecTransformSetAttributeAction, NULL, ^(SecTransformRef tr, CFStringRef name, CFTypeRef value, SecTransformSendAttribute set) {
47 Boolean algo_changed = FALSE;
48 if (name == kSecDigestTypeAttribute || CFStringCompare(kSecDigestTypeAttribute, name, 0) == kCFCompareEqualTo) {
49 algo_changed = TRUE;
50 current_algo = CFStringCreateCopy(NULL, (CFStringRef)value);
51
52 } else if (name == kSecDigestLengthAttribute || CFStringCompare(kSecDigestLengthAttribute, name, 0) == kCFCompareEqualTo) {
53 algo_changed = TRUE;
54 CFNumberGetValue(value, kCFNumberCFIndexType, &current_length);
55 } else if (CFStringCompare(kSecTransformInputAttributeName, name, 0) == kCFCompareEqualTo) {
56 return (CFTypeRef)CreateSecTransformErrorRef(kSecTransformErrorInvalidType, "The type %@ is invalid.", name);
57 } else {
58 return (CFTypeRef)CreateSecTransformErrorRef(kSecTransformErrorInvalidType, "The type %@ is invalid.", name);
59 }
60 if (!algo_changed) {
61 return value;
62 }
63
64 if (current_algo == kSecDigestSHA2 || CFStringCompare(current_algo, kSecDigestSHA2, 0) == kCFCompareEqualTo) {
65 switch (current_length) {
66 case 0:
67 case 512: {
68 __block CC_SHA512_CTX cc_context;
69 CC_SHA512_Init(&cc_context);
70 rebind(kSecTransformActionProcessData, NULL, ^(SecTransformRef tr, CFDataRef d, SecTransformSendAttribute set) {
71 if (d) {
72 CC_SHA512_Update(&cc_context, CFDataGetBytePtr(d), CFDataGetLength(d));
73 return SecTransformNoData();
74 } else {
75 u_int8_t digest_buffer[CC_SHA512_DIGEST_LENGTH];
76
77 CC_SHA512_Final(digest_buffer, &cc_context);
78 set(kSecTransformOutputAttributeName, CFDataCreate(NULL, digest_buffer, CC_SHA512_DIGEST_LENGTH));
79 return NULL;
80 }
81 });
82 return value;
83 }
84 }
85 return (CFTypeRef)CreateSecTransformErrorRef(kSecTransformErrorInvalidLength, "Invalid length.");
86 } else {
87 return (CFTypeRef)CreateSecTransformErrorRef(kSecTransformErrorInvalidAlgorithm, "Invalid algorithm.");
88 }
89 });
90 }, NULL);
91 });
92
93 SecTransformRef dt = SecTransformCreate(kCustomDigestTransformName, NULL);
94 SecTransformSetAttribute(dt, kSecDigestTypeAttribute, digestType, error);
95 CFNumberRef dlen = CFNumberCreate(NULL, kCFNumberCFIndexType, &digestLength);
96 SecTransformSetAttribute(dt, kSecDigestLengthAttribute, dlen, error);
97 CFRelease(dlen);
98
99 return dt;
100 }