Skip to content

Commit e81c3d6

Browse files
authored
[k2] crypto functions optimization (#1710)
1 parent 475badf commit e81c3d6

22 files changed

Lines changed: 7350 additions & 522 deletions

‎builtin-functions/kphp-light/stdlib/crypto-functions.txt‎

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,50 @@
22

33
// ===== SUPPORTED =====
44

5-
define('OPENSSL_ALGO_SHA1' , 0x215fb97d);
6-
define('OPENSSL_ALGO_MD5' , 0x257ddf13);
7-
define('OPENSSL_ALGO_MD4' , 0x317fe3d1);
8-
define('OPENSSL_ALGO_MD2' , 0x5aca6998);
9-
define('OPENSSL_ALGO_DSS1' , 0xf572d6b6);
10-
define('OPENSSL_ALGO_SHA224', 0x8bce55e9);
11-
define('OPENSSL_ALGO_SHA256', 0x6c977f8c);
12-
define('OPENSSL_ALGO_SHA384', 0xf54c2608);
13-
define('OPENSSL_ALGO_SHA512', 0x225df2b6);
14-
define('OPENSSL_ALGO_RMD160', 0x1887e6b4);
5+
define('OPENSSL_ALGO_DSS1' , 0);
6+
define('OPENSSL_ALGO_SHA1' , 1);
7+
define('OPENSSL_ALGO_SHA224', 2);
8+
define('OPENSSL_ALGO_SHA256', 3);
9+
define('OPENSSL_ALGO_SHA384', 4);
10+
define('OPENSSL_ALGO_SHA512', 5);
11+
define('OPENSSL_ALGO_MD5' , 6);
12+
define('OPENSSL_ALGO_RMD160', 7);
13+
define('OPENSSL_ALGO_MD2' , 8);
14+
define('OPENSSL_ALGO_MD4' , 9);
1515

1616
define('OPENSSL_RAW_DATA', 1);
1717
define('OPENSSL_ZERO_PADDING', 2);
1818
define('OPENSSL_DONT_ZERO_PAD_KEY', 4);
1919

20-
21-
/** @kphp-extern-func-info interruptible */
2220
function openssl_sign ($data ::: string, &$signature ::: string, $priv_key_id ::: string, $signature_alg ::: int = 1) ::: bool;
2321

24-
/** @kphp-extern-func-info interruptible */
2522
function openssl_verify ($data ::: string, $signature ::: string, $pub_key_id ::: string, $signature_alg ::: int = 1) ::: int;
2623

27-
/** @kphp-extern-func-info interruptible */
2824
function openssl_random_pseudo_bytes ($length ::: int) ::: string | false;
2925

30-
/** @kphp-extern-func-info interruptible */
3126
function openssl_x509_parse ($x509cert ::: string, $shortnames ::: bool = true) ::: mixed[] | false;
3227

3328
function openssl_get_cipher_methods($aliases ::: bool = false) ::: string[];
3429

3530
function openssl_cipher_iv_length($method ::: string) ::: int | false;
3631

37-
/** @kphp-extern-func-info interruptible */
3832
function openssl_encrypt($data ::: string, $method ::: string, $key ::: string, $options ::: int = 0, $iv ::: string = '',
3933
string &$tag = TODO, $aad ::: string = '', $tag_length ::: int = 16) ::: string | false;
40-
41-
/** @kphp-extern-func-info interruptible */
4234
function openssl_decrypt($data ::: string, $method ::: string, $key ::: string, $options ::: int = 0, $iv ::: string = '',
4335
$tag ::: string = '', $aad ::: string = '') ::: string | false;
4436

45-
/** @kphp-extern-func-info interruptible */
4637
function openssl_pkey_get_private ($key ::: string, $passphrase ::: string = '') ::: string | false;
47-
/** @kphp-extern-func-info interruptible */
4838
function openssl_pkey_get_public ($key ::: string) ::: string | false;
4939

50-
/** @kphp-extern-func-info interruptible */
5140
function openssl_public_encrypt ($data ::: string, &$result ::: mixed, $key ::: string) ::: bool;
52-
/** @kphp-extern-func-info interruptible */
5341
function openssl_private_decrypt ($data ::: string, &$result ::: mixed, $key ::: string) ::: bool;
5442

5543
function hash_algos () ::: string[];
5644
function hash_hmac_algos () ::: string[];
5745

58-
/** @kphp-extern-func-info interruptible */
5946
function hash ($algo ::: string, $data ::: string, $raw_output ::: bool = false) ::: string;
60-
/** @kphp-extern-func-info interruptible */
6147
function hash_hmac ($algo ::: string, $data ::: string, $key ::: string, $raw_output ::: bool = false) ::: string;
6248

63-
/** @kphp-extern-func-info interruptible */
6449
function sha1 ($s ::: string, $raw_output ::: bool = false) ::: string;
6550
function md5 ($s ::: string, $raw_output ::: bool = false): string;
6651

@@ -93,9 +78,7 @@ define('PKCS7_NOATTR', 0x100);
9378
/** @kphp-extern-func-info stub generation-required */
9479
function openssl_x509_verify ($x509cert ::: string, $public_key ::: string) ::: int;
9580

96-
9781
/** @kphp-extern-func-info stub generation-required */
9882
function openssl_pkcs7_sign($infile ::: string, $outfile ::: string,
9983
$sign_cert ::: string, $priv_key ::: string,
10084
string[] $headers, $flags ::: int = PKCS7_DETACHED, $extra_certs ::: string = "") ::: bool;
101-

‎common/algorithms/string-algorithms-test.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,14 @@ TEST(string_algorithms_trim, empty_string) {
6868
TestTrimHelper<vk::string_view>::empty_string();
6969
TestTrimHelper<std::string>::empty_string();
7070
}
71+
72+
TEST(string_algorithms, tolower_ascii) {
73+
EXPECT_EQ(vk::tolower_ascii('A'), 'a');
74+
EXPECT_EQ(vk::tolower_ascii('Z'), 'z');
75+
EXPECT_EQ(vk::tolower_ascii('m'), 'm');
76+
EXPECT_EQ(vk::tolower_ascii('0'), '0');
77+
EXPECT_EQ(vk::tolower_ascii('-'), '-');
78+
EXPECT_EQ(vk::tolower_ascii('@'), '@');
79+
EXPECT_EQ(vk::tolower_ascii('['), '[');
80+
EXPECT_EQ(vk::tolower_ascii(static_cast<char>(0xC4)), static_cast<char>(0xC4));
81+
}

‎common/algorithms/string-algorithms.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ inline bool is_ascii_whitespace(char c) noexcept {
7575
return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
7676
}
7777

78+
inline constexpr char tolower_ascii(char c) noexcept {
79+
if (c >= 'A' && c <= 'Z') {
80+
return c + ('a' - 'A');
81+
}
82+
83+
return c;
84+
}
85+
7886
inline vk::string_view rstrip_ascii_whitespace(vk::string_view view) noexcept {
7987
return vk::string_view{view.begin(), std::find_if_not(view.rbegin(), view.rend(), is_ascii_whitespace).base()};
8088
}

‎runtime-common/stdlib/string/string-functions.h‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,41 @@
1919
namespace kphp {
2020
namespace strings {
2121

22+
/**
23+
* Writes the lowercase hex representation of `src` to `dst`.
24+
*
25+
* @param `src` A pointer to the binary data, `src_len` bytes long.
26+
* @param `src_len` The size of the input in bytes.
27+
* @param `dst` A pointer to the output buffer. The result is not null-terminated.
28+
* @param `dst_len` The size of the output buffer, must be at least `2 * src_len`. Violating this fails `php_assert` at run time.
29+
*
30+
* `src` and `dst` must either not overlap at all or be the same pointer.
31+
*/
32+
inline void bin2hex(const char* src, size_t src_len, char* dst, size_t dst_len) noexcept {
33+
php_assert(dst_len >= 2 * src_len);
34+
const auto& string_lib_constants{StringLibConstants::get()};
35+
for (size_t i = src_len; i-- > 0;) {
36+
dst[2 * i + 1] = string_lib_constants.lhex_digits[src[i] & 15];
37+
dst[2 * i] = string_lib_constants.lhex_digits[(src[i] >> 4) & 15];
38+
}
39+
}
40+
41+
/**
42+
* Converts in-place the first `src_len` bytes of `buf` to their lowercase hex representation, which takes `2 * src_len` bytes.
43+
*
44+
* @param `buf` A pointer to the buffer that holds the binary data and receives the result. The result is not null-terminated.
45+
* @param `src_len` The size of the binary data in bytes.
46+
* @param `buf_len` The size of the whole buffer, must be at least `2 * src_len`. Violating this fails `php_assert` at run time.
47+
*/
48+
inline void bin2hex(char* buf, size_t src_len, size_t buf_len) noexcept {
49+
bin2hex(buf, src_len, buf, buf_len);
50+
}
51+
2252
inline string bin2hex(std::string_view str) noexcept {
2353
int len = str.size();
2454
string result(2 * len, false);
2555

26-
const auto& string_lib_constants{StringLibConstants::get()};
27-
for (int i = 0; i < len; i++) {
28-
result[2 * i] = string_lib_constants.lhex_digits[(str[i] >> 4) & 15];
29-
result[2 * i + 1] = string_lib_constants.lhex_digits[str[i] & 15];
30-
}
56+
bin2hex(str.data(), str.size(), result.buffer(), result.size());
3157

3258
return result;
3359
}

‎runtime-light/components/kphp/bindings/bindings.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "runtime-light/server/job-worker/job-worker-server-state.h"
2222
#include "runtime-light/server/rpc/rpc-server-state.h"
2323
#include "runtime-light/stdlib/confdata/confdata-state.h"
24+
#include "runtime-light/stdlib/crypto/crypto-state.h"
2425
#include "runtime-light/stdlib/curl/curl-state.h"
2526
#include "runtime-light/stdlib/diagnostics/contextual-tags.h"
2627
#include "runtime-light/stdlib/diagnostics/error-handling-state.h"
@@ -142,6 +143,14 @@ auto CurlImageState::get() noexcept -> const CurlImageState& {
142143
return ImageState::get().curl_image_state;
143144
}
144145

146+
auto kphp::openssl::image_state::get() noexcept -> const kphp::openssl::image_state& {
147+
return ImageState::get().crypto_image_state;
148+
}
149+
150+
auto kphp::openssl::instance_state::get() noexcept -> kphp::openssl::instance_state& {
151+
return InstanceState::get().crypto_instance_state;
152+
}
153+
145154
auto FileSystemImageState::get() noexcept -> const FileSystemImageState& {
146155
return ImageState::get().file_system_image_state;
147156
}

‎runtime-light/components/kphp/state/image-state.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "runtime-light/allocator/allocator-state.h"
2020
#include "runtime-light/core/reference-counter/reference-counter-functions.h"
2121
#include "runtime-light/k2-platform/k2-api.h"
22+
#include "runtime-light/stdlib/crypto/crypto-state.h"
2223
#include "runtime-light/stdlib/curl/curl-state.h"
2324
#include "runtime-light/stdlib/diagnostics/logs.h"
2425
#include "runtime-light/stdlib/file/file-system-state.h"
@@ -51,6 +52,7 @@ struct ImageState final : private vk::not_copyable {
5152
MathImageState math_image_state;
5253
RpcImageState rpc_image_state;
5354
CurlImageState curl_image_state;
55+
kphp::openssl::image_state crypto_image_state;
5456
RegexImageState regex_image_state;
5557

5658
ImageState() noexcept {

‎runtime-light/components/kphp/state/instance-state.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "runtime-light/server/job-worker/job-worker-server-state.h"
2424
#include "runtime-light/server/rpc/rpc-server-state.h"
2525
#include "runtime-light/stdlib/confdata/confdata-state.h"
26+
#include "runtime-light/stdlib/crypto/crypto-state.h"
2627
#include "runtime-light/stdlib/curl/curl-state.h"
2728
#include "runtime-light/stdlib/diagnostics/contextual-tags.h"
2829
#include "runtime-light/stdlib/diagnostics/error-handling-state.h"
@@ -111,6 +112,7 @@ struct InstanceState final : vk::not_copyable {
111112
JobWorkerServerInstanceState job_worker_server_instance_state;
112113
InstanceCacheInstanceState instance_cache_instance_state;
113114
kphp::confdata::instance_state confdata_instance_state;
115+
kphp::openssl::instance_state crypto_instance_state;
114116

115117
TimeInstanceState time_instance_state;
116118
MathInstanceState math_instance_state;

0 commit comments

Comments
 (0)