tpm2_policysigned
First of all, it is NOT a tutorial of how policy signed works, for that, refer to A practical guide to TPM2.0, page 226, section "Digital Signatures (such as Smart Cards)"
It is a small and fast tutorial about the command "tpm2_policysigned" from the TPM2_tool library.
You can find an example of use in TPM2-Tool library that uses it but without nonce and expiration time, two concepts hard to include if you use them without an example. And for it is this tutorial.
NonceTPM
Let's start with the nonce to avoid a reply attack using the same signature to satisfy several times the access policy.
First, we create the Signing Authority (in the same way that in TPM2-Tool example):
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
Now, we will load the verification key and create the policysigned policy:
tpm2_loadexternal -C o -G rsa -u public.pem -c signing_key.ctx
tpm2_startauthsession -S session.ctx
tpm2_policysigned -S session.ctx -c signing_key.ctx -L policy_digest
tpm2_flushcontext session.ctx
You can see that to create the policy digest the only input we need is the Signing Authority public key.
Then we create a sealing object using the policy_digest as input policy:
echo "Hello world" > secret.data
tpm2_createprimary -C o -c prim.ctx
tpm2_create -u key.pub -r sealing_key.priv -c sealing_key.ctx -C prim.ctx -i secret.data -L policy_digest
Following, we start a policy-session and we take the generated nonceTPM:
tpm2_startauthsession -S session.ctx --policy-session
From session.ctx we can extract the nonceTPM directly from the binary or with the next command:
tpm2_sessionconfig session.ctx
In this example my nonce is:
414cc7c6b274ed9cf253df4b58f34d443dcf3ffe8cafb5fd267b1c0a223b7bd5
Now that we have the nonce, we can create the signature without close the session:
echo "414cc7c6b274ed9cf253df4b58f34d443dcf3ffe8cafb5fd267b1c0a223b7bd5 00 00 0000" | xxd -r -p | \
openssl dgst -sha256 -sign private.pem -out signature.dat
Note the eights "0"
And finally, we can satisfy our policy and unseal the object:
tpm2_policysigned -S session.ctx -g sha256 -s signature.dat -f rsassa \
-c signing_key.ctx -L policy_signed -x
tpm2_unseal -p session:session.ctx -c sealing_key.ctx
If we want to unseal the object another time, we need to check for the new nonceTPM and create a new signature.
Expiration time
To add an expiration time, we can reuse the same policy digest (policy_digest). We will set an expiration time of 3000 seconds (0x0BB8).
We create the new signature specifying the expiration time (remember, start a session and copy-paste the new nonceTPM) :
echo "414cc7c6b274ed9cf253df4b58f34d443dcf3ffe8cafb5fd267b1c0a223b7bd5 00 00 0BB8" | xxd -r -p | \
openssl dgst -sha256 -sign private.pem -out signature.dat
And finally, we can validate the authorization signature writing the time in decimal:
tpm2_policysigned -S session.ctx -g sha256 -s signature.dat -f rsassa \
-c signing_key.ctx -L policy_signed -x -t 3000
tpm2_unseal -p session:session.ctx -c sealing_key.ctx
tpm2_flushcontext session.ctx
Notes:
IMPORTANT: Ensures that the Signing Authority never signs 0x00000000, because an attacker could use that signature to access the policy signed any number of times becoming this Policy Signed in a Password Authorization.
Just to say, "A practical guide to TPM2.0" put the expiration time in the second position when generating the signature while the TPM2-Tool put it in the last position.
For some reason, I can no use an expiration time shorter than 1000 seconds.
#Tutorials👍
3❤️
👍
💡
👏
😂
Explore More Posts
The latest tpm2-tools 5.8 release addresses three GitHub security advisories. One is a heap overflow in tpm2_getekcertificate, and the other is an out-of-bounds write that happens when too many sessions are in use. I don’t have information on the third advisory. Beyond the fixes, 5.8 extends EK certificate handling. It adds the option to read EK certificates from NV RAM, introduces new EK templates and supports a marshaled quote format. https://github.com/tpm2-software/tpm2-tools/releases/tag/5.8
Random Number Generator (TPM2)
1. IntroductionAmong other features, Trusted Platform Modules (TPM) comes with a True Random Number Generator (TRNG) that can be used for various applications including cryptographic purposes. This RNG module serves as the source for randomness for the TPM 2.0 Chip. A RNG module in TPM2 will typically contain the following -
An entropy source and collector
State Register
A mixing func.
There can be two types of calls to the TPM’s Random Number Generator, Internal call and External Call. RNG is capable to provide enough randomness for all the internal calls made. As well as, RNG is capable of providing 32 octets of randomness as per the specification to an external call. Another thing to note here is, each call made to the RNG produces a new value irrespective of the type of call. 1.1 The source of RandomnessThe source of entropy in the TPM is the entropy collector. As the name suggests, entropy collector collects entropy from entropy sources and also removes bias. A TPM2.0 has at least one internal source of entropy. These sources can include noise, clock variations, air movement amongst other events. As depicted in Fig 1.1 the entropy collector provides input to the State Register. 1.2 Stages involved Various algorithms and process supported by TPM calls RNG for unpredictable numbers, which is actually obtained from “RNG Protected Capability”. The “Deterministic Random Bit Generator (DRBG) Protected Capability“ obtains entropy from “Entropy Protected Capability”. Along with Entropy Protected Capability, TPM2_StirRandom() can also gather entropy from other entropy sources within the same TPM. 2. Random Numbers via TPM2 ToolsIf one needs to use TPM’s RNG for other external applications, then TPM2 Tools Command Line tools can be a great option. TPM2 Tools Command : tpm2_getrandom - - hex 16 - - hex : Converts output to hexadecimal 16 : Size in bytes Now, if one wants to have additional randomness, TPM2 Tools provides a convenient utility for this purpose called tpm2_stirrandom. To inject entropy via TPM2_StirRandom() to “Entropy Protected Capability” , we can use some file or use stdin. TPM2 Tools Command : echo -n “some_random_data” | tpm2_stirrandom Note : 128 bytes can only be injected using stdin at once through stirrandom() 3. ConclusionThe TPM2 provides in a sense True Random Numbers for use. These unpredictable numbers are coming from a dedicated hardware, hence can be trusted to be given as input to Crypto Algorithms amongst other applications. TPM2 also uses this for its internal use, and TCG claims it to be as secure as the strongest algorithm that TPM supports. Also, using the RNG is pretty straight forward via TPM2 Tools. #Tutorials



Comments
Start the Conversation
Hi Ernesto Gomez Marin! Thank you for contributing this "fast and small" tutorial :-) I added it to the Welcome section, so every new TPM.dev member could see it.
Oh, I almost forgot, Ernesto Gomez Marin you receive an invite for the "TPM expert" team as a thank you and recognition for your active participation and contribution to our community.