Skip to content

Commit

Permalink
update secure cell tests to use base64; fix wrapper to accept null co…
Browse files Browse the repository at this point in the history
…ntext;
  • Loading branch information
vixentael committed Nov 8, 2017
1 parent 887302f commit 18597a1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
Expand Up @@ -192,7 +192,7 @@ public SecureCellData protect(byte[] context, byte[] data) throws NullArgumentEx
* @throws UnsupportedEncodingException when UTF-16 decoding is not supported
*/
public SecureCellData protect(String password, String context, byte[] data) throws UnsupportedEncodingException, NullArgumentException, SecureCellException {
return this.protect(password.getBytes(CHARSET), context.getBytes(CHARSET), data);
return this.protect(password.getBytes(CHARSET), (context == null) ? null : context.getBytes(CHARSET), data);
}

/**
Expand All @@ -205,7 +205,7 @@ public SecureCellData protect(String password, String context, byte[] data) thro
* @throws UnsupportedEncodingException when UTF-16 decoding is not supported
*/
public SecureCellData protect(String context, byte[] data) throws UnsupportedEncodingException, NullArgumentException, SecureCellException {
return this.protect(this.key, context.getBytes(CHARSET), data);
return this.protect(this.key, (context == null) ? null : context.getBytes(CHARSET), data);
}

/**
Expand Down Expand Up @@ -247,7 +247,7 @@ public byte[] unprotect(byte[] context, SecureCellData protectedData) throws Nul
* @throws UnsupportedEncodingException when UTF-16 decoding is not supported
*/
public byte[] unprotect(String password, String context, SecureCellData protectedData) throws UnsupportedEncodingException, NullArgumentException, SecureCellException, InvalidArgumentException {
return this.unprotect(password.getBytes(CHARSET), context.getBytes(CHARSET), protectedData);
return this.unprotect(password.getBytes(CHARSET), (context == null) ? null : context.getBytes(CHARSET), protectedData);
}

/**
Expand All @@ -261,6 +261,6 @@ public byte[] unprotect(String password, String context, SecureCellData protecte
* @throws UnsupportedEncodingException when UTF-16 decoding is not supported
*/
public byte[] unprotect(String context, SecureCellData protectedData) throws UnsupportedEncodingException, NullArgumentException, SecureCellException, InvalidArgumentException {
return this.unprotect(this.key, context.getBytes(CHARSET), protectedData);
return this.unprotect(this.key, (context == null) ? null : context.getBytes(CHARSET), protectedData);
}
}
Expand Up @@ -18,11 +18,14 @@

import java.util.Arrays;
import java.util.Random;
import java.nio.charset.StandardCharsets;

import com.cossacklabs.themis.SecureCell;
import com.cossacklabs.themis.SecureCellData;

import android.test.AndroidTestCase;
import android.util.Base64;
import android.util.Log;

public class SecureCellTest extends AndroidTestCase {

Expand All @@ -45,7 +48,9 @@ private byte[] generateTestData() {
@Override
public void runTest() {
try {
testBase64Decoding();
testSeal();
testSealWithStrings();
testTokenProtect();
testContextImprint();
} catch (Exception e) {
Expand All @@ -59,6 +64,19 @@ public void runTest() {
}
}

void testBase64Decoding()throws Exception {
String charset = StandardCharsets.UTF_8.name();
String normalString = "i'm super normal string hello";
byte[] normalData = normalString.getBytes(charset);
String base64Normal = Base64.encodeToString(normalData, Base64.NO_WRAP);

byte[] base64Data = Base64.decode(base64Normal, Base64.NO_WRAP);
String base64Decoded = new String(base64Data, charset);

Log.d("SMC", "normalString = " + normalString + "\nbase64Normal = " + base64Normal + "\nbase64Decoded = " + base64Decoded);
assertTrue(normalString.equals(base64Decoded));
}

void testSeal() throws Exception {
String key = "seal key";
String context = "seal context";
Expand All @@ -77,9 +95,64 @@ void testSeal() throws Exception {

cell = new SecureCell(key);
byte[] unprotectedData = cell.unprotect(context, protectedData);
assertNotNull(unprotectedData);

assertTrue(Arrays.equals(data, unprotectedData));
}

void testSealWithStrings() throws Exception {
String key = "seal key";
String context = "seal context";
String stringToEncrypt = "some random data to encrypt";
String charsetUTF8 = StandardCharsets.UTF_8.name();
String charsetUTF16 = StandardCharsets.UTF_16.name();

runTestSealWithStrings(key, context, stringToEncrypt, charsetUTF8);
runTestSealWithStrings(key, context, stringToEncrypt, charsetUTF16);

runTestSealWithStrings(key, null, stringToEncrypt, charsetUTF8);
runTestSealWithStrings(key, null, stringToEncrypt, charsetUTF16);
}

void runTestSealWithStrings(String key, String context, String stringToEncrypt, String charset) throws Exception {
String encryptedString = encryptStringFromExternalSource(key, context, stringToEncrypt, charset);
String decryptedString = decryptStringFromExternalSource(key, context, encryptedString, charset);
assertTrue(decryptedString.equals(stringToEncrypt));
}

String encryptStringFromExternalSource(String key, String context, String originalString, String charset) throws Exception {
byte[] data = originalString.getBytes(charset);

SecureCell cell = new SecureCell(key);
assertNotNull(cell);

SecureCellData protectedData = cell.protect(context, data);

assertNotNull(protectedData);
assertNotNull(protectedData.getProtectedData());
assertNull(protectedData.getAdditionalData());

assertTrue(protectedData.getProtectedData().length > data.length);

// encrypted data to base64
String protectedDataBytesString = Base64.encodeToString(protectedData.getProtectedData(), Base64.NO_WRAP);
return protectedDataBytesString;
}

String decryptStringFromExternalSource(String key, String context, String stringToDecrypt, String charset) throws Exception {
SecureCell cell = new SecureCell(key);
assertNotNull(cell);

byte[] encryptedDataBytesFromString = Base64.decode(stringToDecrypt, Base64.NO_WRAP);
SecureCellData encryptedDataFromString = new SecureCellData(encryptedDataBytesFromString, null);

// try to decrypted converted
byte[] unprotectedDataFromString = cell.unprotect(context, encryptedDataFromString);
assertNotNull(unprotectedDataFromString);

String unprotectedString = new String(unprotectedDataFromString, charset);
return unprotectedString;
}

void testTokenProtect() throws Exception {
String key = "token protect key";
Expand Down Expand Up @@ -124,4 +197,4 @@ void testContextImprint() throws Exception {

assertTrue(Arrays.equals(data, unprotectedData));
}
}
}

0 comments on commit 18597a1

Please sign in to comment.