use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class ECGOST3410_2012Signer method verifySignature.
/**
* return true if the value r and s represent a GOST3410 2012 signature for the passed in message
* (for standard GOST3410 2012 the message should be a GOST3411 2012 hash of the real message to
* be verified).
*/
public boolean verifySignature(byte[] message, BigInteger r, BigInteger s) {
// conversion is little-endian
byte[] mRev = Arrays.reverse(message);
BigInteger e = new BigInteger(1, mRev);
BigInteger n = key.getParameters().getN();
// r in the range [1,n-1]
if (r.compareTo(ECConstants.ONE) < 0 || r.compareTo(n) >= 0) {
return false;
}
// s in the range [1,n-1]
if (s.compareTo(ECConstants.ONE) < 0 || s.compareTo(n) >= 0) {
return false;
}
BigInteger v = BigIntegers.modOddInverseVar(n, e);
BigInteger z1 = s.multiply(v).mod(n);
BigInteger z2 = (n.subtract(r)).multiply(v).mod(n);
// P
ECPoint G = key.getParameters().getG();
ECPoint Q = ((ECPublicKeyParameters) key).getQ();
ECPoint point = ECAlgorithms.sumOfTwoMultiplies(G, z1, Q, z2).normalize();
// components must be bogus.
if (point.isInfinity()) {
return false;
}
BigInteger R = point.getAffineXCoord().toBigInteger().mod(n);
return R.equals(r);
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class SubjectPublicKeyInfoFactory method createSubjectPublicKeyInfo.
/**
* Create a SubjectPublicKeyInfo public key.
*
* @param publicKey the key to be encoded into the info object.
* @return a SubjectPublicKeyInfo representing the key.
* @throws IOException on an error encoding the key
*/
public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey) throws IOException {
if (publicKey instanceof RSAKeyParameters) {
RSAKeyParameters pub = (RSAKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(pub.getModulus(), pub.getExponent()));
} else if (publicKey instanceof DSAPublicKeyParameters) {
DSAPublicKeyParameters pub = (DSAPublicKeyParameters) publicKey;
DSAParameter params = null;
DSAParameters dsaParams = pub.getParameters();
if (dsaParams != null) {
params = new DSAParameter(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
}
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, params), new ASN1Integer(pub.getY()));
} else if (publicKey instanceof ECPublicKeyParameters) {
ECPublicKeyParameters pub = (ECPublicKeyParameters) publicKey;
ECDomainParameters domainParams = pub.getParameters();
ASN1Encodable params;
if (domainParams == null) {
// Implicitly CA
params = new X962Parameters(DERNull.INSTANCE);
} else if (domainParams instanceof ECGOST3410Parameters) {
ECGOST3410Parameters gostParams = (ECGOST3410Parameters) domainParams;
BigInteger bX = pub.getQ().getAffineXCoord().toBigInteger();
BigInteger bY = pub.getQ().getAffineYCoord().toBigInteger();
params = new GOST3410PublicKeyAlgParameters(gostParams.getPublicKeyParamSet(), gostParams.getDigestParamSet());
int encKeySize;
int offset;
ASN1ObjectIdentifier algIdentifier;
if (cryptoProOids.contains(gostParams.getPublicKeyParamSet())) {
encKeySize = 64;
offset = 32;
algIdentifier = CryptoProObjectIdentifiers.gostR3410_2001;
} else {
boolean is512 = (bX.bitLength() > 256);
if (is512) {
encKeySize = 128;
offset = 64;
algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512;
} else {
encKeySize = 64;
offset = 32;
algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
}
}
byte[] encKey = new byte[encKeySize];
extractBytes(encKey, encKeySize / 2, 0, bX);
extractBytes(encKey, encKeySize / 2, offset, bY);
try {
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(algIdentifier, params), new DEROctetString(encKey));
} catch (IOException e) {
return null;
}
} else if (domainParams instanceof ECNamedDomainParameters) {
params = new X962Parameters(((ECNamedDomainParameters) domainParams).getName());
} else {
X9ECParameters ecP = new X9ECParameters(domainParams.getCurve(), // TODO Support point compression
new X9ECPoint(domainParams.getG(), false), domainParams.getN(), domainParams.getH(), domainParams.getSeed());
params = new X962Parameters(ecP);
}
// TODO Support point compression
byte[] pubKeyOctets = pub.getQ().getEncoded(false);
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), pubKeyOctets);
} else if (publicKey instanceof X448PublicKeyParameters) {
X448PublicKeyParameters key = (X448PublicKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), key.getEncoded());
} else if (publicKey instanceof X25519PublicKeyParameters) {
X25519PublicKeyParameters key = (X25519PublicKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), key.getEncoded());
} else if (publicKey instanceof Ed448PublicKeyParameters) {
Ed448PublicKeyParameters key = (Ed448PublicKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448), key.getEncoded());
} else if (publicKey instanceof Ed25519PublicKeyParameters) {
Ed25519PublicKeyParameters key = (Ed25519PublicKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), key.getEncoded());
} else {
throw new IOException("key parameters not recognized");
}
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class OpenSSHPublicKeyUtil method encodePublicKey.
/**
* Encode a public key from an AsymmetricKeyParameter instance.
*
* @param cipherParameters The key to encode.
* @return the key OpenSSH encoded.
* @throws IOException
*/
public static byte[] encodePublicKey(AsymmetricKeyParameter cipherParameters) throws IOException {
if (cipherParameters == null) {
throw new IllegalArgumentException("cipherParameters was null.");
}
if (cipherParameters instanceof RSAKeyParameters) {
if (cipherParameters.isPrivate()) {
throw new IllegalArgumentException("RSAKeyParamaters was for encryption");
}
RSAKeyParameters rsaPubKey = (RSAKeyParameters) cipherParameters;
SSHBuilder builder = new SSHBuilder();
builder.writeString(RSA);
builder.writeBigNum(rsaPubKey.getExponent());
builder.writeBigNum(rsaPubKey.getModulus());
return builder.getBytes();
} else if (cipherParameters instanceof ECPublicKeyParameters) {
SSHBuilder builder = new SSHBuilder();
//
// checked for named curve parameters..
//
String name = SSHNamedCurves.getNameForParameters(((ECPublicKeyParameters) cipherParameters).getParameters());
if (name == null) {
throw new IllegalArgumentException("unable to derive ssh curve name for " + ((ECPublicKeyParameters) cipherParameters).getParameters().getCurve().getClass().getName());
}
// Magic
builder.writeString(ECDSA + "-sha2-" + name);
builder.writeString(name);
builder.writeBlock(// Uncompressed
((ECPublicKeyParameters) cipherParameters).getQ().getEncoded(false));
return builder.getBytes();
} else if (cipherParameters instanceof DSAPublicKeyParameters) {
DSAPublicKeyParameters dsaPubKey = (DSAPublicKeyParameters) cipherParameters;
DSAParameters dsaParams = dsaPubKey.getParameters();
SSHBuilder builder = new SSHBuilder();
builder.writeString(DSS);
builder.writeBigNum(dsaParams.getP());
builder.writeBigNum(dsaParams.getQ());
builder.writeBigNum(dsaParams.getG());
builder.writeBigNum(dsaPubKey.getY());
return builder.getBytes();
} else if (cipherParameters instanceof Ed25519PublicKeyParameters) {
SSHBuilder builder = new SSHBuilder();
builder.writeString(ED_25519);
builder.writeBlock(((Ed25519PublicKeyParameters) cipherParameters).getEncoded());
return builder.getBytes();
}
throw new IllegalArgumentException("unable to convert " + cipherParameters.getClass().getName() + " to private key");
}
use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class SM2Signer method verifySignature.
private boolean verifySignature(BigInteger r, BigInteger s) {
BigInteger n = ecParams.getN();
// B1
if (r.compareTo(ONE) < 0 || r.compareTo(n) >= 0) {
return false;
}
// B2
if (s.compareTo(ONE) < 0 || s.compareTo(n) >= 0) {
return false;
}
// B3
byte[] eHash = digestDoFinal();
// B4
BigInteger e = calculateE(n, eHash);
// B5
BigInteger t = r.add(s).mod(n);
if (t.equals(ZERO)) {
return false;
}
// B6
ECPoint q = ((ECPublicKeyParameters) ecKey).getQ();
ECPoint x1y1 = ECAlgorithms.sumOfTwoMultiplies(ecParams.getG(), s, q, t).normalize();
if (x1y1.isInfinity()) {
return false;
}
// B7
BigInteger expectedR = e.add(x1y1.getAffineXCoord().toBigInteger()).mod(n);
return expectedR.equals(r);
}
Aggregations