summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>2015-03-17 12:53:10 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2015-03-19 07:46:22 +0100
commitec4f9699718054f2b4e49ed441f3420c257f5f26 (patch)
tree43721d010eba21f157bb3b4c053ae31c7a12fc2a
parent92138a77544da7ad7d880082e905c1ca8cd0c527 (diff)
downloadbarebox-ec4f9699718054f2b4e49ed441f3420c257f5f26.tar.gz
barebox-ec4f9699718054f2b4e49ed441f3420c257f5f26.tar.xz
digest: add verify callback
this will allow to compare a md with the original one When calling this do not call final For RSA_SIGN verification final does not exist only verify as final will be for signing Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--crypto/digest.c24
-rw-r--r--crypto/hmac.c1
-rw-r--r--crypto/internal.h2
-rw-r--r--crypto/md5.c1
-rw-r--r--crypto/sha1.c1
-rw-r--r--crypto/sha2.c2
-rw-r--r--crypto/sha4.c2
-rw-r--r--include/digest.h6
8 files changed, 38 insertions, 1 deletions
diff --git a/crypto/digest.c b/crypto/digest.c
index 208a2041b1..7869c049e9 100644
--- a/crypto/digest.c
+++ b/crypto/digest.c
@@ -26,6 +26,8 @@
#include <module.h>
#include <linux/err.h>
+#include "internal.h"
+
static LIST_HEAD(digests);
static struct digest_algo *digest_algo_get_by_name(const char *name);
@@ -37,9 +39,29 @@ static int dummy_init(struct digest *d)
static void dummy_free(struct digest *d) {}
+int digest_generic_verify(struct digest *d, const unsigned char *md)
+{
+ int ret;
+ int len = digest_length(d);
+ unsigned char *tmp;
+
+ tmp = xmalloc(len);
+
+ ret = digest_final(d, tmp);
+ if (ret)
+ goto end;
+
+ ret = memcmp(md, tmp, len);
+ ret = ret ? -EINVAL : 0;
+end:
+ free(tmp);
+ return ret;
+}
+
int digest_algo_register(struct digest_algo *d)
{
- if (!d || !d->name || !d->update || !d->final || d->length < 1)
+ if (!d || !d->name || !d->update || !d->final || !d->verify ||
+ d->length < 1)
return -EINVAL;
if (!d->init)
diff --git a/crypto/hmac.c b/crypto/hmac.c
index 1462730a0d..f39e4c8e8c 100644
--- a/crypto/hmac.c
+++ b/crypto/hmac.c
@@ -136,6 +136,7 @@ struct digest_algo hmac_algo = {
.init = digest_hmac_init,
.update = digest_hmac_update,
.final = digest_hmac_final,
+ .verify = digest_generic_verify,
.set_key = digest_hmac_set_key,
.free = digest_hmac_free,
.ctx_length = sizeof(struct digest_hmac),
diff --git a/crypto/internal.h b/crypto/internal.h
index cc409d8d21..f482654f63 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -13,3 +13,5 @@ static inline int digest_hmac_register(struct digest_algo *algo,
return 0;
}
#endif
+
+int digest_generic_verify(struct digest *d, const unsigned char *md);
diff --git a/crypto/md5.c b/crypto/md5.c
index fe17ff5863..4847b38631 100644
--- a/crypto/md5.c
+++ b/crypto/md5.c
@@ -294,6 +294,7 @@ static struct digest_algo md5 = {
.init = digest_md5_init,
.update = digest_md5_update,
.final = digest_md5_final,
+ .verify = digest_generic_verify,
.length = 16,
.ctx_length = sizeof(struct MD5Context),
};
diff --git a/crypto/sha1.c b/crypto/sha1.c
index a244b5d627..09dee87321 100644
--- a/crypto/sha1.c
+++ b/crypto/sha1.c
@@ -315,6 +315,7 @@ static struct digest_algo m = {
.init = digest_sha1_init,
.update = digest_sha1_update,
.final = digest_sha1_final,
+ .verify = digest_generic_verify,
.length = SHA1_SUM_LEN,
.ctx_length = sizeof(sha1_context),
};
diff --git a/crypto/sha2.c b/crypto/sha2.c
index cb89c82312..9bf6541498 100644
--- a/crypto/sha2.c
+++ b/crypto/sha2.c
@@ -304,6 +304,7 @@ static struct digest_algo m224 = {
.init = digest_sha224_init,
.update = digest_sha2_update,
.final = digest_sha2_final,
+ .verify = digest_generic_verify,
.length = SHA224_SUM_LEN,
.ctx_length = sizeof(sha2_context),
};
@@ -335,6 +336,7 @@ static struct digest_algo m256 = {
.init = digest_sha256_init,
.update = digest_sha2_update,
.final = digest_sha2_final,
+ .verify = digest_generic_verify,
.length = SHA256_SUM_LEN,
.ctx_length = sizeof(sha2_context),
};
diff --git a/crypto/sha4.c b/crypto/sha4.c
index 1c768e720e..5c3097db4a 100644
--- a/crypto/sha4.c
+++ b/crypto/sha4.c
@@ -309,6 +309,7 @@ static struct digest_algo m384 = {
.init = digest_sha384_init,
.update = digest_sha4_update,
.final = digest_sha4_final,
+ .verify = digest_generic_verify,
.length = SHA384_SUM_LEN,
.ctx_length = sizeof(sha4_context),
};
@@ -341,6 +342,7 @@ static struct digest_algo m512 = {
.init = digest_sha512_init,
.update = digest_sha4_update,
.final = digest_sha4_final,
+ .verify = digest_generic_verify,
.length = SHA512_SUM_LEN,
.ctx_length = sizeof(sha4_context),
};
diff --git a/include/digest.h b/include/digest.h
index 1c742f615e..5d1d80c31d 100644
--- a/include/digest.h
+++ b/include/digest.h
@@ -32,6 +32,7 @@ struct digest_algo {
int (*update)(struct digest *d, const void *data, unsigned long len);
int (*final)(struct digest *d, unsigned char *md);
int (*set_key)(struct digest *d, const unsigned char *key, unsigned int len);
+ int (*verify)(struct digest *d, const unsigned char *md);
unsigned int length;
unsigned int ctx_length;
@@ -77,6 +78,11 @@ static inline int digest_final(struct digest *d, unsigned char *md)
return d->algo->final(d, md);
}
+static inline int digest_verify(struct digest *d, const unsigned char *md)
+{
+ return d->algo->verify(d, md);
+}
+
static inline int digest_length(struct digest *d)
{
return d->algo->length;