summaryrefslogtreecommitdiffstats
path: root/tests/tpm2_selftest.c
blob: 37d95efcb879103fe4615523dd59f57f59f551f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
}