14 Star 0 Fork 12

ocs-commit/openssl

forked from OpenCloudOS Stream/openssl 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0001-turbo-optimized-sha256-call-chain-for-improved-effic.patch 4.21 KB
一键复制 编辑 原始数据 按行查看 历史
From 6f4a91a665e123fa037a2d61b314e16033b5bdbf Mon Sep 17 00:00:00 2001
From: zoedong <zoedong@tencent.com>
Date: Tue, 13 Aug 2024 15:47:23 +0800
Subject: [PATCH] turbo: optimized sha256 call chain for improved efficiency
Optimized the SHA256 execution by skipping the EVP framework, leading to
performance improvements.
Performance testing on Intel(R) Xeon(R) Gold 6133 CPU @ 2.50GHz showed
the following improvements.
Test command used: openssl speed -multi 80 -seconds 30 sha256
| Block Size(Bytes) | Before(kB/s) | After(kB/s) | Improvement(%) |
|-------------------|---------------|---------------|----------------|
| 16 | 1,626,936.53 | 2,604,869.21 | 60.12% |
| 64 | 4,281,664.80 | 5,779,046.85 | 34.97% |
| 256 | 9,609,261.08 | 10,987,963.69 | 14.35% |
| 1024 | 13,396,612.57 | 13,995,488.49 | 4.47% |
| 8192 | 15,183,924.43 | 15,275,545.94 | 0.60% |
| 16384 | 15,342,331.43 | 15,391,760.93 | 0.32% |
Signed-off-by: zoedong <zoedong@tencent.com>
---
crypto/evp/build.info | 2 +-
crypto/evp/digest.c | 5 +++++
crypto/evp/digest_fast_path.c | 33 ++++++++++++++++++++++++++++++
include/openssl/digest_fast_path.h | 8 ++++++++
4 files changed, 47 insertions(+), 1 deletion(-)
create mode 100644 crypto/evp/digest_fast_path.c
create mode 100644 include/openssl/digest_fast_path.h
diff --git a/crypto/evp/build.info b/crypto/evp/build.info
index 95fea31..e14f3d0 100644
--- a/crypto/evp/build.info
+++ b/crypto/evp/build.info
@@ -2,7 +2,7 @@ LIBS=../../libcrypto
$COMMON=digest.c evp_enc.c evp_lib.c evp_fetch.c evp_utils.c \
mac_lib.c mac_meth.c keymgmt_meth.c keymgmt_lib.c kdf_lib.c kdf_meth.c \
m_sigver.c pmeth_lib.c signature.c p_lib.c pmeth_gn.c exchange.c \
- evp_rand.c asymcipher.c kem.c dh_support.c ec_support.c pmeth_check.c
+ evp_rand.c asymcipher.c kem.c dh_support.c ec_support.c pmeth_check.c digest_fast_path.c
SOURCE[../../libcrypto]=$COMMON\
encode.c evp_key.c evp_cnf.c \
diff --git a/crypto/evp/digest.c b/crypto/evp/digest.c
index eefed52..cf24f05 100644
--- a/crypto/evp/digest.c
+++ b/crypto/evp/digest.c
@@ -24,6 +24,7 @@
#include "internal/core.h"
#include "crypto/evp.h"
#include "evp_local.h"
+#include "openssl/digest_fast_path.h"
static void cleanup_old_md_data(EVP_MD_CTX *ctx, int force)
{
@@ -647,6 +648,10 @@ int EVP_Digest(const void *data, size_t count,
unsigned char *md, unsigned int *size, const EVP_MD *type,
ENGINE *impl)
{
+ if (digest_fast_path(data, count, md, size, type)) {
+ return 1;
+ }
+
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
int ret;
diff --git a/crypto/evp/digest_fast_path.c b/crypto/evp/digest_fast_path.c
new file mode 100644
index 0000000..7e744c9
--- /dev/null
+++ b/crypto/evp/digest_fast_path.c
@@ -0,0 +1,33 @@
+#include <openssl/evp.h>
+#include <openssl/sha.h>
+
+int sha256_digest_fast_path(const void *data, size_t count,
+ unsigned char *md, unsigned int *size)
+{
+ static unsigned char m[SHA256_DIGEST_LENGTH];
+ SHA256_CTX ctx;
+
+ if (md == NULL)
+ md = m;
+ SHA256_Init(&ctx);
+ SHA256_Update(&ctx, data, count);
+ SHA256_Final(md, &ctx);
+ OPENSSL_cleanse(&ctx, sizeof(ctx));
+
+ // stores the length of the message digest actually generated
+ if (size != NULL) {
+ *size = SHA256_DIGEST_LENGTH;
+ }
+
+ return 1;
+}
+
+int digest_fast_path(const void *data, size_t count,
+ unsigned char *md, unsigned int *size, const EVP_MD *type)
+{
+ if (EVP_MD_get_type(type) == NID_sha256) {
+ return sha256_digest_fast_path(data, count, md, size);
+ }
+
+ return 0;
+}
diff --git a/include/openssl/digest_fast_path.h b/include/openssl/digest_fast_path.h
new file mode 100644
index 0000000..18d25fc
--- /dev/null
+++ b/include/openssl/digest_fast_path.h
@@ -0,0 +1,8 @@
+#ifndef DIGEST_FAST_PATH_H
+#define DIGEST_FAST_PATH_H
+
+#include <openssl/evp.h>
+
+int digest_fast_path(const void *data, size_t count, unsigned char *md, unsigned int *size, const EVP_MD *type);
+
+#endif // DIGEST_FAST_PATH_H
\ No newline at end of file
--
2.41.0
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ocs-commit/openssl.git
git@gitee.com:ocs-commit/openssl.git
ocs-commit
openssl
openssl
master

搜索帮助