1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/tests/background/junit3/src/nativecode/test/TestNativeCrypto.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,175 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +package org.mozilla.gecko.background.nativecode.test; 1.8 + 1.9 +import java.io.UnsupportedEncodingException; 1.10 +import java.security.GeneralSecurityException; 1.11 +import java.security.MessageDigest; 1.12 +import java.security.NoSuchAlgorithmException; 1.13 +import java.util.Arrays; 1.14 + 1.15 +import junit.framework.TestCase; 1.16 + 1.17 +import org.mozilla.gecko.background.nativecode.NativeCrypto; 1.18 +import org.mozilla.gecko.sync.Utils; 1.19 + 1.20 +/* 1.21 + * Tests the Java wrapper over native implementations of crypto code. Test vectors from: 1.22 + * * PBKDF2SHA256: 1.23 + * - <https://github.com/ircmaxell/PHP-PasswordLib/blob/master/test/Data/Vectors/pbkdf2-draft-josefsson-sha256.test-vectors> 1.24 + * - <https://gitorious.org/scrypt/nettle-scrypt/blobs/37c0d5288e991604fe33dba2f1724986a8dddf56/testsuite/pbkdf2-test.c> 1.25 + * * SHA-1: 1.26 + * - <http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c> 1.27 + */ 1.28 +public class TestNativeCrypto extends TestCase { 1.29 + 1.30 + public final void testPBKDF2SHA256A() throws UnsupportedEncodingException, GeneralSecurityException { 1.31 + String p = "password"; 1.32 + String s = "salt"; 1.33 + int dkLen = 32; 1.34 + 1.35 + checkPBKDF2SHA256(p, s, 1, dkLen, "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b"); 1.36 + checkPBKDF2SHA256(p, s, 4096, dkLen, "c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a"); 1.37 + } 1.38 + 1.39 + public final void testPBKDF2SHA256B() throws UnsupportedEncodingException, GeneralSecurityException { 1.40 + String p = "passwordPASSWORDpassword"; 1.41 + String s = "saltSALTsaltSALTsaltSALTsaltSALTsalt"; 1.42 + int dkLen = 40; 1.43 + 1.44 + checkPBKDF2SHA256(p, s, 4096, dkLen, "348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9"); 1.45 + } 1.46 + 1.47 + public final void testPBKDF2SHA256scryptA() throws UnsupportedEncodingException, GeneralSecurityException { 1.48 + String p = "passwd"; 1.49 + String s = "salt"; 1.50 + int dkLen = 64; 1.51 + 1.52 + checkPBKDF2SHA256(p, s, 1, dkLen, "55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc49ca9cccf179b645991664b39d77ef317c71b845b1e30bd509112041d3a19783"); 1.53 + } 1.54 + 1.55 + public final void testPBKDF2SHA256scryptB() throws UnsupportedEncodingException, GeneralSecurityException { 1.56 + String p = "Password"; 1.57 + String s = "NaCl"; 1.58 + int dkLen = 64; 1.59 + 1.60 + checkPBKDF2SHA256(p, s, 80000, dkLen, "4ddcd8f60b98be21830cee5ef22701f9641a4418d04c0414aeff08876b34ab56a1d425a1225833549adb841b51c9b3176a272bdebba1d078478f62b397f33c8d"); 1.61 + } 1.62 + 1.63 + public final void testPBKDF2SHA256C() throws UnsupportedEncodingException, GeneralSecurityException { 1.64 + String p = "pass\0word"; 1.65 + String s = "sa\0lt"; 1.66 + int dkLen = 16; 1.67 + 1.68 + checkPBKDF2SHA256(p, s, 4096, dkLen, "89b69d0516f829893c696226650a8687"); 1.69 + } 1.70 + 1.71 + /* 1.72 + // This test takes two or three minutes to run, so we don't. 1.73 + public final void testPBKDF2SHA256D() throws UnsupportedEncodingException, GeneralSecurityException { 1.74 + String p = "password"; 1.75 + String s = "salt"; 1.76 + int dkLen = 32; 1.77 + 1.78 + checkPBKDF2SHA256(p, s, 16777216, dkLen, "cf81c66fe8cfc04d1f31ecb65dab4089f7f179e89b3b0bcb17ad10e3ac6eba46"); 1.79 + } 1.80 + */ 1.81 + 1.82 + public final void testTimePBKDF2SHA256() throws UnsupportedEncodingException, GeneralSecurityException { 1.83 + checkPBKDF2SHA256("password", "salt", 80000, 32, null); 1.84 + } 1.85 + 1.86 + public final void testPBKDF2SHA256InvalidLenArg() throws UnsupportedEncodingException, GeneralSecurityException { 1.87 + final String p = "password"; 1.88 + final String s = "salt"; 1.89 + final int c = 1; 1.90 + final int dkLen = -1; // Should always be positive. 1.91 + 1.92 + try { 1.93 + NativeCrypto.pbkdf2SHA256(p.getBytes("US-ASCII"), s.getBytes("US-ASCII"), c, dkLen); 1.94 + fail("Expected sha256 to throw with negative dkLen argument."); 1.95 + } catch (IllegalArgumentException e) { } // Expected. 1.96 + } 1.97 + 1.98 + public final void testSHA1() throws UnsupportedEncodingException { 1.99 + final String[] inputs = new String[] { 1.100 + "abc", 1.101 + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1.102 + "" // To be filled in below. 1.103 + }; 1.104 + final String baseStr = "01234567"; 1.105 + final int repetitions = 80; 1.106 + final StringBuilder builder = new StringBuilder(baseStr.length() * repetitions); 1.107 + for (int i = 0; i < 80; ++i) { 1.108 + builder.append(baseStr); 1.109 + } 1.110 + inputs[2] = builder.toString(); 1.111 + 1.112 + final String[] expecteds = new String[] { 1.113 + "a9993e364706816aba3e25717850c26c9cd0d89d", 1.114 + "84983e441c3bd26ebaae4aa1f95129e5e54670f1", 1.115 + "dea356a2cddd90c7a7ecedc5ebb563934f460452" 1.116 + }; 1.117 + 1.118 + for (int i = 0; i < inputs.length; ++i) { 1.119 + final byte[] input = inputs[i].getBytes("US-ASCII"); 1.120 + final String expected = expecteds[i]; 1.121 + 1.122 + final byte[] actual = NativeCrypto.sha1(input); 1.123 + assertNotNull("Hashed value is non-null", actual); 1.124 + assertExpectedBytes(expected, actual); 1.125 + } 1.126 + } 1.127 + 1.128 + /** 1.129 + * Test to ensure the output of our SHA1 algo is the same as MessageDigest's. This is important 1.130 + * because we intend to replace MessageDigest in FHR with this SHA-1 algo (bug 959652). 1.131 + */ 1.132 + public final void testSHA1AgainstMessageDigest() throws UnsupportedEncodingException, 1.133 + NoSuchAlgorithmException { 1.134 + final String[] inputs = { 1.135 + "password", 1.136 + "saranghae", 1.137 + "aoeusnthaoeusnthaoeusnth \0 12345098765432109876_!" 1.138 + }; 1.139 + 1.140 + final MessageDigest digest = MessageDigest.getInstance("SHA-1"); 1.141 + for (final String input : inputs) { 1.142 + final byte[] inputBytes = input.getBytes("US-ASCII"); 1.143 + 1.144 + final byte[] mdBytes = digest.digest(inputBytes); 1.145 + final byte[] ourBytes = NativeCrypto.sha1(inputBytes); 1.146 + assertTrue("MessageDigest hash is the same as NativeCrypto SHA-1 hash", 1.147 + Arrays.equals(ourBytes, mdBytes)); 1.148 + } 1.149 + } 1.150 + 1.151 + private void checkPBKDF2SHA256(String p, String s, int c, int dkLen, 1.152 + final String expectedStr) 1.153 + throws GeneralSecurityException, UnsupportedEncodingException { 1.154 + long start = System.currentTimeMillis(); 1.155 + byte[] key = NativeCrypto.pbkdf2SHA256(p.getBytes("US-ASCII"), s.getBytes("US-ASCII"), c, dkLen); 1.156 + assertNotNull(key); 1.157 + 1.158 + long end = System.currentTimeMillis(); 1.159 + 1.160 + System.err.println("SHA-256 " + c + " took " + (end - start) + "ms"); 1.161 + if (expectedStr == null) { 1.162 + return; 1.163 + } 1.164 + 1.165 + assertEquals(dkLen, Utils.hex2Byte(expectedStr).length); 1.166 + assertExpectedBytes(expectedStr, key); 1.167 + } 1.168 + 1.169 + private void assertExpectedBytes(final String expectedStr, byte[] key) { 1.170 + assertEquals(expectedStr, Utils.byte2Hex(key)); 1.171 + byte[] expected = Utils.hex2Byte(expectedStr); 1.172 + 1.173 + assertEquals(expected.length, key.length); 1.174 + for (int i = 0; i < key.length; i++) { 1.175 + assertEquals(expected[i], key[i]); 1.176 + } 1.177 + } 1.178 +}