tpm2_duplicate - Man Page

Duplicates a loaded object so that it may be used in a different hierarchy.

Synopsis

tpm2_duplicate [Options]

Description

tpm2_duplicate(1) - This tool duplicates a loaded object so that it may be used in a different hierarchy. The new parent key for the duplicate may be on the same or different TPM or TPM_RH_NULL.

Options

These options control the key importation process:

Objects that can move outside of TPM need to be protected (confidentiality and integrity). For instance, transient objects require that TPM protected data (key or seal material) be stored outside of the TPM. This is seen in tools like tpm2_create(1), where the -r option outputs this protected data. This blob contains the sensitive portions of the object. The sensitive portions of the object are protected by the parent object, using the parent’s symmetric encryption details to encrypt the sensitive data and HMAC it.

In-depth details can be found in sections 23 of:

Notably Figure 20, is relevant, even though it’s specifically referring to duplication blobs, the process is identical.

If the output is from tpm2_duplicate(1), the output will be slightly different, as described fully in section 23.

References

Context Object Format

The type of a context object, whether it is a handle or file name, is determined according to the following logic in-order:

Authorization Formatting

Authorization for use of an object in TPM2.0 can come in 3 different forms: 1. Password 2. HMAC 3. Sessions

NOTE: “Authorizations default to the EMPTY PASSWORD when not specified”.

Passwords

Passwords are interpreted in the following forms below using prefix identifiers.

Note: By default passwords are assumed to be in the string form when they do not have a prefix.

String

A string password, specified by prefix “str:” or it’s absence (raw string without prefix) is not interpreted, and is directly used for authorization.

Examples

foobar
str:foobar

Hex-string

A hex-string password, specified by prefix “hex:” is converted from a hexidecimal form into a byte array form, thus allowing passwords with non-printable and/or terminal un-friendly characters.

Example

hex:1122334455667788

File

A file based password, specified be prefix “file:” should be the path of a file containing the password to be read by the tool or a “-” to use stdin. Storing passwords in files prevents information leakage, passwords passed as options can be read from the process list or common shell history features.

Examples

# to use stdin and be prompted
file:-

# to use a file from a path
file:path/to/password/file

# to echo a password via stdin:
echo foobar | tpm2_tool -p file:-

# to use a bash here-string via stdin:

tpm2_tool -p file:- <<< foobar

Sessions

When using a policy session to authorize the use of an object, prefix the option argument with the session keyword. Then indicate a path to a session file that was created with tpm2_startauthsession(1). Optionally, if the session requires an auth value to be sent with the session handle (eg policy password), then append a + and a string as described in the Passwords section.

Examples

To use a session context file called session.ctx.

session:session.ctx

To use a session context file called session.ctx AND send the authvalue mypassword.

session:session.ctx+mypassword

To use a session context file called session.ctx AND send the HEX authvalue 0x11223344.

session:session.ctx+hex:11223344

PCR Authorizations

You can satisfy a PCR policy using the “pcr:” prefix and the PCR minilanguage. The PCR minilanguage is as follows: <pcr-spec>=<raw-pcr-file>

The PCR spec is documented in in the section “PCR bank specifiers”.

The raw-pcr-file is an optional argument that contains the output of the raw PCR contents as returned by tpm2_pcrread(1).

PCR bank specifiers

Examples

To satisfy a PCR policy of sha256 on banks 0, 1, 2 and 3 use a specifier of:

pcr:sha256:0,1,2,3

specifying AUTH.

Algorithm Specifiers

Options that take algorithms support “nice-names”.

There are two major algorithm specification string classes, simple and complex. Only certain algorithms will be accepted by the TPM, based on usage and conditions.

Simple specifiers

These are strings with no additional specification data. When creating objects, non-specified portions of an object are assumed to defaults. You can find the list of known “Simple Specifiers” below.

Asymmetric

  • rsa
  • ecc

Symmetric

  • aes
  • camellia
  • sm4

Hashing Algorithms

  • sha1
  • sha256
  • sha384
  • sha512
  • sm3_256
  • sha3_256
  • sha3_384
  • sha3_512

Keyed Hash

  • hmac
  • xor

Signing Schemes

  • rsassa
  • rsapss
  • ecdsa
  • ecdaa
  • ecschnorr
  • sm2

Asymmetric Encryption Schemes

  • oaep
  • rsaes
  • ecdh

Modes

  • ctr
  • ofb
  • cbc
  • cfb
  • ecb

Misc

  • null

Complex Specifiers

Objects, when specified for creation by the TPM, have numerous algorithms to populate in the public data. Things like type, scheme and asymmetric details, key size, etc. Below is the general format for specifying this data: <type>:<scheme>:<symmetric-details>

Type Specifiers

This portion of the complex algorithm specifier is required. The remaining scheme and symmetric details will default based on the type specified and the type of the object being created.

  • aes - Default AES: aes128
  • aes128<mode> - 128 bit AES with optional mode (ctr|ofb|cbc|cfb|ecb). If mode is not specified, defaults to null.
  • aes192<mode> - Same as aes128<mode>, except for a 192 bit key size.
  • aes256<mode> - Same as aes128<mode>, except for a 256 bit key size.
  • sm4 - Default SM4: sm4128
  • sm4128 or sm4_128 <mode> - 128 bit SM4 with optional mode (ctr|ofb|cbc|cfb|ecb). If mode is not specified, defaults to null.
  • ecc - Elliptical Curve, defaults to ecc256.
  • ecc192 or ecc_nist_p192 - 192 bit ECC NIST curve
  • ecc224 or ecc_nist_p224 - 224 bit ECC NIST curve
  • ecc256 or ecc_nist_p256 - 256 bit ECC NIST curve
  • ecc384 or ecc_nist_p384 - 384 bit ECC NIST curve
  • ecc521 or ecc_nist_p521 - 521 bit ECC NIST curve
  • ecc_sm2 or ecc_sm2_p256 - 256 bit SM2 curve
  • rsa - Default RSA: rsa2048
  • rsa1024 - RSA with 1024 bit keysize.
  • rsa2048 - RSA with 2048 bit keysize.
  • rsa3072 - RSA with 3072 bit keysize.
  • rsa4096 - RSA with 4096 bit keysize.

Scheme Specifiers

Next, is an optional field, it can be skipped.

Schemes are usually Signing Schemes or Asymmetric Encryption Schemes. Most signing schemes take a hash algorithm directly following the signing scheme. If the hash algorithm is missing, it defaults to sha256. Some take no arguments, and some take multiple arguments.

Hash Optional Scheme Specifiers

These scheme specifiers are followed by a dash and a valid hash algorithm, For example: oaep-sha256.

  • oaep
  • ecdh
  • rsassa
  • rsapss
  • ecdsa
  • ecschnorr
  • sm2

Multiple Option Scheme Specifiers

This scheme specifier is followed by a count (max size UINT16) then followed by a dash(-) and a valid hash algorithm. * ecdaa For example, ecdaa4-sha256. If no count is specified, it defaults to 4.

No Option Scheme Specifiers

This scheme specifier takes NO arguments. * rsaes

Symmetric Details Specifiers

This field is optional, and defaults based on the type of object being created and it’s attributes. Generally, any valid Symmetric specifier from the Type Specifiers list should work. If not specified, an asymmetric objects symmetric details defaults to aes128cfb.

Examples

Create an rsa2048 key with an rsaes asymmetric encryption scheme

tpm2_create -C parent.ctx -G rsa2048:rsaes -u key.pub -r key.priv

Create an ecc256 key with an ecdaa signing scheme with a count of 4 and sha384 hash

/tpm2_create -C parent.ctx -G ecc256:ecdaa4-sha384 -u key.pub -r key.priv cryptographic algorithms ALGORITHM.

Common Options

This collection of options are common to many programs and provide information that many users may expect.

TCTI Configuration

The TCTI or “Transmission Interface” is the communication mechanism with the TPM. TCTIs can be changed for communication with TPMs across different mediums.

To control the TCTI, the tools respect:

  1. The command line option -T or --tcti
  2. The environment variable: TPM2TOOLS_TCTI.

Note: The command line option always overrides the environment variable.

The current known TCTIs are:

The arguments to either the command line option or the environment variable are in the form:

<tcti-name>:<tcti-option-config>

Specifying an empty string for either the <tcti-name> or <tcti-option-config> results in the default being used for that portion respectively.

TCTI Defaults

When a TCTI is not specified, the default TCTI is searched for using dlopen(3) semantics. The tools will search for tabrmd, device and mssim TCTIs IN THAT ORDER and USE THE FIRST ONE FOUND. You can query what TCTI will be chosen as the default by using the -v option to print the version information. The “default-tcti” key-value pair will indicate which of the aforementioned TCTIs is the default.

Custom TCTIs

Any TCTI that implements the dynamic TCTI interface can be loaded. The tools internally use dlopen(3), and the raw tcti-name value is used for the lookup. Thus, this could be a path to the shared library, or a library name as understood by dlopen(3) semantics.

Tcti Options

This collection of options are used to configure the various known TCTI modules available:

Examples

Example-1: Duplicate a key created with a policy that allows for duplication

tpm2_startauthsession -S session.dat
tpm2_policycommandcode -S session.dat -L policy.dat TPM2_CC_Duplicate
tpm2_flushcontext session.dat

tpm2_createprimary -C o -g sha256 -G rsa -c primary.ctxt
tpm2_create -C primary.ctxt -g sha256 -G rsa -r key.prv -u key.pub  -c key.ctxt \
-L policy.dat -a "sensitivedataorigin|userwithauth|decrypt|sign"

tpm2_createprimary -C o -g sha256 -G ecc -c new_parent.ctxt

tpm2_startauthsession \--policy-session -S session.dat
tpm2_policycommandcode -S session.dat -L policy.dat TPM2_CC_Duplicate
tpm2_duplicate -C new_parent.ctxt -c key.ctxt -G null -p "session:session.dat" \
-r duprv.bin -s seed.dat
tpm2_flushcontext session.dat

Example-2: As an end-to-end example, the following will transfer an RSA key generated on TPM-A to TPM-B

On TPM-B

Create a parent object that will be used to wrap/transfer the key.

tpm2_createprimary -C o -g sha256 -G rsa -c primary.ctx

tpm2_create  -C primary.ctx -g sha256 -G rsa \
-r new_parent.prv  -u new_parent.pub \
-a "restricted|sensitivedataorigin|decrypt|userwithauth"

Copy new_parent.pub to TPM-A.

On TPM-A

Create root object and auth policy allows duplication only

tpm2_createprimary -C o -g sha256 -G rsa -c primary.ctx

tpm2_startauthsession -S session.dat

tpm2_policycommandcode -S session.dat -L dpolicy.dat TPM2_CC_Duplicate

tpm2_flushcontext session.dat

rm session.dat

Generate an RSA keypair on TPM-A that will be duplicated (note the passphrase is `foo')

tpm2_create -C primary.ctx -g sha256 -G rsa -p foo -r key.prv \
-u key.pub  -L dpolicy.dat -a "sensitivedataorigin|userwithauth|decrypt|sign"

tpm2_load -C primary.ctx -r key.prv -u key.pub -c key.ctx

tpm2_readpublic -c key.ctx -o dup.pub

Test sign and encryption locally (so we can compare later that the same key was transferred).

echo "meet me at.." >file.txt
tpm2_rsaencrypt -c key.ctx  -o data.encrypted file.txt
tpm2_sign -c key.ctx -g sha256 -f plain -p foo -o sign.raw file.txt

Compare the signature hash (we will use this later to confirm the key was transferred to TPM-B):

sha256sum sign.raw

a1b4e3fbaa29e6e46d95cff498150b6b8e7d9fd21182622e8f5a3ddde257879e

Start an auth session and policy command to allow duplication

tpm2_startauthsession --policy-session -S session.dat

tpm2_policycommandcode -S session.dat -L dpolicy.dat TPM2_CC_Duplicate

Load the new_parent.pub file transferred from TPM-B

tpm2_loadexternal -C o -u new_parent.pub -c new_parent.ctx

Start the duplication

tpm2_duplicate -C new_parent.ctx -c key.ctx -G null  \
-p "session:session.dat" -r dup.dpriv -s dup.seed

Copy the following files to TPM-B: * dup.pub * dup.dpriv * dup.seed * (optionally data.encrypted just to test decryption)

On TPM-B

Start an auth,policy session

tpm2_startauthsession --policy-session -S session.dat

tpm2_policycommandcode -S session.dat -L dpolicy.dat TPM2_CC_Duplicate

Load the context we used to transfer

tpm2_flushcontext --transient-object

tpm2_load -C primary.ctx -u new_parent.pub -r new_parent.prv -c new_parent.ctx

Import the duplicated context against the parent we used

tpm2_import -C new_parent.ctx -u dup.pub -i dup.dpriv \
-r dup.prv -s dup.seed -L dpolicy.dat

Load the duplicated key context

tpm2_flushcontext --transient-object

tpm2_load -C new_parent.ctx -u dup.pub -r dup.prv -c dup.ctx

Test the imported key matches

  • Sign

    echo "meet me at.." >file.txt
    
    tpm2_sign -c dup.ctx -g sha256 -o sig.rss -p foo file.txt
    
    dd if=sig.rss of=sign.raw bs=1 skip=6 count=256

Compare the signature file hash:

$ sha256sum sign.raw

a1b4e3fbaa29e6e46d95cff498150b6b8e7d9fd21182622e8f5a3ddde257879e
  • Decryption

    tpm2_flushcontext --transient-object
    
    tpm2_rsadecrypt -p foo -c dup.ctx -o data.ptext data.encrypted
    
    # cat data.ptext 
    meet me at..

Example-3: Exporting an OpenSSL RSA key for a remote TPM

To securely send an OpenSSL generated RSA key to a remote TPM such that only that remote TPM will be able to load it, and without exposing the private key to the host operating system on the remote machine:

  • On the destination TPM-B, create a primary context and read its public key, then send primary.pub to the source machine:

    tpm2_createprimary -c primary.ctx
    tpm2_readpublic -c primary.ctx -o primary.pub
  • On the source machine create the RSA private key and wrap it for the destination TPM’s public key. Similar to tpm2_makecredential, this step should not require a TPM.

    openssl genrsa -out rsa.pem
    tpm2_duplicate -U primary.pub -G rsa -k rsa.pem -u rsa.pub -r rsa.dpriv -s rsa.seed
  • Send the rsa.pub, rsa.dpriv and rsa.seed to the destination TPM-B and import the files, which will decrypt them using the primary.ctx to produce rsa.priv, which can then be loaded and used as a TPM key:

    tpm2_import -C primary.ctx -G rsa -i rsa.dpriv -s rsa.seed -u rsa.pub -r rsa.priv
    tpm2_load -C primary.ctx -c rsa.ctx -u rsa.pub -r rsa.priv

Returns

Tools can return any of the following codes:

Bugs

Github Issues (https://github.com/tpm2-software/tpm2-tools/issues)

Help

See the Mailing List (https://lists.linuxfoundation.org/mailman/listinfo/tpm2)

Referenced By

tpm2_changeauth(1), tpm2_create(1), tpm2_createak(1), tpm2_import(1), tpm2_policyduplicationselect(1).

tpm2-tools General Commands Manual