]> git.baikalelectronics.ru Git - kernel.git/commitdiff
crypto: inside-secure - Added support for basic SM3 ahash
authorPascal van Leeuwen <pascalvanl@gmail.com>
Fri, 13 Sep 2019 15:20:36 +0000 (17:20 +0200)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 4 Oct 2019 15:06:03 +0000 (01:06 +1000)
Added support for the SM3 ahash algorithm

changes since v1:
- moved definition of CONTEXT_CONTROL_CRYPTO_ALG_SM3 (0x7) up above 0xf

changes since v2:
- allow compilation if CONFIG_CRYPTO_SM3 is not set

Acked-by: Antoine Tenart <antoine.tenart@bootlin.com>
Signed-off-by: Pascal van Leeuwen <pvanleeuwen@verimatrix.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
drivers/crypto/inside-secure/safexcel.c
drivers/crypto/inside-secure/safexcel.h
drivers/crypto/inside-secure/safexcel_hash.c

index b81f0bc5cd5071c5951b05b9f09f9cd2fca97f36..32682e0cf84e74412fd9af89841905a59e5e4954 100644 (file)
@@ -1176,6 +1176,7 @@ static struct safexcel_alg_template *safexcel_algs[] = {
        &safexcel_alg_chacha20,
        &safexcel_alg_chachapoly,
        &safexcel_alg_chachapoly_esp,
+       &safexcel_alg_sm3,
 };
 
 static int safexcel_register_algorithms(struct safexcel_crypto_priv *priv)
index 282d59e523e971308fb2b8645c90914750c938e9..e2993b58e596470de3a383b444e7d9226be6bbfe 100644 (file)
@@ -373,6 +373,7 @@ struct safexcel_context_record {
 #define CONTEXT_CONTROL_CRYPTO_ALG_XCBC128     (0x1 << 23)
 #define CONTEXT_CONTROL_CRYPTO_ALG_XCBC192     (0x2 << 23)
 #define CONTEXT_CONTROL_CRYPTO_ALG_XCBC256     (0x3 << 23)
+#define CONTEXT_CONTROL_CRYPTO_ALG_SM3         (0x7 << 23)
 #define CONTEXT_CONTROL_CRYPTO_ALG_POLY1305    (0xf << 23)
 #define CONTEXT_CONTROL_INV_FR                 (0x5 << 24)
 #define CONTEXT_CONTROL_INV_TR                 (0x6 << 24)
@@ -663,6 +664,12 @@ enum safexcel_eip_version {
 /* Priority we use for advertising our algorithms */
 #define SAFEXCEL_CRA_PRIORITY          300
 
+/* SM3 digest result for zero length message */
+#define EIP197_SM3_ZEROM_HASH  "\x1A\xB2\x1D\x83\x55\xCF\xA1\x7F" \
+                               "\x8E\x61\x19\x48\x31\xE8\x1A\x8F" \
+                               "\x22\xBE\xC8\xC7\x28\xFE\xFB\x74" \
+                               "\x7E\xD0\x35\xEB\x50\x82\xAA\x2B"
+
 /* EIP algorithm presence flags */
 enum safexcel_eip_algorithms {
        SAFEXCEL_ALG_BC0      = BIT(5),
@@ -869,5 +876,6 @@ extern struct safexcel_alg_template safexcel_alg_cmac;
 extern struct safexcel_alg_template safexcel_alg_chacha20;
 extern struct safexcel_alg_template safexcel_alg_chachapoly;
 extern struct safexcel_alg_template safexcel_alg_chachapoly_esp;
+extern struct safexcel_alg_template safexcel_alg_sm3;
 
 #endif
index 0224779f2984bea53240afc6e619032c7bf4f2d5..873b77449b90c8661086469669df522551343182 100644 (file)
@@ -10,6 +10,7 @@
 #include <crypto/md5.h>
 #include <crypto/sha.h>
 #include <crypto/skcipher.h>
+#include <crypto/sm3.h>
 #include <linux/device.h>
 #include <linux/dma-mapping.h>
 #include <linux/dmapool.h>
@@ -776,6 +777,14 @@ static int safexcel_ahash_final(struct ahash_request *areq)
                else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA512)
                        memcpy(areq->result, sha512_zero_message_hash,
                               SHA512_DIGEST_SIZE);
+               else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SM3) {
+                       if (IS_ENABLED(CONFIG_CRYPTO_SM3))
+                               memcpy(areq->result, sm3_zero_message_hash,
+                                      SM3_DIGEST_SIZE);
+                       else
+                               memcpy(areq->result,
+                                      EIP197_SM3_ZEROM_HASH, SM3_DIGEST_SIZE);
+               }
 
                return 0;
        } else if (unlikely(req->digest == CONTEXT_CONTROL_DIGEST_XCM &&
@@ -2221,3 +2230,58 @@ struct safexcel_alg_template safexcel_alg_cmac = {
                },
        },
 };
+
+static int safexcel_sm3_init(struct ahash_request *areq)
+{
+       struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
+       struct safexcel_ahash_req *req = ahash_request_ctx(areq);
+
+       memset(req, 0, sizeof(*req));
+
+       ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SM3;
+       req->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
+       req->state_sz = SM3_DIGEST_SIZE;
+       req->block_sz = SM3_BLOCK_SIZE;
+
+       return 0;
+}
+
+static int safexcel_sm3_digest(struct ahash_request *areq)
+{
+       int ret = safexcel_sm3_init(areq);
+
+       if (ret)
+               return ret;
+
+       return safexcel_ahash_finup(areq);
+}
+
+struct safexcel_alg_template safexcel_alg_sm3 = {
+       .type = SAFEXCEL_ALG_TYPE_AHASH,
+       .algo_mask = SAFEXCEL_ALG_SM3,
+       .alg.ahash = {
+               .init = safexcel_sm3_init,
+               .update = safexcel_ahash_update,
+               .final = safexcel_ahash_final,
+               .finup = safexcel_ahash_finup,
+               .digest = safexcel_sm3_digest,
+               .export = safexcel_ahash_export,
+               .import = safexcel_ahash_import,
+               .halg = {
+                       .digestsize = SM3_DIGEST_SIZE,
+                       .statesize = sizeof(struct safexcel_ahash_export_state),
+                       .base = {
+                               .cra_name = "sm3",
+                               .cra_driver_name = "safexcel-sm3",
+                               .cra_priority = SAFEXCEL_CRA_PRIORITY,
+                               .cra_flags = CRYPTO_ALG_ASYNC |
+                                            CRYPTO_ALG_KERN_DRIVER_ONLY,
+                               .cra_blocksize = SM3_BLOCK_SIZE,
+                               .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
+                               .cra_init = safexcel_ahash_cra_init,
+                               .cra_exit = safexcel_ahash_cra_exit,
+                               .cra_module = THIS_MODULE,
+                       },
+               },
+       },
+};