Commit Graph

1440 Commits

Author SHA1 Message Date
Stefan Berger
dc665f95cc tpm2: Base enabledCommands on command index rather than command code
Base the enabledCommands array on the command index rather than the command
code since the latter left the first few bytes always unused since command
codes start at 0x11f only.

After this change, ppList, auditCommands, and enabledCommandsByIdx bitmaps
are all based on command indices.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-09 14:12:37 -04:00
Stefan Berger
bc4d5e8de8 tpm2: Reformat some libtpms code with clang-format from 'upstream'
Some checks failed
Coverity Scan / coverity (push) Has been cancelled
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-09 10:10:15 -04:00
Stefan Berger
9dfc8065c9 tpm2: Add comment realted to artificial padding
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-09 10:10:15 -04:00
Stefan Berger
c5bbb701f2 tpm2: Deactivate useless pAssert_RC that always evaluates to true (Coverity)
Coverity complains that the pAssert_RC's always evaluate to true and
are therefore useless. Deactivate them (wait for upstream to remove them).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-06 14:06:52 -05:00
Stefan Berger
ec4b1a7d56 tpm2: Pad NV_INDEX structure for m68k
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 21:07:15 -05:00
Stefan Berger
d86d1942de tpm2: Add padding to structures needed by some architectures
On m68k int's only need to be 2-byte aligned and therefore the size of
some data structures or offsets of fields within data structures is not
as expected. Fix this by adding artificial m68k-specific padding where
necessary. If padding was added on any other architecture, it would not
make a difference there. Similarly, if some day m68k gcc was to align int's
as expected, the artifical padding would not have any influence on the
expected sizes and offsets and could be removed.

With the padding applied, swtpm should now be able to read state written
by other architectures. This is for example the case with swtpm test cases.

Link: https://wiki.debian.org/M68k/Alignment
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 21:07:15 -05:00
Stefan Berger
d284f02a29 tpm2: Add more asserts for offsets and sizes of structures
Before adding padding to the data structures, add asserts for offsets and
sizes of data structures so that none of these will change due to padding.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 21:07:15 -05:00
Stefan Berger
11a66d2611 tests: Fix -fanalyzer complaints for base64decode test case
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 15:29:31 -05:00
Stefan Berger
2c2d6bd29c tests: Extend object_size test to marshal an RSA key OBJECT up to SFL 8
Extend the test with 2 more expected sizes for marhshalled RSA key
OBJECT for up to the current StateFormatLevel 8.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 11:50:05 -05:00
Stefan Berger
2672049313 tests: Deactivate check of sizeof(OBJECT) for expected size on most archs
OBJECTs are only written into NVRAM by using marshalling functions, which
should take the same number of bytes for the same OBJECT on all
architectures. So the increase of the size of an OBJECT is not a concern
anymore. Nevertheless, get 'notification' about the increase of the size
of OBJECTs on x86_64.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 11:50:05 -05:00
Stefan Berger
a3bf0cd230 tests: Remove checks for sizes of ppList and auditCommands
It is not necessary anymore to check for an increase of the sizes of the
PERSISTENT_DATA ppList and auditCommands fields since they are marshalled
with an array-size indicator.

Any previous version of libtpms should be able to resume a profile with
the ppList and auditCommands written with a later version. If later
versions have new commands, then those new commands must be added beyond
the current TPM_CC_FIRST. The ppList and auditCommands may be bigger
than before but can be truncated IFF new commands were added. These
new commands will not be usable with the older verison of libtpms based
on the StateFormatLevel then.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 10:55:30 -05:00
Stefan Berger
190e72838f tpm2: Use BITS_TO_BYTES in NVMarshal.c for calculating array sizes
Use the BITS_TO_BYTES macro where the number of bits is used for
calculating the size of a byte array.

Also, make the clearing of the rest of the byte arrays (ppList and
auditCommands) a bit more efficient than clearing the whole array
before copying the new data into it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 10:55:30 -05:00
Stefan Berger
747e279530 tpm2: Limit array_size to current size of array (ppList/auditCommands) (BUGFIX)
The current libtpms v0.10.2 does not accept a TPM 2 state that was written
with a more recent version of libtpms if the sizes of ppList and/or
auditCommands increased. Remove the asserts that trigger state reading
failures and limit array_size to the sizeof(data->ppList) and
sizeof(data->auditCommands) respectively . More recent versions of libtpms,
if they support more TPM 2 commands, will extend these arrays but those new
commands will not be usable by older versions of libtpms (via profile and
StateFormatLevel) and can therefore be ignored by truncating those arrays.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-05 10:55:30 -05:00
Luca Boccassi
801d8fbbaa tpm2: fix build on x32
openssl defines SIXTY_FOUR_BIT for this architecture

./tpm2/crypto/openssl/tpm_radix.h:74:4: error: #error Need to determine RADIX_BITS value
   74 | #  error Need to determine RADIX_BITS value

Signed-off-by: Luca Boccassi <luca.boccassi@gmail.com>
2026-03-05 08:38:55 -05:00
Stefan Berger
0501a89a29 tpm2: Deactivate bufferSize <= 0 check that prevents bufferSize == 0 (#559)
The check covering bufferSize == 0 causes a compatibility issue as
described in #559 where a valid command is passed.

Also, bufferSize should never be <0 since the preceeding UINT16_Unmarshal()
would check whether sufficient bytes are available for unmarshalling
and would return an error code if this was not the case. Otherwise it would
return TPM_RC_SUCCESS along with the unmarshalled value. The bufferSize
would then only be 0 (as lowest possible value) in the success case.

Reported-by: Daniel Trick <daniel.trick@sit.fraunhofer.de>
Resolves: https://github.com/stefanberger/libtpms/issues/559
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-03 15:26:47 -05:00
Stefan Berger
9787502b16 tpm2: Add fallthrough annotation to end of case statement (Coverity)
Add a fallthrough annotation to suppress a Coverity complaint.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-01 22:49:15 -05:00
Stefan Berger
712ab4d531 tpm2: Filter-out commands by given maximum StateFormatLevel
When the user passes no command profile, then the default command profile
of the (custom) profile will be used. If the user limits the
StateFormatLevel by setting it in the profile, then all those commands from
the default command profile that exceed the given StateFormatLevel have to
be filtered-out to avoid a rejection of the command profile. Therefore, add
a parameter to indicate that filtering of commands should be done and
return a new string with the enabled command codes if any command was
filtered-out.

Add a test case that demonstrates the filtering-out of commands 0x199 and
0x19a (from the default command profile) due to the choice of
StateFormatLevel '2'. Both command codes were enable in StateFormatLevel
'3'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-23 10:49:28 -05:00
Stefan Berger
837e618627 tpm2: Add quote parameter to RuntimeCommandsPrint for optional quoting
Not all callers of RuntimeCommandsPrint will want to have the returned
string use quotation marks at the beginning and end of the string.
Therefore, add a quote parameter to this function that allows to avoid
the quoting. Adapt the single caller to pass a quotation mark.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-23 10:49:28 -05:00
Stefan Berger
c87ad14242 man3: Add sentence about TPM_RETRY returned by tpm_nvram_loaddata()
Add a sentence about the return value of TPM_RETRY that is returned if no
state existed so far.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-23 09:50:42 -05:00
Stefan Berger
9525ac9eeb tpm2: Implement own function for ECC key size checking
Refactor the code to have own function for ECC key size checking. Later on
other algorithms may get their own function as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-21 19:02:50 -05:00
Stefan Berger
5a6cc8bda5 tpm2: Refactor Ecc-specific AlgorithmProfile.c code for reuse
Refactor some Ecc-specific code in AlgorithmProfile.c so it can be reused
for other algorithms later on as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-21 15:43:05 -05:00
Stefan Berger
cfbffd84e7 tests: Properly fill-out data structure for stricter marshalling to work
Properly fill out the TPMS_SIG_SCHEME_RSAPSS structure, especially the
hashAlg that's found in TPMS_SCHEME_HASH, so that stricter marshalling
implementations do not refuse to marshal the structure.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-19 21:41:38 -05:00
Stefan Berger
a039171794 Sync: Sync up the order of some #define's to match 'upstream'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
2d656ec134 Sync: Remove some unused files
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
65b3939400 Sync: Move many files to BSD-2-Clause license
Move many files to the BSD-2-clause license that either
- have no modifications in them at all
- where all modifictions are from 'me'
- where I have permission to move the modifications by 3rd parties under
  the new license

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
e56da27cba Sync: Sync minor changes in several files with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
ec610a6beb tpm2: Split off TDES related parts from TpmToOsslSym.h and adjust licenses
Sync the whitespace differences in TpmToOsslSym.h with upstream.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
947846cdf7 Sync: Add include to simulator_sysheaders.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
96424c2e4c Sync: Fix and extend compile-time testing of NUM_AUTHVALUE_PCR_GROUP
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
8b437483c8 Sync: Fix masking-out of unneeded bits in TpmMath_GetRandomBits
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
1425c9133d tpm2: Adopt the directory structure of upstream code repo
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
6d04f7c7ba Sync: Implement platform function to determine enabled self tests
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
ea44b01eb4 Sync: Introduce platform functions for accessing NV indices
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
0ba905f226 Sync: Replace a printf with DEBUG_PRINT in BnToOsslMath.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
5782eca3bb Sync: Add another fallthrough hint for static analysis tools
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
bad09ff655 Sync: Fix comments
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
b12b69553a Sync: Only build x509 related functions if CC_CertifyX509 is '1'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
fdd6b34a79 Sync: Add more buffer size checks to CryptParameterDecryption
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
de08e7e954 Sync: Add NOT_REFERENCED to avoid build error
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
46e299f905 Sync: Throw a build error if CC_AC_GetCapability is enabled
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
ce6e87c921 Sync: Reformat comments in Global.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
89ba040bb3 Sync: Format TpmProfile_CommandList.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
ec17b40d22 Sync: Report versioning information using a structe passed to a function
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
32941241dc Sync: Check for NULL pointers in CommandDispatcher
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
a90b87fda2 Sync: Add checks for locator == (NV_REF)0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
d7adadf113 Sync: Fix code comments and function descriptions and trivial changes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
915f51db3f Sync: Rework failure handling and replace g_inFailureMode
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
12a50d4362 Sync: Adjust pAssert variant being used depending on function return type
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
2319ccd86e Sync: Have PolicyContextUpdate return TPM_RC
Split-out those TPM 2 command functions that need to be adapted due to the
functions they call returning an error code. Split them out into their own
files so they can be synchronized easier.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00
Stefan Berger
3b0e5c0a91 Sync: Have Object and PCR related functions return TPM_RC
Split-out those TPM 2 command functions that need to be adapted due to the
functions they call returning an error code. Split them out into their own
files so they can be synchronized easier.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-02-13 09:59:26 -05:00