Java Code Examples for sun.security.util.ECUtil#encodePoint()

The following examples show how to use sun.security.util.ECUtil#encodePoint() . 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: ECDHKeyAgreement.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static
byte[] deriveKeyNative(ECPrivateKey privateKey, ECPublicKey publicKey) {

    ECParameterSpec params = privateKey.getParams();
    byte[] s = privateKey.getS().toByteArray();
    byte[] encodedParams =                   // DER OID
        ECUtil.encodeECParameterSpec(null, params);

    byte[] publicValue;
    if (publicKey instanceof ECPublicKeyImpl) {
        ECPublicKeyImpl ecPub = (ECPublicKeyImpl) publicKey;
        publicValue = ecPub.getEncodedPublicValue();
    } else { // instanceof ECPublicKey
        publicValue =
            ECUtil.encodePoint(publicKey.getW(), params.getCurve());
    }

    try {
        return deriveKey(s, publicValue, encodedParams);

    } catch (GeneralSecurityException e) {
        throw new ProviderException("Could not derive key", e);
    }
}
 
Example 2
Source File: P11ECKeyFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private PublicKey generatePublic(ECPoint point, ECParameterSpec params)
        throws PKCS11Exception {
    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(getSunECProvider(), params);
    byte[] encodedPoint =
        ECUtil.encodePoint(point, params.getCurve());

    // Check whether the X9.63 encoding of an EC point shall be wrapped
    // in an ASN.1 OCTET STRING
    if (!token.config.getUseEcX963Encoding()) {
        try {
            encodedPoint =
                new DerValue(DerValue.tag_OctetString, encodedPoint)
                    .toByteArray();
        } catch (IOException e) {
            throw new
                IllegalArgumentException("Could not DER encode point", e);
        }
    }

    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
        new CK_ATTRIBUTE(CKA_EC_POINT, encodedPoint),
        new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PUBLIC_KEY, CKK_EC, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.publicKey
            (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
Example 3
Source File: P11ECKeyFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private PublicKey generatePublic(ECPoint point, ECParameterSpec params)
        throws PKCS11Exception {
    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(getSunECProvider(), params);
    byte[] encodedPoint =
        ECUtil.encodePoint(point, params.getCurve());

    // Check whether the X9.63 encoding of an EC point shall be wrapped
    // in an ASN.1 OCTET STRING
    if (!token.config.getUseEcX963Encoding()) {
        try {
            encodedPoint =
                new DerValue(DerValue.tag_OctetString, encodedPoint)
                    .toByteArray();
        } catch (IOException e) {
            throw new
                IllegalArgumentException("Could not DER encode point", e);
        }
    }

    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
        new CK_ATTRIBUTE(CKA_EC_POINT, encodedPoint),
        new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PUBLIC_KEY, CKK_EC, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.publicKey
            (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
Example 4
Source File: P11ECKeyFactory.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static byte[] getEncodedPublicValue(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey ecKey = (ECPublicKey)key;
        ECPoint w = ecKey.getW();
        ECParameterSpec params = ecKey.getParams();
        return ECUtil.encodePoint(w, params.getCurve());
    } else {
        // should never occur
        throw new InvalidKeyException
            ("Key class not yet supported: " + key.getClass().getName());
    }
}
 
Example 5
Source File: ECDHKeyAgreement.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Key engineDoPhase(Key key, boolean lastPhase)
        throws InvalidKeyException, IllegalStateException {
    if (privateKey == null) {
        throw new IllegalStateException("Not initialized");
    }
    if (publicValue != null) {
        throw new IllegalStateException("Phase already executed");
    }
    if (!lastPhase) {
        throw new IllegalStateException
            ("Only two party agreement supported, lastPhase must be true");
    }
    if (!(key instanceof ECPublicKey)) {
        throw new InvalidKeyException
            ("Key must be a PublicKey with algorithm EC");
    }

    ECPublicKey ecKey = (ECPublicKey)key;
    ECParameterSpec params = ecKey.getParams();

    if (ecKey instanceof ECPublicKeyImpl) {
        publicValue = ((ECPublicKeyImpl)ecKey).getEncodedPublicValue();
    } else { // instanceof ECPublicKey
        publicValue =
            ECUtil.encodePoint(ecKey.getW(), params.getCurve());
    }
    int keyLenBits = params.getCurve().getField().getFieldSize();
    secretLen = (keyLenBits + 7) >> 3;

    return null;
}
 
Example 6
Source File: ECDHKeyAgreement.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Key engineDoPhase(Key key, boolean lastPhase)
        throws InvalidKeyException, IllegalStateException {
    if (privateKey == null) {
        throw new IllegalStateException("Not initialized");
    }
    if (publicValue != null) {
        throw new IllegalStateException("Phase already executed");
    }
    if (!lastPhase) {
        throw new IllegalStateException
            ("Only two party agreement supported, lastPhase must be true");
    }
    if (!(key instanceof ECPublicKey)) {
        throw new InvalidKeyException
            ("Key must be a PublicKey with algorithm EC");
    }

    ECPublicKey ecKey = (ECPublicKey)key;
    ECParameterSpec params = ecKey.getParams();

    if (ecKey instanceof ECPublicKeyImpl) {
        publicValue = ((ECPublicKeyImpl)ecKey).getEncodedPublicValue();
    } else { // instanceof ECPublicKey
        publicValue =
            ECUtil.encodePoint(ecKey.getW(), params.getCurve());
    }
    int keyLenBits = params.getCurve().getField().getFieldSize();
    secretLen = (keyLenBits + 7) >> 3;

    return null;
}
 
Example 7
Source File: P11ECKeyFactory.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static byte[] getEncodedPublicValue(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey ecKey = (ECPublicKey)key;
        ECPoint w = ecKey.getW();
        ECParameterSpec params = ecKey.getParams();
        return ECUtil.encodePoint(w, params.getCurve());
    } else {
        // should never occur
        throw new InvalidKeyException
            ("Key class not yet supported: " + key.getClass().getName());
    }
}
 
Example 8
Source File: ECDHKeyAgreement.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Key engineDoPhase(Key key, boolean lastPhase)
        throws InvalidKeyException, IllegalStateException {
    if (privateKey == null) {
        throw new IllegalStateException("Not initialized");
    }
    if (publicValue != null) {
        throw new IllegalStateException("Phase already executed");
    }
    if (!lastPhase) {
        throw new IllegalStateException
            ("Only two party agreement supported, lastPhase must be true");
    }
    if (!(key instanceof ECPublicKey)) {
        throw new InvalidKeyException
            ("Key must be a PublicKey with algorithm EC");
    }

    ECPublicKey ecKey = (ECPublicKey)key;
    ECParameterSpec params = ecKey.getParams();

    if (ecKey instanceof ECPublicKeyImpl) {
        publicValue = ((ECPublicKeyImpl)ecKey).getEncodedPublicValue();
    } else { // instanceof ECPublicKey
        publicValue =
            ECUtil.encodePoint(ecKey.getW(), params.getCurve());
    }
    int keyLenBits = params.getCurve().getField().getFieldSize();
    secretLen = (keyLenBits + 7) >> 3;

    return null;
}
 
Example 9
Source File: P11ECKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static byte[] getEncodedPublicValue(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey ecKey = (ECPublicKey)key;
        ECPoint w = ecKey.getW();
        ECParameterSpec params = ecKey.getParams();
        return ECUtil.encodePoint(w, params.getCurve());
    } else {
        // should never occur
        throw new InvalidKeyException
            ("Key class not yet supported: " + key.getClass().getName());
    }
}
 
Example 10
Source File: P11ECKeyFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private PublicKey generatePublic(ECPoint point, ECParameterSpec params)
        throws PKCS11Exception {
    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(getSunECProvider(), params);
    byte[] encodedPoint =
        ECUtil.encodePoint(point, params.getCurve());

    // Check whether the X9.63 encoding of an EC point shall be wrapped
    // in an ASN.1 OCTET STRING
    if (!token.config.getUseEcX963Encoding()) {
        try {
            encodedPoint =
                new DerValue(DerValue.tag_OctetString, encodedPoint)
                    .toByteArray();
        } catch (IOException e) {
            throw new
                IllegalArgumentException("Could not DER encode point", e);
        }
    }

    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
        new CK_ATTRIBUTE(CKA_EC_POINT, encodedPoint),
        new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PUBLIC_KEY, CKK_EC, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.publicKey
            (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
Example 11
Source File: P11ECKeyFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static byte[] getEncodedPublicValue(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey ecKey = (ECPublicKey)key;
        ECPoint w = ecKey.getW();
        ECParameterSpec params = ecKey.getParams();
        return ECUtil.encodePoint(w, params.getCurve());
    } else {
        // should never occur
        throw new InvalidKeyException
            ("Key class not yet supported: " + key.getClass().getName());
    }
}
 
Example 12
Source File: P11ECKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private PublicKey generatePublic(ECPoint point, ECParameterSpec params)
        throws PKCS11Exception {
    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(getSunECProvider(), params);
    byte[] encodedPoint =
        ECUtil.encodePoint(point, params.getCurve());

    // Check whether the X9.63 encoding of an EC point shall be wrapped
    // in an ASN.1 OCTET STRING
    if (!token.config.getUseEcX963Encoding()) {
        try {
            encodedPoint =
                new DerValue(DerValue.tag_OctetString, encodedPoint)
                    .toByteArray();
        } catch (IOException e) {
            throw new
                IllegalArgumentException("Could not DER encode point", e);
        }
    }

    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
        new CK_ATTRIBUTE(CKA_EC_POINT, encodedPoint),
        new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PUBLIC_KEY, CKK_EC, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.publicKey
            (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
Example 13
Source File: ECDHKeyAgreement.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Key engineDoPhase(Key key, boolean lastPhase)
        throws InvalidKeyException, IllegalStateException {
    if (privateKey == null) {
        throw new IllegalStateException("Not initialized");
    }
    if (publicValue != null) {
        throw new IllegalStateException("Phase already executed");
    }
    if (!lastPhase) {
        throw new IllegalStateException
            ("Only two party agreement supported, lastPhase must be true");
    }
    if (!(key instanceof ECPublicKey)) {
        throw new InvalidKeyException
            ("Key must be a PublicKey with algorithm EC");
    }

    ECPublicKey ecKey = (ECPublicKey)key;
    ECParameterSpec params = ecKey.getParams();

    if (ecKey instanceof ECPublicKeyImpl) {
        publicValue = ((ECPublicKeyImpl)ecKey).getEncodedPublicValue();
    } else { // instanceof ECPublicKey
        publicValue =
            ECUtil.encodePoint(ecKey.getW(), params.getCurve());
    }
    int keyLenBits = params.getCurve().getField().getFieldSize();
    secretLen = (keyLenBits + 7) >> 3;

    return null;
}
 
Example 14
Source File: P11ECKeyFactory.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private PublicKey generatePublic(ECPoint point, ECParameterSpec params)
        throws PKCS11Exception {
    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(getSunECProvider(), params);
    byte[] encodedPoint =
        ECUtil.encodePoint(point, params.getCurve());

    // Check whether the X9.63 encoding of an EC point shall be wrapped
    // in an ASN.1 OCTET STRING
    if (!token.config.getUseEcX963Encoding()) {
        try {
            encodedPoint =
                new DerValue(DerValue.tag_OctetString, encodedPoint)
                    .toByteArray();
        } catch (IOException e) {
            throw new
                IllegalArgumentException("Could not DER encode point", e);
        }
    }

    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
        new CK_ATTRIBUTE(CKA_EC_POINT, encodedPoint),
        new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PUBLIC_KEY, CKK_EC, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.publicKey
            (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
Example 15
Source File: JsseJce.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
    return ECUtil.encodePoint(point, curve);
}
 
Example 16
Source File: SM2PublicKeyImpl.java    From julongchain with Apache License 2.0 4 votes vote down vote up
public SM2PublicKeyImpl(ECPoint ecPoint) throws InvalidKeyException {
    this.w = ecPoint;
    this.algid = new AlgorithmId(EC_OID, SM2Parameters.getAlgorithmParameters(params));
    this.key = ECUtil.encodePoint(ecPoint, params.getCurve());
}
 
Example 17
Source File: JsseJce.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
    return ECUtil.encodePoint(point, curve);
}
 
Example 18
Source File: JsseJce.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
    return ECUtil.encodePoint(point, curve);
}
 
Example 19
Source File: JsseJce.java    From openjsse with GNU General Public License v2.0 4 votes vote down vote up
static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
    return ECUtil.encodePoint(point, curve);
}
 
Example 20
Source File: JsseJce.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
    return ECUtil.encodePoint(point, curve);
}