summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorJean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>2015-03-17 12:53:13 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2015-03-19 07:46:22 +0100
commit5f5d5331bb5e029264b9bc0cfbb3c32ec3158638 (patch)
tree000e810fd69a7a6de90fc7ba88c8688068092bb4 /crypto
parent18d778a06efe5bfc1984b34c6bc6b79e6ed6ecd1 (diff)
downloadbarebox-5f5d5331bb5e029264b9bc0cfbb3c32ec3158638.tar.gz
barebox-5f5d5331bb5e029264b9bc0cfbb3c32ec3158638.tar.xz
crypto: add pbkdf2 hmac key generator
this will allow to generate a KEY + IV based on a password and salt for AES encryption/decryption as example or simply the key for hmac or rsa from text password 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/Kconfig5
-rw-r--r--crypto/Makefile2
-rw-r--r--crypto/pbkdf2.c94
3 files changed, 101 insertions, 0 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig
index e72b91ef79..b721e3049e 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -34,3 +34,8 @@ config DIGEST_HMAC
bool "HMAC"
endif
+
+config CRYPTO_PBKDF2
+ select DIGEST
+ select SHA1
+ bool
diff --git a/crypto/Makefile b/crypto/Makefile
index ff5c289ce1..0bb67d5e23 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -9,3 +9,5 @@ obj-$(CONFIG_SHA224) += sha2.o
obj-$(CONFIG_SHA256) += sha2.o
obj-$(CONFIG_SHA384) += sha4.o
obj-$(CONFIG_SHA512) += sha4.o
+
+obj-$(CONFIG_CRYPTO_PBKDF2) += pbkdf2.o
diff --git a/crypto/pbkdf2.c b/crypto/pbkdf2.c
new file mode 100644
index 0000000000..c4ba7be5ef
--- /dev/null
+++ b/crypto/pbkdf2.c
@@ -0,0 +1,94 @@
+/*
+ * (C) Copyright 2015 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
+ *
+ * Under GPLv2 Only
+ */
+
+#include <common.h>
+#include <malloc.h>
+#include <errno.h>
+#include <crypto/pbkdf2.h>
+
+int pkcs5_pbkdf2_hmac(struct digest* d,
+ const unsigned char *pwd, size_t pwd_len,
+ const unsigned char *salt, size_t salt_len,
+ uint32_t iteration,
+ uint32_t key_len, unsigned char *key)
+{
+ int i, j, k;
+ unsigned char cnt[4];
+ uint32_t pass_len;
+ unsigned char *tmpdgt;
+ uint32_t d_len;
+ int ret;
+
+ if (!d)
+ return -EINVAL;
+
+ d_len = digest_length(d);
+ tmpdgt = malloc(d_len);
+ if (!tmpdgt)
+ return -ENOMEM;
+
+ i = 1;
+
+ ret = digest_set_key(d, pwd, pwd_len);
+ if (ret)
+ goto err;
+
+ while (key_len) {
+ pass_len = min(key_len, d_len);
+ cnt[0] = (i >> 24) & 0xff;
+ cnt[1] = (i >> 16) & 0xff;
+ cnt[2] = (i >> 8) & 0xff;
+ cnt[3] = i & 0xff;
+ ret = digest_init(d);
+ if (ret)
+ goto err;
+ ret = digest_update(d, salt, salt_len);
+ if (ret)
+ goto err;
+ ret = digest_update(d, cnt, 4);
+ if (ret)
+ goto err;
+ ret = digest_final(d, tmpdgt);
+ if (ret)
+ goto err;
+
+ memcpy(key, tmpdgt, pass_len);
+
+ for (j = 1; j < iteration; j++) {
+ ret = digest_digest(d, tmpdgt, d_len, tmpdgt);
+ if (ret)
+ goto err;
+
+ for(k = 0; k < pass_len; k++)
+ key[k] ^= tmpdgt[k];
+ }
+
+ key_len -= pass_len;
+ key += pass_len;
+ i++;
+ }
+
+ ret = 0;
+err:
+ free(tmpdgt);
+
+ return ret;;
+}
+
+int pkcs5_pbkdf2_hmac_sha1(const unsigned char *pwd, size_t pwd_len,
+ const unsigned char *salt, size_t salt_len,
+ uint32_t iter,
+ uint32_t key_len, unsigned char *key)
+{
+ int ret;
+ struct digest* d = digest_alloc("hmac(sha1)");
+
+ ret = pkcs5_pbkdf2_hmac(d, pwd, pwd_len, salt, salt_len, iter,
+ key_len, key);
+
+ digest_free(d);
+ return ret;
+}