private static int getElementSize(EllipticCurve curve) { int fieldSize = curve.getField().getFieldSize(); return (fieldSize + 7) / 8; }
int fieldSizeInBits = this.field.getFieldSize(); if (!(this.a.bitLength() <= fieldSizeInBits)) { throw new IllegalArgumentException("the a is not in the field");
private static int fieldSizeFromKey(ECKey ecPublicKey) { return ecPublicKey.getParams().getCurve().getField().getFieldSize(); } }
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()); } }
public static ECPoint decodePoint(byte[] encodedBytes, EllipticCurve elliptiCcurve) { if (encodedBytes[0] != 0x04) { throw new IllegalArgumentException("Only uncompressed format is supported"); } int size = (elliptiCcurve.getField().getFieldSize() + 7) / 8; byte affineXBytes[] = new byte[size]; byte affineYBytes[] = new byte[size]; System.arraycopy(encodedBytes, 1, affineXBytes, 0, size); System.arraycopy(encodedBytes, size + 1, affineYBytes, 0, size); return new ECPoint(new BigInteger(1, affineXBytes), new BigInteger(1, affineYBytes)); }
private static ECPoint decodePoint(byte[] encodedPoint, EllipticCurve curve) { int elementSize = (curve.getField().getFieldSize() + 7) / 8; if (encodedPoint.length != 2 * elementSize + 1 || encodedPoint[0] != 0x04 || encodedPoint.length == 0) { return null; } byte[] xPoint = new byte[elementSize]; System.arraycopy(encodedPoint, 1, xPoint, 0, elementSize); byte[] yPoint = new byte[elementSize]; System.arraycopy(encodedPoint, 1 + elementSize, yPoint, 0, elementSize); return new ECPoint(new BigInteger(1, xPoint), new BigInteger(1, yPoint)); }
public static ECPoint fromByteArray(byte[] b, EllipticCurve curve) { int len = (curve.getField().getFieldSize() + 7) / 8; if (b.length != 2 * len + 1 || b[0] != 4) return null; byte[] x = new byte[len]; byte[] y = new byte[len]; System.arraycopy(b, 1, x, 0, len); System.arraycopy(b, len + 1, y, 0, len); return new ECPoint(new BigInteger(1,x), new BigInteger(1,y)); }
/** * @param params The curve's {@link ECParameterSpec} * @return The curve's key size in bits * @throws IllegalArgumentException if invalid parameters provided */ public static int getCurveSize(ECParameterSpec params) { EllipticCurve curve = Objects.requireNonNull(params, "No EC params").getCurve(); ECField field = Objects.requireNonNull(curve, "No EC curve").getField(); return Objects.requireNonNull(field, "No EC field").getFieldSize(); }
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 static void assertECFieldEquals(String message, ECField expected, ECField actual) { if (expected == actual) { return; } assertEquals(message + "[size]", expected.getFieldSize(), actual.getFieldSize()); }
public static void assertECFieldEquals(String message, ECField expected, ECField actual) { if (expected == actual) { return; } assertEquals(message + "[size]", expected.getFieldSize(), actual.getFieldSize()); }
public static void assertECFieldEquals(String message, ECField expected, ECField actual) { if (expected == actual) { return; } assertEquals(message + "[size]", expected.getFieldSize(), actual.getFieldSize()); }
public static byte[] encodeECPoint(ECPoint group, EllipticCurve curve) { // M has len 2 ceil(log_2(q)/8) + 1 ? int elementSize = (curve.getField().getFieldSize() + 7) / 8; byte[] m = new byte[2 * elementSize + 1]; // Uncompressed format m[0] = 0x04; byte[] affineX = removeLeadingZeroes(group.getAffineX().toByteArray()); System.arraycopy(affineX, 0, m, 1 + elementSize - affineX.length, affineX.length); byte[] affineY = removeLeadingZeroes(group.getAffineY().toByteArray()); System.arraycopy(affineY, 0, m, 1 + elementSize + elementSize - affineY.length, affineY.length); return m; }
public static byte[] encodePoint(ECPoint ecPoint, EllipticCurve ellipticCurve) { int size = (ellipticCurve.getField().getFieldSize() + 7) / 8; byte affineXBytes[] = stripLeadingZeros(ecPoint.getAffineX().toByteArray()); byte affineYBytes[] = stripLeadingZeros(ecPoint.getAffineY().toByteArray()); byte encodedBytes[] = new byte[size * 2 + 1]; encodedBytes[0] = 0x04; //uncompressed System.arraycopy(affineXBytes, 0, encodedBytes, size - affineXBytes.length + 1, affineXBytes.length); System.arraycopy(affineYBytes, 0, encodedBytes, encodedBytes.length - affineYBytes.length, affineYBytes.length); return encodedBytes; }
public static byte[] encodeECPoint(ECPoint group, EllipticCurve curve) { // M has len 2 ceil(log_2(q)/8) + 1 ? int elementSize = (curve.getField().getFieldSize() + 7) / 8; byte[] m = new byte[2 * elementSize + 1]; // Uncompressed format m[0] = 0x04; byte[] affineX = removeLeadingZeroes(group.getAffineX().toByteArray()); System.arraycopy(affineX, 0, m, 1 + elementSize - affineX.length, affineX.length); byte[] affineY = removeLeadingZeroes(group.getAffineY().toByteArray()); System.arraycopy(affineY, 0, m, 1 + elementSize + elementSize - affineY.length, affineY.length); return m; }
@Override public int size() { ECParameterSpec ecParameterSpec = crv.toECParameterSpec(); if (ecParameterSpec == null) { throw new UnsupportedOperationException("Couldn't determine field size for curve " + crv.getName()); } return ecParameterSpec.getCurve().getField().getFieldSize(); }
private int getCoordinateByteLength() { ECParameterSpec spec = EllipticCurves.getSpec(getCurveName()); return (int) Math.ceil(spec.getCurve().getField().getFieldSize() / 8d); }
@Override public boolean supportsKey(PrivateKey originalKey) { if (!(originalKey instanceof ECPrivateKey)) { return false; } ECPrivateKey key = (ECPrivateKey) originalKey; return super.supportsKey(key) && key.getParams().getCurve().getField().getFieldSize() == getEcParameterSpec().getCurve().getField().getFieldSize(); }
private static boolean matchCurve(ECParameterSpec params, Curve curve) { int fieldSize = params.getCurve().getField().getFieldSize(); if (curve.getCurve().getField().getFieldSize() == fieldSize && curve.getCurve().equals(params.getCurve()) && curve.getGenerator().equals(params.getGenerator()) && curve.getOrder().equals(params.getOrder()) && curve.getCofactor() == params.getCofactor()) { return true; } else { return false; } }
public JWK ec(Key key) { ECPublicKey ecKey = (ECPublicKey) key; ECPublicJWK k = new ECPublicJWK(); String kid = this.kid != null ? this.kid : KeyUtils.createKeyId(key); k.setKeyId(kid); k.setKeyType(KeyType.EC); k.setAlgorithm(algorithm); k.setPublicKeyUse(DEFAULT_PUBLIC_KEY_USE); k.setCrv("P-" + ecKey.getParams().getCurve().getField().getFieldSize()); k.setX(Base64Url.encode(ecKey.getW().getAffineX().toByteArray())); k.setY(Base64Url.encode(ecKey.getW().getAffineY().toByteArray())); return k; }