Package org.drasyl.crypto
Class Crypto
java.lang.Object
org.drasyl.crypto.Crypto
Util class that provides cryptography functions for drasyl.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final SecureRandomstatic final Cryptostatic final shortstatic final shortstatic final shortstatic final short -
Method Summary
Modifier and TypeMethodDescriptionstatic intCompares two keysk1andk2to allow to form a total order on the keys.Converts the given ed25519 long timepublicKeyinto a curve25519 key for (on-demand) key agreement.Converts the given ed25519 long timekeyPairinto a curve25519 key pair for (on-demand) key agreement.byte[]decrypt(byte[] cipher, byte[] authTag, Nonce nonce, SessionPair sessionPair) Decrypt the givencipher, by verify theauthTagas an authentication tag, uses the givennonceand decrypting with the rx part of thesessionPair.byte[]encrypt(byte[] message, byte[] authTag, Nonce nonce, SessionPair sessionPair) Encrypts the givenmessage, by addingauthTagas an authentication tag, using the given (hopefully fresh)nonceand encrypting with the tx part of thesessionPair.Generates a new curve25519 key pair for key exchange.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 themyKeyPairandreceiverKeyPair.Is only for internal usage.static byte[]randomBytes(int entropy) Generates a secure random bytes with the givenentropy.static intrandomNumber(int bound) Generates a random number with the staticSecureRandomof this class.static StringrandomString(int entropy) Generates a secure random HEX String with the givenentropyof bytes.byte[]sign(byte[] message, IdentitySecretKey secretKey) Creates a signature for the givenmessagewith the givensecretKeyin detached mode (signature is not appended to message, rather it is standalone).booleanverifySignature(byte[] signature, byte[] message, IdentityPublicKey publicKey) Verifies thatsignatureis valid for themessage.
-
Field Details
-
INSTANCE
-
CSPRNG
-
PK_LONG_TIME_KEY_LENGTH
public static final short PK_LONG_TIME_KEY_LENGTH- See Also:
-
SK_LONG_TIME_KEY_LENGTH
public static final short SK_LONG_TIME_KEY_LENGTH- See Also:
-
PK_CURVE_25519_KEY_LENGTH
public static final short PK_CURVE_25519_KEY_LENGTH- See Also:
-
SK_CURVE_25519_KEY_LENGTH
public static final short SK_CURVE_25519_KEY_LENGTH- See Also:
-
-
Method Details
-
compare
Compares two keysk1andk2to 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
Generates a secure random HEX String with the givenentropyof 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 staticSecureRandomof 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
intvalue between zero (inclusive) andbound(exclusive) from this random number generator's sequence
-
getSodium
Is only for internal usage.- Returns:
- returns the
DrasylSodiumWrapperinstance.
-
generateLongTimeKeyPair
public KeyPair<IdentityPublicKey,IdentitySecretKey> generateLongTimeKeyPair() throws CryptoExceptionGenerates 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 CryptoExceptionConverts the given ed25519 long timekeyPairinto 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- ifkeyParor a key within the pair isnull
-
convertIdentityKeyToKeyAgreementKey
public KeyAgreementPublicKey convertIdentityKeyToKeyAgreementKey(IdentityPublicKey publicKey) throws CryptoException Converts the given ed25519 long timepublicKeyinto 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 CryptoExceptionGenerates 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 CryptoExceptionGenerates session key pair from themyKeyPairandreceiverKeyPair.- 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 addingauthTagas an authentication tag, using the given (hopefully fresh)nonceand 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- ifmessageorauthTagisnull
-
decrypt
public byte[] decrypt(byte[] cipher, byte[] authTag, Nonce nonce, SessionPair sessionPair) throws CryptoException Decrypt the givencipher, by verify theauthTagas an authentication tag, uses the givennonceand 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- ifmessageorauthTagisnull
-
sign
Creates a signature for the givenmessagewith the givensecretKeyin 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
Verifies thatsignatureis valid for themessage.- Parameters:
signature- the signature of the messagemessage- the messagepublicKey- the public key that signed the message- Returns:
trueif the signature is valid for the message
-