1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.security;
18
19import java.security.MessageDigest;
20import java.security.NoSuchAlgorithmException;
21import java.security.Provider;
22import java.security.Security;
23import java.util.Arrays;
24import java.util.HashMap;
25import java.util.Map;
26import java.util.Set;
27import junit.framework.TestCase;
28
29public final class MessageDigestTest extends TestCase {
30
31    private final byte[] sha_456 = {
32            -24,   9, -59, -47,  -50,  -92, 123, 69, -29,  71,
33              1, -46,  63,  96, -118, -102,  88,  3,  77, -55
34    };
35
36    public void testShaReset() throws NoSuchAlgorithmException {
37        MessageDigest sha = MessageDigest.getInstance("SHA");
38        sha.update(new byte[] { 1, 2, 3 });
39        sha.reset();
40        sha.update(new byte[] { 4, 5, 6 });
41        assertEquals(Arrays.toString(sha_456), Arrays.toString(sha.digest()));
42    }
43
44    public void test_getInstance() throws Exception {
45        Provider[] providers = Security.getProviders();
46        for (Provider provider : providers) {
47            Set<Provider.Service> services = provider.getServices();
48            for (Provider.Service service : services) {
49                String type = service.getType();
50                if (!type.equals("MessageDigest")) {
51                    continue;
52                }
53                String algorithm = service.getAlgorithm();
54                try {
55                    // MessageDigest.getInstance(String)
56                    MessageDigest md1 = MessageDigest.getInstance(algorithm);
57                    assertEquals(algorithm, md1.getAlgorithm());
58                    test_MessageDigest(md1);
59
60                    // MessageDigest.getInstance(String, Provider)
61                    MessageDigest md2 = MessageDigest.getInstance(algorithm, provider);
62                    assertEquals(algorithm, md2.getAlgorithm());
63                    assertEquals(provider, md2.getProvider());
64                    test_MessageDigest(md2);
65
66                    // MessageDigest.getInstance(String, String)
67                    MessageDigest md3 = MessageDigest.getInstance(algorithm, provider.getName());
68                    assertEquals(algorithm, md3.getAlgorithm());
69                    assertEquals(provider, md3.getProvider());
70                    test_MessageDigest(md3);
71                } catch (Exception e) {
72                    throw new Exception("Problem testing MessageDigest." + algorithm, e);
73                }
74            }
75        }
76    }
77
78    private static final Map<String, Map<String, byte[]>> EXPECTATIONS
79            = new HashMap<String, Map<String, byte[]>>();
80    private static void putExpectation(String algorithm, String inputName, byte[] expected) {
81        algorithm = algorithm.toUpperCase();
82        Map<String, byte[]> expectations = EXPECTATIONS.get(algorithm);
83        if (expectations == null) {
84            expectations = new HashMap<String, byte[]>();
85            EXPECTATIONS.put(algorithm, expectations);
86        }
87        expectations.put(inputName, expected);
88    }
89    private static Map<String, byte[]> getExpectations(String algorithm) throws Exception {
90        algorithm = algorithm.toUpperCase();
91        Map<String, byte[]> expectations = EXPECTATIONS.get(algorithm);
92        if (expectations == null) {
93            throw new Exception("No expectations for MessageDigest." + algorithm);
94        }
95        return expectations;
96    }
97    private static final String INPUT_EMPTY = "empty";
98    private static final String INPUT_256MB = "256mb";
99    static {
100        // INPUT_EMPTY
101        putExpectation("MD2",
102                       INPUT_EMPTY,
103                       new byte[] { -125, 80, -27, -93, -30, 76, 21, 61,
104                                    -14, 39, 92, -97, -128, 105, 39, 115 });
105        putExpectation("MD5",
106                       INPUT_EMPTY,
107                       new byte[] { -44, 29, -116, -39, -113, 0, -78, 4,
108                                    -23, -128, 9, -104, -20, -8, 66, 126 });
109        putExpectation("SHA",
110                       INPUT_EMPTY,
111                       new byte[] { -38, 57, -93, -18, 94, 107, 75, 13,
112                                    50, 85, -65, -17, -107, 96, 24, -112,
113                                    -81, -40, 7, 9});
114        putExpectation("SHA-1",
115                       INPUT_EMPTY,
116                       new byte[] { -38, 57, -93, -18, 94, 107, 75, 13,
117                                    50, 85, -65, -17, -107, 96, 24, -112,
118                                    -81, -40, 7, 9});
119        putExpectation("SHA-256",
120                       INPUT_EMPTY,
121                       new byte[] { -29, -80, -60, 66, -104, -4, 28, 20,
122                                    -102, -5, -12, -56, -103, 111, -71, 36,
123                                    39, -82, 65, -28, 100, -101, -109, 76,
124                                    -92, -107, -103, 27, 120, 82, -72, 85 });
125        putExpectation("SHA-384",
126                       INPUT_EMPTY,
127                       new byte[] { 56, -80, 96, -89, 81, -84, -106, 56,
128                                    76, -39, 50, 126, -79, -79, -29, 106,
129                                    33, -3, -73, 17, 20, -66, 7, 67,
130                                    76, 12, -57, -65, 99, -10, -31, -38,
131                                    39, 78, -34, -65, -25, 111, 101, -5,
132                                    -43, 26, -46, -15, 72, -104, -71, 91 });
133        putExpectation("SHA-512",
134                       INPUT_EMPTY,
135                       new byte[] { -49, -125, -31, 53, 126, -17, -72, -67,
136                                    -15, 84, 40, 80, -42, 109, -128, 7,
137                                    -42, 32, -28, 5, 11, 87, 21, -36,
138                                    -125, -12, -87, 33, -45, 108, -23, -50,
139                                    71, -48, -47, 60, 93, -123, -14, -80,
140                                    -1, -125, 24, -46, -121, 126, -20, 47,
141                                    99, -71, 49, -67, 71, 65, 122, -127,
142                                    -91, 56, 50, 122, -7, 39, -38, 62 });
143
144        // Regression test for a SHA-1 problem with inputs larger than 256 MiB. http://b/4501620
145        // In mid-2013 this takes 3 minutes even on the host, so let's not run it on devices.
146        if (System.getenv("ANDROID_BUILD_TOP") != null) {
147            // INPUT_256MB
148            putExpectation("MD2",
149                           INPUT_256MB,
150                           new byte[] { -63, -120, 6, 67, 12, -87, -39, -11,
151                                        -67, -3, -31, -41, -91, 16, -35, 91 });
152            putExpectation("MD5",
153                           INPUT_256MB,
154                           new byte[] { 31, 80, 57, -27, 11, -42, 107, 41,
155                                        12, 86, 104, 77, -123, 80, -58, -62 });
156            putExpectation("SHA",
157                           INPUT_256MB,
158                           new byte[] { 123, -111, -37, -36, 86, -59, 120, 30,
159                                        -33, 108, -120, 71, -76, -86, 105, 101,
160                                        86, 108, 92, 117 });
161            putExpectation("SHA-1",
162                           INPUT_256MB,
163                           new byte[] { 123, -111, -37, -36, 86, -59, 120, 30,
164                                        -33, 108, -120, 71, -76, -86, 105, 101,
165                                        86, 108, 92, 117 });
166            putExpectation("SHA-256",
167                           INPUT_256MB,
168                           new byte[] { -90, -41, 42, -57, 105, 15, 83, -66,
169                                        106, -28, 107, -88, -123, 6, -67, -105,
170                                        48, 42, 9, 63, 113, 8, 71, 43,
171                                        -39, -17, -61, -50, -3, -96, 100, -124 });
172            putExpectation("SHA-384",
173                           INPUT_256MB,
174                           new byte[] { 71, 72, 77, -83, -110, 22, -118, -18,
175                                        -58, 119, 115, 74, -67, -36, 84, 122,
176                                        -105, -67, -75, 15, -33, 37, 78, -95,
177                                        4, 118, -53, 106, 65, -115, -19, 121,
178                                        -59, -94, -45, -111, -124, 35, 35, 60,
179                                        67, -34, 62, 106, -16, 122, -110, -14 });
180            putExpectation("SHA-512",
181                           INPUT_256MB,
182                           new byte[] { 36, 7, -120, 39, -87, -87, 84, -40,
183                                        -66, 114, 62, -73, 107, 101, -117, -12,
184                                        -124, 20, 109, 103, -92, 125, 111, 102,
185                                        12, 114, -68, 100, 30, 25, -88, 62,
186                                        108, 56, 9, -107, 89, -25, -50, 118,
187                                        -87, 100, 13, 37, -14, 66, -40, -97,
188                                        105, -27, 79, -62, 53, -31, 83, 40,
189                                        4, 57, 90, -81, 63, -77, -42, 113 });
190        }
191    }
192
193    private void test_MessageDigest(MessageDigest md) throws Exception {
194        String algorithm = md.getAlgorithm();
195        for (Map.Entry<String, byte[]> expectation : getExpectations(algorithm).entrySet()) {
196            String inputName = expectation.getKey();
197            byte[] expected = expectation.getValue();
198            byte[] actual;
199            if (inputName.equals(INPUT_EMPTY)) {
200                actual = md.digest();
201            } else if (inputName.equals(INPUT_256MB)) {
202                byte[] mb = new byte[1 * 1024 * 1024];
203                for (int i = 0; i < 256; i++) {
204                    md.update(mb);
205                }
206                actual = md.digest();
207            } else {
208                throw new AssertionError(inputName);
209            }
210            assertDigest(algorithm, expected, actual);
211            assertEquals(algorithm, expected.length, md.getDigestLength());
212        }
213    }
214
215    private void assertDigest(String algorithm, byte[] actual, byte[] expected) {
216        assertEquals(algorithm, javaBytes(actual), javaBytes(expected));
217    }
218
219    private String javaBytes(byte[] bytes) {
220        StringBuffer buf = new StringBuffer();
221        buf.append("new byte[] { ");
222        for (byte b : bytes) {
223            buf.append(b);
224            buf.append(", ");
225        }
226        buf.append(" }");
227        return buf.toString();
228    }
229
230}
231