Search in sources :

Example 1 with X9IntegerConverter

use of org.spongycastle.asn1.x9.X9IntegerConverter in project XobotOS by xamarin.

the class JCEECPublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    // BEGIN android-removed
    // if (info.getAlgorithmId().getObjectId().equals(CryptoProObjectIdentifiers.gostR3410_2001))
    // {
    //     DERBitString bits = info.getPublicKeyData();
    //     ASN1OctetString key;
    //     this.algorithm = "ECGOST3410";
    //
    //     try
    //     {
    //         key = (ASN1OctetString) ASN1Object.fromByteArray(bits.getBytes());
    //     }
    //     catch (IOException ex)
    //     {
    //         throw new IllegalArgumentException("error recovering public key");
    //     }
    //
    //     byte[]          keyEnc = key.getOctets();
    //     byte[]          x = new byte[32];
    //     byte[]          y = new byte[32];
    //
    //     for (int i = 0; i != x.length; i++)
    //     {
    //         x[i] = keyEnc[32 - 1 - i];
    //     }
    //
    //     for (int i = 0; i != y.length; i++)
    //     {
    //         y[i] = keyEnc[64 - 1 - i];
    //     }
    //
    //     gostParams = new GOST3410PublicKeyAlgParameters((ASN1Sequence)info.getAlgorithmId().getParameters());
    //
    //     ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
    //
    //     ECCurve curve = spec.getCurve();
    //     EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
    //
    //     this.q = curve.createPoint(new BigInteger(1, x), new BigInteger(1, y), false);
    //
    //     ecSpec = new ECNamedCurveSpec(
    //             ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()),
    //             ellipticCurve,
    //             new ECPoint(
    //                     spec.getG().getX().toBigInteger(),
    //                     spec.getG().getY().toBigInteger()),
    //                     spec.getN(), spec.getH());
    //
    // }
    // else
    // END android-removed
    {
        X962Parameters params = new X962Parameters((DERObject) info.getAlgorithmId().getParameters());
        ECCurve curve;
        EllipticCurve ellipticCurve;
        if (params.isNamedCurve()) {
            DERObjectIdentifier oid = (DERObjectIdentifier) params.getParameters();
            X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
            curve = ecP.getCurve();
            ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
            ecSpec = new ECNamedCurveSpec(ECUtil.getCurveName(oid), ellipticCurve, new ECPoint(ecP.getG().getX().toBigInteger(), ecP.getG().getY().toBigInteger()), ecP.getN(), ecP.getH());
        } else if (params.isImplicitlyCA()) {
            ecSpec = null;
            curve = ProviderUtil.getEcImplicitlyCa().getCurve();
        } else {
            X9ECParameters ecP = new X9ECParameters((ASN1Sequence) params.getParameters());
            curve = ecP.getCurve();
            ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
            this.ecSpec = new ECParameterSpec(ellipticCurve, new ECPoint(ecP.getG().getX().toBigInteger(), ecP.getG().getY().toBigInteger()), ecP.getN(), ecP.getH().intValue());
        }
        DERBitString bits = info.getPublicKeyData();
        byte[] data = bits.getBytes();
        ASN1OctetString key = new DEROctetString(data);
        //
        if (data[0] == 0x04 && data[1] == data.length - 2 && (data[2] == 0x02 || data[2] == 0x03)) {
            int qLength = new X9IntegerConverter().getByteLength(curve);
            if (qLength >= data.length - 3) {
                try {
                    key = (ASN1OctetString) ASN1Object.fromByteArray(data);
                } catch (IOException ex) {
                    throw new IllegalArgumentException("error recovering public key");
                }
            }
        }
        X9ECPoint derQ = new X9ECPoint(curve, key);
        this.q = derQ.getPoint();
    }
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) X9IntegerConverter(org.bouncycastle.asn1.x9.X9IntegerConverter) DERBitString(org.bouncycastle.asn1.DERBitString) IOException(java.io.IOException) X9ECPoint(org.bouncycastle.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) DERObjectIdentifier(org.bouncycastle.asn1.DERObjectIdentifier) DEROctetString(org.bouncycastle.asn1.DEROctetString) X962Parameters(org.bouncycastle.asn1.x9.X962Parameters) DERObject(org.bouncycastle.asn1.DERObject) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) X9ECPoint(org.bouncycastle.asn1.x9.X9ECPoint) ECCurve(org.bouncycastle.math.ec.ECCurve) ECNamedCurveSpec(org.bouncycastle.jce.spec.ECNamedCurveSpec)

Example 2 with X9IntegerConverter

use of org.spongycastle.asn1.x9.X9IntegerConverter in project rskj by rsksmart.

the class ECKey method decompressKey.

/**
 * Decompress a compressed public key (x co-ord and low-bit of y-coord).
 *
 * @param xBN -
 * @param yBit -
 * @return -
 */
private static ECPoint decompressKey(BigInteger xBN, boolean yBit) {
    X9IntegerConverter x9 = new X9IntegerConverter();
    byte[] compEnc = x9.integerToBytes(xBN, 1 + x9.getByteLength(CURVE.getCurve()));
    compEnc[0] = (byte) (yBit ? 0x03 : 0x02);
    return CURVE.getCurve().decodePoint(compEnc);
}
Also used : X9IntegerConverter(org.spongycastle.asn1.x9.X9IntegerConverter)

Example 3 with X9IntegerConverter

use of org.spongycastle.asn1.x9.X9IntegerConverter in project web3sdk by FISCO-BCOS.

the class Sign method decompressKey.

/**
 * Decompress a compressed public key (x co-ord and low-bit of y-coord).
 */
private static ECPoint decompressKey(BigInteger xBN, boolean yBit) {
    X9IntegerConverter x9 = new X9IntegerConverter();
    byte[] compEnc = x9.integerToBytes(xBN, 1 + x9.getByteLength(CURVE.getCurve()));
    compEnc[0] = (byte) (yBit ? 0x03 : 0x02);
    return CURVE.getCurve().decodePoint(compEnc);
}
Also used : X9IntegerConverter(org.bouncycastle.asn1.x9.X9IntegerConverter)

Example 4 with X9IntegerConverter

use of org.spongycastle.asn1.x9.X9IntegerConverter in project java-tron by tronprotocol.

the class ECKey method decompressKey.

/**
 * Decompress a compressed public key (x co-ord and low-bit of y-coord).
 *
 * @param xBN -
 * @param yBit -
 * @return -
 */
private static ECPoint decompressKey(BigInteger xBN, boolean yBit) {
    X9IntegerConverter x9 = new X9IntegerConverter();
    byte[] compEnc = x9.integerToBytes(xBN, 1 + x9.getByteLength(CURVE.getCurve()));
    compEnc[0] = (byte) (yBit ? 0x03 : 0x02);
    return CURVE.getCurve().decodePoint(compEnc);
}
Also used : X9IntegerConverter(org.spongycastle.asn1.x9.X9IntegerConverter)

Example 5 with X9IntegerConverter

use of org.spongycastle.asn1.x9.X9IntegerConverter in project toshi-android-client by toshiapp.

the class ECKey method decompressKey.

/**
 * Decompress a compressed public key (x co-ord and low-bit of y-coord).
 *
 * @param xBN -
 * @param yBit -
 * @return -
 */
private static ECPoint decompressKey(BigInteger xBN, boolean yBit) {
    X9IntegerConverter x9 = new X9IntegerConverter();
    byte[] compEnc = x9.integerToBytes(xBN, 1 + x9.getByteLength(CURVE.getCurve()));
    compEnc[0] = (byte) (yBit ? 0x03 : 0x02);
    return CURVE.getCurve().decodePoint(compEnc);
}
Also used : X9IntegerConverter(org.spongycastle.asn1.x9.X9IntegerConverter)

Aggregations

X9IntegerConverter (org.bouncycastle.asn1.x9.X9IntegerConverter)7 X9IntegerConverter (org.spongycastle.asn1.x9.X9IntegerConverter)6 IOException (java.io.IOException)3 ECParameterSpec (java.security.spec.ECParameterSpec)3 ECPoint (java.security.spec.ECPoint)3 EllipticCurve (java.security.spec.EllipticCurve)3 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 DERBitString (org.bouncycastle.asn1.DERBitString)3 DEROctetString (org.bouncycastle.asn1.DEROctetString)3 X962Parameters (org.bouncycastle.asn1.x9.X962Parameters)3 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)3 X9ECPoint (org.bouncycastle.asn1.x9.X9ECPoint)3 ECNamedCurveSpec (org.bouncycastle.jce.spec.ECNamedCurveSpec)3 ECCurve (org.bouncycastle.math.ec.ECCurve)3 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)2 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)1 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)1 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)1 DERObject (org.bouncycastle.asn1.DERObject)1