Skip to content

Commit 7f7dfe7

Browse files
committed
Add OpenSSL-FIPS CMake flag
Usage of the deprecated 'SHA256_*' OpenSSL API in a FIPS compliant environment results in OpenSSL's assertion failure with the following description: "OpenSSL internal error, assertion failed: Low level API call to digest SHA256 forbidden in FIPS mode!" This commit adds a possibility to use the OpenSSL's 'EVP_MD*' API instead of the deprecated 'SHA256_*' API, by extending the optional CMake flag 'USE_SHA256' with the new option called 'OpenSSL-FIPS'. The new option is used to choose a hashing backend used by libgit2 to calculate SHA256 hashes, in a similar way that currently existing options like 'OpenSSL', 'OpenSSL-Dynamic', 'mbedTLS' etc do. 'OpenSSL-FIPS' is a fully opt-in option which is purposely not interfering with the existing options, because, after running some benchmarks, it's been discovered that using the 'EVP_MD*' API causes hashing to be a bit slower in comparison to using the deprecated 'SHA256_*' API. Another change introduced in this commit is the enhancement of the Nightly workflow (nightly.yml) which will cause libgit2 to be automatically built with '-DUSE_SHA256="OpenSSL-FIPS"' CMake flag, on Linux, macOS and Windows.
1 parent 0da8e72 commit 7f7dfe7

File tree

7 files changed

+116
-3
lines changed

7 files changed

+116
-3
lines changed

.github/workflows/nightly.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,34 @@ jobs:
373373
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DEXPERIMENTAL_SHA256=ON
374374
SKIP_SSH_TESTS: true
375375
SKIP_NEGOTIATE_TESTS: true
376+
- name: "Linux (SHA256-FIPS, Xenial, Clang, OpenSSL)"
377+
id: linux-sha256-fips
378+
container:
379+
name: xenial
380+
env:
381+
CC: clang
382+
CMAKE_GENERATOR: Ninja
383+
CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON -DUSE_SHA256="OpenSSL-FIPS"
384+
os: ubuntu-latest
385+
- name: "macOS (SHA256-FIPS)"
386+
id: macos-sha256-fips
387+
os: macos-13
388+
setup-script: osx
389+
env:
390+
CC: clang
391+
CMAKE_OPTIONS: -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=leaks -DUSE_GSSAPI=ON -DEXPERIMENTAL_SHA256=ON -DUSE_SHA256="OpenSSL-FIPS"
392+
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
393+
SKIP_SSH_TESTS: true
394+
SKIP_NEGOTIATE_TESTS: true
395+
- name: "Windows (SHA256-FIPS, amd64, Visual Studio)"
396+
id: windows-sha256-fips
397+
os: windows-2022
398+
env:
399+
ARCH: amd64
400+
CMAKE_GENERATOR: Visual Studio 17 2022
401+
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DEXPERIMENTAL_SHA256=ON -DUSE_SHA256="OpenSSL-FIPS"
402+
SKIP_SSH_TESTS: true
403+
SKIP_NEGOTIATE_TESTS: true
376404
fail-fast: false
377405
env: ${{ matrix.platform.env }}
378406
runs-on: ${{ matrix.platform.os }}

cmake/SelectHashes.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ elseif(USE_SHA256 STREQUAL "OpenSSL-Dynamic")
7070
set(GIT_SHA256_OPENSSL 1)
7171
set(GIT_SHA256_OPENSSL_DYNAMIC 1)
7272
list(APPEND LIBGIT2_SYSTEM_LIBS dl)
73+
elseif(USE_SHA256 STREQUAL "OpenSSL-FIPS")
74+
set(GIT_SHA256_OPENSSL_FIPS 1)
7375
elseif(USE_SHA256 STREQUAL "CommonCrypto")
7476
set(GIT_SHA256_COMMON_CRYPTO 1)
7577
elseif(USE_SHA256 STREQUAL "mbedTLS")

src/util/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ list(SORT UTIL_SRC_SHA1)
5353

5454
if(USE_SHA256 STREQUAL "Builtin")
5555
file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*)
56-
elseif(USE_SHA256 STREQUAL "OpenSSL" OR USE_SHA256 STREQUAL "OpenSSL-Dynamic")
56+
elseif(USE_SHA256 STREQUAL "OpenSSL" OR USE_SHA256 STREQUAL "OpenSSL-Dynamic" OR USE_SHA256 STREQUAL "OpenSSL-FIPS")
5757
add_definitions(-DOPENSSL_API_COMPAT=0x10100000L)
5858
file(GLOB UTIL_SRC_SHA256 hash/openssl.*)
5959
elseif(USE_SHA256 STREQUAL "CommonCrypto")

src/util/git2_features.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#cmakedefine GIT_SHA256_COMMON_CRYPTO 1
6363
#cmakedefine GIT_SHA256_OPENSSL 1
6464
#cmakedefine GIT_SHA256_OPENSSL_DYNAMIC 1
65+
#cmakedefine GIT_SHA256_OPENSSL_FIPS 1
6566
#cmakedefine GIT_SHA256_MBEDTLS 1
6667

6768
#cmakedefine GIT_RAND_GETENTROPY 1

src/util/hash/openssl.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,75 @@ int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
193193
}
194194

195195
#endif
196+
197+
#ifdef GIT_SHA256_OPENSSL_FIPS
198+
199+
static const EVP_MD* SHA256_ENGINE_DIGEST_TYPE = NULL;
200+
201+
int git_hash_sha256_global_init(void)
202+
{
203+
SHA256_ENGINE_DIGEST_TYPE = EVP_sha256();
204+
return SHA256_ENGINE_DIGEST_TYPE != NULL ? 0 : -1;
205+
}
206+
207+
int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx)
208+
{
209+
return git_hash_sha256_init(ctx);
210+
}
211+
212+
void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx)
213+
{
214+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
215+
EVP_MD_CTX_destroy(ctx->c);
216+
#else
217+
EVP_MD_CTX_free(ctx->c);
218+
#endif
219+
}
220+
221+
int git_hash_sha256_init(git_hash_sha256_ctx *ctx)
222+
{
223+
GIT_ASSERT_ARG(ctx);
224+
225+
GIT_ASSERT(SHA256_ENGINE_DIGEST_TYPE);
226+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
227+
ctx->c = EVP_MD_CTX_create();
228+
#else
229+
ctx->c = EVP_MD_CTX_new();
230+
#endif
231+
GIT_ASSERT(ctx->c);
232+
233+
if (EVP_DigestInit_ex(ctx->c, SHA256_ENGINE_DIGEST_TYPE, NULL) != 1) {
234+
git_hash_sha256_ctx_cleanup(ctx);
235+
git_error_set(GIT_ERROR_SHA, "failed to initialize sha256 context");
236+
return -1;
237+
}
238+
239+
return 0;
240+
}
241+
242+
int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len)
243+
{
244+
GIT_ASSERT_ARG(ctx);
245+
246+
if (EVP_DigestUpdate(ctx->c, data, len) != 1) {
247+
git_error_set(GIT_ERROR_SHA, "failed to update sha256");
248+
return -1;
249+
}
250+
251+
return 0;
252+
}
253+
254+
int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
255+
{
256+
unsigned int len = 0;
257+
GIT_ASSERT_ARG(ctx);
258+
259+
if (EVP_DigestFinal(ctx->c, out, &len) != 1) {
260+
git_error_set(GIT_ERROR_SHA, "failed to finalize sha256");
261+
return -1;
262+
}
263+
264+
return 0;
265+
}
266+
267+
#endif

src/util/hash/openssl.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
#include "hash/sha.h"
1212

1313
#ifndef GIT_OPENSSL_DYNAMIC
14-
# include <openssl/sha.h>
14+
#ifdef GIT_SHA256_OPENSSL_FIPS
15+
#include <openssl/evp.h>
16+
#else
17+
#include <openssl/sha.h>
18+
#endif
1519
#else
1620

1721
typedef struct {
@@ -42,4 +46,10 @@ struct git_hash_sha256_ctx {
4246
};
4347
#endif
4448

49+
#ifdef GIT_SHA256_OPENSSL_FIPS
50+
struct git_hash_sha256_ctx {
51+
EVP_MD_CTX* c;
52+
};
53+
#endif
54+
4555
#endif

src/util/hash/sha.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef struct git_hash_sha256_ctx git_hash_sha256_ctx;
1717
# include "common_crypto.h"
1818
#endif
1919

20-
#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA256_OPENSSL)
20+
#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA256_OPENSSL) || defined(GIT_SHA256_OPENSSL_FIPS)
2121
# include "openssl.h"
2222
#endif
2323

0 commit comments

Comments
 (0)