Skip to content

Commit f6253be

Browse files
committed
Encryption and signing works in three simple tests. Need some test with larger data. Largest message length was 299 byte so far.
1 parent 79f1242 commit f6253be

File tree

3 files changed

+49
-44
lines changed

3 files changed

+49
-44
lines changed

src/net/sharksystem/asap/protocol/CryptoSession.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,9 @@ public void finish() throws ASAPSecurityException {
155155
}
156156
}
157157

158+
// must be after signing
158159
if(this.cipher != null) {
159160
try {
160-
// encrypted asap message
161-
byte[] asapMessageAsBytes = this.asapMessageOS.toByteArray();
162-
163161
// get symmetric key
164162
SecretKey encryptionKey = this.keyStorage.generateSymmetricKey();
165163
byte[] encodedSymmetricKey = encryptionKey.getEncoded();
@@ -170,21 +168,28 @@ public void finish() throws ASAPSecurityException {
170168
// send encrypted key
171169
this.writeByteArray(encryptedSymmetricKeyBytes, this.realOS);
172170

171+
// get maybe signed asap message
172+
byte[] asapMessageAsBytes = this.asapMessageOS.toByteArray();
173+
173174
// encrypt message with symmetric key
174175
try {
175-
this.cipher = Cipher.getInstance(keyStorage.getSymmetricEncryptionAlgorithm());
176-
this.cipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
176+
Cipher symmetricCipher = Cipher.getInstance(keyStorage.getSymmetricEncryptionAlgorithm());
177+
symmetricCipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
177178

179+
/*
178180
// block by block
179181
int i = 0;
180182
while(i + MAX_ENCRYPTION_BLOCK_SIZE < asapMessageAsBytes.length) {
181-
this.cipher.update(asapMessageAsBytes, i, MAX_ENCRYPTION_BLOCK_SIZE);
183+
symmetricCipher.update(asapMessageAsBytes, i, MAX_ENCRYPTION_BLOCK_SIZE);
182184
i += MAX_ENCRYPTION_BLOCK_SIZE;
183185
}
184186
185187
int lastStepLen = asapMessageAsBytes.length - i;
186-
byte[] encryptedContent = this.cipher.doFinal(asapMessageAsBytes, i, lastStepLen);
188+
symmetricCipher.update(asapMessageAsBytes, i, lastStepLen);
189+
byte[] encryptedContent = symmetricCipher.doFinal();
190+
*/
187191

192+
byte[] encryptedContent = symmetricCipher.doFinal(asapMessageAsBytes);
188193
this.writeByteArray(encryptedContent, this.realOS);
189194
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException e) {
190195
throw new ASAPSecurityException(this.getLogStart(), e);

test/net/sharksystem/asap/protocol/PDUTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,43 @@ public void sendAndReceiveInterestSigned() throws IOException, ASAPException {
181181
Assert.assertTrue(interestPDU.verified());
182182
}
183183

184+
@Test
185+
public void sendAndReceiveInterestSignedAndEncrypted() throws IOException, ASAPException {
186+
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage(ALICE_ID);
187+
188+
// add Bob
189+
KeyPair bobKeyPair = keyStorageAlice.createTestPeer(BOB_ID);
190+
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(BOB_ID,bobKeyPair);
191+
keyStorageBob.addKeyPair(ALICE_ID, keyStorageAlice.getKeyPair());
192+
193+
ASAP_1_0 asapModemAlice = new ASAP_Modem_Impl(keyStorageAlice);
194+
ASAP_1_0 asapModemBob = new ASAP_Modem_Impl(keyStorageBob);
195+
196+
String sender = ALICE_ID;
197+
String recipient = BOB_ID;
198+
String channel = "AliceURI";
199+
String format = "format";
200+
201+
ByteArrayOutputStream os = new ByteArrayOutputStream();
202+
203+
/////////////////////// encrypted
204+
asapModemAlice.interest(sender, recipient, format, channel, os,true, true);
205+
206+
// try t read output
207+
InputStream is = new ByteArrayInputStream(os.toByteArray());
208+
209+
ASAP_PDU_1_0 asap_pdu_1_0 = asapModemBob.readPDU(is);
210+
211+
ASAP_Interest_PDU_1_0 interestPDU = (ASAP_Interest_PDU_1_0) asap_pdu_1_0;
212+
213+
Assert.assertTrue(interestPDU.getChannelUri().equalsIgnoreCase(channel));
214+
Assert.assertTrue(interestPDU.getFormat().equalsIgnoreCase(format));
215+
Assert.assertTrue(interestPDU.getSender().equalsIgnoreCase(sender));
216+
Assert.assertTrue(interestPDU.getRecipient().equalsIgnoreCase(recipient));
217+
Assert.assertTrue(interestPDU.encrypted());
218+
Assert.assertTrue(interestPDU.verified());
219+
}
220+
184221
//////////////////// assimilate /////////////////////////////////////////
185222
@Test
186223
public void sendAndReceiveAssimilate() throws IOException, ASAPException {

test/net/sharksystem/asap/protocol/Workbench.java

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,4 @@ public class Workbench {
1515
public static final String ALICE_ID = "Alice";
1616
public static final String BOB_ID = "Bob";
1717

18-
19-
@Test
20-
public void sendAndReceiveInterestSignedAndEncrypted() throws IOException, ASAPException {
21-
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage(ALICE_ID);
22-
23-
// add Bob
24-
KeyPair bobKeyPair = keyStorageAlice.createTestPeer(BOB_ID);
25-
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(BOB_ID,bobKeyPair);
26-
keyStorageBob.addKeyPair(ALICE_ID, keyStorageAlice.getKeyPair());
27-
28-
ASAP_1_0 asapModemAlice = new ASAP_Modem_Impl(keyStorageAlice);
29-
ASAP_1_0 asapModemBob = new ASAP_Modem_Impl(keyStorageBob);
30-
31-
String sender = ALICE_ID;
32-
String recipient = BOB_ID;
33-
String channel = "AliceURI";
34-
String format = "format";
35-
36-
ByteArrayOutputStream os = new ByteArrayOutputStream();
37-
38-
/////////////////////// encrypted
39-
asapModemAlice.interest(sender, recipient, format, channel, os,true, true);
40-
41-
// try t read output
42-
InputStream is = new ByteArrayInputStream(os.toByteArray());
43-
44-
ASAP_PDU_1_0 asap_pdu_1_0 = asapModemBob.readPDU(is);
45-
46-
ASAP_Interest_PDU_1_0 interestPDU = (ASAP_Interest_PDU_1_0) asap_pdu_1_0;
47-
48-
Assert.assertTrue(interestPDU.getChannelUri().equalsIgnoreCase(channel));
49-
Assert.assertTrue(interestPDU.getFormat().equalsIgnoreCase(format));
50-
Assert.assertTrue(interestPDU.getSender().equalsIgnoreCase(sender));
51-
Assert.assertTrue(interestPDU.getRecipient().equalsIgnoreCase(recipient));
52-
Assert.assertTrue(interestPDU.encrypted());
53-
Assert.assertTrue(interestPDU.verified());
54-
}
5518
}

0 commit comments

Comments
 (0)