Search in sources :

Example 1 with DSAPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class DSASigner method verifySignature.

/**
 * return true if the value r and s represent a DSA signature for the passed in message for
 * standard DSA the message should be a SHA-1 hash of the real message to be verified.
 */
public boolean verifySignature(byte[] message, BigInteger r, BigInteger s) {
    DSAParameters params = key.getParameters();
    BigInteger q = params.getQ();
    BigInteger m = calculateE(q, message);
    BigInteger zero = BigInteger.valueOf(0);
    if (zero.compareTo(r) >= 0 || q.compareTo(r) <= 0) {
        return false;
    }
    if (zero.compareTo(s) >= 0 || q.compareTo(s) <= 0) {
        return false;
    }
    BigInteger w = BigIntegers.modOddInverseVar(q, s);
    BigInteger u1 = m.multiply(w).mod(q);
    BigInteger u2 = r.multiply(w).mod(q);
    BigInteger p = params.getP();
    u1 = params.getG().modPow(u1, p);
    u2 = ((DSAPublicKeyParameters) key).getY().modPow(u2, p);
    BigInteger v = u1.multiply(u2).mod(p).mod(q);
    return v.equals(r);
}
Also used : DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters)

Example 2 with DSAPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class OpenSSHPublicKeyUtil method parsePublicKey.

/**
 * Parse a public key from an SSHBuffer instance.
 *
 * @param buffer containing the SSH public key.
 * @return A CipherParameters instance.
 */
public static AsymmetricKeyParameter parsePublicKey(SSHBuffer buffer) {
    AsymmetricKeyParameter result = null;
    String magic = buffer.readString();
    if (RSA.equals(magic)) {
        BigInteger e = buffer.readBigNumPositive();
        BigInteger n = buffer.readBigNumPositive();
        result = new RSAKeyParameters(false, n, e);
    } else if (DSS.equals(magic)) {
        BigInteger p = buffer.readBigNumPositive();
        BigInteger q = buffer.readBigNumPositive();
        BigInteger g = buffer.readBigNumPositive();
        BigInteger pubKey = buffer.readBigNumPositive();
        result = new DSAPublicKeyParameters(pubKey, new DSAParameters(p, q, g));
    } else if (magic.startsWith(ECDSA)) {
        String curveName = buffer.readString();
        ASN1ObjectIdentifier oid = SSHNamedCurves.getByName(curveName);
        X9ECParameters x9ECParameters = SSHNamedCurves.getParameters(oid);
        if (x9ECParameters == null) {
            throw new IllegalStateException("unable to find curve for " + magic + " using curve name " + curveName);
        }
        ECCurve curve = x9ECParameters.getCurve();
        byte[] pointRaw = buffer.readBlock();
        result = new ECPublicKeyParameters(curve.decodePoint(pointRaw), new ECNamedDomainParameters(oid, x9ECParameters));
    } else if (ED_25519.equals(magic)) {
        byte[] pubKeyBytes = buffer.readBlock();
        if (pubKeyBytes.length != Ed25519PublicKeyParameters.KEY_SIZE) {
            throw new IllegalStateException("public key value of wrong length");
        }
        result = new Ed25519PublicKeyParameters(pubKeyBytes, 0);
    }
    if (result == null) {
        throw new IllegalArgumentException("unable to parse key");
    }
    if (buffer.hasRemaining()) {
        throw new IllegalArgumentException("decoded key has trailing data");
    }
    return result;
}
Also used : DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 3 with DSAPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class BCDSAPublicKey method readObject.

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    BigInteger p = (BigInteger) in.readObject();
    if (p.equals(ZERO)) {
        this.dsaSpec = null;
    } else {
        this.dsaSpec = new DSAParameterSpec(p, (BigInteger) in.readObject(), (BigInteger) in.readObject());
    }
    this.lwKeyParams = new DSAPublicKeyParameters(y, DSAUtil.toDSAParameters(dsaSpec));
}
Also used : DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) DSAParameterSpec(java.security.spec.DSAParameterSpec) BigInteger(java.math.BigInteger)

Example 4 with DSAPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        Integer paramStrength = Integers.valueOf(strength);
        if (params.containsKey(paramStrength)) {
            param = (DSAKeyGenerationParameters) params.get(paramStrength);
        } else {
            synchronized (lock) {
                // our key size.
                if (params.containsKey(paramStrength)) {
                    param = (DSAKeyGenerationParameters) params.get(paramStrength);
                } else {
                    DSAParametersGenerator pGen;
                    DSAParameterGenerationParameters dsaParams;
                    int certainty = PrimeCertaintyCalculator.getDefaultCertainty(strength);
                    // For legacy keysize that is less than 1024-bit, we just use the 186-2 style parameters
                    if (strength == 1024) {
                        pGen = new DSAParametersGenerator();
                        if (Properties.isOverrideSet("org.bouncycastle.dsa.FIPS186-2for1024bits")) {
                            pGen.init(strength, certainty, random);
                        } else {
                            dsaParams = new DSAParameterGenerationParameters(1024, 160, certainty, random);
                            pGen.init(dsaParams);
                        }
                    } else if (strength > 1024) {
                        dsaParams = new DSAParameterGenerationParameters(strength, 256, certainty, random);
                        pGen = new DSAParametersGenerator(new SHA256Digest());
                        pGen.init(dsaParams);
                    } else {
                        pGen = new DSAParametersGenerator();
                        pGen.init(strength, certainty, random);
                    }
                    param = new DSAKeyGenerationParameters(random, pGen.generateParameters());
                    params.put(paramStrength, param);
                }
            }
        }
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    DSAPublicKeyParameters pub = (DSAPublicKeyParameters) pair.getPublic();
    DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCDSAPublicKey(pub), new BCDSAPrivateKey(priv));
}
Also used : DSAParametersGenerator(com.github.zhenwei.core.crypto.generators.DSAParametersGenerator) DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) DSAParameterGenerationParameters(com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters) DSAKeyGenerationParameters(com.github.zhenwei.core.crypto.params.DSAKeyGenerationParameters) DSAPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 5 with DSAPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class DSAKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DSAParameters dsaParams = param.getParameters();
    BigInteger x = generatePrivateKey(dsaParams.getQ(), param.getRandom());
    BigInteger y = calculatePublicKey(dsaParams.getP(), dsaParams.getG(), x);
    return new AsymmetricCipherKeyPair(new DSAPublicKeyParameters(y, dsaParams), new DSAPrivateKeyParameters(x, dsaParams));
}
Also used : DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) DSAPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Aggregations

DSAPublicKeyParameters (com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters)7 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)5 BigInteger (java.math.BigInteger)5 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)3 Ed25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters)3 RSAKeyParameters (com.github.zhenwei.core.crypto.params.RSAKeyParameters)3 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)2 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)2 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)2 DSAPrivateKeyParameters (com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters)2 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)2 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)1 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)1 RSAPublicKey (com.github.zhenwei.core.asn1.pkcs.RSAPublicKey)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 DSAParameter (com.github.zhenwei.core.asn1.x509.DSAParameter)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)1