Changeset 6e7f015


Ignore:
Timestamp:
Feb 28, 2016 3:37:03 AM (4 years ago)
Author:
str4d <str4d@…>
Branches:
master
Children:
7ccca9e
Parents:
8086023
Message:

Add tests to check that an EdDSAEngine object can be reused with the same key

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/test/junit/net/i2p/crypto/eddsa/EdDSAEngineTest.java

    r8086023 r6e7f015  
    2525 */
    2626public class EdDSAEngineTest {
    27     static final byte[] ZERO_SEED = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
    28     static final byte[] ZERO_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");
    29     static final byte[] ZERO_MSG_SIG = Utils.hexToBytes("94825896c7075c31bcb81f06dba2bdcd9dcf16e79288d4b9f87c248215c8468d475f429f3de3b4a2cf67fe17077ae19686020364d6d4fa7a0174bab4a123ba0f");
     27    static final byte[] TEST_SEED = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
     28    static final byte[] TEST_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");
     29    static final byte[] TEST_MSG = "This is a secret message".getBytes(Charset.forName("UTF-8"));
     30    static final byte[] TEST_MSG_SIG = Utils.hexToBytes("94825896c7075c31bcb81f06dba2bdcd9dcf16e79288d4b9f87c248215c8468d475f429f3de3b4a2cf67fe17077ae19686020364d6d4fa7a0174bab4a123ba0f");
    3031
    3132    @Rule
     
    7677        Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    7778
    78         EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(ZERO_PK,
     79        EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
    7980                EdDSANamedCurveTable.getByName("ed25519-sha-512"));
    8081        PublicKey vKey = new EdDSAPublicKey(pubKey);
    8182        sgr.initVerify(vKey);
    8283
    83         byte[] message = "This is a secret message".getBytes(Charset.forName("UTF-8"));
    84         sgr.update(message);
     84        sgr.update(TEST_MSG);
    8585
    8686        exception.expect(SignatureException.class);
     
    8888        sgr.verify(new byte[] {0});
    8989    }
     90
     91    @Test
     92    public void testSignResetsForReuse() throws Exception {
     93        Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
     94        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     95
     96        EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
     97        PrivateKey sKey = new EdDSAPrivateKey(privKey);
     98        sgr.initSign(sKey);
     99
     100        // First usage
     101        sgr.update(new byte[] {0});
     102        sgr.sign();
     103
     104        // Second usage
     105        sgr.update(TEST_MSG);
     106        assertThat("Second sign failed", sgr.sign(), is(equalTo(TEST_MSG_SIG)));
     107    }
     108
     109    @Test
     110    public void testVerifyResetsForReuse() throws Exception {
     111        //Signature sgr = Signature.getInstance("EdDSA", "I2P");
     112        Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
     113
     114        EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
     115                EdDSANamedCurveTable.getByName("ed25519-sha-512"));
     116        PublicKey vKey = new EdDSAPublicKey(pubKey);
     117        sgr.initVerify(vKey);
     118
     119        // First usage
     120        sgr.update(new byte[] {0});
     121        sgr.verify(TEST_MSG_SIG);
     122
     123        // Second usage
     124        sgr.update(TEST_MSG);
     125        assertThat("Second verify failed", sgr.verify(TEST_MSG_SIG), is(true));
     126    }
    90127}
Note: See TracChangeset for help on using the changeset viewer.