Merge branch 'maint'
[git] / compat / apple-common-crypto.h
1 /* suppress inclusion of conflicting openssl functions */
2 #define OPENSSL_NO_MD5
3 #define HEADER_HMAC_H
4 #define HEADER_SHA_H
5 #include <CommonCrypto/CommonHMAC.h>
6 #define HMAC_CTX CCHmacContext
7 #define HMAC_Init(hmac, key, len, algo) CCHmacInit(hmac, algo, key, len)
8 #define HMAC_Update CCHmacUpdate
9 #define HMAC_Final(hmac, hash, ptr) CCHmacFinal(hmac, hash)
10 #define HMAC_CTX_cleanup(ignore)
11 #define EVP_md5(...) kCCHmacAlgMD5
12 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
13 #define APPLE_LION_OR_NEWER
14 #include <Security/Security.h>
15 /* Apple's TYPE_BOOL conflicts with config.c */
16 #undef TYPE_BOOL
17 #endif
18
19 #ifdef APPLE_LION_OR_NEWER
20 #define git_CC_error_check(pattern, err) \
21         do { \
22                 if (err) { \
23                         die(pattern, (long)CFErrorGetCode(err)); \
24                 } \
25         } while(0)
26
27 #define EVP_EncodeBlock git_CC_EVP_EncodeBlock
28 static inline int git_CC_EVP_EncodeBlock(unsigned char *out,
29                 const unsigned char *in, int inlen)
30 {
31         CFErrorRef err;
32         SecTransformRef encoder;
33         CFDataRef input, output;
34         CFIndex length;
35
36         encoder = SecEncodeTransformCreate(kSecBase64Encoding, &err);
37         git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
38
39         input = CFDataCreate(kCFAllocatorDefault, in, inlen);
40         SecTransformSetAttribute(encoder, kSecTransformInputAttributeName,
41                         input, &err);
42         git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
43
44         output = SecTransformExecute(encoder, &err);
45         git_CC_error_check("SecTransformExecute failed: %ld", err);
46
47         length = CFDataGetLength(output);
48         CFDataGetBytes(output, CFRangeMake(0, length), out);
49
50         CFRelease(output);
51         CFRelease(input);
52         CFRelease(encoder);
53
54         return (int)strlen((const char *)out);
55 }
56
57 #define EVP_DecodeBlock git_CC_EVP_DecodeBlock
58 static int inline git_CC_EVP_DecodeBlock(unsigned char *out,
59                 const unsigned char *in, int inlen)
60 {
61         CFErrorRef err;
62         SecTransformRef decoder;
63         CFDataRef input, output;
64         CFIndex length;
65
66         decoder = SecDecodeTransformCreate(kSecBase64Encoding, &err);
67         git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
68
69         input = CFDataCreate(kCFAllocatorDefault, in, inlen);
70         SecTransformSetAttribute(decoder, kSecTransformInputAttributeName,
71                         input, &err);
72         git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
73
74         output = SecTransformExecute(decoder, &err);
75         git_CC_error_check("SecTransformExecute failed: %ld", err);
76
77         length = CFDataGetLength(output);
78         CFDataGetBytes(output, CFRangeMake(0, length), out);
79
80         CFRelease(output);
81         CFRelease(input);
82         CFRelease(decoder);
83
84         return (int)strlen((const char *)out);
85 }
86 #endif /* APPLE_LION_OR_NEWER */