Java Code Examples for java.security.interfaces.ECPublicKey#getW()

The following examples show how to use java.security.interfaces.ECPublicKey#getW() . 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: JsonWebKey.java    From azure-keyvault-java with MIT License 6 votes vote down vote up
/**
 * Converts EC key pair to JSON web key.
 * 
 * @param keyPair
 *            EC key pair
 * @param provider
 *            Java security provider
 * @return the JSON web key, converted from EC key pair.
 */
public static JsonWebKey fromEC(KeyPair keyPair, Provider provider) {

    ECPublicKey apub = (ECPublicKey) keyPair.getPublic();
    ECPoint point = apub.getW();
    ECPrivateKey apriv = (ECPrivateKey) keyPair.getPrivate();

    if (apriv != null) {
        return new JsonWebKey().withKty(JsonWebKeyType.EC).withCrv(getCurveFromKeyPair(keyPair, provider))
                .withX(point.getAffineX().toByteArray()).withY(point.getAffineY().toByteArray())
                .withD(apriv.getS().toByteArray()).withKty(JsonWebKeyType.EC);
    } else {
        return new JsonWebKey().withKty(JsonWebKeyType.EC).withCrv(getCurveFromKeyPair(keyPair, provider))
                .withX(point.getAffineX().toByteArray()).withY(point.getAffineY().toByteArray())
                .withKty(JsonWebKeyType.EC);
    }
}
 
Example 2
Source File: TPMAuthenticator.java    From webauthn4j with Apache License 2.0 6 votes vote down vote up
private TPMTPublic createTPMTPublic(PublicKey credentialPublicKey) {
    TPMIAlgPublic type = null;
    TPMIAlgHash nameAlg = TPMIAlgHash.TPM_ALG_SHA256;
    TPMAObject objectAttributes = new TPMAObject(394354);
    byte[] authPolicy = Base64UrlUtil.decode("nf_L82w4OuaZ-5ho3G3LidcVOIS-KAOSLBJBWL-tIq4");
    TPMUPublicId unique = null;
    TPMUPublicParms parameters = null;
    if (credentialPublicKey instanceof ECPublicKey) {
        ECPublicKey ecPublicKey = (ECPublicKey) credentialPublicKey;
        EllipticCurve curve = ecPublicKey.getParams().getCurve();
        parameters = new TPMSECCParms(
                new byte[2],
                new byte[2],
                TPMEccCurve.create(curve),
                new byte[2]
        );
        type = TPMIAlgPublic.TPM_ALG_ECDSA;
        ECPoint ecPoint = ecPublicKey.getW();
        byte[] x = ecPoint.getAffineX().toByteArray();
        byte[] y = ecPoint.getAffineY().toByteArray();
        unique = new ECCUnique(x, y);
    }
    return new TPMTPublic(type, nameAlg, objectAttributes, authPolicy, parameters, unique);
}
 
Example 3
Source File: DOMKeyValue.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    try {
        AccessController.doPrivileged(
            new PrivilegedExceptionAction<Void>() {
                public Void run() throws
                    ClassNotFoundException, NoSuchMethodException
                {
                    getMethods();
                    return null;
                }
            }
        );
    } catch (PrivilegedActionException pae) {
        throw new KeyException("ECKeyValue not supported",
                                pae.getException());
    }
    Object[] args = new Object[] { ecPoint, ecParams.getCurve() };
    try {
        ecPublicKey = (byte[])encodePoint.invoke(null, args);
    } catch (IllegalAccessException iae) {
        throw new KeyException(iae);
    } catch (InvocationTargetException ite) {
        throw new KeyException(ite);
    }
}
 
Example 4
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 5 votes vote down vote up
private static ECPoint extractPublicKey(final ECPublicKey ecPublicKey) {
  final java.security.spec.ECPoint publicPointW = ecPublicKey.getW();
  final BigInteger xCoord = publicPointW.getAffineX();
  final BigInteger yCoord = publicPointW.getAffineY();

  return CURVE.getCurve().createPoint(xCoord, yCoord);
}
 
Example 5
Source File: DOMKeyValue.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    try {
        AccessController.doPrivileged(
            new PrivilegedExceptionAction<Void>() {
                public Void run() throws
                    ClassNotFoundException, NoSuchMethodException
                {
                    getMethods();
                    return null;
                }
            }
        );
    } catch (PrivilegedActionException pae) {
        throw new KeyException("ECKeyValue not supported",
                                pae.getException());
    }
    Object[] args = new Object[] { ecPoint, ecParams.getCurve() };
    try {
        ecPublicKey = (byte[])encodePoint.invoke(null, args);
    } catch (IllegalAccessException iae) {
        throw new KeyException(iae);
    } catch (InvocationTargetException ite) {
        throw new KeyException(ite);
    }
}
 
Example 6
Source File: DOMKeyValue.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    ecPublicKey = encodePoint(ecPoint, ecParams.getCurve());
}
 
Example 7
Source File: DOMKeyValue.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    ecPublicKey = encodePoint(ecPoint, ecParams.getCurve());
}
 
Example 8
Source File: DOMKeyValue.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    try {
        AccessController.doPrivileged(
            new PrivilegedExceptionAction<Void>() {
                public Void run() throws
                    ClassNotFoundException, NoSuchMethodException
                {
                    getMethods();
                    return null;
                }
            }
        );
    } catch (PrivilegedActionException pae) {
        throw new KeyException("ECKeyValue not supported",
                                pae.getException());
    }
    Object[] args = new Object[] { ecPoint, ecParams.getCurve() };
    try {
        ecPublicKey = (byte[])encodePoint.invoke(null, args);
    } catch (IllegalAccessException iae) {
        throw new KeyException(iae);
    } catch (InvocationTargetException ite) {
        throw new KeyException(ite);
    }
}
 
Example 9
Source File: EC2COSEKey.java    From webauthn4j with Apache License 2.0 5 votes vote down vote up
public static EC2COSEKey create(KeyPair keyPair, COSEAlgorithmIdentifier alg) {
    if (keyPair != null && keyPair.getPrivate() instanceof ECPrivateKey && keyPair.getPublic() instanceof ECPublicKey) {
        ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate();
        ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
        ECPoint ecPoint = ecPublicKey.getW();
        Curve curve = getCurve(ecPrivateKey.getParams());
        byte[] x = ECUtil.convertToFixedByteArray(curve.getSize(), ecPoint.getAffineX());
        byte[] y = ECUtil.convertToFixedByteArray(curve.getSize(), ecPoint.getAffineY());
        byte[] d = ecPrivateKey.getS().toByteArray();
        return new EC2COSEKey(null, alg, null, curve, x, y, d);
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 10
Source File: EllipticCurveJsonWebKey.java    From Jose4j with Apache License 2.0 5 votes vote down vote up
protected void fillPublicTypeSpecificParams(Map<String,Object> params)
{
    ECPublicKey ecPublicKey = getECPublicKey();
    ECPoint w = ecPublicKey.getW();
    int coordinateByteLength = getCoordinateByteLength();
    putBigIntAsBase64UrlEncodedParam(params, X_MEMBER_NAME, w.getAffineX(), coordinateByteLength);
    putBigIntAsBase64UrlEncodedParam(params, Y_MEMBER_NAME, w.getAffineY(), coordinateByteLength);
    params.put(CURVE_MEMBER_NAME, getCurveName());
}
 
Example 11
Source File: ECKeySecp256k1.java    From aion with MIT License 5 votes vote down vote up
private static ECPoint extractPublicKey(final ECPublicKey ecPublicKey) {
    final java.security.spec.ECPoint publicPointW = ecPublicKey.getW();
    final BigInteger xCoord = publicPointW.getAffineX();
    final BigInteger yCoord = publicPointW.getAffineY();

    return CURVE.getCurve().createPoint(xCoord, yCoord);
}
 
Example 12
Source File: DOMKeyValue.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    try {
        AccessController.doPrivileged(
            new PrivilegedExceptionAction<Void>() {
                public Void run() throws
                    ClassNotFoundException, NoSuchMethodException
                {
                    getMethods();
                    return null;
                }
            }
        );
    } catch (PrivilegedActionException pae) {
        throw new KeyException("ECKeyValue not supported",
                                pae.getException());
    }
    Object[] args = new Object[] { ecPoint, ecParams.getCurve() };
    try {
        ecPublicKey = (byte[])encodePoint.invoke(null, args);
    } catch (IllegalAccessException iae) {
        throw new KeyException(iae);
    } catch (InvocationTargetException ite) {
        throw new KeyException(ite);
    }
}
 
Example 13
Source File: EcUtil.java    From wycheproof with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a weak public key of order 3 such that the public key point is on the curve specified
 * in ecParams. This method is used to check ECC implementations for missing step in the
 * verification of the public key. E.g. implementations of ECDH must verify that the public key
 * contains a point on the curve as well as public and secret key are using the same curve.
 *
 * @param ecParams the parameters of the key to attack. This must be a curve in Weierstrass form
 *     over a prime order field.
 * @return a weak EC group with a genrator of order 3.
 */
public static ECPublicKeySpec getWeakPublicKey(ECParameterSpec ecParams)
    throws GeneralSecurityException {
  EllipticCurve curve = ecParams.getCurve();
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
  keyGen.initialize(ecParams);
  BigInteger p = getModulus(curve);
  BigInteger three = new BigInteger("3");
  while (true) {
    // Generate a point on the original curve
    KeyPair keyPair = keyGen.generateKeyPair();
    ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
    ECPoint w = pub.getW();
    BigInteger x = w.getAffineX();
    BigInteger y = w.getAffineY();
    // Find the curve parameters a,b such that 3*w = infinity.
    // This is the case if the following equations are satisfied:
    //    3x == l^2 (mod p)
    //    l == (3x^2 + a) / 2*y (mod p)
    //    y^2 == x^3 + ax + b (mod p)
    BigInteger l;
    try {
      l = modSqrt(x.multiply(three), p);
    } catch (GeneralSecurityException ex) {
      continue;
    }
    BigInteger xSqr = x.multiply(x).mod(p);
    BigInteger a = l.multiply(y.add(y)).subtract(xSqr.multiply(three)).mod(p);
    BigInteger b = y.multiply(y).subtract(x.multiply(xSqr.add(a))).mod(p);
    EllipticCurve newCurve = new EllipticCurve(curve.getField(), a, b);
    // Just a sanity check.
    checkPointOnCurve(w, newCurve);
    // Cofactor and order are of course wrong.
    ECParameterSpec spec = new ECParameterSpec(newCurve, w, p, 1);
    return new ECPublicKeySpec(w, spec);
  }
}
 
Example 14
Source File: DOMKeyValue.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    try {
        AccessController.doPrivileged(
            new PrivilegedExceptionAction<Void>() {
                public Void run() throws
                    ClassNotFoundException, NoSuchMethodException
                {
                    getMethods();
                    return null;
                }
            }
        );
    } catch (PrivilegedActionException pae) {
        throw new KeyException("ECKeyValue not supported",
                                pae.getException());
    }
    Object[] args = new Object[] { ecPoint, ecParams.getCurve() };
    try {
        ecPublicKey = (byte[])encodePoint.invoke(null, args);
    } catch (IllegalAccessException iae) {
        throw new KeyException(iae);
    } catch (InvocationTargetException ite) {
        throw new KeyException(ite);
    }
}
 
Example 15
Source File: DOMKeyValue.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    try {
        AccessController.doPrivileged(
            new PrivilegedExceptionAction<Void>() {
                public Void run() throws
                    ClassNotFoundException, NoSuchMethodException
                {
                    getMethods();
                    return null;
                }
            }
        );
    } catch (PrivilegedActionException pae) {
        throw new KeyException("ECKeyValue not supported",
                                pae.getException());
    }
    Object[] args = new Object[] { ecPoint, ecParams.getCurve() };
    try {
        ecPublicKey = (byte[])encodePoint.invoke(null, args);
    } catch (IllegalAccessException iae) {
        throw new KeyException(iae);
    } catch (InvocationTargetException ite) {
        throw new KeyException(ite);
    }
}
 
Example 16
Source File: P11ContentSignerBuilder.java    From xipki with Apache License 2.0 4 votes vote down vote up
public ConcurrentContentSigner createSigner(AlgorithmIdentifier signatureAlgId,
    int parallelism) throws XiSecurityException, P11TokenException {
  Args.positive(parallelism, "parallelism");

  List<XiContentSigner> signers = new ArrayList<>(parallelism);

  Boolean isSm2p256v1 = null;
  for (int i = 0; i < parallelism; i++) {
    XiContentSigner signer;
    if (publicKey instanceof RSAPublicKey) {
      if (i == 0 && !AlgorithmUtil.isRSASigAlgId(signatureAlgId)) {
        throw new XiSecurityException(
            "the given algorithm is not a valid RSA signature algorithm '"
            + signatureAlgId.getAlgorithm().getId() + "'");
      }
      signer = createRSAContentSigner(signatureAlgId);
    } else if (publicKey instanceof ECPublicKey) {
      ECPublicKey ecKey = (ECPublicKey) publicKey;

      if (i == 0) {
        isSm2p256v1 = GMUtil.isSm2primev2Curve(ecKey.getParams().getCurve());
        if (isSm2p256v1) {
          if (!AlgorithmUtil.isSM2SigAlg(signatureAlgId)) {
            throw new XiSecurityException(
              "the given algorithm is not a valid SM2 signature algorithm '"
              + signatureAlgId.getAlgorithm().getId() + "'");
          }
        } else {
          if (!AlgorithmUtil.isECSigAlg(signatureAlgId)) {
            throw new XiSecurityException(
              "the given algorithm is not a valid EC signature algorithm '"
              + signatureAlgId.getAlgorithm().getId() + "'");
          }
        }
      }

      if (isSm2p256v1) {
        java.security.spec.ECPoint w = ecKey.getW();
        signer = createSM2ContentSigner(signatureAlgId, GMObjectIdentifiers.sm2p256v1,
            w.getAffineX(), w.getAffineY());
      } else {
        signer = createECContentSigner(signatureAlgId);
      }
    } else if (publicKey instanceof DSAPublicKey) {
      if (i == 0 && !AlgorithmUtil.isDSASigAlg(signatureAlgId)) {
        throw new XiSecurityException(
            "the given algorithm is not a valid DSA signature algorithm '"
            + signatureAlgId.getAlgorithm().getId() + "'");
      }
      signer = createDSAContentSigner(signatureAlgId);
    } else if (publicKey instanceof EdDSAKey) {
      signer = createEdDSAContentSigner(signatureAlgId);
    } else {
      throw new XiSecurityException("unsupported key " + publicKey.getClass().getName());
    }
    signers.add(signer);
  } // end for

  final boolean mac = false;
  PrivateKey privateKey = new P11PrivateKey(cryptService, identityId);
  DfltConcurrentContentSigner concurrentSigner;
  try {
    concurrentSigner = new DfltConcurrentContentSigner(mac, signers, privateKey);
  } catch (NoSuchAlgorithmException ex) {
    throw new XiSecurityException(ex.getMessage(), ex);
  }

  if (certificateChain != null) {
    concurrentSigner.setCertificateChain(certificateChain);
  } else {
    concurrentSigner.setPublicKey(publicKey);
  }

  return concurrentSigner;
}
 
Example 17
Source File: ECDHClientKeyExchange.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
ECDHClientKeyExchange(PublicKey publicKey) {
    ECPublicKey ecKey = (ECPublicKey)publicKey;
    ECPoint point = ecKey.getW();
    ECParameterSpec params = ecKey.getParams();
    encodedPoint = JsseJce.encodePoint(point, params.getCurve());
}
 
Example 18
Source File: ECDHClientKeyExchange.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
ECDHClientKeyExchange(PublicKey publicKey) {
    ECPublicKey ecKey = (ECPublicKey)publicKey;
    ECPoint point = ecKey.getW();
    ECParameterSpec params = ecKey.getParams();
    encodedPoint = JsseJce.encodePoint(point, params.getCurve());
}
 
Example 19
Source File: ECDHClientKeyExchange.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
ECDHClientKeyExchange(PublicKey publicKey) {
    ECPublicKey ecKey = (ECPublicKey)publicKey;
    ECPoint point = ecKey.getW();
    ECParameterSpec params = ecKey.getParams();
    encodedPoint = JsseJce.encodePoint(point, params.getCurve());
}
 
Example 20
Source File: ECDHClientKeyExchange.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
ECDHClientKeyExchange(PublicKey publicKey) {
    ECPublicKey ecKey = (ECPublicKey)publicKey;
    ECPoint point = ecKey.getW();
    ECParameterSpec params = ecKey.getParams();
    encodedPoint = JsseJce.encodePoint(point, params.getCurve());
}