From 4e37b8ab3f459998755d90ffafb366ffc6fdeb7e Mon Sep 17 00:00:00 2001
From: Aashvij Shenai <a-shenai@ti.com>
Date: Fri, 30 Aug 2024 22:42:41 +0530
Subject: [PATCH 5/6] Enabling sha 256

Signed-off-by: Aashvij Shenai <a-shenai@ti.com>
---
 crypto/testmgr.c       | 1429 ++++++++++++++++++++++++++++++++++++++++
 drivers/crypto/sa2ul.c |   79 ++-
 2 files changed, 1485 insertions(+), 23 deletions(-)
 
diff --git a/drivers/crypto/sa2ul.c b/drivers/crypto/sa2ul.c
index 7916f81bfd41..b538375218fe 100644
--- a/drivers/crypto/sa2ul.c
+++ b/drivers/crypto/sa2ul.c
@@ -2145,7 +2145,7 @@ static int sa_sha_digest(struct ahash_request *req)
 	return sa_sha_run(req);
 }
 
-static int globule_count = 0;
+static int globule_count = 1;
 
 static int sa_sha_init_sha512(struct ahash_request *req)
 {
@@ -2160,12 +2160,18 @@ static int sa_sha_init_sha512(struct ahash_request *req)
 	dev_dbg(sa_k3_dev, "init: digest size: %u, rctx=%p\n",
 		crypto_ahash_digestsize(tfm), rctx);
 
-	/* if(globule_count++){ */
-        ad.aalg_id = SA_AALG_ID_SHA2_512;
-        ad.hash_size = SHA512_DIGEST_SIZE;
-        ad.auth_ctrl = SA_AUTH_SW_CTRL_SHA512;
-        sa_sha_setup(ctx, &ad);
-	/* } */
+	if(globule_count){
+		ad.aalg_id = SA_AALG_ID_SHA2_256;
+		ad.hash_size = SHA256_DIGEST_SIZE;
+		ad.auth_ctrl = SA_AUTH_SW_CTRL_SHA256;
+	}
+	else {
+		ad.aalg_id = SA_AALG_ID_SHA2_512;
+		ad.hash_size = SHA512_DIGEST_SIZE;
+		ad.auth_ctrl = SA_AUTH_SW_CTRL_SHA512;
+
+	}
+	sa_sha_setup(ctx, &ad);
 
 	ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback.ahash);
 	rctx->fallback_req.base.flags =
@@ -2501,6 +2507,23 @@ static int sa_sha256_cra_init(struct crypto_tfm *tfm)
 
 	sa_sha_setup(ctx, &ad);
 
+	globule_count = 1;
+	if(!ctx->buf){
+		ctx->buf = kmalloc(SA_MAX_DATA_SZ, GFP_KERNEL);
+		if(!ctx->buf) {
+			pr_err("Failed to allocate storage buffer\n");
+			return -ENOMEM;
+		}
+	}
+	
+	if(!ctx->bufr){
+		ctx->bufr = kmalloc(SA_MAX_DATA_SZ, GFP_KERNEL);
+		if(!ctx->bufr) {
+			pr_err("Failed to allocate storage buffer\n");
+			return -ENOMEM;
+		}
+	}
+
 	return 0;
 }
 
@@ -2533,16 +2556,20 @@ static int sa_sha512_cra_init_sha512(struct crypto_tfm *tfm)
 
 	sa_sha_setup(ctx, &ad);
 
-	ctx->buf = kmalloc(SA_MAX_DATA_SZ, GFP_KERNEL);
-	if(!ctx->buf) {
-		pr_err("Failed to allocate storage buffer\n");
-		return -ENOMEM;
+	if(!ctx->buf){
+		ctx->buf = kmalloc(SA_MAX_DATA_SZ, GFP_KERNEL);
+		if(!ctx->buf) {
+			pr_err("Failed to allocate storage buffer\n");
+			return -ENOMEM;
+		}
 	}
-
-        ctx->bufr = kmalloc(SA_MAX_DATA_SZ, GFP_KERNEL);
-	if(!ctx->bufr) {
-		pr_err("Failed to allocate storage buffer\n");
-		return -ENOMEM;
+	
+	if(!ctx->bufr){
+		ctx->bufr = kmalloc(SA_MAX_DATA_SZ, GFP_KERNEL);
+		if(!ctx->bufr) {
+			pr_err("Failed to allocate storage buffer\n");
+			return -ENOMEM;
+		}
 	}
 	globule_count = 0;
 
@@ -2582,6 +2609,12 @@ static void sa_sha_cra_exit(struct crypto_tfm *tfm)
 	if (crypto_tfm_alg_type(tfm) == CRYPTO_ALG_TYPE_AHASH)
 		sa_free_ctx_info(&ctx->enc, data);
 
+	if(ctx->buf)
+		kfree(ctx->buf);
+
+        if(ctx->bufr)
+		kfree(ctx->bufr);
+
 	crypto_free_shash(ctx->shash);
 	crypto_free_ahash(ctx->fallback.ahash);
 }
@@ -3006,11 +3039,11 @@ static struct sa_alg_tmpl sa_algs[] = {
 			.halg.digestsize	= SHA256_DIGEST_SIZE,
 			.halg.statesize		= sizeof(struct sa_sha_req_ctx) +
 						  sizeof(struct sha256_state),
-			.init			= sa_sha_init,
-			.update			= sa_sha_update,
-			.final			= sa_sha_final,
-			.finup			= sa_sha_finup,
-			.digest			= sa_sha_digest,
+			.init			= sa_sha_init_sha512,
+			.update			= sa_sha_update_sha512,
+			.final			= sa_sha_final_sha512,
+			.finup			= sa_sha_finup_sha512,
+			.digest			= sa_sha_digest_new,
 			.export			= sa_sha_export,
 			.import			= sa_sha_import,
 		},
@@ -3273,8 +3306,8 @@ static struct sa_match_data am64_match_data = {
 	.priv = 0,
 	.priv_id = 0,
 	.supported_algos = /* BIT(SA_ALG_CBC_AES) | */
-			   /* BIT(SA_ALG_EBC_AES) |
-			    * BIT(SA_ALG_SHA256) | */
+			   /* BIT(SA_ALG_EBC_AES) |*/
+			    BIT(SA_ALG_SHA256) |
         BIT(SA_ALG_SHA512),
 			   /* BIT(SA_ALG_AUTHENC_SHA256_AES), */
 };
-- 
2.34.1

