summaryrefslogtreecommitdiff
path: root/src/util/hash
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-12-12 15:34:35 -0500
committerEdward Thomson <ethomson@edwardthomson.com>2022-03-23 08:39:19 -0400
commitb3e3fa10eacbdf11efb1815d6f3cfcccdde2a23d (patch)
tree4c8c1477a3bdea3c0c289004602bd73898a36767 /src/util/hash
parent83c2778611a4f71b7bd841f643ac457af3f23248 (diff)
downloadlibgit2-b3e3fa10eacbdf11efb1815d6f3cfcccdde2a23d.tar.gz
sha: support mbedTLS for SHA256
Diffstat (limited to 'src/util/hash')
-rw-r--r--src/util/hash/mbedtls.c46
-rw-r--r--src/util/hash/mbedtls.h12
2 files changed, 57 insertions, 1 deletions
diff --git a/src/util/hash/mbedtls.c b/src/util/hash/mbedtls.c
index 56016bec8..ecdfb7879 100644
--- a/src/util/hash/mbedtls.c
+++ b/src/util/hash/mbedtls.c
@@ -7,6 +7,8 @@
#include "mbedtls.h"
+#ifdef GIT_SHA1_MBEDTLS
+
int git_hash_sha1_global_init(void)
{
return 0;
@@ -44,3 +46,47 @@ int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx)
mbedtls_sha1_finish(&ctx->c, out);
return 0;
}
+
+#endif
+
+#ifdef GIT_SHA256_MBEDTLS
+
+int git_hash_sha256_global_init(void)
+{
+ return 0;
+}
+
+int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx)
+{
+ return git_hash_sha256_init(ctx);
+}
+
+void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx)
+{
+ if (ctx)
+ mbedtls_sha256_free(&ctx->c);
+}
+
+int git_hash_sha256_init(git_hash_sha256_ctx *ctx)
+{
+ GIT_ASSERT_ARG(ctx);
+ mbedtls_sha256_init(&ctx->c);
+ mbedtls_sha256_starts(&ctx->c, 0);
+ return 0;
+}
+
+int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len)
+{
+ GIT_ASSERT_ARG(ctx);
+ mbedtls_sha256_update(&ctx->c, data, len);
+ return 0;
+}
+
+int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
+{
+ GIT_ASSERT_ARG(ctx);
+ mbedtls_sha256_finish(&ctx->c, out);
+ return 0;
+}
+
+#endif
diff --git a/src/util/hash/mbedtls.h b/src/util/hash/mbedtls.h
index efe9c07a5..05fb38b0e 100644
--- a/src/util/hash/mbedtls.h
+++ b/src/util/hash/mbedtls.h
@@ -10,10 +10,20 @@
#include "hash/sha.h"
-#include <mbedtls/sha1.h>
+#ifdef GIT_SHA1_MBEDTLS
+# include <mbedtls/sha1.h>
struct git_hash_sha1_ctx {
mbedtls_sha1_context c;
};
+#endif
+
+#ifdef GIT_SHA256_MBEDTLS
+# include <mbedtls/sha256.h>
+
+struct git_hash_sha256_ctx {
+ mbedtls_sha256_context c;
+};
+#endif
#endif /* INCLUDE_hash_sha1_mbedtls_h__ */