Skip to content

Commit 9afe8a5

Browse files
committed
Do not rely on alignment when loading RSA exponent
F/369
1 parent 819ae95 commit 9afe8a5

3 files changed

Lines changed: 151 additions & 2 deletions

File tree

src/tpm.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ int wolfBoot_load_pubkey(const uint8_t* pubkey_hint, WOLFTPM2_KEY* pubKey,
382382
defined(WOLFBOOT_SIGN_RSA3072) || \
383383
defined(WOLFBOOT_SIGN_RSA4096)
384384
uint32_t inOutIdx = 0;
385+
uint32_t exponent = 0;
386+
uint32_t j;
385387
const uint8_t*n = NULL, *e = NULL;
386388
uint32_t nSz = 0, eSz = 0;
387389
if (key_type != AUTH_KEY_RSA2048 && key_type != AUTH_KEY_RSA3072 &&
@@ -395,10 +397,19 @@ int wolfBoot_load_pubkey(const uint8_t* pubkey_hint, WOLFTPM2_KEY* pubKey,
395397
&e, &eSz /* exponent */
396398
);
397399
}
400+
if (rc == 0) {
401+
if (eSz == 0 || eSz > sizeof(exponent))
402+
rc = -1;
403+
}
404+
if (rc == 0) {
405+
for (j = 0; j < eSz; j++) {
406+
exponent = (exponent << 8) | e[j];
407+
}
408+
}
398409
if (rc == 0) {
399410
/* Load public key into TPM */
400411
rc = wolfTPM2_LoadRsaPublicKey_ex(&wolftpm_dev, pubKey,
401-
n, nSz, *((uint32_t*)e),
412+
n, nSz, exponent,
402413
TPM_ALG_NULL, WOLFBOOT_TPM_HASH_ALG);
403414
}
404415
#else

tools/unit-tests/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
3838
unit-image unit-image-rsa unit-nvm unit-nvm-flagshome unit-enc-nvm \
3939
unit-enc-nvm-flagshome unit-delta unit-update-flash \
4040
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
41-
unit-multiboot unit-boot-x86-fsp unit-qspi-flash
41+
unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp
4242

4343
all: $(TESTS)
4444

@@ -109,6 +109,11 @@ unit-spi-flash: ../../include/target.h unit-spi-flash.c
109109
unit-qspi-flash: ../../include/target.h unit-qspi-flash.c
110110
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
111111

112+
unit-tpm-rsa-exp: ../../include/target.h unit-tpm-rsa-exp.c
113+
gcc -o $@ $^ $(CFLAGS) -I../../lib/wolfTPM -DWOLFBOOT_TPM \
114+
-DWOLFBOOT_TPM_VERIFY -DWOLFBOOT_SIGN_RSA2048 -DWOLFBOOT_HASH_SHA256 \
115+
-ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections
116+
112117
unit-string: ../../include/target.h unit-string.c
113118
gcc -o $@ $^ $(CFLAGS) -DDEBUG_UART -DPRINTF_ENABLED $(LDFLAGS)
114119

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* unit-tpm-rsa-exp.c
2+
*
3+
* Unit tests for TPM RSA public-key loading.
4+
*/
5+
6+
#include <check.h>
7+
#include <stdint.h>
8+
#include <string.h>
9+
10+
#ifndef SPI_CS_TPM
11+
#define SPI_CS_TPM 1
12+
#endif
13+
#ifndef WOLFBOOT_SHA_DIGEST_SIZE
14+
#define WOLFBOOT_SHA_DIGEST_SIZE 32
15+
#endif
16+
#ifndef WOLFBOOT_TPM_HASH_ALG
17+
#define WOLFBOOT_TPM_HASH_ALG TPM_ALG_SHA256
18+
#endif
19+
20+
#include "wolfboot/wolfboot.h"
21+
#include "keystore.h"
22+
#include "tpm.h"
23+
24+
static uint8_t test_hdr[16];
25+
static uint8_t test_modulus[256];
26+
static uint8_t test_exponent_der[] = { 0xAA, 0x01, 0x00, 0x01, 0x7B };
27+
static uint32_t captured_exponent;
28+
29+
int keyslot_id_by_sha(const uint8_t* pubkey_hint)
30+
{
31+
(void)pubkey_hint;
32+
return 0;
33+
}
34+
35+
uint32_t keystore_get_key_type(int id)
36+
{
37+
ck_assert_int_eq(id, 0);
38+
return AUTH_KEY_RSA2048;
39+
}
40+
41+
uint8_t *keystore_get_buffer(int id)
42+
{
43+
ck_assert_int_eq(id, 0);
44+
return test_hdr;
45+
}
46+
47+
int keystore_get_size(int id)
48+
{
49+
ck_assert_int_eq(id, 0);
50+
return (int)sizeof(test_hdr);
51+
}
52+
53+
int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx, word32 inSz,
54+
const byte** n, word32* nSz, const byte** e, word32* eSz)
55+
{
56+
(void)input;
57+
(void)inSz;
58+
59+
*inOutIdx = 0;
60+
*n = test_modulus;
61+
*nSz = sizeof(test_modulus);
62+
*e = &test_exponent_der[1];
63+
*eSz = 3;
64+
return 0;
65+
}
66+
67+
int wolfTPM2_LoadRsaPublicKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
68+
const byte* rsaPub, word32 rsaPubSz, word32 exponent,
69+
TPM_ALG_ID scheme, TPMI_ALG_HASH hashAlg)
70+
{
71+
(void)dev;
72+
(void)key;
73+
(void)rsaPub;
74+
(void)rsaPubSz;
75+
(void)scheme;
76+
(void)hashAlg;
77+
78+
captured_exponent = exponent;
79+
return 0;
80+
}
81+
82+
#include "../../src/tpm.c"
83+
84+
static void setup(void)
85+
{
86+
memset(test_hdr, 0x42, sizeof(test_hdr));
87+
memset(test_modulus, 0x5A, sizeof(test_modulus));
88+
captured_exponent = 0;
89+
}
90+
91+
START_TEST(test_wolfBoot_load_pubkey_decodes_der_exponent_bytes)
92+
{
93+
uint8_t hint[WOLFBOOT_SHA_DIGEST_SIZE] = { 0 };
94+
WOLFTPM2_KEY key;
95+
TPM_ALG_ID alg = TPM_ALG_NULL;
96+
int rc;
97+
98+
memset(&key, 0, sizeof(key));
99+
100+
rc = wolfBoot_load_pubkey(hint, &key, &alg);
101+
102+
ck_assert_int_eq(rc, 0);
103+
ck_assert_int_eq(alg, TPM_ALG_RSA);
104+
ck_assert_uint_eq(captured_exponent, 65537U);
105+
}
106+
END_TEST
107+
108+
static Suite *tpm_suite(void)
109+
{
110+
Suite *s;
111+
TCase *tc;
112+
113+
s = suite_create("TPM RSA");
114+
tc = tcase_create("wolfBoot_load_pubkey");
115+
tcase_add_checked_fixture(tc, setup, NULL);
116+
tcase_add_test(tc, test_wolfBoot_load_pubkey_decodes_der_exponent_bytes);
117+
suite_add_tcase(s, tc);
118+
return s;
119+
}
120+
121+
int main(void)
122+
{
123+
Suite *s;
124+
SRunner *sr;
125+
int failed;
126+
127+
s = tpm_suite();
128+
sr = srunner_create(s);
129+
srunner_run_all(sr, CK_NORMAL);
130+
failed = srunner_ntests_failed(sr);
131+
srunner_free(sr);
132+
return failed == 0 ? 0 : 1;
133+
}

0 commit comments

Comments
 (0)