platform: msm_shared: Decouple save kernel hash

The hash of the kernel is sent to tz. This is only needed
when the image is verified successfully. This change prevents
sending the hash to TZ twice in case of calling the
boot_verify_compare_sha256 function twice

Change-Id: I0dbe72597fbb57164e5a223d689b9d8f26b16ba9
diff --git a/platform/msm_shared/boot_verifier.c b/platform/msm_shared/boot_verifier.c
index 4819eeb..e427f81 100644
--- a/platform/msm_shared/boot_verifier.c
+++ b/platform/msm_shared/boot_verifier.c
@@ -197,6 +197,9 @@
 	if(ret == 0)
 	{
 		auth = true;
+#ifdef TZ_SAVE_KERNEL_HASH
+		save_kernel_hash((unsigned char *) &digest, CRYPTO_AUTH_ALG_SHA256);
+#endif
 	}
 
 cleanup:
diff --git a/platform/msm_shared/image_verify.c b/platform/msm_shared/image_verify.c
index cfdb5b9..6fff3d2 100644
--- a/platform/msm_shared/image_verify.c
+++ b/platform/msm_shared/image_verify.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011,2013-2014 The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011,2013-2015 The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -113,14 +113,18 @@
 	 * Calculate hash of image and save calculated hash on TZ.
 	 */
 	hash_find(image_ptr, image_size, (unsigned char *)digest, hash_type);
+}
+
 #ifdef TZ_SAVE_KERNEL_HASH
+void save_kernel_hash(unsigned char *digest, unsigned hash_type)
+{
 	if (hash_type == CRYPTO_AUTH_ALG_SHA256) {
 		save_kernel_hash_cmd(digest);
 		dprintf(INFO, "Image hash saved.\n");
 	} else
 		dprintf(INFO, "image_verify: hash is not SHA-256.\n");
-#endif
 }
+#endif
 
 /*
  * Returns 1 when image is signed and authorized.
@@ -152,6 +156,9 @@
 	    (hash_type == CRYPTO_AUTH_ALG_SHA256) ? SHA256_SIZE : SHA1_SIZE;
 	image_find_digest(image_ptr, image_size, hash_type,
 			(unsigned char *)&digest);
+#ifdef TZ_SAVE_KERNEL_HASH
+	save_kernel_hash((unsigned char *) &digest, hash_type);
+#endif
 
 	/*
 	 * Decrypt the pre-calculated expected image hash.
diff --git a/platform/msm_shared/include/image_verify.h b/platform/msm_shared/include/image_verify.h
index fe20c9f..240803f 100644
--- a/platform/msm_shared/include/image_verify.h
+++ b/platform/msm_shared/include/image_verify.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011,2014 The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011,2014-2015 The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -48,4 +48,5 @@
 void image_find_digest(unsigned char *image_ptr, unsigned int image_size,
 		unsigned hash_type, unsigned char *digest);
 void save_kernel_hash_cmd(void *digest);
+void save_kernel_hash(unsigned char *digest, unsigned hash_type);
 #endif