java.security.spec.EllipticCurve类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(13.1k)|赞(0)|评价(0)|浏览(171)

本文整理了Java中java.security.spec.EllipticCurve类的一些代码示例,展示了EllipticCurve类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EllipticCurve类的具体详情如下:
包路径:java.security.spec.EllipticCurve
类名称:EllipticCurve

EllipticCurve介绍

[英]An Elliptic Curve with its necessary values.
[中]具有必要值的椭圆曲线。

代码示例

代码示例来源:origin: i2p/i2p.i2p

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;
}

代码示例来源:origin: SeanDragon/protools

BigInteger p = new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839");
ECFieldFp ecFieldFp = new ECFieldFp(p);
BigInteger a = new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16);
BigInteger b = new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16);
EllipticCurve ellipticCurve = new EllipticCurve(ecFieldFp, a, b);
BigInteger x = new BigInteger("110282003749548856476348533541186204577905061504881242240149511594420911");
BigInteger y = new BigInteger("869078407435509378747351873793058868500210384946040694651368759217025454");
ECPoint g = new ECPoint(x, y);
BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307");
ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, g, n, 1);

代码示例来源:origin: ibinti/bugvm

public ECParameterSpec getECParameterSpec() {
    final String curveName = NativeCrypto.EC_GROUP_get_curve_name(groupCtx);

    final byte[][] curveParams = NativeCrypto.EC_GROUP_get_curve(groupCtx);
    final BigInteger p = new BigInteger(curveParams[0]);
    final BigInteger a = new BigInteger(curveParams[1]);
    final BigInteger b = new BigInteger(curveParams[2]);

    final ECField field;
    final int type = NativeCrypto.get_EC_GROUP_type(groupCtx);
    if (type == NativeCrypto.EC_CURVE_GFP) {
      field = new ECFieldFp(p);
    } else if (type == NativeCrypto.EC_CURVE_GF2M) {
      field = new ECFieldF2m(p.bitLength() - 1, p);
    } else {
      throw new RuntimeException("unknown curve type " + type);
    }

    final EllipticCurve curve = new EllipticCurve(field, a, b);

    final OpenSSLECPointContext generatorCtx = new OpenSSLECPointContext(this,
        NativeCrypto.EC_GROUP_get_generator(groupCtx));
    final ECPoint generator = generatorCtx.getECPoint();

    final BigInteger order = new BigInteger(NativeCrypto.EC_GROUP_get_order(groupCtx));
    final BigInteger cofactor = new BigInteger(NativeCrypto.EC_GROUP_get_cofactor(groupCtx));

    return new ECParameterSpec(curve, generator, order, cofactor.intValue(), curveName);
  }
}

代码示例来源:origin: org.xbib/jsch-core

public boolean validate(byte[] r, byte[] s) throws Exception {
  BigInteger x = new BigInteger(1, r);
  BigInteger y = new BigInteger(1, s);
  ECPoint w = new ECPoint(x, y);
  if (w.equals(ECPoint.POINT_INFINITY)) {
    return false;
  }
  ECParameterSpec params = publicKey.getParams();
  EllipticCurve curve = params.getCurve();
  BigInteger p = ((ECFieldFp) curve.getField()).getP();
  BigInteger p_sub1 = p.subtract(BigInteger.ONE);
  if (!(x.compareTo(p_sub1) <= 0 && y.compareTo(p_sub1) <= 0)) {
    return false;
  }
  BigInteger tmp = x.multiply(curve.getA()).
      add(curve.getB()).
      add(x.modPow(three, p)).
      mod(p);
  BigInteger y_2 = y.modPow(two, p);
  return y_2.equals(tmp);
}

代码示例来源:origin: ibinti/bugvm

public static com.bugvm.bouncycastle.jce.spec.ECParameterSpec convertSpec(
  ECParameterSpec ecSpec,
  boolean withCompression)
{
  ECCurve curve = convertCurve(ecSpec.getCurve());
  return new com.bugvm.bouncycastle.jce.spec.ECParameterSpec(
    curve,
    convertPoint(curve, ecSpec.getGenerator(), withCompression),
    ecSpec.getOrder(),
    BigInteger.valueOf(ecSpec.getCofactor()),
    ecSpec.getCurve().getSeed());
}

代码示例来源:origin: ibinti/bugvm

public static OpenSSLECGroupContext getInstance(ECParameterSpec params)
    throws InvalidAlgorithmParameterException {
  final String curveName = params.getCurveName();
  if (curveName != null) {
    return OpenSSLECGroupContext.getCurveByName(curveName);
  }
  final EllipticCurve curve = params.getCurve();
  final ECField field = curve.getField();
  final int type;
  final BigInteger p;
  if (field instanceof ECFieldFp) {
    type = NativeCrypto.EC_CURVE_GFP;
    p = ((ECFieldFp) field).getP();
  } else if (field instanceof ECFieldF2m) {
    type = NativeCrypto.EC_CURVE_GF2M;
    p = ((ECFieldF2m) field).getReductionPolynomial();
  } else {
    throw new InvalidParameterException("unhandled field class "
        + field.getClass().getName());
  }
  final ECPoint generator = params.getGenerator();
  return OpenSSLECGroupContext.getInstance(type, p, curve.getA(), curve.getB(),
      generator.getAffineX(), generator.getAffineY(), params.getOrder(),
      BigInteger.valueOf(params.getCofactor()));
}

代码示例来源:origin: com.nimbusds/nimbus-jose-jwt

/**
 * Checks if the specified (ephemeral) public key is on the given
 * curve. Intended to prevent an "Invalid Curve Attack", independent
 * from any JCA provider checks (the SUN provider in Java 1.8.0_51+ and
 * BouncyCastle have them, other / older provider do not).
 *
 * <p>See https://www.cs.bris.ac.uk/Research/CryptographySecurity/RWC/2017/nguyen.quan.pdf
 *
 * @param x               The public EC x coordinate. Must not be
 *                        {@code null}.
 * @param y               The public EC y coordinate. Must not be
 *                        {@code null}.
 * @param ecParameterSpec The EC spec. Must not be {@code null}.
 *
 * @return {@code true} if public key passed the curve check.
 */
public static boolean isPointOnCurve(final BigInteger x, final BigInteger y, final ECParameterSpec ecParameterSpec) {
  
  // Ensure the following condition is met:
  // (y^2) mod p = (x^3 + ax + b) mod p
  EllipticCurve curve = ecParameterSpec.getCurve();
  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);
  
  return leftSide.equals(rightSide);
}

代码示例来源:origin: com.madgag.spongycastle/pkix

private static ECCurve convertCurve(
  EllipticCurve ec, BigInteger order, int coFactor)
{
  ECField field = ec.getField();
  BigInteger a = ec.getA();
  BigInteger b = ec.getB();
  if (field instanceof ECFieldFp)
  {
    return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b, order, BigInteger.valueOf(coFactor));
  }
  else
  {
    throw new IllegalStateException("not implemented yet!!!");
  }
}

代码示例来源:origin: org.apache.santuario/xmlsec

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));
}

代码示例来源:origin: wildfly/wildfly

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());
  }
}

代码示例来源:origin: com.google.crypto.tink/tink

/** 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());
 }
}

代码示例来源:origin: es.gob.afirma.jmulticard/jmulticard

private static ECCurve toSpongyCastleECCurve(final ECParameterSpec params) {
  final EllipticCurve curve = params.getCurve();
  final ECField field = curve.getField();
  if (!(field instanceof ECFieldFp)) {
    throw new IllegalArgumentException(
      "Solo se soporta 'ECFieldFp' y se proporciono  " + field.getClass().getCanonicalName() //$NON-NLS-1$
    );
  }
  final int coFactor = params.getCofactor();
  final BigInteger order = params.getOrder();
  final BigInteger a = curve.getA();
  final BigInteger b = curve.getB();
  final BigInteger p = getPrime(params);
  return new ECCurve.Fp(p, a, b, order, BigInteger.valueOf(coFactor));
}

代码示例来源:origin: i2p/i2p.i2p

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;
}

代码示例来源:origin: i2p/i2p.i2p

public ECParameterSpec genSpec() {
    BigInteger pb = new NativeBigInteger(ps);
    BigInteger nb = new NativeBigInteger(ns);
    BigInteger sb = new NativeBigInteger(ss.replace(" ", ""), 16);
    BigInteger bb = new NativeBigInteger(bs.replace(" ", ""), 16);
    BigInteger gxb = new NativeBigInteger(gxs.replace(" ", ""), 16);
    BigInteger gyb = new NativeBigInteger(gys.replace(" ", ""), 16);
    BigInteger ab = new NativeBigInteger(A.mod(pb));
    ECField field = new ECFieldFp(pb);
    EllipticCurve curve = new EllipticCurve(field, ab, bb, sb.toByteArray());
    ECPoint g = new ECPoint(gxb, gyb);
    return new ECParameterSpec(curve, g, nb, H);
  }
}

代码示例来源:origin: org.apache.santuario/xmlsec

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;
  }
}

代码示例来源:origin: org.conscrypt/conscrypt-openjdk-uber

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());
  }
}

代码示例来源:origin: eclipse/californium

private SupportedGroup(int code, int type, String p, String a, String b, String x, String y,
    String n, int h) {
  this(code);
  BigInteger bip = bi(p);
  ECField field;
  switch(type) {
  case(PRIME):
    field = new ECFieldFp(bip);
    break;
  case(BINARY):
    field = new ECFieldF2m(bip.bitLength() - 1, bip);
    break;
  default:
    throw new RuntimeException("Cannot handle supported groups of type " + type);
  }
  EllipticCurve curve = new EllipticCurve(field, bi(a), bi(b));
  ECPoint g = new ECPoint(bi(x), bi(y));
  this.params = new ECParameterSpec(curve, g, bi(n), h);
  try {
    KeyPairGenerator gen = KeyPairGenerator.getInstance(KEYPAIR_GENERATOR_ALGORITHM);
    gen.initialize(new ECGenParameterSpec(name()));
    usable = true;
  } catch (GeneralSecurityException e) {
    LOGGER.log(Level.FINE, "Group [{0}] is not supported by JRE", name());
  }
}

代码示例来源:origin: org.xipki/security

public static boolean isSm2primev2Curve(EllipticCurve curve) {
 return curve.getB().equals(sm2primev2CurveA);
}

代码示例来源:origin: hierynomus/sshj

private static int fieldSizeFromKey(ECKey ecPublicKey) {
    return ecPublicKey.getParams().getCurve().getField().getFieldSize();
  }
}

代码示例来源:origin: es.gob.afirma.jmulticard/jmulticard

private static ECParameterSpec mapNonceGMWithECDH(final BigInteger nonceS,
                         final ECPoint sharedSecretPointH,
                         final ECParameterSpec params) {
  // D~ = (p, a, b, G~, n, h) where G~ = [s]G + H
  final ECPoint generator = params.getGenerator();
  final EllipticCurve curve = params.getCurve();
  final BigInteger a = curve.getA();
  final BigInteger b = curve.getB();
  final ECFieldFp field = (ECFieldFp)curve.getField();
  final BigInteger p = field.getP();
  final BigInteger order = params.getOrder();
  final int cofactor = params.getCofactor();
  final ECPoint ephemeralGenerator = add(multiply(nonceS, generator, params), sharedSecretPointH, params);
  if (!toSpongyCastleECPoint(ephemeralGenerator, params).isValid()) {
    LOGGER.warning("Se ha generado un punto invalido"); //$NON-NLS-1$
  }
  return new ECParameterSpec(new EllipticCurve(new ECFieldFp(p), a, b), ephemeralGenerator, order, cofactor);
}

相关文章