Class Ecies
- All Implemented Interfaces:
AutoCloseable
ECIES is a hybrid encryption scheme that uses elliptic curve Diffie-Hellman (X25519) for shared secret establishment and ChaCha20-Poly1305 for symmetric encryption of individual messages. HMAC-SHA256 is used as the key derivation function.
ECIES allows a party (the initiator) to establish a communication channel toward another party (the recipient) given knowledge of only its public key. The initiator's key pair is ephemeral and generated anew for each new channel. The initiator must send their ephemeral public key to the recipient so that the recipient can complete the channel establishment on their end.
Since the initiator's key is unauthenticated, the recipient has no way of
knowing who is contacting them. To protect against active man-in-the-middle
(MITM) attacks, an out-of-band confirmation is required after channel
establishment using the CheckCode facility.
This class implements AutoCloseable and should be used in a
try-with-resources block to ensure native resources are properly released.
Once a channel is established via establishOutboundChannel(String, byte[]) or
establishInboundChannel(String), this Ecies object is consumed and
can no longer be used.
- Author:
- François HERBRETEAU
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionestablishInboundChannel(String message) Establishes an inbound ECIES channel from an initial message encrypted by the other side.establishOutboundChannel(String theirPublicKey, byte[] initialPlaintext) Establishes an outbound ECIES channel using the other side's Curve25519 public key and an initial plaintext message.Returns the ephemeral Curve25519 public key associated with this session.static EciesCreates a new, random, unestablished ECIES session with the given application info.Methods inherited from class NativeHandle
checkNotClosed, close, invalidate, nativePtr
-
Constructor Details
-
Ecies
public Ecies()Creates a new, random, unestablished ECIES session.This method uses the
MATRIX_QR_CODE_LOGINapplication info prefix. If you are using this for a different purpose, consider usingwithInfo(String)instead.
-
-
Method Details
-
withInfo
Creates a new, random, unestablished ECIES session with the given application info.The application info is used to derive the various secrets and provide domain separation. Both parties must use the same application info to successfully establish a channel.
- Parameters:
info- the application info string- Returns:
- a new
Eciessession
-
publicKey
Returns the ephemeral Curve25519 public key associated with this session.This public key needs to be sent to the other side so that they can establish an ECIES channel.
- Returns:
- the base64-encoded public key
- Throws:
IllegalStateException- if thisEcieshas been closed or consumed by channel establishment
-
establishOutboundChannel
public OutboundCreationResult establishOutboundChannel(String theirPublicKey, byte[] initialPlaintext) Establishes an outbound ECIES channel using the other side's Curve25519 public key and an initial plaintext message.After the channel has been established, the returned
EstablishedEciescan be used to encrypt and decrypt further messages. The other side uses the initial message to establish the same channel on their end viaestablishInboundChannel(String).This method consumes the
Eciesobject — after this call, theEciesis invalidated and can no longer be used. The native resources are transferred to the returnedOutboundCreationResult.- Parameters:
theirPublicKey- a base64-encoded Curve25519 public keyinitialPlaintext- the initial plaintext message to encrypt- Returns:
- an
OutboundCreationResultcontaining the established channel and the initial message - Throws:
IllegalStateException- if thisEcieshas been closed or consumedKeyException- if the given public key is invalid or cannot be decodedEciesException- if channel establishment fails, e.g. due to a non-contributory key
-
establishInboundChannel
Establishes an inbound ECIES channel from an initial message encrypted by the other side.The initial message is obtained from
OutboundCreationResult.initialMessage()and contains the sender's ephemeral public key along with the ciphertext. After the channel has been established, the returnedEstablishedEciescan be used to encrypt and decrypt further messages.This method consumes the
Eciesobject — after this call, theEciesis invalidated and can no longer be used. The native resources are transferred to the returnedInboundCreationResult.- Parameters:
message- the initial message received from the initiator, as produced byOutboundCreationResult.initialMessage()- Returns:
- an
InboundCreationResultcontaining the established channel and the decrypted plaintext - Throws:
IllegalStateException- if thisEcieshas been closed or consumedEciesException- if channel establishment fails, e.g. due to a non-contributory key or a malformed message
-