Commit Graph

1293 Commits

Author SHA1 Message Date
Stefan Berger
29dbcfa10c rev180: Move #defines from TpmProfile.h into other header files
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
25a66c4463 rev180: Move NvGetIndexName to NV_spt.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
9dfa4b4e4f rev180: Move TPM_CC_FIRST/LAST to TpmTypes.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
6d38dc72da rev180: Rename swap.h to endian_swap.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
9d71aa4299 rev180: Rename PCRSimStart to PCRManufacture
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
0043f1ba9d rev180: Replace VERIFY with GOTO_ERROR_UNLESS
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
16c9d51a3e rev180: Add Marshal.h and sync its usage with upstream
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
2c63bf6c81 rev180: Define MUST_BE and replace cAssert with it
Introduce MUST_BE #define and replace cAssert with it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
fd2e55e3a5 rev180: Add compiler dependencies header for gcc and msvc
Add compiler dependencies files for gcc and msvc and remove some of
the existing #define from CompilerDependencies.h

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
f01c9776f5 rev180: Sync whitespace, comment and trivial changes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-03 20:23:19 -05:00
Stefan Berger
920aa5e02b tpm2: Use Carmichael function for RSA priv. exponent D (>= 2048 bits)
Like OpenSSL use the Carmichael function for the RSA private exponent D
when an RSA key has >= 2048 bits and public exponent e uses more than
2 bytes. Otherwise use the Euler totient function.

The main difference is that by TPM 2 using the Carmichael function OpenSSL
now behaves the same way as when it is used by other programs that for
example load keys from PEM files where the private exponent D was
calculated with this function. The difference is seen when for example
blobs cannot be decrypted where newer versions of OpenSSL (with implicit
rejection enabled) returned results of 48 bytes every time rather than a
deterministic (for same input blob) but varying number of bytes (Euler
totient).

Switching to the Carmichael function does not have any negative impact
on interoperatibility with OpenSSL nor does it affect interoperability
between versions of TPM 2 code that did not use it. This means that
data encrypted or signed by OpenSSL or TPM 2 can be decrypted or verified
by TPM 2 or OpenSSL and that data encrypted or signed with either new
or old code in TPM 2 can be decrypted or verified with either old or
new code in TPM 2.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-02 18:16:51 -05:00
Stefan Berger
6566330177 tpm2: Compute RSA private key before saving
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-02 18:09:13 -05:00
Stefan Berger
27a3e7e2d2 tpm2: Get rid of written variable and replace with objectSize
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-02 18:09:13 -05:00
Stefan Berger
87fbcfd73d tpm2: Replace a few BOOLs with ints
Since BOOL's are effectively int's the replacement does not change anything.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-12-28 13:42:28 -05:00
Stefan Berger
e11413f2e0 tpm2: NVMarshal: Add #error when a #define's are not supported
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-12-28 13:42:28 -05:00
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