Example usage for org.bouncycastle.crypto.params DSAParameters DSAParameters

List of usage examples for org.bouncycastle.crypto.params DSAParameters DSAParameters

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.params DSAParameters DSAParameters.

Prototype

public DSAParameters(BigInteger p, BigInteger q, BigInteger g) 

Source Link

Usage

From source file:com.licel.jcardsim.crypto.DSAKeyImpl.java

License:Apache License

/**
 * Get <code>DSAKeyParameters</code>
 * @return parameters for use with BouncyCastle API
 * @see DSAKeyParameters//  w  w  w  .  j a  v  a  2  s  .  co m
 */
public CipherParameters getParameters() {
    if (!isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    return new DSAKeyParameters(isPrivate,
            new DSAParameters(p.getBigInteger(), q.getBigInteger(), g.getBigInteger()));
}

From source file:com.licel.jcardsim.crypto.DSAKeyImpl.java

License:Apache License

/**
 * Get//from w w  w.  j av a2s . c  om
 * <code>DSAKeyGenerationParameters</code>
 *
 * @param rnd Secure Random Generator
 * @return parameters for use with BouncyCastle API
 */
public KeyGenerationParameters getKeyGenerationParameters(SecureRandom rnd) {
    if (isInitialized()) {
        return new DSAKeyGenerationParameters(rnd,
                new DSAParameters(p.getBigInteger(), q.getBigInteger(), g.getBigInteger()));
    }
    return getDefaultKeyGenerationParameters(size, rnd);
}

From source file:edu.vt.middleware.crypt.signature.DSASignature.java

License:Open Source License

/** {@inheritDoc} */
public void initSign() {
    if (signKey == null) {
        throw new IllegalStateException("Sign key must be set prior to initialization.");
    }// w w  w.ja  v  a  2s  .  c  o m

    final DSAPrivateKey privKey = (DSAPrivateKey) signKey;
    final DSAParams params = privKey.getParams();
    final DSAPrivateKeyParameters bcParams = new DSAPrivateKeyParameters(privKey.getX(),
            new DSAParameters(params.getP(), params.getQ(), params.getG()));
    init(true, bcParams);
}

From source file:edu.vt.middleware.crypt.signature.DSASignature.java

License:Open Source License

/** {@inheritDoc} */
public void initVerify() {
    if (verifyKey == null) {
        throw new IllegalStateException("Verify key must be set prior to initialization.");
    }/*from ww w  .ja v a 2 s. co m*/

    final DSAPublicKey pubKey = (DSAPublicKey) verifyKey;
    final DSAParams params = pubKey.getParams();
    final DSAPublicKeyParameters bcParams = new DSAPublicKeyParameters(pubKey.getY(),
            new DSAParameters(params.getP(), params.getQ(), params.getG()));
    init(false, bcParams);
}

From source file:freenet.crypt.Global.java

License:GNU General Public License

public static final DSAParameters getDSAgroupBigAParameters() {
    return new DSAParameters(DSAgroupBigA.getP(), DSAgroupBigA.getQ(), DSAgroupBigA.getG());
}

From source file:net.java.otr4j.crypto.DSAKeyPair.java

License:LGPL

/**
 * Sign data 'b' using DSA private key 'privateKey' and return signature components 'r' and 's'.
 *
 * @param b          The data to be signed.
 * @return Signature components 'r' and 's'.
 *///ww w .  jav  a2 s  . c o m
@Nonnull
public DSASignature signRS(final byte[] b) {
    assert !allZeroBytes(
            b) : "Expected non-zero bytes for b. This may indicate that a critical bug is present, or it may be a false warning.";
    final DSAParams dsaParams = privateKey.getParams();
    final DSAParameters bcDSAParameters = new DSAParameters(dsaParams.getP(), dsaParams.getQ(),
            dsaParams.getG());
    final DSAPrivateKeyParameters bcDSAPrivateKeyParms = new DSAPrivateKeyParameters(privateKey.getX(),
            bcDSAParameters);

    final DSASigner dsaSigner = new DSASigner();
    dsaSigner.init(true, bcDSAPrivateKeyParms);

    final BigInteger q = dsaParams.getQ();

    // Ian: Note that if you can get the standard DSA implementation you're
    // using to not hash its input, you should be able to pass it ((256-bit
    // value) mod q), (rather than truncating the 256-bit value) and all
    // should be well.
    // ref: Interop problems with libotr - DSA signature
    final BigInteger bmpi = new BigInteger(1, b);
    final BigInteger[] signature = dsaSigner.generateSignature(asUnsignedByteArray(bmpi.mod(q)));
    assert signature.length == 2 : "signRS result does not contain the expected 2 components: r and s";
    return new DSASignature(signature[0], signature[1]);
}

From source file:net.java.otr4j.crypto.DSAKeyPair.java

License:LGPL

/**
 * Verify a message using a signature represented as two MPI components: 'r' and 's'.
 *
 * @param b      the message in bytes//w w w  .j a  v  a 2 s .  co m
 * @param pubKey the DSA public key
 * @param r      the signature component 'r'
 * @param s      the signature component 's'
 * @throws OtrCryptoException In case of illegal signature.
 */
public static void verifySignature(final byte[] b, final DSAPublicKey pubKey, final BigInteger r,
        final BigInteger s) throws OtrCryptoException {
    requireNonNull(b);
    assert !allZeroBytes(
            b) : "Expected non-zero bytes for b. This may indicate that a critical bug is present, or it may be a false warning.";
    final DSAParams dsaParams = pubKey.getParams();
    final BigInteger q = dsaParams.getQ();
    final DSAParameters bcDSAParams = new DSAParameters(dsaParams.getP(), q, dsaParams.getG());
    final DSAPublicKeyParameters dsaPubParams;
    try {
        dsaPubParams = new DSAPublicKeyParameters(pubKey.getY(), bcDSAParams);
    } catch (final IllegalArgumentException e) {
        throw new OtrCryptoException("Illegal parameters provided for DSA public key parameters.", e);
    }

    // Ian: Note that if you can get the standard DSA implementation you're
    // using to not hash its input, you should be able to pass it ((256-bit
    // value) mod q), (rather than truncating the 256-bit value) and all
    // should be well.
    // ref: Interop problems with libotr - DSA signature
    final DSASigner dsaSigner = new DSASigner();
    dsaSigner.init(false, dsaPubParams);

    final BigInteger bmpi = new BigInteger(1, b);
    if (!dsaSigner.verifySignature(asUnsignedByteArray(bmpi.mod(q)), r, s)) {
        throw new OtrCryptoException("DSA signature verification failed.");
    }
}

From source file:net.java.otr4j.crypto.OtrCryptoEngineImpl.java

License:Apache License

@Override
public byte[] sign(byte[] b, PrivateKey privatekey) throws OtrCryptoException {
    if (!(privatekey instanceof DSAPrivateKey))
        throw new IllegalArgumentException();

    DSAParams dsaParams = ((DSAPrivateKey) privatekey).getParams();
    DSAParameters bcDSAParameters = new DSAParameters(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());

    DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privatekey;
    DSAPrivateKeyParameters bcDSAPrivateKeyParms = new DSAPrivateKeyParameters(dsaPrivateKey.getX(),
            bcDSAParameters);/*www . j av a 2 s  .  com*/

    DSASigner dsaSigner = new DSASigner();
    dsaSigner.init(true, bcDSAPrivateKeyParms);

    BigInteger q = dsaParams.getQ();

    // Ian: Note that if you can get the standard DSA implementation you're
    // using to not hash its input, you should be able to pass it ((256-bit
    // value) mod q), (rather than truncating the 256-bit value) and all
    // should be well.
    // ref: Interop problems with libotr - DSA signature
    BigInteger bmpi = new BigInteger(1, b);
    BigInteger[] rs = dsaSigner.generateSignature(BigIntegers.asUnsignedByteArray(bmpi.mod(q)));

    int siglen = q.bitLength() / 4;
    int rslen = siglen / 2;
    byte[] rb = BigIntegers.asUnsignedByteArray(rs[0]);
    byte[] sb = BigIntegers.asUnsignedByteArray(rs[1]);

    // Create the final signature array, padded with zeros if necessary.
    byte[] sig = new byte[siglen];
    System.arraycopy(rb, 0, sig, rslen - rb.length, rb.length);
    System.arraycopy(sb, 0, sig, sig.length - sb.length, sb.length);
    return sig;
}

From source file:net.java.otr4j.crypto.OtrCryptoEngineImpl.java

License:Apache License

private Boolean verify(byte[] b, PublicKey pubKey, BigInteger r, BigInteger s) throws OtrCryptoException {
    if (!(pubKey instanceof DSAPublicKey))
        throw new IllegalArgumentException();

    DSAParams dsaParams = ((DSAPublicKey) pubKey).getParams();

    BigInteger q = dsaParams.getQ();
    DSAParameters bcDSAParams = new DSAParameters(dsaParams.getP(), q, dsaParams.getG());

    DSAPublicKey dsaPrivateKey = (DSAPublicKey) pubKey;
    DSAPublicKeyParameters dsaPrivParms = new DSAPublicKeyParameters(dsaPrivateKey.getY(), bcDSAParams);

    // Ian: Note that if you can get the standard DSA implementation you're
    // using to not hash its input, you should be able to pass it ((256-bit
    // value) mod q), (rather than truncating the 256-bit value) and all
    // should be well.
    // ref: Interop problems with libotr - DSA signature
    DSASigner dsaSigner = new DSASigner();
    dsaSigner.init(false, dsaPrivParms);

    BigInteger bmpi = new BigInteger(1, b);
    Boolean result = dsaSigner.verifySignature(BigIntegers.asUnsignedByteArray(bmpi.mod(q)), r, s);
    return result;
}

From source file:net.jradius.client.auth.EAPTLSAuthenticator.java

License:Open Source License

/**
 * Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
 * //from w  w  w.  j  ava 2s.  c om
 * @param keyInfo the PrivateKeyInfo object containing the key material
 * @return a suitable private key parameter
 * @throws IOException on an error decoding the key
 */
public static AsymmetricKeyParameter createKey(PrivateKeyInfo keyInfo) throws IOException {
    AlgorithmIdentifier algId = keyInfo.getAlgorithmId();

    if (algId.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption)) {
        RSAPrivateKeyStructure keyStructure = new RSAPrivateKeyStructure(
                (ASN1Sequence) keyInfo.getPrivateKey());

        return new RSAPrivateCrtKeyParameters(keyStructure.getModulus(), keyStructure.getPublicExponent(),
                keyStructure.getPrivateExponent(), keyStructure.getPrime1(), keyStructure.getPrime2(),
                keyStructure.getExponent1(), keyStructure.getExponent2(), keyStructure.getCoefficient());
    } else if (algId.getObjectId().equals(PKCSObjectIdentifiers.dhKeyAgreement)) {
        DHParameter params = new DHParameter((ASN1Sequence) keyInfo.getAlgorithmId().getParameters());
        DERInteger derX = (DERInteger) keyInfo.getPrivateKey();

        BigInteger lVal = params.getL();
        int l = lVal == null ? 0 : lVal.intValue();
        DHParameters dhParams = new DHParameters(params.getP(), params.getG(), null, l);

        return new DHPrivateKeyParameters(derX.getValue(), dhParams);
    } else if (algId.getObjectId().equals(OIWObjectIdentifiers.elGamalAlgorithm)) {
        ElGamalParameter params = new ElGamalParameter((ASN1Sequence) keyInfo.getAlgorithmId().getParameters());
        DERInteger derX = (DERInteger) keyInfo.getPrivateKey();

        return new ElGamalPrivateKeyParameters(derX.getValue(),
                new ElGamalParameters(params.getP(), params.getG()));
    } else if (algId.getObjectId().equals(X9ObjectIdentifiers.id_dsa)) {
        DERInteger derX = (DERInteger) keyInfo.getPrivateKey();
        DEREncodable de = keyInfo.getAlgorithmId().getParameters();

        DSAParameters parameters = null;
        if (de != null) {
            DSAParameter params = DSAParameter.getInstance(de.getDERObject());
            parameters = new DSAParameters(params.getP(), params.getQ(), params.getG());
        }

        return new DSAPrivateKeyParameters(derX.getValue(), parameters);
    } else if (algId.getObjectId().equals(X9ObjectIdentifiers.id_ecPublicKey)) {
        X962Parameters params = new X962Parameters((DERObject) keyInfo.getAlgorithmId().getParameters());
        ECDomainParameters dParams = null;

        if (params.isNamedCurve()) {
            DERObjectIdentifier oid = (DERObjectIdentifier) params.getParameters();
            X9ECParameters ecP = X962NamedCurves.getByOID(oid);

            if (ecP == null) {
                ecP = SECNamedCurves.getByOID(oid);

                if (ecP == null) {
                    ecP = NISTNamedCurves.getByOID(oid);

                    if (ecP == null) {
                        ecP = TeleTrusTNamedCurves.getByOID(oid);
                    }
                }
            }

            dParams = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
        } else {
            X9ECParameters ecP = new X9ECParameters((ASN1Sequence) params.getParameters());
            dParams = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
        }

        ECPrivateKeyStructure ec = new ECPrivateKeyStructure((ASN1Sequence) keyInfo.getPrivateKey());

        return new ECPrivateKeyParameters(ec.getKey(), dParams);
    } else {
        throw new RuntimeException("algorithm identifier in key not recognised");
    }
}