use of javax.crypto.spec.SecretKeySpec in project Signal-Android by WhisperSystems.
the class AsymmetricMasterCipher method getDigestedBytes.
private byte[] getDigestedBytes(byte[] secretBytes, int iteration) {
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secretBytes, "HmacSHA256"));
return mac.doFinal(Conversions.intToByteArray(iteration));
} catch (NoSuchAlgorithmException | java.security.InvalidKeyException e) {
throw new AssertionError(e);
}
}
use of javax.crypto.spec.SecretKeySpec in project Signal-Android by WhisperSystems.
the class MasterSecretUtil method generateMasterSecret.
public static MasterSecret generateMasterSecret(Context context, String passphrase) {
try {
byte[] encryptionSecret = generateEncryptionSecret();
byte[] macSecret = generateMacSecret();
byte[] masterSecret = Util.combine(encryptionSecret, macSecret);
byte[] encryptionSalt = generateSalt();
int iterations = generateIterationCount(passphrase, encryptionSalt);
byte[] encryptedMasterSecret = encryptWithPassphrase(encryptionSalt, iterations, masterSecret, passphrase);
byte[] macSalt = generateSalt();
byte[] encryptedAndMacdMasterSecret = macWithPassphrase(macSalt, iterations, encryptedMasterSecret, passphrase);
save(context, "encryption_salt", encryptionSalt);
save(context, "mac_salt", macSalt);
save(context, "passphrase_iterations", iterations);
save(context, "master_secret", encryptedAndMacdMasterSecret);
save(context, "passphrase_initialized", true);
return new MasterSecret(new SecretKeySpec(encryptionSecret, "AES"), new SecretKeySpec(macSecret, "HmacSHA1"));
} catch (GeneralSecurityException e) {
Log.w("keyutil", e);
return null;
}
}
use of javax.crypto.spec.SecretKeySpec in project Openfire by igniterealtime.
the class AesEncryptor method cipher.
/**
* Symmetric encrypt/decrypt routine.
*
* @param attribute The value to be converted
* @param key The encryption key
* @param mode The cipher mode (encrypt or decrypt)
* @return The converted attribute, or null if conversion fails
*/
private byte[] cipher(byte[] attribute, byte[] key, int mode) {
byte[] result = null;
try {
// Create AES encryption key
Key aesKey = new SecretKeySpec(key, "AES");
// Create AES Cipher
Cipher aesCipher = Cipher.getInstance(ALGORITHM);
// Initialize AES Cipher and convert
aesCipher.init(mode, aesKey, new IvParameterSpec(INIT_PARM));
result = aesCipher.doFinal(attribute);
} catch (Exception e) {
log.error("AES cipher failed", e);
}
return result;
}
use of javax.crypto.spec.SecretKeySpec in project Openfire by igniterealtime.
the class Crypto method generateAESKeystream.
public static Byte[] generateAESKeystream(Byte[] key, Integer length, Byte[] counter) {
byte[] output = new byte[length.intValue()];
for (int i = 0; i < output.length; i++) output[i] = 0;
byte[] input = BitAssistant.bytesFromArray(counter);
try {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(1, new SecretKeySpec(BitAssistant.bytesFromArray(key), "AES"));
for (int i = 0; i < length.intValue(); i += 16) {
cipher.update(input, 0, 16, output, i);
IncrementCounter(input);
}
for (int i = 0; i < counter.length; i++) counter[i] = Byte.valueOf(input[i]);
return BitAssistant.bytesToArray(output);
} catch (Exception e) {
return new Byte[0];
}
}
use of javax.crypto.spec.SecretKeySpec in project Openfire by igniterealtime.
the class Crypto method getHmacSha1.
public static Byte[] getHmacSha1(Byte[] key, Byte[] buffer) {
try {
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(new SecretKeySpec(BitAssistant.bytesFromArray(key), "HmacSHA1"));
return BitAssistant.bytesToArray(mac.doFinal(BitAssistant.bytesFromArray(buffer)));
} catch (Exception e) {
return new Byte[0];
}
}
Aggregations