crypto: inside-secure - Added support for HMAC-SM3 ahash
authorPascal van Leeuwen <pascalvanl@gmail.com>
Fri, 13 Sep 2019 15:20:37 +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 hmac(sm3) ahash authentication algorithm

changes since v1:
- added Acked-by tag below, no changes to the source

changes since v2:
- nothing

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 32682e0cf84e74412fd9af89841905a59e5e4954..87d431afd44d0418f6067d4a3aea1574ee061356 100644 (file)
@@ -1177,6 +1177,7 @@ static struct safexcel_alg_template *safexcel_algs[] = {
        &safexcel_alg_chachapoly,
        &safexcel_alg_chachapoly_esp,
        &safexcel_alg_sm3,
+       &safexcel_alg_hmac_sm3,
 };
 
 static int safexcel_register_algorithms(struct safexcel_crypto_priv *priv)
index e2993b58e596470de3a383b444e7d9226be6bbfe..1b2d709dff39db50ea2701ccd9594ae327b5fed3 100644 (file)
@@ -877,5 +877,6 @@ 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;
+extern struct safexcel_alg_template safexcel_alg_hmac_sm3;
 
 #endif
index 873b77449b90c8661086469669df522551343182..272e5fd0de7e73eeba0dcd244fe97c6540bd006f 100644 (file)
@@ -2285,3 +2285,73 @@ struct safexcel_alg_template safexcel_alg_sm3 = {
                },
        },
 };
+
+static int safexcel_hmac_sm3_setkey(struct crypto_ahash *tfm, const u8 *key,
+                                   unsigned int keylen)
+{
+       return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-sm3",
+                                       SM3_DIGEST_SIZE);
+}
+
+static int safexcel_hmac_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));
+
+       /* Start from ipad precompute */
+       memcpy(req->state, ctx->ipad, SM3_DIGEST_SIZE);
+       /* Already processed the key^ipad part now! */
+       req->len        = SM3_BLOCK_SIZE;
+       req->processed  = SM3_BLOCK_SIZE;
+
+       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;
+       req->hmac = true;
+
+       return 0;
+}
+
+static int safexcel_hmac_sm3_digest(struct ahash_request *areq)
+{
+       int ret = safexcel_hmac_sm3_init(areq);
+
+       if (ret)
+               return ret;
+
+       return safexcel_ahash_finup(areq);
+}
+
+struct safexcel_alg_template safexcel_alg_hmac_sm3 = {
+       .type = SAFEXCEL_ALG_TYPE_AHASH,
+       .algo_mask = SAFEXCEL_ALG_SM3,
+       .alg.ahash = {
+               .init = safexcel_hmac_sm3_init,
+               .update = safexcel_ahash_update,
+               .final = safexcel_ahash_final,
+               .finup = safexcel_ahash_finup,
+               .digest = safexcel_hmac_sm3_digest,
+               .setkey = safexcel_hmac_sm3_setkey,
+               .export = safexcel_ahash_export,
+               .import = safexcel_ahash_import,
+               .halg = {
+                       .digestsize = SM3_DIGEST_SIZE,
+                       .statesize = sizeof(struct safexcel_ahash_export_state),
+                       .base = {
+                               .cra_name = "hmac(sm3)",
+                               .cra_driver_name = "safexcel-hmac-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,
+                       },
+               },
+       },
+};