Overview
The encryption APIs provide secure cryptographic operations for Muun Wallet, including:- ECDH-based encryption: Using elliptic curve Diffie-Hellman for key agreement
- AES-CBC encryption: With PKCS7 padding or no padding for fixed-size data
- Key encryption: Password-based encryption using scrypt for key derivation
- HD key encryption/decryption: Hierarchical deterministic key encryption support
Encryption Interfaces
Encrypter
Interface for encrypting payloads.Encrypt
Encrypts data and returns a serialized string containing all information needed for decryption.The plaintext data to encrypt
Serialized encrypted payload with metadata
Error if encryption fails
Decrypter
Interface for decrypting payloads.Decrypt
Decrypts a payload that was encrypted using an Encrypter.The serialized encrypted payload
The decrypted data
Error if decryption fails or authentication fails
HD Key Encryption
Encrypter Methods on HDPrivateKey
Encrypter (Self-Encryption)
Creates an encrypter that encrypts data to the same private key (self-encryption).An encrypter configured for self-encryption
EncrypterTo
Creates an encrypter that encrypts data to a different recipient’s public key.The recipient’s public key
An encrypter configured for the recipient
Decrypter (Self-Decryption)
Creates a decrypter for data encrypted to this private key by itself.A decrypter configured for self-encrypted data
DecrypterFrom
Creates a decrypter for data encrypted by a specific sender.The sender’s public key (can be nil for anonymous sender)
A decrypter configured for the sender
Encryption Operations (Gomobile Compatibility)
These wrapper types provide Gomobile-compatible encryption operations.NewEncryptOperation
Creates an encryption operation for self-encryption.The private key to encrypt with
The data to encrypt
An encryption operation ready to execute
EncryptOperation.Encrypt
Executes the encryption operation.The encrypted payload
NewDecryptOperation
Creates a decryption operation for self-encrypted data.The private key to decrypt with
The encrypted payload
A decryption operation ready to execute
NewDecryptOperationFrom
Creates a decryption operation for data encrypted by a specific sender.The sender’s public key
The private key to decrypt with
The encrypted payload
A decryption operation ready to execute
DecryptOperation.Decrypt
Executes the decryption operation.The decrypted data
Low-Level Encryption Functions
encryptWithPubKey
Low-level ECDH encryption using a public key.The recipient’s public key
The data to encrypt (must be AES block size aligned for no-padding mode)
The ephemeral public key generated for this encryption
The encrypted data
Error if encryption fails
- Generate ephemeral key pair (privEph, pubEph)
- Compute shared secret: ECDH(privEph, pubKey)
- Use last 16 bytes of compressed pubEph as IV
- Encrypt using AES-256-CBC with no padding (plaintext must be block-aligned)
decryptWithPrivKey
Low-level ECDH decryption using a private key.The recipient’s private key
The ephemeral public key (33 bytes compressed)
The encrypted data
The decrypted data
Error if decryption fails
- Parse ephemeral public key from rawPubEph
- Recover shared secret: ECDH(privKey, pubEph)
- Use last 16 bytes of rawPubEph as IV
- Decrypt using AES-256-CBC with no padding
Key Encryption with Passphrase
KeyEncrypt
Encrypts an HD private key using a passphrase. Uses scrypt for key derivation and AES-CBC-PKCS7 for encryption.The HD private key to encrypt
The user’s passphrase
The encrypted key with version, path, and scrypt parameters encoded
Error if encryption fails
- Version information
- Derivation path
- Scrypt parameters (N, r, p)
- Salt for key derivation
- AES IV
- Ciphertext
KeyDecrypt
Decrypts a key encrypted with KeyEncrypt.The encrypted key string from KeyEncrypt
The user’s passphrase
The network configuration
The decrypted key with its derivation path
Error if decryption fails or passphrase is incorrect
Utility Functions
randomBytes
Generates cryptographically secure random bytes.The number of random bytes to generate
Cryptographically secure random bytes
This function panics if the system random number generator fails, as this indicates a critical system issue.
Encryption Algorithms
ECDH Key Agreement
- Curve: secp256k1 (Bitcoin’s elliptic curve)
- Key Agreement: Standard ECDH (Elliptic Curve Diffie-Hellman)
- Shared Secret: Used directly as AES-256 key
AES Encryption
- Algorithm: AES-256-CBC
- Key Size: 256 bits (32 bytes)
- IV Size: 128 bits (16 bytes)
- IV Source: Last 16 bytes of compressed ephemeral public key
- Padding:
- No padding for fixed-size data (e.g., 64-byte key material)
- PKCS7 padding for variable-size data (passphrase encryption)
Scrypt Key Derivation
Used inKeyEncrypt/KeyDecrypt for passphrase-based encryption:
- Algorithm: scrypt
- Output: 256 bits (32 bytes) for AES-256
- Parameters: Encoded in the encrypted output
- Salt: Random, stored with encrypted data
Security Considerations
Best Practices
- Use HD Key Encryption for wallet-to-wallet encryption (better than raw ECDH)
- Use KeyEncrypt for backups when you need passphrase-based encryption
- Verify recipients before encrypting to public keys
- Store ephemeral keys - they’re required for decryption
- Use strong passphrases - scrypt is strong but not immune to weak passwords
Important Notes
- IV Derivation: The IV is derived from the ephemeral public key, not randomly generated. This is secure because each encryption uses a fresh ephemeral key pair.
- No Padding: Low-level encryption functions use AES-CBC with no padding, so plaintext must be block-aligned (multiple of 16 bytes).
- Authenticated Encryption: The current implementation does not include authentication tags (like AEAD). Integrity relies on the encryption scheme itself.