View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.util.sha1;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  import static org.junit.Assert.fail;
49  import static org.junit.Assume.assumeTrue;
50  
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.nio.ByteBuffer;
54  import java.nio.charset.StandardCharsets;
55  import java.security.MessageDigest;
56  import java.security.NoSuchAlgorithmException;
57  
58  import org.eclipse.jgit.lib.Constants;
59  import org.eclipse.jgit.lib.ObjectId;
60  import org.eclipse.jgit.util.IO;
61  import org.junit.Test;
62  
63  public class SHA1Test {
64  	private static final String TEST1 = "abc";
65  
66  	private static final String TEST2a = "abcdbcdecdefdefgefghfghighijhi";
67  	private static final String TEST2b = "jkijkljklmklmnlmnomnopnopq";
68  	private static final String TEST2 = TEST2a + TEST2b;
69  
70  	@Test
71  	public void test0() throws NoSuchAlgorithmException {
72  		ObjectId exp = ObjectId
73  				.fromString("da39a3ee5e6b4b0d3255bfef95601890afd80709");
74  
75  		MessageDigest m = MessageDigest.getInstance("SHA-1");
76  		m.update(new byte[] {});
77  		ObjectId m1 = ObjectId.fromRaw(m.digest());
78  
79  		SHA1 s = SHA1.newInstance();
80  		s.update(new byte[] {});
81  		ObjectId s1 = ObjectId.fromRaw(s.digest());
82  
83  		s.reset();
84  		s.update(new byte[] {});
85  		ObjectId s2 = s.toObjectId();
86  
87  		assertEquals(m1, s1);
88  		assertEquals(exp, s1);
89  		assertEquals(exp, s2);
90  	}
91  
92  	@Test
93  	public void test1() throws NoSuchAlgorithmException {
94  		ObjectId exp = ObjectId
95  				.fromString("a9993e364706816aba3e25717850c26c9cd0d89d");
96  
97  		MessageDigest m = MessageDigest.getInstance("SHA-1");
98  		m.update(TEST1.getBytes(StandardCharsets.UTF_8));
99  		ObjectId m1 = ObjectId.fromRaw(m.digest());
100 
101 		SHA1 s = SHA1.newInstance();
102 		s.update(TEST1.getBytes(StandardCharsets.UTF_8));
103 		ObjectId s1 = ObjectId.fromRaw(s.digest());
104 
105 		s.reset();
106 		s.update(TEST1.getBytes(StandardCharsets.UTF_8));
107 		ObjectId s2 = s.toObjectId();
108 
109 		assertEquals(m1, s1);
110 		assertEquals(exp, s1);
111 		assertEquals(exp, s2);
112 	}
113 
114 	@Test
115 	public void test2() throws NoSuchAlgorithmException {
116 		ObjectId exp = ObjectId
117 				.fromString("84983e441c3bd26ebaae4aa1f95129e5e54670f1");
118 
119 		MessageDigest m = MessageDigest.getInstance("SHA-1");
120 		m.update(TEST2.getBytes(StandardCharsets.UTF_8));
121 		ObjectId m1 = ObjectId.fromRaw(m.digest());
122 
123 		SHA1 s = SHA1.newInstance();
124 		s.update(TEST2.getBytes(StandardCharsets.UTF_8));
125 		ObjectId s1 = ObjectId.fromRaw(s.digest());
126 
127 		s.reset();
128 		s.update(TEST2.getBytes(StandardCharsets.UTF_8));
129 		ObjectId s2 = s.toObjectId();
130 
131 		assertEquals(m1, s1);
132 		assertEquals(exp, s1);
133 		assertEquals(exp, s2);
134 	}
135 
136 	@Test
137 	public void shatteredCollision()
138 			throws IOException, NoSuchAlgorithmException {
139 		byte[] pdf1 = read("shattered-1.pdf", 422435);
140 		byte[] pdf2 = read("shattered-2.pdf", 422435);
141 		MessageDigest md;
142 		SHA1 s;
143 
144 		// SHAttered attack generated these PDFs to have identical SHA-1.
145 		ObjectId bad = ObjectId
146 				.fromString("38762cf7f55934b34d179ae6a4c80cadccbb7f0a");
147 		md = MessageDigest.getInstance("SHA-1");
148 		md.update(pdf1);
149 		assertEquals("shattered-1 collides", bad,
150 				ObjectId.fromRaw(md.digest()));
151 		s = SHA1.newInstance().setDetectCollision(false);
152 		s.update(pdf1);
153 		assertEquals("shattered-1 collides", bad, s.toObjectId());
154 
155 		md = MessageDigest.getInstance("SHA-1");
156 		md.update(pdf2);
157 		assertEquals("shattered-2 collides", bad,
158 				ObjectId.fromRaw(md.digest()));
159 		s = SHA1.newInstance().setDetectCollision(false);
160 		s.update(pdf2);
161 		assertEquals("shattered-2 collides", bad, s.toObjectId());
162 
163 		// SHA1 with detectCollision shouldn't be fooled.
164 		s = SHA1.newInstance().setDetectCollision(true);
165 		s.update(pdf1);
166 		try {
167 			s.digest();
168 			fail("expected " + Sha1CollisionException.class.getSimpleName());
169 		} catch (Sha1CollisionException e) {
170 			assertEquals(e.getMessage(),
171 					"SHA-1 collision detected on " + bad.name());
172 		}
173 
174 		s = SHA1.newInstance().setDetectCollision(true);
175 		s.update(pdf2);
176 		try {
177 			s.digest();
178 			fail("expected " + Sha1CollisionException.class.getSimpleName());
179 		} catch (Sha1CollisionException e) {
180 			assertEquals(e.getMessage(),
181 					"SHA-1 collision detected on " + bad.name());
182 		}
183 	}
184 
185 	@Test
186 	public void shatteredStoredInGitBlob() throws IOException {
187 		byte[] pdf1 = read("shattered-1.pdf", 422435);
188 		byte[] pdf2 = read("shattered-2.pdf", 422435);
189 
190 		// Although the prior test detects the chance of a collision, adding
191 		// the Git blob header permutes the data enough for this specific
192 		// attack example to not be detected as a collision. (A different file
193 		// pair that takes the Git header into account however, would.)
194 		ObjectId id1 = blob(pdf1, SHA1.newInstance().setDetectCollision(true));
195 		ObjectId id2 = blob(pdf2, SHA1.newInstance().setDetectCollision(true));
196 
197 		assertEquals(
198 				ObjectId.fromString("ba9aaa145ccd24ef760cf31c74d8f7ca1a2e47b0"),
199 				id1);
200 		assertEquals(
201 				ObjectId.fromString("b621eeccd5c7edac9b7dcba35a8d5afd075e24f2"),
202 				id2);
203 	}
204 
205 	@Test
206 	public void detectsShatteredByDefault() throws IOException {
207 		assumeTrue(System.getProperty("org.eclipse.jgit.util.sha1.detectCollision") == null);
208 		assumeTrue(System.getProperty("org.eclipse.jgit.util.sha1.safeHash") == null);
209 
210 		byte[] pdf1 = read("shattered-1.pdf", 422435);
211 		SHA1 s = SHA1.newInstance();
212 		s.update(pdf1);
213 		try {
214 			s.digest();
215 			fail("expected " + Sha1CollisionException.class.getSimpleName());
216 		} catch (Sha1CollisionException e) {
217 			assertTrue("shattered-1 detected", true);
218 		}
219 	}
220 
221 	private static ObjectId blob(byte[] pdf1, SHA1 s) {
222 		s.update(Constants.encodedTypeString(Constants.OBJ_BLOB));
223 		s.update((byte) ' ');
224 		s.update(Constants.encodeASCII(pdf1.length));
225 		s.update((byte) 0);
226 		s.update(pdf1);
227 		return s.toObjectId();
228 	}
229 
230 	private byte[] read(String name, int sizeHint) throws IOException {
231 		try (InputStream in = getClass().getResourceAsStream(name)) {
232 			ByteBuffer buf = IO.readWholeStream(in, sizeHint);
233 			byte[] r = new byte[buf.remaining()];
234 			buf.get(r);
235 			return r;
236 		}
237 	}
238 }