summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@github.com>2017-02-24 14:14:56 +0000
committerEdward Thomson <ethomson@github.com>2017-03-03 10:50:28 +0000
commitd087c8f80ecbbbfca2740392cbf84816019f4d49 (patch)
tree81d8191d8b7df26a2489c4e5dd72699892c34fb7
parent2dfd1294f7a694bfa9e864a9489ae3cb318a5ed0 (diff)
downloadlibgit2-d087c8f80ecbbbfca2740392cbf84816019f4d49.tar.gz
hash: test for sha1 collision attack detection
-rw-r--r--tests/core/sha1.c64
-rw-r--r--tests/resources/sha1/hello_c6
-rw-r--r--tests/resources/sha1/shattered-1.pdfbin0 -> 422435 bytes
3 files changed, 70 insertions, 0 deletions
diff --git a/tests/core/sha1.c b/tests/core/sha1.c
new file mode 100644
index 000000000..c5b20f6e0
--- /dev/null
+++ b/tests/core/sha1.c
@@ -0,0 +1,64 @@
+#include "clar_libgit2.h"
+#include "hash.h"
+
+#define FIXTURE_DIR "sha1"
+
+void test_core_sha1__initialize(void)
+{
+ cl_fixture_sandbox(FIXTURE_DIR);
+}
+
+void test_core_sha1__cleanup(void)
+{
+ cl_fixture_cleanup(FIXTURE_DIR);
+}
+
+static int sha1_file(git_oid *oid, const char *filename)
+{
+ git_hash_ctx ctx;
+ char buf[2048];
+ int fd, ret;
+ ssize_t read_len;
+
+ fd = p_open(filename, O_RDONLY);
+ cl_assert(fd >= 0);
+
+ cl_git_pass(git_hash_ctx_init(&ctx));
+
+ while ((read_len = p_read(fd, buf, 2048)) > 0)
+ cl_git_pass(git_hash_update(&ctx, buf, (size_t)read_len));
+
+ cl_assert_equal_i(0, read_len);
+ p_close(fd);
+
+ ret = git_hash_final(oid, &ctx);
+ git_hash_ctx_cleanup(&ctx);
+
+ return ret;
+}
+
+void test_core_sha1__sum(void)
+{
+ git_oid oid, expected;
+
+ cl_git_pass(sha1_file(&oid, FIXTURE_DIR "/hello_c"));
+ git_oid_fromstr(&expected, "4e72679e3ea4d04e0c642f029e61eb8056c7ed94");
+ cl_assert_equal_oid(&expected, &oid);
+}
+
+/* test that sha1 collision detection works when enabled */
+void test_core_sha1__detect_collision_attack(void)
+{
+ git_oid oid, expected;
+
+#ifdef GIT_SHA1_COLLISIONDETECT
+ GIT_UNUSED(expected);
+ cl_git_fail(sha1_file(&oid, FIXTURE_DIR "/shattered-1.pdf"));
+ cl_assert_equal_s("SHA1 collision attack detected", giterr_last()->message);
+#else
+ cl_git_pass(sha1_file(&oid, FIXTURE_DIR "/shattered-1.pdf"));
+ git_oid_fromstr(&expected, "38762cf7f55934b34d179ae6a4c80cadccbb7f0a");
+ cl_assert_equal_oid(&expected, &oid);
+#endif
+}
+
diff --git a/tests/resources/sha1/hello_c b/tests/resources/sha1/hello_c
new file mode 100644
index 000000000..45950b2ad
--- /dev/null
+++ b/tests/resources/sha1/hello_c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+ printf("Hello, %s\n", "world");
+}
diff --git a/tests/resources/sha1/shattered-1.pdf b/tests/resources/sha1/shattered-1.pdf
new file mode 100644
index 000000000..ba9aaa145
--- /dev/null
+++ b/tests/resources/sha1/shattered-1.pdf
Binary files differ