@@ -120,6 +120,82 @@ int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx)
120120
121121#endif
122122
123+ #ifdef GIT_SHA1_OPENSSL_FIPS
124+
125+ static const EVP_MD * SHA1_ENGINE_DIGEST_TYPE = NULL ;
126+
127+ int git_hash_sha1_global_init (void )
128+ {
129+ SHA1_ENGINE_DIGEST_TYPE = EVP_sha1 ();
130+ return SHA1_ENGINE_DIGEST_TYPE != NULL ? 0 : -1 ;
131+ }
132+
133+ int git_hash_sha1_ctx_init (git_hash_sha1_ctx * ctx )
134+ {
135+ return git_hash_sha1_init (ctx );
136+ }
137+
138+ void git_hash_sha1_ctx_cleanup (git_hash_sha1_ctx * ctx )
139+ {
140+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
141+ EVP_MD_CTX_destroy (ctx -> c );
142+ #else
143+ EVP_MD_CTX_free (ctx -> c );
144+ #endif
145+ }
146+
147+ int git_hash_sha1_init (git_hash_sha1_ctx * ctx )
148+ {
149+ GIT_ASSERT_ARG (ctx );
150+ GIT_ASSERT (SHA1_ENGINE_DIGEST_TYPE );
151+
152+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
153+ ctx -> c = EVP_MD_CTX_create ();
154+ #else
155+ ctx -> c = EVP_MD_CTX_new ();
156+ #endif
157+
158+ GIT_ASSERT (ctx -> c );
159+
160+ if (EVP_DigestInit_ex (ctx -> c , SHA1_ENGINE_DIGEST_TYPE , NULL ) != 1 ) {
161+ git_hash_sha1_ctx_cleanup (ctx );
162+ git_error_set (GIT_ERROR_SHA , "failed to initialize sha1 context" );
163+ return -1 ;
164+ }
165+
166+ return 0 ;
167+ }
168+
169+ int git_hash_sha1_update (git_hash_sha1_ctx * ctx , const void * data , size_t len )
170+ {
171+ GIT_ASSERT_ARG (ctx && ctx -> c );
172+
173+ if (EVP_DigestUpdate (ctx -> c , data , len ) != 1 ) {
174+ git_error_set (GIT_ERROR_SHA , "failed to update sha1" );
175+ return -1 ;
176+ }
177+
178+ return 0 ;
179+ }
180+
181+ int git_hash_sha1_final (unsigned char * out , git_hash_sha1_ctx * ctx )
182+ {
183+ unsigned int len = 0 ;
184+
185+ GIT_ASSERT_ARG (ctx && ctx -> c );
186+
187+ if (EVP_DigestFinal (ctx -> c , out , & len ) != 1 ) {
188+ git_error_set (GIT_ERROR_SHA , "failed to finalize sha1" );
189+ return -1 ;
190+ }
191+
192+ ctx -> c = NULL ;
193+
194+ return 0 ;
195+ }
196+
197+ #endif
198+
123199#ifdef GIT_SHA256_OPENSSL
124200
125201# ifdef GIT_OPENSSL_DYNAMIC
@@ -193,3 +269,79 @@ int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
193269}
194270
195271#endif
272+
273+ #ifdef GIT_SHA256_OPENSSL_FIPS
274+
275+ static const EVP_MD * SHA256_ENGINE_DIGEST_TYPE = NULL ;
276+
277+ int git_hash_sha256_global_init (void )
278+ {
279+ SHA256_ENGINE_DIGEST_TYPE = EVP_sha256 ();
280+ return SHA256_ENGINE_DIGEST_TYPE != NULL ? 0 : -1 ;
281+ }
282+
283+ int git_hash_sha256_ctx_init (git_hash_sha256_ctx * ctx )
284+ {
285+ return git_hash_sha256_init (ctx );
286+ }
287+
288+ void git_hash_sha256_ctx_cleanup (git_hash_sha256_ctx * ctx )
289+ {
290+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
291+ EVP_MD_CTX_destroy (ctx -> c );
292+ #else
293+ EVP_MD_CTX_free (ctx -> c );
294+ #endif
295+ }
296+
297+ int git_hash_sha256_init (git_hash_sha256_ctx * ctx )
298+ {
299+ GIT_ASSERT_ARG (ctx );
300+ GIT_ASSERT (SHA256_ENGINE_DIGEST_TYPE );
301+
302+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
303+ ctx -> c = EVP_MD_CTX_create ();
304+ #else
305+ ctx -> c = EVP_MD_CTX_new ();
306+ #endif
307+
308+ GIT_ASSERT (ctx -> c );
309+
310+ if (EVP_DigestInit_ex (ctx -> c , SHA256_ENGINE_DIGEST_TYPE , NULL ) != 1 ) {
311+ git_hash_sha256_ctx_cleanup (ctx );
312+ git_error_set (GIT_ERROR_SHA , "failed to initialize sha256 context" );
313+ return -1 ;
314+ }
315+
316+ return 0 ;
317+ }
318+
319+ int git_hash_sha256_update (git_hash_sha256_ctx * ctx , const void * data , size_t len )
320+ {
321+ GIT_ASSERT_ARG (ctx && ctx -> c );
322+
323+ if (EVP_DigestUpdate (ctx -> c , data , len ) != 1 ) {
324+ git_error_set (GIT_ERROR_SHA , "failed to update sha256" );
325+ return -1 ;
326+ }
327+
328+ return 0 ;
329+ }
330+
331+ int git_hash_sha256_final (unsigned char * out , git_hash_sha256_ctx * ctx )
332+ {
333+ unsigned int len = 0 ;
334+
335+ GIT_ASSERT_ARG (ctx && ctx -> c );
336+
337+ if (EVP_DigestFinal (ctx -> c , out , & len ) != 1 ) {
338+ git_error_set (GIT_ERROR_SHA , "failed to finalize sha256" );
339+ return -1 ;
340+ }
341+
342+ ctx -> c = NULL ;
343+
344+ return 0 ;
345+ }
346+
347+ #endif
0 commit comments