Merge branch 'jm/doc-wording-tweaks' into maint
[alt-git.git] / compat / apple-common-crypto.h
blobc8b9b0e1a6ce63d16aec142611d03fbe24784b43
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 #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)
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)
31 CFErrorRef err;
32 SecTransformRef encoder;
33 CFDataRef input, output;
34 CFIndex length;
36 encoder = SecEncodeTransformCreate(kSecBase64Encoding, &err);
37 git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
39 input = CFDataCreate(kCFAllocatorDefault, in, inlen);
40 SecTransformSetAttribute(encoder, kSecTransformInputAttributeName,
41 input, &err);
42 git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
44 output = SecTransformExecute(encoder, &err);
45 git_CC_error_check("SecTransformExecute failed: %ld", err);
47 length = CFDataGetLength(output);
48 CFDataGetBytes(output, CFRangeMake(0, length), out);
50 CFRelease(output);
51 CFRelease(input);
52 CFRelease(encoder);
54 return (int)strlen((const char *)out);
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)
61 CFErrorRef err;
62 SecTransformRef decoder;
63 CFDataRef input, output;
64 CFIndex length;
66 decoder = SecDecodeTransformCreate(kSecBase64Encoding, &err);
67 git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
69 input = CFDataCreate(kCFAllocatorDefault, in, inlen);
70 SecTransformSetAttribute(decoder, kSecTransformInputAttributeName,
71 input, &err);
72 git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
74 output = SecTransformExecute(decoder, &err);
75 git_CC_error_check("SecTransformExecute failed: %ld", err);
77 length = CFDataGetLength(output);
78 CFDataGetBytes(output, CFRangeMake(0, length), out);
80 CFRelease(output);
81 CFRelease(input);
82 CFRelease(decoder);
84 return (int)strlen((const char *)out);
86 #endif /* APPLE_LION_OR_NEWER */