1 /*
2  * SHA-224 functions
3  *
4  * Copyright (C) 2011-2020, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #if !defined( _LIBHMAC_SHA224_H )
23 #define _LIBHMAC_SHA224_H
24 
25 #include <common.h>
26 #include <types.h>
27 
28 #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H )
29 #include <openssl/sha.h>
30 
31 #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H )
32 #include <openssl/evp.h>
33 
34 #endif
35 
36 #include "libhmac_extern.h"
37 #include "libhmac_libcerror.h"
38 #include "libhmac_types.h"
39 
40 #if defined( __cplusplus )
41 extern "C" {
42 #endif
43 
44 #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA224_DIGEST_LENGTH )
45 #define LIBHMAC_HAVE_SHA224_SUPPORT
46 
47 #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA224 )
48 #define LIBHMAC_HAVE_SHA224_SUPPORT
49 
50 #endif
51 
52 #if !defined( LIBHMAC_HAVE_SHA224_SUPPORT )
53 #define LIBHMAC_SHA224_BLOCK_SIZE		64
54 #endif
55 
56 typedef struct libhmac_internal_sha224_context libhmac_internal_sha224_context_t;
57 
58 struct libhmac_internal_sha224_context
59 {
60 #if defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA224_DIGEST_LENGTH )
61 	/* The SHA-224 functions use the SHA-256 context
62 	 */
63 	SHA256_CTX sha224_context;
64 
65 #elif defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_EVP_H ) && defined( HAVE_EVP_SHA224 )
66 	/* The EVP message digest context
67 	 */
68 #if defined( HAVE_EVP_MD_CTX_INIT )
69 	EVP_MD_CTX internal_evp_md_context;
70 #endif
71 
72 	EVP_MD_CTX *evp_md_context;
73 
74 #else
75 	/* The number of bytes hashed
76 	 */
77 	uint64_t hash_count;
78 
79 	/* The 32-bit hash values
80 	 */
81 	uint32_t hash_values[ 8 ];
82 
83 	/* The block offset
84 	 */
85 	size_t block_offset;
86 
87 	/* The (data) block
88 	 */
89 	uint8_t block[ 128 ];
90 
91 #endif /* defined( HAVE_LIBCRYPTO ) && defined( HAVE_OPENSSL_SHA_H ) && defined( SHA224_DIGEST_LENGTH ) */
92 };
93 
94 #if !defined( LIBHMAC_HAVE_SHA224_SUPPORT )
95 
96 ssize_t libhmac_sha224_transform(
97          libhmac_internal_sha224_context_t *internal_context,
98          const uint8_t *buffer,
99          size_t size,
100          libcerror_error_t **error );
101 
102 #endif /* !defined( LIBHMAC_HAVE_SHA224_SUPPORT ) */
103 
104 LIBHMAC_EXTERN \
105 int libhmac_sha224_initialize(
106      libhmac_sha224_context_t **context,
107      libcerror_error_t **error );
108 
109 LIBHMAC_EXTERN \
110 int libhmac_sha224_free(
111      libhmac_sha224_context_t **context,
112      libcerror_error_t **error );
113 
114 LIBHMAC_EXTERN \
115 int libhmac_sha224_update(
116      libhmac_sha224_context_t *context,
117      const uint8_t *buffer,
118      size_t size,
119      libcerror_error_t **error );
120 
121 LIBHMAC_EXTERN \
122 int libhmac_sha224_finalize(
123      libhmac_sha224_context_t *context,
124      uint8_t *hash,
125      size_t hash_size,
126      libcerror_error_t **error );
127 
128 LIBHMAC_EXTERN \
129 int libhmac_sha224_calculate(
130      const uint8_t *buffer,
131      size_t size,
132      uint8_t *hash,
133      size_t hash_size,
134      libcerror_error_t **error );
135 
136 LIBHMAC_EXTERN \
137 int libhmac_sha224_calculate_hmac(
138      const uint8_t *key,
139      size_t key_size,
140      const uint8_t *buffer,
141      size_t size,
142      uint8_t *hmac,
143      size_t hmac_size,
144      libcerror_error_t **error );
145 
146 #if defined( __cplusplus )
147 }
148 #endif
149 
150 #endif /* !defined( _LIBHMAC_SHA224_H ) */
151 
152