Commit Graph

1258 Commits

Author SHA1 Message Date
Stefan Berger
ffd93ef183 WIP: tpm2: Add RuntimeAttribute fips-140-3 to restrict TPM 2 (StateFormatLevel 8)
Add RuntimeAttribute 'fips-140-3' that restricts a TPM 2 following
requirements of the FIPS-140-3 standard:

- Prevents raw RSA encryption and decryption
- Prevents SHA1 signature generation + verification
- Prevents derivation of ECC keys from derivation parent and for creation
  of ephemeral keys
- Performs a pairwise consistency test on RSA keys and ECC signing keys
- Enables continous DRBG test

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 15:11:32 -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
774cee962e tpm2: Restrict profile names to 32 characters
Restrict profile names to 32 characters to avoid having to carry
excessively long names in the TPM's state file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-10 08:42:36 -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
57074695bc tpm2: Mark unused function parameters to avoid static analyzer warnings
Some function parameters are unused due to OpenSSL usage and other changes
by libtpms. Mark those as unused to avoid static analyzer warnings.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-03 14:35:16 -04:00
Stefan Berger
cbd894e281 tpm2: Add fallthrough comments to avoid static analyzer warnings
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-03 14:35:16 -04:00
Stefan Berger
648cc1ec78 tpm2: Wrap asprintf to avoid static analyzer warnings
To avoid static analyzer warnings due to non-literal format strings being
used, wrap asprintf in TPMLIB_asprintf and call vasprintf from there.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-03 14:35:16 -04:00
Stefan Berger
21e19ffe8d tpm2: Run PCT test on RSA keys and EC signing keys: pct
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-03 14:06:33 -04:00
Stefan Berger
81fbc10a7e tpm2: Implement pairwise consistency test for RSA keys
Implemewnt a pairwise consistency test for RSA keys that is to be enabled
with FIPS_COMPLIANT #define temporarily. Test encryption+decryption and
sign+verify with the created key on random input data.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-03 14:06:33 -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
363cbae3b0 tpm2: Allow naming of custom profiles with prefix 'custom:'
Allow the name of custom profiles to also have the prefix 'custom:'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-01 10:56:52 -04:00
Stefan Berger
6adb99a42c tpm2: Add an assert(profileJSON) since it must never be NULL
When profileJSON is passed to String_Marshal it must never be NULL.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 09:32:02 -04:00
Stefan Berger
ab3da80dec tpm2: Compare CONTEXT_ENCRYPT_ALG against ALG_AES_VALUE
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 09:32:02 -04:00
Stefan Berger
2a883017d6 tpm2: Remove unused function parameters or mark them as unused
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 09:32:02 -04:00
Stefan Berger
8b4ad203d0 tpm2: Correct the ending of the string at max characters
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 09:32:02 -04:00
Stefan Berger
b97c3bb776 tpm2: Check for n < 0 from asprintf before return
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 09:32:02 -04:00
Stefan Berger
27ceda7173 tpm2: Cast void * to BYTE * when doing arithmetic
To avoid warnings from the static analyzer cast void * to BYTE *
when doing arithmetic.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 09:32:02 -04:00
Stefan Berger
3b5b842e16 tpm2: Use mgs parameter in error log message
Use unused msg parameter in error log message

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 09:32:02 -04:00
Stefan Berger
11dfb9a611 tpm2: Check context hash algorithm with static assert
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 21:17:21 -04:00
Stefan Berger
8b3efff19e tpm2: Add const qualifier to char array in StringToUint32
Only constant strings will be passed to StringToUint32 and therefore
add the const qualifier.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 21:17:21 -04:00
Stefan Berger
e6c1ac7062 tpm2: Declare constant string as const char *
Only constant strings will be assigned to the static array, so make the
name field also a const char *.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 21:17:21 -04:00
Stefan Berger
3b0bc31692 build-sys: Set -Wshadow to avoid shadowing variables
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 19:39:55 -04:00
Stefan Berger
7cbdd6113d tpm2: Rename variable to avoid clashes with type and shadowing
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 19:39:55 -04:00
Stefan Berger
0903c69105 tpm2: Rename block_skip to block_skip_t to avoid shadowing
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 19:39:55 -04:00
Stefan Berger
a811c35fca tpm2: Rename buffer to objbuf to avoid shadowing buffer variable
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 19:39:55 -04:00
Stefan Berger
e898872637 tpm2: Adjust selection of StateFormatLevel
When a non-modifyable profile is chosen then copy the StateFormatLevel
(SFL) from the internal profile as before. A reason for copying the SFL
is also because the user is not allowed to make modifications to this
type of profile. Otherwise, if the user chooses a modifyable profile,
then let the user choose the StateFormatLevel.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-13 17:04:38 -04:00
Stefan Berger
46548da8ed tpm2: Add missing break statement
Add a missing break statement that was forgotten in recent
commit b389781f49.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-11 19:01:26 -04:00
Stefan Berger
c7baa7e1ac tpm2: Deduplicate verbs in Commands, Algorithms and Attributes in profile
Deduplicate verbs in Commands, Algorithms, and Attributes strings in a
profile and when a verb with an '=' sign is found, such as
ecc-min-size=224, and there is a duplicate later in the string, such as
ecc-min-size=256, then keep the last one.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-11 16:14:08 -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
209b9adcd3 tpm2: Use OpenSSL-based KDFe implementation if possible
Use the OpenSSL-based KDFe implementation when USE_OPENSSL_FUNCTIONS_SSKDF
is set.

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
37ca5d3ddb build-sys: Check for availability of SSKDF (KDFe) in OpenSSL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-09 14:56:44 -04:00
Stefan Berger
d635ef81e2 tpm2: Prevent HMAC creation & verification with sha1: no-sha1-hmac
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-09 14:43:05 -04:00
Stefan Berger
24afa414ff tpm2: Prevent HMAC verification with sha1: no-sha1-hmac-verification
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-09 14:43:05 -04:00
Stefan Berger
b389781f49 tpm2: Prevent HMAC creation with sha1: no-sha1-hmac-creation
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-09 14:43:05 -04:00
Stefan Berger
e983cdf05c tpm2: Display RuntimeAttributes as part of TPMLIB_GetInfo
Display RuntimeAttributes as part of TPMLIB_GetInfo when the flag
'128' (0x80) is set.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-03 10:28:55 -04:00
Stefan Berger
38a2ad9e1b tpm2: Implement RuntimeAttributesGet to print out attributes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-03 10:28:55 -04:00
Stefan Berger
e197df642b tpm2: Implement attribute for FIPS-enabled host: fips-host
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-03 10:28:55 -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
2a296082f0 tpm2: Implement function checking attributeFlags
Implement function to check whether a profile requires attribute
flags to be 'enforced'.

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
16e648e98f tpm2: Implement TPMLIB_WasManufactured API call
Implement TPMLIB_WasManufactured API call for a TPM 2 so that callers can
detect whether a TPM 2 instance was newly created and therefore the
profile that was set was also applied.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-30 16:25:47 -04:00
Stefan Berger
2dc1af12e5 rev180: Add missing entry to sieveMarks array
Add the missing entry to the sieveMarks array. The new entry would only
be used for RSA 3072 keys but due to the following change in
RsaAdjustPrimeLimit it will not be used.

  primeLimit = s_LastPrimeInTable - 2;  // libtpms: Fix for 3072 bit keys to avoid mark=5

If it was to be used (above change removed) it would occasionally produce
different RSA 3072 prime numbers from the TPM's seeds and therefore any
change to the above will have to depend on the SEED_COMPAT_LEVEL so that
the same keys are always produced.

Use the full sieveMarks array to generate RSA 3072 keys when
SEED_COMPAT_LEVEL > SEED_COMPAT_LEVEL_RSA_PRIME_ADJUST_PREREV169,
otherwise keep the previous adjustment to avoid mark=5.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-21 10:54:23 -04:00
Stefan Berger
c63fd3f765 rev183: Sync ActGetCapabilityData with upstream
Since go.preservedSignaled is not available due to __ACT_DISABLED being defined
apply the same conditional code enablement here.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-20 15:28:21 -04:00
Stefan Berger
046ee612ac rev183: Sync _plat__Signal_PowerOff with upstream
Since ACT_SUPPORT is set to YES in libtpms this change has no effect.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-20 15:28:21 -04:00
Stefan Berger
89092a12f9 rev183: Update TPM_SPEC_VERSION and related defines to reflect rev183
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-20 13:17:28 -04:00