summaryrefslogtreecommitdiffstats
path: root/tests/tpm2_selftest.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 21:41:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 21:41:43 +0000
commit92cccad89d1c12b39165d5f0ed7ccd2d44965a1a (patch)
treef59a2764cd8c50959050a428bd8fc935138df750 /tests/tpm2_selftest.c
parentInitial commit. (diff)
downloadlibtpms-upstream/0.9.2.tar.xz
libtpms-upstream/0.9.2.zip
Adding upstream version 0.9.2.upstream/0.9.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/tpm2_selftest.c')
-rw-r--r--tests/tpm2_selftest.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/tpm2_selftest.c b/tests/tpm2_selftest.c
new file mode 100644
index 0000000..37d95ef
--- /dev/null
+++ b/tests/tpm2_selftest.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <libtpms/tpm_library.h>
+#include <libtpms/tpm_error.h>
+#include <libtpms/tpm_memory.h>
+
+int main(void)
+{
+ unsigned char *rbuffer = NULL;
+ uint32_t rlength;
+ uint32_t rtotal = 0;
+ TPM_RESULT res;
+ int ret = 1;
+ unsigned char tpm2_startup[] = {
+ 0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00,
+ 0x01, 0x44, 0x00, 0x00
+ };
+ unsigned char tpm2_selftest[] = {
+ 0x80, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00,
+ 0x01, 0x43, 0x01
+ };
+ const unsigned char tpm2_selftest_resp[] = {
+ 0x80, 0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00,
+ 0x00, 0x00
+ };
+
+ res = TPMLIB_ChooseTPMVersion(TPMLIB_TPM_VERSION_2);
+ if (res) {
+ fprintf(stderr, "TPMLIB_ChooseTPMVersion() failed: 0x%02x\n", res);
+ goto exit;
+ }
+
+ res = TPMLIB_MainInit();
+ if (res) {
+ fprintf(stderr, "TPMLIB_MainInit() failed: 0x%02x\n", res);
+ goto exit;
+ }
+
+ res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
+ tpm2_startup, sizeof(tpm2_startup));
+ if (res) {
+ fprintf(stderr, "TPMLIB_Process(Startup) failed: 0x%02x\n", res);
+ goto exit;
+ }
+
+ res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
+ tpm2_selftest, sizeof(tpm2_selftest));
+ if (res) {
+ fprintf(stderr, "TPMLIB_Process(TPM2_Self) failed: 0x%02x\n",
+ res);
+ goto exit;
+ }
+
+ if (memcmp(rbuffer, tpm2_selftest_resp, rlength)) {
+ fprintf(stderr, "Expected response from TPM2_Selftest is different than received one.\n");
+ goto exit;
+ }
+
+ fprintf(stdout, "OK\n");
+
+ ret = 0;
+
+exit:
+ TPMLIB_Terminate();
+ TPM_Free(rbuffer);
+
+ return ret;
+}