Class KeyGenerator
java.lang.Object
io.getlime.security.powerauth.crypto.lib.generator.KeyGenerator
An implementation of a high-level key generator class. Keys are generated
using elliptic curves (EC) and unless explicitly stated otherwise, they
have 128b length.
- Author:
- Petr Dvorak
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncomputeSharedKey(PrivateKey privateKey, PublicKey publicKey) Computes a pre-shared key for given private key and public key (ECDH).computeSharedKey(PrivateKey privateKey, PublicKey publicKey, boolean keep32b) Computes a pre-shared key for given private key and public key (ECDH).byte[]convert32Bto16B(byte[] original) Convert 32B byte long array to 16B long array by applying xor between first half and second half values.deriveSecretKey(SecretKey secret, byte[] index) Derive a new secret key KEY_SHARED from a master secret key KEY_MASTER based on following KDF:deriveSecretKeyFromPassword(String password, byte[] salt) Derive a long AES suitable key from a password and salt.deriveSecretKeyFromPassword(String password, byte[] salt, int iterations) Derive a long AES suitable key from a password and salt.deriveSecretKeyHmac(SecretKey secret, byte[] index) Derive a new secret key KEY_SHARED from a master secret key KEY_MASTER based on following KDF:deriveSecretKeyHmacLegacy(SecretKey secret, byte[] index) Deprecated.Generate a new ECDH key pair using P256r1 curve.byte[]generateRandomBytes(int len) Generate a new random byte array with given length.Generate a new random symmetric key.
-
Constructor Details
-
KeyGenerator
public KeyGenerator()
-
-
Method Details
-
generateKeyPair
Generate a new ECDH key pair using P256r1 curve.- Returns:
- A new key pair instance, or null in case of an error.
- Throws:
CryptoProviderException- In case key cryptography provider is incorrectly initialized.
-
convert32Bto16B
Convert 32B byte long array to 16B long array by applying xor between first half and second half values.- Parameters:
original- Original byte array, 32B long.- Returns:
- Result byte array, 16B long.
- Throws:
IllegalArgumentException
-
generateRandomBytes
Generate a new random byte array with given length.- Parameters:
len- Number of random bytes to be generated.- Returns:
- An array with len random bytes.
- Throws:
CryptoProviderException- In case key cryptography provider is incorrectly initialized.
-
generateRandomSecretKey
Generate a new random symmetric key.- Returns:
- A new instance of a symmetric key.
- Throws:
CryptoProviderException- In case key cryptography provider is incorrectly initialized.
-
deriveSecretKey
public SecretKey deriveSecretKey(SecretKey secret, byte[] index) throws InvalidKeyException, GenericCryptoException, CryptoProviderException Derive a new secret key KEY_SHARED from a master secret key KEY_MASTER based on following KDF:BYTES = index, total 16 bytes KEY_SHARED[BYTES] = AES(BYTES, KEY_MASTER)
- Parameters:
secret- A master shared key.index- A byte array index of the key.- Returns:
- A new derived key from a master key with given index.
- Throws:
InvalidKeyException- In case secret key is invalid.GenericCryptoException- In case key derivation fails.CryptoProviderException- In case cryptography provider is incorrectly initialized.
-
deriveSecretKeyHmacLegacy
@Deprecated public SecretKey deriveSecretKeyHmacLegacy(SecretKey secret, byte[] index) throws GenericCryptoException, CryptoProviderException Deprecated.Derive a new secret key KEY_SHARED from a master secret key KEY_MASTER based on following KDF:BYTES = index, total 16 bytes KEY_SHARED[BYTES] = 32B_TO_16B(HMAC-SHA256(KEY_MASTER, BYTES))
Note that this function is deprecated and should not be used for a new functionality.- Parameters:
secret- A master shared key.index- A byte array index of the key.- Returns:
- A new derived key from a master key with given index.
- Throws:
GenericCryptoException- In case key derivation fails.CryptoProviderException- In case cryptography provider is incorrectly initialized.
-
deriveSecretKeyHmac
public SecretKey deriveSecretKeyHmac(SecretKey secret, byte[] index) throws GenericCryptoException, CryptoProviderException Derive a new secret key KEY_SHARED from a master secret key KEY_MASTER based on following KDF:BYTES = index, total 16 bytes KEY_SHARED[BYTES] = 32B_TO_16B(HMAC-SHA256(BYTES, KEY_MASTER))
- Parameters:
secret- A master shared key.index- A byte array index of the key.- Returns:
- A new derived key from a master key with given index.
- Throws:
GenericCryptoException- In case key derivation fails.CryptoProviderException- In case cryptography provider is incorrectly initialized.
-
deriveSecretKeyFromPassword
public SecretKey deriveSecretKeyFromPassword(String password, byte[] salt) throws CryptoProviderException Derive a long AES suitable key from a password and salt. Uses PBKDF with 10 000 iterations.- Parameters:
password- A password used for key derivation.salt- A salt used for key derivation.- Returns:
- A new secret key derived from the password.
- Throws:
CryptoProviderException- In case cryptography provider is incorrectly initialized.
-
deriveSecretKeyFromPassword
public SecretKey deriveSecretKeyFromPassword(String password, byte[] salt, int iterations) throws CryptoProviderException Derive a long AES suitable key from a password and salt. Uses PBKDF with specified number of iterations.- Parameters:
password- A password used for key derivation.salt- A salt used for key derivation.iterations- Number of iterations used in PBKDF.- Returns:
- A new secret key derived from the password.
- Throws:
CryptoProviderException- In case cryptography provider is incorrectly initialized.
-