static JsonObject getJwk(PublicKey publicKey, String algHeader) { if (publicKey instanceof RSAPublicKey) { RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; return Json.createObjectBuilder() .add(EXPONENT, base64UrlEncode(rsaPublicKey.getPublicExponent().toByteArray())) .add(KEY_TYPE, "RSA") .add(MODULUS, base64UrlEncode(modulusToByteArray(rsaPublicKey.getModulus()))) .build(); } else if (publicKey instanceof ECPublicKey) { ECPublicKey ecPublicKey = (ECPublicKey) publicKey; int fieldSize = ecPublicKey.getParams().getCurve().getField().getFieldSize(); return Json.createObjectBuilder() .add(CURVE, getCurveParameterFromAlgHeader(algHeader)) .add(KEY_TYPE, "EC") .add(X_COORDINATE, base64UrlEncode(coordinateToByteArray(fieldSize, ecPublicKey.getW().getAffineX()))) .add(Y_COORDINATE, base64UrlEncode(coordinateToByteArray(fieldSize, ecPublicKey.getW().getAffineY()))) .build(); } else { throw acme.unsupportedAcmeAccountPublicKeyType(publicKey.getAlgorithm()); } }
@Override public byte[] getEncoded() { return this.getKey().getEncoded(); }
@Override public String getAlgorithm() { return this.getKey().getAlgorithm(); }
public void init(AlgorithmParameterSpec params, Factory<Random> randomFactory) throws GeneralSecurityException { generator.initialize(params); KeyPair keyPair = generator.generateKeyPair(); agreement.init(keyPair.getPrivate()); ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic(); this.ecParameterSpec = ecPublicKey.getParams(); ECPoint w = ecPublicKey.getW(); byte[] encoded = getEncoded(w, ecParameterSpec.getCurve()); setE(encoded); }
public byte[] getSecret(byte[] r, byte[] s) throws Exception{ KeyFactory kf = KeyFactory.getInstance("EC"); ECPoint w = new ECPoint(new BigInteger(1, r), new BigInteger(1, s)); ECPublicKeySpec spec = new ECPublicKeySpec(w, publicKey.getParams()); PublicKey theirPublicKey = kf.generatePublic(spec); myKeyAgree.doPhase(theirPublicKey, true); return myKeyAgree.generateSecret(); }
protected void assertsEcdsaKey(ECPublicKey pubKey) { final String x = "48439561293906451759052585252797914202762949526041747995844080717082404635286"; final String y = "36134250956749795798585127919587881956611106672985015071877198253568414405109"; Assert.assertEquals(new BigInteger(x), pubKey.getParams().getGenerator().getAffineX()); Assert.assertEquals(new BigInteger(y), pubKey.getParams().getGenerator().getAffineY()); }
kpg.initialize(ecGenParameterSpec); ECPublicKey key = (ECPublicKey) kpg.generateKeyPair().getPublic(); byte[] x = key.getW().getAffineX().toByteArray(); byte[] y = key.getW().getAffineY().toByteArray(); ECPoint point = new ECPoint(new BigInteger(1, x), new BigInteger(1, y)); ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(name); ECParameterSpec spec = new ECNamedCurveSpec(name, parameterSpec.getCurve(), parameterSpec.getG(), parameterSpec.getN(), parameterSpec.getH(), parameterSpec.getSeed()); ECPublicKey ecPublicKey = (ECPublicKey) eckf.generatePublic(new ECPublicKeySpec(point, spec)); System.out.println(ecPublicKey.getClass().getName());
ECPrivateKey ecPrivateKey = (ECPrivateKey)privateKey; System.out.println(ecPrivateKey.getS().toByteArray().length); ECPublicKey ecPublicKey = (ECPublicKey)publicKey; System.out.println(ecPublicKey.getW().getAffineX().toByteArray().length + ecPublicKey.getW().getAffineY().toByteArray().length);
private void checkPointIsOnCurve(ECPublicKey ephemeralPublicKey, ECPrivateKey privateKey) throws JoseException { // to prevent 'Invalid Curve Attack': for NIST curves, check whether public key is on the private key's curve. // from https://www.cs.bris.ac.uk/Research/CryptographySecurity/RWC/2017/nguyen.quan.pdf // there appear to be similar checks in the JVM starting with 1.8.0_51 but // doing it here explicitly seems prudent // (y^2) mod p = (x^3 + ax + b) mod p // thanks to Antonio Sanso for guidance on how to do this check ECParameterSpec ecParameterSpec = privateKey.getParams(); EllipticCurve curve = ecParameterSpec.getCurve(); ECPoint point = ephemeralPublicKey.getW(); BigInteger x = point.getAffineX(); BigInteger y = point.getAffineY(); BigInteger a = curve.getA(); BigInteger b = curve.getB(); BigInteger p = ((ECFieldFp) curve.getField()).getP(); BigInteger leftSide = (y.pow(2)).mod(p); BigInteger rightSide = (x.pow(3).add(a.multiply(x)).add(b)).mod(p); boolean onCurve = leftSide.equals(rightSide); if (!onCurve) { throw new JoseException(HeaderParameterNames.EPHEMERAL_PUBLIC_KEY + " is invalid for " + EllipticCurves.getName(curve)); } }
EllipticCurve curve = new EllipticCurve(field, ecSpec.getCurve().getA().toBigInteger(), ecSpec.getCurve().getB().toBigInteger()); ECPoint pointG = new ECPoint(ecSpec.getG().getXCoord().toBigInteger(), ecSpec.getG().getYCoord().toBigInteger()); ECParameterSpec spec = new ECParameterSpec(curve, pointG, ecSpec.getN(), ecSpec.getH().intValue()); KeyPairGenerator g = KeyPairGenerator.getInstance(KEY_PAIR_GENERATOR_TYPE); g.initialize(spec, new SecureRandom()); KeyPair keysPair = g.generateKeyPair(); ECPrivateKeySpec ecPrivSpec = new ECPrivateKeySpec(((ECPrivateKey) keysPair.getPrivate()).getS(), spec); ECPublicKeySpec ecPublicSpec = new ECPublicKeySpec(((ECPublicKey) keysPair.getPublic()).getW(), spec); KeyFactory kf = KeyFactory.getInstance(KEY_FACTORY_TYPE); PrivateKey privateKey = kf.generatePrivate(ecPrivSpec); BigInteger serialNumber = BigInteger.valueOf(randomNumber >= 0 ? randomNumber : randomNumber * -1); Date notBefore = new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30); Date notAfter = new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10));
@Override public JsonObject jSerialize(PublicKey publicKey) { if (!(publicKey instanceof ECPublicKey)) throw new IllegalStateException("Unsupported Public Key Format (only ECPublicKey supported)."); ECPublicKey ecPublicKey = (ECPublicKey) publicKey; JsonObject o = new JsonObject(); // Get x coordinate byte[] x = ecPublicKey.getW().getAffineX().toByteArray(); if (x[0] == 0) x = Arrays.copyOfRange(x, 1, x.length); o.add("x", Hex.encodeHexString(x)); // Get Y coordinate byte[] y = ecPublicKey.getW().getAffineY().toByteArray(); if (y[0] == 0) y = Arrays.copyOfRange(y, 1, y.length); o.add("y", Hex.encodeHexString(y)); // Get Curves params o.add("params", ecPublicKey.getParams().toString()); // Get raw public key in format SubjectPublicKeyInfo (DER encoding) o.add("b64Der", Base64.encodeBase64String(ecPublicKey.getEncoded())); return o; }
if (pubKey instanceof ECPublicKey) { ECPublicKey ecPubKey = (ECPublicKey)pubKey; ECPoint point = ecPubKey.getW(); BigInteger affineX = point.getAffineX(); BigInteger affineY = point.getAffineY(); System.out.println("Affine X " + affineX.toString()); System.out.println("Affine Y " + affineY.toString()); }
public String getQX(PublicKey publicKey) throws KeyNotSupportedException { if (publicKey instanceof ECPublicKey) { return ((ECPublicKey) publicKey).getW().getAffineX().toString(16); } else { throw new KeyNotSupportedException("Key is not an elliptic curve key!"); } }
public String getQY(PublicKey publicKey) throws KeyNotSupportedException { if (publicKey instanceof ECPublicKey) { return ((ECPublicKey) publicKey).getW().getAffineY().toString(16); } else { throw new KeyNotSupportedException("Key is not an elliptic curve key!"); } } }
/** Checks that the public key's params spec is the same as the private key's params spec. */ static void validatePublicKeySpec(ECPublicKey publicKey, ECPrivateKey privateKey) throws GeneralSecurityException { try { ECParameterSpec publicKeySpec = publicKey.getParams(); ECParameterSpec privateKeySpec = privateKey.getParams(); if (!publicKeySpec.getCurve().equals(privateKeySpec.getCurve()) || !publicKeySpec.getGenerator().equals(privateKeySpec.getGenerator()) || !publicKeySpec.getOrder().equals(privateKeySpec.getOrder()) || publicKeySpec.getCofactor() != privateKeySpec.getCofactor()) { throw new GeneralSecurityException("invalid public key spec"); } } catch (IllegalArgumentException | NullPointerException ex) { // The Java security providers on Android K and Android L might throw these unchecked // exceptions, converting them to a checked one to not crash the JVM. throw new GeneralSecurityException(ex.toString()); } }
private int getKeySize(PublicKey pkey) { if (pkey instanceof ECPublicKey) { return ((ECPublicKey) pkey).getParams().getCurve().getField().getFieldSize(); } else if (pkey instanceof RSAPublicKey) { return ((RSAPublicKey) pkey).getModulus().bitLength(); } else { throw new IllegalArgumentException( "Unsupported public key type: " + pkey.getClass().getName()); } }
public JCEECPublicKey( ECPublicKey key) { this.algorithm = key.getAlgorithm(); this.ecSpec = key.getParams(); this.q = EC5Util.convertPoint(this.ecSpec, key.getW(), false); }
public static byte[] encode(final ECPublicKey key) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); int bitLength = key.getW().getAffineX().bitLength(); String curveName = null; int qLen; if (bitLength <= 256) { curveName = "nistp256"; qLen = 65; } else if (bitLength <= 384) { curveName = "nistp384"; qLen = 97; } else if (bitLength <= 521) { curveName = "nistp521"; qLen = 133; } else { throw new CryptoException("ECDSA bit length unsupported: " + bitLength); } byte[] name = ("ecdsa-sha2-" + curveName).getBytes(StandardCharsets.US_ASCII); byte[] curve = curveName.getBytes(StandardCharsets.US_ASCII); writeArray(name, buf); writeArray(curve, buf); byte[] javaEncoding = key.getEncoded(); byte[] q = new byte[qLen]; System.arraycopy(javaEncoding, javaEncoding.length - qLen, q, 0, qLen); writeArray(q, buf); return buf.toByteArray(); }
@Override public ECPublicKey clonePublicKey(ECPublicKey key) throws GeneralSecurityException { if (!SecurityUtils.isECCSupported()) { throw new NoSuchProviderException("ECC not supported"); } if (key == null) { return null; } ECParameterSpec params = key.getParams(); if (params == null) { throw new InvalidKeyException("Missing parameters in key"); } return generatePublicKey(new ECPublicKeySpec(key.getW(), params)); }