From 84d29a1cc49ce682e6c71aca327c12787c61615d Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 10 Oct 2015 15:36:30 -0700 Subject: Use Apple's CommonCrypto for SHA-1 when __APPLE__. --- ldid.cpp | 45 +++++++++++++++++++++++++++++++-------------- make.sh | 2 +- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/ldid.cpp b/ldid.cpp index 049338b..b863b35 100644 --- a/ldid.cpp +++ b/ldid.cpp @@ -46,7 +46,24 @@ #include #include #include + +#ifdef __APPLE__ +#include +#define LDID_SHA1_DIGEST_LENGTH CC_SHA1_DIGEST_LENGTH +#define LDID_SHA1 CC_SHA1 +#define LDID_SHA1_CTX CC_SHA1_CTX +#define LDID_SHA1_Init CC_SHA1_Init +#define LDID_SHA1_Update CC_SHA1_Update +#define LDID_SHA1_Final CC_SHA1_Final +#else #include +#define LDID_SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH +#define LDID_SHA1 SHA1 +#define LDID_SHA1_CTX SHA_CTX +#define LDID_SHA1_Init SHA1_Init +#define LDID_SHA1_Update SHA1_Update +#define LDID_SHA1_Final SHA1_Final +#endif #include @@ -843,7 +860,7 @@ struct CodeDirectory { extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval); static void sha1(uint8_t *hash, const void *data, size_t size) { - SHA1(static_cast(data), size, hash); + LDID_SHA1(static_cast(data), size, hash); } struct CodesignAllocation { @@ -1317,22 +1334,22 @@ class HashBuffer : { private: std::vector &hash_; - SHA_CTX context_; + LDID_SHA1_CTX context_; public: HashBuffer(std::vector &hash) : hash_(hash) { - SHA1_Init(&context_); + LDID_SHA1_Init(&context_); } ~HashBuffer() { - hash_.resize(SHA_DIGEST_LENGTH); - SHA1_Final(reinterpret_cast(hash_.data()), &context_); + hash_.resize(LDID_SHA1_DIGEST_LENGTH); + LDID_SHA1_Final(reinterpret_cast(hash_.data()), &context_); } virtual std::streamsize xsputn(const char_type *data, std::streamsize size) { - SHA1_Update(&context_, data, size); + LDID_SHA1_Update(&context_, data, size); return size; } @@ -1455,7 +1472,7 @@ void Sign(const void *idata, size_t isize, std::streambuf &output, const std::st special = std::max(special, slot.first); uint32_t normal((size + PageSize_ - 1) / PageSize_); - alloc = Align(alloc + (special + normal) * SHA_DIGEST_LENGTH, 16); + alloc = Align(alloc + (special + normal) * LDID_SHA1_DIGEST_LENGTH, 16); return alloc; }), fun([&](std::streambuf &output, size_t limit, const std::string &overlap, const char *top) -> size_t { Blobs blobs; @@ -1488,12 +1505,12 @@ void Sign(const void *idata, size_t isize, std::streambuf &output, const std::st CodeDirectory directory; directory.version = Swap(uint32_t(0x00020001)); directory.flags = Swap(uint32_t(0)); - directory.hashOffset = Swap(uint32_t(sizeof(Blob) + sizeof(CodeDirectory) + identifier.size() + 1 + SHA_DIGEST_LENGTH * special)); + directory.hashOffset = Swap(uint32_t(sizeof(Blob) + sizeof(CodeDirectory) + identifier.size() + 1 + LDID_SHA1_DIGEST_LENGTH * special)); directory.identOffset = Swap(uint32_t(sizeof(Blob) + sizeof(CodeDirectory))); directory.nSpecialSlots = Swap(special); directory.codeLimit = Swap(uint32_t(limit)); directory.nCodeSlots = Swap(normal); - directory.hashSize = SHA_DIGEST_LENGTH; + directory.hashSize = LDID_SHA1_DIGEST_LENGTH; directory.hashType = CS_HASHTYPE_SHA1; directory.spare1 = 0x00; directory.pageSize = PageShift_; @@ -1502,8 +1519,8 @@ void Sign(const void *idata, size_t isize, std::streambuf &output, const std::st put(data, identifier.c_str(), identifier.size() + 1); - uint8_t storage[special + normal][SHA_DIGEST_LENGTH]; - uint8_t (*hashes)[SHA_DIGEST_LENGTH] = storage + special; + uint8_t storage[special + normal][LDID_SHA1_DIGEST_LENGTH]; + uint8_t (*hashes)[LDID_SHA1_DIGEST_LENGTH] = storage + special; memset(storage, 0, sizeof(*storage) * special); @@ -1889,7 +1906,7 @@ std::string Bundle(const std::string &root, Folder &folder, const std::string &k copy(data, proxy); })); - _assert(hash.size() == SHA_DIGEST_LENGTH); + _assert(hash.size() == LDID_SHA1_DIGEST_LENGTH); })); auto plist(plist_new_dict()); @@ -2056,7 +2073,7 @@ int main(int argc, char *argv[]) { unsigned number(strtoul(slot, &arge, 0)); _assert(arge == colon); std::vector &hash(slots[number]); - hash.resize(SHA_DIGEST_LENGTH); + hash.resize(LDID_SHA1_DIGEST_LENGTH); sha1(reinterpret_cast(hash.data()), file.data(), file.size()); } break; @@ -2234,7 +2251,7 @@ int main(int argc, char *argv[]) { uint32_t begin = Swap(super->index[index].offset); struct CodeDirectory *directory = reinterpret_cast(blob + begin); - uint8_t (*hashes)[SHA_DIGEST_LENGTH] = reinterpret_cast(blob + begin + Swap(directory->hashOffset)); + uint8_t (*hashes)[LDID_SHA1_DIGEST_LENGTH] = reinterpret_cast(blob + begin + Swap(directory->hashOffset)); uint32_t pages = Swap(directory->nCodeSlots); if (pages != 1) diff --git a/make.sh b/make.sh index 404c3d2..b71b3ef 100755 --- a/make.sh +++ b/make.sh @@ -60,7 +60,7 @@ set -x plist=(-Llibplist/src/.libs -lplist) "${flags[@]}" -c -std=c++11 -o "${out}"/ldid.o ldid.cpp -"${flags[@]}" -o "${out}"/ldid "${out}"/ldid.o "${os[@]}" -x c lookup2.c -lxml2 -lcrypto "${plist[@]}" +"${flags[@]}" -o "${out}"/ldid "${out}"/ldid.o "${os[@]}" -x c lookup2.c -lxml2 -framework Security -lcrypto "${plist[@]}" if ! "${ios}"; then ln -sf out/ldid . -- cgit v1.2.3-56-ge451