Skip to content

Commit 304e0e8

Browse files
committed
Added sign option --custom-tlv-buffer
1 parent 43f7730 commit 304e0e8

2 files changed

Lines changed: 60 additions & 7 deletions

File tree

docs/Signing.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,13 @@ Provides a value to be set with a custom tag
186186
Values can be decimal or hex numbers (prefixed by '0x'). The tag is a 16-bit number.
187187
Valid tags are in the range between 0x0030 and 0xFEFE.
188188

189-
The extra numeric field (can be 1, 2, 4, or 8 bytes long) is stored in the manifest
190-
header and can be retrieved at runtime by wolfBoot, using `wolfBoot_find_header()`.
189+
* `--custom-tlv-buffer tag value`: Adds a TLV entry with arbitrary length to the manifest
190+
header, corresponding to the type identified by `tag`, and assigns the value `value`. The
191+
tag is a 16-bit number. Valid tags are in the range between 0x0030 and 0xFEFE. The length
192+
is implicit, and is the length of the value.
193+
Value argument is in the form of a hex string, e.g. `--custom-tlv-buffer 0x0030 AABBCCDDEE`
194+
will add a TLV entry with tag 0x0030, length 5 and value 0xAABBCCDDEE.
195+
191196

192197
#### Three-steps signing using external provisioning tools
193198

tools/keytools/sign.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ struct cmd_options {
273273
uint16_t tag;
274274
uint16_t len;
275275
uint64_t val;
276+
uint8_t *buffer;
276277
} custom_tlv[MAX_CUSTOM_TLVS];
277278
};
278279

@@ -1086,8 +1087,13 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
10861087
while((header_idx % 8) != 4)
10871088
header_idx++;
10881089
}
1089-
header_append_tag(header, &header_idx, CMD.custom_tlv[i].tag,
1090-
CMD.custom_tlv[i].len, &CMD.custom_tlv[i].val);
1090+
if (CMD.custom_tlv[i].buffer == NULL) {
1091+
header_append_tag(header, &header_idx, CMD.custom_tlv[i].tag,
1092+
CMD.custom_tlv[i].len, &CMD.custom_tlv[i].val);
1093+
} else {
1094+
header_append_tag(header, &header_idx, CMD.custom_tlv[i].tag,
1095+
CMD.custom_tlv[i].len, CMD.custom_tlv[i].buffer);
1096+
}
10911097
}
10921098
/* Align for next field */
10931099
while ((header_idx % 4) != 0)
@@ -2014,8 +2020,40 @@ int main(int argc, char** argv)
20142020
CMD.custom_tlv[p].tag = tag;
20152021
CMD.custom_tlv[p].len = len;
20162022
CMD.custom_tlv[p].val = arg2num(argv[i+3], len);
2023+
CMD.custom_tlv[p].buffer = NULL;
20172024
CMD.custom_tlvs++;
20182025
i += 3;
2026+
} else if (strcmp(argv[i], "--custom-tlv-buffer") == 0) {
2027+
int p = CMD.custom_tlvs;
2028+
uint16_t tag, len;
2029+
uint32_t j;
2030+
if (p >= MAX_CUSTOM_TLVS) {
2031+
fprintf(stderr, "Too many custom TLVs.\n");
2032+
exit(16);
2033+
}
2034+
if (argc < (i + 3)) {
2035+
fprintf(stderr, "Invalid custom TLV fields. \n");
2036+
exit(16);
2037+
}
2038+
tag = (uint16_t)arg2num(argv[i + 1], 2);
2039+
len = (uint16_t)strlen(argv[i + 2]) / 2;
2040+
if (len > 255) {
2041+
fprintf(stderr, "custom tlv buffer size too big: %s\n", argv[i + 2]);
2042+
exit(16);
2043+
}
2044+
CMD.custom_tlv[p].tag = tag;
2045+
CMD.custom_tlv[p].len = len;
2046+
CMD.custom_tlv[p].buffer = malloc(len);
2047+
if (CMD.custom_tlv[p].buffer == NULL) {
2048+
fprintf(stderr, "Error malloc for custom tlv buffer %d\n", len);
2049+
exit(16);
2050+
}
2051+
for (j = 0; j < len; j++) {
2052+
char c[3] = {argv[i + 2][j * 2], argv[i + 2][j * 2 + 1], 0};
2053+
CMD.custom_tlv[p].buffer[j] = (uint8_t)strtol(c, NULL, 16);
2054+
}
2055+
CMD.custom_tlvs++;
2056+
i += 2;
20192057
}
20202058
else {
20212059
i--;
@@ -2092,13 +2130,23 @@ int main(int argc, char** argv)
20922130
printf("\n");
20932131

20942132
if (CMD.custom_tlvs > 0) {
2095-
uint32_t i;
2133+
uint32_t i, j;
20962134
printf("Custom TLVS: %u\n", CMD.custom_tlvs);
20972135
for (i = 0; i < CMD.custom_tlvs; i++) {
20982136
printf("TLV %u\n", i);
20992137
printf("----\n");
2100-
printf("Tag: %04X Len: %hu Val: %" PRIu64 "\n", CMD.custom_tlv[i].tag,
2101-
CMD.custom_tlv[i].len, CMD.custom_tlv[i].val);
2138+
if (CMD.custom_tlv[i].buffer) {
2139+
printf("Tag: %04X Len: %hu Val: ", CMD.custom_tlv[i].tag,
2140+
CMD.custom_tlv[i].len);
2141+
for (j = 0; j < CMD.custom_tlv[i].len; j++) {
2142+
printf("%02X", CMD.custom_tlv[i].buffer[j]);
2143+
}
2144+
printf("\n");
2145+
2146+
} else {
2147+
printf("Tag: %04X Len: %hu Val: %" PRIu64 "\n", CMD.custom_tlv[i].tag,
2148+
CMD.custom_tlv[i].len, CMD.custom_tlv[i].val);
2149+
}
21022150
printf("-----\n");
21032151
}
21042152
}

0 commit comments

Comments
 (0)