Skip to content

Commit 6c6be3c

Browse files
committed
mbedtls: use libmbedcrypto for hashing
1 parent 1a1875f commit 6c6be3c

File tree

6 files changed

+67
-1
lines changed

6 files changed

+67
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ OPTION( PROFILE "Generate profiling information" OFF )
4848
OPTION( ENABLE_TRACE "Enables tracing support" OFF )
4949
OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
5050

51-
SET(SHA1_BACKEND "CollisionDetection" CACHE STRING "Backend to use for SHA1. One of Generic, OpenSSL, Win32, CommonCrypto, CollisionDetection. ")
51+
SET(SHA1_BACKEND "CollisionDetection" CACHE STRING "Backend to use for SHA1. One of Generic, OpenSSL, Win32, CommonCrypto, mbedTLS, CollisionDetection. ")
5252
OPTION( USE_SSH "Link with libssh to enable SSH support" ON )
5353
OPTION( USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON )
5454
OPTION( USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF )

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ ELSEIF(SHA1_BACKEND STREQUAL "Win32")
243243
ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto")
244244
ADD_FEATURE_INFO(SHA ON "using CommonCrypto")
245245
SET(GIT_SHA1_COMMON_CRYPTO 1)
246+
ELSEIF (SHA1_BACKEND STREQUAL "mbedTLS")
247+
ADD_FEATURE_INFO(SHA ON "using mbedTLS")
248+
SET(GIT_SHA1_MBEDTLS 1)
249+
FILE(GLOB SRC_SHA1 src/hash/hash_mbedtls.c)
250+
LIST(APPEND LIBGIT2_PC_REQUIRES "mbedtls")
246251
ELSE()
247252
MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend ${SHA1_BACKEND}")
248253
ENDIF()

src/features.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
#cmakedefine GIT_SHA1_WIN32 1
3434
#cmakedefine GIT_SHA1_COMMON_CRYPTO 1
3535
#cmakedefine GIT_SHA1_OPENSSL 1
36+
#cmakedefine GIT_SHA1_MBEDTLS 1
3637

3738
#endif

src/hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx);
2626
# include "hash/hash_openssl.h"
2727
#elif defined(GIT_SHA1_WIN32)
2828
# include "hash/hash_win32.h"
29+
#elif defined(GIT_SHA1_MBEDTLS)
30+
# include "hash/hash_mbedtls.h"
2931
#else
3032
# include "hash/hash_generic.h"
3133
#endif

src/hash/hash_mbedtls.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "common.h"
9+
#include "hash.h"
10+
#include "hash/hash_mbedtls.h"
11+
12+
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
13+
{
14+
assert(ctx);
15+
mbedtls_sha1_free(&ctx->c);
16+
}
17+
18+
int git_hash_init(git_hash_ctx *ctx)
19+
{
20+
assert(ctx);
21+
mbedtls_sha1_init(&ctx->c);
22+
mbedtls_sha1_starts(&ctx->c);
23+
return 0;
24+
}
25+
26+
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
27+
{
28+
assert(ctx);
29+
mbedtls_sha1_update(&ctx->c, data, len);
30+
return 0;
31+
}
32+
33+
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
34+
{
35+
assert(ctx);
36+
mbedtls_sha1_finish(&ctx->c, out->id);
37+
return 0;
38+
}

src/hash/hash_mbedtls.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_hash_mbedtld_h__
9+
#define INCLUDE_hash_mbedtld_h__
10+
11+
#include <mbedtls/sha1.h>
12+
13+
struct git_hash_ctx {
14+
mbedtls_sha1_context c;
15+
};
16+
17+
#define git_hash_global_init() 0
18+
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
19+
20+
#endif /* INCLUDE_hash_mbedtld_h__ */

0 commit comments

Comments
 (0)