compat / apple-common-crypto.hon commit clone: do not let --depth imply --shallow-submodules (18a74a0)
   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#ifndef SHA1_MAX_BLOCK_SIZE
  20#error Using Apple Common Crypto library requires setting SHA1_MAX_BLOCK_SIZE
  21#endif
  22
  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)
  30
  31#define EVP_EncodeBlock git_CC_EVP_EncodeBlock
  32static inline int git_CC_EVP_EncodeBlock(unsigned char *out,
  33                const unsigned char *in, int inlen)
  34{
  35        CFErrorRef err;
  36        SecTransformRef encoder;
  37        CFDataRef input, output;
  38        CFIndex length;
  39
  40        encoder = SecEncodeTransformCreate(kSecBase64Encoding, &err);
  41        git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
  42
  43        input = CFDataCreate(kCFAllocatorDefault, in, inlen);
  44        SecTransformSetAttribute(encoder, kSecTransformInputAttributeName,
  45                        input, &err);
  46        git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
  47
  48        output = SecTransformExecute(encoder, &err);
  49        git_CC_error_check("SecTransformExecute failed: %ld", err);
  50
  51        length = CFDataGetLength(output);
  52        CFDataGetBytes(output, CFRangeMake(0, length), out);
  53
  54        CFRelease(output);
  55        CFRelease(input);
  56        CFRelease(encoder);
  57
  58        return (int)strlen((const char *)out);
  59}
  60
  61#define EVP_DecodeBlock git_CC_EVP_DecodeBlock
  62static int inline git_CC_EVP_DecodeBlock(unsigned char *out,
  63                const unsigned char *in, int inlen)
  64{
  65        CFErrorRef err;
  66        SecTransformRef decoder;
  67        CFDataRef input, output;
  68        CFIndex length;
  69
  70        decoder = SecDecodeTransformCreate(kSecBase64Encoding, &err);
  71        git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
  72
  73        input = CFDataCreate(kCFAllocatorDefault, in, inlen);
  74        SecTransformSetAttribute(decoder, kSecTransformInputAttributeName,
  75                        input, &err);
  76        git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
  77
  78        output = SecTransformExecute(decoder, &err);
  79        git_CC_error_check("SecTransformExecute failed: %ld", err);
  80
  81        length = CFDataGetLength(output);
  82        CFDataGetBytes(output, CFRangeMake(0, length), out);
  83
  84        CFRelease(output);
  85        CFRelease(input);
  86        CFRelease(decoder);
  87
  88        return (int)strlen((const char *)out);
  89}
  90#endif /* APPLE_LION_OR_NEWER */