--- title: "Generate hardware-based private key (TPM2)" source_url: https://dev.digicert.com/trustedge/tutorials/generate-hardware-based-private-key-tpm2.html --- By using the `--tap` option with the `trustedge certificate` command, you can generate hardware-based private keys using a Trusted Application Platform (TAP). These keys are securely stored in a hardware module, such as a TPM, and are never exposed in software. The below steps will guide you through generating an RSA or ECC private key using a TAP. See [TAP/SMP ASN.1 key format](#tapsmp-asn.1-key-format) for more details. > **Important** > > TrustEdge CLI certificate command can be built for one and only one of the following: TPM2 (default build of TrustEdge), PKCS11 (which can handle multiple PKCS11 providers simultaneously), or TAP REMOTE. ## Before you begin Make sure you understand the following: - TrustEdge must be [installed](https://dev.digicert.com/md/trustedge/install-and-configure.md) on a [supported device](https://dev.digicert.com/md/trustedge/system-requirements.md). - The user running TrustEdge CLI commands **must be** a member of the `trustedge` group. - Use `groups "$(whoami)"` to see group membership. - Use `sudo adduser "$(whoami)" trustedge` to add your user to the `trustedge` group. - TrustEdge must already own the TPM. See [Take ownership of a TPM](https://dev.digicert.com/md/trustedge/tutorials/take-ownership-of-a-tpm.md). > **Info** > > The [TCG TPM 2.0 Library Specification](https://trustedcomputinggroup.org/resource/tpm-library-specification/) (Rev 1.83, Mar 2024) does not define post-quantum algorithms. As a result, TPM-backed (TAP) keys currently support only RSA and ECC. ## Step 1: Choose the algorithm and key parameters 1. For RSA keys, specify the key size with `--size ` and include the `--tap` option. ```bash trustedge certificate --algorithm RSA --size 2048 --tap --output-file RSA_TAP_2048.pem ``` 2. For ECC keys, specify the curve type with `--curve ` and include the `--tap` option. ```bash trustedge certificate --algorithm ECC --curve P256 --tap --output-file ECC_TAP_P256.pem ``` ## Step 2: Configure optional TAP parameters You can further customize the TAP key generation by specifying additional TAP-related options, such as key usage or signature schemes. 1. For RSA keys with specific TAP key usage: ```bash trustedge certificate --algorithm RSA --size 2048 --tap --tap-key-usage TAP_KEY_USAGE_SIGNING --output-file RSA_TAP_SIGN_2048.pem ``` 2. For ECC keys with a specific TAP signature scheme: ```bash trustedge certificate --algorithm ECC --curve P256 --tap --tap-sig-scheme TAP_SIG_SCHEME_ECDSA_SHA256 --output-file ECC_TAP_SIGN_P256.pem ``` > **Tip** > > To enhance the security of your private key, you can protect it with a password by including the `--protect` option with any of the commands above. After running the command, you will be prompted to enter a password to protect the private key. ## Step 3: Verify the key creation After generating the key, verify that the file was created correctly by listing the contents of the directory: ```bash ls -l /etc/digicert/keystore/keys/ ``` This command will display the files in the `keystore/keys/` directory, including the newly created TAP-protected private key. ## TAP/SMP ASN.1 key format This section describes the ASN.1 module for TAP keys. The `PrivateKeyInfo` structure is defined by [RFC 5208 Public-Key Cryptography Standards (PKCS) #8](https://datatracker.ietf.org/doc/html/rfc5208) and includes an algorithm identifier and the private key data, which is wrapped in an **OCTET STRING**. These are the Object Identifiers (OIDs) used to build the OIDs in this document: > **Info** > > *mocana* is **1.3.6.1.4.1.14421** ```bash mocana OBJECT IDENTIFIER ::= { iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) mocana (14421) mocana-tap OBJECT IDENTIFIER ::= { mocana 19 } ``` When the following OIDs are used in an `AlgorithmIdentifier`, the parameters MUST be present and MUST be NULL: ```bash mocana-tap-rsakey OBJECT IDENTIFIER ::= { mocana-tap 0x01 } mocana-tap-ecckey OBJECT IDENTIFIER ::= { mocana-tap 0x02 } mocana-tap-rsaProtectedkey OBJECT IDENTIFIER ::= { mocana-tap 0x21 } mocana-tap-eccProtectedkey OBJECT IDENTIFIER ::= { mocana-tap 0x22 } ``` **Example definitions:** For RSA keys, the `PrivateKey` will be an OCTET STRING wrapping data encoded as follows: ```bash MocanaTapRSAPrivateKey ::= SEQUENCE { version Version, moduleId ModuleId, modulus INTEGER, publicExponent INTEGER, tapPrivateKey TapPrivateKey } ``` For ECC keys, the `PrivateKey` will be an OCTET STRING wrapping data encoded as follows (`ECParameters` and `ECPoint` are defined in other standards, such as [RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480)): ```bash MocanaTapECCPrivateKey ::= SEQUENCE { version Version, moduleId ModuleId, parameters ECParameters, publicKey ECPoint, tapPrivateKey TapPrivateKey } ``` ## About TPM2 key storage - TrustEdge creates the private key in the TPM owner hierarchy and persists it to an unused persistent object handle. - The handle and the `--key-alias` you specify are returned in the command output. Either can be used later by TrustEdge to reference the key. - Keys never leave the TPM in plaintext. Only public key material is written to /etc/digicert/keystore/keys.