sun.security.util.ECUtil Java Examples

The following examples show how to use sun.security.util.ECUtil. 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: P11ECKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private PrivateKey generatePrivate(BigInteger s, ECParameterSpec params)
        throws PKCS11Exception {
    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(getSunECProvider(), params);
    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
        new CK_ATTRIBUTE(CKA_VALUE, s),
        new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PRIVATE_KEY, CKK_EC, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.privateKey
            (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
Example #2
Source File: ECKeyPairGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private KeyPair generateKeyPairNative(SecureRandom random)
    throws Exception {

    ECParameterSpec ecParams = (ECParameterSpec) params;
    byte[] encodedParams = ECUtil.encodeECParameterSpec(null, ecParams);

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    random.nextBytes(seed);
    Object[] keyBytes = generateECKeyPair(keySize, encodedParams, seed);

    // The 'params' object supplied above is equivalent to the native
    // one so there is no need to fetch it.
    // keyBytes[0] is the encoding of the native private key
    BigInteger s = new BigInteger(1, (byte[]) keyBytes[0]);

    PrivateKey privateKey = new ECPrivateKeyImpl(s, ecParams);

    // keyBytes[1] is the encoding of the native public key
    byte[] pubKey = (byte[]) keyBytes[1];
    ECPoint w = ECUtil.decodePoint(pubKey, ecParams.getCurve());
    PublicKey publicKey = new ECPublicKeyImpl(w, ecParams);

    return new KeyPair(publicKey, privateKey);
}
 
Example #3
Source File: ECKeyPairGenerator.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private KeyPair generateKeyPairNative(SecureRandom random)
    throws Exception {

    ECParameterSpec ecParams = (ECParameterSpec) params;
    byte[] encodedParams = ECUtil.encodeECParameterSpec(null, ecParams);

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    random.nextBytes(seed);
    Object[] keyBytes = generateECKeyPair(keySize, encodedParams, seed);

    // The 'params' object supplied above is equivalent to the native
    // one so there is no need to fetch it.
    // keyBytes[0] is the encoding of the native private key
    BigInteger s = new BigInteger(1, (byte[]) keyBytes[0]);

    PrivateKey privateKey = new ECPrivateKeyImpl(s, ecParams);

    // keyBytes[1] is the encoding of the native public key
    byte[] pubKey = (byte[]) keyBytes[1];
    ECPoint w = ECUtil.decodePoint(pubKey, ecParams.getCurve());
    PublicKey publicKey = new ECPublicKeyImpl(w, ecParams);

    return new KeyPair(publicKey, privateKey);
}
 
Example #4
Source File: ECDHKeyAgreement.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected byte[] engineGenerateSecret() throws IllegalStateException {
    if ((privateKey == null) || (publicValue == null)) {
        throw new IllegalStateException("Not initialized correctly");
    }

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

    try {

        return deriveKey(s, publicValue, encodedParams);

    } catch (GeneralSecurityException e) {
        throw new ProviderException("Could not derive key", e);
    }

}
 
Example #5
Source File: ECPrivateKeyImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a key from its components. Used by the
 * KeyFactory.
 */
ECPrivateKeyImpl(BigInteger s, ECParameterSpec params)
        throws InvalidKeyException {
    this.s = s;
    this.params = params;
    // generate the encoding
    algid = new AlgorithmId
        (AlgorithmId.EC_oid, ECParameters.getAlgorithmParameters(params));
    try {
        DerOutputStream out = new DerOutputStream();
        out.putInteger(1); // version 1
        byte[] privBytes = ECUtil.trimZeroes(s.toByteArray());
        out.putOctetString(privBytes);
        DerValue val =
            new DerValue(DerValue.tag_Sequence, out.toByteArray());
        key = val.toByteArray();
    } catch (IOException exc) {
        // should never occur
        throw new InvalidKeyException(exc);
    }
}
 
Example #6
Source File: P11ECKeyFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private PrivateKey generatePrivate(BigInteger s, ECParameterSpec params)
        throws PKCS11Exception {
    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(getSunECProvider(), params);
    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
        new CK_ATTRIBUTE(CKA_VALUE, s),
        new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PRIVATE_KEY, CKK_EC, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.privateKey
            (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
Example #7
Source File: P11ECKeyFactory.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private PrivateKey generatePrivate(BigInteger s, ECParameterSpec params)
        throws PKCS11Exception {
    byte[] encodedParams =
        ECUtil.encodeECParameterSpec(getSunECProvider(), params);
    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
        new CK_ATTRIBUTE(CKA_VALUE, s),
        new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PRIVATE_KEY, CKK_EC, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.privateKey
            (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
Example #8
Source File: ECKeyPairGenerator.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private KeyPair generateKeyPairNative(SecureRandom random)
    throws Exception {

    ECParameterSpec ecParams = (ECParameterSpec) params;
    byte[] encodedParams = ECUtil.encodeECParameterSpec(null, ecParams);

    // seed is twice the key size (in bytes) plus 1
    byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
    random.nextBytes(seed);
    Object[] keyBytes = generateECKeyPair(keySize, encodedParams, seed);

    // The 'params' object supplied above is equivalent to the native
    // one so there is no need to fetch it.
    // keyBytes[0] is the encoding of the native private key
    BigInteger s = new BigInteger(1, (byte[]) keyBytes[0]);

    PrivateKey privateKey = new ECPrivateKeyImpl(s, ecParams);

    // keyBytes[1] is the encoding of the native public key
    byte[] pubKey = (byte[]) keyBytes[1];
    ECPoint w = ECUtil.decodePoint(pubKey, ecParams.getCurve());
    PublicKey publicKey = new ECPublicKeyImpl(w, ecParams);

    return new KeyPair(publicKey, privateKey);
}
 
Example #9
Source File: ECDHKeyAgreement.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected byte[] engineGenerateSecret() throws IllegalStateException {
    if ((privateKey == null) || (publicValue == null)) {
        throw new IllegalStateException("Not initialized correctly");
    }

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

    try {

        return deriveKey(s, publicValue, encodedParams);

    } catch (GeneralSecurityException e) {
        throw new ProviderException("Could not derive key", e);
    }

}
 
Example #10
Source File: ECKeyPairGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    if (params instanceof ECParameterSpec) {
        this.params = ECUtil.getECParameterSpec(null,
                                                (ECParameterSpec)params);
        if (this.params == null) {
            throw new InvalidAlgorithmParameterException(
                "Unsupported curve: " + params);
        }
    } else if (params instanceof ECGenParameterSpec) {
        String name = ((ECGenParameterSpec)params).getName();
        this.params = ECUtil.getECParameterSpec(null, name);
        if (this.params == null) {
            throw new InvalidAlgorithmParameterException(
                "Unknown curve name: " + name);
        }
    } else {
        throw new InvalidAlgorithmParameterException(
            "ECParameterSpec or ECGenParameterSpec required for EC");
    }
    this.keySize =
        ((ECParameterSpec)this.params).getCurve().getField().getFieldSize();
    this.random = random;
}
 
Example #11
Source File: CurveDB.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static NamedCurve lookup(ECParameterSpec params) {
    if ((params instanceof NamedCurve) || (params == null)) {
        return (NamedCurve)params;
    }

    // This is a hack to allow SunJSSE to work with 3rd party crypto
    // providers for ECC and not just SunPKCS11.
    // This can go away once we decide how to expose curve names in the
    // public API.
    // Note that it assumes that the 3rd party provider encodes named
    // curves using the short form, not explicitly. If it did that, then
    // the SunJSSE TLS ECC extensions are wrong, which could lead to
    // interoperability problems.
    int fieldSize = params.getCurve().getField().getFieldSize();
    for (NamedCurve namedCurve : specCollection) {
        // ECParameterSpec does not define equals, so check all the
        // components ourselves.
        // Quick field size check first
        if (namedCurve.getCurve().getField().getFieldSize() != fieldSize) {
            continue;
        }
        if (ECUtil.equals(namedCurve, params)) {
            // everything matches our named curve, return it
            return namedCurve;
        }
    }
    // no match found
    return null;
}
 
Example #12
Source File: TestECDSA2.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private KeyPair genECKeyPair(String curvName, String privD, String pubX,
        String pubY, Provider p) throws Exception {
    ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName);
    ECPrivateKeySpec privKeySpec =
        new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
    ECPublicKeySpec pubKeySpec =
        new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)),
                            ecParams);
    PrivateKey privKey = kf.generatePrivate(privKeySpec);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    return new KeyPair(pubKey, privKey);
}
 
Example #13
Source File: P11ECKeyFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
PrivateKey implTranslatePrivateKey(PrivateKey key)
        throws InvalidKeyException {
    try {
        if (key instanceof ECPrivateKey) {
            ECPrivateKey ecKey = (ECPrivateKey)key;
            return generatePrivate(
                ecKey.getS(),
                ecKey.getParams()
            );
        } else if ("PKCS#8".equals(key.getFormat())) {
            // let Sun provider parse for us, then recurse
            byte[] encoded = key.getEncoded();

            try {
                key = ECUtil.decodePKCS8ECPrivateKey(encoded);
            } catch (InvalidKeySpecException ikse) {
                throw new InvalidKeyException(ikse);
            }

            return implTranslatePrivateKey(key);
        } else {
            throw new InvalidKeyException("PrivateKey must be instance "
                    + "of ECPrivateKey or have PKCS#8 encoding");
        }
    } catch (PKCS11Exception e) {
        throw new InvalidKeyException("Could not create EC private key", e);
    }
}
 
Example #14
Source File: P11ECKeyFactory.java    From openjdk-jdk8u-backup 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: TestECDH2.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private KeyPair genECKeyPair(String curvName, String privD, String pubX,
                             String pubY, Provider p) throws Exception {
    ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName);
    ECPrivateKeySpec privKeySpec =
        new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
    ECPublicKeySpec pubKeySpec =
        new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),
                                        new BigInteger(pubY, 16)),
                            ecParams);
    PrivateKey privKey = kf.generatePrivate(privKeySpec);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    return new KeyPair(pubKey, privKey);
}
 
Example #16
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 #17
Source File: TestECDSA2.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private KeyPair genECKeyPair(String curvName, String privD, String pubX,
        String pubY, Provider p) throws Exception {
    ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName);
    ECPrivateKeySpec privKeySpec =
        new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
    ECPublicKeySpec pubKeySpec =
        new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)),
                            ecParams);
    PrivateKey privKey = kf.generatePrivate(privKeySpec);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    return new KeyPair(pubKey, privKey);
}
 
Example #18
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 #19
Source File: P11Key.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
synchronized byte[] getEncodedInternal() {
    token.ensureValid();
    if (encoded == null) {
        fetchValues();
        try {
            Key key = ECUtil.generateECPrivateKey(s, params);
            encoded = key.getEncoded();
        } catch (InvalidKeySpecException e) {
            throw new ProviderException(e);
        }
    }
    return encoded;
}
 
Example #20
Source File: P11ECKeyFactory.java    From jdk8u60 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 #21
Source File: ECKeyPairGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void initialize(int keySize, SecureRandom random) {

    checkKeySize(keySize);
    this.params = ECUtil.getECParameterSpec(null, keySize);
    if (params == null) {
        throw new InvalidParameterException(
            "No EC parameters available for key size " + keySize + " bits");
    }
    this.random = random;
}
 
Example #22
Source File: TestECDH2.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private KeyPair genECKeyPair(String curvName, String privD, String pubX,
                             String pubY, Provider p) throws Exception {
    ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName);
    ECPrivateKeySpec privKeySpec =
        new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
    ECPublicKeySpec pubKeySpec =
        new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),
                                        new BigInteger(pubY, 16)),
                            ecParams);
    PrivateKey privKey = kf.generatePrivate(privKeySpec);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    return new KeyPair(pubKey, privKey);
}
 
Example #23
Source File: TestECDSA2.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private KeyPair genECKeyPair(String curvName, String privD, String pubX,
        String pubY, Provider p) throws Exception {
    ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName);
    ECPrivateKeySpec privKeySpec =
        new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
    ECPublicKeySpec pubKeySpec =
        new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)),
                            ecParams);
    PrivateKey privKey = kf.generatePrivate(privKeySpec);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    return new KeyPair(pubKey, privKey);
}
 
Example #24
Source File: ECDHKeyAgreement.java    From jdk8u60 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 #25
Source File: TestECDH2.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private KeyPair genECKeyPair(String curvName, String privD, String pubX,
                             String pubY, Provider p) throws Exception {
    ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName);
    ECPrivateKeySpec privKeySpec =
        new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
    ECPublicKeySpec pubKeySpec =
        new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),
                                        new BigInteger(pubY, 16)),
                            ecParams);
    PrivateKey privKey = kf.generatePrivate(privKeySpec);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    return new KeyPair(pubKey, privKey);
}
 
Example #26
Source File: ECKeyPairGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void initialize(int keySize, SecureRandom random) {

    checkKeySize(keySize);
    this.params = ECUtil.getECParameterSpec(null, keySize);
    if (params == null) {
        throw new InvalidParameterException(
            "No EC parameters available for key size " + keySize + " bits");
    }
    this.random = random;
}
 
Example #27
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 #28
Source File: P11ECKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
PrivateKey implTranslatePrivateKey(PrivateKey key)
        throws InvalidKeyException {
    try {
        if (key instanceof ECPrivateKey) {
            ECPrivateKey ecKey = (ECPrivateKey)key;
            return generatePrivate(
                ecKey.getS(),
                ecKey.getParams()
            );
        } else if ("PKCS#8".equals(key.getFormat())) {
            // let Sun provider parse for us, then recurse
            byte[] encoded = key.getEncoded();

            try {
                key = ECUtil.decodePKCS8ECPrivateKey(encoded);
            } catch (InvalidKeySpecException ikse) {
                throw new InvalidKeyException(ikse);
            }

            return implTranslatePrivateKey(key);
        } else {
            throw new InvalidKeyException("PrivateKey must be instance "
                    + "of ECPrivateKey or have PKCS#8 encoding");
        }
    } catch (PKCS11Exception e) {
        throw new InvalidKeyException("Could not create EC private key", e);
    }
}
 
Example #29
Source File: P11Key.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
synchronized byte[] getEncodedInternal() {
    token.ensureValid();
    if (encoded == null) {
        fetchValues();
        try {
            return ECUtil.x509EncodeECPublicKey(w, params);
        } catch (InvalidKeySpecException e) {
            throw new ProviderException(e);
        }
    }
    return encoded;
}
 
Example #30
Source File: P11Key.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
synchronized byte[] getEncodedInternal() {
    token.ensureValid();
    if (encoded == null) {
        fetchValues();
        try {
            Key key = ECUtil.generateECPrivateKey(s, params);
            encoded = key.getEncoded();
        } catch (InvalidKeySpecException e) {
            throw new ProviderException(e);
        }
    }
    return encoded;
}