Commit Graph

997 Commits

Author SHA1 Message Date
Stefan Berger
36d1e03368 tpm2: Use new privateExponent also for dP, dQ, and qInv
Also use the new privatExponent for dP, dQ and qInv.

There are two functions that need to be adapted:
- ComputePrivateExponent: producer of these parameters
- RsaPrivateKeyOp       : consumer of these parameters

ComputePrivateExponent is converted to store the results into Z->dP,
Z->dQ, and Z->qInv. Therefore, remove the old privateExponent parameter
*pExp, that was previously used to store them, from the signature of this
function and pull out the initialization of pExp and preservation of Q
to be done before calling this function. This is done in the 2 calling
functions. After returning from the function copy the values of Z->dP,
Z->dQ, and Z->qInv to the old privateExponent where the results had
been stored previously and where we need to have them.

This change results in a sequence like this for the 2 callers:

RsaInitializeExponentOld(&rsaKey->privateExponent);
BnCopy((bigNum)&rsaKey->privateExponent.Q, Z->Q); // preserve Q

VERIFY(ComputePrivateExponent(bnE, Z));

RsaSetExponentOld(&rsaKey->privateExponent, Z);   // duplicate dP, dQ, qInv

The values for dP, dQ, ad qInv are consumed by RsaPrivateKeyOp. Therefore,
adjust this functions signature by removing the old privateExponent
parameter *pExp from it and make sure that callers initialize Z->dP,
Z->dQ, and Z->qInv before calling this functions. There are two call-sites
where the one in RSADP looks like this:

RsaSetExponentFromOld(Z, &key->privateExponent); // copy Q, dP, dQ, qInv to Z
VERIFY(RsaPrivateKeyOp(bnM, Z));

The call site in CryptRsaGenerateKey has called ComputePrivateExponent
before, therefore it already holds the values in Z.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
c9a0650e52 tpm2: Use new privateExponent struct in RsaPrivateKeyOp
Pass the new privateExponent to RsaPrivateKeyOp replacing the P parameter.
To be able to use MakePgreaterThanQ(Z), make sure that both callers of
this function have Z->P and Z->Q values properly set.

This function has the following two callers:
- CryptRsaPrivateKey: Z->P and Z->Q are already holding valid values
- RSADP: Copy the value of privateExponent.Q to Z->Q.

An inconsequential side effect of the changes to RsaPrivateKeyOp()
is that Z->P is greater than Z->Q upon return from this function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
7edfb28a2e tpm2: RSADP: Replace local bnP with P of new privateExponent
Replace the local variable bnP with Z->P. Initialize Z->P with the
value that bnP had been initialized with.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
a866dda5ed tpm2: Replace P and Q params of ComputerPrivateExponent with new privateExponent
Replace the P and Q parameters of ComputePrivateExponent with the new
privateExponent struct (sync with upstream).

ComputePrivateExponent has two callers:
- CryptRsaGenerateKey: Z already holds P and Q from previous change
- CryptRsaLoadPrivateExponent: Sync the code with upstream so that we can also
     use the privateExponent Z as parameter to ComputePrivateExponent holding
     valid values in Z->P and Z->Q.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
218337ea2a tpm2: Use new privateExponent struct in CryptRsaGenerateKey for P and Q
- Import RsaInitializeExponent from upstream
- CryptRsaGenerateKey: Replace local bnP and bnQ variables by using Z->P
  and Z->Q respectively

The only side-effect this change has is that it costs more initialization
time when NEW_PRIVATE_EXPONENT initializes the currently unused variables
dP_unused, dQ_unused, and qInv_unused.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
9b16a500d3 tpm2: Add new typedef privateExponent used by upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
d28f4e0e34 tpm2: Rename RsaInitializeExponent to RsaInitializeExponentOld
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
0de1cea8a5 tpm2: rev164: Trivial whitespace, comment, and libtpms-unused change
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 12:20:14 -04:00
Stefan Berger
687f0de78c tpm2: Add publicArea and sensitive params to CryptRsaGenerateKey
The upstream function switches entirely to publicArea and sensitive
parameters for CrytpRsaGenerateKey getting rid of the OBJECT. We still
need the OBJECT at this point, so keep it for now but annotate the
code and add a consitency check that ensures that the publicArea and
sensitive parameters are from the OBJECT. This holds for the single
caller.

Adjust the single caller to the change in parameters.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 12:20:14 -04:00
Stefan Berger
c10974ab85 tpm2: Check result from CryptRsaLoadPrivateExponent in RSADP
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 11:17:26 -04:00
Stefan Berger
9e6e0fdc50 tpm2: Rework the logic validating the keys in ObjectLoad
Previously the key check was done when object == NULL. Now this
particular case is handle by a check being done when parent == NULL
since the only caller of ObjectLoad() with object == NULL also has
parent == NULL, so there's no behavior change. All other cases
are handled as before.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 11:17:26 -04:00
Stefan Berger
d2d425a126 tpm2: Apply some trivial changes to RSADP
- Use UnsignedCompareB() rather than BnUnsignedCmp()
- Use VERIFY to check results and add failure exit.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 11:17:26 -04:00
Stefan Berger
6dd7cf4eff tpm2: Apply some trivial changes to RsaPrivateKeyOp
- Remove code related to CRT_FORMAT_RSA == NO.
- Remove N parameter from function and adjust callers
- Use VERIFY after each statement

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 11:17:26 -04:00
Stefan Berger
a33fd3b0de tpm2: Apply some trivial changes to CryptRsaGenerateKey
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 11:17:26 -04:00
Stefan Berger
f1ea4f51cb tpm2: Helpers: Remove CRT_FORMAT_RSA == YES checks
Since CRT_FORMAT_RSA can only be YES, remove the checks for
this #define.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 09:41:54 -04:00
Stefan Berger
2d110169a6 tpm2: Sync part of ComputePrivateExponent
- Remove support for CRT_FORMAT_RSA == NO
- Remove now unused parameter N from function signature; adjust callers
- Rename E to pubExp
- Rename temp to pT

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 09:41:54 -04:00
Stefan Berger
982b8678c5 tpm2: Sync part of CryptRsaGenerateKey
- Continue the prime number generation while retVal == TPM_RC_NO_RESULT
- Terminate the loop when BnGeneratePrimeForRSA() returns a failure

The changes should not lead to any different primary keys than before.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 09:41:54 -04:00
Stefan Berger
a9a8e8fb93 tpm2: Sync: Remove CRT_FORMAT_RSA from RsaInitializeExponent
Since CRT_FORMAT_RSA == NO is not supported, remove it from
RsaInitializeExponent.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 09:41:54 -04:00
Stefan Berger
c579a3c8de tpm2: Sync RSAEP with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 09:41:54 -04:00
Stefan Berger
eace1d8fee tpm2: Sync: Add checks for g_inFailure mode after DRBG ops
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 09:41:54 -04:00
Stefan Berger
bed0069d1f tpm2: Sync: Replace salt size call by call to CryptRsaPssSaltSize
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
b015702b43 tpm2: Sync defintion of RSA-related BN_TYPE's
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
44e31a3263 tpm2: Remove unused RSA_KEY typedef struct
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
55d64c75dd tpm2: Use sensitive instead of equivalent object->sensitive
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
9916aa936e tpm2: Remove duplicate init of sensitive.sym.t.size
The called function is also initializing this field with the same value.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
cf9d0d32b9 tpm2: Add missing libtpms changed comments to sm4 related code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
9f15d86413 tpm2: Sync zeroization of variables with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
c235a90932 tpm2: ExecuteCommand: Handle unreasonably large command sizes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
4bee97102b tpm2: Pass correct RSA_PRIVATE_SIZE tpp TPM2B_Unmarshal
RSA_PRIVATE_SIZE is the correct size to use since this is the size that
TPM2B_PRIVATE_KEY_RSA has been defined with.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-25 22:57:45 -04:00
Stefan Berger
de921c3ead Revert "Object: clear out sensitive area if on load if not provided"
This reverts commit e82727e546.

The issue has previously been resolved in commit 17255da54c.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-17 20:28:04 -04:00
Rob Shearman
e82727e546 Object: clear out sensitive area if on load if not provided
When an object is later marshalled in TPM2_ContextSave, the publicOnly
attribute isn't taken into account and therefore potentially stale
sensitive information can be marshalled, which is a problem if the
buffer sizes it contains have values that are too large - this
triggers assertion failures.

Avoid this by clearing out the sensitive area upon ObjectLoad if not
provided, making the behaviour consistent with when a fresh, unused,
object entry is used.

Signed-off-by: Rob Shearman <rob@graphiant.com>
2023-08-14 08:45:55 -04:00
Stefan Berger
c702d72afb tpm2: Use GET_ATTRIBUTE to get commandIndex from s_ccAttr
Fix two locations where s_ccAttr[0].commandIndex is used to access the
commandIndex, which does not work when bitfields are not used. Use
GET_ATTRIBUTE() to access the field so that it works when bitfields are
used and when they are not used. There are several locations in this
file where GET_ATTRIBUTE() is already used to access commandIndex.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-03 11:34:43 -04:00
Stefan Berger
6690ea89f7 tpm2: Fix spelling errors
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-07-17 20:37:20 -04:00
Stefan Berger
63b9c36a16 Insert assert ensuring *buflen != BUFLEN_EMPTY_BUFFER (Coverity)
Address a false positive issue detect by Coverity (CID 1517797)
about *buflen.

Per this assignment of buflen

cached_blobs[st].buflen = buffer ? buflen : BUFLEN_EMPTY_BUFFER;

the following is true:

If cached_blobs[].buffer is     NULL then *buflen  = BUFLEN_EMPTY_BUFFER
If cached_blobs[].buffer is not NULL then *buflen != BUFLEN_EMPTY_BUFFER

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-07-13 16:40:35 -04:00
Stefan Berger
f2b5d0c128 tpm12: Use proper format specifier %zu for size_t (CID1517801 & 1517798)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-07-12 18:31:22 -04:00
Fergus Dall
52e98e9758 tpm2: Add test vectors for camellia-192
Signed-off-by: Fergus Dall <sidereal@google.com>
2023-06-05 07:28:16 -04:00
Ben Lytle
0c32f39788 tpm2: fix missing algorithm macro checks
Signed-off-by: Ben Lytle <ben.dav.lytle@hpe.com>
2023-03-17 15:28:36 -04:00
Stefan Berger
dc1aac1ecf tpm2: rev164: Synchronize _TPM_Init() with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-07 12:11:50 -05:00
Stefan Berger
5a7d238653 tpm2: rev164: Sync BN_CONST definition (not used by libtpms)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-07 12:11:50 -05:00
Stefan Berger
2f1fb4c549 tpm2: rev164: Add implementation for TPM2_ECC_Encrypt/Decrypt
Add the implementation for TPM2_ECC_Encrypt/Decrypt. It cannot be
easily enabled due to possible downgrading requirements and also
issues with size-expansion of the PERSISTENT_DATA.auditCommands from
14 to 15 bytes.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-07 12:11:50 -05:00
Stefan Berger
6ac04e4681 tpm2: rev164: Simplify SignedCompareB() implementation
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-07 12:11:50 -05:00
Stefan Berger
496b64c561 tpm2: rev164: Fix CommandDispatchData for CertifyX509Data
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-07 12:11:50 -05:00
Stefan Berger
74a9ab5515 tpm2: rev164: Trivial whitespace, command, and libtpms-unused changes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-07 12:11:50 -05:00
Stefan Berger
c9f9fc52cf tpm2: Fix issue in CryptParameterEncryption() (TPM 2 errata v1.4)
Sync with upstream to fix issue in CryptParameterEncryption() from TPM 2
errate v1.4 2.6.1:

"The functions CryptParameterEncryption() and CryptParameterDecryption() in
the reference code in Part 4, 10.2.6.6.5 and 10.2.6.6.6 do not correctly
check the size of the parameter buffer to be encrypted or decrypted. To fix
the issue, the functions should be corrected to check that the parameter
buffer (a TPM2B type field) is at least 2 bytes in length and should use
the function UINT16_Unmarshal() to read the size of the buffer instead of"
BYTE_ARRAY_TO_UINT16().

[...]

The fixed CryptParameterEncryption() function will enter failure mode and
return TPM_RC_FAILURE if the internal response buffer does not contain
enough data for the UINT16 size field."

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-06 17:39:55 -05:00
Stefan Berger
9425f82960 tpm2: Sync fix in TPM2_PolicyAuthorize() with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-06 17:39:55 -05:00
Stefan Berger
d35a48b284 tpm2: Sync CryptParameterDecrypt implementation with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-06 17:39:55 -05:00
Stefan Berger
fb86111ca9 tpm2: Fix issue related to CryptGenerateKeyDes (TPM 2 errata v1.4)
Fix the following issue from TPM 2 errata v1.4 2.6.3:

"The function CryptGenerateKeyDes() in the reference code in Part 4,
0.2.9.2.3 does not correctly check the symmetric key size provided in the
sensitive parameter. To fix the issue, the function will check that the
size of the requested TDES key is a multiple of 8 bytes or otherwise the
TPM will return TPM_RC_SYMMETRIC."

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-03-06 17:39:55 -05:00
Stefan Berger
92f470c1b0 tpm2: Check size of TPM2B_NAME buffer before reading 2 bytes from it
Fix the missing buffer size check that the TPM 2 errata v1.4 mentions in
2.6.2 by adding a buffer size check before reading 2 bytes from a
TPM2B_NAME buffer. There's no known CVE for this.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-28 17:26:57 -05:00
Stefan Berger
324dbb4c27 tpm2: Check size of buffer before accessing it (CVE-2023-1017 & -1018)
Check that there are sufficient bytes in the buffer before reading the
cipherSize from it. Also, reduce the bufferSize variable by the number
of bytes that make up the cipherSize to avoid reading and writing bytes
beyond the buffer in subsequent steps that do in-place decryption.

This fixes CVE-2023-1017 & CVE-2023-1018.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-28 15:56:38 -05:00
Stefan Berger
b87306ca12 tpm2: Add a caching layer to GetEVPCipher() helper function
Add a caching layer to GetEVPCipher() to avoid having to call evpfn()
mulitple times. Instead, return the 'const EVP_CIPHER *' that a single
call to evpfn() (for a particular algorithm + mode + key size) returned
and cache it for subsequent calls.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-28 12:04:59 -05:00
Stefan Berger
0de6f656b8 tpm2: Replace usage of deprecated DES_random_key() (OSSL 3)
Replace usage of deprecated DES_random_key() with EVP_CIPHER API calls.
These newer calls are much more time consuming than the deprecated call.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-23 17:24:17 -05:00
Stefan Berger
7b8572355b tpm2: Replace deprecated EC_POINTs_mul() (OSSL 3)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-23 17:15:11 -05:00
Stefan Berger
d08b929ded tpm2: Use EVP_CIPHER_CTX_get_params() to get IV
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-13 06:50:42 -05:00
Stefan Berger
06ada5ad3e tpm2: Skip call to DoEVPGetIV() if ivLen = 0
The ivLen may be 0 for ECB and in this case we can skip the call to
DoEVPGetIV().

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-13 06:50:42 -05:00
Stefan Berger
f531ad6927 tpm2: Refactor code that gets current IV
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-13 06:50:42 -05:00
William Roberts
c6c8f63686 Revert "tpm_crypto: support libressl api"
This reverts commit 9afebc712a.

The issue is that opensslv.h is not included and thus the
OPENSSL_VERSION_NUMBER is not getting set.

Signed-off-by: William Roberts <william.c.roberts@intel.com>
2022-07-12 11:28:47 -04:00
William Roberts
27beabc518 tpm_crypto: support libressl api
Signed-off-by: William Roberts <william.c.roberts@intel.com>
2022-07-09 13:30:55 -04:00
William Roberts
4970a8a0c7 tpm_crypto: simplify OSSL version ifdef
Signed-off-by: William Roberts <william.c.roberts@intel.com>
2022-07-09 13:30:55 -04:00
Stefan Berger
086e1bcb8b tpm2: Fix a typo in PERSISTENT_ALL_Unmarshal
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-06-15 10:35:08 -04:00
Stefan Berger
9c63f076ec tpm2: Implement OpenSSLCryptRsaGenerateKey for OpenSSL 3.0 case
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
d215cfd612 tpm2: Implement BuildRSAKey for OpenSSL 3.0 case
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
e998c06694 tpm2: Delay creation of EVP_PKEY in InitOpenSSLRSAPrivateKey
Delay the creation of the EVP_PKEY in InitOpenSSLRSAPrivateKey
so that we can create the key with all the parameters at once.
We have to do this since with the OpenSSL 3.0 API we cannot set
parameters after the initial creation of the key anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
c8523779f2 tpm2: Implement DoRSACheckKey with OpenSSL 3.0 API
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
f7ccda56fb tpm2: Implement ObjectGetPublicParameters to get N and E from OBJECT
Get the BIGNUMs N and E from an RSA key OBJECT. The purpose of
this refactoring is be able to reuse the new function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
3eef1fb035 tpm2: Implement BuildRSAKey for building an RSA EVP_PKEY
Implement BuildRSAKey for building an RSA EVP_PKEY from copies
of the BIGNUMs it gets passed. This way it is clear that the
caller has to free the BIGNUMs it passed itself also in case of
error returned by BuildRSAKey.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-31 20:31:10 -04:00
Stefan Berger
6a919f1c6a tpm2: Do not access variable if it could not be read from buffer (Coverity)
Exit the loop when the variable could not be filled with data from the
byte stream. This avoids accessing the variable 'element' in case it
wasn't initialized. The old could would have accessed the possibly
uninitialized variable but exited the loop immediately after.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
b6b8506bce tpm2: Access entrysize variable only if it was read from buffer (Coverity)
Only access the entrysize variable if it was read from the buffer. In case
of an error just head towards the exit. Previously, an error would also
have lead the function to do no more useful processing and exited it with
an error code bug Coverity complains that the entrysize variable would be
access while it wasn't initialized.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
353e835caa tpm2: Expect TPM_SUCCESS from tpm_io_getlocality() callback function
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
77950dd917 tpm2: Fix a potential overflow expression (coverity)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
3b047cb5fe tpm2: Assign result of OsslToTpmBn to OK
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 20:07:35 -04:00
Stefan Berger
0aef1c7471 tpm12: Check returnCode before overwriting it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Stefan Berger
1555b59304 tpm12: Check returnCode before overwriting it
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Stefan Berger
adce2d6980 tpm12: Assign return value of TPM_DelegatePublic_Copy to rc
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Stefan Berger
2025654843 tpm12: Assign TPM_MGF1 return value to rc
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Stefan Berger
03e90a7dd4 tpm12: Assign rc return value of TPM_Sbuffer_Append
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-28 16:40:19 -04:00
Ross Lagerwall
3d2bbe2f19 tpm2: Fix size check in CryptSecretDecrypt
Check the secret size against the size of the buffer, not the size
member that has not been set yet.

Reported by Coverity.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2022-05-24 14:08:56 -04:00
Ross Lagerwall
71d3a27bc7 nvfile: Free allocated memory on failure
In TPM_NVRAM_LoadData(), there is an unlikely path where the function
will return an error code but still expect the caller to free the
allocated data. At least some of the callers don't handle this correctly
so ensure that the caller only needs to free data if the function
returns success.

Reported by Coverity.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2022-05-20 09:57:09 -04:00
Stefan Berger
f9a6f51cb8 tpm: #undef printf in case it is #define'd (OSS-Fuzz)
The build environment used by OSS-Fuzz reports this error.

 In file included from /src/libtpms/src/tpm_debug.c:42:
 /src/libtpms/src/tpm_debug.h:69:9: error: 'printf' macro redefined [-Werror,-Wmacro-redefined]
 #define printf(...) TPMLIB_LogPrintf(__VA_ARGS__);
 :         ^
 /usr/include/x86_64-linux-gnu/bits/stdio2.h:110:11: note: previous definition is here
 # define printf(...) \
           ^
 1 error generated.

The simple fix is to #undef printf in case it is #define'd.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-04-08 08:35:19 -04:00
Stefan Berger
73fca84031 tpm2: Require TPM_NV_DISK to avoid case of tpm_stata_path being NULL
Address an issue reported by cppcheck that raises the issue that
tpm_state_path could be NULL when the #define TPM_NV_DISK is not set.
Require that the #define TPM_NV_DISK always be set.

Resolves: https://github.com/stefanberger/libtpms/issues/313
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-04-01 08:41:35 -04:00
Stefan Berger
5bc860ac37 tpm2: Initialize variable reported by Coverity (false positive)
Initialize a variable in ExecuteCommand following Coverity report
CID 1461252.

Down the callpath as reported in CID 1461252 in
TPMI_ST_COMMAND_TAG_Unmarshal() the passed-in value of
TPMI_ST_COMMAND_TAG *target is stored and possibly restored later on in
case of failure. Coverity complains that the variable is uninitialized.
While this is correct, there's no harm reading the uninitialized value
from the structure and possibly restoring it later on while not doing
anything else with it otherwise. Therefore, it's a false positive.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-31 13:56:25 -04:00
Stefan Berger
fbf413ec86 tpm2: Initialize variable reported by Coverity (false positive)
Initialize a variable in TPM2_PolicyAuthorizeNV() following Coverity
report CID 1470811.

Down the callpath as reported in CID 1470811 in TPMI_ALG_HASH_Unmarshal()
the passed-in value of TPMI_ALG_HASH *target is stored and possibly
restored later on in case of failure. Coverity complains that the variable
is uninitialized. While this is correct, there's no harm reading the
uninitialized value from the structure and possibly restoring it later
on while not doing anything else with it otherwise. Therefore, it's a
false positive.

Resolves: https://github.com/stefanberger/libtpms/issues/311
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-31 10:36:11 -04:00
Stefan Berger
ec873cf8a2 tpm2: Initialize variable reported by Coverity (false positive)
Initialize a variable in USER_NVRAM_Unmarshal() follow Coverity
report CID 1470812.

Down the callpath as reported in CID 1470812 in TPMA_NV_Unmarshal() the
passed-in value of TPMA_NV *target is stored and possibly restored later
on in case of failure. Coverity complains that the variable is
uninitialized. While this is correct, there's no harm reading the
uninitialized value from the structure and possibly restoring it later
on while not doing anything else with it otherwise. Therefore, it's a
false positive.

Resolves: https://github.com/stefanberger/libtpms/issues/310
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-31 10:34:37 -04:00
Stefan Berger
8590849b65 tpm2: Check return code of BN_div()
Check the return code of BN_div() when calculating Q and Qr of
a private key.

Resolves: https://github.com/stefanberger/libtpms/issues/304
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-03-30 10:25:17 -04:00
Steve Langasek
7a64b3e41e tpm2: Initialize variables due to gcc complaint (s390x, false positive)
Detected by gcc on Ubuntu/s390x

Origin: https://git.launchpad.net/ubuntu/+source/libtpms/tree/debian/patches/uninitialized-variable.patch

Signed-off-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
2022-03-30 08:55:33 -04:00
Steve Langasek
c2f2bc8eaf tpm12: Initialize variables due to gcc complaint (s390x, false positive)
Detected by gcc on Ubuntu/s390x

Origin: https://git.launchpad.net/ubuntu/+source/libtpms/tree/debian/patches/uninitialized-variable.patch

Signed-off-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
2022-03-30 08:55:33 -04:00
Juergen Repp
e426198437 tpm2: Do not call EVP_PKEY_CTX_set0_rsa_oaep_label() for label of size 0 (OSSL 3)
Openssl 3.0 did return an error if EVP_PKEY_CTX_set0_rsa_oaep_label was called
with label size 0. The function should only be called if the size of the label
is greater 0.
With this fix TPM2_RSA_Encrypt/Decrypt did work with OpenSSL 1.1 and 3.0
for encryption without label.

Signed-off-by: Juergen Repp <juergen.repp@sit.fraunhofer.de>
2022-03-02 07:24:21 -05:00
Stefan Berger
967e52e624 tpm2: Only access variable if unmarshalling worked
Only access the variable 'nvi' when the previous unmarshalling worked.
Before this change the undefined value of nvi would have been written
to memory but the error code from the failed marshalling propagated to
the caller so that it was working correctly before as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-02-16 13:23:23 -05:00
Stefan Berger
d78a4520ba tpm2: Address Coverity Issue by casting '1' before shift (CID 1470813)
Cast the '1' to UINT64 before shifting it.

Since the shift value is always below 32 it would have never exceeded
the 32bit value it was using before the cast.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-02-16 13:23:23 -05:00
Stefan Berger
26e0eea093 tpm2: Fix typo in __GNUC_MINOR__ preprocessor symbol
Fix typo in the __GNUC_MINOR__ preprocessor symbol.

This change is unlikely to have any impact since it was used for
comparions for gcc version 4.2, which is not in use anymore by now.

Resolves: https://github.com/stefanberger/libtpms/issues/289
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-01-12 07:46:20 -05:00
Stefan Berger
aeb7af78a9 tpm2: When writing state initialize s_ContextSlotMask if not set
If s_ContextSlotMask was not set since the TPM 2 was not initialized
by a call to TPM_Manufacture() or the state was not resumed, then
initialize the s_ContextSlotMask to 0xffff.

This situation can occur if a VM with an attached swtpm was started
and the VM's firmware either doesn't support TPM or didn't get to
initialize the vTPM.

The following commands recreated the issue with a SeaBIOS-only VM that
had no attached hard disk but an attached TPM 2:

virsh start BIOS-only-VM ; virsh save BIOS-only-VM save.bin ; \
 virsh restore save.bin

Error: Failed to restore domain from save.bin
error: internal error: qemu unexpectedly closed the monitor: \
2022-01-04T19:26:18.835851Z qemu-system-x86_64: tpm-emulator: Setting the stateblob (type 2) failed with a TPM error 0x3 a parameter is bad
2022-01-04T19:26:18.835899Z qemu-system-x86_64: error while loading state for instance 0x0 of device 'tpm-emulator'
2022-01-04T19:26:18.835929Z qemu-system-x86_64: load of migration failed: Input/output error

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2035731
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-01-05 08:06:46 -05:00
kpcyrd
cd8025fa6f Fix "maybe-uninitialized" warning
exp_array_size is always initialized if `rc == TPM_RC_SUCCESS` and never used
if `rc != TPM_RC_SUCCESS` but some compilers have trouble noticing this.

Signed-off-by: kpcyrd <git@rxv.cc>
2021-10-29 08:50:36 -04:00
Stefan Berger
1fb639195b tpm2: Do not write permanent state if only clock changed
To avoid timeouts on short-running commands, such as TPM2_PCR_Extend,
avoid triggering the writing of the permanent state of the TPM 2
if only the clock was updated. So the clock by itself will not cause
the permanent state to be written out anymore but there have to be
other reasons as well.

The state will still be written out upon a TPM2_Shutdown, which is
supposed to be the last command to be sent to the TPM when shutting
down the VM/vTPM. Also, the permanent state will still carry the
latest clock value if it is retrieved via control channel for
VM/VTPM suspend.

The case that may be affected, but is of lesser importance, is the one
where swtpm's volatile state is written to storage using 'swtpm_ioctl -v'
and then swtpm is terminated and restarted (similar to suspend/resume)
and the permanent state file is read from storage but does not contain
the latest clock value. In this case the go.clock will be updated when
the first command after resume is executed.

This fixes the swtpm issue https://github.com/stefanberger/swtpm/issues/597.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-15 11:45:54 -04:00
Stefan Berger
00ae50bdca tpm2: Report SM4 keysizes ing GetInfo's JSON if SM4 is enabled
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-13 19:37:20 -04:00
Stefan Berger
3cf876ecf8 tpm2: Fix indentations and comments in SM2 related code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-13 19:37:20 -04:00
JerryDevis
721f6c2e33 Support SM4
Signed-off-by: JerryDevis <seclab@huawei.com>
2021-10-12 10:57:39 -04:00
Stefan Berger
4bc846f869 build-sys: Use -DOPENSSL_SUPPRESS_DEPRECATED to suppress warning (OSSL 3)
Instead of using -Wno-deprecated-declarations use
-DOPENSSL_SUPPRESS_DEPRECATED to only suppress OpenSSL deprecated
declarations warnings.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 16:52:22 -04:00
Stefan Berger
ed94c7fd14 tpm2: Only call EVP_PKEY_CTX_set0_rsa_oaep_label when label != NULL (OSSL 3)
EVP_PKEY_CTX_set0_rsa_oaep_label must only be called with label != NULL.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-15 13:28:08 -04:00
Stefan Berger
c8a7074bb6 tpm2: Use EVP_PKEY_get1_RSA() rather than EVP_PKEY_get0_RSA (OSSL 3)
OpenSSL 3.0 has changed the signature of EVP_PKEY_get0_RSA() from

struct rsa_st *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);

to

const struct rsa_st *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey);

We now have to use EVP_PKEY_get1_RSA with this signature so that we can
access the RSA key. The signature of that function hasn't changed between
OpenSSL 1.1.0 and 3.0.0.

struct rsa_st *EVP_PKEY_get1_RSA(EVP_PKEY *pkey);

Free the additional reference held on the RSA key.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-15 13:28:08 -04:00
Stefan Berger
9eb9677795 tpm2: Marshal event sequence objects' hash state
Event sequence objects were never properly marshalled and when their state
was saved and later restored their state may have been corrupted. Fix this
now by also marshalling the state of event sequence objects.

Bump up the version of the HASH_OBJECT's header to '3' so that previously
written state can be resumed if an event sequence object is encountered
and we only unmarshal an event sequence object when the version is at least
'3'.

Fixes issue #259.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-09 12:28:17 -04:00
Stefan Berger
b06558d32b tpm2: Report supported Camellia keysizes in the JSON
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-06 13:42:06 -04:00
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