Commit Graph

1457 Commits

Author SHA1 Message Date
Arthur Gautier
2d9b00c4e4 tpm2: fix GCC 15 stringop-overflow error in MakeIv
When compiling with GCC 15 using `CFLAGS=-march=x86-64-v4`, the compiler's
aggressively optimized vectorizer triggers a false-positive
-Wstringop-overflow error. Because x86-64-v4 enables wide AVX-512 registers,
the compiler misinterprets the loop unrolling and warns that a 64-byte
vector write is overflowing the destination buffer:
```
  tpm2/TPMCmd/tpm/src/crypt/AlgorithmTests.c:158:17: error: 
  writing 64 bytes into a region of size 15 [-Werror=stringop-overflow=]
    158 |             *iv = i;
```

This fixes the warning by marking the `iv` output pointer parameter as
`volatile`. This inhibits the over-aggressive loop vectorization on this
specific buffer, silencing the compiler error without changing the
underlying logic.

Signed-off-by: Arthur Gautier <arthur.gautier@arista.com>
2026-06-10 22:31:26 -04:00
Moritz Buhl
e7a4061087 tpm2: LibreSSL doesn't have EC_POINTs_mul.
Signed-off-by: Moritz Buhl <mbuhl@openbsd.org>
2026-06-08 11:02:00 -04:00
Moritz Buhl
2c3ddfea18 build-sys: Fixup prerequisites on OpenBSD, remove outdated casing.
Signed-off-by: Moritz Buhl <mbuhl@openbsd.org>
2026-06-08 11:02:00 -04:00
Moritz Buhl
df74dfee58 build-sys: Add a BSDMake target for converting pod files.
Signed-off-by: Moritz Buhl <mbuhl@openbsd.org>
2026-06-08 11:02:00 -04:00
Moritz Buhl
61051fcb0e tpm2: Remove early error to define RADIX_BITS.
Signed-off-by: Moritz Buhl <mbuhl@openbsd.org>
2026-06-08 11:02:00 -04:00
Moritz Buhl
1965add2e2 tpm12: libtpms on OpenBSD no longer builds with OPENSSL_OLD_API.
Signed-off-by: Moritz Buhl <mbuhl@openbsd.org>
2026-06-08 11:02:00 -04:00
Stefan Berger
5a0be228c4 ci: Move testing into ubuntu:26.04 containers for OpenSSL 3.5
Since swtpm now needs OpenSSL 3.5, move the testing into ubuntu:26.04
containers. Several basic dev packages need to be installed for a
container to be able to build.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-06-05 11:49:41 -04:00
Stefan Berger
06ec934c24 ci: Install cpp-coveralls v0.4.2 from its git repo
Install cpp-coveralls from its git repo to avoid having to use an old
version of setuptools and to get it to work again.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-06-01 11:50:55 -04:00
IWillBurn
73c3e7633f tpm2: Fix false deduplication in RuntimeProfileDedupStrItems
Fix a bug where RuntimeProfileDedupStrItems incorrectly removes a
token that is a suffix of the last token in a comma-separated list.
For example, given "...,<token>,...,other-<token>", the function
would wrongly deduplicate "<token>" out of the list.

The root cause is the (dup[slen] == 0) branch in the boundary check,
which handles the last item in the list but does not verify that the
left boundary of the match is valid:

    if (((dup[-1] == ',' || dup[-1] == 0) && dup[slen] == exp) ||
         (dup[slen] == 0) /* last item in list */)

The fix requires a valid left-side delimiter in all cases:

    if ((dup[-1] == ',' || dup[-1] == 0) &&
         (dup[slen] == exp || dup[slen] == 0))

Signed-off-by: IWillBurn <eugene.boyarnikov@gmail.com>
2026-06-01 10:42:09 -04:00
Stefan Berger
521c51073f tpmlib: Introduce TPMPROP_TPM2_BUFFER_MAX and set it to 4096 for now
In preparation for PQC support, introduce TPMPROP_TPM2_BUFFER_MAX for an
API user (e.g., swtpm) to be able to get the size of the TPM 2 request
and response buffer. Set the new TPM2_BUFFER_MAX to 4096 for now, which is
the same value as TPM_BUFFER_MAX. In the TPM 2 related code replace
TPM_BUFFER_MAX with TPM2_BUFFER_MAX.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-05-06 22:10:01 -04:00
Stefan Berger
31457687d6 tpm2: Adjust min. buffer size and add comment about min and max sizes
The minimum buffer size was set to MAX_CONTEXT_SIZE + 128, which is fine
if the assumption that the actual biggest command or response buffer is
created by TPM2_ContextLoad/Save commands holds. If the assumption was
not holding, then the choice of the minimum buffer size would truncate
response sizes. Adjust this size to the sizeof(TPMS_CONTEXT), which is
80 bytes bigger than MAX_CONTEXT_SIZE but still related to
TPM2_ContextLoad/Save commands. Still add a generous additional 128 bytes
to it.

Add a comment about the maximum size and the sizes used by TIS and CRB.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-04-29 23:04:51 -04:00
Stefan Berger
f85abd2d11 tpm2: Limit the response buffer size indicator to the negotiated size
Limit the variable indicating to the caller how many bytes were returned
in a TPM response to the size returned from TPM2_GetBufferSize(). This
then reflects the buffer size that was negotiated with the
TPMLIB_SetBufferSize() call and for which the recipient of the buffer
should have enough space for.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-04-29 23:04:51 -04:00
Stefan Berger
cea0585f5c tpm2: Check valid range of command code before conversion to index
Check the range of the (untrusted) command code before converting it to
a command index.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-23 19:12:48 -04:00
Stefan Berger
de20bf4779 tpm2: Add missing check for disallowed SHA1 HMAC creation
CryptSelectMac is called (only) from TPM2_HMAC and TPM2_Mac_Start and needs
to check whether the creation of an HMAC with SHA1 is not allowed per the
profile attribute no-sha1-hmac-creation and/or no-sha1-hmac.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-19 17:32:34 -04:00
Stefan Berger
77104fbfbd tpm2: Use marshalled size of OBJECT to see whether it fits into NVRAM
Use the marshalled size of an OBJECT to check whether it still fits into
NVRAM rather than using the size of the OBJECT (sizeof(obj), which is much
bigger. Using the marshalled size also handles the case where the OBJECT is
still copied into NVRAM using memcpy for the NULL profile case of
libtpms v0.9. This should have been converted once NvObjectToBuffer()
was started to be called there.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-10 16:55:08 -04:00
Stefan Berger
5098f2cefe tpm2: Only write the necessary number of bytes of command bitmaps
Only write the necessary number of bytes of the ppList and auditCommands
bitmaps. The entries in both of these bitmaps are set by a command's
index and the necessary number of bytes can be determined by finding the
command with the highest command code that is enabled in the current
profile (1). This in turn can be found by by searching for the last byte
in the enableCommandsByIdx bitmap that has any bit set.

1) It would be possible to skip writing these arrays entirely if they have
   no bits set. The unmarshalling functions would clear the arrays.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-09 15:07:32 -04:00
Stefan Berger
c286d281c6 tpm2: Use __builtin_popcount for counting enabled commands
Use __builtin_popcount for counting the number of enabled commands in the
enabledCommandsByIdx bitmap.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2026-03-09 15:07:32 -04:00
Stefan Berger
78ad37e154 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 15:07:32 -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