Commit 3d9fc119 authored by Lena Heimberger's avatar Lena Heimberger
Browse files

Provider + test

parent ce4ca018
package at.iaik.pq.sphincs.provider;
import java.security.PrivilegedAction;
import java.security.Provider;
import java.util.ArrayList;
import java.util.List;
/**
* The Post Quantum JCA Provider
*/
public final class SphincsProvider extends Provider {
private static SphincsProvider instance;
private static final class AddAlgorithmAction extends Provider implements PrivilegedAction<Void> {
private final SphincsProvider provider_;
AddAlgorithmAction(final SphincsProvider provider) {
super("SphincsProvider", 1.0, "PQ Security at.iaik.pq.picnic.provider");
provider_ = provider;
}
@Override
public Void run() {
return null;
}
}
private SphincsProvider() {
super("SphincsProvider", 1.0, "PQ Security at.iaik.pq.sphincs.provider");
//AccessController.doPrivileged(new AddAlgorithmAction(this));
List<String> sphincsAlias = new ArrayList<>();
sphincsAlias.add("SPHINCS+");
sphincsAlias.add("Sphincs+");
putService(new Service(this, "KeyPairGenerator", "SPHINCS+", "at.iaik.pq.sphincs.keys.SphincsKeyPairGenerator", sphincsAlias, null));
putService(new Service(this, "Signature", "SPHINCS+", "at.iaik.pq.sphincs.SphincsPlus", sphincsAlias, null));
putService(new Service(this, "KeyFactory", "SPHINCS+", "at.iaik.pq.sphincs.keys.SphincsKeyFactory", sphincsAlias, null));
putService(new Service(this, "KeyStore", "SPHINCS+", "at.iaik.pq.sphincs.certificate.SphincsKeyStore", sphincsAlias, null));
putService(new Service(this, "Certificate", "SPHINCS+", "at.iaik.pq.sphincs.certificate.SphincsCertificate", sphincsAlias, null));
}
public static SphincsProvider getInstance() {
if (instance == null) {
instance = new SphincsProvider();
}
return instance;
}
public static void addAsProvider() {
java.security.Security.insertProviderAt(getInstance(), 1);
}
}
package sphincs;
import at.iaik.pq.sphincs.SphincsPlus;
import at.iaik.pq.sphincs.provider.SphincsProvider;
import at.iaik.pq.sphincs.utils.SphincsParams;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.security.*;
import java.util.Arrays;
import static at.iaik.pq.sphincs.utils.SphincsParams.SphincsParamSet.Haraka_128f_simple;
public class SphincsProviderTest {
KeyPair sphincsKeyPair;
byte[] signature;
byte[] message = "Cyrillia is a genus of sea snails, marine gastropod mollusks in the family Raphitomidae.".getBytes(); // thanks wikipedia
@Before
public void initMember() {
try {
Security.addProvider(SphincsProvider.getInstance());
KeyPairGenerator sphincsKeyPairGenerator = KeyPairGenerator.getInstance("SPHINCS+");
SphincsParams sphincsParams = new SphincsParams(Haraka_128f_simple);
sphincsKeyPairGenerator.initialize(sphincsParams, null);
sphincsKeyPair = sphincsKeyPairGenerator.generateKeyPair();
SphincsPlus sphincsPlus = new SphincsPlus();
sphincsPlus.setParameter(sphincsParams);
sphincsPlus.update(message, 0, message.length);
sphincsPlus.initSign(sphincsKeyPair.getPrivate());
signature = sphincsPlus.sign();
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
}
@Test
public void signSphincs() {
Signature sphincs = null;
try {
sphincs = Signature.getInstance("SPHINCS+", "SphincsProvider");
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
e.printStackTrace();
}
try {
sphincs.initSign(sphincsKeyPair.getPrivate());
sphincs.update(message, 0, message.length);
byte[] sig = sphincs.sign();
Assert.assertTrue("arrays not equal", Arrays.equals(sig, signature));
return;
} catch (SignatureException | InvalidKeyException e) {
e.printStackTrace();
}
Assert.assertTrue(false);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment