[U-Boot] [PATCH v3] imx: imx7 Support for Manufacturing Protection

Ulises Cardenas raul.casas at nxp.com
Thu Feb 11 14:50:05 CET 2016


i.MX7 has an a protection feature for Manufacturing process.
This feature uses assymetric encryption to sign and verify
authenticated software handled between parties. This command
enables the use of such feature.

The private key is unique and generated once per device.
And it is stored in secure memory and only accessible by CAAM.
Therefore, the public key generation and signature functions
are the only functions available for the user.

Command usage:

Print the public key for the device.
- mfgprot pubk

Generates Signature over given data.
- mfgprot sign <data_address> <data_size> <dgst_addr> <c_addr> <d_addr>

Signed-off-by: Raul Ulises Cardenas <raul.casas at nxp.com>
---

Changes for v2:
	- removed genenerate_mppubk and generate_mpsign
	due to redundancy

Changes for v3:
	- typo correction description
	- extended mfgprot command from an MPSing function example to a fully
	functional command, providing the ability of signing given data

 arch/arm/imx-common/Makefile      |   1 +
 arch/arm/imx-common/cmd_mfgprot.c | 102 ++++++++++++++++
 drivers/crypto/fsl/Makefile       |   4 +
 drivers/crypto/fsl/fsl_mfgprot.c  | 236 ++++++++++++++++++++++++++++++++++++++
 include/fsl_sec.h                 |  45 +++++++-
 5 files changed, 387 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm/imx-common/cmd_mfgprot.c
 create mode 100644 drivers/crypto/fsl/fsl_mfgprot.c

diff --git a/arch/arm/imx-common/Makefile b/arch/arm/imx-common/Makefile
index e7190c3..8563150 100644
--- a/arch/arm/imx-common/Makefile
+++ b/arch/arm/imx-common/Makefile
@@ -22,6 +22,7 @@ ifeq ($(SOC),$(filter $(SOC),mx7))
 obj-y 	+= cpu.o
 obj-$(CONFIG_SYS_I2C_MXC) += i2c-mxv7.o
 obj-$(CONFIG_SYSCOUNTER_TIMER) += syscounter.o
+obj-$(CONFIG_CMD_MFGPROT) += cmd_mfgprot.o
 endif
 ifeq ($(SOC),$(filter $(SOC),mx6 mx7))
 obj-y 	+= cache.o init.o
diff --git a/arch/arm/imx-common/cmd_mfgprot.c b/arch/arm/imx-common/cmd_mfgprot.c
new file mode 100644
index 0000000..3a6e7cb
--- /dev/null
+++ b/arch/arm/imx-common/cmd_mfgprot.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2016 NXP Semiconductors.
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * Command for manufacturing protection
+ */
+
+#include <common.h>
+#include <command.h>
+#include <environment.h>
+#include <malloc.h>
+#include <asm/byteorder.h>
+#include <linux/compiler.h>
+#include <fsl_sec.h>
+#include <asm/arch/clock.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/**
+ * do_mfgprot() - Handle the "mfgprogt" command-line command
+ * @cmdtp:  Command data struct pointer
+ * @flag:   Command flag
+ * @argc:   Command-line argument count
+ * @argv:   Array of command-line arguments
+ *
+ * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
+ * on error.
+ */
+static int do_mfgprot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+{
+	uint32_t m_addr, dgst_addr, c_addr, d_addr;
+	u8 *m_ptr, *dgst_ptr, *c_ptr, *d_ptr;
+	int m_size;
+	int ret;
+	char *pubk, *sign, *sel;
+
+	pubk = "pubk";
+	sign = "sign";
+	sel = argv[1];
+
+	ret = 0;
+
+	/* Enable HAB clock */
+	u32 jr_size = 4;
+	u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR + 0x102c);
+	if (out_jr_size != jr_size) {
+		hab_caam_clock_enable(1);
+		sec_init();
+	}
+
+	if (strcmp(sel, pubk) == 0) {
+		ret = gen_mppubk();
+	} else if (strcmp(sel, sign) == 0) {
+		if (argc != 7)
+			return CMD_RET_USAGE;
+
+		m_addr = simple_strtoul(argv[2], NULL, 16);
+		m_size = simple_strtoul(argv[3], NULL, 10);
+		dgst_addr = simple_strtoul(argv[4], NULL, 16);
+		c_addr = simple_strtoul(argv[5], NULL, 16);
+		d_addr = simple_strtoul(argv[6], NULL, 16);
+
+
+		m_ptr = map_physmem(m_addr, m_size, MAP_NOCACHE);
+		dgst_ptr = map_physmem(dgst_addr, HAB_MES_RESP_DGST_BYTES,
+				MAP_NOCACHE);
+		c_ptr = map_physmem(c_addr, HAB_ENG_MFG_PRVK_BYTES,
+				MAP_NOCACHE);
+		d_ptr = map_physmem(d_addr, HAB_ENG_MFG_PRVK_BYTES,
+				MAP_NOCACHE);
+
+		memset(dgst_ptr, 0, HAB_MES_RESP_DGST_BYTES);
+		memset(c_ptr, 0, HAB_ENG_MFG_PRVK_BYTES);
+		memset(d_ptr, 0, HAB_ENG_MFG_PRVK_BYTES);
+
+		ret = sign_mppubk(m_ptr, m_size, dgst_ptr, c_ptr, d_ptr);
+	} else {
+		return CMD_RET_USAGE;
+	}
+
+	unmap_sysmem(m_ptr);
+	unmap_sysmem(dgst_ptr);
+	unmap_sysmem(c_ptr);
+	unmap_sysmem(d_ptr);
+
+	return ret;
+}
+
+/***************************************************/
+static char mfgprot_help_text[] =
+	"Usage:\n"
+	 "Print the public key for Manufacturing Protection\n"
+	 "\tmfgprot pubk\n"
+	 "Generates a Manufacturing Protection signature\n"
+	 "\tmfgprot sign <data_addr> <size> <dgst_addr> <c_addr> <d_addr>";
+
+U_BOOT_CMD(
+	mfgprot, 7, 1, do_mfgprot,
+	"Manufacturing Protection\n",
+	mfgprot_help_text
+);
diff --git a/drivers/crypto/fsl/Makefile b/drivers/crypto/fsl/Makefile
index fd736cf..6d6903b 100644
--- a/drivers/crypto/fsl/Makefile
+++ b/drivers/crypto/fsl/Makefile
@@ -8,3 +8,7 @@ obj-y += sec.o
 obj-$(CONFIG_FSL_CAAM) += jr.o fsl_hash.o jobdesc.o error.o
 obj-$(CONFIG_CMD_BLOB)$(CONFIG_CMD_DEKBLOB) += fsl_blob.o
 obj-$(CONFIG_RSA_FREESCALE_EXP) += fsl_rsa.o
+
+ifdef CONFIG_MX7
+obj-$(CONFIG_CMD_MFGPROT) += fsl_mfgprot.o
+endif
diff --git a/drivers/crypto/fsl/fsl_mfgprot.c b/drivers/crypto/fsl/fsl_mfgprot.c
new file mode 100644
index 0000000..fe89be7
--- /dev/null
+++ b/drivers/crypto/fsl/fsl_mfgprot.c
@@ -0,0 +1,236 @@
+/*
+ * Copyright 2014 Freescale Semiconductor, Inc.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ *
+ */
+
+#include <common.h>
+#include <malloc.h>
+#include <fsl_sec.h>
+#include <asm-generic/errno.h>
+#include "jobdesc.h"
+#include "desc.h"
+#include "jr.h"
+
+#define HAB_MASK(LBL)	\
+	((((uint32_t)1 << (LBL##_WIDTH)) - 1) << (LBL##_SHIFT))
+
+#define HAB_INSERT_BITS(val, LBL)	\
+	(((uint32_t)(val) << LBL##_SHIFT) & HAB_MASK(LBL))
+
+/* Size of MFG descriptor */
+#define MFG_PUBK_DSC_WORDS 4
+#define MFG_SIGN_DSC_WORDS 8
+
+/* Size of MFG protocol data block */
+#define MFG_PUBK_PDB_WORDS 2
+#define MFG_SIGN_PDB_WORDS 6
+
+void mfg_build_sign_dsc(u32 *dsc_ptr, const u8 *m, int size,
+		u8 *dgst, u8 *c, u8 *d)
+{
+	u32 *dsc = dsc_ptr;
+	*dsc++ = (HAB_ENG_CAAM_CMD_JOBHDR
+		  | HAB_INSERT_BITS(
+				MFG_SIGN_PDB_WORDS + 1,
+				HAB_ENG_CAAM_CMD_JOBHDR_START)
+		  | MFG_SIGN_DSC_WORDS);
+
+	/*** MFG PubK PDB ***/
+	/* Curve */
+	*dsc++ = HAB_INSERT_BITS(HAB_ENG_CAAM_MPPUBK_CSEL_P256,
+				 HAB_ENG_CAAM_MPPUBK_CSEL);
+	/* Message Pointer */
+	*dsc++ = (dma_addr_t)m;
+
+	/* mes-resp Pointer */
+	*dsc++ = (dma_addr_t)dgst;
+
+	/* C Pointer */
+	*dsc++ = (dma_addr_t)c;
+
+	/* d Pointer */
+	*dsc++ = (dma_addr_t)d;
+
+	/* Message Size */
+	*dsc++ = size;
+
+	/* MP PubK generate key command */
+	*dsc = HAB_ENG_CAAM_CMD_PROTOCOL_MPSIGN;
+}
+
+void mfg_build_pubk_dsc(u32 *dsc_ptr, u8 *dst)
+{
+	u32 *dsc = dsc_ptr;
+	*dsc++ = (HAB_ENG_CAAM_CMD_JOBHDR
+		 | HAB_INSERT_BITS(
+			MFG_PUBK_PDB_WORDS + 1,
+			HAB_ENG_CAAM_CMD_JOBHDR_START)
+		 | MFG_PUBK_DSC_WORDS);
+
+	/*** MFG PubK PDB ***/
+	/* Curve */
+	*dsc++ = HAB_INSERT_BITS(HAB_ENG_CAAM_MPPUBK_CSEL_P256,
+				 HAB_ENG_CAAM_MPPUBK_CSEL);
+	/* Message Pointer */
+	*dsc++ = (dma_addr_t)dst;
+
+	/* MP PubK generate key command */
+	*dsc = HAB_ENG_CAAM_CMD_PROTOCOL_MPPUBK;
+}
+
+int gen_mppubk(void)
+{
+	int ret;
+	int size, i;
+
+	u32 *dsc;
+	uint32_t dst_addr;
+	u8 *dst;
+
+	/****************************************************************
+	 * Allocation & Initialization
+	 ****************************************************************/
+	ret = 0;
+
+	/* Memory addresses for output */
+	dst_addr = 0x80100000;
+	dst = map_physmem(dst_addr, HAB_ENG_MFG_PUBK_BYTES, MAP_NOCACHE);
+	memset(dst, 0, HAB_ENG_MFG_PUBK_BYTES);
+
+	/****************************************************************
+	 * Job Descriptor initialization
+	 ****************************************************************/
+	dsc = memalign(ARCH_DMA_MINALIGN,
+			sizeof(uint32_t) * MFG_PUBK_DSC_WORDS);
+	if (!dsc) {
+		debug("Not enough memory for descriptor allocation\n");
+		return -ENOMEM;
+	}
+
+	mfg_build_pubk_dsc(dsc, dst);
+
+	size = roundup(sizeof(uint32_t) * MFG_PUBK_DSC_WORDS,
+			ARCH_DMA_MINALIGN);
+	flush_dcache_range((unsigned long)dsc, (unsigned long)dsc + size);
+
+	size = roundup(HAB_ENG_MFG_PUBK_BYTES, ARCH_DMA_MINALIGN);
+	flush_dcache_range((unsigned long)dst, (unsigned long)dst + size);
+
+	/****************************************************************
+	 * Execute Job Descriptor
+	 ****************************************************************/
+	puts("\nGenerating Manufacturing Protection Public Key\n");
+
+	ret = run_descriptor_jr(dsc);
+	if (ret) {
+		debug("Error in public key generation %d\n", ret);
+	   goto err;
+	}
+
+	size = roundup(HAB_ENG_MFG_PUBK_BYTES, ARCH_DMA_MINALIGN);
+	invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
+
+	/****************************************************************
+	 * Output results
+	 ****************************************************************/
+	puts("Public key:\n");
+	for (i = 0; i < HAB_ENG_MFG_PUBK_BYTES; i++)
+		printf("%02X", ((u8 *)dst)[i]);
+	printf("\n");
+
+err:
+	free(dsc);
+	unmap_sysmem(dst);
+	return ret;
+}
+
+int sign_mppubk(const u8 *m, int data_size, u8 *dgst, u8 *c, u8 *d)
+{
+	int ret;
+	int size, i;
+
+	u32 *dsc;
+
+	/****************************************************************
+	 * Allocation & Initialization
+	 ****************************************************************/
+	ret = 0;
+
+	memset(dgst, 0, HAB_MES_RESP_DGST_BYTES);
+	memset(d, 0, HAB_ENG_MFG_PRVK_BYTES);
+	memset(c, 0, HAB_ENG_MFG_PRVK_BYTES);
+
+	/****************************************************************
+	 * Job Descriptor initialization
+	 ****************************************************************/
+	dsc = memalign(ARCH_DMA_MINALIGN,
+			sizeof(uint32_t) * MFG_SIGN_DSC_WORDS);
+	if (!dsc) {
+		debug("Not enough memory for descriptor allocation\n");
+		return -ENOMEM;
+	}
+
+	mfg_build_sign_dsc(dsc, m, data_size, dgst, c, d);
+
+	size = roundup(sizeof(uint32_t) * MFG_SIGN_DSC_WORDS,
+			ARCH_DMA_MINALIGN);
+	flush_dcache_range((unsigned long)dsc, (unsigned long)dsc + size);
+
+	size = roundup(data_size, ARCH_DMA_MINALIGN);
+	flush_dcache_range((unsigned long)m, (unsigned long)m + size);
+
+	size = roundup(HAB_MES_RESP_DGST_BYTES, ARCH_DMA_MINALIGN);
+	flush_dcache_range((unsigned long)dgst, (unsigned long)dgst + size);
+
+	size = roundup(HAB_ENG_MFG_PRVK_BYTES, ARCH_DMA_MINALIGN);
+	flush_dcache_range((unsigned long)c, (unsigned long)c + size);
+	flush_dcache_range((unsigned long)d, (unsigned long)d + size);
+
+	/****************************************************************
+	 * Execute Job Descriptor
+	 ****************************************************************/
+	puts("\nSiging message with Manufacturing Protection Public Key\n");
+
+	ret = run_descriptor_jr(dsc);
+	if (ret) {
+		debug("Error in public key generation %d\n", ret);
+	   goto err;
+	}
+
+	size = roundup(HAB_MES_RESP_DGST_BYTES, ARCH_DMA_MINALIGN);
+	invalidate_dcache_range((unsigned long)dgst, (unsigned long)dgst+size);
+
+	size = roundup(HAB_ENG_MFG_PRVK_BYTES, ARCH_DMA_MINALIGN);
+	invalidate_dcache_range((unsigned long)c, (unsigned long)c+size);
+	invalidate_dcache_range((unsigned long)d, (unsigned long)d+size);
+
+	/****************************************************************
+	 * Output results
+	 ****************************************************************/
+	puts("Message: ");
+	for (i = 0; i < data_size; i++)
+		printf("%02X ", ((uint8_t *)m)[i]);
+	printf("\n");
+
+	puts("Message Representative Digest(SHA-256):\n");
+	for (i = 0; i < HAB_MES_RESP_DGST_BYTES; i++)
+		printf("%02X", ((uint8_t *)dgst)[i]);
+	printf("\n");
+
+	puts("Signature:\n");
+	puts("D:\n");
+	for (i = 0; i < HAB_ENG_MFG_PRVK_BYTES; i++)
+		printf("%02X", ((uint8_t *)c)[i]);
+	printf("\n");
+
+	puts("d:\n");
+	for (i = 0; i < HAB_ENG_MFG_PRVK_BYTES; i++)
+		printf("%02X", ((uint8_t *)d)[i]);
+	printf("\n");
+
+err:
+	free(dsc);
+	return ret;
+}
diff --git a/include/fsl_sec.h b/include/fsl_sec.h
index 2ddced3..6fe02e9 100644
--- a/include/fsl_sec.h
+++ b/include/fsl_sec.h
@@ -270,9 +270,33 @@ struct sg_entry {
 
 #define ERROR_IN_PAGE_ALLOC	1
 #define ECONSTRJDESC   -1
-
 #endif
 
+#ifdef CONFIG_MX7
+/* Security level index in #mfg_curve */
+#define MFG_SEC_LVL_256 0
+#define MFG_SEC_LVL_384 1
+#define MFG_SEC_LVL_521 2
+
+/* Job Descriptor Header command - add length in words */
+#define HAB_ENG_CAAM_CMD_JOBHDR                 0xB0800000UL
+#define HAB_ENG_CAAM_CMD_JOBHDR_START_SHIFT     16 /**< START INDEX field */
+#define HAB_ENG_CAAM_CMD_JOBHDR_START_WIDTH     6 /**< START INDEX field */
+#define HAB_ENG_CAAM_MPPUBK_SGF         (1UL<<31)       /**< Message SG flag */
+#define HAB_ENG_CAAM_MPPUBK_CSEL_SHIFT  17              /**< Curve selection */
+#define HAB_ENG_CAAM_MPPUBK_CSEL_WIDTH  4               /**< Curve selection */
+#define HAB_ENG_CAAM_MPPUBK_CSEL_P256   0x3             /**< P-256 */
+#define HAB_ENG_CAAM_MPPUBK_CSEL_P384   0x4             /**< P-384 */
+#define HAB_ENG_CAAM_MPPUBK_CSEL_P521   0x5             /**< P-521 */
+
+/** PROTOCOL MPPubKGen command */
+#define HAB_ENG_CAAM_CMD_PROTOCOL_MPPUBK        0x86140000UL
+#define HAB_ENG_CAAM_CMD_PROTOCOL_MPSIGN        0x86150000UL
+
+#define HAB_ENG_MFG_PUBK_BYTES  64
+#define HAB_ENG_MFG_PRVK_BYTES  32
+#define HAB_MES_RESP_DGST_BYTES 32
+#endif
 int sec_init(void);
 
 /* blob_dek:
@@ -284,6 +308,25 @@ int sec_init(void);
  */
 int blob_dek(const u8 *src, u8 *dst, u8 len);
 
+/*
+ * gen_mpubk:
+ * Generates example of public key for manufacturing protection
+ */
+int gen_mppubk(void);
+
+/*
+ * sign_mpubk:
+ * Generates a manufacturing protection signature of the given
+ * data.
+ * @m: reference to data be signed
+ * @data_size: size of data to be signed
+ * @dgst_addr: address for the digest to be stored
+ * @c_addr: address for c to be stored
+ * @d_addr: address for d to be stored
+ * @return: 0 on success, error otherwise
+ *
+ */
+int sign_mppubk(const u8 *m, int data_size, u8 *dgst, u8 *c, u8 *d);
 #endif
 
 #endif /* __FSL_SEC_H */
-- 
2.6.2



More information about the U-Boot mailing list