|
| 1 | +/* unit-tpm-check-rot-auth.c |
| 2 | + * |
| 3 | + * Unit tests for TPM root-of-trust auth validation. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <check.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#ifndef SPI_CS_TPM |
| 12 | +#define SPI_CS_TPM 1 |
| 13 | +#endif |
| 14 | +#ifndef WOLFBOOT_SHA_DIGEST_SIZE |
| 15 | +#define WOLFBOOT_SHA_DIGEST_SIZE 32 |
| 16 | +#endif |
| 17 | +#ifndef WOLFBOOT_TPM_HASH_ALG |
| 18 | +#define WOLFBOOT_TPM_HASH_ALG TPM_ALG_SHA256 |
| 19 | +#endif |
| 20 | +#define WOLFBOOT_TPM_KEYSTORE |
| 21 | +#define WOLFBOOT_TPM_KEYSTORE_AUTH \ |
| 22 | + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" \ |
| 23 | + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 24 | + |
| 25 | +#include "wolfboot/wolfboot.h" |
| 26 | +#include "keystore.h" |
| 27 | +#include "tpm.h" |
| 28 | + |
| 29 | +static uint8_t test_hdr[16]; |
| 30 | +static uint8_t test_modulus[256]; |
| 31 | +static uint8_t test_exponent_der[] = { 0xAA, 0x01, 0x00, 0x01, 0x7B }; |
| 32 | +static uint8_t test_nv_digest[WOLFBOOT_SHA_DIGEST_SIZE]; |
| 33 | +static uint32_t captured_exponent; |
| 34 | + |
| 35 | +int keyslot_id_by_sha(const uint8_t* pubkey_hint) |
| 36 | +{ |
| 37 | + (void)pubkey_hint; |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +uint32_t keystore_get_key_type(int id) |
| 42 | +{ |
| 43 | + ck_assert_int_eq(id, 0); |
| 44 | + return AUTH_KEY_RSA2048; |
| 45 | +} |
| 46 | + |
| 47 | +uint8_t *keystore_get_buffer(int id) |
| 48 | +{ |
| 49 | + ck_assert_int_eq(id, 0); |
| 50 | + return test_hdr; |
| 51 | +} |
| 52 | + |
| 53 | +int keystore_get_size(int id) |
| 54 | +{ |
| 55 | + ck_assert_int_eq(id, 0); |
| 56 | + return (int)sizeof(test_hdr); |
| 57 | +} |
| 58 | + |
| 59 | +int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx, word32 inSz, |
| 60 | + const byte** n, word32* nSz, const byte** e, word32* eSz) |
| 61 | +{ |
| 62 | + (void)input; |
| 63 | + (void)inSz; |
| 64 | + |
| 65 | + *inOutIdx = 0; |
| 66 | + *n = test_modulus; |
| 67 | + *nSz = sizeof(test_modulus); |
| 68 | + *e = &test_exponent_der[1]; |
| 69 | + *eSz = 3; |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +int wolfTPM2_LoadRsaPublicKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, |
| 74 | + const byte* rsaPub, word32 rsaPubSz, word32 exponent, |
| 75 | + TPM_ALG_ID scheme, TPMI_ALG_HASH hashAlg) |
| 76 | +{ |
| 77 | + (void)dev; |
| 78 | + (void)key; |
| 79 | + (void)rsaPub; |
| 80 | + (void)rsaPubSz; |
| 81 | + (void)scheme; |
| 82 | + (void)hashAlg; |
| 83 | + |
| 84 | + captured_exponent = exponent; |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +int wolfTPM2_SetAuthHandle(WOLFTPM2_DEV* dev, int index, |
| 89 | + const WOLFTPM2_HANDLE* handle) |
| 90 | +{ |
| 91 | + (void)dev; |
| 92 | + (void)index; |
| 93 | + (void)handle; |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +int wolfTPM2_SetAuthSession(WOLFTPM2_DEV* dev, int index, |
| 98 | + WOLFTPM2_SESSION* tpmSession, TPMA_SESSION sessionAttributes) |
| 99 | +{ |
| 100 | + (void)dev; |
| 101 | + (void)index; |
| 102 | + (void)tpmSession; |
| 103 | + (void)sessionAttributes; |
| 104 | + return 0; |
| 105 | +} |
| 106 | + |
| 107 | +int wolfTPM2_UnsetAuth(WOLFTPM2_DEV* dev, int index) |
| 108 | +{ |
| 109 | + (void)dev; |
| 110 | + (void)index; |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +int wolfTPM2_UnsetAuthSession(WOLFTPM2_DEV* dev, int index, |
| 115 | + WOLFTPM2_SESSION* tpmSession) |
| 116 | +{ |
| 117 | + (void)dev; |
| 118 | + (void)index; |
| 119 | + (void)tpmSession; |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv, |
| 124 | + word32 nvIndex, byte* dataBuf, word32* pDataSz, word32 offset) |
| 125 | +{ |
| 126 | + (void)dev; |
| 127 | + (void)nv; |
| 128 | + (void)nvIndex; |
| 129 | + (void)offset; |
| 130 | + ck_assert_uint_eq(*pDataSz, WOLFBOOT_SHA_DIGEST_SIZE); |
| 131 | + memcpy(dataBuf, test_nv_digest, WOLFBOOT_SHA_DIGEST_SIZE); |
| 132 | + *pDataSz = WOLFBOOT_SHA_DIGEST_SIZE; |
| 133 | + return 0; |
| 134 | +} |
| 135 | + |
| 136 | +const char* wolfTPM2_GetRCString(int rc) |
| 137 | +{ |
| 138 | + (void)rc; |
| 139 | + return "mock"; |
| 140 | +} |
| 141 | + |
| 142 | +int ConstantCompare(const byte* a, const byte* b, int length) |
| 143 | +{ |
| 144 | + int diff = 0; |
| 145 | + int i; |
| 146 | + |
| 147 | + for (i = 0; i < length; i++) { |
| 148 | + diff |= a[i] ^ b[i]; |
| 149 | + } |
| 150 | + return diff; |
| 151 | +} |
| 152 | + |
| 153 | +#include "../../src/tpm.c" |
| 154 | + |
| 155 | +static void setup(void) |
| 156 | +{ |
| 157 | + memset(test_hdr, 0x42, sizeof(test_hdr)); |
| 158 | + memset(test_modulus, 0x5A, sizeof(test_modulus)); |
| 159 | + memset(test_nv_digest, 0x7C, sizeof(test_nv_digest)); |
| 160 | + captured_exponent = 0; |
| 161 | +} |
| 162 | + |
| 163 | +START_TEST(test_wolfBoot_check_rot_rejects_oversized_keystore_auth) |
| 164 | +{ |
| 165 | + uint8_t hint[WOLFBOOT_SHA_DIGEST_SIZE]; |
| 166 | + int rc; |
| 167 | + |
| 168 | + memcpy(hint, test_nv_digest, sizeof(hint)); |
| 169 | + |
| 170 | + rc = wolfBoot_check_rot(0, hint); |
| 171 | + |
| 172 | + ck_assert_int_eq(rc, BAD_FUNC_ARG); |
| 173 | +} |
| 174 | +END_TEST |
| 175 | + |
| 176 | +static Suite *tpm_suite(void) |
| 177 | +{ |
| 178 | + Suite *s; |
| 179 | + TCase *tc; |
| 180 | + |
| 181 | + s = suite_create("TPM RoT auth"); |
| 182 | + tc = tcase_create("wolfBoot_check_rot"); |
| 183 | + tcase_add_checked_fixture(tc, setup, NULL); |
| 184 | + tcase_add_test(tc, test_wolfBoot_check_rot_rejects_oversized_keystore_auth); |
| 185 | + suite_add_tcase(s, tc); |
| 186 | + return s; |
| 187 | +} |
| 188 | + |
| 189 | +int main(void) |
| 190 | +{ |
| 191 | + Suite *s; |
| 192 | + SRunner *sr; |
| 193 | + int failed; |
| 194 | + |
| 195 | + s = tpm_suite(); |
| 196 | + sr = srunner_create(s); |
| 197 | + srunner_run_all(sr, CK_NORMAL); |
| 198 | + failed = srunner_ntests_failed(sr); |
| 199 | + srunner_free(sr); |
| 200 | + return failed == 0 ? 0 : 1; |
| 201 | +} |
0 commit comments