Skip to content

Commit

Permalink
refactoring of context into ParametersWithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Sep 14, 2024
1 parent d2df9f2 commit 32ba114
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.bouncycastle.crypto.params;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.util.Arrays;

public class ParametersWithContext
implements CipherParameters
{
private CipherParameters parameters;
private byte[] context;

public ParametersWithContext(
CipherParameters parameters,
byte[] context)
{
this.parameters = parameters;
this.context = Arrays.clone(context);
}

public byte[] getContext()
{
return Arrays.clone(context);
}

public CipherParameters getParameters()
{
return parameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import org.bouncycastle.crypto.Signer;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHAKEDigest;
import org.bouncycastle.crypto.params.ParametersWithContext;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.pqc.crypto.DigestUtils;
import org.bouncycastle.util.Arrays;

public class HashMLDSASigner
implements Signer
{
private static final byte[] EMPTY_CONTEXT = new byte[0];

private MLDSAPrivateKeyParameters privKey;
private MLDSAPublicKeyParameters pubKey;

Expand All @@ -34,6 +37,23 @@ public HashMLDSASigner()

public void init(boolean forSigning, CipherParameters param)
{
byte[] ctx;

if (param instanceof ParametersWithContext)
{
ctx = ((ParametersWithContext)param).getContext();
param = ((ParametersWithContext)param).getParameters();

if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}
}
else
{
ctx = EMPTY_CONTEXT;
}

if (forSigning)
{
if (param instanceof ParametersWithRandom)
Expand All @@ -49,12 +69,6 @@ public void init(boolean forSigning, CipherParameters param)

engine = privKey.getParameters().getEngine(this.random);

byte[] ctx = privKey.getContext();
if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}

engine.initSign(privKey.tr, true, ctx);

initDigest(privKey);
Expand All @@ -64,13 +78,7 @@ public void init(boolean forSigning, CipherParameters param)
pubKey = (MLDSAPublicKeyParameters)param;

engine = pubKey.getParameters().getEngine(this.random);

byte[] ctx = pubKey.getContext();
if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}


engine.initVerify(pubKey.rho, pubKey.t1, true, ctx);

initDigest(pubKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,16 @@ public class MLDSAKeyParameters
{
private final MLDSAParameters params;

private final byte[] context;

public MLDSAKeyParameters(boolean isPrivate, MLDSAParameters params, byte[] context)
{
super(isPrivate);
this.params = params;
this.context = context;
}

public MLDSAKeyParameters(
boolean isPrivate,
MLDSAParameters params)
{
super(isPrivate);
this.params = params;
this.context = new byte[0];
}

public MLDSAParameters getParameters()
{
return params;
}

public byte[] getContext()
{
return context.clone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.Signer;
import org.bouncycastle.crypto.digests.SHAKEDigest;
import org.bouncycastle.crypto.params.ParametersWithContext;
import org.bouncycastle.crypto.params.ParametersWithRandom;

public class MLDSASigner
implements Signer
{
private static final byte[] EMPTY_CONTEXT = new byte[0];

private MLDSAPrivateKeyParameters privKey;
private MLDSAPublicKeyParameters pubKey;

Expand All @@ -27,6 +30,23 @@ public MLDSASigner()
public void init(boolean forSigning, CipherParameters param)
{
boolean isPreHash;
byte[] ctx;

if (param instanceof ParametersWithContext)
{
ctx = ((ParametersWithContext)param).getContext();
param = ((ParametersWithContext)param).getParameters();

if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}
}
else
{
ctx = EMPTY_CONTEXT;
}


if (forSigning)
{
Expand All @@ -43,12 +63,6 @@ public void init(boolean forSigning, CipherParameters param)

engine = privKey.getParameters().getEngine(this.random);

byte[] ctx = privKey.getContext();
if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}

engine.initSign(privKey.tr, false, ctx);

msgDigest = engine.getShake256Digest();
Expand All @@ -61,12 +75,6 @@ public void init(boolean forSigning, CipherParameters param)

engine = pubKey.getParameters().getEngine(random);

byte[] ctx = pubKey.getContext();
if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}

engine.initVerify(pubKey.rho, pubKey.t1, false, ctx);

msgDigest = engine.getShake256Digest();
Expand Down Expand Up @@ -111,21 +119,21 @@ public boolean verifySignature(byte[] signature)
boolean isTrue = engine.verifyInternal(signature, signature.length, msgDigest, pubKey.rho, pubKey.t1);

reset();

return isTrue;
}

public void reset()
{
msgDigest = engine.getShake256Digest();
}

protected byte[] internalGenerateSignature(byte[] message, byte[] random)
{
MLDSAEngine engine = privKey.getParameters().getEngine(this.random);

engine.initSign(privKey.tr, false, null);

return engine.signInternal(message, message.length, privKey.rho, privKey.k, privKey.t0, privKey.s1, privKey.s2, random);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHAKEDigest;
import org.bouncycastle.crypto.params.ParametersWithContext;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.pqc.crypto.DigestUtils;
import org.bouncycastle.util.Arrays;
Expand All @@ -23,6 +24,8 @@
public class HashSLHDSASigner
implements Signer
{
private static final byte[] EMPTY_CONTEXT = new byte[0];

private SLHDSAPrivateKeyParameters privKey;
private SLHDSAPublicKeyParameters pubKey;
private byte[] ctx;
Expand All @@ -36,6 +39,21 @@ public HashSLHDSASigner()

public void init(boolean forSigning, CipherParameters param)
{
if (param instanceof ParametersWithContext)
{
ctx = ((ParametersWithContext)param).getContext();
param = ((ParametersWithContext)param).getParameters();

if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}
}
else
{
ctx = EMPTY_CONTEXT;
}

if (forSigning)
{
if (param instanceof ParametersWithRandom)
Expand All @@ -48,26 +66,12 @@ public void init(boolean forSigning, CipherParameters param)
privKey = (SLHDSAPrivateKeyParameters)param;
}

ctx = privKey.getContext();

if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}

initDigest(privKey);
}
else
{
pubKey = (SLHDSAPublicKeyParameters)param;

ctx = pubKey.getContext();

if (ctx.length > 255)
{
throw new IllegalArgumentException("context too long");
}


initDigest(pubKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,15 @@ public class SLHDSAKeyParameters
extends AsymmetricKeyParameter
{
final SLHDSAParameters parameters;
final byte[] context;

protected SLHDSAKeyParameters(boolean isPrivate, SLHDSAParameters parameters, byte[] context)
{
super(isPrivate);
this.parameters = parameters;
this.context = context;
}

protected SLHDSAKeyParameters(boolean isPrivate, SLHDSAParameters parameters)
{
super(isPrivate);
this.parameters = parameters;
this.context = new byte[0];
}

public SLHDSAParameters getParameters()
{
return parameters;
}

public byte[] getContext()
{
return context.clone();
}
}
Loading

0 comments on commit 32ba114

Please sign in to comment.