Cryptography API Reference
The Cryptography API enables secure cryptographic operations within your TERE applications. All operations occur within the Trusted Execution Environment, ensuring that sensitive keys and data remain protected even if the host system is compromised. TERE now supports both software-based cryptography and hardware security modules (HSM) for enhanced security.
Getting Started
Prerequisites
To use the Cryptography API, you need:
- The TERE SDK installed (
npm install @praecise/tere
) - A deployed TERE application (see the Runtime API)
- For HSM features: TERE deployed on a compatible Confidential VM
How Cryptography is Used
The Cryptography API allows your applications to:
- Encrypt and decrypt sensitive data
- Generate secure cryptographic hashes
- Create and manage encryption keys
- Generate cryptographically strong random numbers
- Integrate with cloud key management services (KMS)
- Utilize hardware security modules (HSM) for enhanced protection
Working with Cryptography
The Cryptography API is used through the TERE SDK. Here's how to use it:
Using Cryptography in TypeScript/JavaScript
For JavaScript or TypeScript applications, use the TERE SDK:
Client-side Usage
From your client application, you interact with crypto functions through function execution:
Cryptography API Reference
The following API is available within your code running in the TEE:
Encrypts data using AES-GCM with a 256-bit key.
Parameters
data
: Uint8ArrayRequiredThe data to encrypt
key
: Uint8ArrayRequiredThe 32-byte (256-bit) encryption key
options
: objectOptional configuration for encryption
Returns
The encrypted data with the nonce prepended (first 12 bytes)
Examples
Basic encryption
With custom options
Decrypts data that was encrypted using AES-GCM.
Parameters
encryptedData
: Uint8ArrayRequiredThe encrypted data with nonce prepended (first 12 bytes)
key
: Uint8ArrayRequiredThe 32-byte (256-bit) encryption key
options
: objectOptional configuration for decryption
Returns
The decrypted data
Examples
Decrypting data
With associated data
Computes a SHA-256 hash of the input data.
Parameters
data
: Uint8ArrayRequiredThe data to hash
Returns
The 32-byte SHA-256 hash value
Examples
Computing a hash
Generates a cryptographically secure random key for AES-256 encryption.
Parameters
options
: objectOptional configuration for key generation
Returns
A 32-byte (256-bit) random key
Examples
Generating a software key
Generating an HSM-backed key
Derives an encryption key from a password using PBKDF2-HMAC-SHA256.
Parameters
password
: stringRequiredThe password to derive the key from
salt
: Uint8ArrayRequiredRandom salt value for key derivation (16 bytes recommended)
iterations
: numberRequiredNumber of iterations (recommend at least 100,000)
options
: objectOptional configuration for key derivation
Returns
A 32-byte (256-bit) derived key
Examples
Deriving a key from a password
Generates cryptographically secure random bytes.
Parameters
length
: numberRequiredThe number of random bytes to generate
Returns
An array of random bytes
Examples
Generating random data
Hardware Security Module (HSM) API
TERE now supports Google Cloud HSM for hardware-backed cryptographic operations. This provides FIPS 140-2 Level 3 certified protection for your most sensitive keys and operations.
HSM features are only available when your TERE application is deployed on a compatible Confidential VM with HSM support.
Creates a cryptography provider that uses HSM for operations.
Parameters
options
: objectConfiguration for the HSM provider
Returns
A provider object for HSM-backed operations
Examples
Using an HSM provider
Creates a new key in the HSM.
Parameters
keyId
: stringRequiredID for the key
purpose
: stringRequiredKey purpose: 'encrypt', 'sign', or 'decrypt'
algorithm
: stringOptional algorithm specification
Returns
Information about the created key
Examples
Creating an HSM key
Creating a signing key
Encrypts data using an HSM-backed key.
Parameters
data
: Uint8ArrayRequiredData to encrypt
keyId
: stringRequiredID of the HSM key to use
Returns
The encrypted data
Examples
Encrypting with HSM
Decrypts data using an HSM-backed key.
Parameters
encryptedData
: Uint8ArrayRequiredData to decrypt
keyId
: stringRequiredID of the HSM key to use
Returns
The decrypted data
Examples
Decrypting with HSM
Creates a signature using an HSM-backed key.
Parameters
data
: Uint8ArrayRequiredData to sign
keyId
: stringRequiredID of the HSM signing key to use
Returns
The signature
Examples
Signing with HSM
Verifies a signature using an HSM-backed key.
Parameters
data
: Uint8ArrayRequiredOriginal data that was signed
signature
: Uint8ArrayRequiredSignature to verify
keyId
: stringRequiredID of the HSM signing key to use
Returns
True if the signature is valid
Examples
Verifying signatures with HSM
Lists all keys in the HSM key ring.
Returns
Array of key information objects
Examples
Listing HSM keys
Advanced Usage
HSM-based Key Wrapping
For enhanced security, you can use the HSM to wrap (encrypt) and unwrap (decrypt) keys:
Cloud Key Management Integration
For enterprise applications, TERE integrates with cloud key management services:
HSM-Protected Encrypted Store Example
A complete example of a secure encrypted object store with HSM protection:
Best Practices
- Use HSM for Critical Keys
Store your most sensitive keys in hardware security modules. Use HSM for root keys, key-wrapping keys, and keys that protect high-value assets.
javascript - Secure Key Storage
Always store encryption keys securely within the TEE using the State Management API. Never expose keys outside the secure environment.
- Strong Password Policies
When deriving keys from passwords, enforce strong password policies and use a high iteration count (at least 100,000) for PBKDF2 to resist brute-force attacks.
javascript - Use Authenticated Encryption
Always use authenticated encryption (AES-GCM) which is the default in TERE. This protects against tampering and ensures data integrity.
- Implement Key Rotation and Management
Regularly rotate encryption keys to limit the impact of potential key compromise. Store key version information alongside encrypted data.
javascript - Use Key Hierarchies with HSM
Implement a hierarchical key structure with HSM-protected keys at the root. Use these to protect data encryption keys that are used for actual data encryption.
javascript