mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] crypto: digest: speficied when a digest need a key to be used
@ 2015-03-18  9:37 Jean-Christophe PLAGNIOL-VILLARD
  2015-03-18  9:37 ` [PATCH 2/2] password: add pbkdf2 support Jean-Christophe PLAGNIOL-VILLARD
  2015-03-19  6:53 ` [PATCH 1/2] crypto: digest: speficied when a digest need a key to be used Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2015-03-18  9:37 UTC (permalink / raw)
  To: barebox

such as for hmac(xxx) you must provide a key

This will allow to enforce the correct parameter at digest command

<digest>sum is not impacted

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/digest.c |  3 +++
 crypto/hmac.c     |  1 +
 include/digest.h  | 12 ++++++++++++
 3 files changed, 16 insertions(+)

diff --git a/commands/digest.c b/commands/digest.c
index fa47f24..876c37a 100644
--- a/commands/digest.c
+++ b/commands/digest.c
@@ -34,6 +34,9 @@ int __do_digest(struct digest *d, unsigned char *key, int keylen,
 			perror("set_key");
 			goto err;
 		}
+	} else if (digest_is_flags(d, DIGEST_ALGO_NEED_KEY)) {
+		eprintf("%s need a key to be used\n", digest_name(d));
+		goto err;
 	}
 
 	hash = calloc(digest_length(d), sizeof(unsigned char));
diff --git a/crypto/hmac.c b/crypto/hmac.c
index c2195d9..4c6a703 100644
--- a/crypto/hmac.c
+++ b/crypto/hmac.c
@@ -145,6 +145,7 @@ err:
 }
 
 struct digest_algo hmac_algo = {
+	.flags = DIGEST_ALGO_NEED_KEY,
 	.alloc = digest_hmac_alloc,
 	.init = digest_hmac_init,
 	.update = digest_hmac_update,
diff --git a/include/digest.h b/include/digest.h
index 300ea4e..c574b4d 100644
--- a/include/digest.h
+++ b/include/digest.h
@@ -25,6 +25,8 @@ struct digest;
 
 struct digest_algo {
 	char *name;
+#define DIGEST_ALGO_NEED_KEY	(1 << 0)
+	unsigned int flags;
 
 	int (*alloc)(struct digest *d);
 	void (*free)(struct digest *d);
@@ -109,4 +111,14 @@ static inline int digest_set_key(struct digest *d, const unsigned char *key,
 	return d->algo->set_key(d, key, len);
 }
 
+static inline int digest_is_flags(struct digest *d, unsigned int flags)
+{
+	return d->algo->flags & flags;
+}
+
+static inline const char *digest_name(struct digest *d)
+{
+	return d->algo->name;
+}
+
 #endif /* __SH_ST_DEVICES_H__ */
-- 
2.1.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] password: add pbkdf2 support
  2015-03-18  9:37 [PATCH 1/2] crypto: digest: speficied when a digest need a key to be used Jean-Christophe PLAGNIOL-VILLARD
@ 2015-03-18  9:37 ` Jean-Christophe PLAGNIOL-VILLARD
  2015-03-19  6:53 ` [PATCH 1/2] crypto: digest: speficied when a digest need a key to be used Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2015-03-18  9:37 UTC (permalink / raw)
  To: barebox

We will use random 32 bytes salt and 10000 round to generate a
32 bytes key.

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 common/Kconfig    |  4 +++
 common/password.c | 96 ++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 70 insertions(+), 30 deletions(-)

diff --git a/common/Kconfig b/common/Kconfig
index 96ace6b..ad8a596 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -453,6 +453,10 @@ config PASSWD_SUM_SHA512
 	bool "SHA512"
 	select SHA512
 
+config PASSWD_CRYPTO_PBKDF2
+	bool "PBKDF2"
+	select CRYPTO_PBKDF2
+
 endchoice
 
 endif
diff --git a/common/password.c b/common/password.c
index 6ecf717..c845422 100644
--- a/common/password.c
+++ b/common/password.c
@@ -25,7 +25,9 @@
 #include <malloc.h>
 #include <xfuncs.h>
 #include <clock.h>
+#include <stdlib.h>
 #include <generated/passwd.h>
+#include <crypto/pbkdf2.h>
 
 #if defined(CONFIG_PASSWD_SUM_MD5)
 #define PASSWD_SUM "md5"
@@ -35,8 +37,14 @@
 #define PASSWD_SUM "sha256"
 #elif defined(CONFIG_PASSWD_SUM_SHA512)
 #define PASSWD_SUM "sha512"
+#else
+#define PASSWD_SUM	NULL
 #endif
 
+#define PBKDF2_SALT_LEN	32
+#define PBKDF2_LENGTH	64
+#define PBKDF2_COUNT	10000
+
 int password(unsigned char *passwd, size_t length, int flags, int timeout)
 {
 	unsigned char *buf = passwd;
@@ -277,45 +285,57 @@ EXPORT_SYMBOL(write_env_passwd);
 
 static int __check_passwd(unsigned char* passwd, size_t length, int std)
 {
-	struct digest *d;
+	struct digest *d = NULL;
 	unsigned char *passwd1_sum;
 	unsigned char *passwd2_sum;
 	int ret = 0;
+	int hash_len;
 
-	d = digest_alloc(PASSWD_SUM);
+	if (IS_ENABLED(CONFIG_PASSWD_CRYPTO_PBKDF2)) {
+		hash_len = PBKDF2_LENGTH;
+	} else {
+		d = digest_alloc(PASSWD_SUM);
 
-	passwd1_sum = calloc(digest_length(d), sizeof(unsigned char));
+		hash_len = digest_length(d);
+	}
 
+	passwd1_sum = calloc(hash_len * 2, sizeof(unsigned char));
 	if (!passwd1_sum)
 		return -ENOMEM;
 
-	passwd2_sum = calloc(digest_length(d), sizeof(unsigned char));
+	passwd2_sum = passwd1_sum + hash_len;
 
-	if (!passwd2_sum) {
-		ret = -ENOMEM;
-		goto err1;
-	}
+	if (std)
+		ret = read_env_passwd(passwd2_sum, hash_len);
+	else
+		ret = read_default_passwd(passwd2_sum, hash_len);
 
-	digest_init(d);
+	if (ret < 0)
+		goto err;
 
-	digest_update(d, passwd, length);
+	if (IS_ENABLED(CONFIG_PASSWD_CRYPTO_PBKDF2)) {
+		char *key = passwd2_sum + PBKDF2_SALT_LEN;
+		char *salt = passwd2_sum;
+		int keylen = PBKDF2_LENGTH - PBKDF2_SALT_LEN;
 
-	digest_final(d, passwd1_sum);
+		ret = pkcs5_pbkdf2_hmac_sha1(passwd, length, salt,
+			PBKDF2_SALT_LEN, PBKDF2_COUNT, keylen, passwd1_sum);
+		if (ret)
+			goto err;
 
-	if (std)
-		ret = read_env_passwd(passwd2_sum, digest_length(d));
-	else
-		ret = read_default_passwd(passwd2_sum, digest_length(d));
+		if (strncmp(passwd1_sum, key, keylen) == 0)
+			ret = 1;
+	} else {
+		ret = digest_digest(d, passwd, length, passwd1_sum);
 
-	if (ret < 0)
-		goto err2;
+		if (ret)
+			goto err;
 
-	if (strncmp(passwd1_sum, passwd2_sum, digest_length(d)) == 0)
-		ret = 1;
+		if (strncmp(passwd1_sum, passwd2_sum, hash_len) == 0)
+			ret = 1;
+	}
 
-err2:
-	free(passwd2_sum);
-err1:
+err:
 	free(passwd1_sum);
 	digest_free(d);
 
@@ -346,25 +366,41 @@ int check_passwd(unsigned char* passwd, size_t length)
 
 int set_env_passwd(unsigned char* passwd, size_t length)
 {
-	struct digest *d;
+	struct digest *d = NULL;
 	unsigned char *passwd_sum;
-	int ret;
+	int ret, hash_len;
 
-	d = digest_alloc(PASSWD_SUM);
+	if (IS_ENABLED(CONFIG_PASSWD_CRYPTO_PBKDF2)) {
+		hash_len = PBKDF2_LENGTH;
+	} else {
+		d = digest_alloc(PASSWD_SUM);
 
-	passwd_sum = calloc(digest_length(d), sizeof(unsigned char));
+		hash_len = digest_length(d);
+	}
 
+	passwd_sum = calloc(hash_len, sizeof(unsigned char));
 	if (!passwd_sum)
 		return -ENOMEM;
 
-	digest_init(d);
+	if (IS_ENABLED(CONFIG_PASSWD_CRYPTO_PBKDF2)) {
+		char *key = passwd_sum + PBKDF2_SALT_LEN;
+		char *salt = passwd_sum;
+		int keylen = PBKDF2_LENGTH - PBKDF2_SALT_LEN;
 
-	digest_update(d, passwd, length);
+		get_random_bytes(passwd_sum, PBKDF2_SALT_LEN);
 
-	digest_final(d, passwd_sum);
+		ret = pkcs5_pbkdf2_hmac_sha1(passwd, length, salt,
+				PBKDF2_SALT_LEN, PBKDF2_COUNT, keylen, key);
+	} else {
+		ret = digest_digest(d, passwd, length, passwd_sum);
+	}
+	if (ret)
+		goto err;
 
-	ret = write_env_passwd(passwd_sum, digest_length(d));
+	ret = write_env_passwd(passwd_sum, hash_len);
 
+err:
+	digest_free(d);
 	free(passwd_sum);
 
 	return ret;
-- 
2.1.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] crypto: digest: speficied when a digest need a key to be used
  2015-03-18  9:37 [PATCH 1/2] crypto: digest: speficied when a digest need a key to be used Jean-Christophe PLAGNIOL-VILLARD
  2015-03-18  9:37 ` [PATCH 2/2] password: add pbkdf2 support Jean-Christophe PLAGNIOL-VILLARD
@ 2015-03-19  6:53 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2015-03-19  6:53 UTC (permalink / raw)
  To: Jean-Christophe PLAGNIOL-VILLARD; +Cc: barebox

On Wed, Mar 18, 2015 at 10:37:53AM +0100, Jean-Christophe PLAGNIOL-VILLARD wrote:
> such as for hmac(xxx) you must provide a key
> 
> This will allow to enforce the correct parameter at digest command
> 
> <digest>sum is not impacted
> 
> Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
> ---
>  commands/digest.c |  3 +++
>  crypto/hmac.c     |  1 +
>  include/digest.h  | 12 ++++++++++++
>  3 files changed, 16 insertions(+)

Applied, thanks

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-03-19  6:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-18  9:37 [PATCH 1/2] crypto: digest: speficied when a digest need a key to be used Jean-Christophe PLAGNIOL-VILLARD
2015-03-18  9:37 ` [PATCH 2/2] password: add pbkdf2 support Jean-Christophe PLAGNIOL-VILLARD
2015-03-19  6:53 ` [PATCH 1/2] crypto: digest: speficied when a digest need a key to be used Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox