crypto: add digest_alloc_by_algo()
In barebox the function digest_alloc() allocates a digest based on a string. When a subsystem already uses an integer value to identify a digest it makes no sense to create a string and pass it to digest_alloc(), where it is parsed again. This patch adds the possibility to get a digest by an enum. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
bc94fb379a
commit
fedba5aaef
|
@ -119,6 +119,7 @@ static struct digest_algo m = {
|
|||
.name = "sha1",
|
||||
.driver_name = "sha1-asm",
|
||||
.priority = 150,
|
||||
.algo = HASH_ALGO_SHA1,
|
||||
},
|
||||
|
||||
.init = sha1_init,
|
||||
|
|
|
@ -173,6 +173,7 @@ static struct digest_algo sha224 = {
|
|||
.name = "sha224",
|
||||
.driver_name = "sha224-asm",
|
||||
.priority = 150,
|
||||
.algo = HASH_ALGO_SHA224,
|
||||
},
|
||||
|
||||
.length = SHA224_DIGEST_SIZE,
|
||||
|
@ -195,6 +196,7 @@ static struct digest_algo sha256 = {
|
|||
.name = "sha256",
|
||||
.driver_name = "sha256-asm",
|
||||
.priority = 150,
|
||||
.algo = HASH_ALGO_SHA256,
|
||||
},
|
||||
|
||||
.length = SHA256_DIGEST_SIZE,
|
||||
|
|
|
@ -127,6 +127,26 @@ static struct digest_algo *digest_algo_get_by_name(const char *name)
|
|||
return d;
|
||||
}
|
||||
|
||||
static struct digest_algo *digest_algo_get_by_algo(enum hash_algo algo)
|
||||
{
|
||||
struct digest_algo *d = NULL;
|
||||
struct digest_algo *tmp;
|
||||
int priority = -1;
|
||||
|
||||
list_for_each_entry(tmp, &digests, list) {
|
||||
if (tmp->base.algo != algo)
|
||||
continue;
|
||||
|
||||
if (tmp->base.priority <= priority)
|
||||
continue;
|
||||
|
||||
d = tmp;
|
||||
priority = tmp->base.priority;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
void digest_algo_prints(const char *prefix)
|
||||
{
|
||||
struct digest_algo* d;
|
||||
|
@ -160,6 +180,27 @@ struct digest *digest_alloc(const char *name)
|
|||
}
|
||||
EXPORT_SYMBOL_GPL(digest_alloc);
|
||||
|
||||
struct digest *digest_alloc_by_algo(enum hash_algo hash_algo)
|
||||
{
|
||||
struct digest *d;
|
||||
struct digest_algo *algo;
|
||||
|
||||
algo = digest_algo_get_by_algo(hash_algo);
|
||||
if (!algo)
|
||||
return NULL;
|
||||
|
||||
d = xzalloc(sizeof(*d));
|
||||
d->algo = algo;
|
||||
d->ctx = xzalloc(algo->ctx_length);
|
||||
if (d->algo->alloc(d)) {
|
||||
digest_free(d);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(digest_alloc_by_algo);
|
||||
|
||||
void digest_free(struct digest *d)
|
||||
{
|
||||
if (!d)
|
||||
|
|
|
@ -293,6 +293,7 @@ static struct digest_algo md5 = {
|
|||
.name = "md5",
|
||||
.driver_name = "md5-generic",
|
||||
.priority = 0,
|
||||
.algo = HASH_ALGO_MD5,
|
||||
},
|
||||
.init = digest_md5_init,
|
||||
.update = digest_md5_update,
|
||||
|
|
|
@ -287,6 +287,7 @@ static struct digest_algo m = {
|
|||
.name = "sha1",
|
||||
.driver_name = "sha1-generic",
|
||||
.priority = 0,
|
||||
.algo = HASH_ALGO_SHA1,
|
||||
},
|
||||
|
||||
.init = sha1_init,
|
||||
|
|
|
@ -327,6 +327,7 @@ static struct digest_algo m224 = {
|
|||
.name = "sha224",
|
||||
.driver_name = "sha224-generic",
|
||||
.priority = 0,
|
||||
.algo = HASH_ALGO_SHA224,
|
||||
},
|
||||
|
||||
.init = sha224_init,
|
||||
|
@ -352,6 +353,7 @@ static struct digest_algo m256 = {
|
|||
.name = "sha256",
|
||||
.driver_name = "sha256-generic",
|
||||
.priority = 0,
|
||||
.algo = HASH_ALGO_SHA256,
|
||||
},
|
||||
|
||||
.init = sha256_init,
|
||||
|
|
|
@ -246,6 +246,7 @@ static struct digest_algo m384 = {
|
|||
.name = "sha384",
|
||||
.driver_name = "sha384-generic",
|
||||
.priority = 0,
|
||||
.algo = HASH_ALGO_SHA384,
|
||||
},
|
||||
|
||||
.init = sha384_init,
|
||||
|
@ -272,6 +273,7 @@ static struct digest_algo m512 = {
|
|||
.name = "sha512",
|
||||
.driver_name = "sha512-generic",
|
||||
.priority = 0,
|
||||
.algo = HASH_ALGO_SHA512,
|
||||
},
|
||||
|
||||
.init = sha512_init,
|
||||
|
|
|
@ -23,12 +23,34 @@
|
|||
|
||||
struct digest;
|
||||
|
||||
enum hash_algo {
|
||||
HASH_ALGO_MD4,
|
||||
HASH_ALGO_MD5,
|
||||
HASH_ALGO_SHA1,
|
||||
HASH_ALGO_RIPE_MD_160,
|
||||
HASH_ALGO_SHA224,
|
||||
HASH_ALGO_SHA256,
|
||||
HASH_ALGO_SHA384,
|
||||
HASH_ALGO_SHA512,
|
||||
HASH_ALGO_RIPE_MD_128,
|
||||
HASH_ALGO_RIPE_MD_256,
|
||||
HASH_ALGO_RIPE_MD_320,
|
||||
HASH_ALGO_WP_256,
|
||||
HASH_ALGO_WP_384,
|
||||
HASH_ALGO_WP_512,
|
||||
HASH_ALGO_TGR_128,
|
||||
HASH_ALGO_TGR_160,
|
||||
HASH_ALGO_TGR_192,
|
||||
HASH_ALGO__LAST
|
||||
};
|
||||
|
||||
struct crypto_alg {
|
||||
char *name;
|
||||
char *driver_name;
|
||||
int priority;
|
||||
#define DIGEST_ALGO_NEED_KEY (1 << 0)
|
||||
unsigned int flags;
|
||||
enum hash_algo algo;
|
||||
};
|
||||
|
||||
struct digest_algo {
|
||||
|
@ -65,6 +87,7 @@ void digest_algo_unregister(struct digest_algo *d);
|
|||
void digest_algo_prints(const char *prefix);
|
||||
|
||||
struct digest *digest_alloc(const char *name);
|
||||
struct digest *digest_alloc_by_algo(enum hash_algo);
|
||||
void digest_free(struct digest *d);
|
||||
|
||||
int digest_file_window(struct digest *d, const char *filename,
|
||||
|
|
Loading…
Reference in New Issue