You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SHA1Test.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. package org.eclipse.jgit.util.sha1;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import static org.junit.Assume.assumeTrue;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.nio.ByteBuffer;
  52. import java.security.MessageDigest;
  53. import java.security.NoSuchAlgorithmException;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.util.IO;
  57. import org.junit.Test;
  58. public class SHA1Test {
  59. private static final String TEST1 = "abc";
  60. private static final String TEST2a = "abcdbcdecdefdefgefghfghighijhi";
  61. private static final String TEST2b = "jkijkljklmklmnlmnomnopnopq";
  62. private static final String TEST2 = TEST2a + TEST2b;
  63. @Test
  64. public void test0() throws NoSuchAlgorithmException {
  65. ObjectId exp = ObjectId
  66. .fromString("da39a3ee5e6b4b0d3255bfef95601890afd80709");
  67. MessageDigest m = MessageDigest.getInstance("SHA-1");
  68. m.update(new byte[] {});
  69. ObjectId m1 = ObjectId.fromRaw(m.digest());
  70. SHA1 s = SHA1.newInstance();
  71. s.update(new byte[] {});
  72. ObjectId s1 = ObjectId.fromRaw(s.digest());
  73. s.reset();
  74. s.update(new byte[] {});
  75. ObjectId s2 = s.toObjectId();
  76. assertEquals(m1, s1);
  77. assertEquals(exp, s1);
  78. assertEquals(exp, s2);
  79. }
  80. @Test
  81. public void test1() throws NoSuchAlgorithmException {
  82. ObjectId exp = ObjectId
  83. .fromString("a9993e364706816aba3e25717850c26c9cd0d89d");
  84. MessageDigest m = MessageDigest.getInstance("SHA-1");
  85. m.update(TEST1.getBytes(UTF_8));
  86. ObjectId m1 = ObjectId.fromRaw(m.digest());
  87. SHA1 s = SHA1.newInstance();
  88. s.update(TEST1.getBytes(UTF_8));
  89. ObjectId s1 = ObjectId.fromRaw(s.digest());
  90. s.reset();
  91. s.update(TEST1.getBytes(UTF_8));
  92. ObjectId s2 = s.toObjectId();
  93. assertEquals(m1, s1);
  94. assertEquals(exp, s1);
  95. assertEquals(exp, s2);
  96. }
  97. @Test
  98. public void test2() throws NoSuchAlgorithmException {
  99. ObjectId exp = ObjectId
  100. .fromString("84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  101. MessageDigest m = MessageDigest.getInstance("SHA-1");
  102. m.update(TEST2.getBytes(UTF_8));
  103. ObjectId m1 = ObjectId.fromRaw(m.digest());
  104. SHA1 s = SHA1.newInstance();
  105. s.update(TEST2.getBytes(UTF_8));
  106. ObjectId s1 = ObjectId.fromRaw(s.digest());
  107. s.reset();
  108. s.update(TEST2.getBytes(UTF_8));
  109. ObjectId s2 = s.toObjectId();
  110. assertEquals(m1, s1);
  111. assertEquals(exp, s1);
  112. assertEquals(exp, s2);
  113. }
  114. @Test
  115. public void shatteredCollision()
  116. throws IOException, NoSuchAlgorithmException {
  117. byte[] pdf1 = read("shattered-1.pdf", 422435);
  118. byte[] pdf2 = read("shattered-2.pdf", 422435);
  119. MessageDigest md;
  120. SHA1 s;
  121. // SHAttered attack generated these PDFs to have identical SHA-1.
  122. ObjectId bad = ObjectId
  123. .fromString("38762cf7f55934b34d179ae6a4c80cadccbb7f0a");
  124. md = MessageDigest.getInstance("SHA-1");
  125. md.update(pdf1);
  126. assertEquals("shattered-1 collides", bad,
  127. ObjectId.fromRaw(md.digest()));
  128. s = SHA1.newInstance().setDetectCollision(false);
  129. s.update(pdf1);
  130. assertEquals("shattered-1 collides", bad, s.toObjectId());
  131. md = MessageDigest.getInstance("SHA-1");
  132. md.update(pdf2);
  133. assertEquals("shattered-2 collides", bad,
  134. ObjectId.fromRaw(md.digest()));
  135. s = SHA1.newInstance().setDetectCollision(false);
  136. s.update(pdf2);
  137. assertEquals("shattered-2 collides", bad, s.toObjectId());
  138. // SHA1 with detectCollision shouldn't be fooled.
  139. s = SHA1.newInstance().setDetectCollision(true);
  140. s.update(pdf1);
  141. try {
  142. s.digest();
  143. fail("expected " + Sha1CollisionException.class.getSimpleName());
  144. } catch (Sha1CollisionException e) {
  145. assertEquals(e.getMessage(),
  146. "SHA-1 collision detected on " + bad.name());
  147. }
  148. s = SHA1.newInstance().setDetectCollision(true);
  149. s.update(pdf2);
  150. try {
  151. s.digest();
  152. fail("expected " + Sha1CollisionException.class.getSimpleName());
  153. } catch (Sha1CollisionException e) {
  154. assertEquals(e.getMessage(),
  155. "SHA-1 collision detected on " + bad.name());
  156. }
  157. }
  158. @Test
  159. public void shatteredStoredInGitBlob() throws IOException {
  160. byte[] pdf1 = read("shattered-1.pdf", 422435);
  161. byte[] pdf2 = read("shattered-2.pdf", 422435);
  162. // Although the prior test detects the chance of a collision, adding
  163. // the Git blob header permutes the data enough for this specific
  164. // attack example to not be detected as a collision. (A different file
  165. // pair that takes the Git header into account however, would.)
  166. ObjectId id1 = blob(pdf1, SHA1.newInstance().setDetectCollision(true));
  167. ObjectId id2 = blob(pdf2, SHA1.newInstance().setDetectCollision(true));
  168. assertEquals(
  169. ObjectId.fromString("ba9aaa145ccd24ef760cf31c74d8f7ca1a2e47b0"),
  170. id1);
  171. assertEquals(
  172. ObjectId.fromString("b621eeccd5c7edac9b7dcba35a8d5afd075e24f2"),
  173. id2);
  174. }
  175. @Test
  176. public void detectsShatteredByDefault() throws IOException {
  177. assumeTrue(System.getProperty("org.eclipse.jgit.util.sha1.detectCollision") == null);
  178. assumeTrue(System.getProperty("org.eclipse.jgit.util.sha1.safeHash") == null);
  179. byte[] pdf1 = read("shattered-1.pdf", 422435);
  180. SHA1 s = SHA1.newInstance();
  181. s.update(pdf1);
  182. try {
  183. s.digest();
  184. fail("expected " + Sha1CollisionException.class.getSimpleName());
  185. } catch (Sha1CollisionException e) {
  186. assertTrue("shattered-1 detected", true);
  187. }
  188. }
  189. private static ObjectId blob(byte[] pdf1, SHA1 s) {
  190. s.update(Constants.encodedTypeString(Constants.OBJ_BLOB));
  191. s.update((byte) ' ');
  192. s.update(Constants.encodeASCII(pdf1.length));
  193. s.update((byte) 0);
  194. s.update(pdf1);
  195. return s.toObjectId();
  196. }
  197. private byte[] read(String name, int sizeHint) throws IOException {
  198. try (InputStream in = getClass().getResourceAsStream(name)) {
  199. ByteBuffer buf = IO.readWholeStream(in, sizeHint);
  200. byte[] r = new byte[buf.remaining()];
  201. buf.get(r);
  202. return r;
  203. }
  204. }
  205. }