Skip to content

Commit

Permalink
Add Ed25519 verification to test
Browse files Browse the repository at this point in the history
  • Loading branch information
dufkan committed Jul 4, 2021
1 parent f377923 commit 103cf30
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions applet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.1'
testCompile 'net.i2p.crypto:eddsa:0.3.0'

testImplementation(group: 'com.klinec', name: 'javacard-tools', version: '1.0.4') {
exclude group: "com.klinec", module: "jcardsim"
Expand Down
20 changes: 17 additions & 3 deletions applet/src/test/java/tests/AppletTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package tests;

import applet.Consts;
import applet.jcmathlib;
import cz.muni.fi.crocs.rcard.client.CardManager;
import cz.muni.fi.crocs.rcard.client.CardType;
import net.i2p.crypto.eddsa.EdDSAPublicKey;
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec;
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
import org.junit.Assert;
import org.junit.jupiter.api.*;
import net.i2p.crypto.eddsa.EdDSAEngine;

import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.security.Signature;

/**
* Example test class for the applet
Expand Down Expand Up @@ -39,20 +46,21 @@ public void setUpMethod() throws Exception {
public void tearDownMethod() throws Exception {
}

public void keygen(CardManager cm) throws Exception {
public byte[] keygen(CardManager cm) throws Exception {
final CommandAPDU cmd = new CommandAPDU(Consts.CLA_ED25519, Consts.INS_KEYGEN,0, 0);
final ResponseAPDU responseAPDU = cm.transmit(cmd);
Assert.assertNotNull(responseAPDU);
Assert.assertEquals(0x9000, responseAPDU.getSW());
Assert.assertNotNull(responseAPDU.getBytes());
Assert.assertEquals(32, responseAPDU.getData().length);
return responseAPDU.getData();
}

@Test
public void keygen_and_sign() throws Exception {
final CardManager cm = connect();

keygen(cm);
byte[] pubkeyBytes = keygen(cm);

byte[] data = new byte[32];
for(int i = 0; i < data.length; ++i)
Expand All @@ -64,5 +72,11 @@ public void keygen_and_sign() throws Exception {
Assert.assertEquals(0x9000, responseAPDU.getSW());
Assert.assertNotNull(responseAPDU.getBytes());
Assert.assertEquals(32 + 32, responseAPDU.getData().length);
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
PublicKey pubKey = new EdDSAPublicKey(new EdDSAPublicKeySpec(pubkeyBytes, spec));
sgr.initVerify(pubKey);
sgr.update(data);
Assert.assertTrue(sgr.verify(responseAPDU.getData()));
}
}

0 comments on commit 103cf30

Please sign in to comment.