From a4eeefc13f74e46ea7cec9aeae11c7c3652cabc2 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Thu, 23 Feb 2023 15:15:04 +0100
Subject: [PATCH] support old versions of openssl

Needed for ubuntu 16 and centos 7.
---
 openair3/SECU/sha_256_hmac.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/openair3/SECU/sha_256_hmac.c b/openair3/SECU/sha_256_hmac.c
index ae49b1d1a23..528b2821a1a 100644
--- a/openair3/SECU/sha_256_hmac.c
+++ b/openair3/SECU/sha_256_hmac.c
@@ -24,8 +24,9 @@
 
 #include <openssl/hmac.h>
 
-#if OPENSSL_VERSION_MAJOR >= 3
-// code for version 3.0 or greater
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+
+/* code for version 3.0 or greater */
 
 #include <openssl/core_names.h>
 
@@ -72,8 +73,11 @@ void sha_256_hmac(const uint8_t key[32], byte_array_t data, size_t len, uint8_t
   EVP_MAC_free(mac);
   OSSL_LIB_CTX_free(library_context);
 }
-#else
-// code for 1.1.x or lower
+
+#elif OPENSSL_VERSION_NUMBER >= 0x10100000L
+
+/* code for version >= 1.1.0 and < 3.0.0 */
+
 void sha_256_hmac(const uint8_t key[32], byte_array_t data, size_t len, uint8_t out[len])
 {
   DevAssert(key != NULL);
@@ -92,4 +96,27 @@ void sha_256_hmac(const uint8_t key[32], byte_array_t data, size_t len, uint8_t
   HMAC_CTX_free(ctx);
 }
 
+#else
+
+/* code for version lower than 1.1.0 */
+
+void sha_256_hmac(const uint8_t key[32], byte_array_t data, size_t len, uint8_t out[len])
+{
+  DevAssert(key != NULL);
+  DevAssert(data.buf != NULL);
+  DevAssert(data.len != 0);
+  DevAssert(len != 0);
+
+  HMAC_CTX ctx;
+  HMAC_CTX_init(&ctx);
+
+  HMAC_Init_ex(&ctx, key, 32, EVP_sha256(), NULL);
+
+  HMAC_Update(&ctx, data.buf, data.len);
+
+  HMAC_Final(&ctx, out, (uint32_t*)&len);
+
+  HMAC_CTX_cleanup(&ctx);
+}
+
 #endif
-- 
GitLab