1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.util.sha1;
45
46 import static org.eclipse.jgit.lib.Constants.CHARSET;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertTrue;
49 import static org.junit.Assert.fail;
50 import static org.junit.Assume.assumeTrue;
51
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.nio.ByteBuffer;
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(CHARSET));
99 ObjectId m1 = ObjectId.fromRaw(m.digest());
100
101 SHA1 s = SHA1.newInstance();
102 s.update(TEST1.getBytes(CHARSET));
103 ObjectId s1 = ObjectId.fromRaw(s.digest());
104
105 s.reset();
106 s.update(TEST1.getBytes(CHARSET));
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(CHARSET));
121 ObjectId m1 = ObjectId.fromRaw(m.digest());
122
123 SHA1 s = SHA1.newInstance();
124 s.update(TEST2.getBytes(CHARSET));
125 ObjectId s1 = ObjectId.fromRaw(s.digest());
126
127 s.reset();
128 s.update(TEST2.getBytes(CHARSET));
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
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
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
191
192
193
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 }