private static int getElementSize(EllipticCurve curve) { int fieldSize = curve.getField().getFieldSize(); return (fieldSize + 7) / 8; }
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 scalarMult(ECPoint p, BigInteger kin, EllipticCurve curve) { ECPoint r = ECPoint.POINT_INFINITY; BigInteger prime = ((ECFieldFp) curve.getField()).getP(); BigInteger k = kin.mod(prime); int length = k.bitLength(); byte[] binarray = new byte[length]; for (int i = 0; i <= length-1; i++) { binarray[i] = k.mod(TWO).byteValue(); k = k.divide(TWO); } for (int i = length-1; i >= 0; i--) { // i should start at length-1 not -2 because the MSB of binarry may not be 1 r = doublePoint(r, curve); if (binarray[i] == 1) r = addPoint(r, p, curve); } return r; }
private static ECPoint doublePoint(ECPoint r, EllipticCurve curve) { if (r.equals(ECPoint.POINT_INFINITY)) return r; BigInteger slope = (r.getAffineX().pow(2)).multiply(THREE); slope = slope.add(curve.getA()); BigInteger prime = ((ECFieldFp) curve.getField()).getP(); // use NBI modInverse(); BigInteger tmp = r.getAffineY().multiply(TWO); tmp = new NativeBigInteger(tmp); slope = slope.multiply(tmp.modInverse(prime)); BigInteger xOut = slope.pow(2).subtract(r.getAffineX().multiply(TWO)).mod(prime); BigInteger yOut = (r.getAffineY().negate()).add(slope.multiply(r.getAffineX().subtract(xOut))).mod(prime); ECPoint out = new ECPoint(xOut, yOut); return out; }
private static ECPoint addPoint(ECPoint r, ECPoint s, EllipticCurve curve) { if (r.equals(s)) return doublePoint(r, curve); else if (r.equals(ECPoint.POINT_INFINITY)) return s; else if (s.equals(ECPoint.POINT_INFINITY)) return r; BigInteger prime = ((ECFieldFp) curve.getField()).getP(); // use NBI modInverse(); BigInteger tmp = r.getAffineX().subtract(s.getAffineX()); tmp = new NativeBigInteger(tmp); BigInteger slope = (r.getAffineY().subtract(s.getAffineY())).multiply(tmp.modInverse(prime)).mod(prime); slope = new NativeBigInteger(slope); BigInteger xOut = (slope.modPow(TWO, prime).subtract(r.getAffineX())).subtract(s.getAffineX()).mod(prime); BigInteger yOut = s.getAffineY().negate().mod(prime); yOut = yOut.add(slope.multiply(s.getAffineX().subtract(xOut))).mod(prime); ECPoint out = new ECPoint(xOut, yOut); return out; }
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)); }
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(); }
/** * @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(); }
/** * @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()); } }
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 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; }
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); }
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; } }
/** * Creates a new Elliptic Curve JWK builder. * * @param crv The cryptographic curve. Must not be * {@code null}. * @param pub The public EC key to represent. Must not be * {@code null}. */ public Builder(final Curve crv, final ECPublicKey pub) { this(crv, encodeCoordinate(pub.getParams().getCurve().getField().getFieldSize(), pub.getW().getAffineX()), encodeCoordinate(pub.getParams().getCurve().getField().getFieldSize(), pub.getW().getAffineY())); }