Skip to content

Commit

Permalink
Use OAEPWithSHA256AndMGF1Padding for RSA, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ManfredKarrer committed Mar 30, 2016
1 parent 37b31a5 commit 513bc79
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Encryption {
private static final Logger log = LoggerFactory.getLogger(Encryption.class);

public static final String ASYM_KEY_ALGO = "RSA";
private static final String ASYM_CIPHER = "RSA/ECB/PKCS1Padding";
private static final String ASYM_CIPHER = "RSA/None/OAEPWithSHA256AndMGF1Padding";

private static final String SYM_KEY_ALGO = "AES";
private static final String SYM_CIPHER = "AES";
Expand Down
47 changes: 47 additions & 0 deletions common/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%highlight(%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n)</pattern>
</encoder>
</appender>

<root level="TRACE">
<appender-ref ref="CONSOLE_APPENDER"/>
</root>

<logger name="io.bitsquare.storage.Storage" level="WARN"/>
<logger name="io.bitsquare.storage.FileManager" level="WARN"/>
<logger name="io.bitsquare.locale.BSResources" level="ERROR"/>

<!-- <logger name="io.bitsquare.p2p.peers.PeerGroup" level="TRACE"/>
<logger name="io.bitsquare.p2p.P2PService" level="TRACE"/>
<logger name="io.bitsquare.p2p.storage.ProtectedExpirableDataStorage" level="TRACE"/>
<logger name="io.bitsquare.p2p.network.LocalhostNetworkNode" level="TRACE"/>
<logger name="io.bitsquare.p2p.network.TorNetworkNode" level="TRACE"/>
<logger name="io.bitsquare.p2p.network.NetworkNode" level="TRACE"/>-->


<!-- <logger name="com.msopentech.thali.toronionproxy.OnionProxyManagerEventHandler" level="WARN"/>
<logger name="io.bitsquare.btc.AddressBasedCoinSelector" level="WARN"/>
<logger name="io.bitsquare.storage.Storage" level="WARN"/>
<logger name="io.bitsquare.gui.util.Profiler" level="ERROR"/>
<logger name="io.bitsquare.temp.storage.RemoteStorage" level="WARN"/>
<logger name="io.bitsquare.storage.FileManager" level="WARN"/>
<logger name="org.bitcoinj" level="WARN"/>
<logger name="org.bitcoinj.core.BitcoinSerializer" level="WARN"/>
<logger name="org.bitcoinj.core.Peer" level="WARN"/>
<logger name="org.bitcoinj.core.HeadersMessage" level="WARN"/>
<logger name="org.bitcoinj.core.AbstractBlockChain" level="ERROR"/>-->

<logger name="com.msopentech.thali.toronionproxy.OnionProxyManagerEventHandler" level="INFO"/>
<logger name="org.bitcoinj" level="WARN"/>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.bitsquare.common.crypto;

import io.bitsquare.common.util.Utilities;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.CertificateException;
import java.util.Random;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class EncryptionTest {
private static final Logger log = LoggerFactory.getLogger(EncryptionTest.class);
private KeyRing keyRing;
private File dir;

@Before
public void setup() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, CryptoException {
Security.addProvider(new BouncyCastleProvider());
dir = File.createTempFile("temp_tests", "");
dir.delete();
dir.mkdir();
KeyStorage keyStorage = new KeyStorage(dir);
keyRing = new KeyRing(keyStorage);
}

@After
public void tearDown() throws IOException {
Utilities.deleteDirectory(dir);
}


@Test
public void testDecryptHybridWithSignature() {
long ts = System.currentTimeMillis();
log.trace("start ");
for (int i = 0; i < 100; i++) {
MockMessage payload = new MockMessage(new Random().nextInt());
SealedAndSigned sealedAndSigned = null;
try {
sealedAndSigned = Encryption.encryptHybridWithSignature(payload,
keyRing.getSignatureKeyPair(), keyRing.getPubKeyRing().getEncryptionPubKey());
} catch (CryptoException e) {
log.error("encryptHybridWithSignature failed");
e.printStackTrace();
assertTrue(false);
}
try {
DecryptedDataTuple tuple = Encryption.decryptHybridWithSignature(sealedAndSigned, keyRing.getEncryptionKeyPair().getPrivate());
assertEquals(((MockMessage) tuple.payload).nonce, payload.nonce);
} catch (CryptoException e) {
log.error("decryptHybridWithSignature failed");
e.printStackTrace();
assertTrue(false);
}
}
log.trace("took " + (System.currentTimeMillis() - ts) + " ms.");
}

private static class MockMessage implements Serializable {
public int nonce;

public MockMessage(int nonce) {
this.nonce = nonce;
}
}
}


68 changes: 68 additions & 0 deletions common/src/test/java/io/bitsquare/common/crypto/SigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.bitsquare.common.crypto;

import io.bitsquare.common.util.Utilities;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.CertificateException;
import java.util.Random;

import static org.junit.Assert.assertTrue;

public class SigTest {
private static final Logger log = LoggerFactory.getLogger(SigTest.class);
private KeyRing keyRing;
private File dir;

@Before
public void setup() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, CryptoException {
Security.addProvider(new BouncyCastleProvider());
dir = File.createTempFile("temp_tests", "");
dir.delete();
dir.mkdir();
KeyStorage keyStorage = new KeyStorage(dir);
keyRing = new KeyRing(keyStorage);
}

@After
public void tearDown() throws IOException {
Utilities.deleteDirectory(dir);
}


@Test
public void testSignature() {
long ts = System.currentTimeMillis();
log.trace("start ");
for (int i = 0; i < 100; i++) {
String msg = String.valueOf(new Random().nextInt());
String sig = null;
try {
sig = Sig.sign(keyRing.getSignatureKeyPair().getPrivate(), msg);
} catch (CryptoException e) {
log.error("sign failed");
e.printStackTrace();
assertTrue(false);
}
try {
assertTrue(Sig.verify(keyRing.getSignatureKeyPair().getPublic(), msg, sig));
} catch (CryptoException e) {
log.error("verify failed");
e.printStackTrace();
assertTrue(false);
}
}
log.trace("took " + (System.currentTimeMillis() - ts) + " ms.");
}
}


1 change: 0 additions & 1 deletion gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ public void run() {
.onClose(() -> Utilities.openWebPage("https://github.com/bitsquare/bitsquare/issues"))
.show());
}

}
};
checkCryptoThread.start();
Expand Down

0 comments on commit 513bc79

Please sign in to comment.