[darwinbuild-changes] [857] branches/PR-8279204/darwinup

source_changes at macosforge.org source_changes at macosforge.org
Fri Aug 6 11:33:22 PDT 2010


Revision: 857
          http://trac.macosforge.org/projects/darwinbuild/changeset/857
Author:   wsiegrist at apple.com
Date:     2010-08-06 11:33:20 -0700 (Fri, 06 Aug 2010)
Log Message:
-----------
Replace OpenSSL with CommonCrypto in darwinup

Modified Paths:
--------------
    branches/PR-8279204/darwinup/DB.cpp
    branches/PR-8279204/darwinup/Digest.cpp
    branches/PR-8279204/darwinup/Digest.h

Modified: branches/PR-8279204/darwinup/DB.cpp
===================================================================
--- branches/PR-8279204/darwinup/DB.cpp	2010-08-06 16:48:24 UTC (rev 856)
+++ branches/PR-8279204/darwinup/DB.cpp	2010-08-06 18:33:20 UTC (rev 857)
@@ -155,11 +155,11 @@
 	uint64_t size;
 	memcpy(&size, &data[this->file_offset(6)], sizeof(uint64_t));
 
-	Digest* digest = NULL;
+	SHA1Digest* digest = NULL;
 	uint8_t* dp;
 	memcpy(&dp, &data[this->file_offset(7)], sizeof(uint8_t*));
 	if (dp) {
-		digest = new Digest();
+		digest = new SHA1Digest();
 		digest->m_size = 20; // size of SHA1 hash
 		memcpy(digest->m_data, dp, 20);
 	}

Modified: branches/PR-8279204/darwinup/Digest.cpp
===================================================================
--- branches/PR-8279204/darwinup/Digest.cpp	2010-08-06 16:48:24 UTC (rev 856)
+++ branches/PR-8279204/darwinup/Digest.cpp	2010-08-06 18:33:20 UTC (rev 857)
@@ -49,50 +49,6 @@
 #include "redo_prebinding.h"
 }
 
-Digest::Digest() {
-	memset(m_data, 0, sizeof(m_data));
-	m_size = 0;
-}
-
-Digest::Digest(const EVP_MD* md, int fd) {
-	digest(md, fd);
-}
-
-void Digest::digest(const EVP_MD* md, int fd) {
-	EVP_MD_CTX ctx;
-	EVP_MD_CTX_init(&ctx);
-	EVP_DigestInit(&ctx, md);
-
-	int len;
-	const unsigned int blocklen = 8192;
-	static uint8_t* block = NULL;
-	if (block == NULL) {
-		block = (uint8_t*)malloc(blocklen);
-	}
-	while(1) {
-		len = read(fd, block, blocklen);
-		if (len == 0) { close(fd); break; }
-		if ((len < 0) && (errno == EINTR)) continue;
-		if (len < 0) { close(fd); return; }
-		EVP_DigestUpdate(&ctx, block, len);
-	}
-	if (len >= 0) {
-		EVP_DigestFinal(&ctx, m_data, &m_size);
-	}
-}
-
-Digest::Digest(const EVP_MD* md, uint8_t* data, uint32_t size) {
-	digest(md, data, size);
-}
-
-void Digest::digest(const EVP_MD* md, uint8_t* data, uint32_t size) {
-	EVP_MD_CTX ctx;
-	EVP_MD_CTX_init(&ctx);
-	EVP_DigestInit(&ctx, md);
-	EVP_DigestUpdate(&ctx, data, size);
-	EVP_DigestFinal(&ctx, m_data, &m_size);
-}
-
 uint8_t*	Digest::data() { return m_data; }
 uint32_t	Digest::size() { return m_size; }
 
@@ -121,45 +77,51 @@
 	return (memcmp(a->data(), b->data(), a_size) == 0);
 }
 
-
-const EVP_MD* SHA1Digest::m_md;
-
 SHA1Digest::SHA1Digest() {
-	if (m_md == NULL) {
-		OpenSSL_add_all_digests();
-		m_md = EVP_get_digestbyname("sha1");
-		assert(m_md != NULL);
-	}
+	m_size = CC_SHA1_DIGEST_LENGTH;
 }
 
 SHA1Digest::SHA1Digest(int fd) {
-	if (m_md == NULL) {
-		OpenSSL_add_all_digests();
-		m_md = EVP_get_digestbyname("sha1");
-		assert(m_md != NULL);
-	}
-	digest(m_md, fd);
+	m_size = CC_SHA1_DIGEST_LENGTH;
+	digest(m_data, fd);
 }
 
 SHA1Digest::SHA1Digest(const char* filename) {
+	m_size = CC_SHA1_DIGEST_LENGTH;
 	int fd = open(filename, O_RDONLY);
-	if (m_md == NULL) {
-		OpenSSL_add_all_digests();
-		m_md = EVP_get_digestbyname("sha1");
-		assert(m_md != NULL);
-	}
-	digest(m_md, fd);
+	digest(m_data, fd);
 }
 
 SHA1Digest::SHA1Digest(uint8_t* data, uint32_t size) {
-	if (m_md == NULL) {
-		OpenSSL_add_all_digests();
-		m_md = EVP_get_digestbyname("sha1");
-		assert(m_md != NULL);
+	m_size = CC_SHA1_DIGEST_LENGTH;
+	digest(m_data, data, size);
+}
+
+void SHA1Digest::digest(unsigned char* md, int fd) {
+	CC_SHA1_CTX c;
+	CC_SHA1_Init(&c);
+	
+	int len;
+	const unsigned int blocklen = 8192;
+	static uint8_t* block = NULL;
+	if (block == NULL) {
+		block = (uint8_t*)malloc(blocklen);
 	}
-	digest(m_md, data, size);
+	while(1) {
+		len = read(fd, block, blocklen);
+		if (len == 0) { close(fd); break; }
+		if ((len < 0) && (errno == EINTR)) continue;
+		if (len < 0) { close(fd); return; }
+		CC_SHA1_Update(&c, block, (size_t)len);
+	}
+	if (len >= 0) {
+		CC_SHA1_Final(md, &c);
+	}	
 }
 
+void SHA1Digest::digest(unsigned char* md, uint8_t* data, uint32_t size) {
+	CC_SHA1((const void*)data, (CC_LONG)size, md);
+}
 
 SHA1DigestMachO::SHA1DigestMachO(const char* filename) {
 	char* error = NULL;
@@ -182,11 +144,11 @@
 			&block,
 			&blocklen);
 		if (ret == REDO_PREBINDING_SUCCESS && block != NULL) {
-			digest(SHA1Digest::m_md, (uint8_t*)block, blocklen);
+			digest(m_data, (uint8_t*)block, blocklen);
 		} else {
 			//fprintf(stderr, "%s:%d: unexpected unprebind result: %s: %s (%d)\n", __FILE__, __LINE__, filename, error, ret);
 			int fd = open(filename, O_RDONLY);
-			digest(SHA1Digest::m_md, fd);
+			digest(m_data, fd);
 			close(fd);
 		}
 		if (block != NULL) {
@@ -195,7 +157,7 @@
 		}
 	} else {
 		int fd = open(filename, O_RDONLY);
-		digest(SHA1Digest::m_md, fd);
+		digest(m_data, fd);
 		close(fd);
 	}
 }
@@ -206,6 +168,6 @@
 	if (res == -1) {
 		fprintf(stderr, "%s:%d: readlink: %s: %s (%d)\n", __FILE__, __LINE__, filename, strerror(errno), errno);
 	} else {
-		digest(SHA1Digest::m_md, (uint8_t*)link, res);
+		digest(m_data, (uint8_t*)link, res);
 	}
 }

Modified: branches/PR-8279204/darwinup/Digest.h
===================================================================
--- branches/PR-8279204/darwinup/Digest.h	2010-08-06 16:48:24 UTC (rev 856)
+++ branches/PR-8279204/darwinup/Digest.h	2010-08-06 18:33:20 UTC (rev 857)
@@ -35,34 +35,25 @@
 
 #include <sys/types.h>
 #include <stdint.h>
-#include <openssl/evp.h>
+#include <CommonCrypto/CommonDigest.h>
 
 #include "Utils.h"
 
 ////
 //  Digest
 //
-//  Digest is the root class for all message digest algorithms
-//  supported by darwinup.
+//  Digest is the abstract root class for all message digest algorithms
+//  supported by darwinup. Subclasses must implement the constructors 
+//  and digest() APIs.
 //
-//  Conceptually it's an abstract class, although that
-//  hasn't been formalized.
-//
 //  SHA1Digest is the only concrete subclass.  There are two
 //  subclasses of SHA1Digest which add convenience functions
 //  for digesting a canonicalized Mach-O binary, and the
 //  target of a symlink obtained by readlink(2).
 //
-//  NOTE: It might be more appropriate to use the CommonCrypto
-//  implementation of these algorithms rather than the OpenSSL
-//  implementation.  However, CommonCrypto is only available on
-//  Tiger.
 ////
 
 struct Digest {
-	Digest();
-	Digest(const EVP_MD* md, int fd);
-	Digest(const EVP_MD* md, uint8_t* data, uint32_t size);
 	
 	////
 	//  Accessor functions
@@ -74,7 +65,7 @@
 	// Returns the size of the raw digest.
 	virtual uint32_t	size();
 	
-	// Returns the digest as an ASCIZ string, represented in hexidecimal.
+	// Returns the digest as an ASCII string, represented in hexidecimal.
 	virtual char*		string();
 	
 	////
@@ -88,11 +79,11 @@
 
 	protected:
 
-	virtual	void	digest(const EVP_MD* md, int fd);
-	virtual	void	digest(const EVP_MD* md, uint8_t* data, uint32_t size);
+	virtual	void	digest(unsigned char* md, int fd) = 0;
+	virtual	void	digest(unsigned char* md, uint8_t* data, uint32_t size) = 0;
 
-	uint8_t		m_data[EVP_MAX_MD_SIZE];
-	uint32_t	m_size;
+	unsigned char m_data[CC_SHA512_DIGEST_LENGTH]; // support up to 64 bytes
+	uint32_t	  m_size;
 	
 	friend struct Depot;
 	friend struct DarwinupDatabase;
@@ -102,8 +93,6 @@
 //  SHA1Digest
 ////
 struct SHA1Digest : Digest {
-	static const EVP_MD* m_md;
-	
 	// Creates an empty digest.
 	SHA1Digest();
 	
@@ -115,6 +104,10 @@
 	
 	// Computes the SHA-1 digest of the block of memory.
 	SHA1Digest(uint8_t* data, uint32_t size);
+	
+	void	digest(unsigned char* md, int fd);
+	void	digest(unsigned char* md, uint8_t* data, uint32_t size);
+
 };
 
 ////
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100806/119511b8/attachment.html>


More information about the darwinbuild-changes mailing list