Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ jobs:
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DEXPERIMENTAL_SHA256=ON
SKIP_SSH_TESTS: true
SKIP_NEGOTIATE_TESTS: true
- name: "Linux (SHA256, Xenial, Clang, OpenSSL-FIPS)"
id: linux-sha256-fips
container:
name: xenial
env:
CC: clang
CMAKE_GENERATOR: Ninja
CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON -DUSE_SHA1=OpenSSL-FIPS -DUSE_SHA256=OpenSSL-FIPS
os: ubuntu-latest
fail-fast: false
env: ${{ matrix.platform.env }}
runs-on: ${{ matrix.platform.os }}
Expand Down
7 changes: 6 additions & 1 deletion cmake/SelectHashes.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ if(USE_SHA1 STREQUAL "CollisionDetection")
set(GIT_SHA1_COLLISIONDETECT 1)
elseif(USE_SHA1 STREQUAL "OpenSSL")
set(GIT_SHA1_OPENSSL 1)
elseif(USE_SHA1 STREQUAL "OpenSSL-FIPS")
set(GIT_SHA1_OPENSSL_FIPS 1)
elseif(USE_SHA1 STREQUAL "OpenSSL-Dynamic")
set(GIT_SHA1_OPENSSL 1)
set(GIT_SHA1_OPENSSL_DYNAMIC 1)
Expand Down Expand Up @@ -66,6 +68,8 @@ if(USE_SHA256 STREQUAL "Builtin")
set(GIT_SHA256_BUILTIN 1)
elseif(USE_SHA256 STREQUAL "OpenSSL")
set(GIT_SHA256_OPENSSL 1)
elseif(USE_SHA256 STREQUAL "OpenSSL-FIPS")
set(GIT_SHA256_OPENSSL_FIPS 1)
elseif(USE_SHA256 STREQUAL "OpenSSL-Dynamic")
set(GIT_SHA256_OPENSSL 1)
set(GIT_SHA256_OPENSSL_DYNAMIC 1)
Expand All @@ -81,7 +85,8 @@ else()
endif()

# add library requirements
if(USE_SHA1 STREQUAL "OpenSSL" OR USE_SHA256 STREQUAL "OpenSSL")
if(USE_SHA1 STREQUAL "OpenSSL" OR USE_SHA256 STREQUAL "OpenSSL" OR
USE_SHA1 STREQUAL "OpenSSL-FIPS" OR USE_SHA256 STREQUAL "OpenSSL-FIPS")
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
list(APPEND LIBGIT2_PC_LIBS "-lssl")
else()
Expand Down
12 changes: 9 additions & 3 deletions src/libgit2/commit_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,9 +1027,12 @@ static int commit_graph_write_hash(const char *buf, size_t size, void *data)
struct commit_graph_write_hash_context *ctx = data;
int error;

error = git_hash_update(ctx->ctx, buf, size);
if (error < 0)
return error;
if (ctx->ctx) {
error = git_hash_update(ctx->ctx, buf, size);

if (error < 0)
return error;
}

return ctx->write_cb(buf, size, ctx->cb_data);
}
Expand Down Expand Up @@ -1225,6 +1228,9 @@ static int commit_graph_write(
error = git_hash_final(checksum, &ctx);
if (error < 0)
goto cleanup;

hash_cb_data.ctx = NULL;

error = write_cb((char *)checksum, checksum_size, cb_data);
if (error < 0)
goto cleanup;
Expand Down
11 changes: 8 additions & 3 deletions src/libgit2/midx.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,11 @@ static int midx_write_hash(const char *buf, size_t size, void *data)
struct midx_write_hash_context *ctx = (struct midx_write_hash_context *)data;
int error;

error = git_hash_update(ctx->ctx, buf, size);
if (error < 0)
return error;
if (ctx->ctx) {
error = git_hash_update(ctx->ctx, buf, size);
if (error < 0)
return error;
}

return ctx->write_cb(buf, size, ctx->cb_data);
}
Expand Down Expand Up @@ -863,6 +865,9 @@ static int midx_write(
error = git_hash_final(checksum, &ctx);
if (error < 0)
goto cleanup;

hash_cb_data.ctx = NULL;

error = write_cb((char *)checksum, checksum_size, cb_data);
if (error < 0)
goto cleanup;
Expand Down
4 changes: 2 additions & 2 deletions src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if(USE_SHA1 STREQUAL "CollisionDetection")
target_compile_definitions(util PRIVATE SHA1DC_NO_STANDARD_INCLUDES=1)
target_compile_definitions(util PRIVATE SHA1DC_CUSTOM_INCLUDE_SHA1_C=\"git2_util.h\")
target_compile_definitions(util PRIVATE SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"git2_util.h\")
elseif(USE_SHA1 STREQUAL "OpenSSL" OR USE_SHA1 STREQUAL "OpenSSL-Dynamic")
elseif(USE_SHA1 STREQUAL "OpenSSL" OR USE_SHA1 STREQUAL "OpenSSL-Dynamic" OR USE_SHA1 STREQUAL "OpenSSL-FIPS")
add_definitions(-DOPENSSL_API_COMPAT=0x10100000L)
file(GLOB UTIL_SRC_SHA1 hash/openssl.*)
elseif(USE_SHA1 STREQUAL "CommonCrypto")
Expand All @@ -53,7 +53,7 @@ list(SORT UTIL_SRC_SHA1)

if(USE_SHA256 STREQUAL "Builtin")
file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*)
elseif(USE_SHA256 STREQUAL "OpenSSL" OR USE_SHA256 STREQUAL "OpenSSL-Dynamic")
elseif(USE_SHA256 STREQUAL "OpenSSL" OR USE_SHA256 STREQUAL "OpenSSL-Dynamic" OR USE_SHA256 STREQUAL "OpenSSL-FIPS")
add_definitions(-DOPENSSL_API_COMPAT=0x10100000L)
file(GLOB UTIL_SRC_SHA256 hash/openssl.*)
elseif(USE_SHA256 STREQUAL "CommonCrypto")
Expand Down
2 changes: 2 additions & 0 deletions src/util/git2_features.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
#cmakedefine GIT_SHA1_WIN32 1
#cmakedefine GIT_SHA1_COMMON_CRYPTO 1
#cmakedefine GIT_SHA1_OPENSSL 1
#cmakedefine GIT_SHA1_OPENSSL_FIPS 1
#cmakedefine GIT_SHA1_OPENSSL_DYNAMIC 1
#cmakedefine GIT_SHA1_MBEDTLS 1

#cmakedefine GIT_SHA256_BUILTIN 1
#cmakedefine GIT_SHA256_WIN32 1
#cmakedefine GIT_SHA256_COMMON_CRYPTO 1
#cmakedefine GIT_SHA256_OPENSSL 1
#cmakedefine GIT_SHA256_OPENSSL_FIPS 1
#cmakedefine GIT_SHA256_OPENSSL_DYNAMIC 1
#cmakedefine GIT_SHA256_MBEDTLS 1

Expand Down
152 changes: 152 additions & 0 deletions src/util/hash/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,82 @@ int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx)

#endif

#ifdef GIT_SHA1_OPENSSL_FIPS

static const EVP_MD *SHA1_ENGINE_DIGEST_TYPE = NULL;

int git_hash_sha1_global_init(void)
{
SHA1_ENGINE_DIGEST_TYPE = EVP_sha1();
return SHA1_ENGINE_DIGEST_TYPE != NULL ? 0 : -1;
}

int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
return git_hash_sha1_init(ctx);
}

void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_MD_CTX_destroy(ctx->c);
#else
EVP_MD_CTX_free(ctx->c);
#endif
}

int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
GIT_ASSERT_ARG(ctx);
GIT_ASSERT(SHA1_ENGINE_DIGEST_TYPE);

#if OPENSSL_VERSION_NUMBER < 0x10100000L
ctx->c = EVP_MD_CTX_create();
#else
ctx->c = EVP_MD_CTX_new();
#endif

GIT_ASSERT(ctx->c);

if (EVP_DigestInit_ex(ctx->c, SHA1_ENGINE_DIGEST_TYPE, NULL) != 1) {
git_hash_sha1_ctx_cleanup(ctx);
git_error_set(GIT_ERROR_SHA, "failed to initialize sha1 context");
return -1;
}

return 0;
}

int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
{
GIT_ASSERT_ARG(ctx && ctx->c);

if (EVP_DigestUpdate(ctx->c, data, len) != 1) {
git_error_set(GIT_ERROR_SHA, "failed to update sha1");
return -1;
}

return 0;
}

int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx)
{
unsigned int len = 0;

GIT_ASSERT_ARG(ctx && ctx->c);

if (EVP_DigestFinal(ctx->c, out, &len) != 1) {
git_error_set(GIT_ERROR_SHA, "failed to finalize sha1");
return -1;
}

ctx->c = NULL;

return 0;
}

#endif

#ifdef GIT_SHA256_OPENSSL

# ifdef GIT_OPENSSL_DYNAMIC
Expand Down Expand Up @@ -193,3 +269,79 @@ int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
}

#endif

#ifdef GIT_SHA256_OPENSSL_FIPS

static const EVP_MD *SHA256_ENGINE_DIGEST_TYPE = NULL;

int git_hash_sha256_global_init(void)
{
SHA256_ENGINE_DIGEST_TYPE = EVP_sha256();
return SHA256_ENGINE_DIGEST_TYPE != NULL ? 0 : -1;
}

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 OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_MD_CTX_destroy(ctx->c);
#else
EVP_MD_CTX_free(ctx->c);
#endif
}

int git_hash_sha256_init(git_hash_sha256_ctx *ctx)
{
GIT_ASSERT_ARG(ctx);
GIT_ASSERT(SHA256_ENGINE_DIGEST_TYPE);

#if OPENSSL_VERSION_NUMBER < 0x10100000L
ctx->c = EVP_MD_CTX_create();
#else
ctx->c = EVP_MD_CTX_new();
#endif

GIT_ASSERT(ctx->c);

if (EVP_DigestInit_ex(ctx->c, SHA256_ENGINE_DIGEST_TYPE, NULL) != 1) {
git_hash_sha256_ctx_cleanup(ctx);
git_error_set(GIT_ERROR_SHA, "failed to initialize sha256 context");
return -1;
}

return 0;
}

int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len)
{
GIT_ASSERT_ARG(ctx && ctx->c);

if (EVP_DigestUpdate(ctx->c, data, len) != 1) {
git_error_set(GIT_ERROR_SHA, "failed to update sha256");
return -1;
}

return 0;
}

int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
{
unsigned int len = 0;

GIT_ASSERT_ARG(ctx && ctx->c);

if (EVP_DigestFinal(ctx->c, out, &len) != 1) {
git_error_set(GIT_ERROR_SHA, "failed to finalize sha256");
return -1;
}

ctx->c = NULL;

return 0;
}

#endif
18 changes: 17 additions & 1 deletion src/util/hash/openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
#include "hash/sha.h"

#ifndef GIT_OPENSSL_DYNAMIC
# include <openssl/sha.h>
# if defined(GIT_SHA1_OPENSSL_FIPS) || defined(GIT_SHA256_OPENSSL_FIPS)
# include <openssl/evp.h>
# else
# include <openssl/sha.h>
# endif
#else

typedef struct {
Expand All @@ -36,10 +40,22 @@ struct git_hash_sha1_ctx {
};
#endif

#ifdef GIT_SHA1_OPENSSL_FIPS
struct git_hash_sha1_ctx {
EVP_MD_CTX* c;
};
#endif

#ifdef GIT_SHA256_OPENSSL
struct git_hash_sha256_ctx {
SHA256_CTX c;
};
#endif

#ifdef GIT_SHA256_OPENSSL_FIPS
struct git_hash_sha256_ctx {
EVP_MD_CTX* c;
};
#endif

#endif
5 changes: 4 additions & 1 deletion src/util/hash/sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ typedef struct git_hash_sha256_ctx git_hash_sha256_ctx;
# include "common_crypto.h"
#endif

#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA256_OPENSSL)
#if defined(GIT_SHA1_OPENSSL) || \
defined(GIT_SHA1_OPENSSL_FIPS) || \
defined(GIT_SHA256_OPENSSL) || \
defined(GIT_SHA256_OPENSSL_FIPS)
# include "openssl.h"
#endif

Expand Down
Loading