diff options
author | Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | 2015-03-25 12:56:14 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2015-03-27 07:49:45 +0100 |
commit | ab5b2c35e143a87483700ca92d7fd50292a891d0 (patch) | |
tree | f2aaba38fb2ff847ab1e19d0d611fb3aa0be0a01 /crypto | |
parent | e10cc0b3330d4dd72fef076a7f1b2b67cb271a12 (diff) | |
download | barebox-ab5b2c35e143a87483700ca92d7fd50292a891d0.tar.gz barebox-ab5b2c35e143a87483700ca92d7fd50292a891d0.tar.xz |
crypto: prepare to allow multiple digest driver
This will allow to have hw driver or asm optimised driver.
Use a priority level to determine which one to use at runtime.
The generic one will be 0.
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/Kconfig | 40 | ||||
-rw-r--r-- | crypto/Makefile | 14 | ||||
-rw-r--r-- | crypto/digest.c | 31 | ||||
-rw-r--r-- | crypto/hmac.c | 16 | ||||
-rw-r--r-- | crypto/internal.h | 19 | ||||
-rw-r--r-- | crypto/md5.c | 9 | ||||
-rw-r--r-- | crypto/sha1.c | 9 | ||||
-rw-r--r-- | crypto/sha2.c | 17 | ||||
-rw-r--r-- | crypto/sha4.c | 17 |
9 files changed, 110 insertions, 62 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig index b721e3049e..5a69236e5f 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -13,25 +13,53 @@ menuconfig DIGEST if DIGEST config MD5 - bool "MD5" + bool config SHA1 - bool "SHA1" + bool config SHA224 - bool "SHA224" + bool config SHA256 - bool "SHA256" + bool config SHA384 - bool "SHA384" + bool config SHA512 - bool "SHA512" + bool config DIGEST_HMAC + bool + +config DIGEST_MD5_GENERIC + bool "MD5" + select MD5 + +config DIGEST_SHA1_GENERIC + bool "SHA1" + select SHA1 + +config DIGEST_SHA224_GENERIC + bool "SHA224" + select SHA224 + +config DIGEST_SHA256_GENERIC + bool "SHA256" + select SHA256 + +config DIGEST_SHA384_GENERIC + bool "SHA384" + select SHA384 + +config DIGEST_SHA512_GENERIC + bool "SHA512" + select SHA512 + +config DIGEST_HMAC_GENERIC bool "HMAC" + select DIGEST_HMAC endif diff --git a/crypto/Makefile b/crypto/Makefile index 0bb67d5e23..f39de718e5 100644 --- a/crypto/Makefile +++ b/crypto/Makefile @@ -2,12 +2,12 @@ obj-$(CONFIG_CRC32) += crc32.o obj-$(CONFIG_CRC16) += crc16.o obj-$(CONFIG_CRC7) += crc7.o obj-$(CONFIG_DIGEST) += digest.o -obj-$(CONFIG_DIGEST_HMAC) += hmac.o -obj-$(CONFIG_MD5) += md5.o -obj-$(CONFIG_SHA1) += sha1.o -obj-$(CONFIG_SHA224) += sha2.o -obj-$(CONFIG_SHA256) += sha2.o -obj-$(CONFIG_SHA384) += sha4.o -obj-$(CONFIG_SHA512) += sha4.o +obj-$(CONFIG_DIGEST_HMAC_GENERIC) += hmac.o +obj-$(CONFIG_DIGEST_MD5_GENERIC) += md5.o +obj-$(CONFIG_DIGEST_SHA1_GENERIC) += sha1.o +obj-$(CONFIG_DIGEST_SHA224_GENERIC) += sha2.o +obj-$(CONFIG_DIGEST_SHA256_GENERIC) += sha2.o +obj-$(CONFIG_DIGEST_SHA384_GENERIC) += sha4.o +obj-$(CONFIG_DIGEST_SHA512_GENERIC) += sha4.o obj-$(CONFIG_CRYPTO_PBKDF2) += pbkdf2.o diff --git a/crypto/digest.c b/crypto/digest.c index f902dc1fb1..b3f514c8ad 100644 --- a/crypto/digest.c +++ b/crypto/digest.c @@ -25,8 +25,7 @@ #include <errno.h> #include <module.h> #include <linux/err.h> - -#include "internal.h" +#include <crypto/internal.h> static LIST_HEAD(digests); @@ -78,7 +77,7 @@ int digest_generic_digest(struct digest *d, const void *data, int digest_algo_register(struct digest_algo *d) { - if (!d || !d->name || !d->update || !d->final || !d->verify) + if (!d || !d->base.name || !d->update || !d->final || !d->verify) return -EINVAL; if (!d->init) @@ -90,9 +89,6 @@ int digest_algo_register(struct digest_algo *d) if (!d->free) d->free = dummy_free; - if (digest_algo_get_by_name(d->name)) - return -EEXIST; - list_add_tail(&d->list, &digests); return 0; @@ -110,25 +106,36 @@ EXPORT_SYMBOL(digest_algo_unregister); static struct digest_algo *digest_algo_get_by_name(const char *name) { - struct digest_algo *d; + struct digest_algo *d = NULL; + struct digest_algo *tmp; + int priority = -1; if (!name) return NULL; - list_for_each_entry(d, &digests, list) { - if(strcmp(d->name, name) == 0) - return d; + list_for_each_entry(tmp, &digests, list) { + if (strcmp(tmp->base.name, name) != 0) + continue; + + if (tmp->base.priority <= priority) + continue; + + d = tmp; + priority = tmp->base.priority; } - return NULL; + return d; } void digest_algo_prints(const char *prefix) { struct digest_algo* d; + printf("%s%-15s\t%-20s\t%-15s\n", prefix, "name", "driver", "priority"); + printf("%s--------------------------------------------------\n", prefix); list_for_each_entry(d, &digests, list) { - printf("%s%s\n", prefix, d->name); + printf("%s%-15s\t%-20s\t%d\n", prefix, d->base.name, + d->base.driver_name, d->base.priority); } } diff --git a/crypto/hmac.c b/crypto/hmac.c index 4c6a703e5e..77814a1643 100644 --- a/crypto/hmac.c +++ b/crypto/hmac.c @@ -7,8 +7,7 @@ #include <common.h> #include <digest.h> #include <malloc.h> - -#include "internal.h" +#include <crypto/internal.h> struct digest_hmac { char *name; @@ -145,7 +144,10 @@ err: } struct digest_algo hmac_algo = { - .flags = DIGEST_ALGO_NEED_KEY, + .base = { + .priority = 0, + .flags = DIGEST_ALGO_NEED_KEY, + }, .alloc = digest_hmac_alloc, .init = digest_hmac_init, .update = digest_hmac_update, @@ -160,16 +162,20 @@ struct digest_algo hmac_algo = { int digest_hmac_register(struct digest_algo *algo, unsigned int pad_length) { struct digest_hmac *dh; + char *name; if (!algo || !pad_length) return -EINVAL; + name = algo->base.name; dh = xzalloc(sizeof(*dh)); - dh->name = xstrdup(algo->name); + dh->name = xstrdup(name); dh->pad_length = pad_length; dh->algo = hmac_algo; dh->algo.length = algo->length; - dh->algo.name = asprintf("hmac(%s)", algo->name); + dh->algo.base.name = asprintf("hmac(%s)", name); + dh->algo.base.driver_name = asprintf("hmac(%s)-generic", name); + dh->algo.base.priority = algo->base.priority; return digest_algo_register(&dh->algo); } diff --git a/crypto/internal.h b/crypto/internal.h deleted file mode 100644 index c6f5908ea0..0000000000 --- a/crypto/internal.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * (C) Copyright 2015 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> - * - * GPL v2 only - */ - -#ifdef CONFIG_DIGEST_HMAC -int digest_hmac_register(struct digest_algo *algo, unsigned int pad_length); -#else -static inline int digest_hmac_register(struct digest_algo *algo, - unsigned int pad_length) -{ - return 0; -} -#endif - -int digest_generic_verify(struct digest *d, const unsigned char *md); -int digest_generic_digest(struct digest *d, const void *data, - unsigned int len, u8 *out); diff --git a/crypto/md5.c b/crypto/md5.c index b7ad6f27e9..74c9b706f0 100644 --- a/crypto/md5.c +++ b/crypto/md5.c @@ -28,8 +28,7 @@ #include <common.h> #include <digest.h> #include <init.h> - -#include "internal.h" +#include <crypto/internal.h> struct MD5Context { __u32 buf[4]; @@ -290,7 +289,11 @@ static int digest_md5_final(struct digest *d, unsigned char *md) } static struct digest_algo md5 = { - .name = "md5", + .base = { + .name = "md5", + .driver_name = "md5-generic", + .priority = 0, + }, .init = digest_md5_init, .update = digest_md5_update, .final = digest_md5_final, diff --git a/crypto/sha1.c b/crypto/sha1.c index b108f8ae31..a2ca191899 100644 --- a/crypto/sha1.c +++ b/crypto/sha1.c @@ -26,7 +26,7 @@ #include <linux/string.h> #include <asm/byteorder.h> -#include "internal.h" +#include <crypto/internal.h> #define SHA1_SUM_POS -0x20 #define SHA1_SUM_LEN 20 @@ -311,7 +311,12 @@ static int digest_sha1_final(struct digest *d, unsigned char *md) } static struct digest_algo m = { - .name = "sha1", + .base = { + .name = "sha1", + .driver_name = "sha1-generic", + .priority = 0, + }, + .init = digest_sha1_init, .update = digest_sha1_update, .final = digest_sha1_final, diff --git a/crypto/sha2.c b/crypto/sha2.c index 375a40e1f6..42c40da757 100644 --- a/crypto/sha2.c +++ b/crypto/sha2.c @@ -20,8 +20,7 @@ #include <init.h> #include <linux/string.h> #include <asm/byteorder.h> - -#include "internal.h" +#include <crypto/internal.h> #define SHA224_SUM_LEN 28 #define SHA256_SUM_LEN 32 @@ -300,7 +299,12 @@ static int digest_sha224_init(struct digest *d) } static struct digest_algo m224 = { - .name = "sha224", + .base = { + .name = "sha224", + .driver_name = "sha224-generic", + .priority = 0, + }, + .init = digest_sha224_init, .update = digest_sha2_update, .final = digest_sha2_final, @@ -332,7 +336,12 @@ static int digest_sha256_init(struct digest *d) } static struct digest_algo m256 = { - .name = "sha256", + .base = { + .name = "sha256", + .driver_name = "sha256-generic", + .priority = 0, + }, + .init = digest_sha256_init, .update = digest_sha2_update, .final = digest_sha2_final, diff --git a/crypto/sha4.c b/crypto/sha4.c index 1b91e7fcc3..cb62d1d2f2 100644 --- a/crypto/sha4.c +++ b/crypto/sha4.c @@ -28,8 +28,7 @@ #include <init.h> #include <linux/string.h> #include <asm/byteorder.h> - -#include "internal.h" +#include <crypto/internal.h> #define SHA384_SUM_LEN 48 #define SHA512_SUM_LEN 64 @@ -305,7 +304,12 @@ static int digest_sha384_init(struct digest *d) } static struct digest_algo m384 = { - .name = "sha384", + .base = { + .name = "sha384", + .driver_name = "sha384-generic", + .priority = 0, + }, + .init = digest_sha384_init, .update = digest_sha4_update, .final = digest_sha4_final, @@ -338,7 +342,12 @@ static int digest_sha512_init(struct digest *d) } static struct digest_algo m512 = { - .name = "sha512", + .base = { + .name = "sha512", + .driver_name = "sha512-generic", + .priority = 0, + }, + .init = digest_sha512_init, .update = digest_sha4_update, .final = digest_sha4_final, |