openssl/lk: Enable signed boot img check

This uses openssl to check the boot image prior
to jumping to kernel to see if it is a signed
kernel image.

Change-Id: I92927fd9317a0f701dab395cc6d39929c64340c1
diff --git a/AndroidBoot.mk b/AndroidBoot.mk
index 4dd8553..432063d 100644
--- a/AndroidBoot.mk
+++ b/AndroidBoot.mk
@@ -29,6 +29,11 @@
   TARGET_EMMC := 1
 endif
 
+ifeq ($(TARGET_BOOTIMG_SIGNED),true)
+  SIGNED_KERNEL := SIGNED_KERNEL=1
+else
+  SIGNED_KERNEL := SIGNED_KERNEL=0
+endif
 
 ifeq ($(TARGET_EMMC),1)
 TARGET_BOOTLOADER_EMMC := $(PRODUCT_OUT)/EMMCBOOT.MBN
@@ -42,13 +47,13 @@
 	mkdir -p $(BOOTLOADER_EMMC_OUT)
 
 $(TARGET_BOOTLOADER): appsbootldr_clean emmc_appsbootldr_clean $(BOOTLOADER_OUT) $(BOOTLOADER_EMMC_OUT)
-	$(MAKE) -C bootable/bootloader/lk TOOLCHAIN_PREFIX=$(CROSS_TOOL) BOOTLOADER_OUT=../../../$(BOOTLOADER_OUT) $(TARGET_PRODUCT)
-	$(MAKE) -C bootable/bootloader/lk TOOLCHAIN_PREFIX=$(CROSS_TOOL) BOOTLOADER_OUT=../../../$(BOOTLOADER_EMMC_OUT) $(TARGET_PRODUCT) EMMC_BOOT=1
+	$(MAKE) -C bootable/bootloader/lk TOOLCHAIN_PREFIX=$(CROSS_TOOL) BOOTLOADER_OUT=../../../$(BOOTLOADER_OUT) $(TARGET_PRODUCT) $(SIGNED_KERNEL)
+	$(MAKE) -C bootable/bootloader/lk TOOLCHAIN_PREFIX=$(CROSS_TOOL) BOOTLOADER_OUT=../../../$(BOOTLOADER_EMMC_OUT) $(TARGET_PRODUCT) EMMC_BOOT=1 $(SIGNED_KERNEL)
 
 else
 
 $(TARGET_BOOTLOADER): appsbootldr_clean $(BOOTLOADER_OUT)
-	$(MAKE) -C bootable/bootloader/lk TOOLCHAIN_PREFIX=$(CROSS_TOOL) BOOTLOADER_OUT=../../../$(BOOTLOADER_OUT) $(TARGET_PRODUCT)
+	$(MAKE) -C bootable/bootloader/lk TOOLCHAIN_PREFIX=$(CROSS_TOOL) BOOTLOADER_OUT=../../../$(BOOTLOADER_OUT) $(TARGET_PRODUCT) $(SIGNED_KERNEL)
 
 endif
 
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index b6c60f2..312b278 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -47,7 +47,9 @@
 #include <mmc.h>
 #include <partition_parser.h>
 #include <platform.h>
+#include <crypto_hash.h>
 
+#include "image_verify.h"
 #include "recovery.h"
 #include "bootimg.h"
 #include "fastboot.h"
@@ -72,12 +74,16 @@
 static const char *emmc_cmdline = " androidboot.emmc=true";
 static const char *usb_sn_cmdline = " androidboot.serialno=";
 static const char *battchg_pause = " androidboot.battchg_pause=true";
+static const char *auth_kernel = " androidboot.authorized_kernel=true";
 
 static const char *baseband_apq     = " androidboot.baseband=apq";
 static const char *baseband_msm     = " androidboot.baseband=msm";
 static const char *baseband_csfb    = " androidboot.baseband=csfb";
 static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
 
+/* Assuming unauthorized kernel image by default */
+static int auth_kernel_img = 0;
+
 static struct udc_device surf_udc_device = {
 	.vendor_id	= 0x18d1,
 	.product_id	= 0xD00D,
@@ -175,6 +181,10 @@
 		cmdline_len += strlen(battchg_pause);
 	}
 
+	if(target_use_signed_kernel() && auth_kernel_img) {
+		cmdline_len += strlen(auth_kernel);
+	}
+
 	/* Determine correct androidboot.baseband to use */
 	switch(target_baseband())
 	{
@@ -230,6 +240,12 @@
 			while ((*dst++ = *src++));
 		}
 
+		if(target_use_signed_kernel() && auth_kernel_img) {
+			src = auth_kernel;
+			if (have_cmdline) --dst;
+			while ((*dst++ = *src++));
+		}
+
 		switch(target_baseband())
 		{
 			case BASEBAND_APQ:
@@ -293,6 +309,11 @@
 	const char *cmdline;
 	int index = INVALID_PTN;
 
+	unsigned char *image_addr = 0;
+	unsigned kernel_actual;
+	unsigned ramdisk_actual;
+	unsigned imagesize_actual;
+
 	uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
 	if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
 		dprintf(INFO, "Unified boot method!\n");
@@ -330,24 +351,62 @@
 		page_size = hdr->page_size;
 		page_mask = page_size - 1;
 	}
-	offset += page_size;
 
-	n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
-	if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
-		dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
-                return -1;
-	}
-	offset += n;
-
-	n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
-	if(n != 0)
+	/* Authenticate Kernel */
+	if(target_use_signed_kernel())
 	{
-		if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
-			dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
-			return -1;
+		image_addr = (unsigned char *)target_get_scratch_address();
+		kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
+		ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
+		imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
+
+		offset = 0;
+		/* Read image without signature */
+		if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
+		{
+			dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
+				return -1;
 		}
+
+		offset = imagesize_actual;
+		/* Read signature */
+		if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
+		{
+			dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
+		}
+		else
+		{
+			auth_kernel_img = image_verify((unsigned char *)image_addr,
+					(unsigned char *)(image_addr + imagesize_actual),
+					imagesize_actual,
+					CRYPTO_AUTH_ALG_SHA256);
+		}
+
+		/* Move kernel and ramdisk to correct address */
+		memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
+		memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
 	}
-	offset += n;
+	else
+	{
+		offset += page_size;
+
+		n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
+		if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
+			dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
+					return -1;
+		}
+		offset += n;
+
+		n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
+		if(n != 0)
+		{
+			if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
+				dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
+				return -1;
+			}
+		}
+		offset += n;
+	}
 
 unified_boot:
 	dprintf(INFO, "\nkernel  @ %x (%d bytes)\n", hdr->kernel_addr,
@@ -379,6 +438,11 @@
 	unsigned offset = 0;
 	const char *cmdline;
 
+	unsigned char *image_addr = 0;
+	unsigned kernel_actual;
+	unsigned ramdisk_actual;
+	unsigned imagesize_actual;
+
 	if (target_is_emmc_boot()) {
 		hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
 		if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
@@ -427,20 +491,58 @@
 		return -1;
 	}
 
-	n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
-	if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
-		dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
-		return -1;
-	}
-	offset += n;
+	/* Authenticate Kernel */
+	if(target_use_signed_kernel())
+	{
+		image_addr = (unsigned char *)target_get_scratch_address();
+		kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
+		ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
+		imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
 
-	n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
-	if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
-		dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
-		return -1;
-	}
-	offset += n;
+		offset = 0;
+		/* Read image without signature */
+		if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
+		{
+			dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
+				return -1;
+		}
 
+		offset = imagesize_actual;
+		/* Read signature */
+		if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
+		{
+			dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
+		}
+		else
+		{
+
+			/* Verify signature */
+			auth_kernel_img = image_verify((unsigned char *)image_addr,
+						(unsigned char *)(image_addr + imagesize_actual),
+						imagesize_actual,
+						CRYPTO_AUTH_ALG_SHA256);
+		}
+
+		/* Move kernel and ramdisk to correct address */
+		memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
+		memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
+	}
+	else
+	{
+		n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
+		if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
+			dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
+			return -1;
+		}
+		offset += n;
+
+		n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
+		if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
+			dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
+			return -1;
+		}
+		offset += n;
+	}
 continue_boot:
 	dprintf(INFO, "\nkernel  @ %x (%d bytes)\n", hdr->kernel_addr,
 		hdr->kernel_size);
diff --git a/lib/openssl/android-config.mk b/lib/openssl/android-config.mk
index f9f59aa..b1e4cc4 100644
--- a/lib/openssl/android-config.mk
+++ b/lib/openssl/android-config.mk
@@ -18,6 +18,9 @@
 	  -DLK_NO_STDERR -DLK_NO_ABORT -DLK_NO_PEM -DLK_NO_OAEP -DLK_NO_SSLV23 \
 	  -DLK_NO_RAND -DLK_NO_UNISTD
 
+# TODO: Memory leak using openssl ERR_STATEs need to find root cause
+CFLAGS += -DLK_NO_ERR_STATE
+
 # TODO: Should be able to classify these ones
 CFLAGS += -DOPENSSL_LK
 
diff --git a/lib/openssl/crypto/err/err.c b/lib/openssl/crypto/err/err.c
index 69713a6..6237c1b 100644
--- a/lib/openssl/crypto/err/err.c
+++ b/lib/openssl/crypto/err/err.c
@@ -708,6 +708,14 @@
 	{
 	ERR_STATE *es;
 
+	/* Stubbing out ERR_get_state due to memleak with
+	 * ERR_remove_thread_state. We don't check these ERR_STATE
+	 * anyways for now.
+	 */
+#ifdef LK_NO_ERR_STATE
+	return
+#endif
+
 #ifdef _OSD_POSIX
 	/* In the BS2000-OSD POSIX subsystem, the compiler generates
 	 * path names in the form "*POSIX(/etc/passwd)".
@@ -751,6 +759,7 @@
 		err_clear(es,i);
 		}
 	es->top=es->bottom=0;
+	ERR_STATE_free(es);
 	}
 
 
@@ -1004,6 +1013,14 @@
 ERR_STATE *ERR_get_state(void)
 	{
 	static ERR_STATE fallback;
+	/* TODO: ERR_remove_thread_state does not clean up
+	 * therefore when using ERR_states the malloc here
+	 * will never get freed and there is a memleak only
+	 * in the case where it fails decryption.
+	 */
+#ifdef LK_NO_ERR_STATE
+	return NULL;
+#endif
 	ERR_STATE *ret,tmp,*tmpp=NULL;
 	int i;
 	CRYPTO_THREADID tid;
diff --git a/makefile b/makefile
index da8567d..4c31db2 100644
--- a/makefile
+++ b/makefile
@@ -53,6 +53,11 @@
 ifeq ($(EMMC_BOOT),1)
   CFLAGS += -D_EMMC_BOOT=1
 endif
+
+ifeq ($(SIGNED_KERNEL),1)
+  CFLAGS += -D_SIGNED_KERNEL=1
+endif
+
 # When the host arch is ARM, ensure stack protection code is not emitted since
 # it's not supported by the bootloader's libc
 ifneq ($(shell uname -m | grep "arm.*"),)
diff --git a/platform/init.c b/platform/init.c
index 11bcb8b..49e6e75 100644
--- a/platform/init.c
+++ b/platform/init.c
@@ -60,3 +60,11 @@
 __WEAK void platform_uninit(void)
 {
 }
+
+__WEAK int image_verify(unsigned char * image_ptr,
+			unsigned char * signature_ptr,
+			unsigned int image_size,
+			unsigned hash_type)
+{
+	return 0;
+}
diff --git a/platform/msm_shared/certificate.c b/platform/msm_shared/certificate.c
new file mode 100644
index 0000000..81d3746
--- /dev/null
+++ b/platform/msm_shared/certificate.c
@@ -0,0 +1,122 @@
+/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of Code Aurora Forum, Inc. nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#include <certificate.h>
+
+const char certBuffer [CERTIFICATE_SIZE] ={
+0x30, 0x82, 0x02, 0xd0, 0x30, 0x82, 0x01, 0xb8, 0x02, 0x01, 0x05, 0x30, 0x0d,
+ 0x06, 0x09, 0x2a,
+0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x2f, 0x31,
+ 0x0b, 0x30, 0x09,
+0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09,
+ 0x06, 0x03, 0x55,
+0x04, 0x08, 0x0c, 0x02, 0x43, 0x41, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
+ 0x04, 0x0a, 0x0c,
+0x0a, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x20, 0x4c, 0x4b, 0x30, 0x1e,
+ 0x17, 0x0d, 0x31,
+0x31, 0x30, 0x38, 0x30, 0x31, 0x31, 0x34, 0x35, 0x35, 0x33, 0x35, 0x5a, 0x17,
+ 0x0d, 0x33, 0x32,
+0x30, 0x32, 0x31, 0x32, 0x31, 0x34, 0x35, 0x35, 0x33, 0x35, 0x5a, 0x30, 0x2f,
+ 0x31, 0x0b, 0x30,
+0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30,
+ 0x09, 0x06, 0x03,
+0x55, 0x04, 0x08, 0x0c, 0x02, 0x43, 0x41, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
+ 0x55, 0x04, 0x0a,
+0x0c, 0x0a, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x20, 0x4c, 0x4b, 0x30,
+ 0x82, 0x01, 0x20,
+0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
+ 0x05, 0x00, 0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, 0xe1, 0x4e, 0x64, 0xea, 0x3b, 0xea, 0x6f, 0x30, 0xad, 0x2c,
+ 0x4a, 0x0e, 0xe2,
+0x79, 0x9c, 0xd3, 0x83};
diff --git a/platform/msm_shared/image_verify.c b/platform/msm_shared/image_verify.c
new file mode 100644
index 0000000..0458c76
--- /dev/null
+++ b/platform/msm_shared/image_verify.c
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <x509.h>
+#include <certificate.h>
+#include <crypto_hash.h>
+#include "image_verify.h"
+
+/*
+ * Returns -1 if decryption failed otherwise size of plain_text in bytes
+ */
+static int image_decrypt_signature(unsigned char * signature_ptr,
+					unsigned char * plain_text){
+	/*
+	 * Extract Public Key and Decrypt Signature
+	 */
+	int ret = -1;
+	X509 *x509_certificate = NULL;
+	unsigned char *cert_ptr = certBuffer;
+	unsigned int cert_size = sizeof(certBuffer);
+	EVP_PKEY *pub_key = NULL;
+	RSA* rsa_key = NULL;
+
+	/*
+	 * Get Pubkey and Convert the internal EVP_PKEY to RSA internal struct
+	 */
+	if ((x509_certificate = d2i_X509(NULL, &cert_ptr, cert_size)) == NULL){
+		dprintf(CRITICAL,
+			"ERROR: Image Invalid, X509_Certificate is NULL!\n");
+		goto cleanup;
+	}
+	pub_key = X509_get_pubkey(x509_certificate);
+	rsa_key = EVP_PKEY_get1_RSA(pub_key);
+	if (rsa_key == NULL){
+		dprintf(CRITICAL,
+			"ERROR: Boot Invalid, RSA_KEY is NULL!\n");
+		goto cleanup;
+	}
+
+	ret = RSA_public_decrypt(SIGNATURE_SIZE, signature_ptr, plain_text,
+				rsa_key, RSA_PKCS1_PADDING);
+	dprintf(SPEW,
+		"DEBUG openssl: Return of RSA_public_decrypt = %d\n", ret);
+
+cleanup:
+	if (rsa_key != NULL)
+		RSA_free(rsa_key);
+	if (x509_certificate != NULL)
+		X509_free(x509_certificate);
+	if (pub_key != NULL)
+		EVP_PKEY_free(pub_key);
+	return ret;
+}
+
+/*
+ * Returns 1 when image is signed and authorized.
+ * Returns 0 when image is unauthorized.
+ * Expects a pointer to the start of image and pointer to start of sig
+ */
+int image_verify(unsigned char * image_ptr,
+			unsigned char * signature_ptr,
+			unsigned int image_size,
+			unsigned hash_type){
+
+	int ret = -1;
+	int auth = 0;
+	unsigned char * plain_text = NULL;
+	unsigned int digest[8];
+	unsigned int hash_size;
+
+	plain_text = (unsigned char*) calloc(sizeof(char), SIGNATURE_SIZE);
+	if (plain_text == NULL){
+		dprintf(CRITICAL, "ERROR: Calloc failed during verification\n");
+		goto cleanup;
+	}
+
+	ret = image_decrypt_signature(signature_ptr, plain_text);
+	if (ret == -1){
+		dprintf(CRITICAL, "ERROR: Image Invalid! Decryption failed!\n");
+		goto cleanup;
+	}
+
+	/*
+	 * Calculate hash of image for comparison
+	 */
+	hash_size = (hash_type == CRYPTO_AUTH_ALG_SHA256) ?
+						SHA256_SIZE : SHA1_SIZE;
+	hash_find(image_ptr, image_size,
+		(unsigned char*)&digest, hash_type);
+	if(memcmp(plain_text, digest, hash_size) != 0)
+	{
+		dprintf(CRITICAL,
+			"ERROR: Image Invalid! Please use another image!\n");
+		ret = -1;
+		goto cleanup;
+	}
+	else
+	{
+		/* Authorized image */
+		auth = 1;
+	}
+
+	/* Cleanup after complete usage of openssl - cached data and objects */
+cleanup:
+	if (plain_text != NULL)
+		free(plain_text);
+	EVP_cleanup();
+	CRYPTO_cleanup_all_ex_data();
+	ERR_remove_thread_state(NULL);
+	return auth;
+}
diff --git a/platform/msm_shared/include/certificate.h b/platform/msm_shared/include/certificate.h
new file mode 100644
index 0000000..7dde655
--- /dev/null
+++ b/platform/msm_shared/include/certificate.h
@@ -0,0 +1,31 @@
+/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of Code Aurora Forum, Inc. nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#define CERTIFICATE_SIZE 724
+
+extern const char certBuffer [CERTIFICATE_SIZE];
diff --git a/platform/msm_shared/include/image_verify.h b/platform/msm_shared/include/image_verify.h
new file mode 100644
index 0000000..96ab127
--- /dev/null
+++ b/platform/msm_shared/include/image_verify.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#ifndef __IMAGE_VERIFY_H
+#define __IMAGE_VERIFY_H
+
+#define SHA1_SIZE      16
+#define SHA256_SIZE    32
+/* For keys of length 2048 bits */
+#define SIGNATURE_SIZE 256
+
+static int image_decrypt_signature(unsigned char * signature_ptr,
+				unsigned char * plain_text);
+int image_verify(unsigned char * image_ptr,
+			unsigned char * signature_ptr,
+			unsigned int image_size,
+			unsigned hash_type);
+#endif
diff --git a/platform/msm_shared/rules.mk b/platform/msm_shared/rules.mk
index 1181c36..7887a8f 100644
--- a/platform/msm_shared/rules.mk
+++ b/platform/msm_shared/rules.mk
@@ -28,7 +28,9 @@
 			$(LOCAL_DIR)/lcdc.o \
 			$(LOCAL_DIR)/mddi.o \
 			$(LOCAL_DIR)/qgic.o \
-			$(LOCAL_DIR)/mdp4.o
+			$(LOCAL_DIR)/mdp4.o \
+			$(LOCAL_DIR)/certificate.o \
+			$(LOCAL_DIR)/image_verify.o
 endif
 
 ifeq ($(PLATFORM),apq8064)
@@ -43,7 +45,9 @@
 			$(LOCAL_DIR)/qgic.o \
 			$(LOCAL_DIR)/mdp4.o \
 			$(LOCAL_DIR)/crypto4_eng.o \
-			$(LOCAL_DIR)/crypto_hash.o
+			$(LOCAL_DIR)/crypto_hash.o \
+			$(LOCAL_DIR)/certificate.o \
+			$(LOCAL_DIR)/image_verify.o
 endif
 
 ifeq ($(PLATFORM),msm7x27a)
@@ -67,5 +71,7 @@
 			$(LOCAL_DIR)/uart.o \
 			$(LOCAL_DIR)/proc_comm.o \
 			$(LOCAL_DIR)/lcdc.o \
-			$(LOCAL_DIR)/mddi.o
+			$(LOCAL_DIR)/mddi.o \
+			$(LOCAL_DIR)/certificate.o \
+			$(LOCAL_DIR)/image_verify.o
 endif
diff --git a/target/init.c b/target/init.c
index 9dbcc9d..3da8d89 100644
--- a/target/init.c
+++ b/target/init.c
@@ -91,3 +91,12 @@
 {
 	return 0;
 }
+
+__WEAK bool target_use_signed_kernel(void)
+{
+#if _SIGNED_KERNEL
+	return 1;
+#else
+	return 0;
+#endif
+}