Skip to content

Commit

Permalink
further cleanup work on McEliece
Browse files Browse the repository at this point in the history
added setting of digest to CCA2 params
  • Loading branch information
dghgit committed Apr 2, 2016
1 parent 0416add commit d7344de
Show file tree
Hide file tree
Showing 30 changed files with 223 additions and 585 deletions.
Expand Up @@ -89,14 +89,6 @@ public void reset()
messDigest.reset();
}

/**
* @deprecated use verifySignature
*/
public boolean verify(byte[] signature)
{
return verifySignature(signature);
}

/**
* This function verifies the signature of the message that has been
* updated, with the aid of the public key.
Expand Down
Expand Up @@ -2,6 +2,7 @@


import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.InvalidCipherTextException;

/**
* Base interface for a PQC encryption algorithm.
Expand All @@ -22,12 +23,13 @@ public interface MessageEncryptor
* @param message the message to be signed.
* @throws Exception
*/
public byte[] messageEncrypt(byte[] message) throws Exception;
public byte[] messageEncrypt(byte[] message);

/**
*
* @param cipher the cipher text of the message
* @throws Exception
*/
public byte[] messageDecrypt(byte[] cipher) throws Exception;
public byte[] messageDecrypt(byte[] cipher)
throws InvalidCipherTextException;
}
Expand Up @@ -5,13 +5,7 @@

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.KeyGenerationParameters;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA384Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.pqc.math.linearalgebra.GF2Matrix;
import org.bouncycastle.pqc.math.linearalgebra.GF2mField;
import org.bouncycastle.pqc.math.linearalgebra.GoppaCode;
Expand Down Expand Up @@ -110,36 +104,10 @@ public AsymmetricCipherKeyPair generateKeyPair()
int k = shortG.getNumRows();

// generate keys
McElieceCCA2PublicKeyParameters pubKey = new McElieceCCA2PublicKeyParameters(n, t, shortG, getDigest(mcElieceCCA2Params.getParameters().getDigest().getAlgorithmName()));
McElieceCCA2PrivateKeyParameters privKey = new McElieceCCA2PrivateKeyParameters(n, k, field, gp, p, getDigest(mcElieceCCA2Params.getParameters().getDigest().getAlgorithmName()));
McElieceCCA2PublicKeyParameters pubKey = new McElieceCCA2PublicKeyParameters(n, t, shortG, mcElieceCCA2Params.getParameters().getDigest());
McElieceCCA2PrivateKeyParameters privKey = new McElieceCCA2PrivateKeyParameters(n, k, field, gp, p, mcElieceCCA2Params.getParameters().getDigest());

// return key pair
return new AsymmetricCipherKeyPair(pubKey, privKey);
}

static Digest getDigest(String digestName)
{
if (digestName.equals("SHA-1"))
{
return new SHA1Digest();
}
if (digestName.equals("SHA-224"))
{
return new SHA224Digest();
}
if (digestName.equals("SHA-256"))
{
return new SHA256Digest();
}
if (digestName.equals("SHA-384"))
{
return new SHA384Digest();
}
if (digestName.equals("SHA-512"))
{
return new SHA512Digest();
}

throw new IllegalArgumentException("unrecognised digest algorithm: " + digestName);
}
}
@@ -1,24 +1,23 @@
package org.bouncycastle.pqc.crypto.mceliece;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;


public class McElieceCCA2KeyParameters
extends AsymmetricKeyParameter
{
private Digest params;
private String params;

public McElieceCCA2KeyParameters(
boolean isPrivate,
Digest params)
String params)
{
super(isPrivate);
this.params = params;
}


public Digest getDigest()
public String getDigest()
{
return params;
}
Expand Down
@@ -1,22 +1,19 @@
package org.bouncycastle.pqc.crypto.mceliece;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;

public class McElieceCCA2Parameters
extends McElieceParameters
{
private Digest digest;
private final String digest;

/**
* Constructor. Set the default parameters: extension degree.
*/
public McElieceCCA2Parameters()
{
this(DEFAULT_M, DEFAULT_T, new SHA256Digest());
this(DEFAULT_M, DEFAULT_T, "SHA-256");
}

public McElieceCCA2Parameters(Digest digest)
public McElieceCCA2Parameters(String digest)
{
this(DEFAULT_M, DEFAULT_T, digest);
}
Expand All @@ -29,7 +26,7 @@ public McElieceCCA2Parameters(Digest digest)
*/
public McElieceCCA2Parameters(int keysize)
{
this(keysize, new SHA256Digest());
this(keysize, "SHA-256");
}

/**
Expand All @@ -39,7 +36,7 @@ public McElieceCCA2Parameters(int keysize)
* @param digest CCA2 mode digest
* @throws IllegalArgumentException if <tt>keysize &lt; 1</tt>.
*/
public McElieceCCA2Parameters(int keysize, Digest digest)
public McElieceCCA2Parameters(int keysize, String digest)
{
super(keysize);
this.digest = digest;
Expand All @@ -55,7 +52,7 @@ public McElieceCCA2Parameters(int keysize, Digest digest)
*/
public McElieceCCA2Parameters(int m, int t)
{
this(m, t, new SHA256Digest());
this(m, t, "SHA-256");
}

/**
Expand All @@ -66,7 +63,7 @@ public McElieceCCA2Parameters(int m, int t)
* @throws IllegalArgumentException if <tt>m &lt; 1</tt> or <tt>m &gt; 32</tt> or
* <tt>t &lt; 0</tt> or <tt>t &gt; n</tt>.
*/
public McElieceCCA2Parameters(int m, int t, Digest digest)
public McElieceCCA2Parameters(int m, int t, String digest)
{
super(m, t);
this.digest = digest;
Expand All @@ -84,7 +81,7 @@ public McElieceCCA2Parameters(int m, int t, Digest digest)
*/
public McElieceCCA2Parameters(int m, int t, int poly)
{
this(m, t, poly, new SHA256Digest());
this(m, t, poly, "SHA-256");
}

/**
Expand All @@ -98,7 +95,7 @@ public McElieceCCA2Parameters(int m, int t, int poly)
* <tt>t &lt; 0</tt> or <tt>t &gt; n</tt> or
* <tt>poly</tt> is not an irreducible field polynomial.
*/
public McElieceCCA2Parameters(int m, int t, int poly, Digest digest)
public McElieceCCA2Parameters(int m, int t, int poly, String digest)
{
super(m, t, poly);
this.digest = digest;
Expand All @@ -109,7 +106,7 @@ public McElieceCCA2Parameters(int m, int t, int poly, Digest digest)
*
* @return the CCA2 digest to use, null if not present.
*/
public Digest getDigest()
public String getDigest()
{
return digest;
}
Expand Down
@@ -1,7 +1,6 @@
package org.bouncycastle.pqc.crypto.mceliece;


import org.bouncycastle.crypto.Digest;
import org.bouncycastle.pqc.math.linearalgebra.GF2Matrix;
import org.bouncycastle.pqc.math.linearalgebra.GF2mField;
import org.bouncycastle.pqc.math.linearalgebra.GoppaCode;
Expand Down Expand Up @@ -49,7 +48,7 @@ public class McElieceCCA2PrivateKeyParameters
* @param digest McElieceCCA2Parameters
*/
public McElieceCCA2PrivateKeyParameters(int n, int k, GF2mField field,
PolynomialGF2mSmallM gp, Permutation p, Digest digest)
PolynomialGF2mSmallM gp, Permutation p, String digest)
{
super(true, digest);
this.n = n;
Expand Down
@@ -1,6 +1,5 @@
package org.bouncycastle.pqc.crypto.mceliece;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.pqc.math.linearalgebra.GF2Matrix;

/**
Expand All @@ -27,7 +26,7 @@ public class McElieceCCA2PublicKeyParameters
* @param matrix generator matrix
* @param digest McElieceCCA2Parameters
*/
public McElieceCCA2PublicKeyParameters(int n, int t, GF2Matrix matrix, Digest digest)
public McElieceCCA2PublicKeyParameters(int n, int t, GF2Matrix matrix, String digest)
{
super(false, digest);

Expand Down
Expand Up @@ -44,14 +44,15 @@ public class McElieceCipher
// The maximum number of bytes the cipher can encrypt
public int cipherTextSize;

McElieceKeyParameters key;
private McElieceKeyParameters key;
private boolean forEncryption;


public void init(boolean forSigning,
public void init(boolean forEncryption,
CipherParameters param)
{

if (forSigning)
this.forEncryption = forEncryption;
if (forEncryption)
{
if (param instanceof ParametersWithRandom)
{
Expand All @@ -77,7 +78,6 @@ public void init(boolean forSigning,

}


/**
* Return the key size of the given key object.
*
Expand Down Expand Up @@ -130,6 +130,10 @@ public void initCipherDecrypt(McEliecePrivateKeyParameters privKey)
*/
public byte[] messageEncrypt(byte[] input)
{
if (!forEncryption)
{
throw new IllegalStateException("cipher initialised for decryption");
}
GF2Vector m = computeMessageRepresentative(input);
GF2Vector z = new GF2Vector(n, t, sr);

Expand Down Expand Up @@ -158,6 +162,11 @@ private GF2Vector computeMessageRepresentative(byte[] input)
public byte[] messageDecrypt(byte[] input)
throws InvalidCipherTextException
{
if (forEncryption)
{
throw new IllegalStateException("cipher initialised for decryption");
}

GF2Vector vec = GF2Vector.OS2VP(n, input);
McEliecePrivateKeyParameters privKey = (McEliecePrivateKeyParameters)key;
GF2mField field = privKey.getField();
Expand Down
Expand Up @@ -41,13 +41,14 @@ public class McElieceFujisakiCipher
private int n, k, t;

McElieceCCA2KeyParameters key;
private boolean forEncryption;


public void init(boolean forEncrypting,
public void init(boolean forEncryption,
CipherParameters param)
{

if (forEncrypting)
this.forEncryption = forEncryption;
if (forEncryption)
{
if (param instanceof ParametersWithRandom)
{
Expand Down Expand Up @@ -93,7 +94,7 @@ public int getKeySize(McElieceCCA2KeyParameters key)
private void initCipherEncrypt(McElieceCCA2PublicKeyParameters pubKey)
{
this.sr = sr != null ? sr : new SecureRandom();
this.messDigest = pubKey.getDigest();
this.messDigest = Utils.getDigest(pubKey.getDigest());
n = pubKey.getN();
k = pubKey.getK();
t = pubKey.getT();
Expand All @@ -102,14 +103,19 @@ private void initCipherEncrypt(McElieceCCA2PublicKeyParameters pubKey)

public void initCipherDecrypt(McElieceCCA2PrivateKeyParameters privKey)
{
this.messDigest = privKey.getDigest();
this.messDigest = Utils.getDigest(privKey.getDigest());
n = privKey.getN();
t = privKey.getT();
}


public byte[] messageEncrypt(byte[] input)
{
if (!forEncryption)
{
throw new IllegalStateException("cipher initialised for decryption");
}

// generate random vector r of length k bits
GF2Vector r = new GF2Vector(k, sr);

Expand Down Expand Up @@ -154,6 +160,11 @@ public byte[] messageEncrypt(byte[] input)
public byte[] messageDecrypt(byte[] input)
throws InvalidCipherTextException
{
if (forEncryption)
{
throw new IllegalStateException("cipher initialised for decryption");
}

int c1Len = (n + 7) >> 3;
int c2Len = input.length - c1Len;

Expand Down

0 comments on commit d7344de

Please sign in to comment.