Skip to content

Commit

Permalink
Android N fix + cryptography with proper IV fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aw4y committed Jun 14, 2016
1 parent d18fd6b commit 08246e1
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 82 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "net.rehacktive.waspdbexample"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
}
buildTypes {
release {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.android.tools.build:gradle:2.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jul 27 17:20:00 BST 2015
#Tue May 10 18:13:21 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class MainTest extends InstrumentationTestCase {

String path;
String dbName = "justAtestDb";
String dbName = "justAtestDb2";
String dbPwd = "passw0rd!";

Context ctx;
Expand Down
2 changes: 1 addition & 1 deletion waspdb/src/main/java/net/rehacktive/waspdb/WaspDb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.rehacktive.waspdb;

import net.rehacktive.waspdb.internals.cryptolayer.CipherManager;
import net.rehacktive.waspdb.internals.collision.CipherManager;
import net.rehacktive.waspdb.internals.utils.Utils;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.os.AsyncTask;

import net.rehacktive.waspdb.internals.collision.KryoStoreUtils;
import net.rehacktive.waspdb.internals.cryptolayer.CipherManager;
import net.rehacktive.waspdb.internals.collision.CipherManager;
import net.rehacktive.waspdb.internals.utils.Salt;
import net.rehacktive.waspdb.internals.utils.Utils;

Expand Down
6 changes: 4 additions & 2 deletions waspdb/src/main/java/net/rehacktive/waspdb/WaspHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.rehacktive.waspdb.internals.collision.CollisionHash;
import net.rehacktive.waspdb.internals.collision.exceptions.KeyNotFoundException;
import net.rehacktive.waspdb.internals.cryptolayer.CipherManager;
import net.rehacktive.waspdb.internals.collision.CipherManager;

import org.apache.commons.io.FileUtils;

Expand All @@ -29,13 +29,15 @@ protected WaspHash(CipherManager cipherManager, String path) {
* @param key the Object key
* @param value the Object value
*/
public void put(Object key, Object value) {
public Boolean put(Object key, Object value) {
try {
hash.updateObject(key, value);
notifyObservers();
return true;
}
catch(Exception e) {
e.printStackTrace();
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.rehacktive.waspdb.internals.cryptolayer;
package net.rehacktive.waspdb.internals.collision;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
Expand All @@ -11,9 +11,11 @@
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;


/**
* Created by stefano on 06/08/2014.
*/
Expand All @@ -22,14 +24,24 @@ public class CipherManager {
private int ITERATIONS = 10000;
private int KEYSIZE = 256;

public static String algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC";
public static String cipher_algorithm = "AES/CBC/PKCS7PADDING";
public static String key_algorithm = "PBKDF2WithHmacSHA1";
public static String secretKeyAlgorithm = "AES";

protected Key key;

protected static CipherManager instance = null;

private CipherManager() {
// Exists only to defeat instantiation.
// Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
// Provider[] providers = Security.getProviders();
// for (Provider provider : providers) {
// Log.i("CRYPTO","provider: "+provider.getName());
// Set<Provider.Service> services = provider.getServices();
// for (Provider.Service service : services) {
// Log.i("CRYPTO"," key_algorithm: "+service.getAlgorithm());
// }
// }
}

public static CipherManager getInstance(char[] p, byte[] s) {
Expand All @@ -47,18 +59,31 @@ public static CipherManager getInstance(char[] p, byte[] s) {
}

private void generateSK(char[] passPhrase, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(key_algorithm);

KeySpec spec = new PBEKeySpec(passPhrase,salt,ITERATIONS, KEYSIZE);
SecretKey secretKey = secretKeyFactory.generateSecret(spec);

key = new SecretKeySpec(secretKey.getEncoded(), algorithm);
key = new SecretKeySpec(secretKey.getEncoded(), secretKeyAlgorithm);
}

protected Cipher getEncCipher() {
try {
Cipher cipher = Cipher.getInstance(cipher_algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);

return cipher;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}

protected Cipher getCipher(int mode) {
protected Cipher getDecCipher(byte[] iv) {
try {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(mode, key);
Cipher cipher = Cipher.getInstance(cipher_algorithm);
IvParameterSpec ivParams = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, ivParams);

return cipher;
}catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import net.rehacktive.waspdb.internals.collision.exceptions.KeyAlreadyExistsException;
import net.rehacktive.waspdb.internals.collision.exceptions.KeyNotFoundException;
import net.rehacktive.waspdb.internals.cryptolayer.CipherManager;
import net.rehacktive.waspdb.internals.utils.Utils;

import org.apache.commons.io.FileUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import net.rehacktive.waspdb.internals.cryptolayer.AESSerializer;
import net.rehacktive.waspdb.internals.cryptolayer.CipherManager;
import net.rehacktive.waspdb.internals.collision.exceptions.WaspDataPage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.crypto.Cipher;


public class KryoStoreUtils {

private static String TAG = "KRYOSTORE";
private static Kryo kryoInstance;

private static Kryo getKryoInstance() {
if(kryoInstance==null)
if(kryoInstance==null) {
kryoInstance = new Kryo();

kryoInstance.register(WaspDataPage.class);
}
return kryoInstance;
}

Expand All @@ -32,12 +34,15 @@ public static void serializeToDisk(Object obj, String filename, CipherManager ci
//Log.d(TAG,start+": starting serializeToDisk with password");

Output output = new Output(new FileOutputStream(filename));
WaspDataPage dataPage = new WaspDataPage();
if(cipherManager!=null) {
AESSerializer aes = new AESSerializer(getKryoInstance().getSerializer(obj.getClass()), cipherManager);
aes.write(getKryoInstance(), output, obj);
Cipher cipher = cipherManager.getEncCipher();
dataPage.setIv(cipher.getIV());
dataPage.setData(cipher.doFinal(serialize(obj)));
} else {
getKryoInstance().writeObject(output, obj);
dataPage.setData(serialize(obj));
}
getKryoInstance().writeObject(output, dataPage);
output.close();

//Long end = System.currentTimeMillis();
Expand All @@ -58,12 +63,13 @@ public static Object readFromDisk(String filename, Class type, CipherManager cip
Object hash;
if(f.exists()) {
Input input = new Input(new FileInputStream(f));
WaspDataPage dataPage = getKryoInstance().readObject(input, WaspDataPage.class);
if(cipherManager!=null) {
AESSerializer aes = new AESSerializer(getKryoInstance().getDefaultSerializer(type), cipherManager);
hash = aes.read(getKryoInstance(), input, type);
Cipher decipher = cipherManager.getDecCipher(dataPage.getIv());
hash = unserialize(decipher.doFinal(dataPage.getData()),type);
}
else {
hash = getKryoInstance().readObject(input, type);
hash = unserialize(dataPage.getData(),type);
}
input.close();

Expand All @@ -89,6 +95,11 @@ public static byte[] serialize(Object o) {
getKryoInstance().writeObject(output, o);
return output.toBytes();
}

public static Object unserialize(byte[] buffer, Class type) {
Input input = new Input(buffer);
return getKryoInstance().readObject(input, type);
}

// public static Object cloneObject(Object obj) {
// return getKryoInstance().copy(obj);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.rehacktive.waspdb.internals.collision.exceptions;

/**
* Created by stefano on 14/06/2016.
*/

public class WaspDataPage {

private byte[] iv;
private byte[] data;

public byte[] getIv() {
return iv;
}

public void setIv(byte[] iv) {
this.iv = iv;
}

public byte[] getData() {
return data;
}

public void setData(byte[] data) {
this.data = data;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.rehacktive.waspdb.internals.utils;

import net.rehacktive.waspdb.internals.cryptolayer.CipherManager;
import net.rehacktive.waspdb.internals.collision.CipherManager;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -49,7 +49,7 @@ public static boolean checkForCryptoAvailable() {
// Security.addProvider(new BouncyCastleProvider());
// for(String s : Security.getAlgorithms("Cipher"))
// System.out.println(s);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(CipherManager.algorithm);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(CipherManager.key_algorithm);
return true;
} catch (NoSuchAlgorithmException e) {
return false;
Expand Down

0 comments on commit 08246e1

Please sign in to comment.