-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated scripts as per review comments
- Loading branch information
1 parent
111359c
commit db408ec
Showing
11 changed files
with
343 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 15 additions & 12 deletions
27
...cripts/tpm-examples/tpm_err_aes_nvread.sh → ...ipts/tpm-examples/tpm_error_aes_nvread.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
#!/bin/bash | ||
|
||
#PCR index, NV index, and output file for the AES-256 key | ||
pcr_index=10 | ||
nv_index=0x1400004 | ||
output_file="aes-key" | ||
|
||
# Read the AES-256 key from the TPM NV index with the wrong PCR index to produce tpm error. | ||
if tpm2_nvread $nv_index -P pcr:sha256:$pcr_index -s 768 > $output_file; then | ||
echo "AES-256 key successfully read from TPM NV index and saved to $output_file." | ||
else | ||
echo "Error: Reading the TPM NV index failed because of wrong PCR index value." | ||
#!/bin/bash | ||
|
||
#PCR index, NV index, and output file for the AES-256 key | ||
incorrect_pcr_index=10 | ||
#user need to define / manually select the nv_index | ||
nv_index=0x1400002 | ||
aes_key_size=32 | ||
|
||
output_file="aes-key" | ||
|
||
# Read the AES-256 key from the TPM NV index with the wrong PCR index to produce tpm error. | ||
if tpm2_nvread $nv_index -P pcr:sha256:$incorrect_pcr_index -s $aes_key_size > $output_file; then | ||
echo "AES-256 key successfully read from TPM NV index and saved to $output_file." | ||
else | ||
echo "Error: Reading the TPM NV index failed because of wrong PCR index value." | ||
fi |
37 changes: 19 additions & 18 deletions
37
...ripts/tpm-examples/tpm_err_aes_nvwrite.sh → ...pts/tpm-examples/tpm_error_aes_nvwrite.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
#!/bin/bash | ||
#R index, NV index, and aes output file for the AES-256 key | ||
pcr_index=10 | ||
nv_index=0x1400004 | ||
aes_key_file="aes-256-key" | ||
|
||
# Generate a random AES-256 key (32 bytes) to overwrite the key which is already present in the nv index | ||
openssl rand -out $aes_key_file 32 | ||
|
||
# Save the PCR16 value to pcr16.dat | ||
tpm2_pcrread -o pcr16.dat sha256:$pcr_index | ||
|
||
# Write the AES-256 key to the TPM NV index with the PCR policy | ||
# input -> given the wrong index value which is not linked with pcr16.dat to produce tpm error. | ||
if tpm2_nvwrite $nv_index -P pcr:sha256:$pcr_index=pcr16.dat -i $aes_key_file; then | ||
echo "AES-256 key successfully written to TPM NV index." | ||
else | ||
echo "Error: Writing to TPM NV index failed. Please create the NV index first or check whether your policy is valid or not" | ||
#!/bin/bash | ||
|
||
#PCR index, NV index, and aes output file for the AES-256 key | ||
incorrect_pcr_index=10 | ||
#user need to define / manually select the nv_index | ||
nv_index=0x1400002 | ||
aes_key_file="aes-256-key" | ||
aes_key_size=32 | ||
|
||
# Generate a random AES-256 key (32 bytes) to overwrite the key which is already present in the nv index | ||
openssl rand -out $aes_key_file $aes_key_size | ||
|
||
|
||
# Write the AES-256 key to the TPM NV index with the PCR policy | ||
# input -> given the wrong index value which is not linked with pcr16.dat to produce tpm error. | ||
if tpm2_nvwrite $nv_index -P pcr:sha256:$incorrect_pcr_index -i $aes_key_file; then | ||
echo "AES-256 key successfully written to TPM NV index." | ||
else | ||
echo "Error: Writing to TPM NV index failed. Please create the NV index first or check whether your policy is valid or not" | ||
fi |
47 changes: 47 additions & 0 deletions
47
recipes-support/tpm-test-scripts/tpm-examples/tpm_nv_passphrase_read.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/bin/bash | ||
|
||
# Define the PCR index, NV index, and output file for the AES-256 key | ||
pcr_index=16 | ||
#user need to define / manually select the nv_index | ||
nv_index=0x1400003 | ||
#user can modify the passphrase_size | ||
passphrase_size=32 | ||
# Execute tpm2_getcap to get the list of defined NV indexes | ||
defined_nv_indexes=$(tpm2_getcap handles-nv-index) | ||
|
||
# Check if the NV index is defined | ||
if [[ $defined_nv_indexes == *"$nv_index"* ]]; then | ||
echo "NV index $nv_index is defined" | ||
else | ||
echo "NV index $nv_index is not defined. So can't able to read the value from this nv index $nv_index" | ||
exit 1 | ||
fi | ||
|
||
# Read the AES-256 key from the TPM NV index with the specified PCR policy | ||
if tpm2_nvread $nv_index -P pcr:sha256:$pcr_index -s $passphrase_size > password_retrived; then | ||
echo "password_retrived file is successfully readed from TPM NV index and saved to password_retrived." | ||
else | ||
echo "Error: Reading the TPM NV index failed." | ||
exit 1 | ||
fi | ||
|
||
# Decrypt the private key | ||
openssl rsa -in private_key.pem -out unencrypted_private_key.pem -passin file:password_retrived | ||
|
||
# Extract the corresponding public key | ||
openssl rsa -in unencrypted_private_key.pem -pubout -out public_key.pem | ||
|
||
# Verify the signature using the public key | ||
if openssl dgst -sha256 -verify public_key.pem -signature signature.bin data.txt; then | ||
echo "Signature verified successfully." | ||
else | ||
echo "Signature verification failed." | ||
exit 1 | ||
fi | ||
|
||
# Clean up temporary files | ||
rm unencrypted_private_key.pem | ||
rm public_key.pem | ||
#rm password_retrived | ||
|
||
echo "Script execution complete." |
48 changes: 48 additions & 0 deletions
48
recipes-support/tpm-test-scripts/tpm-examples/tpm_nv_passphrase_write.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/bash | ||
|
||
# Define the PCR index, NV index, and output file for the AES-256 key | ||
pcr_index=16 | ||
#user need to define / manually select the nv_index | ||
nv_index=0x1400003 | ||
#user can modify the passphrase_size | ||
passphrase_size=32 | ||
|
||
# creating a file | ||
echo "hi everyone" > data.txt | ||
|
||
# creating password file | ||
echo "tpm!123" > password_file | ||
|
||
# Create an encrypted RSA private key with the password file | ||
if openssl genrsa -aes256 -passout file:password_file -out private_key.pem 2048; then | ||
echo "rsa private key is created successfully with the password" | ||
else | ||
echo "key creation failed" | ||
exit 1 | ||
fi | ||
|
||
# Sign the data with the encrypted private key | ||
openssl dgst -sha256 -sign private_key.pem -out signature.bin -passin file:password_file data.txt | ||
echo "data signed successfully" | ||
|
||
# Execute tpm2_getcap to get the list of defined NV indexes | ||
defined_nv_indexes=$(tpm2_getcap handles-nv-index) | ||
|
||
# Check if the NV index is defined | ||
if [[ $defined_nv_indexes == *"$nv_index"* ]]; then | ||
echo "NV index $nv_index is defined" | ||
else | ||
echo "NV index $nv_index is not defined. So defining $nv_index" | ||
tpm2_nvdefine $nv_index -s $passphrase_size -L policy16.pcr | ||
fi | ||
|
||
# Write the AES-256 key to the TPM NV index with the PCR policy | ||
if tpm2_nvwrite $nv_index -P pcr:sha256:$pcr_index -i password_file; then | ||
echo "password_file is successfully written to TPM NV index." | ||
else | ||
echo "Error: Writing to TPM NV index failed. Please create the NV index first or check whether your pcr.dat or pcr_index is valid." | ||
exit 1 | ||
fi | ||
|
||
#Clean up temporary files | ||
rm password_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.