public String toString() { StringBuffer buf = new StringBuffer(); String nl = Strings.lineSeparator(); buf.append("EC Public Key").append(nl); buf.append(" X: ").append(this.q.getAffineXCoord().toBigInteger().toString(16)).append(nl); buf.append(" Y: ").append(this.q.getAffineYCoord().toBigInteger().toString(16)).append(nl); return buf.toString(); }
public static String publicKeyToString(String algorithm, org.bouncycastle.math.ec.ECPoint q, org.bouncycastle.jce.spec.ECParameterSpec spec) { StringBuffer buf = new StringBuffer(); String nl = Strings.lineSeparator(); buf.append(algorithm); buf.append(" Public Key [").append(ECUtil.generateKeyFingerprint(q, spec)).append("]").append(nl); buf.append(" X: ").append(q.getAffineXCoord().toBigInteger().toString(16)).append(nl); buf.append(" Y: ").append(q.getAffineYCoord().toBigInteger().toString(16)).append(nl); return buf.toString(); }
public static String privateKeyToString(String algorithm, BigInteger d, org.bouncycastle.jce.spec.ECParameterSpec spec) { StringBuffer buf = new StringBuffer(); String nl = Strings.lineSeparator(); org.bouncycastle.math.ec.ECPoint q = calculateQ(d, spec); buf.append(algorithm); buf.append(" Private Key [").append(ECUtil.generateKeyFingerprint(q, spec)).append("]").append(nl); buf.append(" X: ").append(q.getAffineXCoord().toBigInteger().toString(16)).append(nl); buf.append(" Y: ").append(q.getAffineYCoord().toBigInteger().toString(16)).append(nl); return buf.toString(); }
private boolean keysEqual(PublicKey serverKey, PublicKey clientKey) { if (serverKey != null && clientKey != null) { if (serverKey instanceof org.bouncycastle.jce.provider.JCEECPublicKey) { try { org.bouncycastle.jce.provider.JCEECPublicKey ecServerKey = (org.bouncycastle.jce.provider.JCEECPublicKey)serverKey; org.bouncycastle.math.ec.ECPoint serverKeyQ = ecServerKey.getQ(); BigInteger serverKeyX = serverKeyQ.getAffineXCoord().toBigInteger(); BigInteger serverKeyY = serverKeyQ.getAffineYCoord().toBigInteger(); BigInteger[] clientKeyPoint = clientKeyPointQ(clientKey); return serverKeyX.equals(clientKeyPoint[0]) && serverKeyY.equals(clientKeyPoint[1]); } catch (Exception e) { logger.error( "Could not get component of client key to compare with server key. Client key class: " + clientKey.getClass().getName(), e); } } else { return serverKey.equals(clientKey); } } return false; } private BigInteger[] clientKeyPointQ(PublicKey clientKey)
private byte[] S1(Digest digest, ECPoint u, byte[] inner) { digest.update((byte)0x02); addFieldElement(digest, u.getAffineYCoord()); digest.update(inner, 0, inner.length); return digestDoFinal(); }
private byte[] S2(Digest digest, ECPoint u, byte[] inner) { digest.update((byte)0x03); addFieldElement(digest, u.getAffineYCoord()); digest.update(inner, 0, inner.length); return digestDoFinal(); }
private static boolean isP256(ECParameterSpec params) { ECNamedCurveParameterSpec p256 = ECNamedCurveTable.getParameterSpec("P-256"); return (Objects.equals(p256.getN(), params.getOrder()) && Objects.equals(p256.getG().getAffineXCoord().toBigInteger(), params.getGenerator().getAffineX()) && Objects.equals(p256.getG().getAffineYCoord().toBigInteger(), params.getGenerator().getAffineY()) && Objects.equals(p256.getH(), BigInteger.valueOf(params.getCofactor())) ); }
public static ECPoint convertPoint(org.bouncycastle.math.ec.ECPoint point) { point = point.normalize(); return new ECPoint( point.getAffineXCoord().toBigInteger(), point.getAffineYCoord().toBigInteger()); } }
private byte[] calculateInnerHash(Digest digest, ECPoint u, byte[] za, byte[] zb, ECPoint p1, ECPoint p2) { addFieldElement(digest, u.getAffineXCoord()); digest.update(za, 0, za.length); digest.update(zb, 0, zb.length); addFieldElement(digest, p1.getAffineXCoord()); addFieldElement(digest, p1.getAffineYCoord()); addFieldElement(digest, p2.getAffineXCoord()); addFieldElement(digest, p2.getAffineYCoord()); return digestDoFinal(); }
private byte[] getZ(Digest digest, byte[] userID, ECPoint pubPoint) { addUserID(digest, userID); addFieldElement(digest, ecParams.getCurve().getA()); addFieldElement(digest, ecParams.getCurve().getB()); addFieldElement(digest, ecParams.getG().getAffineXCoord()); addFieldElement(digest, ecParams.getG().getAffineYCoord()); addFieldElement(digest, pubPoint.getAffineXCoord()); addFieldElement(digest, pubPoint.getAffineYCoord()); return digestDoFinal(); }
public byte[] generateSignatureForMessage(byte[] userId, byte[] message) throws CryptoException { // CHECKSTYLE:SKIP byte[] z; if (userId == null) { // use default userId z = GMUtil.getSM2Z(GMObjectIdentifiers.sm2p256v1, pubPoint.getAffineXCoord().toBigInteger(), pubPoint.getAffineYCoord().toBigInteger()); } else { z = GMUtil.getSM2Z(userId, GMObjectIdentifiers.sm2p256v1, pubPoint.getAffineXCoord().toBigInteger(), pubPoint.getAffineYCoord().toBigInteger()); } digest.reset(); digest.update(z, 0, z.length); digest.update(message, 0, message.length); byte[] hash = new byte[digest.getDigestSize()]; digest.doFinal(hash, 0); return generateSignatureForHash(hash); }
private byte[] getZ(byte[] userID) { digest.reset(); addUserID(digest, userID); addFieldElement(digest, ecParams.getCurve().getA()); addFieldElement(digest, ecParams.getCurve().getB()); addFieldElement(digest, ecParams.getG().getAffineXCoord()); addFieldElement(digest, ecParams.getG().getAffineYCoord()); addFieldElement(digest, pubPoint.getAffineXCoord()); addFieldElement(digest, pubPoint.getAffineYCoord()); byte[] result = new byte[digest.getDigestSize()]; digest.doFinal(result, 0); return result; }
public static byte[] encodePoint(ECPoint Q) { /*if (!Q.isCompressed()) Q=new ECPoint.F2m(Q.getCurve(),Q.getX(),Q.getY(),true); byte[] bytes=Q.getEncoded(); if (bytes[0]==0x02) bytes[bytes.length-1]&=0xFE; else if (bytes[0]==0x02) bytes[bytes.length-1]|=0x01; return Arrays.copyOfRange(bytes, 1, bytes.length);*/ Q = Q.normalize(); ECFieldElement x = Q.getAffineXCoord(); byte[] bytes = x.getEncoded(); if (!x.isZero()) { ECFieldElement z = Q.getAffineYCoord().divide(x); if (trace(z).isOne()) { bytes[bytes.length - 1] |= 0x01; } else { bytes[bytes.length - 1] &= 0xFE; } } return bytes; }
private byte[] encrypt(byte[] in, int inOff, int inLen) throws InvalidCipherTextException { byte[] c2 = new byte[inLen]; System.arraycopy(in, inOff, c2, 0, c2.length); ECMultiplier multiplier = createBasePointMultiplier(); byte[] c1; ECPoint kPB; do { BigInteger k = nextK(); ECPoint c1P = multiplier.multiply(ecParams.getG(), k).normalize(); c1 = c1P.getEncoded(false); kPB = ((ECPublicKeyParameters)ecKey).getQ().multiply(k).normalize(); kdf(digest, kPB, c2); } while (notEncrypted(c2, in, inOff)); byte[] c3 = new byte[digest.getDigestSize()]; addFieldElement(digest, kPB.getAffineXCoord()); digest.update(in, inOff, inLen); addFieldElement(digest, kPB.getAffineYCoord()); digest.doFinal(c3, 0); return Arrays.concatenate(c1, c2, c3); }
public JCEECPublicKey( String algorithm, org.bouncycastle.jce.spec.ECPublicKeySpec spec) { this.algorithm = algorithm; this.q = spec.getQ(); if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { if (q.getCurve() == null) { org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa(); q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger(), false); } this.ecSpec = null; } }
public static byte[] getSM2Z(byte[] userID, ASN1ObjectIdentifier curveOid, BigInteger pubPointX, BigInteger pubPointY) { SM3Digest digest = new SM3Digest(); addUserId(digest, userID); X9ECParameters ecParams = GMNamedCurves.getByOID(curveOid); addFieldElement(digest, ecParams.getCurve().getA()); addFieldElement(digest, ecParams.getCurve().getB()); addFieldElement(digest, ecParams.getG().getAffineXCoord()); addFieldElement(digest, ecParams.getG().getAffineYCoord()); int fieldSize = (ecParams.getCurve().getFieldSize() + 7) / 8; byte[] bytes = BigIntegers.asUnsignedByteArray(fieldSize, pubPointX); digest.update(bytes, 0, fieldSize); bytes = BigIntegers.asUnsignedByteArray(fieldSize, pubPointY); digest.update(bytes, 0, fieldSize); byte[] result = new byte[digest.getDigestSize()]; digest.doFinal(result, 0); return result; }
public BCECGOST3410_2012PublicKey( org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); // this may seem a little long-winded but it's how we pick up the custom curve. this.ecPublicKey = new ECPublicKeyParameters( spec.getQ(), ECUtil.getDomainParameters(configuration, spec.getParams())); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); this.ecPublicKey = new ECPublicKeyParameters(s.getCurve().createPoint(spec.getQ().getAffineXCoord().toBigInteger(), spec.getQ().getAffineYCoord().toBigInteger()), EC5Util.getDomainParameters(configuration, (ECParameterSpec)null)); this.ecSpec = null; } }
public BCDSTU4145PublicKey( org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); // this may seem a little long-winded but it's how we pick up the custom curve. this.ecPublicKey = new ECPublicKeyParameters( spec.getQ(), ECUtil.getDomainParameters(configuration, spec.getParams())); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); this.ecPublicKey = new ECPublicKeyParameters(s.getCurve().createPoint(spec.getQ().getAffineXCoord().toBigInteger(), spec.getQ().getAffineYCoord().toBigInteger()), EC5Util.getDomainParameters(configuration, (ECParameterSpec)null)); this.ecSpec = null; } }
public BCECGOST3410PublicKey( org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); // this may seem a little long-winded but it's how we pick up the custom curve. this.ecPublicKey = new ECPublicKeyParameters( spec.getQ(), ECUtil.getDomainParameters(configuration, spec.getParams())); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); this.ecPublicKey = new ECPublicKeyParameters(s.getCurve().createPoint(spec.getQ().getAffineXCoord().toBigInteger(), spec.getQ().getAffineYCoord().toBigInteger()), EC5Util.getDomainParameters(configuration, (ECParameterSpec)null)); this.ecSpec = null; } }
public BCECPublicKey( String algorithm, org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { this.algorithm = algorithm; if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); // this may seem a little long-winded but it's how we pick up the custom curve. this.ecPublicKey = new ECPublicKeyParameters( spec.getQ(), ECUtil.getDomainParameters(configuration, spec.getParams())); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); this.ecPublicKey = new ECPublicKeyParameters(s.getCurve().createPoint(spec.getQ().getAffineXCoord().toBigInteger(), spec.getQ().getAffineYCoord().toBigInteger()), EC5Util.getDomainParameters(configuration, (ECParameterSpec)null)); this.ecSpec = null; } this.configuration = configuration; }