Commit Graph

1178 Commits

Author SHA1 Message Date
Stefan Berger
09c9ea1a44 tpm2: Add missing semicolons after pAsserts
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-12-28 13:42:28 -05:00
Stefan Berger
01ffc69132 tpm2: Comment and rearrange some code
Rearrange the call to RsaInitializeExponentOld in CryptyRsaGenerateKey
so that this function can be removed from the OpenSSLCryptRsaGenerateKey
implementations.

Sync the PackExponent and UnpackExponent functions calls with upstream
but comment them since libtpms does not used them (will not remove
privateExponent from OBJECT).

Add comments to diffs between libtpms and upstream.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
95e54b0d9c tpm2: Sync MarshalSensitive with upstream
Since we never set the RSA_prime_flag or pack Q, dP, dQ, qInv into the
sensitive->rsa TPM 2, dactivate the block checking this flag because
it will never be used.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
5620106e47 tpm2: Sync ObjectLoad with upstream
Sync ObjectLoad with upstream. Now CryptRsaLoadPrivateExponent may be
called with the object parameter being NULL, which happens only when
it is called from TPM2_Import().

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
66a503b709 tpm2: Add TPMT_PUBLIC & TPMT_SENSITIVE params to CryptRsaLoadPrivateExponent
In the next patch there will be ObjectLoad calling
CryptRsaLoadPrivateExponent without providing an OBJECT to this function
but only to use it to check the consistency of TPMT_PUBLIC and
TPMT_SENSITIVE parameters. To allow this, add these two parameter to the
function (= sync with upstream) but also keep the rsaKey as an optional
parameter (libtpms only) so that the flag attributes.privateExp can still
be set on the key in case the values for dP, dQ, qINv are valid.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
Stefan Berger
8532cd818a tpm2: Sync part of CryptRsaLoadPrivateExponent with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 21:20:07 -04:00
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
c9d8fb30bc man: Fix spelling error
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
Stefan Berger
2ef7f12aed CHANGES: Write a note about changes to SignedCompareB()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-04-17 12:28:47 -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