Add support for ChaCha20 skcipher algorithm.
Signed-off-by: Carmen Iorga <carmen.iorga@nxp.com>
Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  * @desc: pointer to buffer used for descriptor construction
  * @cdata: pointer to block cipher transform definitions
  *         Valid algorithm values - one of OP_ALG_ALGSEL_{AES, DES, 3DES} ANDed
- *         with OP_ALG_AAI_CBC or OP_ALG_AAI_CTR_MOD128.
+ *         with OP_ALG_AAI_CBC or OP_ALG_AAI_CTR_MOD128
+ *                                - OP_ALG_ALGSEL_CHACHA20
  * @ivsize: initialization vector size
  * @is_rfc3686: true when ctr(aes) is wrapped by rfc3686 template
  * @ctx1_iv_off: IV offset in CONTEXT1 register
  * @desc: pointer to buffer used for descriptor construction
  * @cdata: pointer to block cipher transform definitions
  *         Valid algorithm values - one of OP_ALG_ALGSEL_{AES, DES, 3DES} ANDed
- *         with OP_ALG_AAI_CBC or OP_ALG_AAI_CTR_MOD128.
+ *         with OP_ALG_AAI_CBC or OP_ALG_AAI_CTR_MOD128
+ *                                - OP_ALG_ALGSEL_CHACHA20
  * @ivsize: initialization vector size
  * @is_rfc3686: true when ctr(aes) is wrapped by rfc3686 template
  * @ctx1_iv_off: IV offset in CONTEXT1 register
 
        u32 *desc;
        u32 ctx1_iv_off = 0;
        const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
-                              OP_ALG_AAI_CTR_MOD128);
+                              OP_ALG_AAI_CTR_MOD128) &&
+                              ((ctx->cdata.algtype & OP_ALG_ALGSEL_MASK) !=
+                              OP_ALG_ALGSEL_CHACHA20);
        const bool is_rfc3686 = alg->caam.rfc3686;
 
        print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
                        .ivsize = AES_BLOCK_SIZE,
                },
                .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
-       }
+       },
+       {
+               .skcipher = {
+                       .base = {
+                               .cra_name = "chacha20",
+                               .cra_driver_name = "chacha20-caam-qi2",
+                               .cra_blocksize = 1,
+                       },
+                       .setkey = skcipher_setkey,
+                       .encrypt = skcipher_encrypt,
+                       .decrypt = skcipher_decrypt,
+                       .min_keysize = CHACHA20_KEY_SIZE,
+                       .max_keysize = CHACHA20_KEY_SIZE,
+                       .ivsize = CHACHA20_IV_SIZE,
+               },
+               .caam.class1_alg_type = OP_ALG_ALGSEL_CHACHA20,
+       },
 };
 
 static struct caam_aead_alg driver_aeads[] = {
                    alg_sel == OP_ALG_ALGSEL_AES)
                        continue;
 
+               /* Skip CHACHA20 algorithms if not supported by device */
+               if (alg_sel == OP_ALG_ALGSEL_CHACHA20 &&
+                   !priv->sec_attr.ccha_acc_num)
+                       continue;
+
                t_alg->caam.dev = dev;
                caam_skcipher_alg_init(t_alg);
 
 
 #include <crypto/gcm.h>
 #include <crypto/sha.h>
 #include <crypto/md5.h>
+#include <crypto/chacha20.h>
 #include <crypto/internal/aead.h>
 #include <crypto/authenc.h>
 #include <crypto/akcipher.h>
 
 #define OP_ALG_ALGSEL_KASUMI   (0x70 << OP_ALG_ALGSEL_SHIFT)
 #define OP_ALG_ALGSEL_CRC      (0x90 << OP_ALG_ALGSEL_SHIFT)
 #define OP_ALG_ALGSEL_SNOW_F9  (0xA0 << OP_ALG_ALGSEL_SHIFT)
+#define OP_ALG_ALGSEL_CHACHA20 (0xD0 << OP_ALG_ALGSEL_SHIFT)
 
 #define OP_ALG_AAI_SHIFT       4
 #define OP_ALG_AAI_MASK                (0x1ff << OP_ALG_AAI_SHIFT)
 #define OP_ALG_AAI_RNG4_AI     (0x80 << OP_ALG_AAI_SHIFT)
 #define OP_ALG_AAI_RNG4_SK     (0x100 << OP_ALG_AAI_SHIFT)
 
+/* Chacha20 AAI set */
+#define OP_ALG_AAI_AEAD        (0x002 << OP_ALG_AAI_SHIFT)
+#define OP_ALG_AAI_KEYSTREAM   (0x001 << OP_ALG_AAI_SHIFT)
+#define OP_ALG_AAI_BC8         (0x008 << OP_ALG_AAI_SHIFT)
+
 /* hmac/smac AAI set */
 #define OP_ALG_AAI_HASH                (0x00 << OP_ALG_AAI_SHIFT)
 #define OP_ALG_AAI_HMAC                (0x01 << OP_ALG_AAI_SHIFT)