InMemoryKeyManagementService

class InMemoryKeyManagementService(keyStore: MutableMap<KeyId, KeyPair> = ConcurrentHashMap(), keyMetadata: MutableMap<KeyId, Algorithm> = ConcurrentHashMap()) : KeyManagementService

Native TrustWeave in-memory Key Management Service.

Constructor maps: passing a ConcurrentHashMap shares it with the service (the caller sees later changes). Any other MutableMap is copied once at construction; the caller's original is then a stale snapshot. BouncyCastle is used through a private provider instance and is not registered globally with java.security.Security.

This is a built-in, native TrustWeave implementation that uses Java's built-in cryptographic providers for key generation, storage, and signing. It's designed for development, testing, and scenarios where a full HSM or cloud KMS is not required or available.

Thread Safety: This implementation is fully thread-safe. All operations use thread-safe ConcurrentHashMap for key storage and are executed on Dispatchers.IO for I/O-bound cryptographic operations.

Key Storage: Keys are stored in memory only and will be lost when the service instance is destroyed. This makes it ideal for:

  • Development and testing

  • Single-process applications

  • Ephemeral key management scenarios

For production use, consider using a persistent KMS provider:

  • AWS KMS (FIPS 140-3 Level 3 validated)

  • Azure Key Vault

  • Google Cloud KMS

  • HashiCorp Vault

  • Hardware Security Modules (HSMs)

Algorithm Support:

  • Ed25519: Requires Java 15+ or BouncyCastle provider

  • secp256k1, P-256, P-384, P-521: Supported via Java's standard EC provider

Input Validation:

  • Key IDs are validated for length (max 256 characters) and non-blank

  • Signing data is validated for non-empty and size limits (max 10 MB to prevent DoS)

  • Duplicate key IDs are rejected during generation

  • Algorithm compatibility is checked before signing

Security Considerations:

  • Private keys are stored in memory and are accessible to the JVM process

  • Keys are not encrypted at rest (they're in memory)

  • Suitable for development/testing, not for production secrets

  • Consider using a hardware-backed KMS for production

Example:

// Create an in-memory KMS instance
val kms = InMemoryKeyManagementService()

// Generate a key
val result = kms.generateKey(Algorithm.Ed25519)
when (result) {
is GenerateKeyResult.Success -> {
val keyHandle = result.keyHandle
println("Key created: ${keyHandle.id}")

// Sign data
val signResult = kms.sign(keyHandle.id, "Hello, World!".toByteArray())
when (signResult) {
is SignResult.Success -> println("Signature: ${signResult.signature.toHexString()}")
is SignResult.Failure -> println("Error: ${signResult.reason}")
}
}
is GenerateKeyResult.Failure -> println("Error: ${result.reason}")
}

Via Factory API:

import org.trustweave.kms.*

val kms = KeyManagementServices.create("inmemory")

Constructors

Link copied to clipboard
constructor(keyStore: MutableMap<KeyId, KeyPair> = ConcurrentHashMap(), keyMetadata: MutableMap<KeyId, Algorithm> = ConcurrentHashMap())

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard

Clears all keys from memory.

Link copied to clipboard
open suspend override fun deleteKey(keyId: KeyId): DeleteKeyResult
Link copied to clipboard
open suspend fun generateKey(algorithmName: String, options: Map<String, Any?>): GenerateKeyResult
open suspend override fun generateKey(algorithm: Algorithm, options: Map<String, Any?>): GenerateKeyResult
Link copied to clipboard

Gets the number of keys currently stored in memory.

Link copied to clipboard
open suspend override fun getPublicKey(keyId: KeyId): GetPublicKeyResult
Link copied to clipboard
open suspend override fun getSupportedAlgorithms(): Set<Algorithm>
Link copied to clipboard
open suspend fun sign(keyId: KeyId, data: ByteArray, algorithmName: String?): SignResult
open suspend override fun sign(keyId: KeyId, data: ByteArray, algorithm: Algorithm?): SignResult
Link copied to clipboard
open suspend fun supportsAlgorithm(algorithmName: String): Boolean
open suspend fun supportsAlgorithm(algorithm: Algorithm): Boolean