测试代码源码、源码如下:
#include
#include
#include
#include
#include
EVP_PKEY* generate_sm2_key_pair() {
EVP_PKEY_CTX *ctx;
EVP_PKEY *pkey = nullptr;
// 创建EVP_PKEY_CTX
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr);
if (!ctx) {
std::cerr << "Error creating EVP_PKEY_CTX for SM2 key generation" << std::endl;
return nullptr;
}
// 初始化SM2密钥对生成
if (EVP_PKEY_keygen_init(ctx) <= 0 ||
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, NID_sm2) <= 0 ||
EVP_PKEY_keygen(ctx, &pkey) <= 0) {
std::cerr << "Error generating SM2 key pair" << std::endl;
EVP_PKEY_CTX_free(ctx);
return nullptr;
}
// 释放资源
EVP_PKEY_CTX_free(ctx);
return pkey;
}
std::string sm2_encrypt(const std::string &plaintext, EVP_PKEY *pubkey) {
EVP_PKEY_CTX *ctx;
unsigned char *ciphertext = nullptr;
size_t ciphertext_len;
// 创建EVP_PKEY_CTX
ctx = EVP_PKEY_CTX_new(pubkey, nullptr);
if (!ctx) {
std::cerr << "Error creating EVP_PKEY_CTX for encryption" << std::endl;
return "";
}
// 初始化加密操作
if (EVP_PKEY_encrypt_init(ctx) <= 0) {
std::cerr << "Error initializing encryption operation" << std::endl;
EVP_PKEY_CTX_free(ctx);
return "";
}
// 设置加密参数(这里可以设置一些参数,例如填充方式)
// EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING);
// 计算加密后的长度
if (EVP_PKEY_encrypt(ctx, nullptr, &ciphertext_len, reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length()) <= 0) {
std::cerr << "Error computing ciphertext length" << std::endl;
EVP_PKEY_CTX_free(ctx);
return "";
}
// 分配内存并执行加密操作
ciphertext = new unsigned char[ciphertext_len];
if (EVP_PKEY_encrypt(ctx, ciphertext, &ciphertext_len, reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length()) <= 0) {
std::cerr << "Error encrypting data" << std::endl;
delete[] ciphertext;
EVP_PKEY_CTX_free(ctx);
return "";
}
// 释放资源
EVP_PKEY_CTX_free(ctx);
// 返回加密后的数据(Base64编码)
std::string encoded_ciphertext(reinterpret_cast<char*>(ciphertext), ciphertext_len);
delete[] ciphertext;
return encoded_ciphertext;
}
std::string sm2_decrypt(const std::string &ciphertext, EVP_PKEY *privkey) {
EVP_PKEY_CTX *ctx;
unsigned char *plaintext = nullptr;
size_t plaintext_len;
// 创建EVP_PKEY_CTX
ctx = EVP_PKEY_CTX_new(privkey, nullptr);
if (!ctx) {
std::cerr << "Error creating EVP_PKEY_CTX for decryption" << std::endl;
return "";
}
// 初始化解密操作
if (EVP_PKEY_decrypt_init(ctx) <= 0) {
std::cerr << "Error initializing decryption operation" << std::endl;
EVP_PKEY_CTX_free(ctx);
return "";
}
// 计算解密后的长度
if (EVP_PKEY_decrypt(ctx, nullptr, &plaintext_len, reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.length()) <= 0) {
std::cerr << "Error computing plaintext length" << std::endl;
EVP_PKEY_CTX_free(ctx);
return "";
}
// 分配内存并执行解密操作
plaintext = new unsigned char[plaintext_len];
if (EVP_PKEY_decrypt(ctx, plaintext, &plaintext_len, reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.length()) <= 0) {
std::cerr << "Error decrypting data" << std::endl;
delete[] plaintext;
EVP_PKEY_CTX_free(ctx);
return "";
}
// 释放资源
EVP_PKEY_CTX_free(ctx);
// 返回解密后的数据
std::string decrypted_plaintext(reinterpret_cast<char*>(plaintext), plaintext_len);
delete[] plaintext;
return decrypted_plaintext;
}
int main() {
EVP_PKEY *pubkey = generate_sm2_key_pair();
EVP_PKEY *privkey = pubkey; // 在实际应用中,公钥和私钥应该分开
if (!pubkey) {
std::cerr << "Error generating SM2 key pair" << std::endl;
return 1;
}
std::string plaintext = "Hello, SM2!";
std::string ciphertext = sm2_encrypt(plaintext, pubkey);
std::string decrypted_text = sm2_decrypt(ciphertext, privkey);
std::cout << "Original Text: " << plaintext << std::endl;
std::cout << "Encrypted Text: " << ciphertext << std::endl;
std::cout << "Decrypted Text: " << decrypted_text << std::endl;
EVP_PKEY_free(pubkey);
return 0;
}
编译命令
g++ -o sm2_example main.cc -lssl -lcrypto
ldd
是 Linux 下的一个命令,用于查看可执行文件或共享库文件的动态链接库依赖关系。通过 ldd
命令,你可以确定一个可执行文件或共享库文件所依赖的动态链接库(也就是它们在运行时需要加载的库文件)。
ldd [OPTIONS] FILE
OPTIONS(可选):ldd
命令支持的一些选项,用于控制输出格式等。常用选项包括:
-v
或 --verbose
:显示详细的版本信息。-u
或 --unused
:显示未使用的直接依赖项。-d
或 --data-relocs
:显示使用 RELRO(Read-only relocations)技术的 ELF 文件的数据重定位信息。FILE(必需):指定要查看动态链接库依赖关系的可执行文件或共享库文件的路径。
ldd sm2_example
sudo ldd /lib/x86_64-linux-gnu/libcrypto.so.1.1
ldd -u sm2_example
这将显示可执行文件中未使用的直接依赖项。
ldd
不是一个单独的软件包,而是 Linux 系统中的一个标准工具,通常随着系统的安装而自动提供。它属于 GNU Binutils 工具集的一部分,用于显示可执行文件或共享库文件的动态链接库依赖关系。
如果您的系统上没有 ldd
命令,这可能是一个不太常见的情况,但您可以尝试重新安装 GNU Binutils 包来获取 ldd
命令。具体的安装步骤可能因您使用的 Linux 发行版而异,以下是几个常见的 Linux 发行版上安装 GNU Binutils 的方法:
sudo apt-get update
sudo apt-get install binutils
sudo yum install binutils
sudo dnf install binutils
sudo zypper install binutils
请注意,您需要具有管理员权限(通常是 sudo
)才能安装软件包。在安装完成后,ldd
命令应该就会可用了。
readelf
是一个用于分析 ELF(Executable and Linkable Format)格式二进制文件(可执行文件和共享库文件)的命令行工具。它通常在Linux系统上预装,并且是 GNU Binutils 工具集的一部分。readelf
可以提供关于二进制文件的各种信息,包括节表、符号表、程序头、段表等。以下是 readelf
命令的详细解释:
readelf [OPTIONS] FILE
OPTIONS(可选):readelf
命令支持的一些选项,用于控制输出格式、显示特定部分的信息等。常用选项包括:
-a
或 --all
:显示所有信息。-h
或 --file-header
:显示文件头信息。-S
或 --sections
:显示节表信息。-s
或 --symbols
:显示符号表信息。-d
或 --dynamic
:显示动态段信息。-r
或 --relocs
:显示重定位信息。FILE(必需):指定要分析的 ELF 格式的可执行文件或共享库文件的路径。
readelf -h sm2_example
这将显示指定 ELF 文件的文件头信息,包括 ELF 类型(32位或64位)、入口地址、段表、节表等。
readelf -S sm2_example
这将显示指定 ELF 文件的节表信息,包括每个节的名称、类型、偏移、大小等。
There are 30 section headers, starting at offset 0x4428:
节头:
[号] 名称 类型 地址 偏移量
大小 全体大小 旗标 链接 信息 对齐
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .interp PROGBITS 00000000004002a8 000002a8
000000000000001c 0000000000000000 A 0 0 1
[ 2] .note.ABI-tag NOTE 00000000004002c4 000002c4
0000000000000020 0000000000000000 A 0 0 4
[ 3] .note.gnu.build-i NOTE 00000000004002e4 000002e4
0000000000000024 0000000000000000 A 0 0 4
[ 4] .gnu.hash GNU_HASH 0000000000400308 00000308
0000000000000038 0000000000000000 A 5 0 8
[ 5] .dynsym DYNSYM 0000000000400340 00000340
0000000000000348 0000000000000018 A 6 1 8
[ 6] .dynstr STRTAB 0000000000400688 00000688
0000000000000481 0000000000000000 A 0 0 1
[ 7] .gnu.version VERSYM 0000000000400b0a 00000b0a
0000000000000046 0000000000000002 A 5 0 2
[ 8] .gnu.version_r VERNEED 0000000000400b50 00000b50
00000000000000a0 0000000000000000 A 6 4 8
[ 9] .rela.dyn RELA 0000000000400bf0 00000bf0
0000000000000060 0000000000000018 A 5 0 8
[10] .rela.plt RELA 0000000000400c50 00000c50
00000000000002d0 0000000000000018 AI 5 23 8
[11] .init PROGBITS 0000000000401000 00001000
0000000000000017 0000000000000000 AX 0 0 4
[12] .plt PROGBITS 0000000000401020 00001020
00000000000001f0 0000000000000010 AX 0 0 16
[13] .text PROGBITS 0000000000401210 00001210
0000000000000af1 0000000000000000 AX 0 0 16
[14] .fini PROGBITS 0000000000401d04 00001d04
0000000000000009 0000000000000000 AX 0 0 4
[15] .rodata PROGBITS 0000000000402000 00002000
00000000000001c5 0000000000000000 A 0 0 8
[16] .eh_frame_hdr PROGBITS 00000000004021c8 000021c8
0000000000000064 0000000000000000 A 0 0 4
[17] .eh_frame PROGBITS 0000000000402230 00002230
00000000000001d8 0000000000000000 A 0 0 8
[18] .gcc_except_table PROGBITS 0000000000402408 00002408
00000000000000a3 0000000000000000 A 0 0 1
[19] .init_array INIT_ARRAY 0000000000403db8 00002db8
0000000000000010 0000000000000008 WA 0 0 8
[20] .fini_array FINI_ARRAY 0000000000403dc8 00002dc8
0000000000000008 0000000000000008 WA 0 0 8
[21] .dynamic DYNAMIC 0000000000403dd0 00002dd0
0000000000000220 0000000000000010 WA 6 0 8
[22] .got PROGBITS 0000000000403ff0 00002ff0
0000000000000010 0000000000000008 WA 0 0 8
[23] .got.plt PROGBITS 0000000000404000 00003000
0000000000000108 0000000000000008 WA 0 0 8
[24] .data PROGBITS 0000000000404108 00003108
0000000000000010 0000000000000000 WA 0 0 8
[25] .bss NOBITS 0000000000404120 00003118
0000000000000238 0000000000000000 WA 0 0 32
[26] .comment PROGBITS 0000000000000000 00003118
0000000000000023 0000000000000001 MS 0 0 1
[27] .symtab SYMTAB 0000000000000000 00003140
0000000000000960 0000000000000018 28 48 8
[28] .strtab STRTAB 0000000000000000 00003aa0
0000000000000871 0000000000000000 0 0 1
[29] .shstrtab STRTAB 0000000000000000 00004311
0000000000000115 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
l (large), p (processor specific)
readelf -s sm2_example
输出内容
Symbol table '.dynsym' contains 35 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _Znam@GLIBCXX_3.4 (2)
2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNKSt7__cxx1112basic_str@GLIBCXX_3.4.21 (3)
3: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_CTX_new@OPENSSL_1_1_0 (4)
4: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_keygen_init@OPENSSL_1_1_0 (4)
5: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt7__cxx1112basic_stri@GLIBCXX_3.4.21 (3)
6: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt7__cxx1112basic_stri@GLIBCXX_3.4.21 (3)
7: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_encrypt_init@OPENSSL_1_1_0 (4)
8: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_decrypt@OPENSSL_1_1_0 (4)
9: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __cxa_atexit@GLIBC_2.2.5 (5)
10: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZStlsIcSt11char_traitsIc@GLIBCXX_3.4.21 (3)
11: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZStlsISt11char_traitsIcE@GLIBCXX_3.4 (2)
12: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSolsEPFRSoS_E@GLIBCXX_3.4 (2)
13: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSaIcED1Ev@GLIBCXX_3.4 (2)
14: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_free@OPENSSL_1_1_0 (4)
15: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt7__cxx1112basic_stri@GLIBCXX_3.4.21 (3)
16: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_CTX_new_id@OPENSSL_1_1_0 (4)
17: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_CTX_ctrl@OPENSSL_1_1_0 (4)
18: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZdaPv@GLIBCXX_3.4 (2)
19: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt7__cxx1112basic_stri@GLIBCXX_3.4.21 (3)
20: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_decrypt_init@OPENSSL_1_1_0 (4)
21: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNKSt7__cxx1112basic_str@GLIBCXX_3.4.21 (3)
22: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt8ios_base4InitC1Ev@GLIBCXX_3.4 (2)
23: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _Unwind_Resume@GCC_3.0 (7)
24: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSaIcEC1Ev@GLIBCXX_3.4 (2)
25: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_CTX_free@OPENSSL_1_1_0 (4)
26: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_keygen@OPENSSL_1_1_0 (4)
27: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (5)
28: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_encrypt@OPENSSL_1_1_0 (4)
29: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
30: 0000000000404240 272 OBJECT GLOBAL DEFAULT 25 _ZSt4cerr@GLIBCXX_3.4 (2)
31: 0000000000401060 0 FUNC GLOBAL DEFAULT UND _ZSt4endlIcSt11char_trait@GLIBCXX_3.4 (2)
32: 0000000000404120 272 OBJECT GLOBAL DEFAULT 25 _ZSt4cout@GLIBCXX_3.4 (2)
33: 00000000004011a0 0 FUNC GLOBAL DEFAULT UND __gxx_personality_v0@CXXABI_1.3 (6)
34: 0000000000401200 0 FUNC GLOBAL DEFAULT UND _ZNSt8ios_base4InitD1Ev@GLIBCXX_3.4 (2)
Symbol table '.symtab' contains 100 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000004002a8 0 SECTION LOCAL DEFAULT 1
2: 00000000004002c4 0 SECTION LOCAL DEFAULT 2
3: 00000000004002e4 0 SECTION LOCAL DEFAULT 3
4: 0000000000400308 0 SECTION LOCAL DEFAULT 4
5: 0000000000400340 0 SECTION LOCAL DEFAULT 5
6: 0000000000400688 0 SECTION LOCAL DEFAULT 6
7: 0000000000400b0a 0 SECTION LOCAL DEFAULT 7
8: 0000000000400b50 0 SECTION LOCAL DEFAULT 8
9: 0000000000400bf0 0 SECTION LOCAL DEFAULT 9
10: 0000000000400c50 0 SECTION LOCAL DEFAULT 10
11: 0000000000401000 0 SECTION LOCAL DEFAULT 11
12: 0000000000401020 0 SECTION LOCAL DEFAULT 12
13: 0000000000401210 0 SECTION LOCAL DEFAULT 13
14: 0000000000401d04 0 SECTION LOCAL DEFAULT 14
15: 0000000000402000 0 SECTION LOCAL DEFAULT 15
16: 00000000004021c8 0 SECTION LOCAL DEFAULT 16
17: 0000000000402230 0 SECTION LOCAL DEFAULT 17
18: 0000000000402408 0 SECTION LOCAL DEFAULT 18
19: 0000000000403db8 0 SECTION LOCAL DEFAULT 19
20: 0000000000403dc8 0 SECTION LOCAL DEFAULT 20
21: 0000000000403dd0 0 SECTION LOCAL DEFAULT 21
22: 0000000000403ff0 0 SECTION LOCAL DEFAULT 22
23: 0000000000404000 0 SECTION LOCAL DEFAULT 23
24: 0000000000404108 0 SECTION LOCAL DEFAULT 24
25: 0000000000404120 0 SECTION LOCAL DEFAULT 25
26: 0000000000000000 0 SECTION LOCAL DEFAULT 26
27: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
28: 0000000000401250 0 FUNC LOCAL DEFAULT 13 deregister_tm_clones
29: 0000000000401280 0 FUNC LOCAL DEFAULT 13 register_tm_clones
30: 00000000004012c0 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux
31: 0000000000404350 1 OBJECT LOCAL DEFAULT 25 completed.7325
32: 0000000000403dc8 0 OBJECT LOCAL DEFAULT 20 __do_global_dtors_aux_fin
33: 00000000004012f0 0 FUNC LOCAL DEFAULT 13 frame_dummy
34: 0000000000403db8 0 OBJECT LOCAL DEFAULT 19 __frame_dummy_init_array_
35: 0000000000000000 0 FILE LOCAL DEFAULT ABS jiami.cc
36: 0000000000402008 1 OBJECT LOCAL DEFAULT 15 _ZStL19piecewise_construc
37: 0000000000404351 1 OBJECT LOCAL DEFAULT 25 _ZStL8__ioinit
38: 0000000000401c4d 62 FUNC LOCAL DEFAULT 13 _Z41__static_initializati
39: 0000000000401c8b 21 FUNC LOCAL DEFAULT 13 _GLOBAL__sub_I__Z21genera
40: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
41: 0000000000402404 0 OBJECT LOCAL DEFAULT 17 __FRAME_END__
42: 0000000000000000 0 FILE LOCAL DEFAULT ABS
43: 00000000004021c8 0 NOTYPE LOCAL DEFAULT 16 __GNU_EH_FRAME_HDR
44: 0000000000403dd0 0 OBJECT LOCAL DEFAULT 21 _DYNAMIC
45: 0000000000403dc8 0 NOTYPE LOCAL DEFAULT 19 __init_array_end
46: 0000000000403db8 0 NOTYPE LOCAL DEFAULT 19 __init_array_start
47: 0000000000404000 0 OBJECT LOCAL DEFAULT 23 _GLOBAL_OFFSET_TABLE_
48: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _Znam@@GLIBCXX_3.4
49: 0000000000404118 0 NOTYPE GLOBAL DEFAULT 24 _edata
50: 0000000000404108 0 NOTYPE WEAK DEFAULT 24 data_start
51: 0000000000402000 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used
52: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNKSt7__cxx1112basic_str
53: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_CTX_new@@OPENSSL
54: 00000000004013e5 842 FUNC GLOBAL DEFAULT 13 _Z11sm2_encryptRKNSt7__cx
55: 0000000000401a79 468 FUNC GLOBAL DEFAULT 13 main
56: 0000000000401060 0 FUNC GLOBAL DEFAULT UND _ZSt4endlIcSt11char_trait
57: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_keygen_init@@OPE
58: 0000000000404110 0 OBJECT GLOBAL HIDDEN 24 __dso_handle
59: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt7__cxx1112basic_stri
60: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt7__cxx1112basic_stri
61: 00000000004012f2 243 FUNC GLOBAL DEFAULT 13 _Z21generate_sm2_key_pair
62: 000000000040172f 842 FUNC GLOBAL DEFAULT 13 _Z11sm2_decryptRKNSt7__cx
63: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_encrypt_init@@OP
64: 0000000000401d04 0 FUNC GLOBAL HIDDEN 14 _fini
65: 0000000000401240 1 FUNC GLOBAL HIDDEN 13 _dl_relocate_static_pie
66: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_decrypt@@OPENSSL
67: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __cxa_atexit@@GLIBC_2.2.5
68: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZStlsIcSt11char_traitsIc
69: 0000000000401210 43 FUNC GLOBAL DEFAULT 13 _start
70: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZStlsISt11char_traitsIcE
71: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSolsEPFRSoS_E@@GLIBCXX
72: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSaIcED1Ev@@GLIBCXX_3.4
73: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_free@@OPENSSL_1_
74: 0000000000401000 0 FUNC GLOBAL HIDDEN 11 _init
75: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt7__cxx1112basic_stri
76: 0000000000404118 0 OBJECT GLOBAL HIDDEN 24 __TMC_END__
77: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_CTX_new_id@@OPEN
78: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_CTX_ctrl@@OPENSS
79: 0000000000404120 272 OBJECT GLOBAL DEFAULT 25 _ZSt4cout@@GLIBCXX_3.4
80: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZdaPv@@GLIBCXX_3.4
81: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt7__cxx1112basic_stri
82: 0000000000404108 0 NOTYPE GLOBAL DEFAULT 24 __data_start
83: 0000000000404358 0 NOTYPE GLOBAL DEFAULT 25 _end
84: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_decrypt_init@@OP
85: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNKSt7__cxx1112basic_str
86: 0000000000404118 0 NOTYPE GLOBAL DEFAULT 25 __bss_start
87: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt8ios_base4InitC1Ev@@
88: 0000000000401ca0 93 FUNC GLOBAL DEFAULT 13 __libc_csu_init
89: 00000000004011a0 0 FUNC GLOBAL DEFAULT UND __gxx_personality_v0@@CXX
90: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _Unwind_Resume@@GCC_3.0
91: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSaIcEC1Ev@@GLIBCXX_3.4
92: 0000000000401d00 1 FUNC GLOBAL DEFAULT 13 __libc_csu_fini
93: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_CTX_free@@OPENSS
94: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_keygen@@OPENSSL_
95: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_
96: 0000000000000000 0 FUNC GLOBAL DEFAULT UND EVP_PKEY_encrypt@@OPENSSL
97: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
98: 0000000000404240 272 OBJECT GLOBAL DEFAULT 25 _ZSt4cerr@@GLIBCXX_3.4
99: 0000000000401200 0 FUNC GLOBAL DEFAULT UND _ZNSt8ios_base4InitD1Ev@@
这将显示指定 ELF 文件的符号表信息,包括每个符号的名称、地址、大小等。
readelf -d sm2_example
这将显示指定 ELF 文件的动态段信息,包括动态链接库的信息、重定位表的地址等。
nm
命令是一个用于显示二进制目标文件(例如可执行文件或共享库文件)的符号表的命令行工具。nm
的名称代表 “name mangling”,它列出了目标文件中的符号,包括函数名称、变量名称和其他标识符,以及它们的地址和类型信息。nm
常用于调试、分析和监视二进制文件。
nm [OPTIONS] FILE
OPTIONS(可选):nm
命令支持的一些选项,用于控制输出格式和显示特定类型的符号等。常用选项包括:
-A
或 --with-syms
:显示所有符号,包括未公开的符号。-C
或 --demangle
:对 C++ 的函数名进行解码(去除名称修饰)。-g
或 --extern-only
:只显示外部符号。-t
或 --radix
:指定输出地址的基数(10、16 等)。-u
或 --undefined-only
:只显示未定义的符号。-D
或 --dynamic
:显示动态符号(仅在共享库文件中有效)。-l
或 --line-numbers
:显示行号(如果可用)。-S
或 --print-size
:显示符号的大小。-h
或 --help
:显示帮助信息。FILE(必需):指定要显示符号表的二进制文件的路径。
nm -A /path/to/your/executable
sm2_example:0000000000404118 B __bss_start
sm2_example:0000000000404350 b completed.7325
sm2_example: U __cxa_atexit@@GLIBC_2.2.5
sm2_example:0000000000404108 D __data_start
sm2_example:0000000000404108 W data_start
sm2_example:0000000000401250 t deregister_tm_clones
sm2_example:0000000000401240 T _dl_relocate_static_pie
sm2_example:00000000004012c0 t __do_global_dtors_aux
sm2_example:0000000000403dc8 t __do_global_dtors_aux_fini_array_entry
sm2_example:0000000000404110 D __dso_handle
sm2_example:0000000000403dd0 d _DYNAMIC
sm2_example:0000000000404118 D _edata
sm2_example:0000000000404358 B _end
sm2_example: U EVP_PKEY_CTX_ctrl@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_CTX_free@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_CTX_new_id@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_CTX_new@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_decrypt_init@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_decrypt@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_encrypt_init@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_encrypt@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_free@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_keygen_init@@OPENSSL_1_1_0
sm2_example: U EVP_PKEY_keygen@@OPENSSL_1_1_0
sm2_example:0000000000401d04 T _fini
sm2_example:00000000004012f0 t frame_dummy
sm2_example:0000000000403db8 t __frame_dummy_init_array_entry
sm2_example:0000000000402404 r __FRAME_END__
sm2_example:0000000000404000 d _GLOBAL_OFFSET_TABLE_
sm2_example:0000000000401c8b t _GLOBAL__sub_I__Z21generate_sm2_key_pairv
sm2_example: w __gmon_start__
sm2_example:00000000004021c8 r __GNU_EH_FRAME_HDR
sm2_example: U __gxx_personality_v0@@CXXABI_1.3
sm2_example:0000000000401000 T _init
sm2_example:0000000000403dc8 t __init_array_end
sm2_example:0000000000403db8 t __init_array_start
sm2_example:0000000000402000 R _IO_stdin_used
sm2_example:0000000000401d00 T __libc_csu_fini
sm2_example:0000000000401ca0 T __libc_csu_init
sm2_example: U __libc_start_main@@GLIBC_2.2.5
sm2_example:0000000000401a79 T main
sm2_example:0000000000401280 t register_tm_clones
sm2_example:0000000000401210 T _start
sm2_example:0000000000404118 D __TMC_END__
sm2_example: U _Unwind_Resume@@GCC_3.0
sm2_example:000000000040172f T _Z11sm2_decryptRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEP11evp_pkey_st
sm2_example:00000000004013e5 T _Z11sm2_encryptRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEP11evp_pkey_st
sm2_example:00000000004012f2 T _Z21generate_sm2_key_pairv
sm2_example:0000000000401c4d t _Z41__static_initialization_and_destruction_0ii
sm2_example: U _ZdaPv@@GLIBCXX_3.4
sm2_example: U _Znam@@GLIBCXX_3.4
sm2_example: U _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv@@GLIBCXX_3.4.21
sm2_example: U _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6lengthEv@@GLIBCXX_3.4.21
sm2_example: U _ZNSaIcEC1Ev@@GLIBCXX_3.4
sm2_example: U _ZNSaIcED1Ev@@GLIBCXX_3.4
sm2_example: U _ZNSolsEPFRSoS_E@@GLIBCXX_3.4
sm2_example: U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EOS4_@@GLIBCXX_3.4.21
sm2_example: U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcmRKS3_@@GLIBCXX_3.4.21
sm2_example: U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@@GLIBCXX_3.4.21
sm2_example: U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@@GLIBCXX_3.4.21
sm2_example: U _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
sm2_example: U _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4
sm2_example:0000000000404240 B _ZSt4cerr@@GLIBCXX_3.4
sm2_example:0000000000404120 B _ZSt4cout@@GLIBCXX_3.4
sm2_example: U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4
sm2_example:0000000000402008 r _ZStL19piecewise_construct
sm2_example:0000000000404351 b _ZStL8__ioinit
sm2_example: U _ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKNSt7__cxx1112basic_stringIS4_S5_T1_EE@@GLIBCXX_3.4.21
sm2_example: U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4
这将显示指定的可执行文件中的所有符号,包括未公开的符号。
nm -u /path/to/your/executable
U __cxa_atexit@@GLIBC_2.2.5
U EVP_PKEY_CTX_ctrl@@OPENSSL_1_1_0
U EVP_PKEY_CTX_free@@OPENSSL_1_1_0
U EVP_PKEY_CTX_new_id@@OPENSSL_1_1_0
U EVP_PKEY_CTX_new@@OPENSSL_1_1_0
U EVP_PKEY_decrypt_init@@OPENSSL_1_1_0
U EVP_PKEY_decrypt@@OPENSSL_1_1_0
U EVP_PKEY_encrypt_init@@OPENSSL_1_1_0
U EVP_PKEY_encrypt@@OPENSSL_1_1_0
U EVP_PKEY_free@@OPENSSL_1_1_0
U EVP_PKEY_keygen_init@@OPENSSL_1_1_0
U EVP_PKEY_keygen@@OPENSSL_1_1_0
w __gmon_start__
U __gxx_personality_v0@@CXXABI_1.3
U __libc_start_main@@GLIBC_2.2.5
U _Unwind_Resume@@GCC_3.0
U _ZdaPv@@GLIBCXX_3.4
U _Znam@@GLIBCXX_3.4
U _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5c_strEv@@GLIBCXX_3.4.21
U _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6lengthEv@@GLIBCXX_3.4.21
U _ZNSaIcEC1Ev@@GLIBCXX_3.4
U _ZNSaIcED1Ev@@GLIBCXX_3.4
U _ZNSolsEPFRSoS_E@@GLIBCXX_3.4
U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EOS4_@@GLIBCXX_3.4.21
U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcmRKS3_@@GLIBCXX_3.4.21
U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@@GLIBCXX_3.4.21
U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@@GLIBCXX_3.4.21
U _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
U _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4
U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4
U _ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKNSt7__cxx1112basic_stringIS4_S5_T1_EE@@GLIBCXX_3.4.21
U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4
这将显示指定的可执行文件中的未定义(未解析)的符号。
nm -C sm2_example
这将解码指定的可执行文件中的 C++ 函数名,去除名称修饰。
0000000000404118 B __bss_start
0000000000404350 b completed.7325
U __cxa_atexit@@GLIBC_2.2.5
0000000000404108 D __data_start
0000000000404108 W data_start
0000000000401250 t deregister_tm_clones
0000000000401240 T _dl_relocate_static_pie
00000000004012c0 t __do_global_dtors_aux
0000000000403dc8 t __do_global_dtors_aux_fini_array_entry
0000000000404110 D __dso_handle
0000000000403dd0 d _DYNAMIC
0000000000404118 D _edata
0000000000404358 B _end
U EVP_PKEY_CTX_ctrl@@OPENSSL_1_1_0
U EVP_PKEY_CTX_free@@OPENSSL_1_1_0
U EVP_PKEY_CTX_new_id@@OPENSSL_1_1_0
U EVP_PKEY_CTX_new@@OPENSSL_1_1_0
U EVP_PKEY_decrypt_init@@OPENSSL_1_1_0
U EVP_PKEY_decrypt@@OPENSSL_1_1_0
U EVP_PKEY_encrypt_init@@OPENSSL_1_1_0
U EVP_PKEY_encrypt@@OPENSSL_1_1_0
U EVP_PKEY_free@@OPENSSL_1_1_0
U EVP_PKEY_keygen_init@@OPENSSL_1_1_0
U EVP_PKEY_keygen@@OPENSSL_1_1_0
0000000000401d04 T _fini
00000000004012f0 t frame_dummy
0000000000403db8 t __frame_dummy_init_array_entry
0000000000402404 r __FRAME_END__
0000000000404000 d _GLOBAL_OFFSET_TABLE_
0000000000401c8b t _GLOBAL__sub_I__Z21generate_sm2_key_pairv
w __gmon_start__
00000000004021c8 r __GNU_EH_FRAME_HDR
U __gxx_personality_v0@@CXXABI_1.3
0000000000401000 T _init
0000000000403dc8 t __init_array_end
0000000000403db8 t __init_array_start
0000000000402000 R _IO_stdin_used
0000000000401d00 T __libc_csu_fini
0000000000401ca0 T __libc_csu_init
U __libc_start_main@@GLIBC_2.2.5
0000000000401a79 T main
0000000000401280 t register_tm_clones
0000000000401210 T _start
0000000000404118 D __TMC_END__
U _Unwind_Resume@@GCC_3.0
000000000040172f T sm2_decrypt(std::__cxx11::basic_string, std::allocator > const&, evp_pkey_st*)
00000000004013e5 T sm2_encrypt(std::__cxx11::basic_string, std::allocator > const&, evp_pkey_st*)
00000000004012f2 T generate_sm2_key_pair()
0000000000401c4d t __static_initialization_and_destruction_0(int, int)
U operator delete[](void*)@@GLIBCXX_3.4
U operator new[](unsigned long)@@GLIBCXX_3.4
U std::__cxx11::basic_string, std::allocator >::c_str() const@@GLIBCXX_3.4.21
U std::__cxx11::basic_string, std::allocator >::length() const@@GLIBCXX_3.4.21
U std::allocator::allocator()@@GLIBCXX_3.4
U std::allocator::~allocator()@@GLIBCXX_3.4
U std::ostream::operator<<(std::ostream& (*)(std::ostream&))@@GLIBCXX_3.4
U std::__cxx11::basic_string, std::allocator >::basic_string(std::__cxx11::basic_string, std::allocator >&&)@@GLIBCXX_3.4.21
U std::__cxx11::basic_string, std::allocator >::basic_string(char const*, unsigned long, std::allocator const&)@@GLIBCXX_3.4.21
U std::__cxx11::basic_string, std::allocator >::basic_string(char const*, std::allocator const&)@@GLIBCXX_3.4.21
U std::__cxx11::basic_string, std::allocator >::~basic_string()@@GLIBCXX_3.4.21
U std::ios_base::Init::Init()@@GLIBCXX_3.4
U std::ios_base::Init::~Init()@@GLIBCXX_3.4
0000000000404240 B std::cerr@@GLIBCXX_3.4
0000000000404120 B std::cout@@GLIBCXX_3.4
U std::basic_ostream >& std::endl >(std::basic_ostream >&)@@GLIBCXX_3.4
0000000000402008 r std::piecewise_construct
0000000000404351 b std::__ioinit
U std::basic_ostream >& std::operator<< , std::allocator >(std::basic_ostream >&, std::__cxx11::basic_string, std::allocator > const&)@@GLIBCXX_3.4.21
U std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)@@GLIBCXX_3.4
nm -D /path/to/your/shared/library.so
这将显示指定的共享库文件中的动态符号,通常用于共享库文件。