Skip to content

Commit 79f1242

Browse files
committed
AES used for encryption
1 parent ba6c485 commit 79f1242

File tree

7 files changed

+104
-100
lines changed

7 files changed

+104
-100
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.sharksystem.asap.ASAPException;
44
import net.sharksystem.asap.ASAPSecurityException;
5+
import net.sharksystem.crypto.ASAPBasicKeyStorage;
56

67
import java.io.ByteArrayInputStream;
78
import java.io.IOException;
@@ -10,13 +11,13 @@
1011
import java.util.List;
1112

1213
public class ASAP_Modem_Impl implements ASAP_1_0 {
13-
private final ASAPReadonlyKeyStorage signAndEncryptionKeyStorage;
14+
private final ASAPBasicKeyStorage signAndEncryptionKeyStorage;
1415

1516
public ASAP_Modem_Impl() {
1617
this.signAndEncryptionKeyStorage = null; // no key storage
1718
}
1819

19-
public ASAP_Modem_Impl(ASAPReadonlyKeyStorage signAndEncryptionKeyStorage) {
20+
public ASAP_Modem_Impl(ASAPBasicKeyStorage signAndEncryptionKeyStorage) {
2021
this.signAndEncryptionKeyStorage = signAndEncryptionKeyStorage;
2122
}
2223

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

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package net.sharksystem.asap.protocol;
22

3-
import net.sharksystem.Utils;
43
import net.sharksystem.asap.ASAPException;
54
import net.sharksystem.asap.ASAPSecurityException;
5+
import net.sharksystem.asap.protocol.ASAP_1_0;
6+
import net.sharksystem.asap.protocol.PDU_Impl;
7+
import net.sharksystem.crypto.ASAPBasicKeyStorage;
68

7-
import javax.crypto.BadPaddingException;
8-
import javax.crypto.Cipher;
9-
import javax.crypto.IllegalBlockSizeException;
10-
import javax.crypto.NoSuchPaddingException;
9+
import javax.crypto.*;
10+
import javax.crypto.spec.SecretKeySpec;
1111
import java.io.*;
1212
import java.security.*;
1313

1414
class CryptoSession {
15-
public static final int MAX_ENCRYPTION_BLOCK_SIZE = 240;
15+
private static final int MAX_ENCRYPTION_BLOCK_SIZE = 128;
1616
private Signature signature;
1717
private CharSequence recipient;
18-
private ASAPReadonlyKeyStorage keyStorage;
18+
private ASAPBasicKeyStorage keyStorage;
1919
private Cipher cipher = null;
2020
private PublicKey publicKey;
2121
private byte cmd;
@@ -25,13 +25,13 @@ class CryptoSession {
2525
private ByteArrayOutputStream asapMessageOS;
2626
private InputStreamCopy verifyStream;
2727

28-
CryptoSession(ASAPReadonlyKeyStorage keyStorage) {
28+
CryptoSession(ASAPBasicKeyStorage keyStorage) {
2929
this.keyStorage = keyStorage;
3030
}
3131

3232
CryptoSession(byte cmd, OutputStream os, boolean sign, boolean encrypted,
3333
CharSequence recipient,
34-
ASAPReadonlyKeyStorage keyStorage)
34+
ASAPBasicKeyStorage keyStorage)
3535
throws ASAPSecurityException {
3636

3737
this.cmd = cmd;
@@ -113,7 +113,7 @@ byte getCMD() {
113113
return this.cmd;
114114
}
115115

116-
OutputStream getOutputStream() {
116+
public OutputStream getOutputStream() {
117117
return this.effectivOS;
118118
}
119119

@@ -160,20 +160,35 @@ public void finish() throws ASAPSecurityException {
160160
// encrypted asap message
161161
byte[] asapMessageAsBytes = this.asapMessageOS.toByteArray();
162162

163-
// TODO: create AES key, encrypt with RSA, send and encrypt rest with that AES key
163+
// get symmetric key
164+
SecretKey encryptionKey = this.keyStorage.generateSymmetricKey();
165+
byte[] encodedSymmetricKey = encryptionKey.getEncoded();
164166

165-
// that stuff will not work.
166-
int i = 0;
167-
while(i + MAX_ENCRYPTION_BLOCK_SIZE < asapMessageAsBytes.length) {
168-
this.cipher.update(asapMessageAsBytes, i, MAX_ENCRYPTION_BLOCK_SIZE);
169-
i += MAX_ENCRYPTION_BLOCK_SIZE;
170-
}
167+
// encrypt key
168+
byte[] encryptedSymmetricKeyBytes = this.cipher.doFinal(encodedSymmetricKey);
169+
170+
// send encrypted key
171+
this.writeByteArray(encryptedSymmetricKeyBytes, this.realOS);
172+
173+
// encrypt message with symmetric key
174+
try {
175+
this.cipher = Cipher.getInstance(keyStorage.getSymmetricEncryptionAlgorithm());
176+
this.cipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
177+
178+
// block by block
179+
int i = 0;
180+
while(i + MAX_ENCRYPTION_BLOCK_SIZE < asapMessageAsBytes.length) {
181+
this.cipher.update(asapMessageAsBytes, i, MAX_ENCRYPTION_BLOCK_SIZE);
182+
i += MAX_ENCRYPTION_BLOCK_SIZE;
183+
}
171184

172-
int lastStepLen = asapMessageAsBytes.length - i;
173-
byte[] encryptedBytes = this.cipher.doFinal(asapMessageAsBytes, i, lastStepLen);
185+
int lastStepLen = asapMessageAsBytes.length - i;
186+
byte[] encryptedContent = this.cipher.doFinal(asapMessageAsBytes, i, lastStepLen);
174187

175-
this.writeByteArray(encryptedBytes, this.realOS);
176-
this.realOS.write(encryptedBytes);
188+
this.writeByteArray(encryptedContent, this.realOS);
189+
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException e) {
190+
throw new ASAPSecurityException(this.getLogStart(), e);
191+
}
177192
} catch (IllegalBlockSizeException | BadPaddingException | IOException e) {
178193
throw new ASAPSecurityException(this.getLogStart(), e);
179194
}
@@ -236,30 +251,32 @@ public boolean verify(String sender, InputStream is) throws IOException, ASAPExc
236251

237252
////////////////////////////////// decrypt
238253

239-
InputStream decrypt(InputStream is) throws ASAPSecurityException {
254+
public InputStream decrypt(InputStream is) throws ASAPSecurityException {
240255
return this.decrypt(is, this.keyStorage.getPrivateKey());
241256
}
242257

243-
// parameter private key is usually no option. There is just one - burt was good for debugging
258+
// parameter private key is usually not an option. Good entry for testing / debugging, though
244259
private InputStream decrypt(InputStream is, PrivateKey privateKey) throws ASAPSecurityException {
245260
try {
261+
// read encrypted symmetric key
262+
byte[] encryptedSymmetricKey = this.readByteArray(is);
263+
264+
// decrypt encoded symmetric key
246265
this.cipher = Cipher.getInstance(keyStorage.getRSAEncryptionAlgorithm());
247266
this.cipher.init(Cipher.DECRYPT_MODE, privateKey);
267+
byte[] encodedSymmetricKey = this.cipher.doFinal(encryptedSymmetricKey);
248268

249-
// read encrypted message
250-
byte[] messageBytes = this.readByteArray(is);
251-
252-
/*
253-
// read len
254-
int len = PDU_Impl.readIntegerParameter(is);
255-
byte[] messageBytes = new byte[len];
269+
// create symmetric key object
270+
SecretKey symmetricKey =
271+
new SecretKeySpec(encodedSymmetricKey, this.keyStorage.getSymmetricKeyType());
256272

257-
// read encrypted bytes from stream
258-
is.read(messageBytes);
259-
*/
273+
// read content
274+
byte[] encryptedContent = this.readByteArray(is);
260275

261-
// decrypt
262-
byte[] decryptedBytes = this.cipher.doFinal(messageBytes);
276+
// decrypt content
277+
Cipher symmetricCipher = Cipher.getInstance(keyStorage.getSymmetricEncryptionAlgorithm());
278+
symmetricCipher.init(Cipher.DECRYPT_MODE, symmetricKey);
279+
byte[] decryptedBytes = symmetricCipher.doFinal(encryptedContent);
263280
return new ByteArrayInputStream(decryptedBytes);
264281
} catch (BadPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException |
265282
NoSuchPaddingException | InvalidKeyException | IOException | ASAPException e) {

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

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ protected static void sendHeader(byte cmd, int flags, OutputStream os) throws IO
8989
PDU_Impl.sendByteParameter((byte)flags, os); // mand
9090
}
9191

92-
protected static void sendFlags(int flags, OutputStream os) throws IOException {
92+
public static void sendFlags(int flags, OutputStream os) throws IOException {
9393
PDU_Impl.sendByteParameter((byte)flags, os); // mand
9494
}
9595

96-
protected static void sendCmd(byte cmd, OutputStream os) throws IOException {
96+
public static void sendCmd(byte cmd, OutputStream os) throws IOException {
9797
PDU_Impl.sendByteParameter(cmd, os); // mand
9898
}
9999

@@ -106,58 +106,6 @@ protected void evaluateFlags(int flag) {
106106
this.eraTo = flagSet(ERA_TO_BIT_POSITION, flag);
107107
this.offsetsSet = flagSet(OFFSETS_BIT_POSITION, flag);
108108
this.signed = flagSet(SIGNED_TO_BIT_POSITION, flag);
109-
110-
/*
111-
// sender parameter set ?
112-
int testFlag = 1;
113-
testFlag = testFlag << SENDER_BIT_POSITION;
114-
int result = flag & testFlag;
115-
senderSet = result != 0;
116-
117-
// recipient peer parameter set ?
118-
testFlag = 1;
119-
testFlag = testFlag << RECIPIENT_BIT_POSITION;
120-
result = flag & testFlag;
121-
recipientSet = result != 0;
122-
123-
124-
// channel parameter set ?
125-
testFlag = 1;
126-
testFlag = testFlag << CHANNEL_BIT_POSITION;
127-
result = flag & testFlag;
128-
channelSet = result != 0;
129-
130-
// era parameter set ?
131-
testFlag = 1;
132-
testFlag = testFlag << ERA_BIT_POSITION;
133-
result = flag & testFlag;
134-
eraSet = result != 0;
135-
136-
// era from parameter set ?
137-
testFlag = 1;
138-
testFlag = testFlag << ERA_FROM_BIT_POSITION;
139-
result = flag & testFlag;
140-
eraFrom = result != 0;
141-
142-
// era from parameter set ?
143-
testFlag = 1;
144-
testFlag = testFlag << ERA_TO_BIT_POSITION;
145-
result = flag & testFlag;
146-
eraTo = result != 0;
147-
148-
// offsets parameter set ?
149-
testFlag = 1;
150-
testFlag = testFlag << OFFSETS_BIT_POSITION;
151-
result = flag & testFlag;
152-
offsetsSet = result != 0;
153-
154-
// signed parameter set ?
155-
testFlag = 1;
156-
testFlag = testFlag << SIGNED_TO_BIT_POSITION;
157-
result = flag & testFlag;
158-
this.signed = result != 0;
159-
*/
160-
161109
}
162110

163111
static boolean flagSet(int bitPosition, int flags) {

src/net/sharksystem/asap/protocol/ASAPReadonlyKeyStorage.java renamed to src/net/sharksystem/crypto/ASAPBasicKeyStorage.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package net.sharksystem.asap.protocol;
1+
package net.sharksystem.crypto;
22

33
import net.sharksystem.asap.ASAPSecurityException;
44

5+
import javax.crypto.SecretKey;
56
import java.security.PrivateKey;
67
import java.security.PublicKey;
78

8-
public interface ASAPReadonlyKeyStorage {
9+
public interface ASAPBasicKeyStorage {
910
/**
1011
*
1112
* @return private key of local device - for signing
@@ -33,4 +34,10 @@ public interface ASAPReadonlyKeyStorage {
3334
String getRSAEncryptionAlgorithm();
3435

3536
String getRSASigningAlgorithm();
37+
38+
SecretKey generateSymmetricKey() throws ASAPSecurityException;
39+
40+
String getSymmetricEncryptionAlgorithm();
41+
42+
String getSymmetricKeyType();
3643
}

test/net/sharksystem/asap/protocol/TestASAPKeyStorage.java renamed to src/net/sharksystem/crypto/TestASAPKeyStorage.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
package net.sharksystem.asap.protocol;
1+
package net.sharksystem.crypto;
22

33
import net.sharksystem.asap.ASAPSecurityException;
44
import net.sharksystem.asap.util.Log;
55

6+
import javax.crypto.KeyGenerator;
7+
import javax.crypto.SecretKey;
68
import java.security.*;
79
import java.util.HashMap;
810

9-
public class TestASAPKeyStorage implements ASAPReadonlyKeyStorage {
11+
public class TestASAPKeyStorage implements ASAPBasicKeyStorage {
1012
private final KeyPair keyPair;
1113
private final String name;
1214
private long timeInMillis = 0;
1315

1416
public static final String DEFAULT_RSA_ENCRYPTION_ALGORITHM = "RSA/ECB/PKCS1Padding";
15-
public static final String DEFAULT_SIGNATURE_METHOD = "SHA256withRSA";
17+
public static final String DEFAULT_SYMMETRIC_KEY_TYPE = "AES";
18+
public static final String DEFAULT_SYMMETRIC_ENCRYPTION_ALGORITHM = "AES/ECB/PKCS5Padding";
19+
// public static int DEFAULT_AES_KEY_SIZE = 256; TODO we need a better one
20+
public static int DEFAULT_AES_KEY_SIZE = 128;
21+
public static final String DEFAULT_SIGNATURE_ALGORITHM = "SHA256withRSA";
1622

1723
private HashMap<String, KeyPair> peerKeyPairs = new HashMap<>();
1824

19-
TestASAPKeyStorage(String name) throws ASAPSecurityException {
25+
public TestASAPKeyStorage(String name) throws ASAPSecurityException {
2026
// generate owners key pair;
2127
this.name = name;
2228
this.keyPair = this.generateKeyPair();
@@ -28,6 +34,21 @@ public TestASAPKeyStorage(String name, KeyPair ownerKeyPair) {
2834
this.keyPair = ownerKeyPair;
2935
}
3036

37+
public SecretKey generateSymmetricKey() throws ASAPSecurityException {
38+
try {
39+
KeyGenerator gen = KeyGenerator.getInstance(DEFAULT_SYMMETRIC_KEY_TYPE);
40+
gen.init(DEFAULT_AES_KEY_SIZE);
41+
SecretKey secretKey = gen.generateKey();
42+
return secretKey;
43+
} catch (NoSuchAlgorithmException e) {
44+
throw new ASAPSecurityException(this.getLogStart(), e);
45+
}
46+
}
47+
48+
private String getLogStart() {
49+
return this.getClass().getSimpleName() + ": ";
50+
}
51+
3152
private KeyPair generateKeyPair() throws ASAPSecurityException {
3253
Log.writeLog(this, "create key pair");
3354
KeyPairGenerator keyGen = null;
@@ -97,9 +118,19 @@ public String getRSAEncryptionAlgorithm() {
97118
return DEFAULT_RSA_ENCRYPTION_ALGORITHM;
98119
}
99120

121+
@Override
122+
public String getSymmetricEncryptionAlgorithm() {
123+
return DEFAULT_SYMMETRIC_ENCRYPTION_ALGORITHM;
124+
}
125+
126+
@Override
127+
public String getSymmetricKeyType() {
128+
return DEFAULT_SYMMETRIC_KEY_TYPE;
129+
}
130+
100131
@Override
101132
public String getRSASigningAlgorithm() {
102-
return DEFAULT_SIGNATURE_METHOD;
133+
return DEFAULT_SIGNATURE_ALGORITHM;
103134
}
104135

105136
public void addKeyPair(String peerID, KeyPair keyPair) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sharksystem.asap.protocol;
22

33
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.crypto.TestASAPKeyStorage;
45
import org.junit.Assert;
56
import org.junit.Test;
67

@@ -9,8 +10,6 @@
910
import java.util.ArrayList;
1011
import java.util.List;
1112

12-
import static net.sharksystem.asap.protocol.ASAP_1_0.ERA_NOT_DEFINED;
13-
1413
public class PDUTests {
1514
public static final String ALICE_ID = "Alice";
1615
public static final String BOB_ID = "Bob";

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sharksystem.asap.protocol;
22

33
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.crypto.TestASAPKeyStorage;
45
import org.junit.Assert;
56
import org.junit.Test;
67

0 commit comments

Comments
 (0)