Search in sources :

Example 16 with ECCurve

use of org.bouncycastle.math.ec.ECCurve in project robovm by robovm.

the class JCEECPublicKey method getEncoded.

public byte[] getEncoded() {
    ASN1Encodable params;
    SubjectPublicKeyInfo info;
    // BEGIN android-removed
    // if (algorithm.equals("ECGOST3410"))
    // {
    //     if (gostParams != null)
    //     {
    //         params = gostParams;
    //     }
    //     else
    //     {
    //         if (ecSpec instanceof ECNamedCurveSpec)
    //         {
    //             params = new GOST3410PublicKeyAlgParameters(
    //                            ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec)ecSpec).getName()),
    //                            CryptoProObjectIdentifiers.gostR3411_94_CryptoProParamSet);
    //         }
    //         else
    //         {   // strictly speaking this may not be applicable...
    //             ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
    //
    //             X9ECParameters ecP = new X9ECParameters(
    //                 curve,
    //                 EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
    //                 ecSpec.getOrder(),
    //                 BigInteger.valueOf(ecSpec.getCofactor()),
    //                 ecSpec.getCurve().getSeed());
    //
    //             params = new X962Parameters(ecP);
    //         }
    //     }
    //
    //     BigInteger      bX = this.q.getX().toBigInteger();
    //     BigInteger      bY = this.q.getY().toBigInteger();
    //     byte[]          encKey = new byte[64];
    //
    //     extractBytes(encKey, 0, bX);
    //     extractBytes(encKey, 32, bY);
    //
    //     try
    //     {
    //         info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, params), new DEROctetString(encKey));
    //     }
    //     catch (IOException e)
    //     {
    //         return null;
    //     }
    // }
    // else
    // END android-removed
    {
        if (ecSpec instanceof ECNamedCurveSpec) {
            ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveSpec) ecSpec).getName());
            if (curveOid == null) {
                curveOid = new ASN1ObjectIdentifier(((ECNamedCurveSpec) ecSpec).getName());
            }
            params = new X962Parameters(curveOid);
        } else if (ecSpec == null) {
            params = new X962Parameters(DERNull.INSTANCE);
        } else {
            ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
            X9ECParameters ecP = new X9ECParameters(curve, EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression), ecSpec.getOrder(), BigInteger.valueOf(ecSpec.getCofactor()), ecSpec.getCurve().getSeed());
            params = new X962Parameters(ecP);
        }
        ECCurve curve = this.engineGetQ().getCurve();
        ASN1OctetString p = (ASN1OctetString) new X9ECPoint(curve.createPoint(this.getQ().getX().toBigInteger(), this.getQ().getY().toBigInteger(), withCompression)).toASN1Primitive();
        info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());
    }
    return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
}
Also used : X962Parameters(org.bouncycastle.asn1.x9.X962Parameters) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) X9ECPoint(org.bouncycastle.asn1.x9.X9ECPoint) ECCurve(org.bouncycastle.math.ec.ECCurve) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(org.bouncycastle.jce.spec.ECNamedCurveSpec) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 17 with ECCurve

use of org.bouncycastle.math.ec.ECCurve in project habot by ghys.

the class Utils method loadPublicKey.

/**
 * Load the public key from a URL-safe base64 encoded string. Takes into
 * account the different encodings, including point compression.
 *
 * @param encodedPublicKey
 */
public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] decodedPublicKey = base64Decode(encodedPublicKey);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
    ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECCurve curve = parameterSpec.getCurve();
    ECPoint point = curve.decodePoint(decodedPublicKey);
    ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec);
    return keyFactory.generatePublic(pubSpec);
}
Also used : ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) ECCurve(org.bouncycastle.math.ec.ECCurve) ECPoint(org.bouncycastle.math.ec.ECPoint) KeyFactory(java.security.KeyFactory) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec)

Example 18 with ECCurve

use of org.bouncycastle.math.ec.ECCurve in project openremote by openremote.

the class ProvisioningPublicKeyState method generateSharedECDHSecret.

private void generateSharedECDHSecret(final byte[] provisioneePublicKeyXYPDU) {
    if (provisioneePublicKeyXYPDU.length != 66) {
        throw new IllegalArgumentException("Invalid Provisionee Public Key PDU," + " length of the Provisionee public key must be 66 bytes, but was " + provisioneePublicKeyXYPDU.length);
    }
    final ByteBuffer buffer = ByteBuffer.allocate(provisioneePublicKeyXYPDU.length - 2);
    buffer.put(provisioneePublicKeyXYPDU, 2, buffer.limit());
    final byte[] xy = mTempProvisioneeXY = buffer.array();
    mUnprovisionedMeshNode.setProvisioneePublicKeyXY(xy);
    final byte[] xComponent = new byte[32];
    System.arraycopy(xy, 0, xComponent, 0, xComponent.length);
    final byte[] yComponent = new byte[32];
    System.arraycopy(xy, 32, yComponent, 0, xComponent.length);
    final byte[] provisioneeX = convertToLittleEndian(xComponent, ByteOrder.LITTLE_ENDIAN);
    LOG.info("Provsionee X: " + MeshParserUtils.bytesToHex(provisioneeX, false));
    final byte[] provisioneeY = convertToLittleEndian(yComponent, ByteOrder.LITTLE_ENDIAN);
    LOG.info("Provsionee Y: " + MeshParserUtils.bytesToHex(provisioneeY, false));
    final BigInteger x = BigIntegers.fromUnsignedByteArray(xy, 0, 32);
    final BigInteger y = BigIntegers.fromUnsignedByteArray(xy, 32, 32);
    final ECParameterSpec ecParameters = ECNamedCurveTable.getParameterSpec("secp256r1");
    ECCurve curve = ecParameters.getCurve();
    ECPoint ecPoint = curve.createPoint(x, y);
    ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPoint, ecParameters);
    KeyFactory keyFactory;
    try {
        keyFactory = KeyFactory.getInstance("ECDH", "SC");
        ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic(keySpec);
        KeyAgreement a = KeyAgreement.getInstance("ECDH", "SC");
        a.init(mProvisionerPrivaetKey);
        a.doPhase(publicKey, true);
        final byte[] sharedECDHSecret = a.generateSecret();
        mUnprovisionedMeshNode.setSharedECDHSecret(sharedECDHSecret);
        LOG.info("ECDH Secret: " + MeshParserUtils.bytesToHex(sharedECDHSecret, false));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ECPoint(org.bouncycastle.math.ec.ECPoint) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) ECCurve(org.bouncycastle.math.ec.ECCurve) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyAgreement(javax.crypto.KeyAgreement) NoSuchProviderException(java.security.NoSuchProviderException) KeyFactory(java.security.KeyFactory)

Example 19 with ECCurve

use of org.bouncycastle.math.ec.ECCurve in project hutool by looly.

the class BCUtil method decodeECPoint.

/**
 * 解码恢复EC压缩公钥,支持Base64和Hex编码,(基于BouncyCastle)
 *
 * @param encodeByte 压缩公钥
 * @param curveName  EC曲线名,例如{@link SmUtil#SM2_DOMAIN_PARAMS}
 * @return 公钥
 * @since 4.4.4
 */
public static PublicKey decodeECPoint(byte[] encodeByte, String curveName) {
    final X9ECParameters x9ECParameters = ECUtil.getNamedCurveByName(curveName);
    final ECCurve curve = x9ECParameters.getCurve();
    final ECPoint point = EC5Util.convertPoint(curve.decodePoint(encodeByte));
    // 根据曲线恢复公钥格式
    final ECNamedCurveSpec ecSpec = new ECNamedCurveSpec(curveName, curve, x9ECParameters.getG(), x9ECParameters.getN());
    return KeyUtil.generatePublicKey("EC", new ECPublicKeySpec(point, ecSpec));
}
Also used : X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ECCurve(org.bouncycastle.math.ec.ECCurve) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECNamedCurveSpec(org.bouncycastle.jce.spec.ECNamedCurveSpec)

Aggregations

ECCurve (org.bouncycastle.math.ec.ECCurve)19 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)12 ECNamedCurveSpec (org.bouncycastle.jce.spec.ECNamedCurveSpec)10 X962Parameters (org.bouncycastle.asn1.x9.X962Parameters)9 EllipticCurve (java.security.spec.EllipticCurve)7 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)6 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)6 X9ECPoint (org.bouncycastle.asn1.x9.X9ECPoint)6 IOException (java.io.IOException)5 DERObjectIdentifier (org.bouncycastle.asn1.DERObjectIdentifier)5 ECFieldF2m (java.security.spec.ECFieldF2m)4 ECFieldFp (java.security.spec.ECFieldFp)4 ECPoint (java.security.spec.ECPoint)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 ECParameterSpec (java.security.spec.ECParameterSpec)3 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)3 DERBitString (org.bouncycastle.asn1.DERBitString)3 DEROctetString (org.bouncycastle.asn1.DEROctetString)3 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)3 SubjectPublicKeyInfo (org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)3