use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class KeyPairGeneratorSpi method generateKeyPair.
public KeyPair generateKeyPair() {
if (!initialised) {
throw new IllegalStateException("EC Key Pair Generator not initialised");
}
AsymmetricCipherKeyPair pair = engine.generateKeyPair();
ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
if (ecParams instanceof ECParameterSpec) {
ECParameterSpec p = (ECParameterSpec) ecParams;
BCECGOST3410_2012PublicKey pubKey = new BCECGOST3410_2012PublicKey(algorithm, pub, p);
return new KeyPair(pubKey, new BCECGOST3410_2012PrivateKey(algorithm, priv, pubKey, p));
} else if (ecParams == null) {
return new KeyPair(new BCECGOST3410_2012PublicKey(algorithm, pub), new BCECGOST3410_2012PrivateKey(algorithm, priv));
} else {
java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
BCECGOST3410_2012PublicKey pubKey = new BCECGOST3410_2012PublicKey(algorithm, pub, p);
return new KeyPair(pubKey, new BCECGOST3410_2012PrivateKey(algorithm, priv, pubKey, p));
}
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class IESCipher method engineDoFinal.
// Finalisation methods
public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
if (inputLen != 0) {
buffer.write(input, inputOffset, inputLen);
}
final byte[] in = buffer.toByteArray();
buffer.reset();
// Convert parameters for use in IESEngine
CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(), engineSpec.getEncodingV(), engineSpec.getMacKeySize(), engineSpec.getCipherKeySize());
if (engineSpec.getNonce() != null) {
params = new ParametersWithIV(params, engineSpec.getNonce());
}
final ECDomainParameters ecParams = ((ECKeyParameters) key).getParameters();
final byte[] V;
if (otherKeyParameter != null) {
try {
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
engine.init(true, otherKeyParameter, key, params);
} else {
engine.init(false, key, otherKeyParameter, params);
}
return engine.processBlock(in, 0, in.length);
} catch (Exception e) {
throw new BadBlockException("unable to process block", e);
}
}
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
// Generate the ephemeral key pair
ECKeyPairGenerator gen = new ECKeyPairGenerator();
gen.init(new ECKeyGenerationParameters(ecParams, random));
final boolean usePointCompression = engineSpec.getPointCompression();
EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {
public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
return ((ECPublicKeyParameters) keyParameter).getQ().getEncoded(usePointCompression);
}
});
// Encrypt the buffer
try {
engine.init(key, params, kGen);
return engine.processBlock(in, 0, in.length);
} catch (final Exception e) {
throw new BadBlockException("unable to process block", e);
}
} else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) {
// Decrypt the buffer
try {
engine.init(key, params, new ECIESPublicKeyParser(ecParams));
return engine.processBlock(in, 0, in.length);
} catch (InvalidCipherTextException e) {
throw new BadBlockException("unable to process block", e);
}
} else {
throw new IllegalStateException("cipher not initialised");
}
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class KeyAgreementSpi method engineDoPhase.
protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException, IllegalStateException {
if (parameters == null) {
throw new IllegalStateException(kaAlgorithm + " not initialised.");
}
if (!lastPhase) {
throw new IllegalStateException(kaAlgorithm + " can only be between two parties.");
}
CipherParameters pubKey;
if (agreement instanceof ECMQVBasicAgreement) {
if (!(key instanceof MQVPublicKey)) {
ECPublicKeyParameters staticKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter((PublicKey) key);
ECPublicKeyParameters ephemKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvParameters.getOtherPartyEphemeralKey());
pubKey = new MQVPublicParameters(staticKey, ephemKey);
} else {
MQVPublicKey mqvPubKey = (MQVPublicKey) key;
ECPublicKeyParameters staticKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvPubKey.getStaticKey());
ECPublicKeyParameters ephemKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvPubKey.getEphemeralKey());
pubKey = new MQVPublicParameters(staticKey, ephemKey);
}
} else if (agreement instanceof ECDHCUnifiedAgreement) {
ECPublicKeyParameters staticKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter((PublicKey) key);
ECPublicKeyParameters ephemKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(dheParameters.getOtherPartyEphemeralKey());
pubKey = new ECDHUPublicParameters(staticKey, ephemKey);
} else {
if (!(key instanceof PublicKey)) {
throw new InvalidKeyException(kaAlgorithm + " key agreement requires " + getSimpleName(ECPublicKey.class) + " for doPhase");
}
pubKey = ECUtils.generatePublicKeyParameter((PublicKey) key);
}
try {
if (agreement instanceof BasicAgreement) {
result = bigIntToBytes(((BasicAgreement) agreement).calculateAgreement(pubKey));
} else {
result = ((ECDHCUnifiedAgreement) agreement).calculateAgreement(pubKey);
}
} catch (final Exception e) {
throw new InvalidKeyException("calculation failed: " + e.getMessage()) {
public Throwable getCause() {
return e;
}
};
}
return null;
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class KeyFactorySpi method engineGetKeySpec.
protected KeySpec engineGetKeySpec(Key key, Class spec) throws InvalidKeySpecException {
if ((spec.isAssignableFrom(KeySpec.class) || spec.isAssignableFrom(java.security.spec.ECPublicKeySpec.class)) && key instanceof ECPublicKey) {
ECPublicKey k = (ECPublicKey) key;
if (k.getParams() != null) {
return new java.security.spec.ECPublicKeySpec(k.getW(), k.getParams());
} else {
ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
return new java.security.spec.ECPublicKeySpec(k.getW(), EC5Util.convertSpec(EC5Util.convertCurve(implicitSpec.getCurve(), implicitSpec.getSeed()), implicitSpec));
}
} else if ((spec.isAssignableFrom(KeySpec.class) || spec.isAssignableFrom(java.security.spec.ECPrivateKeySpec.class)) && key instanceof ECPrivateKey) {
ECPrivateKey k = (ECPrivateKey) key;
if (k.getParams() != null) {
return new java.security.spec.ECPrivateKeySpec(k.getS(), k.getParams());
} else {
ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
return new java.security.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(EC5Util.convertCurve(implicitSpec.getCurve(), implicitSpec.getSeed()), implicitSpec));
}
} else if (spec.isAssignableFrom(com.github.zhenwei.provider.jce.spec.ECPublicKeySpec.class) && key instanceof ECPublicKey) {
ECPublicKey k = (ECPublicKey) key;
if (k.getParams() != null) {
return new com.github.zhenwei.provider.jce.spec.ECPublicKeySpec(EC5Util.convertPoint(k.getParams(), k.getW()), EC5Util.convertSpec(k.getParams()));
} else {
ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
return new com.github.zhenwei.provider.jce.spec.ECPublicKeySpec(EC5Util.convertPoint(k.getParams(), k.getW()), implicitSpec);
}
} else if (spec.isAssignableFrom(com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec.class) && key instanceof ECPrivateKey) {
ECPrivateKey k = (ECPrivateKey) key;
if (k.getParams() != null) {
return new com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(k.getParams()));
} else {
ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
return new com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec(k.getS(), implicitSpec);
}
} else if (spec.isAssignableFrom(OpenSSHPublicKeySpec.class) && key instanceof ECPublicKey) {
if (key instanceof BCECPublicKey) {
BCECPublicKey bcPk = (BCECPublicKey) key;
ECParameterSpec sc = bcPk.getParameters();
try {
return new OpenSSHPublicKeySpec(OpenSSHPublicKeyUtil.encodePublicKey(new ECPublicKeyParameters(bcPk.getQ(), new ECDomainParameters(sc.getCurve(), sc.getG(), sc.getN(), sc.getH(), sc.getSeed()))));
} catch (IOException e) {
throw new IllegalArgumentException("unable to produce encoding: " + e.getMessage());
}
} else {
throw new IllegalArgumentException("invalid key type: " + key.getClass().getName());
}
} else if (spec.isAssignableFrom(OpenSSHPrivateKeySpec.class) && key instanceof ECPrivateKey) {
if (key instanceof BCECPrivateKey) {
try {
return new OpenSSHPrivateKeySpec(PrivateKeyInfo.getInstance(key.getEncoded()).parsePrivateKey().toASN1Primitive().getEncoded());
} catch (IOException e) {
throw new IllegalArgumentException("cannot encoded key: " + e.getMessage());
}
} else {
throw new IllegalArgumentException("invalid key type: " + key.getClass().getName());
}
}
return super.engineGetKeySpec(key, spec);
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class KeyAgreementSpi method initFromKey.
private void initFromKey(Key key, AlgorithmParameterSpec parameterSpec) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (agreement instanceof ECMQVBasicAgreement) {
mqvParameters = null;
if (!(key instanceof MQVPrivateKey) && !(parameterSpec instanceof MQVParameterSpec)) {
throw new InvalidAlgorithmParameterException(kaAlgorithm + " key agreement requires " + getSimpleName(MQVParameterSpec.class) + " for initialisation");
}
ECPrivateKeyParameters staticPrivKey;
ECPrivateKeyParameters ephemPrivKey;
ECPublicKeyParameters ephemPubKey;
if (key instanceof MQVPrivateKey) {
MQVPrivateKey mqvPrivKey = (MQVPrivateKey) key;
staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvPrivKey.getStaticPrivateKey());
ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvPrivKey.getEphemeralPrivateKey());
ephemPubKey = null;
if (mqvPrivKey.getEphemeralPublicKey() != null) {
ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey());
}
} else {
MQVParameterSpec mqvParameterSpec = (MQVParameterSpec) parameterSpec;
staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvParameterSpec.getEphemeralPrivateKey());
ephemPubKey = null;
if (mqvParameterSpec.getEphemeralPublicKey() != null) {
ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvParameterSpec.getEphemeralPublicKey());
}
mqvParameters = mqvParameterSpec;
ukmParameters = mqvParameterSpec.getUserKeyingMaterial();
}
MQVPrivateParameters localParams = new MQVPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
this.parameters = staticPrivKey.getParameters();
// TODO Validate that all the keys are using the same parameters?
((ECMQVBasicAgreement) agreement).init(localParams);
} else if (parameterSpec instanceof DHUParameterSpec) {
if (!(agreement instanceof ECDHCUnifiedAgreement)) {
throw new InvalidAlgorithmParameterException(kaAlgorithm + " key agreement cannot be used with " + getSimpleName(DHUParameterSpec.class));
}
DHUParameterSpec dheParameterSpec = (DHUParameterSpec) parameterSpec;
ECPrivateKeyParameters staticPrivKey;
ECPrivateKeyParameters ephemPrivKey;
ECPublicKeyParameters ephemPubKey;
staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(dheParameterSpec.getEphemeralPrivateKey());
ephemPubKey = null;
if (dheParameterSpec.getEphemeralPublicKey() != null) {
ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(dheParameterSpec.getEphemeralPublicKey());
}
dheParameters = dheParameterSpec;
ukmParameters = dheParameterSpec.getUserKeyingMaterial();
ECDHUPrivateParameters localParams = new ECDHUPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
this.parameters = staticPrivKey.getParameters();
((ECDHCUnifiedAgreement) agreement).init(localParams);
} else {
if (!(key instanceof PrivateKey)) {
throw new InvalidKeyException(kaAlgorithm + " key agreement requires " + getSimpleName(ECPrivateKey.class) + " for initialisation");
}
if (kdf == null && parameterSpec instanceof UserKeyingMaterialSpec) {
throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
}
ECPrivateKeyParameters privKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
this.parameters = privKey.getParameters();
ukmParameters = (parameterSpec instanceof UserKeyingMaterialSpec) ? ((UserKeyingMaterialSpec) parameterSpec).getUserKeyingMaterial() : null;
((BasicAgreement) agreement).init(privKey);
}
}
Aggregations