linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] crypto: lib/sha256 - add sha256_value function
@ 2021-08-07  3:06 Chen Li
  2021-08-07  5:33 ` Eric Biggers
  0 siblings, 1 reply; 3+ messages in thread
From: Chen Li @ 2021-08-07  3:06 UTC (permalink / raw)
  To: herbert, davem, linux-crypto, linux-kernel

Add a function sha256_value() which accepts a string and store SHA256 hash
of this string into dest.

Signed-off-by: Chen Li <chenli@uniontech.com>
---
 include/crypto/sha2.h |  1 +
 lib/crypto/sha256.c   | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h
index 2838f529f31e..ce17954cab38 100644
--- a/include/crypto/sha2.h
+++ b/include/crypto/sha2.h
@@ -115,6 +115,7 @@ static inline void sha256_init(struct sha256_state *sctx)
 void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
 void sha256_final(struct sha256_state *sctx, u8 *out);
 void sha256(const u8 *data, unsigned int len, u8 *out);
+int  sha256_value(u8 **dest, const u8 *src);
 
 static inline void sha224_init(struct sha256_state *sctx)
 {
diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
index 72a4b0b1df28..ce1de7a3e32e 100644
--- a/lib/crypto/sha256.c
+++ b/lib/crypto/sha256.c
@@ -13,6 +13,8 @@
 
 #include <linux/bitops.h>
 #include <linux/export.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/string.h>
 #include <crypto/sha2.h>
@@ -206,4 +208,25 @@ void sha256(const u8 *data, unsigned int len, u8 *out)
 }
 EXPORT_SYMBOL(sha256);
 
+int sha256_value(u8 **dest, const u8 *src)
+{
+	u8 out[SHA256_DIGEST_SIZE];
+	int i, k;
+	unsigned char hex[2];
+
+	*dest = kvmalloc(sizeof(u8) * (SHA256_BLOCK_SIZE + 1), GFP_KERNEL);
+	if (ZERO_OR_NULL_PTR(*dest))
+		return -ENOMEM;
+	sha256(src, strlen(src), out);
+
+	for (i = 0, k = 0; i < SHA256_DIGEST_SIZE; i++) {
+		sprintf(hex, "%02x", out[i]);
+		(*dest)[k++] = hex[0];
+		(*dest)[k++] = hex[1];
+	}
+	(*dest)[k] = '\0';
+	return 0;
+}
+EXPORT_SYMBOL(sha256_value);
+
 MODULE_LICENSE("GPL");
-- 
2.32.0.93.g670b81a890

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

* Re: [PATCH] crypto: lib/sha256 - add sha256_value function
  2021-08-07  3:06 [PATCH] crypto: lib/sha256 - add sha256_value function Chen Li
@ 2021-08-07  5:33 ` Eric Biggers
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Biggers @ 2021-08-07  5:33 UTC (permalink / raw)
  To: Chen Li; +Cc: herbert, davem, linux-crypto, linux-kernel

On Sat, Aug 07, 2021 at 11:06:39AM +0800, Chen Li wrote:
> Add a function sha256_value() which accepts a string and store SHA256 hash
> of this string into dest.
> 
> Signed-off-by: Chen Li <chenli@uniontech.com>
> ---
>  include/crypto/sha2.h |  1 +
>  lib/crypto/sha256.c   | 23 +++++++++++++++++++++++
>  2 files changed, 24 insertions(+)
> 
> diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h
> index 2838f529f31e..ce17954cab38 100644
> --- a/include/crypto/sha2.h
> +++ b/include/crypto/sha2.h
> @@ -115,6 +115,7 @@ static inline void sha256_init(struct sha256_state *sctx)
>  void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
>  void sha256_final(struct sha256_state *sctx, u8 *out);
>  void sha256(const u8 *data, unsigned int len, u8 *out);
> +int  sha256_value(u8 **dest, const u8 *src);
>  
>  static inline void sha224_init(struct sha256_state *sctx)
>  {
> diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
> index 72a4b0b1df28..ce1de7a3e32e 100644
> --- a/lib/crypto/sha256.c
> +++ b/lib/crypto/sha256.c
> @@ -13,6 +13,8 @@
>  
>  #include <linux/bitops.h>
>  #include <linux/export.h>
> +#include <linux/mm.h>
> +#include <linux/slab.h>
>  #include <linux/module.h>
>  #include <linux/string.h>
>  #include <crypto/sha2.h>
> @@ -206,4 +208,25 @@ void sha256(const u8 *data, unsigned int len, u8 *out)
>  }
>  EXPORT_SYMBOL(sha256);
>  
> +int sha256_value(u8 **dest, const u8 *src)
> +{
> +	u8 out[SHA256_DIGEST_SIZE];
> +	int i, k;
> +	unsigned char hex[2];
> +
> +	*dest = kvmalloc(sizeof(u8) * (SHA256_BLOCK_SIZE + 1), GFP_KERNEL);
> +	if (ZERO_OR_NULL_PTR(*dest))
> +		return -ENOMEM;
> +	sha256(src, strlen(src), out);
> +
> +	for (i = 0, k = 0; i < SHA256_DIGEST_SIZE; i++) {
> +		sprintf(hex, "%02x", out[i]);
> +		(*dest)[k++] = hex[0];
> +		(*dest)[k++] = hex[1];
> +	}
> +	(*dest)[k] = '\0';
> +	return 0;
> +}
> +EXPORT_SYMBOL(sha256_value);

You forgot to include something that actually calls this function.

Anyway, there should be no need for this.  If you're trying to convert a SHA-256
hash value to a string, just use the %*phN printk format specifier which
converts bytes to hex.

- Eric

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

* [PATCH] crypto: lib/sha256 - add sha256_value function
@ 2021-08-07  3:04 Chen Li
  0 siblings, 0 replies; 3+ messages in thread
From: Chen Li @ 2021-08-07  3:04 UTC (permalink / raw)
  To: herbert, davem, linux-crypto, linux-kernel

Add a function sha256_value() which accepts a string and store SHA256 hash
of this string into dest.

Signed-off-by: Chen Li <chenli@uniontech.com>
---
 include/crypto/sha2.h |  1 +
 lib/crypto/sha256.c   | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h
index 2838f529f31e..ce17954cab38 100644
--- a/include/crypto/sha2.h
+++ b/include/crypto/sha2.h
@@ -115,6 +115,7 @@ static inline void sha256_init(struct sha256_state *sctx)
 void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len);
 void sha256_final(struct sha256_state *sctx, u8 *out);
 void sha256(const u8 *data, unsigned int len, u8 *out);
+int  sha256_value(u8 **dest, const u8 *src);
 
 static inline void sha224_init(struct sha256_state *sctx)
 {
diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
index 72a4b0b1df28..ce1de7a3e32e 100644
--- a/lib/crypto/sha256.c
+++ b/lib/crypto/sha256.c
@@ -13,6 +13,8 @@
 
 #include <linux/bitops.h>
 #include <linux/export.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/string.h>
 #include <crypto/sha2.h>
@@ -206,4 +208,25 @@ void sha256(const u8 *data, unsigned int len, u8 *out)
 }
 EXPORT_SYMBOL(sha256);
 
+int sha256_value(u8 **dest, const u8 *src)
+{
+	u8 out[SHA256_DIGEST_SIZE];
+	int i, k;
+	unsigned char hex[2];
+
+	*dest = kvmalloc(sizeof(u8) * (SHA256_BLOCK_SIZE + 1), GFP_KERNEL);
+	if (ZERO_OR_NULL_PTR(*dest))
+		return -ENOMEM;
+	sha256(src, strlen(src), out);
+
+	for (i = 0, k = 0; i < SHA256_DIGEST_SIZE; i++) {
+		sprintf(hex, "%02x", out[i]);
+		(*dest)[k++] = hex[0];
+		(*dest)[k++] = hex[1];
+	}
+	(*dest)[k] = '\0';
+	return 0;
+}
+EXPORT_SYMBOL(sha256_value);
+
 MODULE_LICENSE("GPL");
-- 
2.32.0.93.g670b81a890

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

end of thread, other threads:[~2021-08-07  5:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-07  3:06 [PATCH] crypto: lib/sha256 - add sha256_value function Chen Li
2021-08-07  5:33 ` Eric Biggers
  -- strict thread matches above, loose matches on Subject: below --
2021-08-07  3:04 Chen Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).