Package org.drasyl.crypto
Class Crypto
- java.lang.Object
-
- org.drasyl.crypto.Crypto
-
public class Crypto extends Object
Util class that provides cryptography functions for drasyl.
-
-
Field Summary
Fields Modifier and Type Field Description static SecureRandom
CSPRNG
static Crypto
INSTANCE
static short
PK_CURVE_25519_KEY_LENGTH
static short
PK_LONG_TIME_KEY_LENGTH
static short
SK_CURVE_25519_KEY_LENGTH
static short
SK_LONG_TIME_KEY_LENGTH
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static int
compare(Key k1, Key k2)
Compares two keysk1
andk2
to allow to form a total order on the keys.KeyAgreementPublicKey
convertIdentityKeyToKeyAgreementKey(IdentityPublicKey publicKey)
Converts the given ed25519 long timepublicKey
into a curve25519 key for (on-demand) key agreement.KeyPair<KeyAgreementPublicKey,KeyAgreementSecretKey>
convertLongTimeKeyPairToKeyAgreementKeyPair(KeyPair<IdentityPublicKey,IdentitySecretKey> keyPair)
Converts the given ed25519 long timekeyPair
into a curve25519 key pair for (on-demand) key agreement.byte[]
decrypt(byte[] cipher, byte[] authTag, Nonce nonce, SessionPair sessionPair)
Decrypt the givencipher
, by verify theauthTag
as an authentication tag, uses the givennonce
and decrypting with the rx part of thesessionPair
.byte[]
encrypt(byte[] message, byte[] authTag, Nonce nonce, SessionPair sessionPair)
Encrypts the givenmessage
, by addingauthTag
as an authentication tag, using the given (hopefully fresh)nonce
and encrypting with the tx part of thesessionPair
.KeyPair<KeyAgreementPublicKey,KeyAgreementSecretKey>
generateEphemeralKeyPair()
Generates a new curve25519 key pair for key exchange.KeyPair<IdentityPublicKey,IdentitySecretKey>
generateLongTimeKeyPair()
Generates a new ed25519 key pair for signing and on-demand encryption.<P extends PublicKey,S extends SecretKey>
SessionPairgenerateSessionKeyPair(KeyPair<P,S> myKeyPair, PublicKey receiverPublicKey)
Generates session key pair from themyKeyPair
andreceiverKeyPair
.DrasylSodiumWrapper
getSodium()
Is only for internal usage.static byte[]
randomBytes(int entropy)
Generates a secure random bytes with the givenentropy
.static int
randomNumber(int bound)
Generates a random number with the staticSecureRandom
of this class.static String
randomString(int entropy)
Generates a secure random HEX String with the givenentropy
of bytes.byte[]
sha256(byte[] input)
Generates a SHA-256 hash of the given input.byte[]
sign(byte[] message, IdentitySecretKey secretKey)
Creates a signature for the givenmessage
with the givensecretKey
in detached mode (signature is not appended to message, rather it is standalone).boolean
verifySignature(byte[] signature, byte[] message, IdentityPublicKey publicKey)
Verifies thatsignature
is valid for themessage
.
-
-
-
Field Detail
-
INSTANCE
public static final Crypto INSTANCE
-
CSPRNG
public static final SecureRandom CSPRNG
-
PK_LONG_TIME_KEY_LENGTH
public static final short PK_LONG_TIME_KEY_LENGTH
- See Also:
- Constant Field Values
-
SK_LONG_TIME_KEY_LENGTH
public static final short SK_LONG_TIME_KEY_LENGTH
- See Also:
- Constant Field Values
-
PK_CURVE_25519_KEY_LENGTH
public static final short PK_CURVE_25519_KEY_LENGTH
- See Also:
- Constant Field Values
-
SK_CURVE_25519_KEY_LENGTH
public static final short SK_CURVE_25519_KEY_LENGTH
- See Also:
- Constant Field Values
-
-
Method Detail
-
compare
public static int compare(Key k1, Key k2)
Compares two keysk1
andk2
to allow to form a total order on the keys. This is especially important in asynchronous environments to make deterministic decisions.- Parameters:
k1
- first keyk2
- second key- Returns:
- -1 if the first key is smaller than, 0 if equals to, 1 if greater than the second key
-
randomString
public static String randomString(int entropy)
Generates a secure random HEX String with the givenentropy
of bytes.Recommendation:
- 4 byte for small sets
- 8 bytes for unique internal strings, e.g. hash tables
- 16 bytes for global uniqueness, e.g. auth token
- 24 bytes for cryptographic operations, e.g. nonce's
You can also use the following probability table for the "Birthday problem", as a starting point for a suitable entropy size: Birthday problem probability table
- Parameters:
entropy
- entropy in bytes- Returns:
- a secure random HEX String
-
randomBytes
public static byte[] randomBytes(int entropy)
Generates a secure random bytes with the givenentropy
.Recommendation:
- 4 byte for small sets
- 8 bytes for unique internal strings, e.g. hash tables
- 16 bytes for global uniqueness, e.g. auth token
- 24 bytes for cryptographic operations, e.g. nonce's
You can also use the following probability table for the "Birthday problem", as a starting point for a suitable entropy size: Birthday problem probability table
- Parameters:
entropy
- entropy in bytes- Returns:
- a secure random bytes
-
randomNumber
public static int randomNumber(int bound)
Generates a random number with the staticSecureRandom
of this class. Avoids overhead of generating a new instance ofSecureRandom
.- Parameters:
bound
- the upper bound (exclusive). Must be positive.- Returns:
- the next pseudorandom, uniformly distributed
int
value between zero (inclusive) andbound
(exclusive) from this random number generator's sequence
-
getSodium
public DrasylSodiumWrapper getSodium()
Is only for internal usage.- Returns:
- returns the
DrasylSodiumWrapper
instance.
-
sha256
public byte[] sha256(byte[] input) 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
-
generateLongTimeKeyPair
public KeyPair<IdentityPublicKey,IdentitySecretKey> generateLongTimeKeyPair() throws CryptoException
Generates a new ed25519 key pair for signing and on-demand encryption. This key pair can be used as identity of a node.- Returns:
- new ed25519 key pair
- Throws:
CryptoException
- if any error occurs during key generation
-
convertLongTimeKeyPairToKeyAgreementKeyPair
public KeyPair<KeyAgreementPublicKey,KeyAgreementSecretKey> convertLongTimeKeyPairToKeyAgreementKeyPair(KeyPair<IdentityPublicKey,IdentitySecretKey> keyPair) throws CryptoException
Converts the given ed25519 long timekeyPair
into a curve25519 key pair for (on-demand) key agreement.- Parameters:
keyPair
- the ed25519 long time key pair- Returns:
- ed25519 key pair as curve25519
- Throws:
CryptoException
- if any error occurs during conversionNullPointerException
- ifkeyPar
or a key within the pair isnull
-
convertIdentityKeyToKeyAgreementKey
public KeyAgreementPublicKey convertIdentityKeyToKeyAgreementKey(IdentityPublicKey publicKey) throws CryptoException
Converts the given ed25519 long timepublicKey
into a curve25519 key for (on-demand) key agreement.- Parameters:
publicKey
- the ed25519 public key- Returns:
- ed25519 public key as curve25519
- Throws:
CryptoException
- if any error occurs during conversion
-
generateEphemeralKeyPair
public KeyPair<KeyAgreementPublicKey,KeyAgreementSecretKey> generateEphemeralKeyPair() throws CryptoException
Generates a new curve25519 key pair for key exchange. This key should only be used for one session and never be re-used.- Returns:
- new curve25519 key pair
- Throws:
CryptoException
- if any error occurs during key generation
-
generateSessionKeyPair
public <P extends PublicKey,S extends SecretKey> SessionPair generateSessionKeyPair(KeyPair<P,S> myKeyPair, PublicKey receiverPublicKey) throws CryptoException
Generates session key pair from themyKeyPair
andreceiverKeyPair
.- Parameters:
myKeyPair
- my own curve25519 key pair (long time or ephemeral)receiverPublicKey
- the receiver public key (long time or ephemeral)- Returns:
- a session key for sending and receiving messages
- Throws:
CryptoException
- if any error occurs during generation
-
encrypt
public byte[] encrypt(byte[] message, byte[] authTag, Nonce nonce, SessionPair sessionPair) throws CryptoException
Encrypts the givenmessage
, by addingauthTag
as an authentication tag, using the given (hopefully fresh)nonce
and encrypting with the tx part of thesessionPair
.- Parameters:
message
- the message to encryptauthTag
- some authentication tagnonce
- the fresh noncesessionPair
- the session pair- Returns:
- encrypted message
- Throws:
CryptoException
- if any error occurs during encryptionNullPointerException
- ifmessage
orauthTag
isnull
-
decrypt
public byte[] decrypt(byte[] cipher, byte[] authTag, Nonce nonce, SessionPair sessionPair) throws CryptoException
Decrypt the givencipher
, by verify theauthTag
as an authentication tag, uses the givennonce
and decrypting with the rx part of thesessionPair
.- Parameters:
cipher
- the cipher text to decryptauthTag
- some authentication tagnonce
- the fresh noncesessionPair
- the session pair- Returns:
- decrypted message
- Throws:
CryptoException
- if any error occurs during decryptionNullPointerException
- ifmessage
orauthTag
isnull
-
sign
public byte[] sign(byte[] message, IdentitySecretKey secretKey) throws CryptoException
Creates a signature for the givenmessage
with the givensecretKey
in detached mode (signature is not appended to message, rather it is standalone).- Parameters:
message
- the message to signsecretKey
- the secret key to sign- Returns:
- the signature of the message
- Throws:
CryptoException
- if any error occurs during signing
-
verifySignature
public boolean verifySignature(byte[] signature, byte[] message, IdentityPublicKey publicKey)
Verifies thatsignature
is valid for themessage
.- Parameters:
signature
- the signature of the messagemessage
- the messagepublicKey
- the public key that signed the message- Returns:
true
if the signature is valid for the message
-
-