Commit Graph

577 Commits

Author SHA1 Message Date
Stefan Berger
ae15c8c76b tpm12: Use TPM_PrintFourLimit where <= 4 bytes may exist
Use TPM_PrintFourLimit in those cases where less than 4 bytes of
valid data may exist. Hashes, nonces, encrypted data, and others
typically have more than 4 bytes but data read from NVRAM or
to be encrypted data may have less.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-26 15:00:17 -05:00
Stefan Berger
1d9abaf99d tpm2: Sanitize values read from TPM state stream (Coverity)
Sanitize some of the values read from the TPM state stream.
All Coverity discoveries seem to be false positives.

Coverity doesn't like to see array_size being used in the loop even
though it was compared against ARRAY_SIZE() before. We solve this by
using ARRAY_SIZE() as the loop limit now rather than array size.

Compare seed.b.size against PRIMARY_SEED_SIZE even though this is
already being done in TPM2B_Unmarshal().

The num_bytes parameter is sanitized via a comparison involving a
sum over a sum of values, but Coverity doesn't seem to detect this.
Then we have to use it as a loop limit. I don't see another way.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-23 07:28:19 -05:00
Stefan Berger
31d4c1dcb6 tpm2: Prevent a potential buffer overrun (Coverity)
Prevent a potential buffer overrun by checking that EVP_DecryptUpdate()
has not overrun the buffer it was passed in, so this overrun should
never occurr unless EVP_DecryptUpdate() was wrong. Also the pAssert above
it should have taken care of it already.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-23 07:28:19 -05:00
Stefan Berger
f19282b243 tpm2: Initialize keyschedule before AES_set_encrypt_key (overity)
Coverity complains that the *output* variable passed to
AES_set_encrypt_key contains uninitialized bytes, so we initialize
the variables now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-23 07:28:19 -05:00
Stefan Berger
25223a2d0e tpm2: Initialize variable and check rc before accessing nrh.size (Coverity)
Coverity complains that nrh may not be initialize when copying nrh.size
from it into the buffer pointer to by nrhp. So resolve this by clearing
nrh at the beginning of the loop and checking 'rc' after the Unmarshal.

Previously we could have copied an uninitialized nrh.size but would have
propagated the rc error code from UINT32_Unmarshal(), so this fix doesn't
really change anything.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-23 07:28:19 -05:00
Stefan Berger
e271498466 rpm/debian: Add 0.7.5 entries to changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-18 07:49:07 -05:00
Stefan Berger
d13e628f9e CHANGES: Update CHANGES file for 0.7.5
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-18 07:49:07 -05:00
Stefan Berger
153a9579e8 tpm2: Add comment to buggy RSAAdjustPrimeCandidate
This patch adds a description to the buggy RSAAdjustPrimeCandidate
implementation that can only be properly fixed after 0.7.x.

Reported-by: Nicolas Iooss <nicolas.iooss@ledger.fr>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-18 07:49:07 -05:00
Stefan Berger
8001ad0b96 tpm2: Return properly sized array for b parameter for NIST P521 (HLK)
This patch ensures that the leading zeros in the b parameter for NIST P521
are being kept so that HLK accepts the returned parameters from
TPM2_ECC_Parameters. Now 66 bytes are reported for 'b' rather than only 65.
Do the same for the 'a' parameter, though that one was properly reported
already because it didn't have any leading zeros.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-15 19:13:11 -05:00
Stefan Berger
b2e3991057 tpm2: Fix typos and error reporting inconsitencies in NVMarshal.c
This patch addresses issue #177 by fixing some typos and error
reporting inconsistencies (how structures are spelled) in NVMarhsal.c.

Reported-by: Nicolas Iooss <nicolas.iooss@ledger.fr>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-15 13:01:10 -05:00
Stefan Berger
f1740791c3 tpm2: Address issues detected by cppcheck (false positives)
cppcheck has detected the following issues in 2 functions. However,
neither one of the out-of-bounds array access can happen with the
existing code (see comments in patch).

src/tpm2/Session.c:399:5: note: After for loop, slotIndex has value 3
    for(slotIndex = 0; slotIndex < MAX_LOADED_SESSIONS; slotIndex++)
    ^
src/tpm2/Session.c:414:15: note: Assuming condition is false
    if(result != TPM_RC_SUCCESS)
              ^
src/tpm2/Session.c:419:15: note: Array index out of bounds
    s_sessions[slotIndex].occupied = TRUE;
              ^
src/tpm2/Session.c:591:27: error: Array 's_sessions[3]' accessed at index 3, which is out of bounds. [arrayIndexOutOfBounds]
    MemoryCopy(&s_sessions[slotIndex].session, session, sizeof(SESSION));
                          ^
src/tpm2/Session.c:571:5: note: After for loop, slotIndex has value 3
    for(slotIndex = 0; slotIndex < MAX_LOADED_SESSIONS; slotIndex++)
    ^
src/tpm2/Session.c:581:8: note: Assuming condition is false
       && contextIndex != s_oldestSavedSession)
       ^
src/tpm2/Session.c:591:27: note: Array index out of bounds
    MemoryCopy(&s_sessions[slotIndex].session, session, sizeof(SESSION));
                          ^

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-01-02 20:38:26 -05:00
Stefan Berger
1df79fddec tpm2: Fix negate overflow error (UBSAN)
Fix a negate overflow error.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-25 12:21:48 -05:00
Stefan Berger
e2b71ca032 tpm2: Fix issue with misaligned address when marshalling NVRAM (UBSAN)
UBSAN detects possibly misaligned address when reading out of the
TPM 2's NVRAM and when writing back into it. The NV_RAM_HEADER may
be unaligned like this:

tests/test_tpm2_save_load_state_3.log:tpm2/Marshal.c:117:29: \
  runtime error: load of misaligned address 0x7ffcb53b3bca for type 'UINT32', which requires 4 byte alignment

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-25 12:21:48 -05:00
Stefan Berger
ff8af1d9e1 build-sys: Build libtpms v0.7.5
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-25 12:21:48 -05:00
Stefan Berger
590ce162a8 debian: Add missing line in changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-02 13:54:36 -05:00
Stefan Berger
984ad238de debian: Fix typo in changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-01 19:32:05 -05:00
Stefan Berger
2452a24dab rpm/debian: Add 0.7.4 entry to changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-30 15:24:40 -04:00
Stefan Berger
87bb1758fc CHANGES: Update CHANGES file for 0.7.4
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-30 15:24:40 -04:00
Stefan Berger
937cdade6d build-sys: Build libtpms v0.7.4
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-30 15:24:40 -04:00
Stefan Berger
6f247f7105 tpm2: Add utilities for debugging of constant time issues
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-30 11:27:32 -04:00
Stefan Berger
92adc76acf tpm2: Add Ec signing related changes to consttime.txt notes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-30 11:27:32 -04:00
Stefan Berger
089485035f tpm2: EcSM2: Enforce that the random bnK has no leading zeros
Make sure that the value of bnK is not short so that the subsequent
BnEccModMult() runs in constant time. We take the same approach as with
the modifications to BnEccGenerateKeyPair() where we request bnK to have
all bytes set (no leading zeros that will be cut away) in case the order
of the curve is as byte boundary. In the other cases we add the order
to bnK, which creates bnK1, which we then use for BnEccModMult's scalar
parameter.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Suggested-by: Charanjit Jutla <csjutla@us.ibm.com>
Reviewed-by: Charanjit Jutla <csjutla@us.ibm.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-30 11:27:32 -04:00
Stefan Berger
5c224b8d98 tpm2: EcSchnorr: Enforce that the OpenSSL-generated bnD has no leading zeros
To avoid a potential side channel in the EcSchnorr signing algorithm,
enforce that the OpenSSL-generated bnD does not have leading zeros
that may then cause a timing side channel in the BnEccModMult() operation.

We modified BnEccGenerateKeyPair() so it calls BnEccModMult with a scalar
of constant number of bytes (for a particular curve):

In this version of BnEccGenerateKeyPair we take a dual approach to constant
time requirements: For curves whose order is at the byte boundary, e.g.
NIST P224/P256/P384, we make sure that bnD has all bytes set (no leading zeros)
so that OpenSSL BIGNUM code will not reduce the number of bytes and the
subsequent BnEccModMult() would run faster for a shoter value. For all other
curves whose order is not at the byte boundary, e.g. NIST P521, we simply
always add the order to bnD and call BnEccModMult() with the result bnD1,
which leads to the same result.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Suggested-by: Charanjit Jutla <csjutla@us.ibm.com>
Reviewed-by: Charanjit Jutla <csjutla@us.ibm.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-30 11:27:32 -04:00
Stefan Berger
8583927139 tpm2: Leave notes in code about Nonces that may have leading zeros
Some parameters in the EC code may have leading zeros without causeing
a timing side channel.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-30 11:27:32 -04:00
Stefan Berger
785ad4d03f tpm12: Add a note to RSA related to EVP conversion for constant-time
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-23 23:06:46 -04:00
Stefan Berger
ecc2e95929 tpm12: Use EVP functions for encryption
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-23 23:06:46 -04:00
Stefan Berger
51d1ca8ce4 tpm12: Use EVP functions for decryption
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-23 23:06:46 -04:00
Stefan Berger
03d13d5f19 tpm12: Set BN_FLG_CONSTTIME to select constant time computations
Set BN_FLG_CONSTTIME on the sensitive parts of the RSA key to
select constant time computations.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-23 23:06:46 -04:00
Stefan Berger
5ea3e6183d Travis: Add python3 dependencies for swtpm test
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-23 23:06:46 -04:00
Stefan Berger
b6f1a15268 Travis: Run additional IBM TSS2 related test; use Bionic
Run some additional IBM TSS2 related tests for better code
coverage. We need to switch to Bionic to get the tss2 package.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-23 23:06:46 -04:00
Stefan Berger
bc3534a39c tpm2: Set BN_FLG_CONSTTIME to select constant time computations
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-23 23:06:46 -04:00
Stefan Berger
d00c3b2785 tpm2: Fix compilation error in TPM2B_CREATION_DATA_Marshal (Fedora 32/s390x)
This patch fixes the following compilation error on Fedora 32 / s390x:

tpm2/Marshal.c: In function 'TPM2B_CREATION_DATA_Marshal':
tpm2/Marshal.c:95:19: error: 'sizePtr' may be used uninitialized in this function [-Werror=maybe-uninitialized]
   95 |      (*buffer)[0] = (BYTE)((*source >> 8) & 0xff);
      |      ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tpm2/Marshal.c:2201:11: note: 'sizePtr' was declared here
 2201 |     BYTE *sizePtr;
      |           ^~~~~~~

The error is a false positive since sizePtr will have been initialized if
UINT16_Marshal() is called.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-17 15:21:00 -04:00
Stefan Berger
59e4c1eca1 tpm12: Fix compilation error for Fedora 32 / s390x
tpm12/tpm_nvram.c: In function 'TPM_Process_NVWriteValue':

tpm12/tpm_nvram.c:2313:45: error: 'd1NvdataSensitive' may be used uninitialized in this function [-Werror=maybe-uninitialized]

 2313 |   if ((d1NvdataSensitive->pubInfo.permission.attributes & TPM_NV_PER_WRITEALL) &&
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~

This compiler error is a false positive since the above statement is inside
this if clause:

   if ((returnCode == TPM_SUCCESS) && !done && !dir) {

However, if d1NvdataSensitive was not set then returnCode is
either != TPM_SUCCESS    OR
- case index0 = FALSE             : dir = TRUE per line 2106    OR
- case index0 = TRUE (nvIndex = 0): done = TRUE per line 2215.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-17 15:21:00 -04:00
Stefan Berger
f46924da42 tpm2: Add missing prototypes for function added to Marshal.c
This patch fixes the following compilation error detected (only) on
Fedora 32/s390x:

tpm2/Marshal.c:1129:1: error: no previous prototype for 'TPMI_TDES_KEY_BITS_Marshal' [-Werror=missing-prototypes]
 1129 | TPMI_TDES_KEY_BITS_Marshal(TPMI_TDES_KEY_BITS *source, BYTE **buffer, INT32 *size)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~
tpm2/Marshal.c:1370:1: error: no previous prototype for 'TPMS_SIG_SCHEME_SM2_Marshal' [-Werror=missing-prototypes]
 1370 | TPMS_SIG_SCHEME_SM2_Marshal(TPMS_SIG_SCHEME_SM2 *source, BYTE **buffer, INT32 *size)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
tpm2/Marshal.c:1798:1: error: no previous prototype for 'TPMS_SIGNATURE_SM2_Marshal' [-Werror=missing-prototypes]
 1798 | TPMS_SIGNATURE_SM2_Marshal(TPMS_SIGNATURE_SM2 *source, BYTE **buffer, INT32 *size)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-17 15:21:00 -04:00
Stefan Berger
1d392d466a rpm/debian: Add 0.7.3-1 entry to changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-07-09 14:26:39 -04:00
Stefan Berger
efa4185592 CHANGES: Update CHANGES file for 0.7.3
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-07-09 14:26:39 -04:00
Stefan Berger
94b13010b4 tpm2: fix PCRBelongsTCBGroup for PCClient (bugfix)
Fix PCRBelongsTCBGroup by adjusting the set of PCRs that belong to the TCB
Group. The effect of this is that PCR changes to PCR 16 (for example) do
not change the pcrUpdateCounter anymore. The effect *should not* have any
negative side effects when using the TPM.

We also need to update the test cases that now show a different
pcrUpdateCounter in the responses. Also 'swtpm' test cases need
to be fixed to expect the changed result.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:48:04 -04:00
Stefan Berger
998323fe37 build-sys: Build libtpms v0.7.3 2020-06-29 08:48:04 -04:00
Stefan Berger
7325acb477 rpm/debian: Add 0.7.2-1 entry to changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 15:54:05 -04:00
Stefan Berger
5e9d4a7c12 CHANGES: Update CHANGES file for 0.7.2
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 15:54:05 -04:00
Stefan Berger
b3125ffbbe tpm2: Restrict setting the PSS salt length to the digest length
In corner cases where the size of the salt and the size of the hash
to sign + 2 exceed the signature size we cannot use the salt length =
hash length but have to resort to using the maximum possible salt
length.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 15:54:05 -04:00
Stefan Berger
5bfdd8eba4 tpm2: Set the PSS salt length to the digest length
Call EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, -1) when creating an RSA
signature to set the PSS salt length to the digest length. Without
this call we previously set the salt length to the maximum
permissible value, but this is not how TPM 2 implements it.

Per interoperability testing between signatures created previously
with the max. permissible value and the new code, which does not
modify the signature verification code, old signatures still verify.
New signatures also verify.

This patch may solve interoperability with hardware TPMs that signatures
created following this patch now verify on hardware TPMs as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:24:35 -04:00
Stefan Berger
93ae920a9c tpm2: Always use a temporary buffer for decryption
Always use a temporary buffer large enough to meet the requirements of the
EVP_DecryptUpdate() call.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:24:35 -04:00
Stefan Berger
fe6eb5d6c2 tpm2: Add call to EVP_CIPHER_CTX_set_padding(ctx, 0) in sym. decryption
Add missing call of EVP_CIPHER_CTX_set_padding(ctx, 0) in the symmetric
decryption case. This was missing and failed some decryption cases.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:24:35 -04:00
Stefan Berger
5c1ed86de0 tpm2: Add missing input size check for CryptSymmetricDecrypt
Add a missing input size check for CryptSymmetricDecrypt so that we return
the proper error code TPM_RC_SIZE in case the input size is not a multiple
of the block size. Before TPM_RC_FAILURE was returned.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:24:35 -04:00
Stefan Berger
40cfe134c0 tpm2: Fix output buffer parameter and size for RSA decryption
For the RSA decryption we have to use an output buffer of the size of the
(largest possible) RSA key for the decryption to always work.

This fixes a stack corruption bug that caused a SIGBUS and termination of
'swtpm'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-23 13:45:02 -04:00
Stefan Berger
6050bf5899 build-sys: Build libtpms 0.7.2
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-23 09:37:05 -04:00
Stefan Berger
8fe99d1fd0 tpm2: Fix a gcc 10.1.0 complaint
This PR addresses issue 133: https://github.com/stefanberger/libtpms/issues/133

bin/sh ../libtool  --tag=CC   --mode=compile x86_64-pc-linux-gnu-gcc \
  -DHAVE_CONFIG_H -I. -I..    -include tpm_library_conf.h \
  -I../include/libtpms -I../include/libtpms -fstack-protector-strong \
  -D_POSIX_ -DTPM_POSIX -DTPM_LIBTPMS_CALLBACKS -I ./tpm2 \
  -I ./tpm2/crypto -I ./tpm2/crypto/openssl -g -O2 \
  -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 \
  -DUSE_OPENSSL_FUNCTIONS_ECDSA=1 -DUSE_OPENSSL_FUNCTIONS_RSA=1 \
  -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign \
  -c -o tpm2/libtpms_tpm2_la-NVDynamic.lo `test -f 'tpm2/NVDynamic.c' \
  || echo './'`tpm2/NVDynamic.c
libtool: compile:  x86_64-pc-linux-gnu-gcc -DHAVE_CONFIG_H -I. \
  -I.. -include tpm_library_conf.h -I../include/libtpms \
  -I../include/libtpms -fstack-protector-strong -D_POSIX_ -DTPM_POSIX \
  -DTPM_LIBTPMS_CALLBACKS -I ./tpm2 -I ./tpm2/crypto \
  -I ./tpm2/crypto/openssl -g -O2 -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 \
  -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=1 \
  -DUSE_OPENSSL_FUNCTIONS_RSA=1 -Wall -Werror -Wreturn-type -Wsign-compare \
  -Wno-self-assign -c tpm2/NVDynamic.c  -fPIC -DPIC \
  -o tpm2/.libs/libtpms_tpm2_la-NVDynamic.o
tpm2/NVDynamic.c: In function ?NvNextByType?:
tpm2/NVDynamic.c:126:10: error: ?nvHandle? may be used uninitialized in this function [-Werror=maybe-uninitialized]
  126 |  *handle = nvHandle;
      |  ~~~~~~~~^~~~~~~~~~
tpm2/NVDynamic.c: At top level:

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-20 12:59:58 -04:00
Stefan Berger
84fd561166 rpm/debian: Add 0.7.1-1 entry to changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-18 12:18:57 -04:00
Stefan Berger
e1a2239cae CHANGES: Update CHANGES file for 0.7.1
Update the CHANGES file for 0.7.1

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-18 12:18:57 -04:00