]> git.baikalelectronics.ru Git - kernel.git/commitdiff
crypto: inside-secure - Add support for the rfc3685(ctr(sm4)) skcipher
authorPascal van Leeuwen <pascalvanl@gmail.com>
Fri, 13 Sep 2019 09:10:41 +0000 (11:10 +0200)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 4 Oct 2019 15:06:05 +0000 (01:06 +1000)
This patch adds support for SM4 in (32 bit) CTR mode, i.e. skcipher
rfc3686(ctr(sm4)).

changes since v1:
- nothing

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_cipher.c

index be39f0bbe909960cf81fb442d017c5fe29cf1c5c..3c140d8c3ae47a726545cb1aba278b9b0a7cf524 100644 (file)
@@ -1182,6 +1182,7 @@ static struct safexcel_alg_template *safexcel_algs[] = {
        &safexcel_alg_cbc_sm4,
        &safexcel_alg_ofb_sm4,
        &safexcel_alg_cfb_sm4,
+       &safexcel_alg_ctr_sm4,
 };
 
 static int safexcel_register_algorithms(struct safexcel_crypto_priv *priv)
index 3f729a604136d5337647bc85939c1401ac1e4cdf..62965fb63dc53de6b9ff968b71cf2dfba4377857 100644 (file)
@@ -883,5 +883,6 @@ extern struct safexcel_alg_template safexcel_alg_ecb_sm4;
 extern struct safexcel_alg_template safexcel_alg_cbc_sm4;
 extern struct safexcel_alg_template safexcel_alg_ofb_sm4;
 extern struct safexcel_alg_template safexcel_alg_cfb_sm4;
+extern struct safexcel_alg_template safexcel_alg_ctr_sm4;
 
 #endif
index 583cc8331706e733892aea22d4d17413371e3249..ed1b186a5535bb83808b329ee3adc656bd0e4833 100644 (file)
@@ -2815,3 +2815,54 @@ struct safexcel_alg_template safexcel_alg_cfb_sm4 = {
                },
        },
 };
+
+static int safexcel_skcipher_sm4ctr_setkey(struct crypto_skcipher *ctfm,
+                                          const u8 *key, unsigned int len)
+{
+       struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
+       struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
+
+       /* last 4 bytes of key are the nonce! */
+       ctx->nonce = *(u32 *)(key + len - CTR_RFC3686_NONCE_SIZE);
+       /* exclude the nonce here */
+       len -= CTR_RFC3686_NONCE_SIZE;
+
+       return safexcel_skcipher_sm4_setkey(ctfm, key, len);
+}
+
+static int safexcel_skcipher_sm4_ctr_cra_init(struct crypto_tfm *tfm)
+{
+       struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
+
+       safexcel_skcipher_cra_init(tfm);
+       ctx->alg  = SAFEXCEL_SM4;
+       ctx->mode = CONTEXT_CONTROL_CRYPTO_MODE_CTR_LOAD;
+       return 0;
+}
+
+struct safexcel_alg_template safexcel_alg_ctr_sm4 = {
+       .type = SAFEXCEL_ALG_TYPE_SKCIPHER,
+       .algo_mask = SAFEXCEL_ALG_SM4,
+       .alg.skcipher = {
+               .setkey = safexcel_skcipher_sm4ctr_setkey,
+               .encrypt = safexcel_encrypt,
+               .decrypt = safexcel_decrypt,
+               /* Add nonce size */
+               .min_keysize = SM4_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
+               .max_keysize = SM4_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
+               .ivsize = CTR_RFC3686_IV_SIZE,
+               .base = {
+                       .cra_name = "rfc3686(ctr(sm4))",
+                       .cra_driver_name = "safexcel-ctr-sm4",
+                       .cra_priority = SAFEXCEL_CRA_PRIORITY,
+                       .cra_flags = CRYPTO_ALG_ASYNC |
+                                    CRYPTO_ALG_KERN_DRIVER_ONLY,
+                       .cra_blocksize = 1,
+                       .cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
+                       .cra_alignmask = 0,
+                       .cra_init = safexcel_skcipher_sm4_ctr_cra_init,
+                       .cra_exit = safexcel_skcipher_cra_exit,
+                       .cra_module = THIS_MODULE,
+               },
+       },
+};