/** * SECG 2.3.3 ECPoint to Octet String */ public static byte[] getEncoded(ECPoint point, EllipticCurve curve) { int elementSize = getElementSize(curve); byte[] M = new byte[2 * elementSize + 1]; M[0] = 0x04; byte[] xBytes = stripLeadingZeroes(point.getAffineX().toByteArray()); byte[] yBytes = stripLeadingZeroes(point.getAffineY().toByteArray()); System.arraycopy(xBytes, 0, M, 1 + elementSize - xBytes.length, xBytes.length); System.arraycopy(yBytes, 0, M, 1 + 2 * elementSize - yBytes.length, yBytes.length); return M; }
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); }
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()); } }
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 SigningPublicKey fromJavaKey(ECPublicKey pk, SigType type) throws GeneralSecurityException { ECPoint w = pk.getW(); BigInteger x = w.getAffineX(); BigInteger y = w.getAffineY(); int len = type.getPubkeyLen(); byte[] b = combine(x, y, len); return new SigningPublicKey(type, b); }
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!"); } } }
private static org.spongycastle.math.ec.ECPoint convertPoint( ECCurve curve, java.security.spec.ECPoint point) { return curve.createPoint(point.getAffineX(), point.getAffineY()); }
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; }
public static byte[] getUncompressedEncodedECPoint(ECPoint point, int orderBitLength) { int orderByteLength = (orderBitLength + 7) / 8; byte[] keyData = new byte[1 + orderByteLength * 2]; keyData[0] = 4; unsignedByteArrayCopy(keyData, 1, orderByteLength, point.getAffineX()); unsignedByteArrayCopy(keyData, 1 + orderByteLength, orderByteLength, point.getAffineY()); return keyData; }
public static OpenSSLECPointContext getInstance(int curveType, OpenSSLECGroupContext group, ECPoint javaPoint) { OpenSSLECPointContext point = new OpenSSLECPointContext(group, NativeCrypto.EC_POINT_new(group.getContext())); NativeCrypto.EC_POINT_set_affine_coordinates(group.getContext(), point.getContext(), javaPoint.getAffineX().toByteArray(), javaPoint.getAffineY().toByteArray()); return point; } }
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); }
public static JsonWebKey fromECPublicKey(ECPublicKey pk, String curve, String kid) { JsonWebKey jwk = prepareECJwk(curve, kid); jwk.setProperty(JsonWebKey.EC_X_COORDINATE, Base64UrlUtility.encode(pk.getW().getAffineX().toByteArray())); jwk.setProperty(JsonWebKey.EC_Y_COORDINATE, Base64UrlUtility.encode(pk.getW().getAffineY().toByteArray())); return jwk; } public static JsonWebKey fromECPrivateKey(ECPrivateKey pk, String curve) {
public static com.bugvm.bouncycastle.math.ec.ECPoint convertPoint( ECCurve curve, ECPoint point, boolean withCompression) { return curve.createPoint(point.getAffineX(), point.getAffineY(), withCompression); } }
public static void assertECPointEquals(String message, ECPoint expected, ECPoint actual) { if (expected == actual) { return; } assertEquals(message + "[x]", expected.getAffineX(), actual.getAffineX()); assertEquals(message + "[y]", expected.getAffineY(), actual.getAffineY()); }
public static org.spongycastle.math.ec.ECPoint convertPoint( ECCurve curve, ECPoint point, boolean withCompression) { return curve.createPoint(point.getAffineX(), point.getAffineY()); } }
public static org.bouncycastle.math.ec.ECPoint convertPoint( ECCurve curve, ECPoint point, boolean withCompression) { return curve.createPoint(point.getAffineX(), point.getAffineY()); }
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()); }