Example usage for org.bouncycastle.asn1.sec SECNamedCurves getByName

List of usage examples for org.bouncycastle.asn1.sec SECNamedCurves getByName

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.sec SECNamedCurves getByName.

Prototype

public static X9ECParameters getByName(String name) 

Source Link

Usage

From source file:ACNS.thresholdDSA.Util.java

License:Apache License

public static PublicParameters generateParamsforBitcoin(int k, int kPrime, SecureRandom rand,
        PaillierKey paillierPubKey) {//from w  w w  .  j a  va 2s .  c o m

    X9ECParameters params = SECNamedCurves.getByName("secp256k1");
    ECDomainParameters CURVE = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(),
            params.getH());

    int primeCertainty = k;
    BigInteger p;
    BigInteger q;
    BigInteger pPrime;
    BigInteger qPrime;
    BigInteger pPrimeqPrime;
    BigInteger nHat;

    do {
        p = new BigInteger(kPrime / 2, primeCertainty, rand);
    } while (!p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2)).isProbablePrime(primeCertainty));

    pPrime = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));

    do {
        q = new BigInteger(kPrime / 2, primeCertainty, rand);
    } while (!q.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2)).isProbablePrime(primeCertainty));

    qPrime = q.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));

    // generate nhat. the product of two safe primes, each of length
    // kPrime/2
    nHat = p.multiply(q);

    BigInteger h2 = randomFromZnStar(nHat, rand);
    pPrimeqPrime = pPrime.multiply(qPrime);

    BigInteger x = randomFromZn(pPrimeqPrime, rand);
    BigInteger h1 = h2.modPow(x, nHat);

    return new PublicParameters(CURVE, nHat, kPrime, h1, h2, paillierPubKey);

}

From source file:com.bitsofproof.supernode.api.KeyFormatter.java

License:Apache License

private ECKeyPair parseBIP38EC(byte[] store, boolean compressed, boolean hasLot) throws ValidationException {
    byte[] addressHash = new byte[4];
    System.arraycopy(store, 3, addressHash, 0, 4);

    byte[] ownentropy = new byte[8];
    System.arraycopy(store, 7, ownentropy, 0, 8);

    byte[] ownersalt = ownentropy;
    if (hasLot) {
        ownersalt = new byte[4];
        System.arraycopy(ownentropy, 0, ownersalt, 0, 4);
    }/*from ww  w  . j  a  v  a 2 s.c  om*/
    try {
        byte[] passfactor = SCrypt.generate(passphrase.getBytes("UTF-8"), ownersalt, 16384, 8, 8, 32);
        if (hasLot) {
            byte[] tmp = new byte[40];
            System.arraycopy(passfactor, 0, tmp, 0, 32);
            System.arraycopy(ownentropy, 0, tmp, 32, 8);
            passfactor = Hash.hash(tmp);
        }
        ECKeyPair kp = new ECKeyPair(passfactor, true);

        byte[] salt = new byte[12];
        System.arraycopy(store, 3, salt, 0, 12);
        byte[] derived = SCrypt.generate(kp.getPublic(), salt, 1024, 1, 1, 64);
        byte[] aeskey = new byte[32];
        System.arraycopy(derived, 32, aeskey, 0, 32);

        SecretKeySpec keyspec = new SecretKeySpec(aeskey, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
        cipher.init(Cipher.DECRYPT_MODE, keyspec);

        byte[] encrypted = new byte[16];
        System.arraycopy(store, 23, encrypted, 0, 16);
        byte[] decrypted2 = cipher.doFinal(encrypted);
        for (int i = 0; i < 16; ++i) {
            decrypted2[i] ^= derived[i + 16];
        }

        System.arraycopy(store, 15, encrypted, 0, 8);
        System.arraycopy(decrypted2, 0, encrypted, 8, 8);
        byte[] decrypted1 = cipher.doFinal(encrypted);
        for (int i = 0; i < 16; ++i) {
            decrypted1[i] ^= derived[i];
        }

        byte[] seed = new byte[24];
        System.arraycopy(decrypted1, 0, seed, 0, 16);
        System.arraycopy(decrypted2, 8, seed, 16, 8);
        BigInteger priv = new BigInteger(1, passfactor).multiply(new BigInteger(1, Hash.hash(seed)))
                .remainder(SECNamedCurves.getByName("secp256k1").getN());

        kp = new ECKeyPair(priv, compressed);
        byte[] acs = Hash
                .hash(AddressConverter.toSatoshiStyle(kp.getAddress(), addressFlag).getBytes("US-ASCII"));
        byte[] check = new byte[4];
        System.arraycopy(acs, 0, check, 0, 4);
        if (!Arrays.equals(check, addressHash)) {
            throw new ValidationException("failed to decrpyt");
        }
        return kp;
    } catch (UnsupportedEncodingException e) {
        throw new ValidationException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new ValidationException(e);
    } catch (NoSuchProviderException e) {
        throw new ValidationException(e);
    } catch (NoSuchPaddingException e) {
        throw new ValidationException(e);
    } catch (InvalidKeyException e) {
        throw new ValidationException(e);
    } catch (IllegalBlockSizeException e) {
        throw new ValidationException(e);
    } catch (BadPaddingException e) {
        throw new ValidationException(e);
    }
}

From source file:com.bitsofproof.supernode.wallet.KeyFormatter.java

License:Apache License

private ECKeyPair parseBIP38EC(byte[] store, boolean compressed, boolean hasLot) throws ValidationException {
    byte[] addressHash = new byte[4];
    System.arraycopy(store, 3, addressHash, 0, 4);

    byte[] ownentropy = new byte[8];
    System.arraycopy(store, 7, ownentropy, 0, 8);

    byte[] ownersalt = ownentropy;
    if (hasLot) {
        ownersalt = new byte[4];
        System.arraycopy(ownentropy, 0, ownersalt, 0, 4);
    }// w  w  w  . j a  va  2  s.  com
    try {
        byte[] passfactor = SCrypt.scrypt(passphrase.getBytes("UTF-8"), ownersalt, 16384, 8, 8, 32);
        if (hasLot) {
            byte[] tmp = new byte[40];
            System.arraycopy(passfactor, 0, tmp, 0, 32);
            System.arraycopy(ownentropy, 0, tmp, 32, 8);
            passfactor = Hash.hash(tmp);
        }
        ECKeyPair kp = new ECKeyPair(passfactor, true);

        byte[] salt = new byte[12];
        System.arraycopy(store, 3, salt, 0, 12);
        byte[] derived = SCrypt.scrypt(kp.getPublic(), salt, 1024, 1, 1, 64);
        byte[] aeskey = new byte[32];
        System.arraycopy(derived, 32, aeskey, 0, 32);

        SecretKeySpec keyspec = new SecretKeySpec(aeskey, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
        cipher.init(Cipher.DECRYPT_MODE, keyspec);

        byte[] encrypted = new byte[16];
        System.arraycopy(store, 23, encrypted, 0, 16);
        byte[] decrypted2 = cipher.doFinal(encrypted);
        for (int i = 0; i < 16; ++i) {
            decrypted2[i] ^= derived[i + 16];
        }

        System.arraycopy(store, 15, encrypted, 0, 8);
        System.arraycopy(decrypted2, 0, encrypted, 8, 8);
        byte[] decrypted1 = cipher.doFinal(encrypted);
        for (int i = 0; i < 16; ++i) {
            decrypted1[i] ^= derived[i];
        }

        byte[] seed = new byte[24];
        System.arraycopy(decrypted1, 0, seed, 0, 16);
        System.arraycopy(decrypted2, 8, seed, 16, 8);
        BigInteger priv = new BigInteger(1, passfactor).multiply(new BigInteger(1, Hash.hash(seed)))
                .remainder(SECNamedCurves.getByName("secp256k1").getN());

        kp = new ECKeyPair(priv, compressed);
        byte[] acs = Hash.hash(new Address(network, kp.getAddress()).toString().getBytes("US-ASCII"));
        byte[] check = new byte[4];
        System.arraycopy(acs, 0, check, 0, 4);
        if (!Arrays.equals(check, addressHash)) {
            throw new ValidationException("failed to decrpyt");
        }
        return kp;
    } catch (UnsupportedEncodingException e) {
        throw new ValidationException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new ValidationException(e);
    } catch (NoSuchProviderException e) {
        throw new ValidationException(e);
    } catch (NoSuchPaddingException e) {
        throw new ValidationException(e);
    } catch (InvalidKeyException e) {
        throw new ValidationException(e);
    } catch (IllegalBlockSizeException e) {
        throw new ValidationException(e);
    } catch (BadPaddingException e) {
        throw new ValidationException(e);
    } catch (GeneralSecurityException e) {
        throw new ValidationException("JVM doesn't support SHA1PRNG or HMAC_SHA256?");
    }
}

From source file:com.google.bitcoin.core.ECKey.java

License:Apache License

/**
 * Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by the BitCoin reference
 * implementation in its wallet storage format.
 *//*from w  w w . ja va2s.  com*/
public byte[] toASN1() {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(400);

        // ASN1_SEQUENCE(EC_PRIVATEKEY) = {
        //   ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
        //   ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
        //   ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
        //   ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
        // } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
        DERSequenceGenerator seq = new DERSequenceGenerator(baos);
        seq.addObject(new ASN1Integer(1)); // version
        seq.addObject(new DEROctetString(priv.toByteArray()));
        seq.addObject(new DERTaggedObject(0, SECNamedCurves.getByName("secp256k1").toASN1Primitive()));
        seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey())));
        seq.close();
        return baos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e); // Cannot happen, writing to memory stream.
    }
}

From source file:com.google.u2f.server.impl.BouncyCastleCrypto.java

License:Open Source License

@Override
public PublicKey decodePublicKey(byte[] encodedPublicKey) throws U2FException {
    try {/*from  ww w  . j  av  a  2 s.co m*/
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECPoint point;
        try {
            point = curve.getCurve().decodePoint(encodedPublicKey);
        } catch (RuntimeException e) {
            throw new U2FException("Couldn't parse user public key", e);
        }

        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point,
                new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(), curve.getH())));
    } catch (InvalidKeySpecException e) {
        throw new U2FException("Error when decoding public key", e);
    } catch (NoSuchAlgorithmException e) {
        throw new U2FException("Error when decoding public key", e);
    }
}

From source file:com.google.u2f.TestUtils.java

License:Open Source License

public static PrivateKey parsePrivateKey(String keyBytesHex) {
    try {//from  ww w  .  j  av  a2 s  .  co  m
        KeyFactory fac = KeyFactory.getInstance("ECDSA");
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECParameterSpec curveSpec = new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(),
                curve.getH());
        ECPrivateKeySpec keySpec = new ECPrivateKeySpec(new BigInteger(keyBytesHex, 16), curveSpec);
        return fac.generatePrivate(keySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.u2f.TestUtils.java

License:Open Source License

public static PublicKey parsePublicKey(byte[] keyBytes) {
    try {/*from  ww w  .  jav  a2s.com*/
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECParameterSpec curveSpec = new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(),
                curve.getH());
        ECPoint point = curve.getCurve().decodePoint(keyBytes);
        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point, curveSpec));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jin.u2f.crypto.BouncyCastleCrypto.java

License:Open Source License

public PublicKey decodePublicKey(byte[] encodedPublicKey) throws U2fBadInputException {
    try {//w w  w.  jav a2s.  com
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECPoint point;
        try {
            point = curve.getCurve().decodePoint(encodedPublicKey);
        } catch (RuntimeException e) {
            throw new U2fBadInputException("Could not parse user public key", e);
        }

        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point,
                new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(), curve.getH())));
    } catch (GeneralSecurityException e) { // This should not happen
        throw new RuntimeException(e);
    }
}

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

License:Apache License

/**
 * Get defaults//w ww .j  av  a  2 s.co m
 * <code>ECDomainParameters</code> for EC curve
 * {@link http://www.secg.org/collateral/sec2_final.pdf}
 *
 * @param keyType
 * @param keySize
 * @return parameters for use with BouncyCastle API
 * @see ECDomainParameters
 */
static ECDomainParameters getDefaultsDomainParameters(byte keyType, short keySize) {
    String curveName = "";
    switch (keySize) {
    case 113:
    case 131:
    case 163:
    case 193:
        if ((keyType != KeyBuilder.TYPE_EC_F2M_PRIVATE) & (keyType != KeyBuilder.TYPE_EC_F2M_PUBLIC)) {
            CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
        }
        curveName = "sect" + keySize + "r1";
        break;
    case 112:
    case 128:
    case 160:
    case 192:
    case 256:
        if ((keyType != KeyBuilder.TYPE_EC_FP_PRIVATE) & (keyType != KeyBuilder.TYPE_EC_FP_PUBLIC)) {
            CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
        }
        curveName = "secp" + keySize + "r1";
        break;
    default:
        CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
        break;
    }
    X9ECParameters x9params = SECNamedCurves.getByName(curveName);
    return new ECDomainParameters(x9params.getCurve(), x9params.getG(), // G
            x9params.getN(), x9params.getH(), x9params.getSeed());
}

From source file:com.yubico.u2f.crypto.BouncyCastleCrypto.java

License:Open Source License

@Override
public PublicKey decodePublicKey(byte[] encodedPublicKey) throws U2fBadInputException {
    try {/*www .ja  va  2s  .  c  o  m*/
        X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
        ECPoint point;
        try {
            point = curve.getCurve().decodePoint(encodedPublicKey);
        } catch (RuntimeException e) {
            throw new U2fBadInputException("Could not parse user public key", e);
        }

        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point,
                new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(), curve.getH())));
    } catch (GeneralSecurityException e) { //This should not happen
        throw new RuntimeException(e);
    }
}