@@ -139,9 +139,18 @@ int mod_gm_aes_decrypt(EVP_CIPHER_CTX * ctx, unsigned char * plaintext, unsigned
139139 return 1 ;
140140}
141141
142+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
143+ /* OpenSSL 3.0+ */
142144static TLS EVP_MAC * mac = NULL ;
143145static TLS EVP_MAC_CTX * mac_ctx = NULL ;
146+ #else
147+ /* OpenSSL 1.1.x and earlier */
148+ static TLS HMAC_CTX * hmac_ctx = NULL ;
149+ #endif
150+
144151int hmac_sha256_init () {
152+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
153+ /* OpenSSL 3.0+ */
145154 OSSL_PARAM params [] = {
146155 OSSL_PARAM_construct_utf8_string (OSSL_MAC_PARAM_DIGEST ,
147156 (char * )"SHA256" , 0 ),
@@ -161,6 +170,19 @@ int hmac_sha256_init() {
161170 }
162171
163172 return EVP_MAC_init (mac_ctx , key , KEYBYTES , params ) == 1 ;
173+ #else
174+ /* OpenSSL 1.1.x and earlier */
175+ if (hmac_ctx == NULL ) {
176+ hmac_ctx = HMAC_CTX_new ();
177+ if (!hmac_ctx )
178+ return 0 ;
179+ }
180+
181+ if (HMAC_Init_ex (hmac_ctx , key , KEYBYTES , EVP_sha256 (), NULL ) != 1 )
182+ return 0 ;
183+
184+ return 1 ;
185+ #endif
164186}
165187
166188/* create hex sum for char[] */
@@ -170,6 +192,9 @@ void mod_gm_hexsum(char *dest, char *text) {
170192 unsigned int i = 0 ;
171193
172194 hmac_sha256_init ();
195+
196+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
197+ /* OpenSSL 3.0+ */
173198 if (mac_ctx == NULL ) {
174199 fprintf (stderr , "failed to initialize HMAC context\n" );
175200 exit (1 );
@@ -179,6 +204,24 @@ void mod_gm_hexsum(char *dest, char *text) {
179204 fprintf (stderr , "HMAC computation failed\n" );
180205 exit (1 );
181206 }
207+ #else
208+ /* OpenSSL 1.1.x and earlier */
209+ if (hmac_ctx == NULL ) {
210+ fprintf (stderr , "failed to initialize HMAC context\n" );
211+ exit (1 );
212+ }
213+
214+ if (HMAC_Update (hmac_ctx , (const unsigned char * )text , strlen (text )) != 1 ) {
215+ fprintf (stderr , "HMAC computation failed\n" );
216+ exit (1 );
217+ }
218+
219+ resultlen = sizeof (result );
220+ if (HMAC_Final (hmac_ctx , result , (unsigned int * )& resultlen ) != 1 ) {
221+ fprintf (stderr , "HMAC computation failed\n" );
222+ exit (1 );
223+ }
224+ #endif
182225
183226 dest [0 ] = 0 ;
184227 for (i = 0 ; i < resultlen ; i ++ ){
0 commit comments