Skip to contentSkip to conversation

crypto: driver for tegra AES hardware

PATCH Superseded [email protected] 5 participants 13 messages Nov 5, 2011 – Nov 17, 2011 10 messages not in the archive

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: driver for tegra AES hardware

From: Varun Wadekar <[email protected]>

driver supports ecb/cbc/ofb/ansi_x9.31rng modes and 128, 192 and 256-bit key sizes.

---
v1: fixed comments from Stephen Warren, Kim Phillips and Henning Heinold

 drivers/crypto/Kconfig     |    8 +
 drivers/crypto/Makefile    |    1 +
 drivers/crypto/tegra-aes.c | 1090 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/crypto/tegra-aes.h |  103 +++++
 4 files changed, 1202 insertions(+), 0 deletions(-)
 create mode 100644 drivers/crypto/tegra-aes.c
 create mode 100644 drivers/crypto/tegra-aes.h
drivers/crypto/Kconfig +8 -0
index 6d16b4b..6799188 100644
@@ -293,4 +293,12 @@ config CRYPTO_DEV_S5P
 	  Select this to offload Samsung S5PV210 or S5PC110 from AES
 	  algorithms execution.
 
+config CRYPTO_DEV_TEGRA_AES
+	tristate "Support for TEGRA AES hw engine"
+	depends on ARCH_TEGRA
+	select CRYPTO_AES
+	help
+	  TEGRA processors have AES module accelerator. Select this if you
+	  want to use the TEGRA module for AES algorithms.
+
 endif # CRYPTO_HW
drivers/crypto/Makefile +1 -0
index 53ea501..f3e64ea 100644
@@ -13,3 +13,4 @@ obj-$(CONFIG_CRYPTO_DEV_OMAP_SHAM) += omap-sham.o
 obj-$(CONFIG_CRYPTO_DEV_OMAP_AES) += omap-aes.o
 obj-$(CONFIG_CRYPTO_DEV_PICOXCELL) += picoxcell_crypto.o
 obj-$(CONFIG_CRYPTO_DEV_S5P) += s5p-sss.o
+obj-$(CONFIG_CRYPTO_DEV_TEGRA_AES) += tegra-aes.o
drivers/crypto/tegra-aes.c new file +1090 -0

1,090 lines not on this page · raw message

drivers/crypto/tegra-aes.h new file +103 -0

103 lines not on this page · raw message

Signature · 1 line
1.7.1

A message that is not in the archive (sent off-list or not yet received): 1320489734-9085-1-git-send-email-vwadekar-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org

#2 lore raw

drivers/crypto/Kconfig config CRYPTO_DEV_S5P · line 303

Quoted 8 lines · On Sat, 5 Nov 2011 16:12:14 +0530 <[email protected]…
drivers/crypto/Kconfig +8 -0
+config CRYPTO_DEV_TEGRA_AES
+	tristate "Support for TEGRA AES hw engine"
+	depends on ARCH_TEGRA
+	select CRYPTO_AES
+	help
+	  TEGRA processors have AES module accelerator. Select this if you
+	  want to use the TEGRA module for AES algorithms.
+

"To compile this driver as a module, choose M here: the module will be called tegra-aes."

Quoted 7 lines
drivers/crypto/tegra-aes.c new file +7 -0
+static int aes_start_crypt(struct tegra_aes_dev *dd, u32 in_addr, u32 out_addr,
+	int nblocks, int mode, bool upd_iv)
+{
+	u32 cmdq[AES_HW_MAX_ICQ_LENGTH];
+	int i, eng_busy, icq_empty, ret;
+	u32 value;
+

drivers/crypto/tegra-aes.c line 235

Quoted 10 lines
drivers/crypto/tegra-aes.c new file +10 -0
+	/* reset all the interrupt bits */
+	aes_writel(dd, 0xFFFFFFFF, TEGRA_AES_INTR_STATUS);
+
+	/* enable error, dma xfer complete interrupts */
+	aes_writel(dd, 0x33, TEGRA_AES_INT_ENB);
+
+	/* this module is shared with the other hardware blocks
+	 * and there have been cases where another user of the VDE
+	 * has caused this irq to trigger */
+	enable_irq(dd->irq);

do the other users of the VDE cause this IRQ to trigger in error? If so, they should be fixed. If not, and the IRQ line is shared by h/w, then all users of the IRQ should request_irq with IRQF_SHARED, and return IRQ_NONE if the IRQ wasn't for them. Either way, the IRQ should be left enabled.

drivers/crypto/tegra-aes.c line 248

Quoted 7 lines
drivers/crypto/tegra-aes.c new file +7 -0
+	value = aes_readl(dd, TEGRA_AES_CMDQUE_CONTROL);
+	/* access SDRAM through AHB */
+	value &= ~TEGRA_AES_CMDQ_CTRL_SRC_STM_SEL_FIELD;
+	value &= ~TEGRA_AES_CMDQ_CTRL_DST_STM_SEL_FIELD;
+	value |= (TEGRA_AES_CMDQ_CTRL_SRC_STM_SEL_FIELD |
+		  TEGRA_AES_CMDQ_CTRL_DST_STM_SEL_FIELD |
+		  TEGRA_AES_CMDQ_CTRL_ICMDQEN_FIELD);

unnecessary parens

drivers/crypto/tegra-aes.c line 293

Quoted 2 lines
drivers/crypto/tegra-aes.c new file +2 -0
+	ret = wait_for_completion_timeout(&dd->op_complete,
+			msecs_to_jiffies(150));

alignment

drivers/crypto/tegra-aes.c line 453

Quoted 8 lines
drivers/crypto/tegra-aes.c new file +8 -0
+	total = dd->total;
+	rctx = ablkcipher_request_ctx(req);
+	ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
+	rctx->mode &= FLAGS_MODE_MASK;
+	dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
+
+	dd->iv = (u8 *)req->info;
+	dd->ivlen = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));

cleaner:

tfm = crypto_ablkcipher_reqtfm(req);
...
ctx = crypto_ablkcipher_ctx(tfm);
...
dd->ivlen = crypto_ablkcipher_ivsize(tfm);

drivers/crypto/tegra-aes.c line 462

Quoted 8 lines
drivers/crypto/tegra-aes.c new file +8 -0
+	/* assign new context to device */
+	ctx->dd = dd;
+	dd->ctx = ctx;
+
+	if (ctx->flags & FLAGS_NEW_KEY) {
+		/* copy the key */
+		memset(dd->ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
+		memcpy(dd->ivkey_base, ctx->key, ctx->keylen);

these really should be writes to mutually exclusive addresses.

drivers/crypto/tegra-aes.c line 501

Quoted 4 lines
drivers/crypto/tegra-aes.c new file +4 -0
+		addr_in = sg_dma_address(in_sg);
+		addr_out = sg_dma_address(out_sg);
+		dd->flags |= FLAGS_FAST;
+		count = min((int)sg_dma_len(in_sg), (int)dma_max);

use min_t

drivers/crypto/tegra-aes.c line 601

Quoted 13 lines
drivers/crypto/tegra-aes.c new file +13 -0
+static irqreturn_t aes_irq(int irq, void *dev_id)
+{
+	struct tegra_aes_dev *dd = (struct tegra_aes_dev *)dev_id;
+	u32 value = aes_readl(dd, TEGRA_AES_INTR_STATUS);
+
+	dev_dbg(dd->dev, "irq_stat: 0x%x", value);
+	if (value & TEGRA_AES_INT_ERROR_MASK)
+		aes_writel(dd, TEGRA_AES_INT_ERROR_MASK, TEGRA_AES_INTR_STATUS);
+
+	if (!(value & TEGRA_AES_ENGINE_BUSY_FIELD))
+		complete(&dd->op_complete);
+
+	return IRQ_HANDLED;

return IRQ_NONE if there was no error and (value & TEGRA_AES_ENGINE_BUSY_FIELD).

drivers/crypto/tegra-aes.c line 679

Quoted 2 lines
drivers/crypto/tegra-aes.c new file +2 -0
+	ret = aes_start_crypt(dd, (u32)dd->dma_buf_in,
+		(u32)dd->dma_buf_out, 1, dd->flags, true);

alignment

drivers/crypto/tegra-aes.c line 703

Quoted 2 lines
drivers/crypto/tegra-aes.c new file +2 -0
+static int tegra_aes_rng_reset(struct crypto_rng *tfm, u8 *seed,
+	unsigned int slen)

alignment

drivers/crypto/tegra-aes.c line 746

Quoted 3 lines
drivers/crypto/tegra-aes.c new file +3 -0
+	/* copy the key to the key slot */
+	memset(dd->ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
+	memcpy(dd->ivkey_base, seed + DEFAULT_RNG_BLK_SZ, AES_KEYSIZE_128);

should be to mutually exclusive addresses

drivers/crypto/tegra-aes.c line 760

Quoted 4 lines
drivers/crypto/tegra-aes.c new file +4 -0
+	/* set seed to the aes hw slot */
+	memcpy(dd->buf_in, dd->iv, DEFAULT_RNG_BLK_SZ);
+	ret = aes_start_crypt(dd, (u32)dd->dma_buf_in,
+			dd->dma_buf_out, 1, FLAGS_CBC, false);

alignment

drivers/crypto/tegra-aes.c line 1041

Quoted 9 lines
drivers/crypto/tegra-aes.c new file +9 -0
+static int __devexit tegra_aes_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct tegra_aes_dev *dd = platform_get_drvdata(pdev);
+	int i;
+
+	if (!dd)
+		return -ENODEV;
+

when would this condition be met?

drivers/crypto/tegra-aes.h line 80

Quoted 2 lines
drivers/crypto/tegra-aes.h new file +2 -0
+/* init vector select */
+#define TEGRA_AES_SECURE_IV_SELECT_SHIFT	(10)

no parens

Kim

A message that is not in the archive (sent off-list or not yet received): 20111105181025.69918e625a169ccb30d9b7c8-KZfg59tc24xl57MIdRCFDg@public.gmane.org

#4 lore raw
Quoted 10 lines

drivers/crypto/tegra-aes.c line 462

Quoted 9 lines
drivers/crypto/tegra-aes.c new file +8 -0
+	/* assign new context to device */
+	ctx->dd = dd;
+	dd->ctx = ctx;
+
+	if (ctx->flags & FLAGS_NEW_KEY) {
+		/* copy the key */
+		memset(dd->ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
+		memcpy(dd->ivkey_base, ctx->key, ctx->keylen);

these really should be writes to mutually exclusive addresses.

Why?

Quoted 4 lines

drivers/crypto/tegra-aes.c line 746

Quoted 3 lines
drivers/crypto/tegra-aes.c new file +3 -0
+	/* copy the key to the key slot */
+	memset(dd->ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
+	memcpy(dd->ivkey_base, seed + DEFAULT_RNG_BLK_SZ, AES_KEYSIZE_128);

should be to mutually exclusive addresses

Why?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

Varun Wadekar wrote at Saturday, November 12, 2011 3:23 AM:

Quoted 11 lines
Quoted 9 lines

drivers/crypto/tegra-aes.c line 462

Quoted 8 lines
drivers/crypto/tegra-aes.c new file +8 -0
+	/* assign new context to device */
+	ctx->dd = dd;
+	dd->ctx = ctx;
+
+	if (ctx->flags & FLAGS_NEW_KEY) {
+		/* copy the key */
+		memset(dd->ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
+		memcpy(dd->ivkey_base, ctx->key, ctx->keylen);

these really should be writes to mutually exclusive addresses.

Why?

To avoid redundant work; there's little point memset()ing a region that's going to be copied over the top of immediately afterwards.

Signature · 1 line
nvpublic
#7 lore raw
Quoted 11 lines · On Mon, 14 Nov 2011 09:26:16 -0800 Stephen Warren <[email protected]> wrote:

Varun Wadekar wrote at Saturday, November 12, 2011 3:23 AM:

Quoted 10 lines
Quoted 9 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+	/* assign new context to device */
+	ctx->dd = dd;
+	dd->ctx = ctx;
+
+	if (ctx->flags & FLAGS_NEW_KEY) {
+		/* copy the key */
+		memset(dd->ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
+		memcpy(dd->ivkey_base, ctx->key, ctx->keylen);

these really should be writes to mutually exclusive addresses.

Quoted 4 lines
Quoted 1 line

Why?

To avoid redundant work; there's little point memset()ing a region that's going to be copied over the top of immediately afterwards.

plus, why is the memset needed at all?

Kim

#8 lore raw
In reply to Stephen Warren
Quoted 5 lines
Quoted 2 lines

Why?

To avoid redundant work; there's little point memset()ing a region that's going to be copied over the top of immediately afterwards.

The length used for memset is different from the length being copied over. I am initially memsetting the entire key struct (which contains the key + original IV + updated IV) and then copying only the key. Down the line we copy the original IV and/or the updated IV in this memory space.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw

Varun Wadekar wrote at Monday, November 14, 2011 9:11 PM:

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Why?

To avoid redundant work; there's little point memset()ing a region that's going to be copied over the top of immediately afterwards.

The length used for memset is different from the length being copied over. I am initially memsetting the entire key struct (which contains the key + original IV + updated IV) and then copying only the key. Down the line we copy the original IV and/or the updated IV in this memory space.

That doesn't make the duplicate memset/copy cease to be redundant.

Why not copy the key to where it goes, then memset the rest of the data; wouldn't that be as simple as:

memcpy(dd->ivkey_base, ctx->key, ctx->keylen);
memset(dd->ivkey_base + ctx->keylen, 0, AES_HW_KEY_TABLE_LENGTH_BYTES - ctx->keylen);

Related to this, I wonder why:

/*
* The key table length is 64 bytes
* (This includes first upto 32 bytes key + 16 bytes original initial vector
* and 16 bytes updated initial vector)
*/
#define AES_HW_KEY_TABLE_LENGTH_BYTES 64

Yet:

dd->ivkey_base = dma_alloc_coherent(dev, SZ_512, &dd->ivkey_phys_base, ...

Why is SZ_512 used to during alloc instead of AES_HW_KEY_TABLE_LENGTH_BYTES?

--
nvpublic

A message that is not in the archive (sent off-list or not yet received): 74CDBE0F657A3D45AFBB94109FB122FF1740805D40-C7FfzLzN0UxDw2glCA4ptUEOCMrvLtNR@public.gmane.org

#12 lore raw
Quoted 8 lines

That doesn't make the duplicate memset/copy cease to be redundant.

Why not copy the key to where it goes, then memset the rest of the data; wouldn't that be as simple as:

memcpy(dd->ivkey_base, ctx->key, ctx->keylen);
memset(dd->ivkey_base + ctx->keylen, 0, AES_HW_KEY_TABLE_LENGTH_BYTES - ctx->keylen);

Seems like the same thing to me. Kim is inclined towards removing the memset completely, which I think should not be done. We need the memset to clear the entire key table.

Quoted 9 lines

Related to this, I wonder why:

/*
* The key table length is 64 bytes
* (This includes first upto 32 bytes key + 16 bytes original initial vector
* and 16 bytes updated initial vector)
*/
#define AES_HW_KEY_TABLE_LENGTH_BYTES 64

Quoted 5 lines

Yet:

dd->ivkey_base = dma_alloc_coherent(dev, SZ_512, &dd->ivkey_phys_base, ...

Why is SZ_512 used to during alloc instead of AES_HW_KEY_TABLE_LENGTH_BYTES?

Correct. Will fix this.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw
Quoted 9 lines · On Wed, 16 Nov 2011 12:44:50 +0530 Varun Wadekar…
Quoted 7 lines

That doesn't make the duplicate memset/copy cease to be redundant.

Why not copy the key to where it goes, then memset the rest of the data; wouldn't that be as simple as:

memcpy(dd->ivkey_base, ctx->key, ctx->keylen);
memset(dd->ivkey_base + ctx->keylen, 0, AES_HW_KEY_TABLE_LENGTH_BYTES - ctx->keylen);

Seems like the same thing to me.

it's not - it saves writes.

Quoted 3 lines

Kim is inclined towards removing the
memset completely, which I think should not be done. We need the memset to clear the entire key table.

why do you need to clear the entire key table if it will be overwritten anyway?

Kim

A message that is not in the archive (sent off-list or not yet received): 20111116025720.27e6b710e8b333612862bb2e-KZfg59tc24xl57MIdRCFDg@public.gmane.org

#16 lore raw
Quoted 2 lines

it's not - it saves writes.

Are you ok with this solution? Either way I wan to start with a clear key table before programming the hardware.

Quoted 2 lines

why do you need to clear the entire key table if it will be overwritten anyway?

If you set a > 128-bit key and then set a 128-bit key, the remaining bits still remain in the key table. Similarly, if we use updated IV in one operation and want to use the initial IV for the next, the updated IV will still remain in the key table. The entire key table is copied to the AES engine. Even though, we program the engine with the exact number of bits to use for the key and whether to use the updated/initial IV, I feel its better if the unused bits are zero instead of having garbage.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#18 lore raw
Quoted 5 lines · On Wed, 16 Nov 2011 14:35:00 +0530 Varun Wadekar…
Quoted 2 lines

it's not - it saves writes.

Are you ok with this solution? Either way I wan to start with a clear key table before programming the hardware.

the hardware doesn't care.

Quoted 10 lines
Quoted 2 lines

why do you need to clear the entire key table if it will be overwritten anyway?

If you set a > 128-bit key and then set a 128-bit key, the remaining bits still remain in the key table. Similarly, if we use updated IV in one operation and want to use the initial IV for the next, the updated IV will still remain in the key table. The entire key table is copied to the AES engine. Even though, we program the engine with the exact number of bits to use for the key and whether to use the updated/initial IV, I feel its better if the unused bits are zero instead of having garbage.

you're losing free performance.

Kim

A message that is not in the archive (sent off-list or not yet received): 20111116031703.e006c81ebf5368365e7a22fb-KZfg59tc24xl57MIdRCFDg@public.gmane.org

#20 lore raw
Quoted 11 lines
Quoted 10 lines
Quoted 3 lines

why do you need to clear the entire key table if it will be overwritten anyway?

If you set a > 128-bit key and then set a 128-bit key, the remaining bits still remain in the key table. Similarly, if we use updated IV in one operation and want to use the initial IV for the next, the updated IV will still remain in the key table. The entire key table is copied to the AES engine. Even though, we program the engine with the exact number of bits to use for the key and whether to use the updated/initial IV, I feel its better if the unused bits are zero instead of having garbage.

you're losing free performance.

I am really not comfortable having garbage in the keytable. Can you suggest another way to ensure this?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#22 lore raw
Quoted 3 lines
Quoted 2 lines

you're losing free performance.

I am really not comfortable having garbage in the keytable.

Kim, do you have any other solution in mind?