* [cbootimage PATCH v7 1/5] Add support for update pubkey and rsa-pss signatures
[not found] ` <1445295718-19146-1-git-send-email-jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
@ 2015-10-19 23:01 ` Jimmy Zhang
2015-10-19 23:01 ` [cbootimage PATCH v7 2/5] Add support to dump rsa related fields for t210 Jimmy Zhang
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Jimmy Zhang @ 2015-10-19 23:01 UTC (permalink / raw)
To: amartin-DDmLM1+adcrQT0dZR+AlfA, swarren-DDmLM1+adcrQT0dZR+AlfA
Cc: linux-tegra-u79uwXL29TY76Z2rM5mHXA, Jimmy Zhang
Create new configuration keywords:
RsaKeyModulusFile: pubkey modulus
RsaPssSigBlFile: bootloader rsa pss signature
RsaPssSigBctFile: bct rsa pss signature
Sample Configuration file update_bl_sig.cfg
RsaKeyModulusFile = pubkey.mod;
RsaPssSigBlFile = bl.sig;
where pubkey.mod and bl.sig are files that contain the public key
modulus and bootloader's rsa-pss signature respectively.
public key modulus and signature are created through utilities
outside cbootimage.
Command line example:
$ cbootimage -s tegra210 -u update_bl_sig.cfg image.bin image.bin-bl-signed
Above three new keywords added in this CL are only implemented to support
for T210.
Changes in V7:
1) Redefine parameter "u_int8_t *in" as "const u_int8_t *in" for function
reverse_byte_order()
2) Clean up compiler warnings from nvbctlib_t210.c
Changes in V6:
1) Rename function swap_endianness() to reverse_byte_order()
2) Put "size - 1 - i" to a variable to avoid double calculation
3) Remove checking NULL pointer of get_value_size() in function set_rsa_param()
4) Change function prototype for get_value_size()
Signed-off-by: Jimmy Zhang <jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
---
src/cbootimage.h | 1 +
src/crypto.c | 29 +++++++++++++++++++++++++++++
src/crypto.h | 5 +++++
src/parse.c | 40 ++++++++++++++++++++++++++++++++++++++++
src/parse.h | 17 +++++++++++++++++
src/set.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
src/set.h | 5 +++++
src/t114/nvbctlib_t114.c | 1 +
src/t124/nvbctlib_t124.c | 1 +
src/t210/nvbctlib_t210.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
10 files changed, 190 insertions(+), 1 deletion(-)
diff --git a/src/cbootimage.h b/src/cbootimage.h
index 9706b2c1edb8..63f0ee97e12e 100644
--- a/src/cbootimage.h
+++ b/src/cbootimage.h
@@ -60,6 +60,7 @@ typedef enum
file_type_bl = 0,
file_type_bct,
file_type_mts,
+ file_type_bin,
} file_type;
/*
diff --git a/src/crypto.c b/src/crypto.c
index 99e9f085763c..039be0a8a611 100644
--- a/src/crypto.c
+++ b/src/crypto.c
@@ -297,3 +297,32 @@ sign_bct(build_image_context *context,
free(hash_buffer);
return e;
}
+
+/*
+ * reverse_byte_order
+ *
+ * Reverse the order of bytes pointed by 'in' and place the results
+ * to location pointed by 'out'. If 'out' is the same as 'in', then
+ * order of bytes pointed by 'in' is reversed.
+ */
+void
+reverse_byte_order(
+ u_int8_t *out,
+ const u_int8_t *in,
+ const u_int32_t size)
+{
+ u_int32_t i, j;
+ u_int8_t b1, b2;
+
+ for (i = 0; i < size / 2; i++) {
+ j = size - 1 - i;
+ b1 = in[i];
+ b2 = in[j];
+ out[i] = b2;
+ out[j] = b1;
+ }
+
+ /* In case odd number of bytes */
+ if (size % 2)
+ out[size / 2] = in[size / 2];
+}
diff --git a/src/crypto.h b/src/crypto.h
index d7151e0cd191..f56b67d983f3 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -44,4 +44,9 @@ sign_data_block(u_int8_t *source,
u_int32_t length,
u_int8_t *signature);
+void
+reverse_byte_order(
+ u_int8_t *out,
+ const u_int8_t *in,
+ const u_int32_t size);
#endif /* #ifndef INCLUDED_CRYPTO_H */
diff --git a/src/parse.c b/src/parse.c
index 8c9824437393..667895c4dd54 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -65,6 +65,8 @@ parse_bootloader(build_image_context *context, parse_token token, char *rest);
static int
parse_mts_image(build_image_context *context, parse_token token, char *rest);
static int
+parse_rsa_param(build_image_context *context, parse_token token, char *rest);
+static int
parse_value_u32(build_image_context *context, parse_token token, char *rest);
static int
parse_value_chipuid(build_image_context *context,
@@ -116,6 +118,9 @@ static parse_item s_top_level_items[] = {
{ "ChipUid=", token_unique_chip_id, parse_value_chipuid },
{ "JtagCtrl=", token_secure_jtag_control, parse_value_u32 },
{ "DebugCtrl=", token_secure_debug_control, parse_value_u32 },
+ { "RsaKeyModulusFile=", token_rsa_key_modulus, parse_rsa_param },
+ { "RsaPssSigBlFile=", token_rsa_pss_sig_bl, parse_rsa_param },
+ { "RsaPssSigBctFile=", token_rsa_pss_sig_bct, parse_rsa_param },
{ NULL, 0, NULL } /* Must be last */
};
@@ -480,6 +485,36 @@ static int parse_mts_image(build_image_context *context,
}
/*
+ * Parse the given rsa modulus/key/signature file name
+ * then call set_rsa_settings to set proper rsa field.
+ *
+ * @param context The main context pointer
+ * @param token The parse token value
+ * @param rest String to parse
+ * @return 0 and 1 for success and failure
+ */
+static int parse_rsa_param(build_image_context *context,
+ parse_token token,
+ char *rest)
+{
+ char filename[MAX_BUFFER];
+
+ assert(context != NULL);
+ assert(rest != NULL);
+
+ if (context->generate_bct != 0)
+ return 0;
+
+ /* Parse the file name. */
+ rest = parse_filename(rest, filename, MAX_BUFFER);
+ if (rest == NULL)
+ return 1;
+
+ /* Parsing has finished - set the bootloader */
+ return set_rsa_param(context, token, filename);
+}
+
+/*
* Parse the given string and find the array items in config file.
*
* @param context The main context pointer
@@ -939,3 +974,8 @@ void process_config_file(build_image_context *context, u_int8_t simple_parse)
printf("Error parsing: %s\n", buffer);
exit(1);
}
+
+int bct_get_unsupported(parse_token id)
+{
+ return -ENODATA;
+}
diff --git a/src/parse.h b/src/parse.h
index ce3f21fb8a31..f2e28b306709 100644
--- a/src/parse.h
+++ b/src/parse.h
@@ -114,6 +114,10 @@ typedef enum
token_secure_jtag_control,
token_secure_debug_control,
+ token_rsa_key_modulus,
+ token_rsa_pss_sig_bl,
+ token_rsa_pss_sig_bct,
+
token_nand_clock_divider,
token_nand_nand_timing,
token_nand_nand_timing2,
@@ -1109,6 +1113,14 @@ typedef struct cbootimage_soc_config_rec {
void *data,
u_int8_t *bct);
/*
+ * Get the size of specified bct field
+ *
+ * @param id The parse token
+ * @return size or 0/-ENODATA for failure
+ */
+ int (*get_value_size)(parse_token id);
+
+ /*
* Set the bct crypto hash data.
*
* @param id The parse token value
@@ -1339,6 +1351,11 @@ u_int32_t ceil_log2(u_int32_t a);
extern cbootimage_soc_config *g_soc_config;
/*
+ * Dummy function for unsupported token
+ */
+int bct_get_unsupported(parse_token id);
+
+/*
* Provide access to enum and field tables. These tables are useful when
* pretty printing a BCT file using bct_dump.
*/
diff --git a/src/set.c b/src/set.c
index 73af52111360..388bc1acb5c4 100644
--- a/src/set.c
+++ b/src/set.c
@@ -147,6 +147,50 @@ set_mts_image(build_image_context *context,
context->mts_entry_point = entry_point;
return update_mts_image(context);
}
+
+int
+set_rsa_param(build_image_context *context, parse_token token,
+ char *filename)
+{
+ int result;
+ u_int8_t *rsa_storage; /* Holds the rsa param after reading */
+ int32_t size; /* Bytes to read */
+ u_int32_t actual_size; /* In bytes */
+
+ if ((size = g_soc_config->get_value_size(token)) <= 0) {
+ printf("Error: Unsupported token %d for value size.\n", token);
+ exit(1);
+ }
+
+ /* Read the image into memory. */
+ result = read_from_image(filename,
+ 0,
+ (u_int32_t)size,
+ &rsa_storage,
+ &actual_size,
+ file_type_bin);
+
+ if (result) {
+ printf("Error reading file %s.\n", filename);
+ exit(1);
+ }
+
+ if (actual_size != size) {
+ printf("Error: invalid size, file %s.\n", filename);
+ exit(1);
+ }
+
+ if (enable_debug)
+ printf("Updating token %d with file %s\n", (int)token, filename);
+
+ /* set to appropriate bct field */
+ result = g_soc_config->set_value(token,
+ rsa_storage, context->bct);
+
+ free(rsa_storage);
+ return result;
+}
+
#define DEFAULT() \
default: \
printf("Unexpected token %d at line %d\n", \
diff --git a/src/set.h b/src/set.h
index 8b9a69b2a950..b38d4cefcb4f 100644
--- a/src/set.h
+++ b/src/set.h
@@ -42,6 +42,11 @@ set_mts_image(build_image_context *context,
u_int32_t entry_point);
int
+set_rsa_param(build_image_context *context,
+ parse_token token,
+ char *filename);
+
+int
context_set_value(build_image_context *context,
parse_token token,
void *value);
diff --git a/src/t114/nvbctlib_t114.c b/src/t114/nvbctlib_t114.c
index dad8f4f8f07d..9e764fb547ad 100644
--- a/src/t114/nvbctlib_t114.c
+++ b/src/t114/nvbctlib_t114.c
@@ -1112,6 +1112,7 @@ cbootimage_soc_config tegra114_config = {
.getbl_param = t114_getbl_param,
.set_value = t114_bct_set_value,
.get_value = t114_bct_get_value,
+ .get_value_size = bct_get_unsupported,
.set_data = t114_bct_set_data,
.get_bct_size = t114_get_bct_size,
.token_supported = t114_bct_token_supported,
diff --git a/src/t124/nvbctlib_t124.c b/src/t124/nvbctlib_t124.c
index 5df93cdcdb91..5b760ad0eeec 100644
--- a/src/t124/nvbctlib_t124.c
+++ b/src/t124/nvbctlib_t124.c
@@ -1125,6 +1125,7 @@ cbootimage_soc_config tegra124_config = {
.getbl_param = t124_getbl_param,
.set_value = t124_bct_set_value,
.get_value = t124_bct_get_value,
+ .get_value_size = bct_get_unsupported,
.set_data = t124_bct_set_data,
.get_bct_size = t124_get_bct_size,
.token_supported = t124_bct_token_supported,
diff --git a/src/t210/nvbctlib_t210.c b/src/t210/nvbctlib_t210.c
index 9921bbbe0d2d..3380411c131c 100644
--- a/src/t210/nvbctlib_t210.c
+++ b/src/t210/nvbctlib_t210.c
@@ -113,7 +113,10 @@ parse_token t210_root_token_list[] = {
token_crypto_length,
token_max_bct_search_blks,
token_unique_chip_id,
- token_secure_debug_control
+ token_secure_debug_control,
+ token_rsa_key_modulus,
+ token_rsa_pss_sig_bl,
+ token_rsa_pss_sig_bct
};
int
@@ -2174,6 +2177,28 @@ t210_bct_get_value(parse_token id, void *data, u_int8_t *bct)
}
int
+t210_bct_get_value_size(parse_token id)
+{
+ switch (id) {
+ case token_rsa_key_modulus:
+ return sizeof(nvboot_rsa_key_modulus);
+
+ case token_rsa_pss_sig_bl:
+ return sizeof(nvboot_rsa_pss_sig);
+
+ case token_rsa_pss_sig_bct:
+ return sizeof(nvboot_rsa_pss_sig);
+
+ /*
+ * Other bct fields can be added in when needed
+ */
+ default:
+ return -ENODATA;
+ }
+ return 0;
+}
+
+int
t210_bct_set_value(parse_token id, void *data, u_int8_t *bct)
{
nvboot_config_table *bct_ptr = (nvboot_config_table *)bct;
@@ -2198,6 +2223,26 @@ t210_bct_set_value(parse_token id, void *data, u_int8_t *bct)
memcpy(&bct_ptr->unique_chip_id, data, sizeof(nvboot_ecid));
break;
+ case token_rsa_key_modulus:
+ reverse_byte_order((u_int8_t *)&bct_ptr->key, data,
+ sizeof(nvboot_rsa_key_modulus));
+ break;
+
+ case token_rsa_pss_sig_bl:
+ /*
+ * Update bootloader 0 since there is only one copy
+ * of bootloader being built in.
+ */
+ reverse_byte_order(
+ (u_int8_t *)&bct_ptr->bootloader[0].signature.rsa_pss_sig,
+ data, sizeof(nvboot_rsa_pss_sig));
+ break;
+
+ case token_rsa_pss_sig_bct:
+ reverse_byte_order((u_int8_t *)&bct_ptr->signature.rsa_pss_sig,
+ data, sizeof(nvboot_rsa_pss_sig));
+ break;
+
default:
return -ENODATA;
}
@@ -2279,6 +2324,7 @@ cbootimage_soc_config tegra210_config = {
.getbl_param = t210_getbl_param,
.set_value = t210_bct_set_value,
.get_value = t210_bct_get_value,
+ .get_value_size = t210_bct_get_value_size,
.set_data = t210_bct_set_data,
.get_bct_size = t210_get_bct_size,
.token_supported = t210_bct_token_supported,
--
1.8.1.5
^ permalink raw reply related [flat|nested] 8+ messages in thread* [cbootimage PATCH v7 2/5] Add support to dump rsa related fields for t210
[not found] ` <1445295718-19146-1-git-send-email-jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2015-10-19 23:01 ` [cbootimage PATCH v7 1/5] Add support for update pubkey and rsa-pss signatures Jimmy Zhang
@ 2015-10-19 23:01 ` Jimmy Zhang
2015-10-19 23:01 ` [cbootimage PATCH v7 3/5] Add new configuration keyword "RehashBl" Jimmy Zhang
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Jimmy Zhang @ 2015-10-19 23:01 UTC (permalink / raw)
To: amartin-DDmLM1+adcrQT0dZR+AlfA, swarren-DDmLM1+adcrQT0dZR+AlfA
Cc: linux-tegra-u79uwXL29TY76Z2rM5mHXA, Jimmy Zhang
Add support to dump rsa pubkey, bct's rsa-pss signature and
bootloader's rsa-pss signature.
Cahgnes in V7:
1) Clean up compiler warnings from nvbctlib_t210.c
Changes in V6:
1) Add token id as input parameter for format_function()
2) Call get_value_size() to get paramter size in function
format_rsa_param() instead of using a constant.
Signed-off-by: Jimmy Zhang <jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
---
src/bct_dump.c | 65 ++++++++++++++++++++++++++++++++++++++++--------
src/t210/nvbctlib_t210.c | 19 ++++++++++++++
2 files changed, 74 insertions(+), 10 deletions(-)
diff --git a/src/bct_dump.c b/src/bct_dump.c
index be7b85dc72d6..4f50fa261e6e 100644
--- a/src/bct_dump.c
+++ b/src/bct_dump.c
@@ -27,11 +27,13 @@
int enable_debug;
cbootimage_soc_config * g_soc_config;
-static void format_u32_hex8(char const * message, void * data);
-static void format_u32(char const * message, void * data);
-static void format_chipuid(char const * message, void * data);
+static void format_u32_hex8(parse_token id, char const * message, void * data);
+static void format_u32(parse_token id, char const * message, void * data);
+static void format_chipuid(parse_token id, char const * message, void * data);
+static void format_hex_16_bytes(parse_token id, char const * message, void * data);
+static void format_rsa_param(parse_token id, char const * message, void * data);
-typedef void (*format_function)(char const * message, void * data);
+typedef void (*format_function)(parse_token id, char const * message, void * data);
typedef struct {
parse_token id;
@@ -39,9 +41,11 @@ typedef struct {
format_function format;
} value_data;
+#define PARAM_TYPE_BINARY_DATA_MAX_SIZE 256
typedef union {
u_int32_t val;
u_int8_t uid[16];
+ u_int8_t binary[PARAM_TYPE_BINARY_DATA_MAX_SIZE];
} param_types;
#define MAX_PARAM_SIZE sizeof(param_types)
@@ -54,6 +58,9 @@ static value_data const values[] = {
{ token_odm_data, "OdmData = ", format_u32_hex8 },
{ token_secure_jtag_control, "JtagCtrl = ", format_u32_hex8 },
{ token_secure_debug_control, "DebugCtrl = ", format_u32_hex8 },
+ { token_crypto_hash, "BCT AES Hash = ", format_hex_16_bytes },
+ { token_rsa_key_modulus, "RsaKeyModulus:\n", format_rsa_param },
+ { token_rsa_pss_sig_bct, "RsaPssSigBct:\n", format_rsa_param },
{ token_unique_chip_id, "ChipUid = ", format_chipuid },
{ token_bootloader_used, "# Bootloader used = ", format_u32 },
{ token_bootloaders_max, "# Bootloaders max = ", format_u32 },
@@ -72,6 +79,8 @@ static value_data const bl_values[] = {
{ token_bl_load_addr, "Load address = ", format_u32_hex8 },
{ token_bl_entry_point, "Entry point = ", format_u32_hex8 },
{ token_bl_attribute, "Attributes = ", format_u32_hex8 },
+ { token_bl_crypto_hash, "Bl AES Hash = ", format_hex_16_bytes },
+ { token_rsa_pss_sig_bl, "RsaPssSigBl:\n", format_rsa_param },
};
static value_data const mts_values[] = {
@@ -85,17 +94,17 @@ static value_data const mts_values[] = {
};
/*****************************************************************************/
-static void format_u32_hex8(char const * message, void * data)
+static void format_u32_hex8(parse_token id, char const * message, void * data)
{
printf("%s0x%08x;\n", message, *((u_int32_t *) data));
}
-static void format_u32(char const * message, void * data)
+static void format_u32(parse_token id, char const * message, void * data)
{
printf("%s%d;\n", message, *((u_int32_t *) data));
}
-static void format_chipuid(char const * message, void * data)
+static void format_chipuid(parse_token id, char const * message, void * data)
{
u_int8_t *uid = (u_int8_t *)data;
int byte_index;
@@ -108,6 +117,38 @@ static void format_chipuid(char const * message, void * data)
printf("%s%s;\n", message, uid_str);
}
+static void format_hex_16_bytes(parse_token id, char const * message, void * data)
+{
+ u_int8_t *p_byte = (u_int8_t *)data;
+ int byte_index;
+
+ printf("%s", message);
+ for (byte_index = 0; byte_index < 16; ++byte_index)
+ printf("%02x", *p_byte++);
+
+ printf(";\n");
+}
+
+static void format_rsa_param(parse_token id, char const * message, void * data)
+{
+#define MAX_BYTE_NUMBER_PER_LINE 16
+ u_int8_t *rsa = (u_int8_t *)data;
+ int size = g_soc_config->get_value_size(id);
+ int byte_index;
+
+ printf("%s", message);
+ for (byte_index = 0; byte_index < size; ++byte_index) {
+ printf(" %02x", *rsa++);
+
+ if ((byte_index + 1) % MAX_BYTE_NUMBER_PER_LINE == 0)
+ printf("\n");
+ }
+
+ if (byte_index % MAX_BYTE_NUMBER_PER_LINE != 0)
+ printf("\n");
+#undef MAX_BYTE_NUMBER_PER_LINE
+}
+
/*****************************************************************************/
static void usage(void)
{
@@ -213,7 +254,7 @@ int main(int argc, char *argv[])
if (e)
memset(&data, 0, MAX_PARAM_SIZE);
- values[i].format(values[i].message, &data);
+ values[i].format(values[i].id, values[i].message, &data);
}
/* Display bootloader values */
@@ -241,7 +282,9 @@ int main(int argc, char *argv[])
if (e)
data.val = -1;
- bl_values[j].format(bl_values[j].message, &data);
+ bl_values[j].format(bl_values[j].id,
+ bl_values[j].message,
+ &data);
}
}
}
@@ -271,7 +314,9 @@ int main(int argc, char *argv[])
if (e)
data.val = -1;
- mts_values[j].format(mts_values[j].message, &data);
+ mts_values[j].format(mts_values[j].id,
+ mts_values[j].message,
+ &data);
}
}
}
diff --git a/src/t210/nvbctlib_t210.c b/src/t210/nvbctlib_t210.c
index 3380411c131c..1d41cd6e1e6d 100644
--- a/src/t210/nvbctlib_t210.c
+++ b/src/t210/nvbctlib_t210.c
@@ -109,6 +109,8 @@ parse_token t210_root_token_list[] = {
token_bootloaders_max,
token_bct_size,
token_hash_size,
+ token_crypto_hash,
+ token_bl_crypto_hash,
token_crypto_offset,
token_crypto_length,
token_max_bct_search_blks,
@@ -2034,6 +2036,12 @@ t210_getbl_param(u_int32_t set,
sizeof(nvboot_hash));
break;
+ case token_rsa_pss_sig_bl:
+ reverse_byte_order((u_int8_t *)data,
+ (const u_int8_t *)&bct_ptr->bootloader[set].signature.rsa_pss_sig,
+ sizeof(nvboot_rsa_pss_sig));
+ break;
+
default:
return -ENODATA;
}
@@ -2130,6 +2138,17 @@ t210_bct_get_value(parse_token id, void *data, u_int8_t *bct)
memcpy(data, &(bct_ptr->unique_chip_id), sizeof(nvboot_ecid));
break;
+ case token_rsa_key_modulus:
+ reverse_byte_order(data, (const u_int8_t *)&bct_ptr->key,
+ sizeof(nvboot_rsa_key_modulus));
+ break;
+
+ case token_rsa_pss_sig_bct:
+ reverse_byte_order(data,
+ (const u_int8_t *)&bct_ptr->signature.rsa_pss_sig,
+ sizeof(nvboot_rsa_pss_sig));
+ break;
+
case token_reserved_offset:
*((u_int32_t *)data) = (u_int8_t *)&(samplebct.reserved)
- (u_int8_t *)&samplebct;
--
1.8.1.5
^ permalink raw reply related [flat|nested] 8+ messages in thread* [cbootimage PATCH v7 3/5] Add new configuration keyword "RehashBl"
[not found] ` <1445295718-19146-1-git-send-email-jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2015-10-19 23:01 ` [cbootimage PATCH v7 1/5] Add support for update pubkey and rsa-pss signatures Jimmy Zhang
2015-10-19 23:01 ` [cbootimage PATCH v7 2/5] Add support to dump rsa related fields for t210 Jimmy Zhang
@ 2015-10-19 23:01 ` Jimmy Zhang
2015-10-19 23:01 ` [cbootimage PATCH v7 4/5] Add a sample script to do rsa signing for T210 bootimage Jimmy Zhang
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Jimmy Zhang @ 2015-10-19 23:01 UTC (permalink / raw)
To: amartin-DDmLM1+adcrQT0dZR+AlfA, swarren-DDmLM1+adcrQT0dZR+AlfA
Cc: linux-tegra-u79uwXL29TY76Z2rM5mHXA, Jimmy Zhang
This feature is needed in case an image is updated at later stage
after bootimage has been created.
How to use:
Add keyword "RehashBl" to configuration file, for example, update.cfg:
RehashBl;
Invoke cbootimage to re-calculate bootloader aes hash, for example, for
bootimage bootloader.bin:
$ cbootimage -s tegra210 --update update.cfg bootloader.bin bootloader.bin-resigned
Where bootloader.bin-resigned is the resigned bootimage bootloader.bin
Signed-off-by: Jimmy Zhang <jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
---
samples/update.cfg | 1 +
src/crypto.c | 34 ++++++++++++++++++++++++++++++++++
src/crypto.h | 7 +++++++
src/data_layout.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
src/data_layout.h | 2 ++
src/parse.c | 9 +++++++++
src/parse.h | 1 +
7 files changed, 105 insertions(+)
create mode 100644 samples/update.cfg
diff --git a/samples/update.cfg b/samples/update.cfg
new file mode 100644
index 000000000000..c5c741bad536
--- /dev/null
+++ b/samples/update.cfg
@@ -0,0 +1 @@
+RehashBl;
diff --git a/src/crypto.c b/src/crypto.c
index 039be0a8a611..5438a5314981 100644
--- a/src/crypto.c
+++ b/src/crypto.c
@@ -326,3 +326,37 @@ reverse_byte_order(
if (size % 2)
out[size / 2] = in[size / 2];
}
+
+int
+sign_bl(build_image_context *context,
+ u_int8_t *bootloader,
+ u_int32_t length,
+ u_int32_t image_instance)
+{
+ int e = 0;
+ u_int8_t *hash_buffer;
+ u_int32_t hash_size;
+
+ g_soc_config->get_value(token_hash_size,
+ &hash_size, context->bct);
+
+ hash_buffer = calloc(1, hash_size);
+ if (hash_buffer == NULL)
+ return -ENOMEM;
+
+ /* Encrypt and compute hash */
+ if ((e = sign_data_block(bootloader,
+ length,
+ hash_buffer)) != 0)
+ goto fail;
+
+ if ((e = g_soc_config->setbl_param(image_instance,
+ token_bl_crypto_hash,
+ (u_int32_t*)hash_buffer,
+ context->bct)) != 0)
+ goto fail;
+
+ fail:
+ free(hash_buffer);
+ return e;
+}
diff --git a/src/crypto.h b/src/crypto.h
index f56b67d983f3..3cd73f2ac1a2 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -49,4 +49,11 @@ reverse_byte_order(
u_int8_t *out,
const u_int8_t *in,
const u_int32_t size);
+
+int
+sign_bl(build_image_context *context,
+ u_int8_t *bootloader,
+ u_int32_t length,
+ u_int32_t image_instance);
+
#endif /* #ifndef INCLUDED_CRYPTO_H */
diff --git a/src/data_layout.c b/src/data_layout.c
index 082609236724..5d3fe10ceda4 100644
--- a/src/data_layout.c
+++ b/src/data_layout.c
@@ -1065,3 +1065,54 @@ int get_bct_size_from_image(build_image_context *context)
context->bct = 0;
return bct_size;
}
+
+int resign_bl(build_image_context *context)
+{
+ int ret;
+ u_int8_t *buffer, *image;
+ u_int32_t image_instance = 0; /* support only one instance */
+ u_int32_t image_actual_size; /* In bytes */
+ u_int32_t bl_length;
+ u_int32_t pages_in_image;
+ u_int32_t blk_size, page_size, current_blk, current_page;
+ u_int32_t offset;
+
+ /* read in bl from image */
+ g_soc_config->get_value(token_block_size, &blk_size, context->bct);
+ g_soc_config->get_value(token_page_size, &page_size, context->bct);
+
+ GET_BL_FIELD(image_instance, start_blk, ¤t_blk);
+ GET_BL_FIELD(image_instance, start_page, ¤t_page);
+ GET_BL_FIELD(image_instance, length, &bl_length);
+
+ offset = current_blk * blk_size +
+ current_page * page_size;
+
+ if (read_from_image(context->input_image_filename,
+ offset, bl_length,
+ &image, &image_actual_size, file_type_bin)) {
+ printf("Error reading image file %s.\n",
+ context->input_image_filename);
+ return -ENOMEM;
+ }
+
+ pages_in_image = ICEIL(image_actual_size, page_size);
+
+ /* Create a local copy of the bl */
+ if ((buffer = malloc(pages_in_image * page_size)) == NULL) {
+ ret = -ENOMEM;
+ goto fail;
+ }
+
+ memset(buffer, 0, pages_in_image * page_size);
+ memcpy(buffer, image, image_actual_size);
+
+ insert_padding(buffer, image_actual_size);
+
+ /* sign bl */
+ ret = sign_bl(context, buffer, image_actual_size, image_instance);
+ free (buffer);
+ fail:
+ free (image);
+ return ret;
+}
diff --git a/src/data_layout.h b/src/data_layout.h
index c6e53e61be83..0e6e41fcb24c 100644
--- a/src/data_layout.h
+++ b/src/data_layout.h
@@ -64,4 +64,6 @@ get_bct_size_from_image(build_image_context *context);
int
begin_update(build_image_context *context);
+int
+resign_bl(build_image_context *context);
#endif /* #ifndef INCLUDED_DATA_LAYOUT_H */
diff --git a/src/parse.c b/src/parse.c
index 667895c4dd54..6f37dad76212 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -80,6 +80,8 @@ static int
parse_dev_param(build_image_context *context, parse_token token, char *rest);
static int
parse_sdram_param(build_image_context *context, parse_token token, char *rest);
+static int
+parse_sign_bl(build_image_context *context, parse_token token, char *rest);
static int process_statement(build_image_context *context,
char *str,
@@ -121,6 +123,7 @@ static parse_item s_top_level_items[] = {
{ "RsaKeyModulusFile=", token_rsa_key_modulus, parse_rsa_param },
{ "RsaPssSigBlFile=", token_rsa_pss_sig_bl, parse_rsa_param },
{ "RsaPssSigBctFile=", token_rsa_pss_sig_bct, parse_rsa_param },
+ { "RehashBl", token_sign_bl, parse_sign_bl },
{ NULL, 0, NULL } /* Must be last */
};
@@ -689,6 +692,12 @@ parse_bct_file(build_image_context *context, parse_token token, char *rest)
return 0;
}
+static int
+parse_sign_bl(build_image_context *context, parse_token token, char *rest)
+{
+ return resign_bl(context);
+}
+
static char *
parse_end_state(char *str, char *uname, int chars_remaining)
{
diff --git a/src/parse.h b/src/parse.h
index f2e28b306709..191742c082ee 100644
--- a/src/parse.h
+++ b/src/parse.h
@@ -117,6 +117,7 @@ typedef enum
token_rsa_key_modulus,
token_rsa_pss_sig_bl,
token_rsa_pss_sig_bct,
+ token_sign_bl,
token_nand_clock_divider,
token_nand_nand_timing,
--
1.8.1.5
^ permalink raw reply related [flat|nested] 8+ messages in thread* [cbootimage PATCH v7 4/5] Add a sample script to do rsa signing for T210 bootimage
[not found] ` <1445295718-19146-1-git-send-email-jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
` (2 preceding siblings ...)
2015-10-19 23:01 ` [cbootimage PATCH v7 3/5] Add new configuration keyword "RehashBl" Jimmy Zhang
@ 2015-10-19 23:01 ` Jimmy Zhang
2015-10-19 23:01 ` [cbootimage PATCH v7 5/5] Bump to version 1.6 Jimmy Zhang
2015-10-19 23:47 ` [cbootimage PATCH v7 0/5] Add RSA signing support Stephen Warren
5 siblings, 0 replies; 8+ messages in thread
From: Jimmy Zhang @ 2015-10-19 23:01 UTC (permalink / raw)
To: amartin-DDmLM1+adcrQT0dZR+AlfA, swarren-DDmLM1+adcrQT0dZR+AlfA
Cc: linux-tegra-u79uwXL29TY76Z2rM5mHXA, Jimmy Zhang
sign.sh runs openssl and other linux utilities to generate rsa-pss
signatures for a prebuilt bootimage and then uses cbootimage option
--update to update bootimage's rsa signatures and rsa modulus.
Syntax: sign.sh <bootimage> <rsa_key.pem>
Signed-off-by: Jimmy Zhang <jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
---
samples/rsa_priv.pem | 27 +++++++++++++++++++
samples/sign.sh | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+)
create mode 100644 samples/rsa_priv.pem
create mode 100755 samples/sign.sh
diff --git a/samples/rsa_priv.pem b/samples/rsa_priv.pem
new file mode 100644
index 000000000000..a02d77fc438c
--- /dev/null
+++ b/samples/rsa_priv.pem
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpQIBAAKCAQEA2L0jolLgp2pKAzn/JeZuxgGPY1Yz4ZNkttzvBlVhozEynj2x
+Lttz1gZ6fYUb/ObM8v2PoeOlrwkGoWMscuMS4MnLG2NcJlWmlsLTyfw3EwxblM3D
+DniscakhMexNK3J7uxmmRkQTfldec4JHjsAN6d9cZQ0POdsA7j5lKNG0KgCohKk6
+p+lMYXFqgxx3IQWcynhuKVtVFm/UJJBC+a4ibbXcpnio96ySVrPO/ZpEOhEpPTWX
+VLeiqBB3dsu//9X0vDfShyBlctaonx2Z7xWQWotubze0iIvyU6U+T69aDOXfHQfu
+kNMX3Dj4VCndW/FUrrg5k/y9dMA1We3Ng1A0NQIDAQABAoIBABcWRqZy15VdwBaJ
+5gDeg+w5nFGDjDE6Jx9Hd3qgO69LfU3X2njYTYV92SxnsmyFFU3I7rTa7/ouJvOo
+AcMXJxqkxCrdsaIvu3gRtsesQx2XUmYOaPmwpwXQc0XDGxFGt6FdgRW5CK6LlfcN
+6JtvH8xKy6fD9Vw/VOEL6nCnrd5PU3UNU/Ng7h/SZ+5NEALJE7+gaMvmK9o9lX3a
+/tze6bwKKF+a2luTs2aVGxjUYBud6YOE2KPG7zltuHUHUeEgJ/X/sgWYiHsqpK3l
+rIrjCVIQnrRCCtCHg5BbqtwStl5Gz+Y431DXU9Sv6fVqIFgveweePhhDux/YV+KY
+rvq5RiECgYEA7OHr8BYWkeZKuU/IkGdsdiPEEB7mNOJHwE4OXdwLIIygQGtQCuJG
+EHMQv9kE/1ibVRIxqnliFb/CupZ5wwyvjFgUq5XZl7s6XpNOBhJHV0U5AJSvS0rb
+YNU2PBfRmMMI/gRdF/onUpopY7ZWLv7u+VF7ZgtM5hQr2jwcjwBzbRkCgYEA6jsK
+tB6SGIO2c5E+CLAY5J4eJca6ORaVcKw1OfDL346UJYkvqOLBc8KwFs87gDbwhmjn
+GJUWlhk5iUoWZrFJpTj8+hVNxKumtZ5x8MQkNXL7WBNYcVxobuGVW8c6jZU3C/al
+Im9DRTPXhgvMy7mu4slVaAhhrmUJRdl6fwmCR30CgYEA5FoxwML6RPGUrSl9Nb+N
+riFyWvv+fZJ5Cqf0b4S08U6/GPqaMbPJSQgzaE3D5Ie9Tff5CtZyuHagOJDglie/
+fvJWEsak+QETFqK3/2BVh4qClc2/YjyqWKGQ48MuWS4CmCUKvRd4GsfkCGx4jltR
+ceSbqVZRbiaZ04pJGY2ct9kCgYEArWaaLO/4zgcsGfArUXk0ZIMd5G9zS3IJnckO
++l7mPxEpYYRm8Qs1lcJKZAh0jx2dAJRGiO9OMj5oVtevL8UNtTA0L9t3oCJHH2s2
+BLzf5WXC5tgjgICdm4CK9s/N7CTMBKJKa+yci22un0C7ExLagm/0NzkFP3ry22/9
+/HAIr20CgYEAnUGwciM7Z9aMpPkX3iaRG/zm1FWbsuJldNa5IZQ6CamDIZhb+u2u
+1yuCUJZ7zY51RO4n2Hi/1OU1XS7XlevoT22i7xJmIjPVoWzumUwMjmhYVqxK/X50
+Hcd+qL1Xs6KmsWrlg2sgFliX79RawE3jl/yZrFMuHvWiItXO92YFuOI=
+-----END RSA PRIVATE KEY-----
diff --git a/samples/sign.sh b/samples/sign.sh
new file mode 100755
index 000000000000..2edd12695f4b
--- /dev/null
+++ b/samples/sign.sh
@@ -0,0 +1,73 @@
+#!/bin/bash
+#
+# Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms and conditions of the GNU General Public License,
+# version 2, as published by the Free Software Foundation.
+#
+# This program is distributed in the hope it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# See file CREDITS for list of people who contributed to this
+# project.
+#
+set -e
+IMAGE_FILE=$1
+KEY_FILE=$2
+TARGET_IMAGE=$IMAGE_FILE
+CONFIG_FILE=config.tmp
+
+CBOOTIMAGE=../src/cbootimage
+BCT_DUMP=../src/bct_dump
+OBJCOPY=objcopy
+OPENSSL=openssl
+DD=dd
+RM=rm
+MV=mv
+XXD=xxd
+CUT=cut
+
+echo "Get rid of all temporary files: *.sig, *.tosig, *.tmp *.mod"
+$RM -f *.sig *.tosig *.tmp *.mod
+
+echo "Get bl length "
+BL_LENGTH=`$BCT_DUMP $IMAGE_FILE | grep "Bootloader\[0\].Length"\
+ | awk -F ' ' '{print $4}' | awk -F ';' '{print $1}'`
+
+echo "Extract bootloader to $IMAGE_FILE.bl.tosig, length $BL_LENGTH"
+$DD bs=1 skip=32768 if=$IMAGE_FILE of=$IMAGE_FILE.bl.tosig count=$BL_LENGTH
+
+echo "Calculate rsa signature for bootloader and save to $IMAGE_FILE.bl.sig"
+$OPENSSL dgst -sha256 -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-1 \
+ -sign $KEY_FILE -out $IMAGE_FILE.bl.sig $IMAGE_FILE.bl.tosig
+
+echo "Update bootloader's rsa signature, aes hash and bct's aes hash"
+echo "RsaPssSigBlFile = $IMAGE_FILE.bl.sig;" > $CONFIG_FILE
+echo "RehashBl;" >> $CONFIG_FILE
+$CBOOTIMAGE -s tegra210 -u $CONFIG_FILE $IMAGE_FILE $IMAGE_FILE.tmp
+
+echo "Extract the part of bct which needs to be rsa signed"
+$DD bs=1 if=$IMAGE_FILE.tmp of=$IMAGE_FILE.bct.tosig count=8944 skip=1296
+
+echo "Calculate rsa signature for bct and save to $IMAGE_FILE.bct.sig"
+$OPENSSL dgst -sha256 -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-1 \
+ -sign $KEY_FILE -out $IMAGE_FILE.bct.sig $IMAGE_FILE.bct.tosig
+
+echo "Create public key modulus from key file $KEY_FILE and save to $KEY_FILE.mod"
+$OPENSSL rsa -in $KEY_FILE -noout -modulus -out $KEY_FILE.mod
+# remove prefix
+$CUT -d= -f2 < $KEY_FILE.mod > $KEY_FILE.mod.tmp
+
+# convert from hexdecimal to binary
+$XXD -r -p -l 256 $KEY_FILE.mod.tmp $KEY_FILE.mod.bin
+
+echo "Update bct's rsa signature and modulus"
+echo "RsaPssSigBctFile = $IMAGE_FILE.bct.sig;" > $CONFIG_FILE
+echo "RsaKeyModulusFile = $KEY_FILE.mod.bin;" >> $CONFIG_FILE
+$CBOOTIMAGE -s tegra210 -u $CONFIG_FILE $IMAGE_FILE.tmp $TARGET_IMAGE
--
1.8.1.5
^ permalink raw reply related [flat|nested] 8+ messages in thread* [cbootimage PATCH v7 5/5] Bump to version 1.6
[not found] ` <1445295718-19146-1-git-send-email-jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
` (3 preceding siblings ...)
2015-10-19 23:01 ` [cbootimage PATCH v7 4/5] Add a sample script to do rsa signing for T210 bootimage Jimmy Zhang
@ 2015-10-19 23:01 ` Jimmy Zhang
2015-10-19 23:47 ` [cbootimage PATCH v7 0/5] Add RSA signing support Stephen Warren
5 siblings, 0 replies; 8+ messages in thread
From: Jimmy Zhang @ 2015-10-19 23:01 UTC (permalink / raw)
To: amartin-DDmLM1+adcrQT0dZR+AlfA, swarren-DDmLM1+adcrQT0dZR+AlfA
Cc: linux-tegra-u79uwXL29TY76Z2rM5mHXA, Jimmy Zhang
Create a release that adds rsa-pss signature support. Currently
it has only been tested on T210.
Signed-off-by: Jimmy Zhang <jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
---
configure.ac | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/configure.ac b/configure.ac
index f0f050946504..f251f09c211f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.67])
-AC_INIT([cbootimage], [1.5], [pchiu-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org])
+AC_INIT([cbootimage], [1.6], [jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/cbootimage.c])
AC_CONFIG_HEADERS([config.h])
--
1.8.1.5
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [cbootimage PATCH v7 0/5] Add RSA signing support
[not found] ` <1445295718-19146-1-git-send-email-jimmzhang-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
` (4 preceding siblings ...)
2015-10-19 23:01 ` [cbootimage PATCH v7 5/5] Bump to version 1.6 Jimmy Zhang
@ 2015-10-19 23:47 ` Stephen Warren
[not found] ` <562580FF.5000908-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
5 siblings, 1 reply; 8+ messages in thread
From: Stephen Warren @ 2015-10-19 23:47 UTC (permalink / raw)
To: Jimmy Zhang
Cc: amartin-DDmLM1+adcrQT0dZR+AlfA, swarren-DDmLM1+adcrQT0dZR+AlfA,
linux-tegra-u79uwXL29TY76Z2rM5mHXA
On 10/19/2015 05:01 PM, Jimmy Zhang wrote:
> V7:
> 1 Redefine parameter "u_int8_t *in" as "const u_int8_t *in" for function
> reverse_byte_order()
> 2 Clean up compiler warnings from nvbctlib_t210.c
I've applied this series, and pushed it and a new tag to github.
(I edited the commit descriptions to remove the changelog from them; it
should go below the --- line)
^ permalink raw reply [flat|nested] 8+ messages in thread