Skip to content

Bbs plus correction and add multi message signing/verifying #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public static PublicKeyVerifier<?> publicKeyVerifierForKey(KeyTypeName keyTypeNa
if (JWSAlgorithm.ES256KCC.equals(algorithm)) return new secp256k1_ES256KCC_PublicKeyVerifier((ECKey) publicKey);
} else if (KeyTypeName.Bls12381G1.equals(keyTypeName)) {

if (JWSAlgorithm.BBSPlus.equals(algorithm)) return new Bls12381G1_BBSPlus_PublicKeyVerifier((bbs.signatures.KeyPair) publicKey);
if (JWSAlgorithm.BBSPlus.equals(algorithm)) return new Bls12381G1_BBSPlus_PublicKeyVerifier((byte[]) publicKey);
} else if (KeyTypeName.Bls12381G2.equals(keyTypeName)) {

if (JWSAlgorithm.BBSPlus.equals(algorithm)) return new Bls12381G2_BBSPlus_PublicKeyVerifier((bbs.signatures.KeyPair) publicKey);
if (JWSAlgorithm.BBSPlus.equals(algorithm)) return new Bls12381G2_BBSPlus_PublicKeyVerifier((byte[]) publicKey);
} else if (KeyTypeName.Bls48581G1.equals(keyTypeName)) {

if (JWSAlgorithm.BBSPlus.equals(algorithm)) return new Bls48581G1_BBSPlus_PublicKeyVerifier((bbs.signatures.KeyPair) publicKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.danubetech.keyformats.crypto.impl;

import bbs.signatures.Bbs;
import bbs.signatures.KeyPair;
import com.danubetech.keyformats.crypto.PrivateKeySigner;
import com.danubetech.keyformats.jose.JWSAlgorithm;

import java.security.GeneralSecurityException;
import java.util.List;

public class BBSPlus_PrivateKeySigner extends PrivateKeySigner<byte[]> {

private final byte[] publicKey;

public BBSPlus_PrivateKeySigner(KeyPair keyPair) {
super( keyPair.secretKey, JWSAlgorithm.BBSPlus);
int keySize = Bbs.getSecretKeySize();
if (keyPair.secretKey.length != keySize) {
throw new IllegalArgumentException("wrong key size: expected: " + keySize + "but was " + keyPair.secretKey.length);
}
publicKey = keyPair.publicKey;
}

@Override
public byte[] sign(byte[] content) throws GeneralSecurityException {
return sign(List.of(content));
}

public final byte[] sign(List<byte[]> content, String algorithm) throws GeneralSecurityException {

if (! algorithm.equals(getAlgorithm())) throw new GeneralSecurityException("Unexpected algorithm " + algorithm + " is different from " + getAlgorithm());

return this.sign(content);
}

public byte[] sign(List<byte[]> content) throws GeneralSecurityException {
try {
return Bbs.blsSign(getPrivateKey(), publicKey, content.toArray(new byte[content.size()][]));
} catch (GeneralSecurityException ex) {
throw ex;
} catch (Exception ex) {
throw new GeneralSecurityException(ex.getMessage(), ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.danubetech.keyformats.crypto.impl;

import bbs.signatures.Bbs;
import com.danubetech.keyformats.crypto.PublicKeyVerifier;
import com.danubetech.keyformats.jose.JWSAlgorithm;

import java.security.GeneralSecurityException;
import java.util.List;

public class BBSPlus_PublicKeyVerifier extends PublicKeyVerifier<byte[]> {

public BBSPlus_PublicKeyVerifier(byte[] publicKey) {
super(publicKey, JWSAlgorithm.BBSPlus);
}

@Override
public boolean verify(byte[] content, byte[] signature) throws GeneralSecurityException {
return verify(List.of(content), signature);
}

public final boolean verify(List<byte[]> content, byte[] signature, String algorithm) throws GeneralSecurityException {
if (!algorithm.equals(getAlgorithm())) {
throw new GeneralSecurityException("Unexpected algorithm " + algorithm + " is different from " + getAlgorithm());
}
return this.verify(content, signature);
}

public boolean verify(List<byte[]> content, byte[] signature) throws GeneralSecurityException {
try {
return Bbs.blsVerify(getPublicKey(), signature, content.toArray(new byte[content.size()][]));
} catch (GeneralSecurityException ex) {
throw ex;
} catch (Exception ex) {
throw new GeneralSecurityException(ex.getMessage(), ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,9 @@

import java.security.GeneralSecurityException;

public class Bls12381G1_BBSPlus_PrivateKeySigner extends PrivateKeySigner<KeyPair> {
public class Bls12381G1_BBSPlus_PrivateKeySigner extends BBSPlus_PrivateKeySigner {

public Bls12381G1_BBSPlus_PrivateKeySigner(KeyPair privateKey) {

super(privateKey, JWSAlgorithm.BBSPlus);
}

@Override
public byte[] sign(byte[] content) throws GeneralSecurityException {

try {

return Bbs.blsSign(this.getPrivateKey().secretKey, this.getPrivateKey().publicKey, new byte[][]{content});
} catch (GeneralSecurityException ex) {

throw ex;
} catch (Exception ex) {

throw new GeneralSecurityException(ex.getMessage(), ex);
}
public Bls12381G1_BBSPlus_PrivateKeySigner(KeyPair keyPair) {
super(keyPair);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
package com.danubetech.keyformats.crypto.impl;

import bbs.signatures.Bbs;
import bbs.signatures.KeyPair;
import com.danubetech.keyformats.crypto.PublicKeyVerifier;
import com.danubetech.keyformats.jose.JWSAlgorithm;

import java.security.GeneralSecurityException;
public class Bls12381G1_BBSPlus_PublicKeyVerifier extends BBSPlus_PublicKeyVerifier {

public class Bls12381G1_BBSPlus_PublicKeyVerifier extends PublicKeyVerifier<KeyPair> {

public Bls12381G1_BBSPlus_PublicKeyVerifier(KeyPair publicKey) {

super(publicKey, JWSAlgorithm.BBSPlus);
}

@Override
public boolean verify(byte[] content, byte[] signature) throws GeneralSecurityException {

try {

return Bbs.blsVerify(this.getPublicKey().publicKey, signature, new byte[][]{signature});
} catch (GeneralSecurityException ex) {

throw ex;
} catch (Exception ex) {

throw new GeneralSecurityException(ex.getMessage(), ex);
public Bls12381G1_BBSPlus_PublicKeyVerifier(byte[] publicKey) {
super(publicKey);
int keySize = Bbs.getBls12381G1PublicKeySize();
if (publicKey.length != keySize) {
throw new IllegalArgumentException("wrong key size: expected: " + keySize + "but was " + publicKey.length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,9 @@

import java.security.GeneralSecurityException;

public class Bls12381G2_BBSPlus_PrivateKeySigner extends PrivateKeySigner<KeyPair> {
public class Bls12381G2_BBSPlus_PrivateKeySigner extends BBSPlus_PrivateKeySigner {

public Bls12381G2_BBSPlus_PrivateKeySigner(KeyPair privateKey) {

super(privateKey, JWSAlgorithm.BBSPlus);
}

@Override
public byte[] sign(byte[] content) throws GeneralSecurityException {

try {

return Bbs.blsSign(this.getPrivateKey().secretKey, this.getPrivateKey().publicKey, new byte[][]{content});
} catch (GeneralSecurityException ex) {

throw ex;
} catch (Exception ex) {

throw new GeneralSecurityException(ex.getMessage(), ex);
}
public Bls12381G2_BBSPlus_PrivateKeySigner(KeyPair keyPair) {
super(keyPair);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,12 @@

import java.security.GeneralSecurityException;

public class Bls12381G2_BBSPlus_PublicKeyVerifier extends PublicKeyVerifier<KeyPair> {

public Bls12381G2_BBSPlus_PublicKeyVerifier(KeyPair publicKey) {

super(publicKey, JWSAlgorithm.BBSPlus);
}

@Override
public boolean verify(byte[] content, byte[] signature) throws GeneralSecurityException {

try {

return Bbs.blsVerify(this.getPublicKey().publicKey, signature, new byte[][]{signature});
} catch (GeneralSecurityException ex) {

throw ex;
} catch (Exception ex) {

throw new GeneralSecurityException(ex.getMessage(), ex);
public class Bls12381G2_BBSPlus_PublicKeyVerifier extends BBSPlus_PublicKeyVerifier {
public Bls12381G2_BBSPlus_PublicKeyVerifier(byte[] publicKey) {
super(publicKey);
int keySize = Bbs.getBls12381G2PublicKeySize();
if (publicKey.length != keySize) {
throw new IllegalArgumentException("wrong key size: expected: " + keySize + "but was " + publicKey.length);
}
}
}