diff mbox series

[v2,4/7] crypto: sun4i-ss: handle BigEndian for cipher

Message ID 1600627038-40000-5-git-send-email-clabbe@baylibre.com
State Superseded
Headers show
Series crypto: sun4i-ss: prevent always fallback for ciphers | expand

Commit Message

Corentin Labbe Sept. 20, 2020, 6:37 p.m. UTC
Ciphers produce invalid results on BE.
Key and IV need to be written in LE.

Fixes: 6298e948215f2 ("crypto: sunxi-ss - Add Allwinner Security System crypto accelerator")
Cc: <stable@vger.kernel.org>
Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Comments

Arnd Bergmann Sept. 23, 2020, 6:59 p.m. UTC | #1
On Wed, Sep 23, 2020 at 8:08 PM LABBE Corentin <clabbe@baylibre.com> wrote:
> On Wed, Sep 23, 2020 at 04:00:32PM +0200, Arnd Bergmann wrote:

> > On Sun, Sep 20, 2020 at 8:37 PM Corentin Labbe <clabbe@baylibre.com> wrote:

> > > diff --git a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c

> > > index c6c25204780d..a05889745097 100644

> > > --- a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c

> > > +++ b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c

> > > @@ -52,13 +52,13 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)

> > >

> > >         spin_lock_irqsave(&ss->slock, flags);

> > >

> > > -       for (i = 0; i < op->keylen; i += 4)

> > > -               writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);

> > > +       for (i = 0; i < op->keylen / 4; i++)

> > > +               writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);

> >

> > I suspect what you actually want here is writesl() in place of the

> > loop. This skips the byteswap on big-endian, rather than swapping

> > each word twice.

> >

> > The point is that this register seems to act as a FIFO for a byte-stream

> > rather than a 32-bit fixed-endian register.

>

> Thanks, using writesl() fixes the warning, but I need to keep the loop

> since the register is different each time.


Ah, I see. I thought we had an interface for that as well, but I can't
find it now. I see memcpy_toio32() in one driver, but that implementation
appears to be wrong here (and probably also wrong for the machine
it was meant for)

There is the regular memcpy_toio(), but on big-endian Arm that
turns into a per-byte copy, which might either not work on your
hardware or be too slow.

There is also __iowrite32_copy(), which is not what I had remembered
but does seem to do what you want here.

> Or does it is better to use directly __raw_writel() ?


__raw_writel() is not very portable, so I would avoid that in normal
device drivers even when you only run them on specific hardware.

      Arnd
diff mbox series

Patch

diff --git a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
index c6c25204780d..a05889745097 100644
--- a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
+++ b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
@@ -52,13 +52,13 @@  static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 
 	spin_lock_irqsave(&ss->slock, flags);
 
-	for (i = 0; i < op->keylen; i += 4)
-		writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
+	for (i = 0; i < op->keylen / 4; i++)
+		writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);
 
 	if (areq->iv) {
 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
 			v = *(u32 *)(areq->iv + i * 4);
-			writel(v, ss->base + SS_IV0 + i * 4);
+			writel(cpu_to_le32(v), ss->base + SS_IV0 + i * 4);
 		}
 	}
 	writel(mode, ss->base + SS_CTL);
@@ -225,13 +225,13 @@  static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 
 	spin_lock_irqsave(&ss->slock, flags);
 
-	for (i = 0; i < op->keylen; i += 4)
-		writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
+	for (i = 0; i < op->keylen / 4; i++)
+		writel(cpu_to_le32(op->key[i]), ss->base + SS_KEY0 + i * 4);
 
 	if (areq->iv) {
 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
 			v = *(u32 *)(areq->iv + i * 4);
-			writel(v, ss->base + SS_IV0 + i * 4);
+			writel(cpu_to_le32(v), ss->base + SS_IV0 + i * 4);
 		}
 	}
 	writel(mode, ss->base + SS_CTL);