|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 package org.mozilla.gecko.background.nativecode.test; |
|
5 |
|
6 import java.io.UnsupportedEncodingException; |
|
7 import java.security.GeneralSecurityException; |
|
8 import java.security.MessageDigest; |
|
9 import java.security.NoSuchAlgorithmException; |
|
10 import java.util.Arrays; |
|
11 |
|
12 import junit.framework.TestCase; |
|
13 |
|
14 import org.mozilla.gecko.background.nativecode.NativeCrypto; |
|
15 import org.mozilla.gecko.sync.Utils; |
|
16 |
|
17 /* |
|
18 * Tests the Java wrapper over native implementations of crypto code. Test vectors from: |
|
19 * * PBKDF2SHA256: |
|
20 * - <https://github.com/ircmaxell/PHP-PasswordLib/blob/master/test/Data/Vectors/pbkdf2-draft-josefsson-sha256.test-vectors> |
|
21 * - <https://gitorious.org/scrypt/nettle-scrypt/blobs/37c0d5288e991604fe33dba2f1724986a8dddf56/testsuite/pbkdf2-test.c> |
|
22 * * SHA-1: |
|
23 * - <http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c> |
|
24 */ |
|
25 public class TestNativeCrypto extends TestCase { |
|
26 |
|
27 public final void testPBKDF2SHA256A() throws UnsupportedEncodingException, GeneralSecurityException { |
|
28 String p = "password"; |
|
29 String s = "salt"; |
|
30 int dkLen = 32; |
|
31 |
|
32 checkPBKDF2SHA256(p, s, 1, dkLen, "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b"); |
|
33 checkPBKDF2SHA256(p, s, 4096, dkLen, "c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a"); |
|
34 } |
|
35 |
|
36 public final void testPBKDF2SHA256B() throws UnsupportedEncodingException, GeneralSecurityException { |
|
37 String p = "passwordPASSWORDpassword"; |
|
38 String s = "saltSALTsaltSALTsaltSALTsaltSALTsalt"; |
|
39 int dkLen = 40; |
|
40 |
|
41 checkPBKDF2SHA256(p, s, 4096, dkLen, "348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9"); |
|
42 } |
|
43 |
|
44 public final void testPBKDF2SHA256scryptA() throws UnsupportedEncodingException, GeneralSecurityException { |
|
45 String p = "passwd"; |
|
46 String s = "salt"; |
|
47 int dkLen = 64; |
|
48 |
|
49 checkPBKDF2SHA256(p, s, 1, dkLen, "55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc49ca9cccf179b645991664b39d77ef317c71b845b1e30bd509112041d3a19783"); |
|
50 } |
|
51 |
|
52 public final void testPBKDF2SHA256scryptB() throws UnsupportedEncodingException, GeneralSecurityException { |
|
53 String p = "Password"; |
|
54 String s = "NaCl"; |
|
55 int dkLen = 64; |
|
56 |
|
57 checkPBKDF2SHA256(p, s, 80000, dkLen, "4ddcd8f60b98be21830cee5ef22701f9641a4418d04c0414aeff08876b34ab56a1d425a1225833549adb841b51c9b3176a272bdebba1d078478f62b397f33c8d"); |
|
58 } |
|
59 |
|
60 public final void testPBKDF2SHA256C() throws UnsupportedEncodingException, GeneralSecurityException { |
|
61 String p = "pass\0word"; |
|
62 String s = "sa\0lt"; |
|
63 int dkLen = 16; |
|
64 |
|
65 checkPBKDF2SHA256(p, s, 4096, dkLen, "89b69d0516f829893c696226650a8687"); |
|
66 } |
|
67 |
|
68 /* |
|
69 // This test takes two or three minutes to run, so we don't. |
|
70 public final void testPBKDF2SHA256D() throws UnsupportedEncodingException, GeneralSecurityException { |
|
71 String p = "password"; |
|
72 String s = "salt"; |
|
73 int dkLen = 32; |
|
74 |
|
75 checkPBKDF2SHA256(p, s, 16777216, dkLen, "cf81c66fe8cfc04d1f31ecb65dab4089f7f179e89b3b0bcb17ad10e3ac6eba46"); |
|
76 } |
|
77 */ |
|
78 |
|
79 public final void testTimePBKDF2SHA256() throws UnsupportedEncodingException, GeneralSecurityException { |
|
80 checkPBKDF2SHA256("password", "salt", 80000, 32, null); |
|
81 } |
|
82 |
|
83 public final void testPBKDF2SHA256InvalidLenArg() throws UnsupportedEncodingException, GeneralSecurityException { |
|
84 final String p = "password"; |
|
85 final String s = "salt"; |
|
86 final int c = 1; |
|
87 final int dkLen = -1; // Should always be positive. |
|
88 |
|
89 try { |
|
90 NativeCrypto.pbkdf2SHA256(p.getBytes("US-ASCII"), s.getBytes("US-ASCII"), c, dkLen); |
|
91 fail("Expected sha256 to throw with negative dkLen argument."); |
|
92 } catch (IllegalArgumentException e) { } // Expected. |
|
93 } |
|
94 |
|
95 public final void testSHA1() throws UnsupportedEncodingException { |
|
96 final String[] inputs = new String[] { |
|
97 "abc", |
|
98 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
|
99 "" // To be filled in below. |
|
100 }; |
|
101 final String baseStr = "01234567"; |
|
102 final int repetitions = 80; |
|
103 final StringBuilder builder = new StringBuilder(baseStr.length() * repetitions); |
|
104 for (int i = 0; i < 80; ++i) { |
|
105 builder.append(baseStr); |
|
106 } |
|
107 inputs[2] = builder.toString(); |
|
108 |
|
109 final String[] expecteds = new String[] { |
|
110 "a9993e364706816aba3e25717850c26c9cd0d89d", |
|
111 "84983e441c3bd26ebaae4aa1f95129e5e54670f1", |
|
112 "dea356a2cddd90c7a7ecedc5ebb563934f460452" |
|
113 }; |
|
114 |
|
115 for (int i = 0; i < inputs.length; ++i) { |
|
116 final byte[] input = inputs[i].getBytes("US-ASCII"); |
|
117 final String expected = expecteds[i]; |
|
118 |
|
119 final byte[] actual = NativeCrypto.sha1(input); |
|
120 assertNotNull("Hashed value is non-null", actual); |
|
121 assertExpectedBytes(expected, actual); |
|
122 } |
|
123 } |
|
124 |
|
125 /** |
|
126 * Test to ensure the output of our SHA1 algo is the same as MessageDigest's. This is important |
|
127 * because we intend to replace MessageDigest in FHR with this SHA-1 algo (bug 959652). |
|
128 */ |
|
129 public final void testSHA1AgainstMessageDigest() throws UnsupportedEncodingException, |
|
130 NoSuchAlgorithmException { |
|
131 final String[] inputs = { |
|
132 "password", |
|
133 "saranghae", |
|
134 "aoeusnthaoeusnthaoeusnth \0 12345098765432109876_!" |
|
135 }; |
|
136 |
|
137 final MessageDigest digest = MessageDigest.getInstance("SHA-1"); |
|
138 for (final String input : inputs) { |
|
139 final byte[] inputBytes = input.getBytes("US-ASCII"); |
|
140 |
|
141 final byte[] mdBytes = digest.digest(inputBytes); |
|
142 final byte[] ourBytes = NativeCrypto.sha1(inputBytes); |
|
143 assertTrue("MessageDigest hash is the same as NativeCrypto SHA-1 hash", |
|
144 Arrays.equals(ourBytes, mdBytes)); |
|
145 } |
|
146 } |
|
147 |
|
148 private void checkPBKDF2SHA256(String p, String s, int c, int dkLen, |
|
149 final String expectedStr) |
|
150 throws GeneralSecurityException, UnsupportedEncodingException { |
|
151 long start = System.currentTimeMillis(); |
|
152 byte[] key = NativeCrypto.pbkdf2SHA256(p.getBytes("US-ASCII"), s.getBytes("US-ASCII"), c, dkLen); |
|
153 assertNotNull(key); |
|
154 |
|
155 long end = System.currentTimeMillis(); |
|
156 |
|
157 System.err.println("SHA-256 " + c + " took " + (end - start) + "ms"); |
|
158 if (expectedStr == null) { |
|
159 return; |
|
160 } |
|
161 |
|
162 assertEquals(dkLen, Utils.hex2Byte(expectedStr).length); |
|
163 assertExpectedBytes(expectedStr, key); |
|
164 } |
|
165 |
|
166 private void assertExpectedBytes(final String expectedStr, byte[] key) { |
|
167 assertEquals(expectedStr, Utils.byte2Hex(key)); |
|
168 byte[] expected = Utils.hex2Byte(expectedStr); |
|
169 |
|
170 assertEquals(expected.length, key.length); |
|
171 for (int i = 0; i < key.length; i++) { |
|
172 assertEquals(expected[i], key[i]); |
|
173 } |
|
174 } |
|
175 } |