Commit Graph

359 Commits

Author SHA1 Message Date
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
Stefan Berger
fee2ae97bc tpm2: Use OpenSSL to create TDES keys if rand == NULL
Use OpenSSL function to create TDES keys if rand == NULL,
which indicates that a truely random key needs to be generated
rather than one derived from a KDF.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-19 10:12:24 -04:00
Stefan Berger
fcd2d24e1a tpm2: Add comment 'libtpms added' to added #include
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-17 18:57:21 -04:00
Stefan Berger
15e8d05b07 tpm2: Add comment 'libtpms added' to BnConvert.c change
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-17 18:57:21 -04:00
Stefan Berger
271b05fdcf tpm2: Add 'libtpms bugfix' comment to CMAC fix
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-17 18:57:21 -04:00
Stefan Berger
08ede08509 tpm2: Add comment 'libtpms added'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-17 18:57:21 -04:00
Stefan Berger
c5ba5e1291 tpm2: Remove duplicate CryptUtil_fp.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-10 11:19:58 -04:00
Stefan Berger
afbb327423 Implement EC key generation using OpenSSL functions if rand == NULL
Use OpenSSL functions to create EC keys only for the case that
rand == NULL in which case no KDF is being used and where we can
create a truly random key. This doesn't break the upgrade path.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-10 11:19:58 -04:00
Stefan Berger
d175ee918b tpm2: Also include openssl/ecdsa.h for older openssl versions
Older OpenSSL versions had the ECDSA headers in openssl/ecdsa.h, so
include this file as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-04 15:36:10 -04:00
Stefan Berger
55f5988781 tpm2: Switch ECDSA signature creation to OpenSSL
Use OpenSSL crypto library now also for the ECDSA signature creation.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-04 15:36:10 -04:00
Stefan Berger
82686260c7 tpm2: Make OsslToTpmBn's BIGNUM parameter const
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-04 15:36:10 -04:00
Stefan Berger
46869d307e Switch ECDSA signature verification to OpenSSL
Switch the ECDSA signature verification to OpenSSL. Do the signature
creation in the next step so we can verify the creation / verification
against the original TPM 2 code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-06-03 18:18:13 -04:00
Stefan Berger
4e1cd261ef build-sys: Add support for --disable-use-openssl-functions
OpenSSL's crypto library does not support all crypto functionality
we need in all versions. Elliptic curve support via EVP seems to
have been added much later than for example symmetric crypto support.
So, we move the USE_OPENSSL_FUNCTIONS out of Implementation.h
into configure.ac and let the build system detect what functionality
is available in the crypto library. In this patch we now also rename
USE_OPENSSL_FUNCTIONS to USE_OPENSSL_FUNCTIONS_SYMMETRIC to indicate
that we can use the symmetric crypto functions of the crypto lib.

Using the OpenSSL crypto support is enabled by default, so one has
to use --disable-use-openssl-functions, which we do for Travis now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-30 16:25:15 -04:00
Stefan Berger
e91633b271 tpm2: Convert symmetric cipher implementations to use OpenSSL functions
Convert functions for symmetric encryption via AES and TDES to
use OpenSSL EVP functions where necessary. Leave the old code around
and use the #if USE_OPENSSL_FUNCTIONS to activate the new parts.

OpenSSL does not provide an implementation for TDES in CTR mode, so
we reuse the original TPM 2 code for this mode.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-30 12:23:05 -04:00
Stefan Berger
8de7f334c7 tpm2: Implement helper for getting OpenSSL crypto functions
Implement helper for getting OpenSSL crypto functions from
crytpo algorithm IDs, encryption modes, and key sizes.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-30 12:23:05 -04:00
Stefan Berger
f5e4768265 tpm2: Introduce clear_and_free() to clear memory before freeing
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-30 12:23:05 -04:00
Stefan Berger
1ae430e708 tpm2: Introduce #define USE_OPENSSL_FUNCTIONS
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-30 12:23:05 -04:00
Stefan Berger
31e4db99f4 tpm2: Add fall through comment to address Coverity finding
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-30 10:11:33 -04:00
Stefan Berger
bca3109634 tpm2: Rearrange code in _plat__IsNvAvailable to avoid unused var
Rerrange the code in _plat__IsNvavailable to avoid an unused
variable.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-30 10:11:33 -04:00
Stefan Berger
1b2c4fc5b9 tpm2: Fix coverity scan errors in NvFileSize
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-30 10:11:33 -04:00
Stefan Berger
7e6473b938 tpm2: Clear all EC_POINT and BIGNUM before freeing
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-24 12:59:10 -04:00
Stefan Berger
10d4e5fd9d tpm2: Rename NV_ROUNDUP to TPM2_ROUNDUP and move to new Utils.h
Move NV_ROUNDUP to Utils.h and call it TPM2_ROUNDUP for reuse by
other functions.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-23 11:39:24 -04:00
Stefan Berger
e2129d2939 tpm2: Add CMAC test cases from RFC 4493
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-09 23:11:27 -04:00
Stefan Berger
137b9da5c8 tpm2: Reset the internal CMAC state when starting a CMAC
Initialize the internal CMAC state to 0s when starting a CMAC.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-09 23:11:27 -04:00
Stefan Berger
a829ddbdeb Extend TDES test cases (CFB, OFB) with test cases for short input
Add TDES test cases testing CFB and OFB with non-blocksized short
input.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-08 14:59:55 -04:00
Stefan Berger
c14f1b01c9 tpm2: Extend internal test case with tests for TDES
Also test the TDES implementations.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-08 11:11:41 -04:00
Stefan Berger
cb386e4c7f tpm2: Add script to re-generate the test data for AES and new test for TDES
Add a script that uses openssl to create the test data we are using
in the TPM 2 code already. Extend this script with TDES 128 and 192 bit
key sizes and tests.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-05-08 11:11:41 -04:00
Stefan Berger
5ac0d9a3d5 Add missing #define TPM_HAVE_TPM2_DECLARATIONS in tpm2 code (RHEL 6)
Add some missing #define TPM_HAVE_TPM2_DECLARATIONS before the include
of "tpm_library_intern.h" in TPM 2 code so we don't run into compile
errors on RHEL 6 when data types are redefined in TPM 1.2 code.

Previous patch 73cad883ba seems to have missed those.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-04-12 11:53:28 -04:00
Stefan Berger
5b13401a35 tpm2: Sync with TPM 2 code comments or commented code
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>
2019-04-08 10:12:14 -04:00
Stefan Berger
639243d60d build-sys: Remove cruft from 'check'
Remove some cruft that was useful when TPM 2 code was still in the
works but now that things have settled don't need it anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-04-04 13:19:16 -04:00
Stefan Berger
7620f55ac1 tpm2: TPMT_PUBLIC_PARMS_Marshal is unused
... and TPMU_PUBLIC_PARMS_Marshal is used, so leave its prototype active.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-04-03 10:50:16 -04:00
Stefan Berger
25f267c491 tpm2: Deactivate unused code
Also TPM 2 code has a lot of unused code that we can #if 0 out.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-04-01 16:41:21 -04:00
Marc-André Lureau
3e591832c4 build-sys: fix grep warnings when building out-of-tree
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-04-01 11:22:03 -04:00
Stefan Berger
402fc2b1e1 tpm12: Build without support for maintentance commands
Introduce TPM_NOMAINTENANCE_COMMANDS #define to build the TPM 1.2 code
without maintenance commands support. The state for the maintenance
commands has been written out so far, so we have to leave this part
alive even though nothing can be done with the maintenance key anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-04-01 10:16:25 -04:00
Stefan Berger
d0b9d9a9ff tpm12: Deactivate unused code
There are several functions in the code that do not have a caller.
Deactivate all of them to get higher coverage of the code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-04-01 10:16:25 -04:00
Marc-André Lureau
8373f09854 build-sys: add oss-fuzz support
This script will permit integration with Google OSS-FUZZ
https://github.com/google/oss-fuzz

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-03-27 14:19:37 -04:00
Stefan Berger
174bae9542 tpm2: TPM2_MainInit: Enable NVRAM in NVChip file case
When no callbacks are set and the plain NVChip file is used it wasn't
possible to save the state of the TPM 2 after a TPMLIB_Terminate()
TPMLIB_MainInit() sequence with a cached state (TPMLIB_SetState) since
in this case the NVRAM was not initialized. We now initialize the NV
in case no callback for restoring the state is set.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-03-26 10:24:41 -04:00
Stefan Berger
f31840453a tpm2: Split off _plat__NVEnable_NVChipFile from _plat__NVEnable
Split off the part that was handling the NVChip file in _plat__NVEnable
and call this new function _plat__NVEnable_ChipFile so we can call it
separately in case the caller works with the plain NVChip file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-03-26 10:24:41 -04:00
Stefan Berger
95ab136b83 tpm2: Adapt code for OpenSSL 1.2 deprecated API calls
Some OpenSSL EC API calls are deprecated starting in OpenSSL 1.2
and may fail the build due to the #warning they produce.

The affected functions have been renamed and consolidated with
other functions inside OpenSSL:

- EC_POINT_set_affine_coorindates_GFp
https://github.com/openssl/openssl/blob/OpenSSL_1_1_1-stable/crypto/ec/ec_lib.c#L730

- EC_POINT_get_affine_coordinates_GFp
https://github.com/openssl/openssl/blob/OpenSSL_1_1_1-stable/crypto/ec/ec_lib.c#L768

On both functions the suffix _GFp has been removed by the renaming.
We need to call the new functions if OPENSSL_API_COMPAT is defined
and is >= 0x10200000L.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2019-03-19 09:25:44 -04:00