Class PkDecryption
- All Implemented Interfaces:
AutoCloseable
This implements the m.megolm_backup.v1.curve25519-aes-sha2 algorithm
described in the
Matrix specification.
It is a hybrid encryption scheme utilizing Curve25519 and AES-CBC. X25519
ECDH is performed between an ephemeral key pair and a long-lived backup key
pair to establish a shared secret, from which symmetric encryption and
message authentication (MAC) keys are derived.
A PkDecryption holds a Curve25519 secret key that serves as the
long-term key to derive individual message keys. The corresponding public
key can be shared with others, allowing them to encrypt messages which can
only be decrypted using this object.
Warning: The algorithm contains a critical flaw and does not provide authentication of the ciphertext. The MAC is computed over an empty message rather than the actual ciphertext, meaning tampering with the ciphertext will not be detected.
This class implements AutoCloseable and should be used in a
try-with-resources block to ensure native resources are properly released.
- Author:
- François HERBRETEAU
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newPkDecryptionwith a fresh random Curve25519 key pair. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]Decrypts aPkMessagethat was encrypted for thisPkDecryption.static PkDecryptionCreates aPkDecryptionfrom a base64-encoded Curve25519 secret key.pickleLegacy(byte[] pickleKey) Pickles thisPkDecryptioninto the legacy libolm pickle format.Returns the Curve25519 public key associated with thisPkDecryption.Returns the Curve25519 secret key of thisPkDecryption.static PkDecryptionunpickleLegacy(String pickleData, byte[] pickleKey) Restores aPkDecryptionfrom a legacy libolm pickle format.Methods inherited from class NativeHandle
checkNotClosed, close, invalidate, nativePtr
-
Constructor Details
-
PkDecryption
public PkDecryption()Creates a newPkDecryptionwith a fresh random Curve25519 key pair.
-
-
Method Details
-
fromKey
Creates aPkDecryptionfrom a base64-encoded Curve25519 secret key.The secret key is used as the long-term key to derive individual message keys. The corresponding public key can be obtained via
publicKey()after creation.- Parameters:
key- the base64-encoded Curve25519 secret key (32 bytes)- Returns:
- a new
PkDecryptioninstance - Throws:
ConversionException- if the key is not a valid 32-byte base64-encoded key
-
secretKey
Returns the Curve25519 secret key of thisPkDecryption.If persistence is required, securely store this key. It can be used to reconstruct the
PkDecryptionobject for decrypting associated messages viafromKey(String).- Returns:
- the base64-encoded Curve25519 secret key
- Throws:
IllegalStateException- if thisPkDecryptionhas been closed
-
publicKey
Returns the Curve25519 public key associated with thisPkDecryption.This key can be shared with others to allow them to create a
PkEncryptionobject and encrypt messages that only thisPkDecryptioncan decrypt.- Returns:
- the base64-encoded Curve25519 public key
- Throws:
IllegalStateException- if thisPkDecryptionhas been closed
-
decrypt
Decrypts aPkMessagethat was encrypted for thisPkDecryption.The decryption performs X25519 ECDH between the secret key and the message's ephemeral key to derive the symmetric keys, then verifies the MAC and decrypts the AES-CBC ciphertext.
- Parameters:
message- thePkMessageto decrypt- Returns:
- the decrypted plaintext bytes
- Throws:
IllegalStateException- if thisPkDecryptionhas been closedKeyException- if the ephemeral key in the message is invalidDecryptionException- if the MAC verification fails or the padding is invalid
-
unpickleLegacy
Restores aPkDecryptionfrom a legacy libolm pickle format. The pickle must be encrypted with the provided key.- Parameters:
pickleData- the libolm pickle datapickleKey- the key used to encrypt the pickle data- Returns:
- a restored
PkDecryption - Throws:
PickleException- if the data cannot be decrypted or deserialized
-
pickleLegacy
Pickles thisPkDecryptioninto the legacy libolm pickle format. The pickle is encrypted with the provided key.- Parameters:
pickleKey- the key used to encrypt the pickle data- Returns:
- the libolm pickle data
- Throws:
IllegalStateException- if thisPkDecryptionhas been closedPickleException- if the pickling fails
-