Commit Graph

900 Commits

Author SHA1 Message Date
Stefan Berger
5662046c0b rev164: Rename DebugFileOpen to DebugFileInit
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
9165880bf7 rev164: Comment, commented code, and whitespace changes only
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
c7d9f0d837 rev164: Rename GetHeriarchy to GetHierarchy
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
a6765d4dfb rev164: Rename unmarshal/marhsalArray to Unmarshal/MarshalArray
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -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
32c159ab53 tpm2: CryptSym: fix AES output IV
The TPM is supposed to provide the output IV in the ivInOut parameter in
CryptSymmetricEncrypt. In the case of using the openssl routines, the
output IV is missed, and the resulting output from the TPM is in the
input IV.

OpenSSL unfortunately does not export EVP_CIPHER_CTX_iv() until
tags/OpenSSL_1_1_0, so we have to fall back to the reference code for
previous OpenSSL versions.

Signed-off-by: William Roberts <william.c.roberts@intel.com>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-01 12:54:27 -05:00
Stefan Berger
610e0e71e6 tpm2: Fix public key context save due to ANY_OBJECT_Marshal usage
This patch addresses the bug reported in issue #195 where the saving of
an externally loaded public key's context doesn't work due to the usage of
ANY_CONTEXT_SAVE for saving key contexts. This patch fixes the issue by
creating local versions of TPM_SENSITIVE_Marshal/_Unmarshal that deals
with the case where sensitiveType is not a type of private key but a
public key instead that basically doesn't have much information in
TPM_SENSITIVE but is all zeros instead.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-26 11:51:28 -05:00
Stefan Berger
ae4c5f780f debian: Add missing pkg-config to debian/control
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-25 19:15:11 -05:00
Stefan Berger
1253aa359b Bump up the revision of the library for next version to 0.9.0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-24 15:08:38 -05:00
Stefan Berger
3fd4b94903 rpm/debian: Adjust date of 0.8.0 entry in changelog
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-24 12:58:38 -05:00
Stefan Berger
6905e8a29c tpm2: Test RSA private keys when -DDO_CHECK_RSA_KEY=1 is used
For some peace-of-mind add a function that allows us to check the RSA keys
that are generated, especially the primary keys that are not generated by
OpenSSL.

Use the following configure line to compile libtpms:

CFLAGS="-DDO_RSA_CHECK_KEY=1" ./autogen.sh --prefix=/usr \
  --with-tpm2 --with-openssl

Start swtpm after installing libtpms:

swtpm socket --tpmstate dir=/tmp/myvtpm --tpm2 --ctrl type=tcp,port=2322  \
  --server type=tcp,port=2321 --flags not-need-init --log level=0

We can now run this test program to check keys by using an RSA primary key
for signing.

export TPM_COMMAND_PORT=2321 TPM_PLATFORM_PORT=2322 \
  TPM_SERVER_NAME=localhost TPM_INTERFACE_TYPE=socsim \
  TPM_SERVER_TYPE=raw

echo "test" > input

swtpm_ioctl --tcp :${TPM_PLATFORM_PORT} -i
tssstartup

while :; do
  for keysize in 2048 3072; do

    tsscreateprimary -rsa $keysize -si -hi n
    tsssign -hk 80000000 -if input
    tssflushcontext -ha 80000000
  done
done

Libtpms has passed multiple hours of testing.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-24 07:14:39 -05:00
Stefan Berger
fb8bc527bc tpm2: Add #error gate for SM3 and SHA3 to NVMarshal.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-23 11:11:24 -05:00
Stefan Berger
ef0a741bd2 tpm2: Add statement about unsupported modifications to TpmProfile.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-23 11:11:24 -05:00
Stefan Berger
9a13be06b4 tpm2: Add comment to HASH_ALIGNMENT define
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-23 11:11:24 -05:00
Stefan Berger
f66a719eda CHANGES: Updated CHANGES document for 0.8.0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-22 12:10:54 -05:00
Stefan Berger
8f284d3594 tpm2: Add define for static_assert in case it doesn't exist in assert.h
Some older systems do not define static_assert, so we have to provide
our own static_assert that does 'nothing'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-22 12:10:54 -05:00
Stefan Berger
d2686dda87 tpm2: Add missing swap defines for FreeBSD
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-22 12:10:54 -05:00
Stefan Berger
b8e883c21d 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-22 09:51:34 -05:00
Stefan Berger
5710d697bf 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-22 09:51:34 -05:00
Stefan Berger
00860abce3 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-22 09:51:34 -05:00
Stefan Berger
b4ef12a69c 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-22 09:51:34 -05:00
Stefan Berger
0f86f8b53b tpm2: Fix a double-free in case of unlikely error (Coverity)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-02-22 09:51:34 -05:00
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