hash: Add function to find hash_algo struct with progressive hash
authorRuchika Gupta <ruchika.gupta@freescale.com>
Fri, 23 Jan 2015 10:31:57 +0000 (16:01 +0530)
committerSimon Glass <sjg@chromium.org>
Fri, 30 Jan 2015 00:09:59 +0000 (17:09 -0700)
The hash_algo structure has some implementations in which progressive hash
API's are not defined. These are basically the hardware based implementations
of SHA. An API is added to find the algo which has progressive hash API's
defined. This can then be integrated with RSA checksum library which uses
Progressive Hash API's.

Signed-off-by: Ruchika Gupta <ruchika.gupta@freescale.com>
CC: Simon Glass <sjg@chromium.org>
Acked-by: Simon Glass <sjg@chromium.org>
common/hash.c
include/hash.h

index aceabc5caddb12d1c52d9b4c4028981ff01670b0..c4d8c3a041e979d1139eb597fcbd8edd7f6c7674 100644 (file)
@@ -20,7 +20,7 @@
 #include <asm/io.h>
 #include <asm/errno.h>
 
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1
 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
 {
        sha1_context *ctx = malloc(sizeof(sha1_context));
@@ -125,12 +125,7 @@ static struct hash_algo hash_algo[] = {
                CHUNKSZ_SHA256,
        },
 #endif
-       /*
-        * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since otherwise
-        * it bloats the code for boards which use SHA1 but not the 'hash'
-        * or 'sha1sum' commands.
-        */
-#ifdef CONFIG_CMD_SHA1SUM
+#ifdef CONFIG_SHA1
        {
                "sha1",
                SHA1_SUM_LEN,
@@ -140,7 +135,6 @@ static struct hash_algo hash_algo[] = {
                hash_update_sha1,
                hash_finish_sha1,
        },
-#define MULTI_HASH
 #endif
 #ifdef CONFIG_SHA256
        {
@@ -152,7 +146,6 @@ static struct hash_algo hash_algo[] = {
                hash_update_sha256,
                hash_finish_sha256,
        },
-#define MULTI_HASH
 #endif
        {
                "crc32",
@@ -165,6 +158,10 @@ static struct hash_algo hash_algo[] = {
        },
 };
 
+#if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM)
+#define MULTI_HASH
+#endif
+
 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
 #define MULTI_HASH
 #endif
@@ -311,6 +308,24 @@ int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
        return -EPROTONOSUPPORT;
 }
 
+int hash_progressive_lookup_algo(const char *algo_name,
+                                struct hash_algo **algop)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
+               if (!strcmp(algo_name, hash_algo[i].name)) {
+                       if (hash_algo[i].hash_init) {
+                               *algop = &hash_algo[i];
+                               return 0;
+                       }
+               }
+       }
+
+       debug("Unknown hash algorithm '%s'\n", algo_name);
+       return -EPROTONOSUPPORT;
+}
+
 void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
 {
        int i;
index d8ec4f08e166702d6a33b145f82eee3c78f6a6a3..c0a7ebc6a6cbe583b4eafe4adb74c7b44387c8ea 100644 (file)
@@ -127,6 +127,20 @@ int hash_block(const char *algo_name, const void *data, unsigned int len,
  */
 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
 
+/**
+ * hash_progressive_lookup_algo() - Look up hash_algo for prog. hash support
+ *
+ * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
+ * algorithm is not available with progressive hash support.
+ *
+ * @algo_name: Hash algorithm to look up
+ * @algop: Pointer to the hash_algo struct if found
+ *
+ * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
+ */
+int hash_progressive_lookup_algo(const char *algo_name,
+                                struct hash_algo **algop);
+
 /**
  * hash_show() - Print out a hash algorithm and value
  *