Skip to content

Commit 35afaa2

Browse files
committed
add compatibility code for older ssl libs
1 parent 21246e0 commit 35afaa2

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

common/gm_crypt.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+ */
142144
static TLS EVP_MAC *mac = NULL;
143145
static 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+
144151
int 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++){

include/gm_crypt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include <string.h>
3434
#include <openssl/evp.h>
3535
#include <openssl/hmac.h>
36+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3637
#include <openssl/core_names.h>
38+
#endif
3739
#include <openssl/err.h>
3840

3941
#define KEYBITS 256 /**< key size */

0 commit comments

Comments
 (0)