Commit Graph

997 Commits

Author SHA1 Message Date
Stefan Berger
1522d293f5 tpm2: Enable ALG_CAMELLIA in TpmProfile.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-06 13:42:06 -04:00
Stefan Berger
d78e6e3879 tpm2: Add more compile-time constants to array
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-06 13:42:06 -04:00
Stefan Berger
5e97c2e78d tpm2: Make exp. size of compile-time constants dependent on header version
Make the expected array size of compile-time constants dependent on
the version of the header. This way we can add elements to the array
while bumping up the version of the header.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-06 13:42:06 -04:00
Stefan Berger
a9657246e2 tpm2: Extend asym key test case data generation script with sm4
Extend the test case data generation script with sm4. Since several
distros' openssl do not support sm4, we need to test for whether sm4
is supported by the installed openssl.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-02 12:03:23 -04:00
JerryDevis
4c19adce30 Update SymmetricTestData.h
update the selftest data in dataOut_SM4_CTR

Signed-off-by: JerryDevis <seclab@huawei.com>
2021-09-02 10:55:05 -04:00
Marc-André Lureau
057242b84d build-sys: add optional --with-tpm1
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-06 08:04:37 -04:00
Marc-André Lureau
e60c35ecf9 Move common debug, memory & nvfile units to src/
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-06 08:04:37 -04:00
Stefan Berger
aa579cee5b tpm2: Use EC_POINT_set/get_affine_coordinates on OpenSSL >= 1.1
Use the EC_POINT_set/get_affine_coordinates function on OpenSSL >= 1.1.
These function are a 1:1 replacement for the
EC_POINT_set/get_affine_coordinates_GFp functions and are available
since OpenSSL 1.1 and are deprecated in OpenSSL 3.0.

This patch addresses one aspect of the OpenSSL 3.0 issues raised in
issue #215.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-04 08:57:20 -04:00
Stefan Berger
7899608f2a tpm2: Enable supported AES bit size cases using #if AES_<size>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-29 19:15:51 -04:00
Stefan Berger
8b889f50fe tpm2: Enable supported Camellia bit size cases using #if Camellia_<size>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-29 19:15:51 -04:00
Stefan Berger
034a5c0248 tpm2: NVMarshal: Handle index orderly RAM without 0-sized terminating node
The NVRAM entries in s_indexOrderlyRam array do not need to contain a
0-sized terminating node. Instead, the entries may fill up this 512
byte array so that no NV_RAM_HEADER structure fits anymore. The fact
that no more NV_RAM_HEADER structure fits is also an indicator for the
last entry. We need to account for this in the code marshalling and
unmarshalling the entries so that we stop marshalling the entries
then and similarly stop unmarshalling.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-23 15:54:58 -04:00
Stefan Berger
17255da54c tpm2: Initialize a whole OBJECT before using it
Initialize a while OBJECT before using it. This is necessary since
an OBJECT may also be used as a HASH_OBJECT via the ANY_OBJECT
union and that HASH_OBJECT can leave bad size inidicators in TPM2B
buffer in the OBJECT. To get rid of this problem we reset the whole
OBJECT to 0 before using it. This is as if the memory for the
OBJECT was just initialized.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-23 15:54:58 -04:00
Stefan Berger
71b33cb9e0 tpm2: Use #if ALG_CAMELLIA rather than #if 0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-22 13:21:45 -04:00
Stefan Berger
98b34147ad tpm2: Avoid compiler warning by using memcpy instead of MemoryCopy (gcc 10.3)
Fix the following compiler warning from gcc 10.3.0 by using memcpy
instead of MemoryCopy (fixes issue #229).

tpm2/NVDynamic.c: In function 'NvRamGetEnd':
tpm2/NVDynamic.c:378:12: warning: function may return address of local variable [-Wreturn-local-addr]
  378 |     return iter;
      |            ^
tpm2/NVDynamic.c:339:26: note: declared here
  339 |     NV_RAM_HEADER        header;
      |                          ^
tpm2/NVDynamic.c: In function 'NvRamGetIndex':
tpm2/NVDynamic.c:411:12: warning: function may return address of local variable [-Wreturn-local-addr]
  411 |     return currentAddr;
      |            ^
tpm2/NVDynamic.c:339:26: note: declared here
  339 |     NV_RAM_HEADER        header;
      |                          ^

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-07-09 16:21:16 -04:00
Stefan Berger
2e6173c273 tpm2: Restore original value if unmarsalled value was illegal
Restore the original value of the memory location where data from
a stream was unmarshalled and the unmarshalled value was found to
be illegal. The goal is to not keep illegal values in memory.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-24 09:08:44 -04:00
Stefan Berger
7981d9ad90 tpm2: Add maxSize parameter to TPM2B_Marshal for sanity checks
Add maxSize parameter to TPM2B_Marshal and assert on it checking
the size of the data intended to be marshaled versus the maximum
buffer size.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-23 11:51:34 -04:00
Stefan Berger
2f30d620d3 tpm2: Reset TPM2B buffer sizes after test fails for valid buffer size
Reset the buffer size indicator in a TPM2B type of buffer after it failed
the test for the maximum buffer size it allows. This prevents having bad
buffer sizes in memory that can come to haunt us when writing the volatile
state for example.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-23 11:51:34 -04:00
Stefan Berger
be5fabf155 tpm2: Consume padding bytes in TPM2_ContextLoad() (Win2k19, issue #217)
Windows 2019 Server padds the TPM_ContextLoad() command with additional
bytes up to TPM_PT_MAX_OBJECT_CONTEXT for the TPMS_CONTEXT part. Since
libtpms does not use an OBJECT to serialize the keys (anymore) it now
uses less bytes than the MAXimum of TPM_PT_MAX_OBJECT_CONTEXT bytes and
the padding leaves some unconsumed bytes that end up failing the command
since no left-over bytes are allowed in any command.

When unconsumed bytes are left in TPMS_CONTEXT_Unmarshal() we check that
the original passed in size was that of TPM_PT_MAX_OBJECT_CONTEXT and
only then consume the additional padding bytes. Luckily only one command
calls TPMS_CONTEXT_Unmarshal() so that no unwanted side effects should
occur anywhere else, such as no bytes left for unmarshalling the next
structure.

The wisdom behind the padding is not quite clear but it feels like
ill-fixing the code to work around a Windows 2019 server bug...

This patch fixes issed #217

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-06-01 06:39:35 -04:00
Marc-André Lureau
96383e1e9c build-sys: leave CFLAGS/LDFLAGS for user to be defined
This allows user to set specific flags during compilation, without
overriding configure-time cflags necessary for compilation.

See also:
https://www.gnu.org/software/automake/manual/html_node/User-Variables.html
https://www.gnu.org/software/automake/manual/html_node/Flag-Variables-Ordering.html

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-05-18 12:21:31 -04:00
Stefan Berger
db1fd5948b tpm2: Switch to UINT16 for CONTEXT_SLOT and 64k context gap
This patch addresses issue #209.

The context gap for libtpms is currently only 0xff due to the CONTEXT_SLOT
being a UINT8. To extend this to 0xffff, we need to define the CONTEXT_SLOT
as UINT16 and introduce a global variable s_ContextArrayMask that takes on
two valid values, 0xff for simulating the CONTEXT_SLOT when it was UINT8
and 0xffff for usage with the new CONTEXT_SLOT of type UINT16. All
occurrences of casts to CONTEXT_SLOT are replaced with a macro
CONTEXT_SLOT_MASKED that applies this mask to a value instead of using the
cast. We also use it for some calculations to avoid spilling over from
1 byte into 2 bytes for example. The cast with the new code is the same as
applying the mask 0xffff, and using the 0xff mask we can simulate the old
CONTEXT_SLOT (1 byte), which we need for seamlessly resuming old state. We
switch from the 0xff mask to the 0xffff mask when the TPM is reset.

There's one place where the s_ContextArrayMask is initialized to 0xff, and
this is when we resume 'old' STATE_RESET_DATA. The places where it is
intialized to 0xffff are in TPM_Manufacture() and
TPM_SessionStartup(SU_CLEAR), both of which are not called after resuming
state.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-05-13 08:53:26 -04:00
Stefan Berger
251d9a12c3 tpm2: Surround all occurrences of BLOCK_SKIP_READ() with tests of 'rc'
Do not call BLOCK_SKIP_READ once rc has been set to any error value.
Therefore, surround all occurrences of BLOCK_SKIP_READ() with tests
of 'rc'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-05-04 14:23:35 -04:00
Stefan Berger
fdb9ad3b73 tpm2: Implement a cache for the private exponent D and prime Q
Implement a cache for the private exponent 'D' and prime 'Q' so that we
do not have to recalculate 'Q' and 'D' every time an RSA key is used. For
a cache hit we now use ~34000 cycles and on a cache miss it needs around
130000 cycles. Previously it needed around 100000 cycles to calcuate 'Q'
and 'D'. Assuming that keys will be reused and the cache is big enough
for the number of keys being use (64 entries), it seems well worth it.

This solution is better than extending the OBJECT with 'D' since OBJECT is
kept in the TPM's NVRAM and we would then need more memory to store OBJECTs
there.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-23 15:39:27 -04:00
Stefan Berger
23b27060b6 rev164: Bump spec version, year, and day to rev164
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
cc2accea2b rev164: Introduce FOR_EACH_SYM and use it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
941c8ebdc8 rev164: Sync TpmToOsslHash.h SM3_256 support
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
f01a41e5cf rev164: Adjust SMAC_IMPLEMENTED define
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
b15b2e6527 rev164: Sync DebugHelpers.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
9137a773d8 rev164: Change AGL_XYZ_VALUE to equivalend TPM_ALG_XYZ
$ grep -En "ALG_[A-Z]*_VALUE" src/tpm2/TpmTypes.h
71:#define     ALG_ERROR_VALUE             0x0000
72:#define TPM_ALG_ERROR                   (TPM_ALG_ID)(ALG_ERROR_VALUE)
73:#define     ALG_RSA_VALUE               0x0001
74:#define TPM_ALG_RSA                     (TPM_ALG_ID)(ALG_RSA_VALUE)
[...]

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
97d2df2510 rev164: Introduce FOR_EACH_HASH and use it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
6c18509583 rev164: Change return types from BOOL to bool
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
3620198c54 rev164: Use multiple BOOLs to evaluate rather than single one
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
ef0bc94f83 rev164: Remove unreachable return statement
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
dafac1e49a rev164: Fix a comparison (< to <=) in unused code (bugfix?)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
04d7ac7ca2 rev164: sync NvFileSize.c 2021-03-19 15:59:52 -04:00
Stefan Berger
ec45043361 rev164: Resolve long chain of logical ands (trivial)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
ade739ce51 rev164: Add (unused) SM3 and SHA3 related digests to HashTestData
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
Stefan Berger
fdde809489 rev164: Add (unused) SHA3 related defines for CONTEXT_HASH_ALGORITHM
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-03-19 15:59:52 -04:00
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
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
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
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
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
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
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
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
Stefan Berger
e9c1c15fbd tpm2: rev162: Sync code related to NVChip file size
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
deef829344 tpm2: rev162: Removed unused CryptDataEcc.c file
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
f0e2424a38 tpm2: rev162: Add (unused) macros for bignums 2020-05-27 07:45:29 -04:00
Stefan Berger
77222e4314 tpm2: rev162: Add support for new (unsed) ECC decrypt/encrypt functions 2020-05-27 07:45:29 -04:00
Stefan Berger
1a28680af7 tpm2: rev162: Add unused field anyKdf to union TPMU_KDF_SCHEME
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
1f7202decd tpm2: rev162: Cast values produced by initializer to proper type
Cast the values produced by the #define'd initializers to the proper
type.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
e4e403d4c3 tpm2: rev162: Rename (unused) '#if ALG_SM3' to '#if ALG_SM3_256'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
5003daf7a6 tpm2: rev162: Add (unused) SM4 related data structures and prototypes
Add SM4 related data structures and prototypes. We are not using SM4,
so this change is harmless.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
07eb437319 tpm2: rev162: Add #define SYMMETRIC_ALIGNMENT
Add the #define SYMMETRIC_ALIGNMENT that aligns the tpmCryptKeySchedule_t
size. Since this tpmCryptKeySchedule_t only seems to be used as a stack
variable and the alignment field is never accessed nor the size of the
structre taken, it shouldn't affect anything.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
0f553bafb2 tpm2: rev162: Add (unused) RSA_16384
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
133a119892 tpm2: rev162: Reorder functions in Marshal.c to match rev162
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
5819ba69c5 tpm2: rev162: Rename ALG_<xyz>_VALUE to synonymous TPM_ALG_<xyz>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
a4ba69caaa tpm2: rev162: Sync largely unused ACT_spt.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
232b00cef4 tpm2: rev162: Rename MAX_DERIVATION_BITS to TPM_MAX_DERIVATION_BITS
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
198c723f0b tpm2: rev162: Remove inactive block of RADIX_BITS defines
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
d2b31d8140 tpm2: Add missing 'libtpms added' comments 2020-05-27 07:45:29 -04:00
Stefan Berger
01cc2d07b4 tpm2: rev162: Rename TPM_SYM_MODE_FIRST/LAST to SYM_MODE_FIRST/LAST
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
b8883ba55e tpm2: rev162: Use TPM_ALG_LAST rather than synonymous ALG_LAST_VALUE
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
9c757607ce tpm2: rev162: Use TPM_ALG_<asym> rather than synonymous TPM_<asym>_VALUE
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
e9f48a8bd7 tpm2: rev162: Use TPM_ALG_<SYM> rather than synonymous ALG_<SYM>_VALUE 2020-05-27 07:45:29 -04:00
Stefan Berger
44325fb94b tpm2: rev162: Use TPM_ALG_NULL rather than synonymous ALG_NULL_VALUE
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
58fc790f6d tpm2: rev162: Add TPMT_KDF_SCHEME_P_UNMARSHAL to CommandDispatchData.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
b51bd9efdf tpm2: rev162: Refactor CryptMGF1 and rename to CryptMGF_KDF
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
ed6a8d8cbf tpm2: rev162: Sync unused TpmSizeChecks.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
a2f0265879 tpm2: rev162: Surround #include's with #if ALG_XYZ to make conditional
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
1ae74993cf tpm2: rev162: Fix algorithm cap initialize for ECDSA (bugfix?)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
9420f06753 tpm2: rev162: Fix typos related to (unused) SHA3 constants
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
99607d5426 tpm2: rev162: Give anonymous struct the type KDF_STRUCT
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
4521d02dc5 tpm2: rev162: Rename TPM_GENERATED to TPM_CONSTANTS32
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
c3b7295941 tpm2: rev162: Refactor code in ComputeContextProtectionKey
Do some simple code refactoring.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
1e3b70543f tpm2: rev162: Change SCHEME to KDF_SCHEME in datatypes related to KDF
A simple renaming operation.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
05c032777f tpm2: rev162: Trivial whitespace and comment changes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-27 07:45:29 -04:00
Stefan Berger
ae710d3872 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:47 -04:00
Stefan Berger
78de69d2d4 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:47 -04:00
Stefan Berger
2233af9ca1 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:47 -04:00
Stefan Berger
dc1365a77e 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:47 -04:00
Stefan Berger
505ef841c0 tpm2: Fix output buffer parameter and size for RSA decyrption
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 09:36:51 -04:00
Stefan Berger
819caa4be8 tpm2: Remove space after 'IBM' in MANUFACTURER
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-18 11:22:21 -04:00
Stefan Berger
a9900d4128 tpm2: Add missing EC Curve cases in Unmarshal function
Add missing EC Curve cases in Unmarshal function.
Also, don't accept curves that are not usable during runtime because OpenSSL
may not support them.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-15 12:55:43 -04:00
Stefan Berger
9f915067fe tpm2: Runtime filter useable EC Curves before advertising them
Runtime filter useable EC Curves before advertising them as being
supported.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-15 12:55:43 -04:00
Stefan Berger
f3f78c72a5 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-12 14:02:57 -04:00
Stefan Berger
62f66e5c37 tpm2: Prevent RSA 3072 related out-of-bounds access to sieveMarks[5]
PrimeSieve was accessing the sieveMarks array at out-of-bounds index 5
due to a bug in other parts of the code. This patch fixes the issue
and prevents this access by limiting the values that 'next' can take on.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-04 17:56:17 -04:00
Stefan Berger
b7b670c787 tpm2: Fix memory leaks in TPM2_GetInfo()
This patch fixes two memory leaks in the new code in TPM2_GetInfo().

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-04 09:28:30 -04:00
Stefan Berger
e97909d845 tpm2: Add RSAKeySizes field to TPM2_GetInfo() JSON under new flag
Return the RSAKeySizes in the JSON produced by TPM2_GetInfo() under
a new flag with value '4'. This helps higher level tools and users
to easily determine what key sizes are supported for RSA.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-04 08:01:26 -04:00
Stefan Berger
61cb823169 tpm2: Start supporting RSA 3072 keys
Start supporting RSA 3072 keys.

NVMarshal.c: We now accept state that was written by libtpms when RSA keys
sizes were 2048 or are 3072, basically less-or-equal than 3072.

Also increase the NVRAM memory size by ~45 kb to accommodate the worst
case where the USER NVRAM is full of 65 2048 bit persisted keys whose 65
OBJECTs are now expanding and need to again fit into the NVRAM. We have
to add exactly 45760 bytes to accomodate this case. See swtpm test
case test_tpm2_save_load_state_2. 65 * 704 = 45760.

NOTE: BETTER TO NOT BACKPORT!!! MAY NEGATIVELY AFFECT UPGRADE PATH!

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-04 08:01:26 -04:00
Stefan Berger
8016592b34 tpm2: Increase MAX_CONTEXT_SIZE to 2680 for RSA 3072 bit keys
Increase the MAX_CONTEXT_SIZE to 2680 to support the increased context
size when using 3072 bit keys.

NVMarhsal.c: Accept MAX_CONTEXT_SIZE values of less-or-equal the 2680,
which also accepts context sizes of the old value 2474.

NOTE: BETTER TO NOT BACKPORT!

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-05-04 08:01:26 -04:00
Stefan Berger
92d1544b0d tpm2: Add a pAssert after a statement causing unfounded Coverity complaint
This is the Coverity complaint about the line

infoDataSize = TimeGetMarshaled(&infoData);

CID 1402057: Out-of-bounds access (OVERRUN)
1. overrun-buffer-val: Overrunning array infoData of 32 bytes by passing
  it to a function which accesses it at byte offset 255.

TimeGetMarshaled() correctly serializes into &infoData, which is casted to
a buffer and then the data are written into the buffer. Also only 25 bytes,
as indicated by infoDataSize, are used, which is less than sizeof(infoData),
which is 32.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-24 17:19:58 -04:00
Stefan Berger
91b947f41e tpm2: Comment dead code in Unmarshal.c/Marshal.c
This patch comments unreference/dead code in Unmarshal.c/Marshal.c.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-17 09:52:57 -04:00
Stefan Berger
157630bcc4 tpm2: Comment unused hash and RSA related functions
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
82342abebe tpm2: Upgrade advertised revision to rev 159
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e6bbb8755e tpm2: Sync TpmToOsslMath.c with svn repo
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
0fc4f49119 tpm2: Initialize g_daUsed to False on startup (bugfix?)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
5c91611028 tpm2: Deactivate unused BnIsProbablyPrime() and BnGcd()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
dd84e23a2d tpm2: Fix conditional compile of TPM2_Vendor_TCG_Test
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f4c1824ec0 tpm2: ACT: Update CommandDispatchData.h to latest version
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
569a9e03ea tpm2: ACT: Add rest of ACT code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
46c267da85 tpm2: ACT: Extend case statements with FOR_EACH_ACT
Since none of the ACTs are enabled, the FOR_EACH_ACT statements
will all be no-ops.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f4b7a835ee tpm2: ACT: Add (disabled) ACT fields to ORDERLY_DATA and STATE_CLEAR_DATA
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
1f4c70cd3c tpm2: ACT: Extend TPM2_Getcapability with TPM_CAP_ACT
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
213271eed8 tpm2: ACT: #define __ACT_DISABLED if no RH_ACT_* is YES
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
fd41faa54a tpm2: ACT: Add data structures, defines, and un/marshalling code
Disable ACT via the TpmProfile.h's RH_ACT_{0,1,A) settings of NO.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
69537eca7e tpm2: Add comment to MAX_CONTEXT_SIZE in TpmProfile.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
fce3cafa1a tpm2: Have default case return TPM_RC_VALUE rather than FAIL()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
9cf21831ba tpm2: Move CONTEXT_COUNTER defintion to GpMacros.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
2a28b96aac tpm2: Use CONTEXT_SLOT definition from TpmProfile.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
1f686aa8e9 tpm2: Remove unused define of CONTEXT_SLOT in Global.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
fb94dadeca tpm2: Rename TPM_MAX_DERIVATION_BITS to MAX_DERIVATION_BITS
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
57bfde7bf7 tpm2: Include Platform_fp.h in AttestationCommands.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
6c2517355c tpm2: Remove unused RADIX_BITS and BSIZE defines from TpmProfile.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
99e025c3d9 tpm2: Return int rather than BOOL on _plat__* functions
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
69d0950e34 tpm2: Move #defines for hash and sym alignment out of TpmProfile.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
2296a4dd2a tpm2: Comments and whitespace changes only
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
25a9338eb4 tpm2: Assign TPM_ALG_ERROR to hashAlg rather than direct return
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c9bc79eaa5 tpm2: Remove PRIVATE_VENDOR_SPECIFIC_BYTES from GpMacros.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c4d84b994c tpm2: Remove unnecessary check of #defines
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
5f0e2aef9d tpm2: Add missing SM4; do not enable it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
5233aa9e54 tpm2: Add support for SM3 hash; test cases missing
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
15687b63bc tpm2: Add missing Camellia code including test cases
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e226ea4c2d tpm2: Reorganize TpmToOsslSym.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
893dafd7c0 tpm2: Re-introduce RSA_PRIVATE_SIZE and use it in TPM2B_PRIVATE_KEY_RSA
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
bbbaf89572 tpm2: Move NULL pointer check and remove size limit check in CryptRand.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
53d8e0de55 tpm2: Replace TPMI_RH_HIERARCHY_AUTH with .._POLICY in SetPrimaryPolicy_In
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
167c566c4f tpm2: Add TPMI_RH_HIERARCHY_AUTH and unmarshaling code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
292a7660b4 tpm2: Add (unused) TABLE_DRIVEN_MARSHAL code to CommandDispatcher
signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
9c8bbf7777 tpm2: Indicate build switch choices and add for TABLE_DRIVEN_MARSHAL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
8c3dd46150 tpm2: Change order of includes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
6eab7590e8 tpm2: Remove unncessary includes from NVDynamic.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c9b676df07 tpm2: Add casts to some variables
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
58aa852d8a tpm2: Refactor functions in TPMCmdp (logic change)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
0aaae35073 tpm2: Improve handling of broken NVChip file
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
695922df87 tpm2: Sync BnCurveInitialize and BnCurveFree
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
6d3b2f413b tpm2: Have MathLibraryCompatibilityCheck() return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
0be25cbb22 tpm2: Add BIGUM parameter to BigInitialized and use VERIFY in TpmToOsslMath.c
The original TPM 2 version of BIG_INITIALIZED uses a BIGNUM
on the stack and uses the internals of the BIGNUM's data structure
to initialize it. We don't do this here but use only the API
functions and therefor have tp free the BIGNMs create with
BIG_INITIALIZED().

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e180ef05f7 tpm2: Add unused BnNewVariable() to TpmtoOsslMath.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
22208e47cf tpm2: Have OsslToTpmBn return TRUE on success, FALSE on failure
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
d273c4424c tpm2: Reorder things in TpmToOsslMath.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
eb1c8d6b5d tpm2: Fix definition of LIBRARY_COMPATIBILITY_CHECK in TpmBuildSwitches
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
4f30e41379 tpm2: Sync unused TpcserverPosix_fp.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
d56fad94e2 tpm2: Move CERTIFYX509_DEBUG into TpmBuildSwitches.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
2e3fafb013 tpm2: Fix unsigned comparison to use == 0 rather than <= 0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
a5c10693c8 tpm2: Refactor X509_spt.c a bit and capitalize global variables
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
0c18e09b81 tpm2: Sync unused TpmSizeChecks function
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f7c1c7785e tpm2: Remove a #if guarding a typedef in TpmTypes.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
359a61eafd tpm2: OIDs.h: Fix SM2_256 to be SM3_256
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
bb84bfbf36 tpm2: Move CLOCK #defines into PlatformClock.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
2b8ce8d8c1 tpm2: Move variables from PlatformData.c to PlatformData.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
77fc62a7da tpm2: Change #if that makes functions visible
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c2893ed157 tpm2: Add comment regarding ubsan issue solved with casting (TPM2B *)name
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
cd2daec6b0 tpm2: Addition input validation in TPM2_CertifyX509
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
53294e8efa tpm2: Add LIB_EXPORT to _plat__RealTime
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
2685d2b8fd tpm2: replace & in some #defines with &&
Camellia is not enabled at this point, so nothing changes.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
0b0b2f1d08 tpm2: Remove PCR_SELECT_{MAX,MIN} from TpmProfile.h since in GpMacros.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
3948401c55 tpm2: check for TPM_RS_PW rather than session == NULL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
ec85717048 tpm2: Get rid of a variable 2020-04-14 16:03:45 -04:00
Stefan Berger
8265f6e8bd tpm2: x509 certify: rename field qualifyinData to reserved
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e821b4e849 tpm2: Sync unused DebugHelpers
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
d2eee38cd1 tpm2: Include X509.h if CC_Ceritfyx509 is defined
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
ff6ea3b2bd tpm2: Add missing #define and rename crlSign bit to cRLSign
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
da68840e17 tpm2: Surround parameters in define with ()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
0569a67bc5 tpm2: Introduce TPMA_ZERO_INITIALIZER() and use it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
530e2e14fd tpm2: Add commented tpmCryptKeySchedule to tpmCryptKeySchedule_t
Add tpmCryptKeySchedule to union tpmCryptKeySchedule_t. Since this is
causing linker problems on OSX and is not used anywhere at this point,
comment it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
b19e728bb3 tpm2: Code reformatting and comment changes only
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c310987975 tpm2: Remove dead functions and their prototypes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c7e53749ed tpm2: Remove a couple of unused #defines
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
5148fe63d3 tpm2: Rename OIDS.h to OIDs.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
6847901cfa tpm2: Remove dead functions and their prototypes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
52b7c83927 tpm2: rev155: Set spec version to 155
With some x509 related issues fixed, advertise revision 155 now
so that IBM TSS 2 test suite passes the X509 tests.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
db1d01526a tpm2: Some x509 related fixes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
baa5b3995d tpm2: Fix bit ordering related to TPMA_X509_KEY_USAGE
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
9cc5f38c96 tpm2: Add parameter to (unused) TPMA_OBJECT_INITIALIZER
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
521d5948f6 tpm2: Added (not used) #defines
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
1cbee17a16 tpm2: Fix some ASN.1 related code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
b822e395c3 tpm2: Whitespace changes only
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f5e6f48ff7 tpm2: Move some #defines into GpMacros and include headers
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
5ca051d272 tpm2: Update DebugHelpers but deactivate code since not needed
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
b546847a17 tpm2: sync svn 1490; whitespace and comment changes only
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
4903a423ea tpm2: Pass SEED_COMPAT_LEVEL to Object for use when deriving keys
When a child key is derived from an Object, we need to know what
SEED_COMPAT_LEVEL the Object was created under (or what the its seed is)
so that we can later on derive child with the appropriate
SEED_COMPAT_LEVEL.

When an external object is loaded we set the SEED_COMPAT_LEVEL to
SEED_COMPAT_LEVEL_ORIGINAL so that any RSA key that could possibly be
derived from it will always create the same RSA key independent
of the SEED_COMPAT_LEVEL of the hierarchy it is loaded under. This
way any key derivation that could happen on an external key remains
the same before and after this code change.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
480d42bb9f tpm2: rev155: Fix a bug in the RSAES decoder checking bounds
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c1f7bf5509 tpm2: Activate SEED_COMPAT_LEVEL_RSA_PRIME_ADJUST_FIX
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
5b8200d7f5 tpm2: Pass SEED_COMPAT_LEVEL to CryptAdjustPrimeCandidate function
Pass the SEED_COMPAT_LEVEL, originating from the seed that's being used,
to the CryptAdjustPrimeCandidate function and use it to determine
whether the old code should be used or the new one.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
b0ca810347 tpm2: Introduce SEED_COMPAT_LEVEL_RSA_PRIME_ADJUST_FIX but do not use yet
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
625171be0c tpm2: rev155: Add new RsaAdjustPrimeCandidate code but do not use
Add in the new RsaAdjustPrimeCandidate() function but do not use it
so far since it creates slightly different primes than the previous
code and we would get different derived keys if we were to use it with
'old' seeds.

Adjust the code to return the same results for 64 bit and 32 bit machines.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c97d016d27 tpm2: Add SEED_COMPAT_LEVEL to DRBG state
Add a SEED_COMPAT_LEVEL to the DRBG state that is associated with the seed
and indicates the seed compatibility level we need to maintain when deriving
RSA keys from seeds. We only need to be able to handle RSA keys derived via
the DRBG state. Other keys, such as symmetric keys, are not affected. Also
RSA keys cannot be derived from a KDR, so the KDF does not need to carry
the SEED_COMPAT_LEVEL.

All functions that need to pass a value set SEED_COMPAT_LEVEL to
SEED_COMPAT_LEVEL_ORIGINAL (0) for now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
7804d19dac tpm2: Add SEED_COMPAT_LEVEL to nullSeed to track compatibility level
Add SEED_COMPAT_LEVEL to the nullSeed in the state_reset data to track
its compatibility level. We need it for VM suspend and resume.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
eabcd048c4 tpm2: Add SEED_COMPAT_LEVEL to seeds in PERSISTENT_DATA
Add a variable seedCompatLevel to the each seed in the PERSISTENT_DATA
that allows us to track the age of the seed. Whenever a new seed
is created the seedCompatLevel is also written and set to the latest
version. This seedCompatLevel then influences the crypto algorithm that
can be used for deriving keys so that previously derived keys
are now still generated in the same way. When the seed is changed
the old keys are all useless and newly derived keys can then use
the new algorithm.

This patch only sets the variables to the current compatibility
level SEED_COMPAT_LEVEL_ORIGINAL and writes it out as part of the state
file. This makes the state file not downgradeable.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
63f3354d58 tpm2: Remove libtpms-specific setting of g_inFailureMode
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
8cf244fa91 tpm2: rev155: Rework CONTEXT_ENCRYPT_ALG handling in GpMacros.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
cd76780d71 tpm2: rev155: Remove unused #defines from SelfTest.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
3399586357 tpm2: rev155: Add #defines to TpmTypes that are not used in rev155
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
6cff090057 tpm2: rev155: Pass parameters->p.t.size rather than 0 to BnTo2B
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
bf40ccab72 tpm2: rev155: Refactor CryptGetSymmetricBlockSize
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
9215a8277e tpm2: rev155: Avoid FAIL in default case and return a value instead
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e602b6898c tpm2: rev155: Sync commented code in TpmFail.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
de19b5d4ab tpm2: rev155: Add CompilerDependencies to Manufacture_fp.h 2020-04-14 16:03:45 -04:00
Stefan Berger
e8478a7424 tpm2: rev155: Enable new command TPM2_CertifyX509
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f743d7faae tpm2: rev155: Add support for x509 in TPM2_Sign
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
b1bac6d212 tpm2: rev155: Add DebugHelpers and header file
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
a5866291ca tpm2: rev155: Implement new command TPM2_CertifyX509
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
77f452fd14 tpm2: rev155: Add code to create X509 RSA and EC key certificates
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
83221ed68e tpm2: rev155: Add ASN.1 related code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e5a67ccde9 tpm2: rev155: Remove old hash related code and definitions
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
ddba4e3c6b tpm2: rev155: Use MakeDerTag instead of CryptHashGetDer
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
8679241b2f tpm2: rev155: Implement MakeDerTag
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
d098436a34 tpm2: rev155: Extend HASH_DEF with BYTE *OID
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
244cd1bae9 tpm2: rev155: Extend ECC_CURVE with BYTE *OID and adjust code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f402d39d66 tpm2: rev155: Add OIDS.h 2020-04-14 16:03:45 -04:00
Stefan Berger
fd1b23daae tpm2: rev155: Remove CURVE_SPEC #define
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
b6269798bd tpm2: rev155: Consider g_inFailureMode when returning from crypto functions
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c72fded7fc tpm2: rev155: Marshal functions for TPMS_NV_DIGEST_CERTIFY_INFO
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
3dcaa01d0a tpm2: rev155: Major refactoring of header files
- Introduce Platform.h and replace usage of PlatformData.h and
  Platform_fp.h
- Drop Implementation.h since we now use TpmProfile.h (which we
  prepared previously); many #defines were moved to TpmTypes.h

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
296cbb1699 tpm2: rev155: Refactor hash templates; for now also keep old templates
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
19574a4c42 tpm2: rev155: Implement CryptRsaPssSaltSize
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
fa7af853d8 tpm2: rev155: Implement case TPM_ST_ATTEST_NV_DIGEST
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c453ceec08 tpm2: rev155: Implement NvHashIndexData
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e97ac75c95 tpm2: rev155: Reorder #include statements
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
5e4564a286 tpm2: rev155: Add TYPE_OF_TPM_XYZ to TpmTypes.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
44f3d032a5 tpm2: rev155: Introduce #define EXTERN and use it to move vars to Global.h
Move all variables from Global.c into Global.h

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f415f46939 tpm2: rev155: Remove unnecessary #if ALG_RSA and ALG_ECC
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
7e6a43a7f8 tpm2: rev155: Allow PROFILE to be defined and #included
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f4316a7011 tpm2: rev155: Consider return value of DRBG_InstantiateSeeded
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
259d47279b tpm2: rev155: Have DRBG related functions return values not void
Have some DRBG related functions return BOOL or TPM_RC to be
able to handle failure cases. Inside the functions do not
use FAIL anymore but LOG_FAILURE and return FALSE.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e62e6d0c12 tpm2: rev155: Replace FAIL_NOCMD with LOG_FAILURE
Replace FAIL_NOCMD with LOG_FAILURE, which calls into LogFailure,
which in turn calls TpmSetFailureMode, just like before.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f1e92fee8b tpm2: rev155: Implement TpmLogFailure and modify for libtpms
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
7b617b4c2b tpm2: rev155: Refactor MarshalUint16/32 in TpmFail.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
94de67dad7 tpm2: rev155: Remove UnmarshalHeader from TpmFail.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
da96ac6f08 tpm2: rev155: Add LIB_EXPORT to BnCurveFree()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
ba240b3cca tpm2: rev155: Add TpmAlgorithmDefines.h (no users yet)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
a3f7213f75 tpm2: rev155: Add Platform.h (no users yet)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
d216a724c1 tpm2: rev155: Add TpmProfile.h and adjust for libtpms
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
8a0af9b12a tpm2: rev155: Add new file MinMax.h (no users yet)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
772911e36d tpm2: rev155: Have MillerRabin() and RsaCheckPrime() check g_inFailureMode
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
85901749b3 tpm2: rev155: Get rid of #define COMMA in SymmetricTest.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
2ad9e144af tpm2: rev155: Refactor to introduce MATH_LIB_OSSL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
3a899ea39b tpm2: rev155: Implement OsslPush/PopContext and use it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
a06bdadf97 tpm2: rev155: Refactor to introduce SYM_LIB_OSSL 2020-04-14 16:03:45 -04:00
Stefan Berger
a840312536 tpm2: rev155: Give structure name SYMMETRIC_TEST_VECTOR (nit)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
3f12200a41 tpm2: rev155: Sync up unused SMAC_IMPLEMENTED #define
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
4a1fca7604 tpm2: rev155: Comment and whitespace changes only
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
400ce96414 tpm2: rev155: Refactor ByteArrayToUintXY functions
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
68b81c3f9e tpm2: rev155: Implement ByteArrayToUint8
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
191e989d75 tpm2: rev155: only do memmove if src != dest
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f0ae45a51c tpm2: rev155: Change list terminator in s_ccAttr
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
caf22f0e03 tpm2: rev155: Remove LIB_EXPORT from funcs in CryptPrimeSieve.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
9d202ba5e7 tpm2: rev155: Refacoring in CryptCreateObject()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
d88f5198ec tpm2: rev155: Refactor StartupCommands looking at returns of XYZStartup()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
ba3d47baf4 tpm2: rev155: Have PCRStarup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
ffd2511967 tpm2: rev155: Refactor GetSavedPcrPointer()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
9e47ac1e23 tpm2: rev155: Have NvEntityStartup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
108c585c18 tpm2: rev155: Have ObjectStartup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
e96f6857d2 tpm2: rev155: Have SessionStartup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
2fada6139a tpm2: rev155: Have HierarchyStartup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
1973935e2e tpm2: rev155: Have DAStartup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
8302aa22ea tpm2: rev155: Have TimeStartup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c58d60398b tpm2: rev155: Have TPMRegisterStartup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
f0ec1bfe36 tpm2: rev155: Have CommandAuditStartup return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
1bbedefe5d tpm2: rev155: Have NvWrite return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
7712e5dacc tpm2: rev155: Have _plat__NvMemoryWrite return BOOL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
8833dee474 tpm2: rev155: Have BnGeneratePrimeForRSA return TPM_RC
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
110bb0ea8d tpm2: rev155: Change parameter to SensitivetoPrivate to TPM2B_NAME 2020-04-14 16:03:45 -04:00
Stefan Berger
c1b16825ee tpm2: rev155: Remove ObjectGetNameAlg()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
1207eec953 tpm2: Replace MAX_SM4_KEY_BITS and MAX_CAMELLIA_KEY_BITS with 128
Rev 155 code defines MAX_SM4_KEY_BITS = 0 and MAX_CAMELLIA_KEY_BITS = 0
since both algorithms are not enabled. To be able to read old state
we now replace the usage of the #defines with hard coded values that
have no further meaning. If we ever activate the algoritms, we have to
extend the state.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
12233f42cf tpm2: sync: Use the ubsan fixes from upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
236a2fd119 tpm2: Sync with TPM 2 code comments or commented code (part 2)
Sync with TPM 2 code where the only difference is now comments
or commented/dead code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
18924c6bde tpm2: sync: keep PRIVATE_VENDOR_SPECIFIC_BYTES at size from rev 150
Keep the size of PRIVATE_VENDOR_SPECIFIC_BYTES. Changing its size would
change the size of an Object, which we don't want since Objects are in
NVRAM memory and now more would fit than before. Fitting more is not the
problem but the state cannot be downgraded anymore since an earlier
implementation would require more bytes per Object and would not allow
fitting them all anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
9865d9014c tpm2: sync: Initalize gc.platformAlg to ALG_NULL
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
19a12fbf79 tpm2: sync: Return error code in case of shutdown type != SU_CLEAR
Return an error code from TPM2_Shutdown in case an unknown shutdown
type code was given. Before that a failure was returned, which should
never have happened since the unmarshal function for parsing the shutdown
code would have caused an error when the command was parsed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
675b53db61 tpm2: sync: Change variable to UINT16 as returned by CryptHashGetDigestSize
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
6a04c24a3d tpm2: sync: Change size parameter to CryptRandomGenerate to UINT16
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
c5546b5d76 tpm2: sync: Change #if defined SELF_TEST to #if SELF_TEST
This change has no negative effect on what branch of code gets
compiled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
cfd12a7458 tpm2: sync: Add Windows specific includes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
58d8971db0 tpm2: sync: Fix typo in comment
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-04-14 16:03:45 -04:00
Stefan Berger
6de06a596c tpm12: Fix yet some more -O3 compilation errors (Trusty)
This patch addresses the following false positives on Ubuntu Trusty
on x86_64:

tpm12/tpm_process.c: In function ?TPM_Process_GetCapability?:
tpm12/tpm_process.c:3521:35: error: ?transportEncrypt? may be used uninitialized in this function [-Werror=maybe-uninitialized]
      returnCode = TPM_ProcessAudit(tpm_state,
                                   ^

tpm12/tpm_counter.c: In function ?TPM_Process_IncrementCounter?:
tpm12/tpm_counter.c:421:27: error: ?counterValue? may be used uninitialized in this function [-Werror=maybe-uninitialized]
  rc = TPM_Sbuffer_Append32(sbuffer, tpm_counter_value->counter);
                           ^
tpm12/tpm_counter.c:791:24: note: ?counterValue? was declared here
     TPM_COUNTER_VALUE *counterValue; /* The counter value */
                        ^

tpm12/tpm_key.c: In function ?TPM_KeyParms_CheckProperties?:
tpm12/tpm_key.c:1959:42: error: ?tpm_rsa_key_parms? may be used uninitialized in this function [-Werror=maybe-uninitialized]
   rc = TPM_KeyParams_CheckDefaultExponent(&(tpm_rsa_key_parms->exponent));

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-30 08:39:03 -05:00
Stefan Berger
49f3b55de9 tpm12: Initialize a few variables for x86 gcc -O3
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609

The above gcc on x86 with -O3 reports the following false positives:

 gcc -DHAVE_CONFIG_H -I. -I.. -include tpm_library_conf.h -I../include/libtpms -I../include/libtpms -fstack-protector-strong -DTPM_V12 -DTPM_PCCLIENT -DTPM_VOLATILE_LOAD -DTPM_ENABLE_ACTIVATE -DTPM_AES -DTPM_LIBTPMS_CALLBACKS -DTPM_NV_DISK -DTPM_POSIX -DTPM_NOMAINTENANCE_COMMANDS -O3 -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=0 -DUSE_OPENSSL_FUNCTIONS_RSA=0 -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign -MT tpm12/libtpms_tpm12_la-tpm_nvram.lo -MD -MP -MF tpm12/.deps/libtpms_tpm12_la-tpm_nvram.Tpo -c tpm12/tpm_nvram.c -o tpm12/libtpms_tpm12_la-tpm_nvram.o
tpm12/tpm_nvram.c: In function ‘TPM_Process_NVReadValue’:
tpm12/tpm_nvram.c:1539:38: error: ‘isGPIO’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      if ((returnCode == TPM_SUCCESS) && !isGPIO) {
                                      ^
tpm12/tpm_nvram.c: In function ‘TPM_Process_NVWriteValue’:
tpm12/tpm_nvram.c:2323:6: error: ‘isGPIO’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
   if (!isGPIO) {
      ^

gcc -DHAVE_CONFIG_H -I. -I.. -include tpm_library_conf.h -I../include/libtpms -I../include/libtpms -fstack-protector-strong -DTPM_V12 -DTPM_PCCLIENT -DTPM_VOLATILE_LOAD -DTPM_ENABLE_ACTIVATE -DTPM_AES -DTPM_LIBTPMS_CALLBACKS -DTPM_NV_DISK -DTPM_POSIX -DTPM_NOMAINTENANCE_COMMANDS -O3 -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=0 -DUSE_OPENSSL_FUNCTIONS_RSA=0 -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign -MT tpm12/libtpms_tpm12_la-tpm_process.lo -MD -MP -MF tpm12/.deps/libtpms_tpm12_la-tpm_process.Tpo -c tpm12/tpm_process.c -o tpm12/libtpms_tpm12_la-tpm_process.o
tpm12/tpm_process.c: In function ‘TPM_Process_GetCapabilitySigned’:
tpm12/tpm_process.c:5089:19: error: ‘transportEncrypt’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      returnCode = TPM_ProcessAudit(tpm_state,
                   ^
tpm12/tpm_process.c: In function ‘TPM_Process_SetCapability’:
tpm12/tpm_process.c:5309:19: error: ‘transportEncrypt’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      returnCode = TPM_ProcessAudit(tpm_state,
                   ^
tpm12/tpm_process.c: At top level:

gcc -DHAVE_CONFIG_H -I. -I.. -include tpm_library_conf.h -I../include/libtpms -I../include/libtpms -fstack-protector-strong -DTPM_V12 -DTPM_PCCLIENT -DTPM_VOLATILE_LOAD -DTPM_ENABLE_ACTIVATE -DTPM_AES -DTPM_LIBTPMS_CALLBACKS -DTPM_NV_DISK -DTPM_POSIX -DTPM_NOMAINTENANCE_COMMANDS -O3 -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=0 -DUSE_OPENSSL_FUNCTIONS_RSA=0 -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign -MT tpm12/libtpms_tpm12_la-tpm_transport.lo -MD -MP -MF tpm12/.deps/libtpms_tpm12_la-tpm_transport.Tpo -c tpm12/tpm_transport.c -o tpm12/libtpms_tpm12_la-tpm_transport.o
tpm12/tpm_transport.c: In function ‘TPM_Process_ReleaseTransportSigned’:
tpm12/tpm_transport.c:2810:42: error: ‘t1TpmTransportInternal’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  returnCode = TPM_TransportLogOut_Extend(t1TpmTransportInternal->transDigest,
                                          ^

This patch initializes the variables.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-17 15:21:32 -05:00
Stefan Berger
9781c71743 tpm2: Initialize a variable for gcc-5 ppc64el compiler
Another false positive report by a gcc-5 pcc64el compiler.

gcc-5 -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 -O3 -fstack-protector-strong -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=0 -DUSE_OPENSSL_FUNCTIONS_RSA=0 -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign -MT tpm2/libtpms_tpm2_la-Marshal.lo -MD -MP -MF tpm2/.deps/libtpms_tpm2_la-Marshal.Tpo -c tpm2/Marshal.c -o tpm2/libtpms_tpm2_la-Marshal.o
tpm2/Marshal.c: In function ‘TPM2B_PUBLIC_Marshal’:
tpm2/Marshal.c:96:19: error: ‘sizePtr’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      (*buffer)[1] = (BYTE)((*source >> 0) & 0xff);
                   ^
tpm2/Marshal.c:2023:11: note: ‘sizePtr’ was declared here
     BYTE *sizePtr;
           ^

This patch initializes the variable to NULL.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-17 15:21:32 -05:00
Stefan Berger
86118eb13c tpm12: Initialize some variables for gcc-5 ppc64el compiler
The -O3 passed to the ppc64el gcc-5 compiler on Ubuntu 16.04 causes
the following false positives:

gcc-5 -DHAVE_CONFIG_H -I. -I.. -include tpm_library_conf.h -I../include/libtpms -I../include/libtpms -fstack-protector-strong -DTPM_V12 -DTPM_PCCLIENT -DTPM_VOLATILE_LOAD -DTPM_ENABLE_ACTIVATE -DTPM_AES -DTPM_LIBTPMS_CALLBACKS -DTPM_NV_DISK -DTPM_POSIX -DTPM_NOMAINTENANCE_COMMANDS -g -O3 -fstack-protector-strong -DUSE_OPENSSL_FUNCTIONS_SYMMETRIC=1 -DUSE_OPENSSL_FUNCTIONS_EC=1 -DUSE_OPENSSL_FUNCTIONS_ECDSA=0 -DUSE_OPENSSL_FUNCTIONS_RSA=0 -Wall -Werror -Wreturn-type -Wsign-compare -Wno-self-assign -MT tpm12/libtpms_tpm12_la-tpm_session.lo -MD -MP -MF tpm12/.deps/libtpms_tpm12_la-tpm_session.Tpo -c tpm12/tpm_session.c -o tpm12/libtpms_tpm12_la-tpm_session.o
tpm12/tpm_session.c: In function ‘TPM_Process_SaveContext’:
tpm12/tpm_session.c:3056:16: error: ‘contextIndex’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
     uint32_t   contextIndex;  /* free index in context list */
                ^
tpm12/tpm_session.c: In function ‘TPM_Process_SaveAuthContext’:
tpm12/tpm_session.c:4934:16: error: ‘contextIndex’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
     uint32_t   contextIndex;  /* free index in context list */
                ^

This patch initializes these variables to 0.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-17 15:21:32 -05:00
Stefan Berger
0d22a7aea2 tpm12: Initialize some variables for gcc ppc64el compiler
gcc (Ubuntu 9.2.1-21ubuntu1) 9.2.1 20191130

The gcc compiler on Ubuntu Focal reports several false positives for
potentially uninitialized variables:

tpm12/tpm_session.c: In function ‘TPM_Process_SaveContext’:
tpm12/tpm_session.c:3229:19: error: ‘tpm_auth_session_data’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
 3229 |      returnCode = TPM_AuthSessionData_Store(&r1ContextSensitive, tpm_auth_session_data);
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tpm12/tpm_delegate.c: In function ‘TPM_Process_DelegateManage’:
tpm12/tpm_delegate.c:1787:49: error: ‘familyRow’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
 1787 |  if ((opCode != TPM_FAMILY_CREATE) && (familyRow->flags & TPM_DELEGATE_ADMIN_LOCK)) {
      |                                        ~~~~~~~~~^~~~~~~
tpm12/tpm_delegate.c: In function ‘TPM_Process_DelegateUpdateVerification’:
tpm12/tpm_delegate.c:3575:48: error: ‘d1DelegateTableRow’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
 3575 |      d1DelegateTableRow->pub.verificationCount = familyRow->verificationCount;
      |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All of the variables are initialize under the same condition as they are
accessed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-14 20:25:19 -05:00
Stefan Berger
9ccad78596 tpm2: Initialize variable to address valgrind complaint
This patch addresses the following valgrind issue detetcted by swtpm test suite:

== Syscall param write(buf) points to uninitialised byte(s)
==3515669==    at 0x4DC14B7: write (in /usr/lib64/libc-2.30.so)
==3515669==    by 0x48547FB: write_full (utils.c:242)
==3515669==    by 0x48548F3: writev_full (utils.c:301)
==3515669==    by 0x48520A6: SWTPM_IO_Write (swtpm_io.c:229)
==3515669==    by 0x4850662: mainLoop (mainloop.c:282)
==3515669==    by 0x402BE7: swtpm_main (swtpm.c:497)
==3515669==    by 0x4CF61A2: (below main) (in /usr/lib64/libc-2.30.so)
==3515669==  Address 0x52413a2 is 34 bytes inside a block of size 2,006 alloc'd
==3515669==    at 0x483980B: malloc (vg_replace_malloc.c:309)
==3515669==    by 0x48548A1: writev_full (utils.c:287)
==3515669==    by 0x48520A6: SWTPM_IO_Write (swtpm_io.c:229)
==3515669==    by 0x4850662: mainLoop (mainloop.c:282)
==3515669==    by 0x402BE7: swtpm_main (swtpm.c:497)
==3515669==    by 0x4CF61A2: (below main) (in /usr/lib64/libc-2.30.so)
==3515669==  Uninitialised value was created by a stack allocation
==3515669==    at 0x4953993: TPM2_Load (ObjectCommands.c:132)
==3515669==

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-10 08:34:27 -05:00
Stefan Berger
048e207b8e tpm12: Revert changes using strncpy to now use strcpy again
Revert the changes trying to use strncpy to now use strcpy again.
Recent compilers complain like this:

tpm12/tpm_nvfile.c: In function ‘TPM_NVRAM_Init’:
tpm12/tpm_nvfile.c:138:9: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
  138 |         strncpy(state_directory, tpm_state_path, length + 1);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tpm12/tpm_nvfile.c:130:18: note: length computed here
  130 |         length = strlen(tpm_state_path);
      |                  ^~~~~~~~~~~~~~~~~~~~~~
tpm12/tpm_nvfile.c: At top level:
cc1: error: unrecognized command line option ‘-Wno-self-assign’ [-Werror]
cc1: all warnings being treated as errors

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-10 07:54:57 -05:00
Stefan Berger
d186b753c8 tpm12: coverity: Use length + 1 for strncpy() parameter
Use length + 1 as size parameter to strncpy() to address a Coverity
issue (false positive).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2020-01-04 13:47:00 -05:00
Stefan Berger
6c0144be4b tpm2: Convert CMAC test to SMAC test for better coverage
The SMAC test now covers CMAC as well but covers more code paths.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-12-27 10:48:52 -05:00
Stefan Berger
1243d534a4 tpm2: Fix TDES key creation by adding missing un-/marshalling functions
Some TDES related marshalling and unmarshalling functions were
missing, so add them. Now TDES keys can be created.

Signed-off-by: stefan Berger <stefanb@linux.ibm.com>
2019-12-26 00:09:35 -05:00
Stefan Berger
e58cbdd4fd tpm2: Fix a bug in CheckAuthSession
This patch fixes a bug described in the following TCG Document

ERRATA; Errata Version 1.9, August 23, 2019; For TCG Trusted Platform
Module Library; Family "2.0", Level 00, Revision 1.38, Sept 29, 2016

section 2.34: NV PIN Indices

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-12-20 19:15:40 -05:00
Stefan Berger
75a2cd1a78 tpm12: Replace strcpy with safer strncpy
Replace the strcpy with the safer strncpy version.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-12-19 13:23:25 -05:00
Stefan Berger
f289817a61 tpm2: Clarify that PRIMARY_SEED_SIZE is 64 per USE_SPEC_COMPLIANT_PROOFS
We define USE_SPEC_COMPLIANT_PROOFS which overrides PRIMARY_SEED_SIZE in
Implentation.h and cause PRIMARY_SEED_SIZE to be set to 64. Nevertheless,
adjust the value in Implementation.h.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-12-05 21:04:47 -05:00
Stefan Berger
df2a7f9d75 tpm2: Always test implemented symmetric modes
Do not go by the toTest vector when it comes to testing symmetric
crypto modes since those bits may have been cleared elsewhere.
Check the implemented algorithm vector instead for whether the
mode is implemented.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-11-20 09:35:40 -05:00
Stefan Berger
8d68e403b3 tpm2: Add missing break statement to EVP function selectors
Add a missing break statement to the EVP function selectors
based on algorithm. So far this missing break statement had
not negative effect since no other algoritms are in use in
case statements below the TDES.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-11-20 09:35:40 -05:00
Stefan Berger
3909f211ac 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>
2019-11-18 11:18:34 -05:00
Stefan Berger
653a524330 tpm12: Implement TPM_PrintFourLimit to print 4 or less bytes
Implement TPM_PrintFourLimit to print only up to 4 bytes or less.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-11-18 11:18:34 -05:00
Stefan Berger
57d628a5ce tpm12: Get rid of usage of sprintf and use snprintf instead
Get rid of the usage of an sprintf and use snprintf instead. In this
case the buffer is 128, so big enough to not cause problems, though.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-10-17 11:06:48 -04:00
Stefan Berger
7bd2fb0d42 tpm12: Fix potential buffer overflow in filename creation
Fix a potential buffer overflow bug in the creation of filenames
that were using sprintf() rather than snprintf(). The buffer overflow
could occurr if the buffer is longer than 4096 bytes. The state path
may alone be 4096 bytes and could possibly trigger the overflow.

Swtpm for example is not affected from this since it uses the callbacks
that are invoked before the faulty function is called.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-10-17 11:06:48 -04:00
Stefan Berger
8ddc460ad1 tpm2: Check data size to be at least size of hash
Check the size of the available data before hashing them. A minimum
of 20 bytes needs to be passed into the function so that we can hash
the data 'before' it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reported-by: Yi Ren <yunye.ry@alibaba-inc.com>
2019-08-06 09:30:52 -04:00
Stefan Berger
24a22ef733 tpm2: Fix Coverity complaint by using iv.t.buffer
Fix a Coverity complaint by using iv.t.buffer rather than the
1-byte synonym (due to union) iv.b.buffer.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-07-09 10:46:42 -04:00
Stefan Berger
6ae0d8c52c tpm2: Implement key creation with OpenSSL for rand == NULL
Implement key creation with OpenSSL if rand == NULL, meaning
that we create a non-KDF-derived key, so a purely random key.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-20 12:23:29 -04:00
Stefan Berger
e793051573 tpm2: Use OpenSSL for decryption and signature verification
Use OpenSSL funtions now also for decryption and signature verification.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-19 15:10:19 -04:00
Stefan Berger
05dc373b59 tpm2: Use OpenSSL functions for encrypting and signing using RSA keys
Use the CRT (Chinese Remainder Theorem) method for doing the RSA
operations. Unfortunately it is not sufficient to just pass the
precalculated dP, dQ, and qInv parameters to OpenSSL private key
crypto functions but it also needs D, which is a bit more involved
to calculate.

We are not caching D (private exponent) as part of the OBJECT since
the OBJECT lives in size-limited NVRAM and we would need to add
'D' to the privateExponent_t, which would make it bigger and which
would allow less keys to fit into memory, which in turn could be-
come an issue if we wanted to resume a TPM that filled up the NVRAM
with keys and now less keys would fit into the NVRAM.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-19 13:57:45 -04:00
Stefan Berger
a572dbc424 tpm2: Implement helpers for creating OpenSSL RSA keys
Implement helper functions to create an RSA public and private
key. Create the latter with the CRT parameters.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-19 13:57:45 -04:00
Stefan Berger
bc681a1bc9 tmp2: Implement helper to get digest name from hashAlg
Implement a helper that converts a hash algorithm Id to a digest
name for lookup with OpenSSL.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-19 13:57:45 -04:00