org.bouncycastle.asn1.x9.ECNamedCurveTable Java Examples

The following examples show how to use org.bouncycastle.asn1.x9.ECNamedCurveTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: skfsCommon.java    From fido2 with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static String getPolicyCurveFromOID(ASN1ObjectIdentifier oid) {
        String curveName = ECNamedCurveTable.getName(oid);
        switch (curveName) {
            case "P-256":
                return "secp256r1";
            case "P-384":
                return "secp384r1";
            case "P-521":
                return "secp521r1";
//            case "X25519":             //TODO when BCFIPS 1.0.2 supports Curve25519, this probably will display X25519
//                return "curve25519";
            default:
                skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.SEVERE, "FIDO-ERR-2002",
                        "Unsupported Curve" + oid);
                throw new UnsupportedOperationException("Unsupported Curve");
        }
    }
 
Example #2
Source File: DataStore.java    From athenz with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
String getCurveName(org.bouncycastle.jce.spec.ECParameterSpec ecParameterSpec, boolean rfc) {

    String curveName = null;
    for (Enumeration names = ECNamedCurveTable.getNames(); names.hasMoreElements();) {

        final String name = (String) names.nextElement();
        final X9ECParameters params = ECNamedCurveTable.getByName(name);

        if (params.getN().equals(ecParameterSpec.getN())
                && params.getH().equals(ecParameterSpec.getH())
                && params.getCurve().equals(ecParameterSpec.getCurve())
                && params.getG().equals(ecParameterSpec.getG())) {
            curveName = name;
            break;
        }
    }

    return rfc ? rfcEllipticCurveName(curveName) : curveName;
}
 
Example #3
Source File: RFC6637Factory.java    From InflatableDonkey with MIT License 6 votes vote down vote up
private static RFC6637 create(
        String curveName,
        Supplier<Digest> digestFactory,
        Supplier<Wrapper> wrapperFactory,
        int publicKeyAlgID,
        int symAlgID,
        int symAlgIDLength,
        int kdfHashID) {

    try {
        ASN1ObjectIdentifier oid = ECNamedCurveTable.getOID(curveName);

        RFC6637KDF kdf = new RFC6637KDF(
                digestFactory,
                oid,
                (byte) publicKeyAlgID,
                (byte) symAlgID,
                (byte) kdfHashID);

        return new RFC6637(wrapperFactory, curveName, symAlgIDLength, kdf);

    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example #4
Source File: GPSession.java    From GlobalPlatformPro with GNU Lesser General Public License v3.0 6 votes vote down vote up
byte[] encodeECKey(ECPublicKey pubkey) {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();

    try {
        byte[] key = ECNamedCurveTable.getByName("secp256r1").getCurve().createPoint(pubkey.getW().getAffineX(), pubkey.getW().getAffineY()).getEncoded(false);

        bo.write(0xB0); // EC Public key
        bo.write(key.length);
        bo.write(key);
        bo.write(0xF0);
        bo.write(0x01);
        bo.write(0x00); // P-256
        bo.write(0x00); // No KCV
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return bo.toByteArray();
}
 
Example #5
Source File: CryptoPrimitives.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Security Level determines the elliptic curve used in key generation
 *
 * @param securityLevel currently 256 or 384
 * @throws InvalidArgumentException
 */
void setSecurityLevel(final int securityLevel) throws InvalidArgumentException {
    logger.trace(format("setSecurityLevel to %d", securityLevel));

    if (securityCurveMapping.isEmpty()) {
        throw new InvalidArgumentException("Security curve mapping has no entries.");
    }

    if (!securityCurveMapping.containsKey(securityLevel)) {
        StringBuilder sb = new StringBuilder();
        String sp = "";
        for (int x : securityCurveMapping.keySet()) {
            sb.append(sp).append(x);

            sp = ", ";

        }
        throw new InvalidArgumentException(format("Illegal security level: %d. Valid values are: %s", securityLevel, sb.toString()));
    }

    String lcurveName = securityCurveMapping.get(securityLevel);

    logger.debug(format("Mapped curve strength %d to %s", securityLevel, lcurveName));

    X9ECParameters params = ECNamedCurveTable.getByName(lcurveName);
    //Check if can match curve name to requested strength.
    if (params == null) {

        InvalidArgumentException invalidArgumentException = new InvalidArgumentException(
                format("Curve %s defined for security strength %d was not found.", curveName, securityLevel));

        logger.error(invalidArgumentException);
        throw invalidArgumentException;

    }

    curveName = lcurveName;
    this.securityLevel = securityLevel;
}
 
Example #6
Source File: CryptoPrimitives.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Sign data with the specified elliptic curve private key.
 *
 * @param privateKey elliptic curve private key.
 * @param data       data to sign
 * @return the signed data.
 * @throws CryptoException
 */
private byte[] ecdsaSignToBytes(ECPrivateKey privateKey, byte[] data) throws CryptoException {
    if (data == null) {
        throw new CryptoException("Data that to be signed is null.");
    }
    if (data.length == 0) {
        throw new CryptoException("Data to be signed was empty.");
    }

    try {
        X9ECParameters params = ECNamedCurveTable.getByName(curveName);
        BigInteger curveN = params.getN();

        Signature sig = SECURITY_PROVIDER == null ? Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM) :
                Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM, SECURITY_PROVIDER);
        sig.initSign(privateKey);
        sig.update(data);
        byte[] signature = sig.sign();

        BigInteger[] sigs = decodeECDSASignature(signature);

        sigs = preventMalleability(sigs, curveN);

        try (ByteArrayOutputStream s = new ByteArrayOutputStream()) {

            DERSequenceGenerator seq = new DERSequenceGenerator(s);
            seq.addObject(new ASN1Integer(sigs[0]));
            seq.addObject(new ASN1Integer(sigs[1]));
            seq.close();
            return s.toByteArray();
        }

    } catch (Exception e) {
        throw new CryptoException("Could not sign the message using private key", e);
    }

}
 
Example #7
Source File: RFC6637.java    From InflatableDonkey with MIT License 5 votes vote down vote up
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);
}