Example usage for org.bouncycastle.math.ec ECCurve getFieldSize

List of usage examples for org.bouncycastle.math.ec ECCurve getFieldSize

Introduction

In this page you can find the example usage for org.bouncycastle.math.ec ECCurve getFieldSize.

Prototype

public abstract int getFieldSize();

Source Link

Usage

From source file:com.github.horrorho.inflatabledonkey.crypto.ec.ECAssistant.java

License:Open Source License

public static int fieldLength(ECCurve curve) {
    return fieldLength(curve.getFieldSize());
}

From source file:com.github.horrorho.inflatabledonkey.crypto.ec.ECPointsCompact.java

License:Open Source License

@Deprecated
public static ECPoint decodeFPPoint(ECCurve curve, byte[] data) {
    // Patched org.bouncycastle.math.ec.ECCurve#decodePoint code.
    int expectedLength = (curve.getFieldSize() + 7) / 8;
    if (expectedLength != data.length) {
        throw new IllegalArgumentException("incorrect data length for compact encoding");
    }/*from  w w  w. j a v a  2  s .  com*/

    BigInteger X = BigIntegers.fromUnsignedByteArray(data, 0, expectedLength);
    ECPoint p = decompressFPPoint(curve, X);

    if (!satisfiesCofactor(curve, p)) {
        throw new IllegalArgumentException("invalid point");
    }

    return p;
}

From source file:com.github.horrorho.inflatabledonkey.crypto.rfc6637.RFC6637.java

License:Open Source License

ECPoint decodePoint(byte[] data) {
    ECCurve curve = ECNamedCurveTable.getByName(curveName).getCurve();
    int compactExportSize = (curve.getFieldSize() + 7) / 8;

    return data.length == compactExportSize ? ECPointsCompact.decodeFPPoint(curve, data) // Compact keys support, non RFC6636 compliant.
            : curve.decodePoint(data);//from   www.  j a  v  a  2 s. c o m
}

From source file:org.cesecore.certificates.util.AlgorithmTools.java

License:Open Source License

/**
 * Gets the key specification from a public key. Example: "2048" for a RSA 
 * or DSA key or "secp256r1" for EC key. The EC curve is only detected 
 * if <i>publickey</i> is an object known by the bouncy castle provider.
 * @param publicKey The public key to get the key specification from
 * @return The key specification, "unknown" if it could not be determined and
 * null if the key algorithm is not supported
 *//*  w  w w  .j  a v a 2s .  c  om*/
public static String getKeySpecification(final PublicKey publicKey) {
    if (log.isTraceEnabled()) {
        log.trace(">getKeySpecification");
    }
    String keyspec = null;
    if (publicKey instanceof RSAPublicKey) {
        keyspec = Integer.toString(((RSAPublicKey) publicKey).getModulus().bitLength());
    } else if (publicKey instanceof DSAPublicKey) {
        keyspec = Integer.toString(((DSAPublicKey) publicKey).getParams().getP().bitLength());
    } else if (publicKey instanceof ECPublicKey) {
        final ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
        if (ecPublicKey.getParams() instanceof ECNamedCurveSpec) {
            keyspec = ((ECNamedCurveSpec) ecPublicKey.getParams()).getName();
            // Prefer to return a curve name alias that also works with the default and BC provider
            for (String keySpecAlias : getEcKeySpecAliases(keyspec)) {
                if (isNamedECKnownInDefaultProvider(keySpecAlias)) {
                    keyspec = keySpecAlias;
                    break;
                }
            }
        } else {
            keyspec = KEYSPEC_UNKNOWN;
            // Try to detect if it is a curve name known by BC even though the public key isn't a BC key
            final ECParameterSpec namedCurve = ecPublicKey.getParams();
            if (namedCurve != null) {
                final int c1 = namedCurve.getCofactor();
                final EllipticCurve ec1 = namedCurve.getCurve();
                final BigInteger a1 = ec1.getA();
                final BigInteger b1 = ec1.getB();
                final int fs1 = ec1.getField().getFieldSize();
                //final byte[] s1 = ec1.getSeed();
                final ECPoint g1 = namedCurve.getGenerator();
                final BigInteger ax1 = g1.getAffineX();
                final BigInteger ay1 = g1.getAffineY();
                final BigInteger o1 = namedCurve.getOrder();
                if (log.isDebugEnabled()) {
                    log.debug("a1=" + a1 + " b1=" + b1 + " fs1=" + fs1 + " ax1=" + ax1 + " ay1=" + ay1 + " o1="
                            + o1 + " c1=" + c1);
                }
                @SuppressWarnings("unchecked")
                final Enumeration<String> ecNamedCurves = ECNamedCurveTable.getNames();
                while (ecNamedCurves.hasMoreElements()) {
                    final String ecNamedCurveBc = ecNamedCurves.nextElement();
                    final ECNamedCurveParameterSpec parameterSpec2 = ECNamedCurveTable
                            .getParameterSpec(ecNamedCurveBc);
                    final ECCurve ec2 = parameterSpec2.getCurve();
                    final BigInteger a2 = ec2.getA().toBigInteger();
                    final BigInteger b2 = ec2.getB().toBigInteger();
                    final int fs2 = ec2.getFieldSize();
                    final org.bouncycastle.math.ec.ECPoint g2 = parameterSpec2.getG();
                    final BigInteger ax2 = g2.getX().toBigInteger();
                    final BigInteger ay2 = g2.getY().toBigInteger();
                    final BigInteger h2 = parameterSpec2.getH();
                    final BigInteger n2 = parameterSpec2.getN();
                    if (a1.equals(a2) && ax1.equals(ax2) && b1.equals(b2) && ay1.equals(ay2) && fs1 == fs2
                            && o1.equals(n2) && c1 == h2.intValue()) {
                        // We have a matching curve here!
                        if (log.isDebugEnabled()) {
                            log.debug("a2=" + a2 + " b2=" + b2 + " fs2=" + fs2 + " ax2=" + ax2 + " ay2=" + ay2
                                    + " h2=" + h2 + " n2=" + n2 + " " + ecNamedCurveBc);
                        }
                        // Since this public key is a SUN PKCS#11 pub key if we get here, we only return an alias if it is recognized by the provider
                        if (isNamedECKnownInDefaultProvider(ecNamedCurveBc)) {
                            keyspec = ecNamedCurveBc;
                            break;
                        }
                    }
                }
            }
        }
    }
    if (log.isTraceEnabled()) {
        log.trace("<getKeySpecification: " + keyspec);
    }
    return keyspec;
}

From source file:org.sufficientlysecure.keychain.securitytoken.ECKeyFormat.java

License:Open Source License

public void addToSaveKeyringParcel(SaveKeyringParcel keyring, int keyFlags) {
    final X9ECParameters params = NISTNamedCurves.getByOID(mECCurveOID);
    final ECCurve curve = params.getCurve();

    SaveKeyringParcel.Algorithm algo = SaveKeyringParcel.Algorithm.ECDSA;
    if (((keyFlags & KeyFlags.ENCRYPT_COMMS) == KeyFlags.ENCRYPT_COMMS)
            || ((keyFlags & KeyFlags.ENCRYPT_STORAGE) == KeyFlags.ENCRYPT_STORAGE)) {
        algo = SaveKeyringParcel.Algorithm.ECDH;
    }/*w  ww . j  ava  2  s  .com*/

    SaveKeyringParcel.Curve scurve;
    if (mECCurveOID.equals(NISTNamedCurves.getOID("P-256"))) {
        scurve = SaveKeyringParcel.Curve.NIST_P256;
    } else if (mECCurveOID.equals(NISTNamedCurves.getOID("P-384"))) {
        scurve = SaveKeyringParcel.Curve.NIST_P384;
    } else if (mECCurveOID.equals(NISTNamedCurves.getOID("P-521"))) {
        scurve = SaveKeyringParcel.Curve.NIST_P521;
    } else {
        throw new IllegalArgumentException("Unsupported curve " + mECCurveOID);
    }

    keyring.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(algo, curve.getFieldSize(), scurve, keyFlags, 0L));
}

From source file:org.xipki.ca.api.profile.x509.BaseX509Certprofile.java

License:Open Source License

private static void checkECSubjectPublicKeyInfo(final ASN1ObjectIdentifier curveOid, final byte[] encoded)
        throws BadCertTemplateException {
    Integer expectedLength = ecCurveFieldSizes.get(curveOid);
    if (expectedLength == null) {
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
        ECCurve curve = ecP.getCurve();
        expectedLength = (curve.getFieldSize() + 7) / 8;
        ecCurveFieldSizes.put(curveOid, expectedLength);
    }/*from  ww w.  j a v a2  s. c o  m*/

    switch (encoded[0]) {
    case 0x02: // compressed
    case 0x03: // compressed
    {
        if (encoded.length != (expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for compressed encoding");
        }
        break;
    }
    case 0x04: // uncompressed
    case 0x06: // hybrid
    case 0x07: // hybrid
    {
        if (encoded.length != (2 * expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
        }
        break;
    }
    default:
        throw new BadCertTemplateException("invalid point encoding 0x" + Integer.toString(encoded[0], 16));
    }
}

From source file:org.xipki.ca.qa.impl.X509CertprofileQAImpl.java

License:Open Source License

private static void checkECSubjectPublicKeyInfo(final ASN1ObjectIdentifier curveOid, final byte[] encoded)
        throws BadCertTemplateException {
    Integer expectedLength = ecCurveFieldSizes.get(curveOid);
    if (expectedLength == null) {
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
        ECCurve curve = ecP.getCurve();
        expectedLength = (curve.getFieldSize() + 7) / 8;
        ecCurveFieldSizes.put(curveOid, expectedLength);
    }//from  ww w  .  j ava 2  s . c  o  m

    switch (encoded[0]) {
    case 0x02: // compressed
    case 0x03: // compressed
    {
        if (encoded.length != (expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for compressed encoding");
        }
        break;
    }
    case 0x04: // uncompressed
    case 0x06: // hybrid
    case 0x07: // hybrid
    {
        if (encoded.length != (2 * expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
        }
        break;
    }
    default:
        throw new BadCertTemplateException("invalid point encoding 0x" + Integer.toString(encoded[0], 16));
    }// end switch
}

From source file:org.xipki.pki.ca.api.profile.x509.BaseX509Certprofile.java

License:Open Source License

private static void checkEcSubjectPublicKeyInfo(final ASN1ObjectIdentifier curveOid, final byte[] encoded)
        throws BadCertTemplateException {
    ParamUtil.requireNonNull("curveOid", curveOid);
    ParamUtil.requireNonNull("encoded", encoded);
    ParamUtil.requireMin("encoded.length", encoded.length, 1);

    Integer expectedLength = ecCurveFieldSizes.get(curveOid);
    if (expectedLength == null) {
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
        ECCurve curve = ecP.getCurve();
        expectedLength = (curve.getFieldSize() + 7) / 8;
        ecCurveFieldSizes.put(curveOid, expectedLength);
    }//w w  w. ja  va2  s .c o  m

    switch (encoded[0]) {
    case 0x02: // compressed
    case 0x03: // compressed
        if (encoded.length != (expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for compressed encoding");
        }
        break;
    case 0x04: // uncompressed
    case 0x06: // hybrid
    case 0x07: // hybrid
        if (encoded.length != (2 * expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
        }
        break;
    default:
        throw new BadCertTemplateException(String.format("invalid point encoding 0x%02x", encoded[0]));
    }
}

From source file:org.xipki.pki.ca.qa.PublicKeyChecker.java

License:Open Source License

private static void checkECSubjectPublicKeyInfo(final ASN1ObjectIdentifier curveOid, final byte[] encoded)
        throws BadCertTemplateException {
    Integer expectedLength = EC_CURVEFIELD_SIZES.get(curveOid);
    if (expectedLength == null) {
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(curveOid);
        ECCurve curve = ecP.getCurve();
        expectedLength = (curve.getFieldSize() + 7) / 8;
        EC_CURVEFIELD_SIZES.put(curveOid, expectedLength);
    }//from   w  ww  .j  a  va 2s .  c o  m

    switch (encoded[0]) {
    case 0x02: // compressed
    case 0x03: // compressed
        if (encoded.length != (expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for compressed encoding");
        }
        break;
    case 0x04: // uncompressed
    case 0x06: // hybrid
    case 0x07: // hybrid
        if (encoded.length != (2 * expectedLength + 1)) {
            throw new BadCertTemplateException("incorrect length for uncompressed/hybrid encoding");
        }
        break;
    default:
        throw new BadCertTemplateException("invalid point encoding 0x" + Integer.toString(encoded[0], 16));
    } // end switch
}