File: object_size.c

package info (click to toggle)
libtpms 0.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,188 kB
  • sloc: ansic: 120,340; makefile: 829; sh: 336; cpp: 125
file content (101 lines) | stat: -rw-r--r-- 3,327 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <assert.h>
#include <stdlib.h>

#include "Tpm.h"

int main(void)
{
    /* This is supposed to be the OBJECT that requires the most bytes
     * when it is marshalled: currently an RSA key
     */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-braces"
    OBJECT object = {
        .attributes = {
            .occupied = SET,
        },
        .publicArea = {
            .type = TPM_ALG_RSA,
            .nameAlg = TPM_ALG_SHA512,
            .objectAttributes = 0,
            .authPolicy.t = {
                .size = sizeof(object.publicArea.authPolicy.t.buffer),
            },
            .parameters.rsaDetail = {
                .symmetric = {
                    .algorithm = TPM_ALG_AES,
                    .keyBits = 256,
                    .mode = TPM_ALG_ECB,
                },
                .scheme = TPM_ALG_RSAPSS,
                .keyBits = MAX_RSA_KEY_BITS,
                .exponent = 0x10001,
            },
            .unique.rsa.t = {
                .size = sizeof(object.publicArea.unique.rsa.t.buffer),
            },
        },
        .sensitive = {
            .sensitiveType = TPM_ALG_RSA,
            .authValue.t = {
                .size = sizeof(object.sensitive.authValue.t.buffer),
            },
            .seedValue.t = {
                .size = sizeof(object.sensitive.seedValue.t.buffer),
            },
            .sensitive.rsa.t = {
                .size = sizeof(object.sensitive.sensitive.rsa.t.buffer),
            },
        },
        .privateExponent = {
            .Q = {
                .size = CRYPT_WORDS(BITS_TO_BYTES(MAX_RSA_KEY_BITS / 2)),
            },
            .dP = {
                .size = CRYPT_WORDS(BITS_TO_BYTES(MAX_RSA_KEY_BITS / 2)),
            },
            .dQ = {
                .size = CRYPT_WORDS(BITS_TO_BYTES(MAX_RSA_KEY_BITS / 2)),
            },
            .qInv = {
                .size = CRYPT_WORDS(BITS_TO_BYTES(MAX_RSA_KEY_BITS / 2)),
            },
        },
        .qualifiedName.t = {
            .size = sizeof(object.qualifiedName.t.name),
        },
        .evictHandle = 0x12345678,
        .name.t = {
            .size = sizeof(object.name.t.name),
        },
        .seedCompatLevel = 1,
    };
#pragma GCC diagnostics pop
    static const size_t exp_sizes[7] = {
        0, 2580, 2580, 2580, 2580, 2580, 2584,
    };
    BYTE buffer[2 * MAX_MARSHALLED_OBJECT_SIZE];
    UINT32 stateFormatLevel;
    UINT32 written;
    INT32 size;
    BYTE *buf;

    for (stateFormatLevel = 1; stateFormatLevel <= 6; stateFormatLevel++) {
        /* this buffer must only be filled to <= MAX_MARSHALLED_OBJECT_SIZE bytes */
        buf = buffer;
        size = sizeof(buffer);

        g_RuntimeProfile.stateFormatLevel = stateFormatLevel;

        written = ANY_OBJECT_Marshal(&object, &buf, &size, &g_RuntimeProfile);
        if (written != exp_sizes[stateFormatLevel]) {
            fprintf(stderr,
                    "Expected flattened OBJECT to have %zu bytes, but it has %u.\n",
                    exp_sizes[stateFormatLevel], written);
            return EXIT_FAILURE;
        }
        fprintf(stdout, "  stateFormatLevel: %d   written = %d  < MAX_MARSHALLED_OBJECT_SIZE = %zu\n",
                stateFormatLevel, written, MAX_MARSHALLED_OBJECT_SIZE);
    }
    return EXIT_SUCCESS;
}