Re: .S_shipped unnecessary?

2018-11-08 Thread Masahiro Yamada
On Fri, Nov 9, 2018 at 8:42 AM Ard Biesheuvel  wrote:
>
> (+ Masahiro, kbuild ml)
>
> On 8 November 2018 at 21:37, Jason A. Donenfeld  wrote:
> > Hi Ard, Eric, and others,
> >
> > As promised, the next Zinc patchset will have less generated code! After a
> > bit of work with Andy and Samuel, I'll be bundling the perlasm.
> >
>
> Wonderful! Any problems doing that for x86_64 ?
>
> > One thing I'm wondering about, though, is the wisdom behind the current
> > .S_shipped pattern. Usually the _shipped is for big firmware blobs that are
> > hard (or impossible) to build independently. But in this case, the .S is
> > generated from the .pl significantly faster than gcc even compiles a basic
> > C file. And, since perl is needed to build the kernel anyway, it's not like
> > it will be impossible to find the right tools. Rather than clutter up 
> > commits
> > with the .pl _and_ the .S_shipped, what would you think if I just generated
> > the .S each time as an ordinary build artifact. AFAICT, this is fairly 
> > usual,
> > and it's hard to see downsides. Hence, why I'm writing this email: are there
> > any downsides to that?
> >
>
> I agree 100%. When I added this the first time, it was at the request
> of the ARM maintainer, who was reluctant to rely on Perl for some
> reason.
>
> Recently, we have had to add a kludge to prevent spurious rebuilds of
> the .S_shipped files as well.
>
> I'd be perfectly happy to get rid of this entirely, and always
> generate the .S from the .pl, which to me is kind of the point of
> carrying these files in the first place.
>
> Masahiro: do you see any problems with this?


No problem.


Documentation/process/changes.rst says the following:

You will need perl 5 and the following modules: ``Getopt::Long``,
``Getopt::Std``, ``File::Basename``, and ``File::Find`` to build the kernel.



We can assume perl is installed on the user's build machine.



--
Best Regards
Masahiro Yamada


Re: [PATCH] crypto/simd: correctly take reqsize of wrapped skcipher into account

2018-11-08 Thread Qian Cai



> On Nov 8, 2018, at 6:33 PM, Ard Biesheuvel  wrote:
> 
> On 8 November 2018 at 23:55, Ard Biesheuvel  wrote:
>> The simd wrapper's skcipher request context structure consists
>> of a single subrequest whose size is taken from the subordinate
>> skcipher. However, in simd_skcipher_init(), the reqsize that is
>> retrieved is not from the subordinate skcipher but from the
>> cryptd request structure, whose size is completely unrelated to
>> the actual wrapped skcipher.
>> 
>> Reported-by: Qian Cai 
>> Signed-off-by: Ard Biesheuvel 
>> ---
>> crypto/simd.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/crypto/simd.c b/crypto/simd.c
>> index ea7240be3001..2f3d6e897afc 100644
>> --- a/crypto/simd.c
>> +++ b/crypto/simd.c
>> @@ -125,7 +125,7 @@ static int simd_skcipher_init(struct crypto_skcipher 
>> *tfm)
>>ctx->cryptd_tfm = cryptd_tfm;
>> 
>>reqsize = sizeof(struct skcipher_request);
>> -   reqsize += crypto_skcipher_reqsize(_tfm->base);
>> +   reqsize += 
>> crypto_skcipher_reqsize(cryptd_skcipher_child(cryptd_tfm));
>> 
> 
> This should be
> 
> reqsize += max(crypto_skcipher_reqsize(_tfm->base);
>   crypto_skcipher_reqsize(cryptd_skcipher_child(cryptd_tfm)));
> 
> since the cryptd path in simd still needs some space in the subreq for
> the completion.
Tested-by: Qian Cai 


Re: .S_shipped unnecessary?

2018-11-08 Thread Jason A. Donenfeld
Hey Ard,

On Fri, Nov 9, 2018 at 12:42 AM Ard Biesheuvel
 wrote:
> Wonderful! Any problems doing that for x86_64 ?

The x86_64 is still a WIP, but hopefully we'll succeed.

> I agree 100%. When I added this the first time, it was at the request
> of the ARM maintainer, who was reluctant to rely on Perl for some
> reason.
>
> Recently, we have had to add a kludge to prevent spurious rebuilds of
> the .S_shipped files as well.
>
> I'd be perfectly happy to get rid of this entirely, and always
> generate the .S from the .pl, which to me is kind of the point of
> carrying these files in the first place.

Terrific. I'll move ahead in that direction then. It makes things _so_
much cleaner, and doesn't introduce new build modes ("should the
generated _ship go into the build directory or the source directory?
what kind of artifact is it? how to address $(srcdir) vs $(src) in
that context? bla bla") that really over complicate things.

Jason


Re: .S_shipped unnecessary?

2018-11-08 Thread Ard Biesheuvel
(+ Masahiro, kbuild ml)

On 8 November 2018 at 21:37, Jason A. Donenfeld  wrote:
> Hi Ard, Eric, and others,
>
> As promised, the next Zinc patchset will have less generated code! After a
> bit of work with Andy and Samuel, I'll be bundling the perlasm.
>

Wonderful! Any problems doing that for x86_64 ?

> One thing I'm wondering about, though, is the wisdom behind the current
> .S_shipped pattern. Usually the _shipped is for big firmware blobs that are
> hard (or impossible) to build independently. But in this case, the .S is
> generated from the .pl significantly faster than gcc even compiles a basic
> C file. And, since perl is needed to build the kernel anyway, it's not like
> it will be impossible to find the right tools. Rather than clutter up commits
> with the .pl _and_ the .S_shipped, what would you think if I just generated
> the .S each time as an ordinary build artifact. AFAICT, this is fairly usual,
> and it's hard to see downsides. Hence, why I'm writing this email: are there
> any downsides to that?
>

I agree 100%. When I added this the first time, it was at the request
of the ARM maintainer, who was reluctant to rely on Perl for some
reason.

Recently, we have had to add a kludge to prevent spurious rebuilds of
the .S_shipped files as well.

I'd be perfectly happy to get rid of this entirely, and always
generate the .S from the .pl, which to me is kind of the point of
carrying these files in the first place.

Masahiro: do you see any problems with this?


Re: [PATCH] crypto/simd: correctly take reqsize of wrapped skcipher into account

2018-11-08 Thread Ard Biesheuvel
On 8 November 2018 at 23:55, Ard Biesheuvel  wrote:
> The simd wrapper's skcipher request context structure consists
> of a single subrequest whose size is taken from the subordinate
> skcipher. However, in simd_skcipher_init(), the reqsize that is
> retrieved is not from the subordinate skcipher but from the
> cryptd request structure, whose size is completely unrelated to
> the actual wrapped skcipher.
>
> Reported-by: Qian Cai 
> Signed-off-by: Ard Biesheuvel 
> ---
>  crypto/simd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/crypto/simd.c b/crypto/simd.c
> index ea7240be3001..2f3d6e897afc 100644
> --- a/crypto/simd.c
> +++ b/crypto/simd.c
> @@ -125,7 +125,7 @@ static int simd_skcipher_init(struct crypto_skcipher *tfm)
> ctx->cryptd_tfm = cryptd_tfm;
>
> reqsize = sizeof(struct skcipher_request);
> -   reqsize += crypto_skcipher_reqsize(_tfm->base);
> +   reqsize += crypto_skcipher_reqsize(cryptd_skcipher_child(cryptd_tfm));
>

This should be

reqsize += max(crypto_skcipher_reqsize(_tfm->base);
   crypto_skcipher_reqsize(cryptd_skcipher_child(cryptd_tfm)));

since the cryptd path in simd still needs some space in the subreq for
the completion.


[PATCH] crypto/simd: correctly take reqsize of wrapped skcipher into account

2018-11-08 Thread Ard Biesheuvel
The simd wrapper's skcipher request context structure consists
of a single subrequest whose size is taken from the subordinate
skcipher. However, in simd_skcipher_init(), the reqsize that is
retrieved is not from the subordinate skcipher but from the
cryptd request structure, whose size is completely unrelated to
the actual wrapped skcipher.

Reported-by: Qian Cai 
Signed-off-by: Ard Biesheuvel 
---
 crypto/simd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/crypto/simd.c b/crypto/simd.c
index ea7240be3001..2f3d6e897afc 100644
--- a/crypto/simd.c
+++ b/crypto/simd.c
@@ -125,7 +125,7 @@ static int simd_skcipher_init(struct crypto_skcipher *tfm)
ctx->cryptd_tfm = cryptd_tfm;
 
reqsize = sizeof(struct skcipher_request);
-   reqsize += crypto_skcipher_reqsize(_tfm->base);
+   reqsize += crypto_skcipher_reqsize(cryptd_skcipher_child(cryptd_tfm));
 
crypto_skcipher_set_reqsize(tfm, reqsize);
 
-- 
2.19.1



.S_shipped unnecessary?

2018-11-08 Thread Jason A. Donenfeld
Hi Ard, Eric, and others,

As promised, the next Zinc patchset will have less generated code! After a
bit of work with Andy and Samuel, I'll be bundling the perlasm.

One thing I'm wondering about, though, is the wisdom behind the current
.S_shipped pattern. Usually the _shipped is for big firmware blobs that are
hard (or impossible) to build independently. But in this case, the .S is
generated from the .pl significantly faster than gcc even compiles a basic
C file. And, since perl is needed to build the kernel anyway, it's not like
it will be impossible to find the right tools. Rather than clutter up commits
with the .pl _and_ the .S_shipped, what would you think if I just generated
the .S each time as an ordinary build artifact. AFAICT, this is fairly usual,
and it's hard to see downsides. Hence, why I'm writing this email: are there
any downsides to that?

Thanks,
Jason


[PATCH 5/5] crypto: caam/qi2 - add support for Chacha20 + Poly1305

2018-11-08 Thread Horia Geantă
Add support for Chacha20 + Poly1305 combined AEAD:
-generic (rfc7539)
-IPsec (rfc7634 - known as rfc7539esp in the kernel)

Signed-off-by: Horia Geantă 
---
 drivers/crypto/caam/caamalg.c  |   4 +-
 drivers/crypto/caam/caamalg_desc.c |  24 ++-
 drivers/crypto/caam/caamalg_desc.h |   3 +-
 drivers/crypto/caam/caamalg_qi2.c  | 129 -
 4 files changed, 154 insertions(+), 6 deletions(-)

diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c
index cbaeb264a261..523565ce0060 100644
--- a/drivers/crypto/caam/caamalg.c
+++ b/drivers/crypto/caam/caamalg.c
@@ -527,13 +527,13 @@ static int chachapoly_set_sh_desc(struct crypto_aead 
*aead)
 
desc = ctx->sh_desc_enc;
cnstr_shdsc_chachapoly(desc, >cdata, >adata, ivsize,
-  ctx->authsize, true);
+  ctx->authsize, true, false);
dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
   desc_bytes(desc), ctx->dir);
 
desc = ctx->sh_desc_dec;
cnstr_shdsc_chachapoly(desc, >cdata, >adata, ivsize,
-  ctx->authsize, false);
+  ctx->authsize, false, false);
dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
   desc_bytes(desc), ctx->dir);
 
diff --git a/drivers/crypto/caam/caamalg_desc.c 
b/drivers/crypto/caam/caamalg_desc.c
index 0eb2add7e4e2..7db1640d3577 100644
--- a/drivers/crypto/caam/caamalg_desc.c
+++ b/drivers/crypto/caam/caamalg_desc.c
@@ -1227,10 +1227,12 @@ EXPORT_SYMBOL(cnstr_shdsc_rfc4543_decap);
  * @ivsize: initialization vector size
  * @icvsize: integrity check value (ICV) size (truncated or full)
  * @encap: true if encapsulation, false if decapsulation
+ * @is_qi: true when called from caam/qi
  */
 void cnstr_shdsc_chachapoly(u32 * const desc, struct alginfo *cdata,
struct alginfo *adata, unsigned int ivsize,
-   unsigned int icvsize, const bool encap)
+   unsigned int icvsize, const bool encap,
+   const bool is_qi)
 {
u32 *key_jump_cmd, *wait_cmd;
u32 nfifo;
@@ -1267,6 +1269,26 @@ void cnstr_shdsc_chachapoly(u32 * const desc, struct 
alginfo *cdata,
 OP_ALG_DECRYPT);
}
 
+   if (is_qi) {
+   u32 *wait_load_cmd;
+   u32 ctx1_iv_off = is_ipsec ? 8 : 4;
+
+   /* REG3 = assoclen */
+   append_seq_load(desc, 4, LDST_CLASS_DECO |
+   LDST_SRCDST_WORD_DECO_MATH3 |
+   4 << LDST_OFFSET_SHIFT);
+
+   wait_load_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
+   JUMP_COND_CALM | JUMP_COND_NCP |
+   JUMP_COND_NOP | JUMP_COND_NIP |
+   JUMP_COND_NIFP);
+   set_jump_tgt_here(desc, wait_load_cmd);
+
+   append_seq_load(desc, ivsize, LDST_CLASS_1_CCB |
+   LDST_SRCDST_BYTE_CONTEXT |
+   ctx1_iv_off << LDST_OFFSET_SHIFT);
+   }
+
/*
 * MAGIC with NFIFO
 * Read associated data from the input and send them to class1 and
diff --git a/drivers/crypto/caam/caamalg_desc.h 
b/drivers/crypto/caam/caamalg_desc.h
index a1a7b0e6889d..d5ca42ff961a 100644
--- a/drivers/crypto/caam/caamalg_desc.h
+++ b/drivers/crypto/caam/caamalg_desc.h
@@ -98,7 +98,8 @@ void cnstr_shdsc_rfc4543_decap(u32 * const desc, struct 
alginfo *cdata,
 
 void cnstr_shdsc_chachapoly(u32 * const desc, struct alginfo *cdata,
struct alginfo *adata, unsigned int ivsize,
-   unsigned int icvsize, const bool encap);
+   unsigned int icvsize, const bool encap,
+   const bool is_qi);
 
 void cnstr_shdsc_skcipher_encap(u32 * const desc, struct alginfo *cdata,
unsigned int ivsize, const bool is_rfc3686,
diff --git a/drivers/crypto/caam/caamalg_qi2.c 
b/drivers/crypto/caam/caamalg_qi2.c
index a9e264bb9629..2598640aa98b 100644
--- a/drivers/crypto/caam/caamalg_qi2.c
+++ b/drivers/crypto/caam/caamalg_qi2.c
@@ -462,7 +462,15 @@ static struct aead_edesc *aead_edesc_alloc(struct 
aead_request *req,
edesc->dst_nents = dst_nents;
edesc->iv_dma = iv_dma;
 
-   edesc->assoclen = cpu_to_caam32(req->assoclen);
+   if ((alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK) ==
+   OP_ALG_ALGSEL_CHACHA20 && ivsize != CHACHAPOLY_IV_SIZE)
+   /*
+* The associated data comes already with the IV but we need
+* to skip it when we authenticate or encrypt...
+*/
+   edesc->assoclen = cpu_to_caam32(req->assoclen - ivsize);
+   else

[PATCH 4/5] crypto: caam/jr - add support for Chacha20 + Poly1305

2018-11-08 Thread Horia Geantă
Add support for Chacha20 + Poly1305 combined AEAD:
-generic (rfc7539)
-IPsec (rfc7634 - known as rfc7539esp in the kernel)

Signed-off-by: Cristian Stoica 
Signed-off-by: Horia Geantă 
---
 drivers/crypto/caam/caamalg.c  | 221 -
 drivers/crypto/caam/caamalg_desc.c | 111 +++
 drivers/crypto/caam/caamalg_desc.h |   4 +
 drivers/crypto/caam/compat.h   |   1 +
 drivers/crypto/caam/desc.h |  15 +++
 drivers/crypto/caam/desc_constr.h  |   7 +-
 6 files changed, 354 insertions(+), 5 deletions(-)

diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c
index 9f1414030bc2..cbaeb264a261 100644
--- a/drivers/crypto/caam/caamalg.c
+++ b/drivers/crypto/caam/caamalg.c
@@ -72,6 +72,8 @@
 #define AUTHENC_DESC_JOB_IO_LEN(AEAD_DESC_JOB_IO_LEN + \
 CAAM_CMD_SZ * 5)
 
+#define CHACHAPOLY_DESC_JOB_IO_LEN (AEAD_DESC_JOB_IO_LEN + CAAM_CMD_SZ * 6)
+
 #define DESC_MAX_USED_BYTES(CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN)
 #define DESC_MAX_USED_LEN  (DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
 
@@ -513,6 +515,61 @@ static int rfc4543_setauthsize(struct crypto_aead *authenc,
return 0;
 }
 
+static int chachapoly_set_sh_desc(struct crypto_aead *aead)
+{
+   struct caam_ctx *ctx = crypto_aead_ctx(aead);
+   struct device *jrdev = ctx->jrdev;
+   unsigned int ivsize = crypto_aead_ivsize(aead);
+   u32 *desc;
+
+   if (!ctx->cdata.keylen || !ctx->authsize)
+   return 0;
+
+   desc = ctx->sh_desc_enc;
+   cnstr_shdsc_chachapoly(desc, >cdata, >adata, ivsize,
+  ctx->authsize, true);
+   dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
+  desc_bytes(desc), ctx->dir);
+
+   desc = ctx->sh_desc_dec;
+   cnstr_shdsc_chachapoly(desc, >cdata, >adata, ivsize,
+  ctx->authsize, false);
+   dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
+  desc_bytes(desc), ctx->dir);
+
+   return 0;
+}
+
+static int chachapoly_setauthsize(struct crypto_aead *aead,
+ unsigned int authsize)
+{
+   struct caam_ctx *ctx = crypto_aead_ctx(aead);
+
+   if (authsize != POLY1305_DIGEST_SIZE)
+   return -EINVAL;
+
+   ctx->authsize = authsize;
+   return chachapoly_set_sh_desc(aead);
+}
+
+static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
+unsigned int keylen)
+{
+   struct caam_ctx *ctx = crypto_aead_ctx(aead);
+   unsigned int ivsize = crypto_aead_ivsize(aead);
+   unsigned int saltlen = CHACHAPOLY_IV_SIZE - ivsize;
+
+   if (keylen != CHACHA20_KEY_SIZE + saltlen) {
+   crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
+   return -EINVAL;
+   }
+
+   ctx->cdata.key_virt = key;
+   ctx->cdata.keylen = keylen - saltlen;
+
+   return chachapoly_set_sh_desc(aead);
+}
+
 static int aead_setkey(struct crypto_aead *aead,
   const u8 *key, unsigned int keylen)
 {
@@ -1031,6 +1088,40 @@ static void init_gcm_job(struct aead_request *req,
/* End of blank commands */
 }
 
+static void init_chachapoly_job(struct aead_request *req,
+   struct aead_edesc *edesc, bool all_contig,
+   bool encrypt)
+{
+   struct crypto_aead *aead = crypto_aead_reqtfm(req);
+   unsigned int ivsize = crypto_aead_ivsize(aead);
+   unsigned int assoclen = req->assoclen;
+   u32 *desc = edesc->hw_desc;
+   u32 ctx_iv_off = 4;
+
+   init_aead_job(req, edesc, all_contig, encrypt);
+
+   if (ivsize != CHACHAPOLY_IV_SIZE) {
+   /* IPsec specific: CONTEXT1[223:128] = {NONCE, IV} */
+   ctx_iv_off += 4;
+
+   /*
+* The associated data comes already with the IV but we need
+* to skip it when we authenticate or encrypt...
+*/
+   assoclen -= ivsize;
+   }
+
+   append_math_add_imm_u32(desc, REG3, ZERO, IMM, assoclen);
+
+   /*
+* For IPsec load the IV further in the same register.
+* For RFC7539 simply load the 12 bytes nonce in a single operation
+*/
+   append_load_as_imm(desc, req->iv, ivsize, LDST_CLASS_1_CCB |
+  LDST_SRCDST_BYTE_CONTEXT |
+  ctx_iv_off << LDST_OFFSET_SHIFT);
+}
+
 static void init_authenc_job(struct aead_request *req,
 struct aead_edesc *edesc,
 bool all_contig, bool encrypt)
@@ -1289,6 +1380,72 @@ static int gcm_encrypt(struct aead_request *req)
return ret;
 }
 
+static int chachapoly_encrypt(struct aead_request *req)
+{
+   struct aead_edesc *edesc;
+   struct crypto_aead *aead = 

[PATCH 0/5] crypto: caam - add support for Era 10

2018-11-08 Thread Horia Geantă
This patch set adds support for CAAM Era 10, currently used in LX2160A SoC:
-new register mapping: some registers/fields are deprecated and moved
to different locations, mainly version registers
-algorithms
chacha20 (over DPSECI - Data Path SEC Interface on fsl-mc bus)
rfc7539(chacha20,poly1305) (over both DPSECI and Job Ring Interface)
rfc7539esp(chacha20,poly1305) (over both DPSECI and Job Ring Interface)

Note: the patch set is generated on top of cryptodev-2.6, however testing
was performed based on linux-next (tag: next-20181108) - which includes
LX2160A platform support + manually updating LX2160A dts with:
-fsl-mc bus DT node
-missing dma-ranges property in soc DT node

Cristian Stoica (1):
  crypto: export CHACHAPOLY_IV_SIZE

Horia Geantă (4):
  crypto: caam - add register map changes cf. Era 10
  crypto: caam/qi2 - add support for ChaCha20
  crypto: caam/jr - add support for Chacha20 + Poly1305
  crypto: caam/qi2 - add support for Chacha20 + Poly1305

 crypto/chacha20poly1305.c  |   2 -
 drivers/crypto/caam/caamalg.c  | 266 ++---
 drivers/crypto/caam/caamalg_desc.c | 139 ++-
 drivers/crypto/caam/caamalg_desc.h |   5 +
 drivers/crypto/caam/caamalg_qi.c   |  37 --
 drivers/crypto/caam/caamalg_qi2.c  | 156 +-
 drivers/crypto/caam/caamhash.c |  20 ++-
 drivers/crypto/caam/caampkc.c  |  10 +-
 drivers/crypto/caam/caamrng.c  |  10 +-
 drivers/crypto/caam/compat.h   |   2 +
 drivers/crypto/caam/ctrl.c |  28 +++-
 drivers/crypto/caam/desc.h |  28 
 drivers/crypto/caam/desc_constr.h  |   7 +-
 drivers/crypto/caam/regs.h |  74 +--
 include/crypto/chacha20.h  |   1 +
 15 files changed, 724 insertions(+), 61 deletions(-)

-- 
2.16.2



[PATCH 1/5] crypto: caam - add register map changes cf. Era 10

2018-11-08 Thread Horia Geantă
Era 10 changes the register map.

The updates that affect the drivers:
-new version registers are added
-DBG_DBG[deco_state] field is moved to a new register -
DBG_EXEC[19:16] @ 8_0E3Ch.

Signed-off-by: Horia Geantă 
---
 drivers/crypto/caam/caamalg.c| 47 +
 drivers/crypto/caam/caamalg_qi.c | 37 +++-
 drivers/crypto/caam/caamhash.c   | 20 ---
 drivers/crypto/caam/caampkc.c| 10 --
 drivers/crypto/caam/caamrng.c| 10 +-
 drivers/crypto/caam/ctrl.c   | 28 +++
 drivers/crypto/caam/desc.h   |  7 
 drivers/crypto/caam/regs.h   | 74 ++--
 8 files changed, 184 insertions(+), 49 deletions(-)

diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c
index 869f092432de..9f1414030bc2 100644
--- a/drivers/crypto/caam/caamalg.c
+++ b/drivers/crypto/caam/caamalg.c
@@ -3135,7 +3135,7 @@ static int __init caam_algapi_init(void)
struct device *ctrldev;
struct caam_drv_private *priv;
int i = 0, err = 0;
-   u32 cha_vid, cha_inst, des_inst, aes_inst, md_inst;
+   u32 aes_vid, aes_inst, des_inst, md_vid, md_inst;
unsigned int md_limit = SHA512_DIGEST_SIZE;
bool registered = false;
 
@@ -3168,14 +3168,34 @@ static int __init caam_algapi_init(void)
 * Register crypto algorithms the device supports.
 * First, detect presence and attributes of DES, AES, and MD blocks.
 */
-   cha_vid = rd_reg32(>ctrl->perfmon.cha_id_ls);
-   cha_inst = rd_reg32(>ctrl->perfmon.cha_num_ls);
-   des_inst = (cha_inst & CHA_ID_LS_DES_MASK) >> CHA_ID_LS_DES_SHIFT;
-   aes_inst = (cha_inst & CHA_ID_LS_AES_MASK) >> CHA_ID_LS_AES_SHIFT;
-   md_inst = (cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
+   if (priv->era < 10) {
+   u32 cha_vid, cha_inst;
+
+   cha_vid = rd_reg32(>ctrl->perfmon.cha_id_ls);
+   aes_vid = cha_vid & CHA_ID_LS_AES_MASK;
+   md_vid = (cha_vid & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
+
+   cha_inst = rd_reg32(>ctrl->perfmon.cha_num_ls);
+   des_inst = (cha_inst & CHA_ID_LS_DES_MASK) >>
+  CHA_ID_LS_DES_SHIFT;
+   aes_inst = cha_inst & CHA_ID_LS_AES_MASK;
+   md_inst = (cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
+   } else {
+   u32 aesa, mdha;
+
+   aesa = rd_reg32(>ctrl->vreg.aesa);
+   mdha = rd_reg32(>ctrl->vreg.mdha);
+
+   aes_vid = (aesa & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
+   md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
+
+   des_inst = rd_reg32(>ctrl->vreg.desa) & CHA_VER_NUM_MASK;
+   aes_inst = aesa & CHA_VER_NUM_MASK;
+   md_inst = mdha & CHA_VER_NUM_MASK;
+   }
 
/* If MD is present, limit digest size based on LP256 */
-   if (md_inst && ((cha_vid & CHA_ID_LS_MD_MASK) == CHA_ID_LS_MD_LP256))
+   if (md_inst && md_vid  == CHA_VER_VID_MD_LP256)
md_limit = SHA256_DIGEST_SIZE;
 
for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
@@ -3196,10 +3216,10 @@ static int __init caam_algapi_init(void)
 * Check support for AES modes not available
 * on LP devices.
 */
-   if ((cha_vid & CHA_ID_LS_AES_MASK) == CHA_ID_LS_AES_LP)
-   if ((t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK) ==
-OP_ALG_AAI_XTS)
-   continue;
+   if (aes_vid == CHA_VER_VID_AES_LP &&
+   (t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK) ==
+   OP_ALG_AAI_XTS)
+   continue;
 
caam_skcipher_alg_init(t_alg);
 
@@ -3236,9 +3256,8 @@ static int __init caam_algapi_init(void)
 * Check support for AES algorithms not available
 * on LP devices.
 */
-   if ((cha_vid & CHA_ID_LS_AES_MASK) == CHA_ID_LS_AES_LP)
-   if (alg_aai == OP_ALG_AAI_GCM)
-   continue;
+   if (aes_vid  == CHA_VER_VID_AES_LP && alg_aai == OP_ALG_AAI_GCM)
+   continue;
 
/*
 * Skip algorithms requiring message digests
diff --git a/drivers/crypto/caam/caamalg_qi.c b/drivers/crypto/caam/caamalg_qi.c
index 23c9fc4975f8..c0d55310aade 100644
--- a/drivers/crypto/caam/caamalg_qi.c
+++ b/drivers/crypto/caam/caamalg_qi.c
@@ -2462,7 +2462,7 @@ static int __init caam_qi_algapi_init(void)
struct device *ctrldev;
struct caam_drv_private *priv;
int i = 0, err = 0;
-   u32 cha_vid, cha_inst, des_inst, aes_inst, md_inst;
+   u32 aes_vid, aes_inst, des_inst, md_vid, md_inst;
unsigned int md_limit = SHA512_DIGEST_SIZE;
bool registered = false;
 
@@ -2497,14 

[PATCH 3/5] crypto: export CHACHAPOLY_IV_SIZE

2018-11-08 Thread Horia Geantă
From: Cristian Stoica 

Move CHACHAPOLY_IV_SIZE to header file, so it can be reused.

Signed-off-by: Cristian Stoica 
Signed-off-by: Horia Geantă 
---
 crypto/chacha20poly1305.c | 2 --
 include/crypto/chacha20.h | 1 +
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/crypto/chacha20poly1305.c b/crypto/chacha20poly1305.c
index 600afa99941f..f9dd5453046a 100644
--- a/crypto/chacha20poly1305.c
+++ b/crypto/chacha20poly1305.c
@@ -22,8 +22,6 @@
 
 #include "internal.h"
 
-#define CHACHAPOLY_IV_SIZE 12
-
 struct chachapoly_instance_ctx {
struct crypto_skcipher_spawn chacha;
struct crypto_ahash_spawn poly;
diff --git a/include/crypto/chacha20.h b/include/crypto/chacha20.h
index f76302d99e2b..2d3129442a52 100644
--- a/include/crypto/chacha20.h
+++ b/include/crypto/chacha20.h
@@ -13,6 +13,7 @@
 #define CHACHA20_IV_SIZE   16
 #define CHACHA20_KEY_SIZE  32
 #define CHACHA20_BLOCK_SIZE64
+#define CHACHAPOLY_IV_SIZE 12
 
 struct chacha20_ctx {
u32 key[8];
-- 
2.16.2



[PATCH 2/5] crypto: caam/qi2 - add support for ChaCha20

2018-11-08 Thread Horia Geantă
Add support for ChaCha20 skcipher algorithm.

Signed-off-by: Carmen Iorga 
Signed-off-by: Horia Geantă 
---
 drivers/crypto/caam/caamalg_desc.c |  6 --
 drivers/crypto/caam/caamalg_qi2.c  | 27 +--
 drivers/crypto/caam/compat.h   |  1 +
 drivers/crypto/caam/desc.h |  6 ++
 4 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/drivers/crypto/caam/caamalg_desc.c 
b/drivers/crypto/caam/caamalg_desc.c
index 1a6f0da14106..d850590079a2 100644
--- a/drivers/crypto/caam/caamalg_desc.c
+++ b/drivers/crypto/caam/caamalg_desc.c
@@ -1228,7 +1228,8 @@ static inline void skcipher_append_src_dst(u32 *desc)
  * @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
@@ -1293,7 +1294,8 @@ EXPORT_SYMBOL(cnstr_shdsc_skcipher_encap);
  * @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
diff --git a/drivers/crypto/caam/caamalg_qi2.c 
b/drivers/crypto/caam/caamalg_qi2.c
index 7d8ac0222fa3..a9e264bb9629 100644
--- a/drivers/crypto/caam/caamalg_qi2.c
+++ b/drivers/crypto/caam/caamalg_qi2.c
@@ -816,7 +816,9 @@ static int skcipher_setkey(struct crypto_skcipher 
*skcipher, const u8 *key,
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__)": ",
@@ -1494,7 +1496,23 @@ static struct caam_skcipher_alg driver_algs[] = {
.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[] = {
@@ -4908,6 +4926,11 @@ static int dpaa2_caam_probe(struct fsl_mc_device 
*dpseci_dev)
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);
 
diff --git a/drivers/crypto/caam/compat.h b/drivers/crypto/caam/compat.h
index 9604ff7a335e..a5081b4050b6 100644
--- a/drivers/crypto/caam/compat.h
+++ b/drivers/crypto/caam/compat.h
@@ -36,6 +36,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
diff --git a/drivers/crypto/caam/desc.h b/drivers/crypto/caam/desc.h
index ec1ef06049b4..9d117e51629f 100644
--- a/drivers/crypto/caam/desc.h
+++ b/drivers/crypto/caam/desc.h
@@ -1159,6 +1159,7 @@
 #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)
@@ -1206,6 +1207,11 @@
 #define OP_ALG_AAI_RNG4_AI (0x80 << OP_ALG_AAI_SHIFT)
 #define OP_ALG_AAI_RNG4_SK (0x100 <<