Skip to content

Commit

Permalink
refactoring of context setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Sep 14, 2024
1 parent 42b5928 commit d2df9f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class HashSLHDSASigner
{
private SLHDSAPrivateKeyParameters privKey;
private SLHDSAPublicKeyParameters pubKey;

private byte[] ctx;
private SecureRandom random;
private Digest digest;
private byte[] digestOidEncoding;
Expand All @@ -48,12 +48,26 @@ 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 Expand Up @@ -93,12 +107,6 @@ public byte[] generateSignature() throws CryptoException, DataLengthException
SLHDSAEngine engine = privKey.getParameters().getEngine();

engine.init(privKey.pk.seed);
byte[] ctx = privKey.getContext();

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

byte[] hash = new byte[digest.getDigestSize()];
digest.doFinal(hash, 0);
Expand All @@ -118,13 +126,6 @@ public byte[] generateSignature() throws CryptoException, DataLengthException
@Override
public boolean verifySignature(byte[] signature)
{
byte[] ctx = pubKey.getContext();

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

byte[] hash = new byte[digest.getDigestSize()];
digest.doFinal(hash, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SLHDSASigner
{
private SLHDSAPrivateKeyParameters privKey;
private SLHDSAPublicKeyParameters pubKey;

private byte[] ctx;
private SecureRandom random;

/**
Expand All @@ -48,11 +48,26 @@ public void init(boolean forSigning, CipherParameters param)
privKey = (SLHDSAPrivateKeyParameters)param;
}

ctx = privKey.getContext();

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

isPreHash = privKey.parameters.isPreHash();
}
else
{
pubKey = (SLHDSAPublicKeyParameters)param;

ctx = pubKey.getContext();

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

isPreHash = pubKey.parameters.isPreHash();
}

Expand All @@ -67,12 +82,6 @@ public byte[] generateSignature(byte[] message)
SLHDSAEngine engine = privKey.getParameters().getEngine();

engine.init(privKey.pk.seed);
byte[] ctx = privKey.getContext();

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

byte[] ds_message = new byte[1 + 1 + ctx.length + message.length];
ds_message[0] = 0;
Expand All @@ -88,12 +97,6 @@ public byte[] generateSignature(byte[] message)
// Equivalent to slh_verify_internal from specs
public boolean verifySignature(byte[] message, byte[] signature)
{
byte[] ctx = pubKey.getContext();
if (ctx.length > 255)
{
throw new RuntimeException("Context too long");
}

byte[] ds_message = new byte[1 + 1 + ctx.length + message.length];
ds_message[0] = 0;
ds_message[1] = (byte)ctx.length;
Expand All @@ -102,6 +105,7 @@ public boolean verifySignature(byte[] message, byte[] signature)

return internalVerifySignature(ds_message, signature);
}

public boolean internalVerifySignature(byte[] message, byte[] signature)
{
//# Input: Message M, signature SIG, public key PK
Expand Down

0 comments on commit d2df9f2

Please sign in to comment.