Abstracting mcuboot crypto functions for cleaner porting and less of an ifdef hell.

- The enc_context needs to initialize.

boot_enc_load seems to always be used to start the process, so calling
init inside makes sense.

- Handle boot_encrypt getting called with size of 0.

- No need to free contexts because Zephyr sets MBEDTLS_PLATFORM_NO_STD_FUNCTIONS.

I don't quite like this because it's implicit and will leak memory on
other ports.

Signed-off-by: Blaž Hrastnik <blaz@mxxn.io>
diff --git a/boot/bootutil/src/image_validate.c b/boot/bootutil/src/image_validate.c
index b5d259a..8865bb4 100644
--- a/boot/bootutil/src/image_validate.c
+++ b/boot/bootutil/src/image_validate.c
@@ -33,7 +33,7 @@
 #include <flash_map_backend/flash_map_backend.h>
 
 #include "bootutil/image.h"
-#include "bootutil/sha256.h"
+#include "bootutil/crypto/sha256.h"
 #include "bootutil/sign_key.h"
 #include "bootutil/security_cnt.h"
 
@@ -134,6 +134,7 @@
 #endif
         rc = flash_area_read(fap, off, tmp_buf, blk_sz);
         if (rc) {
+            bootutil_sha256_drop(&sha256_ctx);
             return rc;
         }
 #ifdef MCUBOOT_ENC_IMAGES
@@ -150,6 +151,7 @@
     }
 #endif /* MCUBOOT_RAM_LOAD */
     bootutil_sha256_finish(&sha256_ctx, hash_result);
+    bootutil_sha256_drop(&sha256_ctx);
 
     return 0;
 }
@@ -213,9 +215,11 @@
         bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
         bootutil_sha256_finish(&sha256_ctx, hash);
         if (!memcmp(hash, keyhash, keyhash_len)) {
+            bootutil_sha256_drop(&sha256_ctx);
             return i;
         }
     }
+    bootutil_sha256_drop(&sha256_ctx);
     return -1;
 }
 #else
@@ -232,6 +236,7 @@
     bootutil_sha256_init(&sha256_ctx);
     bootutil_sha256_update(&sha256_ctx, key, key_len);
     bootutil_sha256_finish(&sha256_ctx, hash);
+    bootutil_sha256_drop(&sha256_ctx);
 
     rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
     if (rc) {