Search in sources :

Example 31 with ECPublicKeyParameters

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);
}
Also used : BigInteger(java.math.BigInteger) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Example 32 with ECPublicKeyParameters

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");
    }
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECGOST3410Parameters(com.github.zhenwei.core.crypto.params.ECGOST3410Parameters) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) Ed448PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) RSAPublicKey(com.github.zhenwei.core.asn1.pkcs.RSAPublicKey) X25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) DSAParameter(com.github.zhenwei.core.asn1.x509.DSAParameter) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) X448PublicKeyParameters(com.github.zhenwei.core.crypto.params.X448PublicKeyParameters) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) IOException(java.io.IOException) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 33 with ECPublicKeyParameters

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");
}
Also used : DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters)

Example 34 with ECPublicKeyParameters

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);
}
Also used : BigInteger(java.math.BigInteger) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Aggregations

ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)34 BigInteger (java.math.BigInteger)16 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)14 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)12 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)10 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)7 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)6 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)6 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)5 X9ECPoint (com.github.zhenwei.core.asn1.x9.X9ECPoint)5 IOException (java.io.IOException)5 ASN1BitString (com.github.zhenwei.core.asn1.ASN1BitString)4 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)4 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)4 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)4 InvalidKeyException (java.security.InvalidKeyException)4 ECPoint (java.security.spec.ECPoint)4 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)3 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)3