aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/gnunet/util/CryptoECCTest.java
blob: 1dda545738459d587bbd4d5543780417b5460472 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.gnunet.util;


import org.junit.Assert;
import org.junit.Test;

import java.util.Random;

public class CryptoECCTest {
    /**
     * Check that signed messages can be verified correctly.
     */
    @Test
    public void test_sign_success() {
        Random r = new Random();
        // the test uses random data, repeat it multiple times!
        for (int i = 0; i < 5; i++) {
            byte[] msg = new byte[16];
            r.nextBytes(msg);

            CryptoECC.PrivateKey privateKey = new CryptoECC.PrivateKey();
            privateKey.d = new byte[32];
            r.nextBytes(privateKey.d);
            CryptoECC.PublicSignKey publicKey = CryptoECC.computePublicKey(privateKey);
            System.out.println("gen");
            CryptoECC.Signature sig = CryptoECC.sign(msg, privateKey, publicKey);
            System.out.println("sign");
            boolean valid = CryptoECC.verify(sig, msg, publicKey);
            System.out.println("verify");
            Assert.assertTrue(valid);
        }
    }

    /**
     * Check that signature verification fails for manipulated data.
     */
    @Test
    public void test_sign_failure() {
        Random r = new Random();
        // the test uses random data, repeat it multiple times!
        for (int i = 0; i < 5; i++) {
            byte[] msg = new byte[16];
            r.nextBytes(msg);
            CryptoECC.PrivateKey privateKey = new CryptoECC.PrivateKey();
            privateKey.d = new byte[32];
            r.nextBytes(privateKey.d);
            CryptoECC.PublicSignKey publicKey = CryptoECC.computePublicKey(privateKey);
            System.out.println("gen");
            CryptoECC.Signature sig = CryptoECC.sign(msg, privateKey, publicKey);
            System.out.println("sign");
            // corrupt the message
            msg[0] = (byte) (msg[0] + 1);
            boolean valid = CryptoECC.verify(sig, msg, publicKey);
            System.out.println("verify");
            Assert.assertFalse(valid);
        }
    }

    /**
     * Check whether ecdh key coincide
     */
    @Test
    public void test_ecdh() {
        Random r = new Random();

        CryptoECC.PrivateKey privateAlice = new CryptoECC.PrivateKey();
        privateAlice.d = new byte[32];
        r.nextBytes(privateAlice.d);
        CryptoECC.PublicSignKey publicAlice = CryptoECC.computePublicKey(privateAlice);

        CryptoECC.PrivateKey privateBob = new CryptoECC.PrivateKey();
        privateBob.d = new byte[32];
        r.nextBytes(privateBob.d);
        CryptoECC.PublicSignKey publicBob = CryptoECC.computePublicKey(privateBob);

        HashCode ssAlice = CryptoECC.ecdh(privateAlice, publicBob);
        HashCode ssBob = CryptoECC.ecdh(privateBob, publicAlice);

        Assert.assertArrayEquals(ssAlice.data, ssBob.data);
    }
}