Skip to content

Commit 703651f

Browse files
coder-sageresjyao1
authored andcommitted
Remove deprecated interfaces and internal interfaces, and use modern OpenSSL interfaces
Signed-off-by: coder-sageres <[email protected]>
1 parent 00cb395 commit 703651f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+3146
-26179
lines changed

CMakeLists.txt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,17 +1191,12 @@ else()
11911191
)
11921192
elseif(CRYPTO STREQUAL "openssl")
11931193
set(CRYPTO_DEPS "-lssl -lcrypto")
1194-
if((TOOLCHAIN STREQUAL "NONE") AND (ENABLE_BINARY_BUILD STREQUAL "0"))
1195-
target_link_libraries(${LIB_NAME}_crypto
1196-
PUBLIC openssllib
1197-
PUBLIC cryptlib_openssl
1198-
)
1199-
else()
1200-
target_link_libraries(${LIB_NAME}_crypto
1201-
PUBLIC ssl
1202-
PUBLIC crypto
1203-
)
1204-
endif()
1194+
# Always link to the OpenSSL library built from source (openssllib target)
1195+
# openssllib will be STATIC when TOOLCHAIN=NONE, SHARED otherwise
1196+
target_link_libraries(${LIB_NAME}_crypto
1197+
PUBLIC openssllib
1198+
PUBLIC cryptlib_openssl
1199+
)
12051200
endif()
12061201

12071202
target_link_libraries(${LIB_NAME}

os_stub/cryptlib_openssl/CMakeLists.txt

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ target_include_directories(cryptlib_openssl
99
${LIBSPDM_DIR}/os_stub/include
1010
${LIBSPDM_DIR}/os_stub/cryptlib_openssl
1111
${LIBSPDM_DIR}/os_stub/openssllib/include
12-
${LIBSPDM_DIR}/os_stub/openssllib/openssl_gen
13-
${LIBSPDM_DIR}/os_stub/openssllib/openssl/include
14-
${LIBSPDM_DIR}/os_stub/openssllib/openssl/crypto/include
15-
${LIBSPDM_DIR}/os_stub/openssllib/openssl
1612
)
1713

14+
# Ensure OpenSSL is built before cryptlib_openssl
15+
add_dependencies(cryptlib_openssl openssllib)
16+
1817
target_sources(cryptlib_openssl
1918
PRIVATE
2019
cipher/aead_aes_gcm.c
@@ -52,20 +51,4 @@ target_sources(cryptlib_openssl
5251

5352
target_compile_options(cryptlib_openssl PRIVATE ${OPENSSL_FLAGS})
5453

55-
if(ARCH STREQUAL "x64")
56-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_X64)
57-
elseif(ARCH STREQUAL "ia32")
58-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_IA32)
59-
elseif(ARCH STREQUAL "aarch64")
60-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_AARCH64)
61-
elseif(ARCH STREQUAL "riscv32")
62-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_RISCV32)
63-
elseif(ARCH STREQUAL "riscv64")
64-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_RISCV64)
65-
elseif((ARCH STREQUAL "arm") OR (ARCH STREQUAL "aarch64"))
66-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_ARM)
67-
elseif(ARCH STREQUAL "loongarch64")
68-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_LOONGARCH64)
69-
else()
70-
message(FATAL_ERROR "Unknown ARCH")
71-
endif()
54+
target_link_libraries(cryptlib_openssl PUBLIC openssllib memlib)

os_stub/cryptlib_openssl/cipher/aead_aes_gcm.c

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,27 @@
1212
**/
1313

1414
#include "internal_crypt_lib.h"
15-
#include <openssl/aes.h>
1615
#include <openssl/evp.h>
1716

17+
/**
18+
* Get AES-GCM cipher name by key size
19+
* @param key_size Key size in bytes (16, 24, or 32)
20+
* @return Cipher name string or NULL if invalid key size
21+
*/
22+
static const char *get_aes_gcm_cipher_name(size_t key_size)
23+
{
24+
switch (key_size) {
25+
case 16:
26+
return "AES-128-GCM";
27+
case 24:
28+
return "AES-192-GCM";
29+
case 32:
30+
return "AES-256-GCM";
31+
default:
32+
return NULL;
33+
}
34+
}
35+
1836
/**
1937
* Performs AEAD AES-GCM authenticated encryption on a data buffer and additional authenticated data (AAD).
2038
*
@@ -47,9 +65,10 @@ bool libspdm_aead_aes_gcm_encrypt(const uint8_t *key, size_t key_size,
4765
uint8_t *data_out, size_t *data_out_size)
4866
{
4967
EVP_CIPHER_CTX *ctx;
50-
const EVP_CIPHER *cipher;
68+
EVP_CIPHER *cipher;
5169
size_t temp_out_size;
5270
bool ret_value;
71+
const char *cipher_name;
5372

5473
if (data_in_size > INT_MAX) {
5574
return false;
@@ -60,32 +79,33 @@ bool libspdm_aead_aes_gcm_encrypt(const uint8_t *key, size_t key_size,
6079
if (iv_size != 12) {
6180
return false;
6281
}
63-
switch (key_size) {
64-
case 16:
65-
cipher = EVP_aes_128_gcm();
66-
break;
67-
case 24:
68-
cipher = EVP_aes_192_gcm();
69-
break;
70-
case 32:
71-
cipher = EVP_aes_256_gcm();
72-
break;
73-
default:
82+
83+
cipher_name = get_aes_gcm_cipher_name(key_size);
84+
if (cipher_name == NULL) {
85+
return false;
86+
}
87+
88+
cipher = EVP_CIPHER_fetch(NULL, cipher_name, NULL);
89+
if (cipher == NULL) {
7490
return false;
7591
}
92+
7693
if ((tag_size != 12) && (tag_size != 13) && (tag_size != 14) &&
7794
(tag_size != 15) && (tag_size != 16)) {
95+
EVP_CIPHER_free(cipher);
7896
return false;
7997
}
8098
if (data_out_size != NULL) {
8199
if ((*data_out_size > INT_MAX) ||
82100
(*data_out_size < data_in_size)) {
101+
EVP_CIPHER_free(cipher);
83102
return false;
84103
}
85104
}
86105

87106
ctx = EVP_CIPHER_CTX_new();
88107
if (ctx == NULL) {
108+
EVP_CIPHER_free(cipher);
89109
return false;
90110
}
91111

@@ -129,6 +149,7 @@ bool libspdm_aead_aes_gcm_encrypt(const uint8_t *key, size_t key_size,
129149

130150
done:
131151
EVP_CIPHER_CTX_free(ctx);
152+
EVP_CIPHER_free(cipher);
132153
if (!ret_value) {
133154
return ret_value;
134155
}
@@ -173,9 +194,10 @@ bool libspdm_aead_aes_gcm_decrypt(const uint8_t *key, size_t key_size,
173194
uint8_t *data_out, size_t *data_out_size)
174195
{
175196
EVP_CIPHER_CTX *ctx;
176-
const EVP_CIPHER *cipher;
197+
EVP_CIPHER *cipher;
177198
size_t temp_out_size;
178199
bool ret_value;
200+
const char *cipher_name;
179201

180202
if (data_in_size > INT_MAX) {
181203
return false;
@@ -186,32 +208,33 @@ bool libspdm_aead_aes_gcm_decrypt(const uint8_t *key, size_t key_size,
186208
if (iv_size != 12) {
187209
return false;
188210
}
189-
switch (key_size) {
190-
case 16:
191-
cipher = EVP_aes_128_gcm();
192-
break;
193-
case 24:
194-
cipher = EVP_aes_192_gcm();
195-
break;
196-
case 32:
197-
cipher = EVP_aes_256_gcm();
198-
break;
199-
default:
211+
212+
cipher_name = get_aes_gcm_cipher_name(key_size);
213+
if (cipher_name == NULL) {
200214
return false;
201215
}
216+
217+
cipher = EVP_CIPHER_fetch(NULL, cipher_name, NULL);
218+
if (cipher == NULL) {
219+
return false;
220+
}
221+
202222
if ((tag_size != 12) && (tag_size != 13) && (tag_size != 14) &&
203223
(tag_size != 15) && (tag_size != 16)) {
224+
EVP_CIPHER_free(cipher);
204225
return false;
205226
}
206227
if (data_out_size != NULL) {
207228
if ((*data_out_size > INT_MAX) ||
208229
(*data_out_size < data_in_size)) {
230+
EVP_CIPHER_free(cipher);
209231
return false;
210232
}
211233
}
212234

213235
ctx = EVP_CIPHER_CTX_new();
214236
if (ctx == NULL) {
237+
EVP_CIPHER_free(cipher);
215238
return false;
216239
}
217240

@@ -255,6 +278,7 @@ bool libspdm_aead_aes_gcm_decrypt(const uint8_t *key, size_t key_size,
255278

256279
done:
257280
EVP_CIPHER_CTX_free(ctx);
281+
EVP_CIPHER_free(cipher);
258282
if (!ret_value) {
259283
return ret_value;
260284
}

0 commit comments

Comments
 (0)