|
| 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