use of java.security.spec.AlgorithmParameterSpec in project jdk8u_jdk by JetBrains.
the class CryptoPolicyParser method getInstance.
private static final AlgorithmParameterSpec getInstance(String type, Integer[] params) throws ParsingException {
AlgorithmParameterSpec ret = null;
try {
Class<?> apsClass = Class.forName(type);
Class<?>[] paramClasses = new Class<?>[params.length];
for (int i = 0; i < params.length; i++) {
paramClasses[i] = int.class;
}
Constructor<?> c = apsClass.getConstructor(paramClasses);
ret = (AlgorithmParameterSpec) c.newInstance((Object[]) params);
} catch (Exception e) {
throw new ParsingException("Cannot call the constructor of " + type + e);
}
return ret;
}
use of java.security.spec.AlgorithmParameterSpec in project android_frameworks_base by crdroidandroid.
the class AndroidKeyStoreKeyPairGeneratorSpi method initAlgorithmSpecificParameters.
private void initAlgorithmSpecificParameters() throws InvalidAlgorithmParameterException {
AlgorithmParameterSpec algSpecificSpec = mSpec.getAlgorithmParameterSpec();
switch(mKeymasterAlgorithm) {
case KeymasterDefs.KM_ALGORITHM_RSA:
{
BigInteger publicExponent = null;
if (algSpecificSpec instanceof RSAKeyGenParameterSpec) {
RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec) algSpecificSpec;
if (mKeySizeBits == -1) {
mKeySizeBits = rsaSpec.getKeysize();
} else if (mKeySizeBits != rsaSpec.getKeysize()) {
throw new InvalidAlgorithmParameterException("RSA key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + rsaSpec.getKeysize());
}
publicExponent = rsaSpec.getPublicExponent();
} else if (algSpecificSpec != null) {
throw new InvalidAlgorithmParameterException("RSA may only use RSAKeyGenParameterSpec");
}
if (publicExponent == null) {
publicExponent = RSAKeyGenParameterSpec.F4;
}
if (publicExponent.compareTo(BigInteger.ZERO) < 1) {
throw new InvalidAlgorithmParameterException("RSA public exponent must be positive: " + publicExponent);
}
if (publicExponent.compareTo(KeymasterArguments.UINT64_MAX_VALUE) > 0) {
throw new InvalidAlgorithmParameterException("Unsupported RSA public exponent: " + publicExponent + ". Maximum supported value: " + KeymasterArguments.UINT64_MAX_VALUE);
}
mRSAPublicExponent = publicExponent;
break;
}
case KeymasterDefs.KM_ALGORITHM_EC:
if (algSpecificSpec instanceof ECGenParameterSpec) {
ECGenParameterSpec ecSpec = (ECGenParameterSpec) algSpecificSpec;
String curveName = ecSpec.getName();
Integer ecSpecKeySizeBits = SUPPORTED_EC_NIST_CURVE_NAME_TO_SIZE.get(curveName.toLowerCase(Locale.US));
if (ecSpecKeySizeBits == null) {
throw new InvalidAlgorithmParameterException("Unsupported EC curve name: " + curveName + ". Supported: " + SUPPORTED_EC_NIST_CURVE_NAMES);
}
if (mKeySizeBits == -1) {
mKeySizeBits = ecSpecKeySizeBits;
} else if (mKeySizeBits != ecSpecKeySizeBits) {
throw new InvalidAlgorithmParameterException("EC key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + ecSpecKeySizeBits);
}
} else if (algSpecificSpec != null) {
throw new InvalidAlgorithmParameterException("EC may only use ECGenParameterSpec");
}
break;
default:
throw new ProviderException("Unsupported algorithm: " + mKeymasterAlgorithm);
}
}
use of java.security.spec.AlgorithmParameterSpec in project jdk8u_jdk by JetBrains.
the class KAParticipant method runTest.
public static boolean runTest(String algo, int numParties, String secretAlgo) {
KAParticipant[] parties = new KAParticipant[numParties];
Key[] keyArchives = new Key[numParties];
try {
// generate AlogirhtmParameterSpec
AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH", "SunJCE");
AlgorithmParameterSpec aps = new DHGenParameterSpec(512, 64);
apg.init(aps);
DHParameterSpec spec = apg.generateParameters().getParameterSpec(DHParameterSpec.class);
//initilize all KeyAgreement participants
for (int i = 0; i < numParties; i++) {
parties[i] = new KAParticipant(PA_NAMES[i], algo);
parties[i].initialize(spec);
keyArchives[i] = parties[i].getPublicKey();
}
// Do all phases in the KeyAgreement for all participants
Key[] keyBuffer = new Key[numParties];
boolean lastPhase = false;
for (int j = 0; j < numParties - 1; j++) {
if (j == numParties - 2) {
lastPhase = true;
}
for (int k = 0; k < numParties; k++) {
if (k == numParties - 1) {
keyBuffer[k] = parties[k].doPhase(keyArchives[0], lastPhase);
} else {
keyBuffer[k] = parties[k].doPhase(keyArchives[k + 1], lastPhase);
}
}
System.arraycopy(keyBuffer, 0, keyArchives, 0, numParties);
}
//Comparison: The secret keys generated by all involved parties should be the same
SecretKey[] sKeys = new SecretKey[numParties];
for (int n = 0; n < numParties; n++) {
sKeys[n] = parties[n].generateSecret(secretAlgo);
}
for (int q = 0; q < numParties - 1; q++) {
if (!Arrays.equals(sKeys[q].getEncoded(), sKeys[q + 1].getEncoded())) {
return false;
}
}
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
use of java.security.spec.AlgorithmParameterSpec in project android_frameworks_base by DirtyUnicorns.
the class AndroidKeyStoreKeyPairGeneratorSpi method initAlgorithmSpecificParameters.
private void initAlgorithmSpecificParameters() throws InvalidAlgorithmParameterException {
AlgorithmParameterSpec algSpecificSpec = mSpec.getAlgorithmParameterSpec();
switch(mKeymasterAlgorithm) {
case KeymasterDefs.KM_ALGORITHM_RSA:
{
BigInteger publicExponent = null;
if (algSpecificSpec instanceof RSAKeyGenParameterSpec) {
RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec) algSpecificSpec;
if (mKeySizeBits == -1) {
mKeySizeBits = rsaSpec.getKeysize();
} else if (mKeySizeBits != rsaSpec.getKeysize()) {
throw new InvalidAlgorithmParameterException("RSA key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + rsaSpec.getKeysize());
}
publicExponent = rsaSpec.getPublicExponent();
} else if (algSpecificSpec != null) {
throw new InvalidAlgorithmParameterException("RSA may only use RSAKeyGenParameterSpec");
}
if (publicExponent == null) {
publicExponent = RSAKeyGenParameterSpec.F4;
}
if (publicExponent.compareTo(BigInteger.ZERO) < 1) {
throw new InvalidAlgorithmParameterException("RSA public exponent must be positive: " + publicExponent);
}
if (publicExponent.compareTo(KeymasterArguments.UINT64_MAX_VALUE) > 0) {
throw new InvalidAlgorithmParameterException("Unsupported RSA public exponent: " + publicExponent + ". Maximum supported value: " + KeymasterArguments.UINT64_MAX_VALUE);
}
mRSAPublicExponent = publicExponent;
break;
}
case KeymasterDefs.KM_ALGORITHM_EC:
if (algSpecificSpec instanceof ECGenParameterSpec) {
ECGenParameterSpec ecSpec = (ECGenParameterSpec) algSpecificSpec;
String curveName = ecSpec.getName();
Integer ecSpecKeySizeBits = SUPPORTED_EC_NIST_CURVE_NAME_TO_SIZE.get(curveName.toLowerCase(Locale.US));
if (ecSpecKeySizeBits == null) {
throw new InvalidAlgorithmParameterException("Unsupported EC curve name: " + curveName + ". Supported: " + SUPPORTED_EC_NIST_CURVE_NAMES);
}
if (mKeySizeBits == -1) {
mKeySizeBits = ecSpecKeySizeBits;
} else if (mKeySizeBits != ecSpecKeySizeBits) {
throw new InvalidAlgorithmParameterException("EC key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + ecSpecKeySizeBits);
}
} else if (algSpecificSpec != null) {
throw new InvalidAlgorithmParameterException("EC may only use ECGenParameterSpec");
}
break;
default:
throw new ProviderException("Unsupported algorithm: " + mKeymasterAlgorithm);
}
}
use of java.security.spec.AlgorithmParameterSpec in project Lucee by lucee.
the class Cryptor method _crypt.
private static byte[] _crypt(byte[] input, String key, String algorithm, byte[] ivOrSalt, int iterations, boolean doDecrypt) throws PageException {
byte[] result = null;
Key secretKey = null;
AlgorithmParameterSpec params = null;
String algo = algorithm;
boolean isFBM = false, isPBE = StringUtil.startsWithIgnoreCase(algo, "PBE");
int ivsLen = 0, algoDelimPos = algorithm.indexOf('/');
if (algoDelimPos > -1) {
algo = algorithm.substring(0, algoDelimPos);
isFBM = !StringUtil.startsWithIgnoreCase(algorithm.substring(algoDelimPos + 1), "ECB");
}
try {
Cipher cipher = Cipher.getInstance(algorithm);
if (ivOrSalt == null) {
if (isPBE || isFBM) {
ivsLen = cipher.getBlockSize();
ivOrSalt = new byte[ivsLen];
if (doDecrypt)
System.arraycopy(input, 0, ivOrSalt, 0, ivsLen);
else
secureRandom.nextBytes(ivOrSalt);
}
}
if (isPBE) {
secretKey = SecretKeyFactory.getInstance(algorithm).generateSecret(new PBEKeySpec(key.toCharArray()));
// set Salt and Iterations for PasswordBasedEncryption
params = new PBEParameterSpec(ivOrSalt, iterations > 0 ? iterations : DEFAULT_ITERATIONS);
} else {
secretKey = new SecretKeySpec(Coder.decode(Coder.ENCODING_BASE64, key), algo);
if (isFBM)
// set Initialization Vector for non-ECB Feedback Mode
params = new IvParameterSpec(ivOrSalt);
}
if (doDecrypt) {
cipher.init(Cipher.DECRYPT_MODE, secretKey, params);
result = cipher.doFinal(input, ivsLen, input.length - ivsLen);
} else {
cipher.init(Cipher.ENCRYPT_MODE, secretKey, params);
result = new byte[ivsLen + cipher.getOutputSize(input.length)];
if (ivsLen > 0)
System.arraycopy(ivOrSalt, 0, result, 0, ivsLen);
cipher.doFinal(input, 0, input.length, result, ivsLen);
}
return result;
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw Caster.toPageException(t);
}
}
Aggregations