Package org.drasyl.crypto.sodium
Class DrasylSodiumWrapper
- java.lang.Object
-
- org.drasyl.crypto.sodium.DrasylSodiumWrapper
-
public class DrasylSodiumWrapper extends Object
Simple wrapper class that make native class easier.
-
-
Field Summary
Fields Modifier and Type Field Description static short
CURVE25519_PUBLICKEYBYTES
static short
CURVE25519_SECRETKEYBYTES
static short
ED25519_BYTES
static short
ED25519_PUBLICKEYBYTES
static short
ED25519_SECRETKEYBYTES
static short
SESSIONKEYBYTES
static short
SHA256_BYTES
static short
SIGN_BYTES
static short
XCHACHA20POLY1305_IETF_ABYTES
static short
XCHACHA20POLY1305_IETF_NPUBBYTES
-
Constructor Summary
Constructors Constructor Description DrasylSodiumWrapper(Sodium sodium)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
convertPublicKeyEd25519ToCurve25519(byte[] curve, byte[] ed)
Converts a public ed25519 key to a public curve25519 key.boolean
convertSecretKeyEd25519ToCurve25519(byte[] curve, byte[] ed)
Converts a secret ed25519 key to a secret curve25519 key.byte[]
cryptoAeadXChaCha20Poly1305IetfDecrypt(byte[] c, byte[] ad, byte[] nPub, byte[] k)
This function decrypts the given ciphertextc
.byte[]
cryptoAeadXChaCha20Poly1305IetfEncrypt(byte[] m, byte[] ad, byte[] nPub, byte[] k)
This function encrypts the given messagem
.SessionPair
cryptoKxClientSessionKeys(byte[] clientPk, byte[] clientSk, byte[] serverPk)
This function computes a pair of shared keys (rx and tx) using the client's public key clientPk, the client's secret key clientSk and the server's public key serverPk.SessionPair
cryptoKxServerSessionKeys(byte[] serverPk, byte[] serverSk, byte[] clientPk)
This function computes a pair of shared keys (rx and tx) using the client's public key clientPk, the server's secret key serverSk and the server's public key serverPk.byte[]
cryptoSignDetached(byte[] message, byte[] secretKey)
Returns a signature for a message.boolean
cryptoSignKeypair(byte[] publicKey, byte[] secretKey)
Generate a signing keypair (ed25519).boolean
cryptoSignVerifyDetached(byte[] signature, byte[] message, byte[] publicKey)
Verifies thatsignature
is valid for themessage
.Sodium
getSodium()
byte[]
sha256(byte[] in)
Generates a SHA-256 hash of the given input.boolean
successful(int res)
Evaluates the return value of a native sodium function call.
-
-
-
Field Detail
-
SHA256_BYTES
public static final short SHA256_BYTES
- See Also:
- Constant Field Values
-
ED25519_PUBLICKEYBYTES
public static final short ED25519_PUBLICKEYBYTES
- See Also:
- Constant Field Values
-
ED25519_SECRETKEYBYTES
public static final short ED25519_SECRETKEYBYTES
- See Also:
- Constant Field Values
-
ED25519_BYTES
public static final short ED25519_BYTES
- See Also:
- Constant Field Values
-
CURVE25519_PUBLICKEYBYTES
public static final short CURVE25519_PUBLICKEYBYTES
- See Also:
- Constant Field Values
-
CURVE25519_SECRETKEYBYTES
public static final short CURVE25519_SECRETKEYBYTES
- See Also:
- Constant Field Values
-
SESSIONKEYBYTES
public static final short SESSIONKEYBYTES
- See Also:
- Constant Field Values
-
XCHACHA20POLY1305_IETF_ABYTES
public static final short XCHACHA20POLY1305_IETF_ABYTES
- See Also:
- Constant Field Values
-
XCHACHA20POLY1305_IETF_NPUBBYTES
public static final short XCHACHA20POLY1305_IETF_NPUBBYTES
- See Also:
- Constant Field Values
-
SIGN_BYTES
public static final short SIGN_BYTES
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DrasylSodiumWrapper
public DrasylSodiumWrapper(Sodium sodium)
-
-
Method Detail
-
sha256
public byte[] sha256(byte[] in) throws CryptoException
Generates a SHA-256 hash of the given input.- Parameters:
in
- the input to hash- Returns:
- SHA-256 hash of the input
- Throws:
CryptoException
-
cryptoSignKeypair
public boolean cryptoSignKeypair(byte[] publicKey, byte[] secretKey)
Generate a signing keypair (ed25519).- Parameters:
publicKey
- Public key.secretKey
- Secret key.- Returns:
- True if successful.
-
convertPublicKeyEd25519ToCurve25519
public boolean convertPublicKeyEd25519ToCurve25519(byte[] curve, byte[] ed)
Converts a public ed25519 key to a public curve25519 key.- Parameters:
curve
- The array in which the generated key will be placed.ed
- The public key in ed25519.- Returns:
- Return true if the conversion was successful.
-
convertSecretKeyEd25519ToCurve25519
public boolean convertSecretKeyEd25519ToCurve25519(byte[] curve, byte[] ed)
Converts a secret ed25519 key to a secret curve25519 key.- Parameters:
curve
- The array in which the generated key will be placed.ed
- The secret key in ed25519.- Returns:
- Return true if the conversion was successful.
-
cryptoKxServerSessionKeys
public SessionPair cryptoKxServerSessionKeys(byte[] serverPk, byte[] serverSk, byte[] clientPk) throws CryptoException
This function computes a pair of shared keys (rx and tx) using the client's public key clientPk, the server's secret key serverSk and the server's public key serverPk.- Parameters:
serverPk
- Server public key of sizeCURVE25519_PUBLICKEYBYTES
.serverSk
- Server secret key of sizeCURVE25519_SECRETKEYBYTES
.clientPk
- Client public key of sizeCURVE25519_PUBLICKEYBYTES
.- Returns:
- True if successful or false if the client public key is wrong.
- Throws:
CryptoException
-
cryptoKxClientSessionKeys
public SessionPair cryptoKxClientSessionKeys(byte[] clientPk, byte[] clientSk, byte[] serverPk) throws CryptoException
This function computes a pair of shared keys (rx and tx) using the client's public key clientPk, the client's secret key clientSk and the server's public key serverPk.- Parameters:
clientPk
- Client public key of sizeCURVE25519_PUBLICKEYBYTES
.clientSk
- Client secret key of sizeCURVE25519_SECRETKEYBYTES
.serverPk
- Server public key of sizeCURVE25519_PUBLICKEYBYTES
.- Returns:
- True if successful or false if the server public key is wrong.
- Throws:
CryptoException
-
cryptoAeadXChaCha20Poly1305IetfEncrypt
public byte[] cryptoAeadXChaCha20Poly1305IetfEncrypt(byte[] m, byte[] ad, byte[] nPub, byte[] k)
This function encrypts the given messagem
.- Parameters:
m
- the message as byte arrayad
- the authentication tagnPub
- the public noncek
- the key for encryption- Returns:
- the encrypted message or
null
on failure
-
cryptoAeadXChaCha20Poly1305IetfDecrypt
public byte[] cryptoAeadXChaCha20Poly1305IetfDecrypt(byte[] c, byte[] ad, byte[] nPub, byte[] k)
This function decrypts the given ciphertextc
.- Parameters:
c
- the cipher textad
- the authentication tagnPub
- the public noncek
- the key for encryption- Returns:
- the decrypted message or
null
on failure
-
cryptoSignDetached
public byte[] cryptoSignDetached(byte[] message, byte[] secretKey)
Returns a signature for a message. This does not prepend the signature to the message.- Parameters:
message
- The message to sign.secretKey
- The secret key.- Returns:
- the signature or
null
on failure
-
cryptoSignVerifyDetached
public boolean cryptoSignVerifyDetached(byte[] signature, byte[] message, byte[] publicKey)
Verifies thatsignature
is valid for themessage
.- Parameters:
signature
- The signature.message
- The message.publicKey
- The public key that signed the message.- Returns:
- Returns true if the signature is valid for the message.
-
successful
public boolean successful(int res)
Evaluates the return value of a native sodium function call.- Parameters:
res
- the result of the function call- Returns:
- true if call was successful, otherwise false
-
getSodium
public Sodium getSodium()
-
-