use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.
the class ECIESKeyEncapsulation method encrypt.
/**
* Generate and encapsulate a random session key.
*
* @param out the output buffer for the encapsulated key.
* @param outOff the offset for the output buffer.
* @param keyLen the length of the session key.
* @return the random session key.
*/
public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException {
if (!(key instanceof ECPublicKeyParameters)) {
throw new IllegalArgumentException("Public key required for encryption");
}
ECPublicKeyParameters ecPubKey = (ECPublicKeyParameters) key;
ECDomainParameters ecParams = ecPubKey.getParameters();
ECCurve curve = ecParams.getCurve();
BigInteger n = ecParams.getN();
BigInteger h = ecParams.getH();
// Generate the ephemeral key pair
BigInteger r = BigIntegers.createRandomInRange(ONE, n, rnd);
// Compute the static-ephemeral key agreement
BigInteger rPrime = OldCofactorMode ? r.multiply(h).mod(n) : r;
ECMultiplier basePointMultiplier = createBasePointMultiplier();
ECPoint[] ghTilde = new ECPoint[] { basePointMultiplier.multiply(ecParams.getG(), r), ecPubKey.getQ().multiply(rPrime) };
// NOTE: More efficient than normalizing each individually
curve.normalizeAll(ghTilde);
ECPoint gTilde = ghTilde[0], hTilde = ghTilde[1];
// Encode the ephemeral public key
byte[] C = gTilde.getEncoded(false);
System.arraycopy(C, 0, out, outOff, C.length);
// Encode the shared secret value
byte[] PEH = hTilde.getAffineXCoord().getEncoded();
return deriveKey(keyLen, C, PEH);
}
use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.
the class ECElGamalEncryptor method encrypt.
/**
* Process a single EC point using the basic ElGamal algorithm.
*
* @param point the EC point to process.
* @return the result of the Elgamal process.
*/
public ECPair encrypt(ECPoint point) {
if (key == null) {
throw new IllegalStateException("ECElGamalEncryptor not initialised");
}
ECDomainParameters ec = key.getParameters();
BigInteger k = ECUtil.generateK(ec.getN(), random);
ECMultiplier basePointMultiplier = createBasePointMultiplier();
ECPoint[] gamma_phi = new ECPoint[] { basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), point)) };
ec.getCurve().normalizeAll(gamma_phi);
return new ECPair(gamma_phi[0], gamma_phi[1]);
}
use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.
the class ECNewPublicKeyTransform method transform.
/**
* Transform an existing cipher text pair using the ElGamal algorithm. Note: the input cipherText
* will need to be preserved in order to complete the transformation to the new public key.
*
* @param cipherText the EC point to process.
* @return returns a new ECPair representing the result of the process.
*/
public ECPair transform(ECPair cipherText) {
if (key == null) {
throw new IllegalStateException("ECNewPublicKeyTransform not initialised");
}
ECDomainParameters ec = key.getParameters();
BigInteger n = ec.getN();
ECMultiplier basePointMultiplier = createBasePointMultiplier();
BigInteger k = ECUtil.generateK(n, random);
ECPoint[] gamma_phi = new ECPoint[] { basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) };
ec.getCurve().normalizeAll(gamma_phi);
return new ECPair(gamma_phi[0], gamma_phi[1]);
}
use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.
the class SM2Engine method encryptGm.
// todo 实现SM2Cipher
private byte[] encryptGm(byte[] in, int inOff, int inLen) throws IOException {
byte[] cipher = new byte[inLen];
System.arraycopy(in, inOff, cipher, 0, cipher.length);
ECMultiplier multiplier = createBasePointMultiplier();
ECPoint kPB;
BigInteger x, y;
do {
BigInteger k = nextK();
ECPoint c1P = multiplier.multiply(ecParams.getG(), k).normalize();
// x , y
x = c1P.getAffineXCoord().toBigInteger();
y = c1P.getAffineYCoord().toBigInteger();
kPB = ((ECPublicKeyParameters) ecKey).getQ().multiply(k).normalize();
kdf(digest, kPB, cipher);
} while (notEncrypted(cipher, in, inOff));
byte[] hash = new byte[digest.getDigestSize()];
addFieldElement(digest, kPB.getAffineXCoord());
digest.update(in, inOff, inLen);
addFieldElement(digest, kPB.getAffineYCoord());
digest.doFinal(hash, 0);
return new Sm2Cipher(x, y, hash, cipher).setMode(mode).getEncoded(ASN1Encoding.DER);
}
use of com.github.zhenwei.core.math.ec.ECMultiplier in project LinLong-Java by zhenwei1108.
the class DSTU4145Signer method generateSignature.
public BigInteger[] generateSignature(byte[] message) {
ECDomainParameters ec = key.getParameters();
ECCurve curve = ec.getCurve();
ECFieldElement h = hash2FieldElement(curve, message);
if (h.isZero()) {
h = curve.fromBigInteger(ONE);
}
BigInteger n = ec.getN();
BigInteger e, r, s;
ECFieldElement Fe, y;
BigInteger d = ((ECPrivateKeyParameters) key).getD();
ECMultiplier basePointMultiplier = createBasePointMultiplier();
do {
do {
do {
e = generateRandomInteger(n, random);
Fe = basePointMultiplier.multiply(ec.getG(), e).normalize().getAffineXCoord();
} while (Fe.isZero());
y = h.multiply(Fe);
r = fieldElement2Integer(n, y);
} while (r.signum() == 0);
s = r.multiply(d).add(e).mod(n);
} while (s.signum() == 0);
return new BigInteger[] { r, s };
}
Aggregations