Search in sources :

Example 6 with ECMultiplier

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);
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Example 7 with ECMultiplier

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]);
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Example 8 with ECMultiplier

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]);
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Example 9 with ECMultiplier

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);
}
Also used : BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) Sm2Cipher(com.github.zhenwei.core.asn1.pkcs.Sm2Cipher) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Example 10 with ECMultiplier

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 };
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement)

Aggregations

ECMultiplier (com.github.zhenwei.core.math.ec.ECMultiplier)12 BigInteger (java.math.BigInteger)12 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)11 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)9 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)5 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)2 Sm2Cipher (com.github.zhenwei.core.asn1.pkcs.Sm2Cipher)1 CryptoException (com.github.zhenwei.core.crypto.CryptoException)1 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)1 ECFieldElement (com.github.zhenwei.core.math.ec.ECFieldElement)1