use of javax.crypto.SecretKey in project android_frameworks_base by DirtyUnicorns.
the class LockSettingsService method getDecryptedPasswordForTiedProfile.
private String getDecryptedPasswordForTiedProfile(int userId) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, CertificateException, IOException {
if (DEBUG)
Slog.v(TAG, "Get child profile decrytped key");
byte[] storedData = mStorage.readChildProfileLock(userId);
if (storedData == null) {
throw new FileNotFoundException("Child profile lock file not found");
}
byte[] iv = Arrays.copyOfRange(storedData, 0, PROFILE_KEY_IV_SIZE);
byte[] encryptedPassword = Arrays.copyOfRange(storedData, PROFILE_KEY_IV_SIZE, storedData.length);
byte[] decryptionResult;
java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
SecretKey decryptionKey = (SecretKey) keyStore.getKey(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId, null);
Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE);
cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new GCMParameterSpec(128, iv));
decryptionResult = cipher.doFinal(encryptedPassword);
return new String(decryptionResult, StandardCharsets.UTF_8);
}
use of javax.crypto.SecretKey in project jdk8u_jdk by JetBrains.
the class PBKDF2TranslateTest method testMyOwnSecretKey.
/**
* The test case scenario implemented in the method: - derive Key1 for the
* given PBKDF2 algorithm - create my own secret Key2 as an instance of a
* class implements PBEKey - translate Key2 - check if the key value of the
* translated key and Key1 are the same.
*/
private void testMyOwnSecretKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt);
SecretKey key2 = getMyOwnSecretKey(salt);
// Is it actually the same?
if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
throw new RuntimeException("We shouldn't be here. The key1 and key2 values in its" + " primary encoding format have to be the same!");
}
// translate key
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest);
SecretKey key3 = skf.translateKey(key2);
// Check if it still the same after translation
if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) {
System.out.println("Key1=" + new String(key1.getEncoded()) + " key3=" + new String(key3.getEncoded()) + " salt=" + new String(salt));
throw new RuntimeException("testMyOwnSecretKey test case failed: the key1 and key3" + " values in its primary encoding format are not" + " the same for " + algoForTest + " algorithm.");
}
}
use of javax.crypto.SecretKey in project jdk8u_jdk by JetBrains.
the class PBKDF2TranslateTest method generateAndTranslateKey.
/**
* The test case scenario implemented in the method: - derive PBKDF2 key
* using the given algorithm; - translate the key - check if the translated
* and original keys have the same key value.
*
*/
public void generateAndTranslateKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
// derive PBKDF2 key
SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt);
// translate key
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest);
SecretKey key2 = skf.translateKey(key1);
// Check if it still the same after translation
if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
System.out.println("Key1=" + new String(key1.getEncoded()) + " key2=" + new String(key2.getEncoded()) + " salt=" + new String(salt));
throw new RuntimeException("generateAndTranslateKey test case failed: the key1 and" + " key2 values in its primary encoding format are" + " not the same for " + algoForTest + " algorithm.");
}
}
use of javax.crypto.SecretKey in project jdk8u_jdk by JetBrains.
the class TestPRF method main.
public void main(Provider provider) throws Exception {
if (provider.getService("KeyGenerator", "SunTlsPrf") == null) {
System.out.println("Provider does not support algorithm, skipping");
return;
}
InputStream in = new FileInputStream(new File(BASE, "prfdata.txt"));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
int n = 0;
int lineNumber = 0;
byte[] secret = null;
String label = null;
byte[] seed = null;
int length = 0;
byte[] output = null;
while (true) {
String line = reader.readLine();
lineNumber++;
if (line == null) {
break;
}
if (line.startsWith("prf-") == false) {
continue;
}
String data = line.substring(PREFIX_LENGTH);
if (line.startsWith("prf-secret:")) {
secret = parse(data);
} else if (line.startsWith("prf-label:")) {
label = data;
} else if (line.startsWith("prf-seed:")) {
seed = parse(data);
} else if (line.startsWith("prf-length:")) {
length = Integer.parseInt(data);
} else if (line.startsWith("prf-output:")) {
output = parse(data);
System.out.print(".");
n++;
KeyGenerator kg = KeyGenerator.getInstance("SunTlsPrf", provider);
SecretKey inKey;
if (secret == null) {
inKey = null;
} else {
inKey = new SecretKeySpec(secret, "Generic");
}
TlsPrfParameterSpec spec = new TlsPrfParameterSpec(inKey, label, seed, length, null, -1, -1);
SecretKey key;
try {
kg.init(spec);
key = kg.generateKey();
} catch (Exception e) {
if (secret == null) {
// This fails on Solaris, but since we never call this
// API for this case in JSSE, ignore the failure.
// (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE
// mechanism)
System.out.print("X");
continue;
}
System.out.println();
throw new Exception("Error on line: " + lineNumber, e);
}
byte[] enc = key.getEncoded();
if (Arrays.equals(output, enc) == false) {
System.out.println();
System.out.println("expected: " + toString(output));
System.out.println("actual: " + toString(enc));
throw new Exception("mismatch line: " + lineNumber);
}
} else {
throw new Exception("Unknown line: " + line);
}
}
if (n == 0) {
throw new Exception("no tests");
}
in.close();
System.out.println();
System.out.println("OK: " + n + " tests");
}
use of javax.crypto.SecretKey in project jdk8u_jdk by JetBrains.
the class TestCipherPBE method runTest.
private void runTest(String algorithm) throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, ShortBufferException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
out.println("=> Testing: " + algorithm);
try {
// Initialization
AlgorithmParameterSpec algoParamSpec = new PBEParameterSpec(SALT, 6);
SecretKey secretKey = SecretKeyFactory.getInstance(KEY_ALGO).generateSecret(new PBEKeySpec(("Secret Key Value").toCharArray()));
Cipher ci = Cipher.getInstance(algorithm);
ci.init(Cipher.ENCRYPT_MODE, secretKey, algoParamSpec);
// Encryption
byte[] cipherText = ci.doFinal(PLAIN_TEXT);
// Decryption
ci.init(Cipher.DECRYPT_MODE, secretKey, algoParamSpec);
byte[] recoveredText = ci.doFinal(cipherText);
if (algorithm.contains("TripleDES")) {
throw new RuntimeException("Expected InvalidKeyException exception uncaugh");
}
// Comparison
if (!Arrays.equals(PLAIN_TEXT, recoveredText)) {
throw new RuntimeException("Test failed: plainText is not equal to recoveredText");
}
out.println("Test Passed.");
} catch (InvalidKeyException ex) {
if (algorithm.contains("TripleDES")) {
out.println("Expected InvalidKeyException raised");
} else {
throw new RuntimeException(ex);
}
}
}
Aggregations