Merge branch 'kn/ref-filter-atom-parsing'
[git/gitweb.git] / compat / apple-common-crypto.h
blobd3fb2641813404a95709d2b219b9ab1fa1434296
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
19 #ifndef SHA1_MAX_BLOCK_SIZE
20 #error Using Apple Common Crypto library requires setting SHA1_MAX_BLOCK_SIZE
21 #endif
23 #ifdef APPLE_LION_OR_NEWER
24 #define git_CC_error_check(pattern, err) \
25 do { \
26 if (err) { \
27 die(pattern, (long)CFErrorGetCode(err)); \
28 } \
29 } while(0)
31 #define EVP_EncodeBlock git_CC_EVP_EncodeBlock
32 static inline int git_CC_EVP_EncodeBlock(unsigned char *out,
33 const unsigned char *in, int inlen)
35 CFErrorRef err;
36 SecTransformRef encoder;
37 CFDataRef input, output;
38 CFIndex length;
40 encoder = SecEncodeTransformCreate(kSecBase64Encoding, &err);
41 git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
43 input = CFDataCreate(kCFAllocatorDefault, in, inlen);
44 SecTransformSetAttribute(encoder, kSecTransformInputAttributeName,
45 input, &err);
46 git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
48 output = SecTransformExecute(encoder, &err);
49 git_CC_error_check("SecTransformExecute failed: %ld", err);
51 length = CFDataGetLength(output);
52 CFDataGetBytes(output, CFRangeMake(0, length), out);
54 CFRelease(output);
55 CFRelease(input);
56 CFRelease(encoder);
58 return (int)strlen((const char *)out);
61 #define EVP_DecodeBlock git_CC_EVP_DecodeBlock
62 static int inline git_CC_EVP_DecodeBlock(unsigned char *out,
63 const unsigned char *in, int inlen)
65 CFErrorRef err;
66 SecTransformRef decoder;
67 CFDataRef input, output;
68 CFIndex length;
70 decoder = SecDecodeTransformCreate(kSecBase64Encoding, &err);
71 git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
73 input = CFDataCreate(kCFAllocatorDefault, in, inlen);
74 SecTransformSetAttribute(decoder, kSecTransformInputAttributeName,
75 input, &err);
76 git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
78 output = SecTransformExecute(decoder, &err);
79 git_CC_error_check("SecTransformExecute failed: %ld", err);
81 length = CFDataGetLength(output);
82 CFDataGetBytes(output, CFRangeMake(0, length), out);
84 CFRelease(output);
85 CFRelease(input);
86 CFRelease(decoder);
88 return (int)strlen((const char *)out);
90 #endif /* APPLE_LION_OR_NEWER */