use of com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair in project LinLong-Java by zhenwei1108.
the class ECNRSigner method generateSignature.
// Section 7.2.5 ECSP-NR, pg 34
/**
* generate a signature for the given message using the key we were initialised with. Generally,
* the order of the curve should be at least as long as the hash of the message of interest, and
* with ECNR it *must* be at least as long.
*
* @param digest the digest to be signed.
* @throws DataLengthException if the digest is longer than the key allows
*/
public BigInteger[] generateSignature(byte[] digest) {
if (!this.forSigning) {
throw new IllegalStateException("not initialised for signing");
}
BigInteger n = getOrder();
BigInteger e = new BigInteger(1, digest);
ECPrivateKeyParameters privKey = (ECPrivateKeyParameters) key;
if (e.compareTo(n) >= 0) {
throw new DataLengthException("input too large for ECNR key");
}
BigInteger r = null;
BigInteger s = null;
AsymmetricCipherKeyPair tempPair;
do // generate r
{
// generate another, but very temporary, key pair using
// the same EC parameters
ECKeyPairGenerator keyGen = new ECKeyPairGenerator();
keyGen.init(new ECKeyGenerationParameters(privKey.getParameters(), this.random));
tempPair = keyGen.generateKeyPair();
// BigInteger Vx = tempPair.getPublic().getW().getAffineX();
// get temp's public key
ECPublicKeyParameters V = (ECPublicKeyParameters) tempPair.getPublic();
BigInteger Vx = V.getQ().getAffineXCoord().toBigInteger();
r = Vx.add(e).mod(n);
} while (r.equals(ECConstants.ZERO));
// generate s
// private key value
BigInteger x = privKey.getD();
// temp's private key value
BigInteger u = ((ECPrivateKeyParameters) tempPair.getPrivate()).getD();
s = u.subtract(r.multiply(x)).mod(n);
BigInteger[] res = new BigInteger[2];
res[0] = r;
res[1] = s;
return res;
}
use of com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair in project LinLong-Java by zhenwei1108.
the class McElieceKeyPairGenerator method genKeyPair.
private AsymmetricCipherKeyPair genKeyPair() {
if (!initialized) {
initializeDefault();
}
// finite field GF(2^m)
GF2mField field = new GF2mField(m, fieldPoly);
// irreducible Goppa polynomial
PolynomialGF2mSmallM gp = new PolynomialGF2mSmallM(field, t, PolynomialGF2mSmallM.RANDOM_IRREDUCIBLE_POLYNOMIAL, random);
PolynomialRingGF2m ring = new PolynomialRingGF2m(field, gp);
// matrix used to compute square roots in (GF(2^m))^t
PolynomialGF2mSmallM[] sqRootMatrix = ring.getSquareRootMatrix();
// generate canonical check matrix
GF2Matrix h = GoppaCode.createCanonicalCheckMatrix(field, gp);
// compute short systematic form of check matrix
MaMaPe mmp = GoppaCode.computeSystematicForm(h, random);
GF2Matrix shortH = mmp.getSecondMatrix();
Permutation p1 = mmp.getPermutation();
// compute short systematic form of generator matrix
GF2Matrix shortG = (GF2Matrix) shortH.computeTranspose();
// extend to full systematic form
GF2Matrix gPrime = shortG.extendLeftCompactForm();
// obtain number of rows of G (= dimension of the code)
int k = shortG.getNumRows();
// generate random invertible (k x k)-matrix S and its inverse S^-1
GF2Matrix[] matrixSandInverse = GF2Matrix.createRandomRegularMatrixAndItsInverse(k, random);
// generate random permutation P2
Permutation p2 = new Permutation(n, random);
// compute public matrix G=S*G'*P2
GF2Matrix g = (GF2Matrix) matrixSandInverse[0].rightMultiply(gPrime);
g = (GF2Matrix) g.rightMultiply(p2);
// generate keys
McEliecePublicKeyParameters pubKey = new McEliecePublicKeyParameters(n, t, g);
McEliecePrivateKeyParameters privKey = new McEliecePrivateKeyParameters(n, k, field, gp, p1, p2, matrixSandInverse[1]);
// return key pair
return new AsymmetricCipherKeyPair(pubKey, privKey);
}
use of com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair in project LinLong-Java by zhenwei1108.
the class GMSSKeyPairGenerator method genKeyPair.
/**
* Generates the GMSS key pair. The public key is an instance of JDKGMSSPublicKey, the private key
* is an instance of JDKGMSSPrivateKey.
*
* @return Key pair containing a JDKGMSSPublicKey and a JDKGMSSPrivateKey
*/
private AsymmetricCipherKeyPair genKeyPair() {
if (!initialized) {
initializeDefault();
}
// initialize authenticationPaths and treehash instances
byte[][][] currentAuthPaths = new byte[numLayer][][];
byte[][][] nextAuthPaths = new byte[numLayer - 1][][];
Treehash[][] currentTreehash = new Treehash[numLayer][];
Treehash[][] nextTreehash = new Treehash[numLayer - 1][];
Vector[] currentStack = new Vector[numLayer];
Vector[] nextStack = new Vector[numLayer - 1];
Vector[][] currentRetain = new Vector[numLayer][];
Vector[][] nextRetain = new Vector[numLayer - 1][];
for (int i = 0; i < numLayer; i++) {
currentAuthPaths[i] = new byte[heightOfTrees[i]][mdLength];
currentTreehash[i] = new Treehash[heightOfTrees[i] - K[i]];
if (i > 0) {
nextAuthPaths[i - 1] = new byte[heightOfTrees[i]][mdLength];
nextTreehash[i - 1] = new Treehash[heightOfTrees[i] - K[i]];
}
currentStack[i] = new Vector();
if (i > 0) {
nextStack[i - 1] = new Vector();
}
}
// initialize roots
byte[][] currentRoots = new byte[numLayer][mdLength];
byte[][] nextRoots = new byte[numLayer - 1][mdLength];
// initialize seeds
byte[][] seeds = new byte[numLayer][mdLength];
// layer
for (int i = 0; i < numLayer; i++) {
System.arraycopy(currentSeeds[i], 0, seeds[i], 0, mdLength);
}
// initialize rootSigs
currentRootSigs = new byte[numLayer - 1][mdLength];
// from bottom up to the root
for (int h = numLayer - 1; h >= 0; h--) {
GMSSRootCalc tree;
// the method with null as first parameter
if (h == numLayer - 1) {
tree = this.generateCurrentAuthpathAndRoot(null, currentStack[h], seeds[h], h);
} else // otherwise call the method with the former computed root
// value
{
tree = this.generateCurrentAuthpathAndRoot(currentRoots[h + 1], currentStack[h], seeds[h], h);
}
// set initial values needed for the private key construction
for (int i = 0; i < heightOfTrees[h]; i++) {
System.arraycopy(tree.getAuthPath()[i], 0, currentAuthPaths[h][i], 0, mdLength);
}
currentRetain[h] = tree.getRetain();
currentTreehash[h] = tree.getTreehash();
System.arraycopy(tree.getRoot(), 0, currentRoots[h], 0, mdLength);
}
// ------
for (int h = numLayer - 2; h >= 0; h--) {
GMSSRootCalc tree = this.generateNextAuthpathAndRoot(nextStack[h], seeds[h + 1], h + 1);
// set initial values needed for the private key construction
for (int i = 0; i < heightOfTrees[h + 1]; i++) {
System.arraycopy(tree.getAuthPath()[i], 0, nextAuthPaths[h][i], 0, mdLength);
}
nextRetain[h] = tree.getRetain();
nextTreehash[h] = tree.getTreehash();
System.arraycopy(tree.getRoot(), 0, nextRoots[h], 0, mdLength);
// create seed for the Merkle tree after next (nextNextSeeds)
// SEEDs++
System.arraycopy(seeds[h + 1], 0, this.nextNextSeeds[h], 0, mdLength);
}
// ------------
// generate JDKGMSSPublicKey
GMSSPublicKeyParameters publicKey = new GMSSPublicKeyParameters(currentRoots[0], gmssPS);
// generate the JDKGMSSPrivateKey
GMSSPrivateKeyParameters privateKey = new GMSSPrivateKeyParameters(currentSeeds, nextNextSeeds, currentAuthPaths, nextAuthPaths, currentTreehash, nextTreehash, currentStack, nextStack, currentRetain, nextRetain, nextRoots, currentRootSigs, gmssPS, digestProvider);
// return the KeyPair
return (new AsymmetricCipherKeyPair(publicKey, privateKey));
}
use of com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair in project LinLong-Java by zhenwei1108.
the class NTRUEncryptionKeyPairGenerator method generateKeyPair.
/**
* Generates a new encryption key pair.
*
* @return a key pair
*/
public AsymmetricCipherKeyPair generateKeyPair() {
int N = params.N;
int q = params.q;
int df = params.df;
int df1 = params.df1;
int df2 = params.df2;
int df3 = params.df3;
int dg = params.dg;
boolean fastFp = params.fastFp;
boolean sparse = params.sparse;
Polynomial t;
IntegerPolynomial fq;
IntegerPolynomial fp = null;
// choose a random f that is invertible mod 3 and q
while (true) {
IntegerPolynomial f;
// choose random t, calculate f and fp
if (fastFp) {
// if fastFp=true, f is always invertible mod 3
t = params.polyType == NTRUParameters.TERNARY_POLYNOMIAL_TYPE_SIMPLE ? Util.generateRandomTernary(N, df, df, sparse, params.getRandom()) : ProductFormPolynomial.generateRandom(N, df1, df2, df3, df3, params.getRandom());
f = t.toIntegerPolynomial();
f.mult(3);
f.coeffs[0] += 1;
} else {
t = params.polyType == NTRUParameters.TERNARY_POLYNOMIAL_TYPE_SIMPLE ? Util.generateRandomTernary(N, df, df - 1, sparse, params.getRandom()) : ProductFormPolynomial.generateRandom(N, df1, df2, df3, df3 - 1, params.getRandom());
f = t.toIntegerPolynomial();
fp = f.invertF3();
if (fp == null) {
continue;
}
}
fq = f.invertFq(q);
if (fq == null) {
continue;
}
break;
}
// if fastFp=true, fp=1
if (fastFp) {
fp = new IntegerPolynomial(N);
fp.coeffs[0] = 1;
}
// choose a random g that is invertible mod q
DenseTernaryPolynomial g;
while (true) {
g = DenseTernaryPolynomial.generateRandom(N, dg, dg - 1, params.getRandom());
if (g.invertFq(q) != null) {
break;
}
}
IntegerPolynomial h = g.mult(fq, q);
h.mult3(q);
h.ensurePositive(q);
g.clear();
fq.clear();
NTRUEncryptionPrivateKeyParameters priv = new NTRUEncryptionPrivateKeyParameters(h, t, fp, params.getEncryptionParameters());
NTRUEncryptionPublicKeyParameters pub = new NTRUEncryptionPublicKeyParameters(h, params.getEncryptionParameters());
return new AsymmetricCipherKeyPair(pub, priv);
}
use of com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair in project LinLong-Java by zhenwei1108.
the class KeyPairGeneratorSpi method generateKeyPair.
public KeyPair generateKeyPair() {
if (!initialised) {
throw new IllegalStateException("DSTU Key Pair Generator not initialised");
}
AsymmetricCipherKeyPair pair = engine.generateKeyPair();
ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
if (ecParams instanceof ECParameterSpec) {
ECParameterSpec p = (ECParameterSpec) ecParams;
BCDSTU4145PublicKey pubKey = new BCDSTU4145PublicKey(algorithm, pub, p);
return new KeyPair(pubKey, new BCDSTU4145PrivateKey(algorithm, priv, pubKey, p));
} else if (ecParams == null) {
return new KeyPair(new BCDSTU4145PublicKey(algorithm, pub), new BCDSTU4145PrivateKey(algorithm, priv));
} else {
java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
BCDSTU4145PublicKey pubKey = new BCDSTU4145PublicKey(algorithm, pub, p);
return new KeyPair(pubKey, new BCDSTU4145PrivateKey(algorithm, priv, pubKey, p));
}
}
Aggregations