aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test/java/org/whispersystems/libaxolotl/groups/GroupCipherTest.java
blob: 7c9892b02909fa70f19fce18e6add48ea0b1ac85 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.whispersystems.libaxolotl.groups;

import junit.framework.TestCase;

import org.whispersystems.libaxolotl.DuplicateMessageException;
import org.whispersystems.libaxolotl.InvalidMessageException;
import org.whispersystems.libaxolotl.LegacyMessageException;
import org.whispersystems.libaxolotl.NoSessionException;
import org.whispersystems.libaxolotl.ecc.ECKeyPair;
import org.whispersystems.libaxolotl.protocol.SenderKeyDistributionMessage;
import org.whispersystems.libaxolotl.util.KeyHelper;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;

public class GroupCipherTest extends TestCase {

  public void testBasicEncryptDecrypt()
      throws LegacyMessageException, DuplicateMessageException, InvalidMessageException, NoSessionException
  {
    InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
    InMemorySenderKeyStore bobStore   = new InMemorySenderKeyStore();

    GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore);
    GroupSessionBuilder bobSessionBuilder   = new GroupSessionBuilder(bobStore);

    GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, "groupWithBobInIt");
    GroupCipher bobGroupCipher   = new GroupCipher(bobStore, "groupWithBobInIt::aliceUserName");

    byte[]    aliceSenderKey        = KeyHelper.generateSenderKey();
    ECKeyPair aliceSenderSigningKey = KeyHelper.generateSenderSigningKey();
    int       aliceSenderKeyId      = KeyHelper.generateSenderKeyId();

    SenderKeyDistributionMessage aliceDistributionMessage =
        aliceSessionBuilder.process("groupWithBobInIt", aliceSenderKeyId, 0,
                                    aliceSenderKey, aliceSenderSigningKey);

    bobSessionBuilder.process("groupWithBobInIt::aliceUserName", aliceDistributionMessage);

    byte[] ciphertextFromAlice = aliceGroupCipher.encrypt("smert ze smert".getBytes());
    byte[] plaintextFromAlice  = bobGroupCipher.decrypt(ciphertextFromAlice);

    assertTrue(new String(plaintextFromAlice).equals("smert ze smert"));
  }

  public void testBasicRatchet()
      throws LegacyMessageException, DuplicateMessageException, InvalidMessageException, NoSessionException
  {
    InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
    InMemorySenderKeyStore bobStore   = new InMemorySenderKeyStore();

    GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore);
    GroupSessionBuilder bobSessionBuilder   = new GroupSessionBuilder(bobStore);

    GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, "groupWithBobInIt");
    GroupCipher bobGroupCipher   = new GroupCipher(bobStore, "groupWithBobInIt::aliceUserName");

    byte[]    aliceSenderKey        = KeyHelper.generateSenderKey();
    ECKeyPair aliceSenderSigningKey = KeyHelper.generateSenderSigningKey();
    int       aliceSenderKeyId      = KeyHelper.generateSenderKeyId();

    SenderKeyDistributionMessage aliceDistributionMessage =
        aliceSessionBuilder.process("groupWithBobInIt", aliceSenderKeyId, 0,
                                    aliceSenderKey, aliceSenderSigningKey);

    bobSessionBuilder.process("groupWithBobInIt::aliceUserName", aliceDistributionMessage);

    byte[] ciphertextFromAlice  = aliceGroupCipher.encrypt("smert ze smert".getBytes());
    byte[] ciphertextFromAlice2 = aliceGroupCipher.encrypt("smert ze smert2".getBytes());
    byte[] ciphertextFromAlice3 = aliceGroupCipher.encrypt("smert ze smert3".getBytes());

    byte[] plaintextFromAlice   = bobGroupCipher.decrypt(ciphertextFromAlice);

    try {
      bobGroupCipher.decrypt(ciphertextFromAlice);
      throw new AssertionError("Should have ratcheted forward!");
    } catch (DuplicateMessageException dme) {
      // good
    }

    byte[] plaintextFromAlice2  = bobGroupCipher.decrypt(ciphertextFromAlice2);
    byte[] plaintextFromAlice3  = bobGroupCipher.decrypt(ciphertextFromAlice3);

    assertTrue(new String(plaintextFromAlice).equals("smert ze smert"));
    assertTrue(new String(plaintextFromAlice2).equals("smert ze smert2"));
    assertTrue(new String(plaintextFromAlice3).equals("smert ze smert3"));
  }

  public void testOutOfOrder()
      throws LegacyMessageException, DuplicateMessageException, InvalidMessageException, NoSessionException
  {
    InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
    InMemorySenderKeyStore bobStore   = new InMemorySenderKeyStore();

    GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore);
    GroupSessionBuilder bobSessionBuilder   = new GroupSessionBuilder(bobStore);

    GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, "groupWithBobInIt");
    GroupCipher bobGroupCipher   = new GroupCipher(bobStore, "groupWithBobInIt::aliceUserName");

    byte[]    aliceSenderKey        = KeyHelper.generateSenderKey();
    ECKeyPair aliceSenderSigningKey = KeyHelper.generateSenderSigningKey();
    int       aliceSenderKeyId      = KeyHelper.generateSenderKeyId();

    SenderKeyDistributionMessage aliceDistributionMessage =
        aliceSessionBuilder.process("groupWithBobInIt", aliceSenderKeyId, 0,
                                    aliceSenderKey, aliceSenderSigningKey);

    bobSessionBuilder.process("groupWithBobInIt::aliceUserName", aliceDistributionMessage);


    ArrayList<byte[]> ciphertexts = new ArrayList<>(100);

    for (int i=0;i<100;i++) {
      ciphertexts.add(aliceGroupCipher.encrypt("up the punks".getBytes()));
    }

    while (ciphertexts.size() > 0) {
      int    index      = randomInt() % ciphertexts.size();
      byte[] ciphertext = ciphertexts.remove(index);
      byte[] plaintext  = bobGroupCipher.decrypt(ciphertext);

      assertTrue(new String(plaintext).equals("up the punks"));
    }
  }

  public void testEncryptNoSession() {
    InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
    GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, "groupWithBobInIt");
    try {
      aliceGroupCipher.encrypt("up the punks".getBytes());
      throw new AssertionError("Should have failed!");
    } catch (NoSessionException nse) {
      // good
    }
  }

  private int randomInt() {
    try {
      return SecureRandom.getInstance("SHA1PRNG").nextInt(Integer.MAX_VALUE);
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    }
  }
}