Example usage for org.bouncycastle.crypto.params ECDomainParameters getCurve

List of usage examples for org.bouncycastle.crypto.params ECDomainParameters getCurve

Introduction

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

Prototype

public ECCurve getCurve() 

Source Link

Usage

From source file:ACNS.thresholdDSA.Util.java

License:Apache License

public static boolean verifySignature(byte[] message, BigInteger r, BigInteger s, byte[] pub,
        ECDomainParameters Curve) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(Curve.getCurve().decodePoint(pub), Curve);
    signer.init(false, params);/*  w  w w.j  a  va2s  . c  om*/
    try {
        return signer.verifySignature(message, r, s);
    } catch (NullPointerException e) {
        // Bouncy Castle contains a bug that can cause NPEs given specially
        // crafted signatures. Those signatures
        // are inherently invalid/attack sigs so we just fail them here
        // rather than crash the thread.
        System.out.println("Caught NPE inside bouncy castle");
        e.printStackTrace();
        return false;
    }
}

From source file:ACNS.thresholdDSA.Util.java

License:Apache License

@SuppressWarnings("deprecation")
public static ECPoint compressPoint(ECPoint uncompressed, ECDomainParameters CURVE) {
    return new ECPoint.Fp(CURVE.getCurve(), uncompressed.getX(), uncompressed.getY(), true);
}

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

License:Apache License

/**
 * Set//from www .  j a  v a2 s  .  co  m
 * <code>ECDomainParameters</code> for EC curve
 *
 * @param parameters
 * @see ECDomainParameters
 */
final void setDomainParameters(ECDomainParameters parameters) {
    a.setBigInteger(parameters.getCurve().getA().toBigInteger());
    b.setBigInteger(parameters.getCurve().getB().toBigInteger());
    // generator
    g.setBytes(parameters.getG().getEncoded());
    // order
    r.setBigInteger(parameters.getN());
    // cofactor
    setK(parameters.getH().shortValue());
    if (parameters.getCurve() instanceof ECCurve.Fp) {
        ECCurve.Fp ecfp = (ECCurve.Fp) parameters.getCurve();
        fp.setBigInteger(ecfp.getQ());
    } else {
        ECCurve.F2m ecf2m = (ECCurve.F2m) parameters.getCurve();
        setFieldF2M((short) ecf2m.getK1(), (short) ecf2m.getK2(), (short) ecf2m.getK3());
    }
}

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

License:Apache License

/**
 * Get <code>ECPublicKeyParameters</code>
 * @return parameters for use with BouncyCastle API
 * @see ECPublicKeyParameters/* w  ww.  j av a2s .  c  o m*/
 */
public CipherParameters getParameters() {
    if (!isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    ECDomainParameters dp = getDomainParameters();
    return new ECPublicKeyParameters(dp.getCurve().decodePoint(w.getBytes(JCSystem.CLEAR_ON_RESET)), dp);
}

From source file:dorkbox.util.crypto.CryptoECC.java

License:Apache License

public static boolean compare(ECPrivateKeyParameters privateA, ECPrivateKeyParameters privateB) {
    ECDomainParameters parametersA = privateA.getParameters();
    ECDomainParameters parametersB = privateB.getParameters();

    // is it the same curve?
    boolean equals = parametersA.getCurve().equals(parametersB.getCurve());
    if (!equals) {
        return false;
    }//from ww w . j  av a 2  s.co m

    equals = parametersA.getG().equals(parametersB.getG());
    if (!equals) {
        return false;
    }

    equals = parametersA.getH().equals(parametersB.getH());
    if (!equals) {
        return false;
    }

    equals = parametersA.getN().equals(parametersB.getN());
    if (!equals) {
        return false;
    }

    equals = privateA.getD().equals(privateB.getD());

    return equals;
}

From source file:dorkbox.util.crypto.CryptoECC.java

License:Apache License

/**
 * @return true if publicA and publicB are NOT NULL, and are both equal to eachother
 *//*from   w w  w  .jav  a  2  s  . c  o  m*/
@SuppressWarnings({ "RedundantIfStatement", "SpellCheckingInspection" })
public static boolean compare(ECPublicKeyParameters publicA, ECPublicKeyParameters publicB) {
    if (publicA == null || publicB == null) {
        return false;
    }

    ECDomainParameters parametersA = publicA.getParameters();
    ECDomainParameters parametersB = publicB.getParameters();

    // is it the same curve?
    boolean equals = parametersA.getCurve().equals(parametersB.getCurve());
    if (!equals) {
        return false;
    }

    equals = parametersA.getG().equals(parametersB.getG());
    if (!equals) {
        return false;
    }

    equals = parametersA.getH().equals(parametersB.getH());
    if (!equals) {
        return false;
    }

    equals = parametersA.getN().equals(parametersB.getN());
    if (!equals) {
        return false;
    }

    ECPoint normalizeA = publicA.getQ().normalize();
    ECPoint normalizeB = publicB.getQ().normalize();

    ECFieldElement xCoordA = normalizeA.getXCoord();
    ECFieldElement xCoordB = normalizeB.getXCoord();

    equals = xCoordA.equals(xCoordB);
    if (!equals) {
        return false;
    }

    ECFieldElement yCoordA = normalizeA.getYCoord();
    ECFieldElement yCoordB = normalizeB.getYCoord();

    equals = yCoordA.equals(yCoordB);
    if (!equals) {
        return false;
    }

    return true;
}

From source file:dorkbox.util.serialization.EccPrivateKeySerializer.java

License:Apache License

public static void write(Output output, ECPrivateKeyParameters key) throws KryoException {
    byte[] bytes;
    int length;// w ww  .  j av  a2s.c om

    ECDomainParameters parameters = key.getParameters();
    ECCurve curve = parameters.getCurve();

    EccPrivateKeySerializer.serializeCurve(output, curve);

    /////////////
    BigInteger n = parameters.getN();
    ECPoint g = parameters.getG();

    /////////////
    bytes = n.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    serializeECPoint(g, output);

    /////////////
    bytes = key.getD().toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);
}

From source file:dorkbox.util.serialization.EccPublicKeySerializer.java

License:Apache License

public static void write(Output output, ECPublicKeyParameters key) throws KryoException {
    byte[] bytes;
    int length;//from w w  w  . j  a  va2s  .  com

    ECDomainParameters parameters = key.getParameters();
    ECCurve curve = parameters.getCurve();

    EccPrivateKeySerializer.serializeCurve(output, curve);

    /////////////
    BigInteger n = parameters.getN();
    ECPoint g = parameters.getG();

    /////////////
    bytes = n.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    EccPrivateKeySerializer.serializeECPoint(g, output);
    EccPrivateKeySerializer.serializeECPoint(key.getQ(), output);
}

From source file:me.grapebaba.hyperledger.fabric.Crypto.java

License:Apache License

public ByteString eciesDecrypt(PrivateKey recipientPrivateKey, ByteString cipherText) {
    BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) recipientPrivateKey;
    ECNamedCurveSpec ecNamedCurveSpec = (ECNamedCurveSpec) bcecPrivateKey.getParams();
    int level = SecurityLevel.from(ecNamedCurveSpec.getName()).size();

    //cipherText = ephemeralPubKeyBytes + encryptedTokBytes + macBytes
    //ephemeralPubKeyBytes = first ((384+7)/8)*2 + 1 bytes = first 97 bytes
    //hmac is sha3_384 = 48 bytes or sha3_256 = 32 bytes
    int ephemeralPubKeyLength = ((level + 7) / 8) * 2 + 1;
    int hmacLength = level >> 3;
    int cipherTextLength = cipherText.size();

    if (cipherTextLength <= ephemeralPubKeyLength + hmacLength)
        throw new RuntimeException(String.format("Illegal cipherText length: %d must be > %d", cipherTextLength,
                ephemeralPubKeyLength + hmacLength));

    ByteString ephemeralPubKey = cipherText.substring(0, ephemeralPubKeyLength);
    ByteString encryptedContent = cipherText.substring(ephemeralPubKeyLength, cipherTextLength - hmacLength);
    ByteString hmac = cipherText.substring(cipherTextLength - hmacLength);

    ECPrivateKeyParameters ecdhPrivateKeyParameters;
    try {//from www.  j av  a  2  s  .  c  om
        ecdhPrivateKeyParameters = (ECPrivateKeyParameters) (PrivateKeyFactory
                .createKey(bcecPrivateKey.getEncoded()));
    } catch (IOException e) {
        logger.error("ECIES decrypt load private key exception", e);
        throw new RuntimeException(e);
    }
    ECDomainParameters ecDomainParameters = ecdhPrivateKeyParameters.getParameters();
    ECCurve ecCurve = ecDomainParameters.getCurve();
    ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(
            ecCurve.decodePoint(ephemeralPubKey.toByteArray()), ecDomainParameters);
    BasicAgreement agree = new ECDHBasicAgreement();
    agree.init(ecdhPrivateKeyParameters);
    byte[] keyAgreement = agree.calculateAgreement(ecPublicKeyParameters).toByteArray();

    HKDFParameters hkdfParameters = new HKDFParameters(keyAgreement, null, null);
    HKDFBytesGenerator hkdfBytesGenerator = new HKDFBytesGenerator(digest);
    hkdfBytesGenerator.init(hkdfParameters);
    byte[] hkdfOutputBytes = new byte[AESKEY_LENGTH + HMACKEY_LENGTH];
    hkdfBytesGenerator.generateBytes(hkdfOutputBytes, 0, AESKEY_LENGTH + HMACKEY_LENGTH);
    ByteString hkdfOutput = ByteString.copyFrom(hkdfOutputBytes);
    ByteString aesKey = hkdfOutput.substring(0, AESKEY_LENGTH);
    ByteString hmacKey = hkdfOutput.substring(AESKEY_LENGTH, AESKEY_LENGTH + HMACKEY_LENGTH);
    HMac hMac = new HMac(digest);
    hMac.init(new KeyParameter(hmacKey.toByteArray()));
    hMac.update(encryptedContent.toByteArray(), 0, encryptedContent.size());
    byte[] recoveredHmac = new byte[hMac.getMacSize()];
    hMac.doFinal(recoveredHmac, 0);
    if (!MessageDigest.isEqual(hmac.toByteArray(), recoveredHmac)) {
        throw new RuntimeException("HMAC verify failed");
    }

    CFBBlockCipher aesCipher = new CFBBlockCipher(new AESEngine(), BLOCK_BIT_SIZE);
    ByteString iv = encryptedContent.substring(0, IV_LENGTH);
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(aesKey.toByteArray()), iv.toByteArray());
    aesCipher.init(false, ivAndKey);
    byte[] decryptedBytes = new byte[500];
    aesCipher.decryptBlock(encryptedContent.substring(IV_LENGTH).toByteArray(), 0, decryptedBytes, 0);
    return ByteString.copyFrom(decryptedBytes);
}

From source file:org.cryptacular.adapter.AbstractWrappedECKey.java

License:Open Source License

/** @return  EC domain parameters. */
public ECParameterSpec getParams() {
    final ECDomainParameters params = delegate.getParameters();
    return new ECParameterSpec(EC5Util.convertCurve(params.getCurve(), params.getSeed()),
            new ECPoint(params.getG().normalize().getXCoord().toBigInteger(),
                    params.getG().normalize().getYCoord().toBigInteger()),
            params.getN(), params.getH().intValue());
}