Commit Graph

1178 Commits

Author SHA1 Message Date
Stefan Berger
1e1648fec3 tpm2: Save key and hash contexts using ANY_OBJECT_Marshal
Save key and hash contexts using the ANY_OBJECT_Marshal function and try
to load it using ANY_OBJECT_Unmarshal(). Unfortunately older contexts were
written out as plain OBJECTs, so we have to accomodate this case as well
so that we can restore key contexts from libtpms-0.7.x. We do not support
resuming HASH contexts from libtpms-0.7.x.

Before this modification context files written out by the IBM TSS stack
were 2692 bytes independent of content. Now an RSA 2048 key is 1222 bytes
and a NIST p384 key is 982 bytes.

Several of the original TPM 2 function exporting Sequence state and
importing it can now be disabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-20 15:08:46 -05:00
Stefan Berger
81c507f528 tpm2: Make ANY_OBJECT_Marshal/Unmarshal non-static
Make the functions ANY_OBJECT_Marshal/Unmarshal non-static so that we can
call it from other places. Also allow passing a parameter 'verbose' to the
ANY_OBJECT_Unmarshal function that allows us to call this function without
it logging errors. We need this when trying to load a context from an older
libtpms versions that did not use ANY_OBJECT_Marshal to write out the
OBJECT (but copied it right from memory).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-20 15:08:46 -05:00
Stefan Berger
0319d814f3 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:06 -05:00
Stefan Berger
8b7f8d8e71 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:06 -05:00
William Roberts
901c9a76c7 travis: drop Trusty (14.04) for Xenial (16.04)
PKG_INSTALLDIR is missing from 14.04, so switch to 16.04 to pick it up.

Signed-off-by: William Roberts <william.c.roberts@intel.com>
2021-02-09 13:33:15 -05:00
William Roberts
90cba58180 configure: support --with-pkgconfigdir
Support setting different install paths for package config files
using the --with-pkgconfigdir option.

Drop the hardcoded pkgconfigdir variable in the Makefile.am as per the
manpage http://manpages.ubuntu.com/manpages/cosmic/man7/pkg.m4.7.html
the macro PKG_INSTALLDIR defaults to $libdir/pkgconfig.

Signed-off-by: William Roberts <william.c.roberts@intel.com>
2021-02-09 13:33:15 -05:00
Stefan Berger
eb36ad5a5d 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:26:23 -05:00
Stefan Berger
1ddf6450aa github: Update issues templates
Add bug report template to issues template.
2020-12-27 22:52:39 -05:00
Stefan Berger
92e97766e1 tpm2: Fix negate overflow error (UBSAN)
Fix a negate overflow error.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-11-24 12:52:08 -05:00
Stefan Berger
572af18d5b 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-24 12:52:08 -05:00
Stefan Berger
2e2f854dfe tpm2: Use the define HELPERS_FP_H in Helpers_fp.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-29 23:11:32 -04:00
Stefan Berger
514dc4fa2a tpm2: Add utilities for debugging of constant time issues
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-29 23:11:32 -04:00
Stefan Berger
0b76f19990 tpm2: Add Ec signing related changes to consttime.txt notes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-29 23:11:32 -04:00
Stefan Berger
67e07fd506 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-29 23:11:32 -04:00
Stefan Berger
9b434a5f06 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-29 23:11:32 -04:00
Stefan Berger
c9a8379984 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-29 23:11:32 -04:00
Stefan Berger
80152a229a tpm12: Add a note to RSA related to EVP conversion for constant-time
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-22 20:08:02 -04:00
Stefan Berger
74a8ffd2f0 tpm12: Extend self-test with sign/verify tests
Signed-off-by Stefan Berger <stefanb@linux.ibm.com>
2020-10-22 20:08:02 -04:00
Stefan Berger
d4f4d514a2 tpm12: Use EVP functions for decryption
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-22 20:08:02 -04:00
Stefan Berger
56351430a3 tpm12: Use EVP functions for decryption
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-22 20:08:02 -04:00
Stefan Berger
7b91801f39 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-22 20:08:02 -04:00
Stefan Berger
85fe93a861 tpm2: 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-22 20:08:02 -04:00
Stefan Berger
188e4de138 tpm2: Only call BN_num_bytes once
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-22 20:08:02 -04:00
Stefan Berger
1a58c73bc8 build-sys: Make --with-tpm2 the default and if used choose openssl
Make --with-tpm2 the implicit default now and choosen openssl.
When using --without-tpm2 one has to again choose the crypto-library
which defaults to freebl as before. This type of build seems rather
rare by now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-13 17:02:53 -04:00
Stefan Berger
54ce6b5c65 tpm12: Use USE_FREEBL_CRYPTO_LIBRARY to enable function
A tpm12 function that is only needed with freebl library can
be conditionally enabled with '#if USE_FREEBL_CRYPTO_LIBRARY'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-13 17:02:53 -04: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
Stefan Berger
8df35ac8da build-sys: Fix freebl build of TPM 1.2
The freebl build (TPM 1.2 only) is currently broken:

configure: error: OpenSSL crypto function usage requires openssl as crypto library

Set 'enable_use_openssl_functions=no' in the freebl case to avoid probing
the OpenSSL crypto functions.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-13 17:02:53 -04:00
Stefan Berger
921d6dc92e man: Remove generated man pages
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-10-12 19:51:10 -04:00
Stefan Berger
d41dedca4d build-sys: Use AC_LINK_IFELSE to check for understood linker flags
Use AC_LINK_IFELSE to check whether linker flags like 'now' and
'relro' are understood or remain unused (= useless), and only add them
to the set of HARDENING_LDFLAGS, if they are used by the linker. clang
for example does not seem to use them and Cygwin's linker does not
understand them.

Note: This patch merely improves on the handling of these flags but does
not solve a compilation issue when clang is used, unlike swtpm where
this created issues.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-09-11 12:23:26 -04:00
Stefan Berger
c3e6bb971d Travis: Add python3 dependencies for swtpm test
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-08-28 20:33:09 -04:00
Stefan Berger
781f97a68f tpm2: Fix the returned number in the JSON
The JSON returned by TPM2_GetInfo contains a leading zero in the level.

$> swtpm_ioctl --tcp :10000 --info 1
{"TPMSpecification":{"family":"2.0","level":00,"revision":162+0}}

This patch fixes this to:

$> swtpm_ioctl --tcp :10000 --info 1
{"TPMSpecification":{"family":"2.0","level":0,"revision":162+0}}
2020-08-24 20:47:33 -04:00
Stefan Berger
5d2ae35cfc 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:20:11 -04:00
Stefan Berger
dd8c4f7522 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:20:11 -04:00
Stefan Berger
0f5d791a7d rev162: 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:42:37 -04:00
Stefan Berger
bc60d19203 rev162: Add marshal functions related to ACT
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:42:37 -04:00
Stefan Berger
cafda2e88e rev162: Handle TPM_CAP_ACT in TPM_CAP_Unmarshal
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:42:37 -04:00
Stefan Berger
09bf3ed589 rev162: make union tpmCryptKeySchedule_t a typedef union
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:42:37 -04:00
Stefan Berger
f49621ff3a rev162: Comment fixes and changes to unused code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:42:37 -04:00
Stefan Berger
4f8f6e7259 rev162: Fix typoe in SHA384_OID #define (unused)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:42:37 -04:00
Stefan Berger
0ee6eb18d4 rev162: Fix data type for signaledACT (unused)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:42:37 -04:00
Stefan Berger
082d9b4af8 rev162: Fix order of commands in s_CommandDataArray
This does not affect the proper functioning of the code since all
of the commands at the end of the array are currently disabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-29 08:42:37 -04:00
Stefan Berger
6b65a6bd97 build-sys: Enable automake silent rules
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-18 14:04:59 -04:00
Stefan Berger
d5e419ea34 rpm/debian: Add 0.8.0-1 entry to changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-15 11:55:41 -04:00
Stefan Berger
c762ca4aa6 CHANGES: Update CHANGES file for 0.8.0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-02 09:00:46 -04:00
Stefan Berger
5d7a04c624 [build-sys] Add -Wmissing-prototypes to CFLAGS and clean up
Add -Wmissing-prototypes to CFLAGS and make functions static add #include
where necessary.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-06-01 18:23:07 -04:00
Stefan Berger
cb956cfdeb 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:53:54 -04:00
Stefan Berger
06eff9ee55 tpm2: rev162: Have TPM2 show spec revision 162
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
8ce417c647 tpm2: rev162: Sync up on HASH_ALIGNMENT
Sync up on the #define's for HASH_ALIGNMENT, which does not have much
relevance for the OpenSSL implementation.

The affected 32 or 64 bit align field in the ANY_HASH_STATE doesn't carry
any significance. It can be commented without side effects.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
b10772dbd0 tpm2: rev162: Add (unused) macros for ECC curves
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
40f7362401 tpm2: rev162: Remove CURVE_NAME_DEF from ECC_CURVE (trivial)
Remove CURVE_NAME_DEF field from ECC_CURVE structure and add
  #define CURVE_NAME(N)
so that nothing misses the removed field, which wasn't used
before, either.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00