Skip to content

Commit

Permalink
Peer review cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarske committed Apr 2, 2024
1 parent 892726e commit d915287
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
36 changes: 19 additions & 17 deletions examples/firmware/ifx_fw_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
}


/* macros for stdio */
/* portability macros for stdio */
#define XFILE FILE*
#define XFOPEN fopen
#define XFSEEK fseek
Expand All @@ -97,12 +97,10 @@
#define XFGETS fgets
#define XFEOF feof

/* internal error codes */
#define MEMORY_E -125 /* out of memory error */
#define BUFFER_E -132 /* output buffer too small or input too large */
#define BAD_FUNC_ARG -173 /* Bad function argument provided */


/* Helper function to split .bin into manifest and firmware file
* based on key group id */
/* Can also display the key group */
static int extractFW(
uint8_t *fw, size_t fw_size, uint32_t keygroup_id,
uint8_t **manifest, size_t *manifest_size,
Expand All @@ -111,7 +109,9 @@ static int extractFW(
size_t offset = 0, offset2;
uint16_t size16, num;
uint32_t size32, group;
int i;

/* all Infineon firmware.bin files have this GUID header */
const uint8_t guid[] = { 0x1a, 0x53, 0x66, 0x7a,
0xfb, 0x12, 0x47, 0x9e,
0xac, 0x58, 0xec, 0x99,
Expand Down Expand Up @@ -143,7 +143,7 @@ static int extractFW(
READ_BE16(num, fw, offset, offset2);

*manifest = NULL;
for (int i = 0; i < num; i++) {
for (i = 0; i < num; i++) {
READ_BE32(group, fw, offset, offset2);
printf("Found group %08x\n", group);

Expand Down Expand Up @@ -200,13 +200,13 @@ static int readfile(const char* fname, uint8_t** buf, size_t* bufLen)
XFILE fp;

if (fname == NULL || buf == NULL || bufLen == NULL)
return BAD_FUNC_ARG;
return -1;

/* open file (read-only binary) */
fp = XFOPEN(fname, "rb");
if (fp == XBADFILE) {
fprintf(stderr, "Error loading %s\n", fname);
return BUFFER_E;
return -1;
}

XFSEEK(fp, 0, XSEEK_END);
Expand All @@ -216,10 +216,10 @@ static int readfile(const char* fname, uint8_t** buf, size_t* bufLen)
if (*buf == NULL) {
*buf = (uint8_t*)malloc(fileSz);
if (*buf == NULL)
ret = MEMORY_E;
ret = -1;
}
else if (*buf != NULL && fileSz > (ssize_t)*bufLen) {
ret = BUFFER_E;
ret = -1;
}
*bufLen = (size_t)fileSz;
if (ret == 0) {
Expand All @@ -228,7 +228,7 @@ static int readfile(const char* fname, uint8_t** buf, size_t* bufLen)
}
}
else {
ret = BUFFER_E;
ret = -1;
}
XFCLOSE(fp);
return ret;
Expand All @@ -241,7 +241,7 @@ static int writefile(const char* filename, const uint8_t *buf, size_t bufSz)
size_t fileSz = 0;

if (filename == NULL || buf == NULL)
return BAD_FUNC_ARG;
return -1;

fp = XFOPEN(filename, "wb");
if (fp != XBADFILE) {
Expand Down Expand Up @@ -270,7 +270,8 @@ int main(int argc, char **argv)
{
printf("Usage:\n");
printf(" ifx_fw_extract <fw-file>\n");
printf(" ifx_fw_extract <fw-file> <keygroup_id> <manifest-file> <data-file>\n");
printf(" ifx_fw_extract <fw-file> "
"<keygroup_id> <manifest-file> <data-file>\n");
exit(1);
}

Expand All @@ -282,7 +283,9 @@ int main(int argc, char **argv)
}

if (argc >= 3) {
if (sscanf(argv[2], "0x%08x", &keygroup_id) != 1 && sscanf(argv[2], "%08x", &keygroup_id) != 1) {
if (sscanf(argv[2], "0x%08x", &keygroup_id) != 1 &&
sscanf(argv[2], "%08x", &keygroup_id) != 1)
{
LOG("Cannot read keygroup_id.");
rc = EXIT_FAILURE;
goto exit;
Expand All @@ -306,12 +309,11 @@ int main(int argc, char **argv)
goto exit;
}
}
rc = 0;
rc = 0; /* success */
}
else {
printf("Bad arguments.\n");
rc = EXIT_FAILURE;
goto exit;
}

exit:
Expand Down
10 changes: 7 additions & 3 deletions src/tpm2_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -7442,6 +7442,9 @@ int wolfTPM2_FirmwareUpgrade(WOLFTPM2_DEV* dev,
uint8_t manifest_hash[TPM_SHA384_DIGEST_SIZE];
uint32_t manifest_hash_sz = (uint32_t)sizeof(manifest_hash);

/* Can use SHA2-384 or SHA2-512 for manifest hash */
hashAlg = TPM_ALG_SHA384;

/* check the operational mode */
rc = wolfTPM2_GetCapabilities(dev, &caps);
if (rc == TPM_RC_SUCCESS) {
Expand All @@ -7454,9 +7457,10 @@ int wolfTPM2_FirmwareUpgrade(WOLFTPM2_DEV* dev,
}
}

/* hash the manifest */
hashAlg = TPM_ALG_SHA384; /* use SHA2-384 or SHA2-512 for manifest hash */
rc = wc_Sha384Hash(manifest, manifest_sz, manifest_hash);
if (rc == TPM_RC_SUCCESS) {
/* hash the manifest */
rc = wc_Sha384Hash(manifest, manifest_sz, manifest_hash);
}
if (rc == TPM_RC_SUCCESS) {
rc = tpm2_ifx_firmware_enable_policy(dev);
}
Expand Down

0 comments on commit d915287

Please sign in to comment.