libtpms/tests/object_size.c
Stefan Berger bb4dafb1e7 tpm2: NVMarshal: Introduce MAX_MARSHALLED_OBJECT_SIZE
Introduce the #define MAX_MARSHALLED_OBJECT_SIZE to be used for a size of
a byte buffer that is supposed to hold a marshalled OBJECT. The number
is not exact but provides for a 'safe' size of a buffer.

Add a test case to check that MAX_MARSHALLED_OBJECT_SIZE is sufficient.
The test case needs access to ANY_OBJECT_Marshal, which is only available
when -static can be used for linking since otherwise the function is
private to the library. Static linking the test case does not work when
test-coverage is enabled, therefore disable statically linked test cases
in when test-coverage is enabled and give control to the user to disable
statically linked test cases in other cases as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-05 21:13:57 -05:00

87 lines
2.8 KiB
C

#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
*/
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,
};
/* this buffer must only be filled to <= MAX_MARSHALLED_OBJECT_SIZE bytes */
BYTE buffer[2 * MAX_MARSHALLED_OBJECT_SIZE];
BYTE *buf = buffer;
INT32 size = sizeof(buffer);
UINT32 written;
written = ANY_OBJECT_Marshal(&object, &buf, &size);
if (written > MAX_MARSHALLED_OBJECT_SIZE) {
fprintf(stderr,
"Expected flattened OBJECT to have %zu bytes, but it has %u.\n",
MAX_MARSHALLED_OBJECT_SIZE, written);
return EXIT_FAILURE;
}
fprintf(stdout, " written = %d < MAX_MARSHALLED_OBJECT_SIZE = %zu\n",
written, MAX_MARSHALLED_OBJECT_SIZE);
return EXIT_SUCCESS;
}