Last active
July 6, 2023 07:46
-
-
Save idesai/676f27edc6eac87bbcead18c739a3122 to your computer and use it in GitHub Desktop.
Protected write & undefine operations for NV index
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
#!/bin/bash | |
# | |
# Copyright (C) 2021 Imran Desai | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
# | |
# | |
# Goal: Protect the write and undefine operations of an NV Index | |
# | |
# Constraints: | |
# 1. NV index must be readable by all | |
# 2. NV index can only be written by authenticating signing authority. | |
# 3. NV index can only be undefined by authenticating signing authority. | |
# 4. TPM2_CC_Clear shouldn't be able to delete the NV index. | |
# 5. Platform hierarchy authorization must not be required after provisioning. | |
# | |
# set -e | |
cleanup() { | |
rm -f session.ctx signing_key_public.pem signing_key.ctx signing_key.name \ | |
write.policy undefine.policy nvwrite_OR_nvundefine.policy to_sign.bin \ | |
signature.dat | |
tpm2_flushcontext session.ctx 2>/dev/null || true | |
} | |
# trap cleanup EXIT | |
setup_signing_authority() { | |
openssl genrsa -out signing_key_private.pem 2048 | |
openssl rsa -in signing_key_private.pem -outform PEM -pubout -out \ | |
signing_key_public.pem | |
tpm2_loadexternal -G rsa -C n -u signing_key_public.pem -c signing_key.ctx \ | |
-n signing_key.name | |
} | |
setup_nv_index_policies() { | |
tpm2_flushcontext session.ctx 2>/dev/null || true | |
tpm2_startauthsession -S session.ctx | |
tpm2 policysigned -S session.ctx -c signing_key.ctx -L write.policy | |
tpm2_policycommandcode -S session.ctx -L undefine.policy \ | |
TPM2_CC_NV_UndefineSpaceSpecial | |
tpm2_flushcontext session.ctx 2>/dev/null || true | |
tpm2_startauthsession -S session.ctx | |
tpm2_policyor -S session.ctx -L nvwrite_OR_nvundefine.policy \ | |
-l sha256:write.policy,undefine.policy | |
tpm2_flushcontext session.ctx 2>/dev/null || true | |
} | |
satisfy_nv_write_policy() { | |
tpm2_flushcontext session.ctx 2>/dev/null || true | |
tpm2_startauthsession -S session.ctx --policy-session | |
tpm2_policysigned -S session.ctx -c signing_key.ctx -x \ | |
--raw-data to_sign.bin | |
openssl dgst -sha256 -sign signing_key_private.pem -out signature.dat \ | |
to_sign.bin | |
tpm2_policysigned -S session.ctx -g sha256 -s signature.dat -f rsassa \ | |
-c signing_key.ctx -x | |
tpm2_policyor -S session.ctx -l sha256:write.policy,undefine.policy | |
} | |
satisfy_nv_undefine_policy() { | |
tpm2_flushcontext session.ctx 2>/dev/null || true | |
tpm2_startauthsession -S session.ctx --policy-session | |
tpm2_policysigned -S session.ctx -c signing_key.ctx -x --raw-data \ | |
to_sign.bin | |
openssl dgst -sha256 -sign signing_key_private.pem -out signature.dat \ | |
to_sign.bin | |
tpm2_policysigned -S session.ctx -g sha256 -s signature.dat -f rsassa \ | |
-c signing_key.ctx -x | |
tpm2_policycommandcode -S session.ctx TPM2_CC_NV_UndefineSpaceSpecial | |
tpm2_policyor -S session.ctx -l sha256:write.policy,undefine.policy | |
} | |
# | |
# Sample Flow | |
# | |
<<'SAMPLE-FLOW' | |
setup_signing_authority | |
setup_nv_index_policies | |
# Define NV index | |
tpm2_nvdefine -C p 0x1500019 --size 6 --policy nvwrite_OR_nvundefine.policy \ | |
--attributes "authread|policydelete|policywrite|platformcreate" | |
# Write NV index | |
satisfy_nv_write_policy | |
echo "6byte" > 1.txt | |
tpm2_nvwrite 0x1500019 -P "session:session.ctx" -i 1.txt | |
# Read NV index contents | |
tpm2_nvread 0x1500019 | hexdump -Cv | |
# Undefine NV index | |
satisfy_nv_undefine_policy | |
tpm2_nvundefine --session session.ctx 0x1500019 | |
# Cleanup | |
cleanup | |
exit 0 | |
SAMPLE-FLOW |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment