Commit Graph

79 Commits

Author SHA1 Message Date
Stefan Berger
d4be04707e rev180: Add new function to generate RSA primes (StateFormatLevel 9)
Some time around rev169 a new function to generate RSA prime numbers was
introduced. Use this function for a 'new' SEED_COMPAT_LEVEL_LAST that now
gets value '2'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-06-12 11:21:38 -04:00
Stefan Berger
4dd12eb253 tpm2: Set SFL of default-v2 profile to STATE_FORMAT_LEVEL_CURRENT
To enable RSA-4096 in the default-v2 profile, set the stateFormatLevel
to STATE_FORMAT_LEVEL_CURRENT (8).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-08 20:48:06 -04:00
Dan Streetman
e814a1c731 tpm2: Enable support for 4096 bit RSA keys (with fixes)
- Enable RSA_4096
- Add RSA_4096 to s_KeySizesRSA at stateFormatLevel 8
- Increase STATE_FORMAT_LEVEL_CURRENT to 8
- Update tests for larger object size and increased StateFormatLevel
- In NVMarshal.c replace MAX_RSA_KEY_BITS with old value 3072
  so that the state is acceptable to older versions of libtpms;
  if we wrote 4096, then older versions of libtpms would reject the
  state.
- In NVMarshal.c replace RSA_4096 with '0' so it is acceptable to older
  versions; if we wrote '1', then older versions of libtpms would reject
  the state.

Fixes: #491

Signed-off-by: Dan Streetman <ddstreet@ieee.org>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-07 12:23:44 -04:00
Stefan Berger
f756952283 Revert "tpm2: Enable support for 4096 bit RSA keys"
This reverts commit 63627b335e.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-07 11:45:36 -04:00
Dan Streetman
63d0d49a7f tpm2: Add 'default-v2' profile
The 'default-v2' profile is currently just a copy of the 'default-v1'
profile, other than the change in the .description text to change
'v0.10' to 'v0.11'.

Signed-off-by: Dan Streetman <ddstreet@ieee.org>
2025-05-07 10:38:22 -04:00
Dan Streetman
63627b335e tpm2: Enable support for 4096 bit RSA keys
- Enable RSA_4096
- Add RSA_4096 to s_KeySizesRSA at stateFormatLevel 8
- Increase STATE_FORMAT_LEVEL_CURRENT to 8
- Update tests for larger object size and increased StateFormatLevel

Fixes: #491

Signed-off-by: Dan Streetman <ddstreet@ieee.org>
2025-05-07 10:38:22 -04:00
Stefan Berger
5695736b4f tpm2 + test: Make it compilable on Debian GNU Hurd
The simple changes make it compilable on GNU Hurd where it is currently
failing to build.

Link: https://buildd.debian.org/status/package.php?p=libtpms&suite=sid
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-28 12:16:21 -04:00
Stefan Berger
7e2526a265 tests: Initialize len_cmp to 0 so it has an assigned value
Avoid the following error message due to potentially
uninitialized variable:

base64decode.c:64:20: warning: The right operand of '!=' is a garbage \
  value [core.UndefinedBinaryOperatorResult] <--[clang]

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-11 17:11:19 -05:00
Stefan Berger
fa4f9ab561 tests: Ignore gcc -Wmissing-braces on OBJECT initialization
Have gcc ignore -Wmissing-braces to suppress 15 of these types of
warnings:

/home/stefanb/dev/libtpms/tests/object_size.c:13:21: warning: missing braces around initializer [-Wmissing-braces] <--[gcc]
   13 |     OBJECT object = {
      |                     ^
/home/stefanb/dev/libtpms/tests/object_size.c:13:21: warning: missing braces around initializer [-Wmissing-braces] <--[gcc]
[...]

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-11 17:11:19 -05:00
Stefan Berger
48d52b5e40 tests: Initialize cmd with NULL at top of function
Fix the following gcc warning on Fedora rawhide:

tpm2_cve-2023-1017.c: In function ‘main’:
tpm2_cve-2023-1017.c:169:5: warning: ‘cmd’ may be used uninitialized [-Wmaybe-uninitialized]
  169 |     free(cmd);
      |     ^~~~~~~~~
tpm2_cve-2023-1017.c:125:20: note: ‘cmd’ was declared here
  125 |     unsigned char *cmd = malloc(maxcmdsize);
      |                    ^~~

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-11 17:11:19 -05:00
Stefan Berger
a5248a9859 test: Allow setting Attributes in JSON profile with an empty string
Adjust the regex checking the JSON input to allow for empty string values,
which will be only used by 'Attributes' since they are all optional.
Then, allow the user to provide an empty string with the Attributes in the
JSON like this: {...,"Attributes":"", ...}

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-17 16:30:30 -04:00
Stefan Berger
ecff064e8d tpm2: Implement check to disable ECC key derivation: no-ecc-key-derivation
Per "TCG FIPS 140-3 guidance for TPM 2.0" document the following functions
must prevent an asymmetric ECC key derivation:
- Table 14: TPM2_CreateLoaded
- Table 18: TPM2_ZGen_2Phase
- Table 26: TPM2_Commit
- Table 26: TPM2_EC_Ephemeral

Return TPM_RC_TYPE as a return code to indicate failure of deriving a key.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 11:56:24 -04:00
Stefan Berger
0d8377b1b4 tpm2: Allow setting the minimum HMAC key size: hmac-min-key-size
Allow setting the minimum HMAC key size and add enforcement gates.

Check that the value of hmac=min-key-size given in the profile is not
larger than 1024. This value is taken from the maximum size of
TPM2B_SENSITIVE, which is MAX_SYM_DATA (=128), which can be provided as
key to an HMAC.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-08 15:55:19 -04:00
Stefan Berger
99b52fa982 tpm2: Enable DRBG continous test: drbg-continous-test
drbg-continous-test enables an existing code block that was previously
only enabled when FIPS_COMPLIANT #define was set. This code block
ensures that previous 4 consecutive random numbers do not appear again
at the beginning of a 16-byte block.

Extend an existing test case with this new attribute.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-03 08:52:35 -04:00
Stefan Berger
7d95e9ce41 tpm2: Remove test case for OpenSSL SSKDF to KDFe comparison
Remove the test case and renaming of the reference implementation of
CryptKDFe. Reverting this patch would bring the test case back in case
it was necessary.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-09 14:56:44 -04:00
Stefan Berger
2ff2f84170 tests: Rename reference KDFe implemention to ReferenceCryptKDFe for tests
To still be able to run the KDFe test case when the OpenSSL replacement
for the KDFe is to be used, rename the reference implemention to
ReferenceCryptKDFe that coexists with the CryptKDFe function that then
calls OSSLCryptKDFe.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-09 14:56:44 -04:00
Stefan Berger
c0dcce5afd tests: Add KDFe replacement by OpenSSL 'SSKDF' and test case
Implement a KDFe replacement using OpenSSL's SSKDF and add a test
case that compares the current implementation against the one based
on the OpenSSL SSKDF.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-09 14:56:44 -04:00
Stefan Berger
2d8d6a256c tpm2: Prevent SHA1 signature verification: no-sha1-verification
Prevent SHA1 signature verification like FIPS mode on the host does
by implementing attribute no-sha1-signing.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-03 10:28:55 -04:00
Stefan Berger
2fc551ffbc tpm2: Prevent SHA1 signature generation using new flag: no-sha1-signing
Prevent SHA1 signature generation like FIPS mode on the host does
for RSA and all ECC (ecdsa, ecdaa, ecschnorr, sm2) signing algorithms
by implementing attribute no-sha1-signing.
Since CryptRSASign and CryptEccSign are called from CryptSign the
check for SHA1 can be done there. The other call locations are
from the algorithm test functions where the default hash is SHA512.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-03 10:28:55 -04:00
Stefan Berger
2db51d0f88 tpm2: Prevent unpadded/raw RSA en- and decryption: no-unpadded-encryption
Implement attribute no-unpadded-encrytion to prevent unpadded/raw RSA
encryption and decryption.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-03 10:28:55 -04:00
Stefan Berger
45467a2d83 tpm2: Implement support for RuntimeAttributes (StateFormatLevel 7)
Implement support for RuntimeAttributes which will be provided using
the Attribute key in the map.

Implement a fip-host attribute that at this point does not do much.

Add test case for fips-host attribute.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-03 10:28:55 -04:00
Stefan Berger
f5518e596e tests: Use free rather than TPM_Free (OS/X)
Use free rather than TPM_Free to avoid the following warning:

tpm2_setprofile.c:377:18: warning: passing 'char *' to parameter \
  of type 'unsigned char *' converts between pointers to integer \
  types with different sign [-Wpointer-sign]

        TPM_Free(profile);

../include/libtpms/tpm_memory.h:57:36: note: passing argument to \
  parameter 'buffer' here

void       TPM_Free(unsigned char *buffer);

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-30 18:27:26 -04:00
Stefan Berger
247a100cd8 tpm2: Add ecc-sm2-p256 to all profiles
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-26 22:34:38 -04:00
Stefan Berger
18cffed604 tests: Use %zu for size_t
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-26 22:34:38 -04:00
Stefan Berger
1df35f6c77 tpm2: Change marshalled fields of OBJECT (StateFormatLevel 6)
Bump up the StateFormatLevel to 6 and use it to introduce a new OBJECT
marshalling format version '4' that slighly changes how an OBJECT is
marshalled:

- only marshal the private exponent for an RSA key
- always marshal the new hierarchy field

The marshalling code can still write previous version '3' when an older
StateFormatLevel is used to support backwards compatibility.

Adjust the test cases marshalling an RSA key OBJECT to check against
expected sizes across a series of StateFormatLevels.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-25 11:17:42 -04:00
Stefan Berger
a5977d2018 rev180: Add hierarchy field to OBJECT and use it
Add new field hierarchy to OBJECT and assign values to it and access it.

Beyond the sync with upstream we need to also set the hierachy when
- an older OBJECT is converted to the current OBJECT
- an OBJECT is unmarshalled

Remove ObjectGetHierarchy since it is not needed anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-25 11:17:42 -04:00
Stefan Berger
a76e8802e8 tests: Add test case when user chooses specific StateFormatLevel
Test that a user is able to choose a specific StateFormatLevel that is not
required by any of the chosen commands but enables a bugfix in the TPM 2
code for example.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 08:27:49 -04:00
Stefan Berger
714511aef7 tpm2: Intercept unsupported external and permanent NV indices
Intercept newly added unsupported external and permanent NV indices when
trying to unmarshal and NV index. Add a test case that was using an
unsupported external NV index.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-19 16:04:10 -04:00
Stefan Berger
8e27756625 tpm2: Enable TPM2_PolicyParameters in default-v1 profile
Enable TPM2_PolicyParameters in default-v1 profile and bump up the
StateFormatLevel to '5'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
7d31d5ae8b rev180: Add TPM2_SetCapability and dependencies
This command does not do much at the moment and should therefore
also not be enabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
f1117d4832 rev180: Add TPM2_NV_ReadPublic2 and dependencies
This command would only need to be enabled if TPM2_NV_DefineSpace2 was
enabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-18 16:36:02 -04:00
Stefan Berger
ee141c60e0 tpm2: Enable Camellia-192 and AES-192 and bump up stateFormatLevel
Enable Camellia-192 and AES-192 and bump up the stateFormatLevel to '4'.
This now prevents using this state with previous stateFormatLevels (< 4)
because there Camellia-192 or AES-192 was not enabled and the user would
otherwise not be able to decrypt data with either one if it was usable.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 14:09:09 -04:00
Stefan Berger
341a278896 tpm2: Enable ECC_Decrypt & ECC_Encrypt in 'default' profile
Enable new commands ECC_Decrypt and ECC_Encrypt in the TPmProfile.h
and also in the 'default' profile. Since the additional commands extend
the ppList and auditCommands array, bump up the version of the stateLevel
to '2' and use the new marshalling functions by using the PERSISTENT_DATA
blob_version '5'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 14:09:09 -04:00
Stefan Berger
2803917a09 tpm2: Switch to uncompressed lists and adapt un/marshal functions
Switch the implementation to uncompressed lists (from now on it must
remain uncompressed lists forever) and adapt functions who marshal
and unmarshal arrays that are affected by this switch:

- PERSISTENT_DATA.ppList
- PERSISTENT_DATA.auditCommands

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 14:09:09 -04:00
Stefan Berger
bda457104e tests: Add test case for TPMLIB_SetProfile()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-15 11:51:38 -04:00
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
Stefan Berger
5679903b34 rev180: Rearrange order of TPMI_ECC_CURVE_P_UNMARSHAL in unmarshalArray
After adding BOOL allowNull parameter to TPMI_ECC_CURVE_Unmarshal rearrange
the order in which this command is listed in the unmarshalArray so that the
caller passes an addtional parameter to this function now.

Also add a fuzzer test case.

Fixes: 44f0cb0535 ("rev180: Sync TPMI_ECC_CURVE_Unmarshal")
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-02-28 11:04:12 -05:00
Stefan Berger
23ab02a1c2 tests: oss-fuzz.sh: Append -fno-sanitize=bounds to CFLAGS
The new TPM 2 code casts bigNum's to Crypt_Int, which has an array 'd' of
type 'crypt_uword_t[2]' which then leads to the following types of errors
that we haven't found another solution for so far.

Running: /mnt/scratch0/clusterfuzz/bot/inputs/fuzzer-testcases/crash-2675de6341d4e056d04ab49179b8e5b8bd456589
	/src/libtpms/src/tpm2/crypto/openssl/CryptPrime.c:377:20: runtime error: index 23 out of bounds for type 'crypt_uword_t[2]' (aka 'unsigned long[2]')
 #0 0x5b4196 in RsaAdjustPrimeCandidate_New libtpms/src/tpm2/crypto/openssl/CryptPrime.c:377:20
 #1 0x5b4196 in RsaAdjustPrimeCandidate libtpms/src/tpm2/crypto/openssl/CryptPrime.c:405:9
 #2 0x5b4366 in TpmRsa_GeneratePrimeForRSA libtpms/src/tpm2/crypto/openssl/CryptPrime.c:454:6
 #3 0x590953 in CryptRsaGenerateKey libtpms/src/tpm2/crypto/openssl/CryptRsa.c:1433:9

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-15 14:53:19 -05:00
Stefan Berger
6e95c68503 tests: Add test cases for CVE-2023-1017 and CVE-2023-1018
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-02 09:32:38 -05:00
Stefan Berger
ddbc966ccc tests: Refactor repetitive test scripts to use one common script
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-02 09:32:38 -05:00
Stefan Berger
eb8a76807a tests: Use double quotes on command parameters (shellcheck)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-02 09:32:38 -05:00
orbea
0c2bc32a21 tests: Fix the build with slibtool
When building the tests with `make check` and slibtool the tests will
then all fail to load libtpms.so.0.

  $ ./base64decode
  /tmp/libtpms/tests/.libs/base64decode: error while loading shared libraries: libtpms.so.0: cannot open shared object file: No such file or directory

This happens because they are linked with -ltpms rather than the
libtpms.la file which has unexpected results with slibtool. GNU libtool
does some magic to make this work while slibtool fails to link the
dependency.

The correct way to link internal dependencies is directly with the
libtool archive (.la) files where the -lfoo linker flags should be only
used with external dependencies. Additionally -no-undefined is added to
the LDFLAGS to ensure there aren't undefined references in the future.

Note:

* This doesn't happen if libtpms is installed to the system and the tests
  find the already installs libtpms rather than the newly built library.

* GNU libtool silently ignores -no-undefined, but slibtool will respect
  it.

Signed-off-by: orbea <orbea@riseup.net>
2022-07-16 22:32:30 -04:00
Stefan Berger
f1e8b6c694 tests: Add license identifier to test cases
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-07-12 11:30:53 -04:00
Stefan Berger
4e5dffc08a tests: Fix outdated output of expected file size in case of error
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-07-12 11:30:53 -04:00
Stefan Berger
e56316617c tests: fuzz: Store initialized permall state blob
We have to store the permall state blob once it has been initialized since
otherwise some fields are not having proper values in the internal state.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 22:23:16 -04:00
Stefan Berger
a6c9836af5 tests: fuzz: Have state suspended and resumed after every fuzz step
Have the TPM 2's state suspended and resumed at every step to
ensure that we can marshal and unmarshal it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 17:37:00 -04:00
Stefan Berger
70ebecdf6a tests: fuzz: Register callbacks to avoid creating NVChip file
Register callbacks so that we don't create the NVChip file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 17:37:00 -04:00
Stefan Berger
a59db1faea tests: fuzz: Call die() when an error from an API call was returned
Call die() causing as assert() to be triggered if an API call
returned an unexpected failure result.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 17:37:00 -04:00
Nicolas Iooss
bbd7b75d71 Fix many misspellings
When testing downgrading from libtpms 0.8 to 0.7 (which is not
possible), the error message which is reported is:

    libtpms/tpm2: Unexpect value for MAX_RSA_KEY_BITS; its value 3072 is
    not = 2048; (version: 2).

codespell (https://github.com/codespell-project/codespell) reports a
misspelling for "Unexpect", which should be "Unexpected". As the project
contains many more misspellings in comments, error messages and
documentation, fix all misspellings reported by codespell.

Signed-off-by: Nicolas Iooss <nicolas.iooss@ledger.fr>
2021-03-02 08:20:25 -05:00
Stefan Berger
5b8471a921 build-sys: Only build TPM 2 tests if WITH_TPM2 is set
Conditionally enable TPM 2 test cases to build and run if WITH_TPM2
is set.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-13 17:02:53 -04:00