Skip to content

Commit 64bca9a

Browse files
authored
repo-sync-2025-04-22T17:16:18+0800 (#519)
1 parent 06f4246 commit 64bca9a

Some content is hidden

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

53 files changed

+6330
-373
lines changed

.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ build:gm --define BRPC_WITH_TONGSUO=true
5858

5959
# make sure yacl is built with tongsuo in gm mode
6060
build:gm --define YACL_WITH_TONGSUO=true
61+
62+
build:gmp --define BIGNUM_WITH_GMP=true

STANDARDS.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,43 @@
33
This library provides full implementation of the following NIST-standardized cryptography specifications:
44

55
## **Core Algorithms**
6-
- **AES** (FIPS 197)
6+
- **AES** (FIPS 197)
77

88
- **SM3** (GM/T 0004-2012)
99

1010
- **SM4** (GM/T 0002-2012)
1111

12-
- **SHA2/SHA3 Family** (FIPS 180-4, FIPS 202)
12+
- **SHA2/SHA3 Family** (FIPS 180-4, FIPS 202)
1313

14-
- **HMAC** (FIPS 198-1)
14+
- **HMAC** (FIPS 198-1)
1515

16-
- **KMAC** (SP 800-56B)
16+
- **KMAC** (SP 800-56B)
1717

1818
## **Post-Quantum Cryptography**
19-
- **ML-KEM** (FIPS 203)
19+
- **ML-KEM** (FIPS 203)
2020

21-
- **ML-DSA** (FIPS 204)
21+
- **ML-DSA** (FIPS 204)
2222

2323
## **Digital Signatures**
24-
- **DSA** (FIPS 186)
24+
- **DSA** (FIPS 186)
2525

2626
## Public Key Cryptography
2727
- **RSA** (SP 800-185)
2828

2929
## **Elliptic Curve Cryptography**
3030
- **ECC** (GM/T 0003-2012, FIPS 186-4, RFC 7748, RFC 8032)
31-
- **Hash-to-Curve** (RFC 9380)
31+
- **Hash-to-Curve** (RFC 9380)
3232

3333
## **Secure Randomness**
34-
- **Hash-DRBG** (SP 800-90A)
35-
36-
- **CTR-DRBG** (SP 800-90A)
37-
34+
- **Hash-DRBG** (SP 800-90A)
35+
36+
- **CTR-DRBG** (SP 800-90A)
37+
3838
- **Entropy Sources** (SP 800-90B)
3939

4040
## **Authenticated Encryption**
41-
- **AES-GCM** (SP 800-38D)
42-
43-
- **AEAD** (RFC 5116)
41+
- **AES-GCM** (SP 800-38D)
42+
43+
- **AEAD** (RFC 5116)
4444

4545
---

bazel/config/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ config_setting(
1717
define_values = {"YACL_WITH_TONGSUO": "true"},
1818
visibility = ["//visibility:public"],
1919
)
20+
21+
config_setting(
22+
name = "gmp",
23+
define_values = {"BIGNUM_WITH_GMP": "true"},
24+
visibility = ["//visibility:public"],
25+
)

yacl/crypto/experimental/sse/README.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ This paper proposes a highly scalable searchable symmetric encryption (SSE) sche
1313
## Implemention
1414

1515
1. **T-Set Instantiation**
16-
- `tset.h`
17-
- `tset.cc`
18-
- `tset_test.cc`
16+
- `tset.h`
17+
- `tset.cc`
18+
- `tset_test.cc`
1919
2. **OXT: Oblivious Cross-Tags Protocol**
20-
- `sse.h`
21-
- `sse.cc`
22-
- Test:`sse_test.cc`
20+
- `sse.h`
21+
- `sse.cc`
22+
- Test:`sse_test.cc`
2323

2424
## Test
2525

@@ -30,11 +30,3 @@ This paper proposes a highly scalable searchable symmetric encryption (SSE) sche
3030

3131
Census Income Dataset
3232
https://tianchi.aliyun.com/dataset/111479
33-
34-
35-
36-
37-
38-
39-
40-
###

yacl/crypto/hash/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ yacl_cc_library(
2828
],
2929
)
3030

31+
yacl_cc_library(
32+
name = "ssl_hash_xof",
33+
srcs = ["ssl_hash_xof.cc"],
34+
hdrs = ["ssl_hash_xof.h"],
35+
deps = [
36+
":hash_interface",
37+
"//yacl/base:exception",
38+
"//yacl/crypto:openssl_wrappers",
39+
"//yacl/utils:scope_guard",
40+
],
41+
)
42+
3143
yacl_cc_test(
3244
name = "ssl_hash_all_test",
3345
srcs = ["ssl_hash_all_test.cc"],
@@ -36,6 +48,14 @@ yacl_cc_test(
3648
],
3749
)
3850

51+
yacl_cc_test(
52+
name = "ssl_hash_xof_test",
53+
srcs = ["ssl_hash_xof_test.cc"],
54+
deps = [
55+
":ssl_hash_xof",
56+
],
57+
)
58+
3959
yacl_cc_library(
4060
name = "blake3",
4161
srcs = ["blake3.cc"],
@@ -81,6 +101,7 @@ yacl_cc_library(
81101
hdrs = ["hash_utils.h"],
82102
deps = [
83103
":ssl_hash",
104+
":ssl_hash_xof",
84105
"//yacl/base:int128",
85106
"@blake3",
86107
],

yacl/crypto/hash/hash_interface.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ enum class HashAlgorithm : int {
4242
BLAKE2B = 7, // blake2 is disabled by tongsuo
4343
#endif
4444
BLAKE3 = 8,
45-
46-
SHAKE512 = 9,
45+
SHAKE128 = 9,
46+
SHAKE256 = 10,
4747
};
4848

4949
// HashInterface defines an interface for hash functions.
@@ -107,8 +107,10 @@ inline const char *ToString(HashAlgorithm hash_algo) {
107107
case HashAlgorithm::BLAKE2B:
108108
return "blake2b-512";
109109
#endif
110-
case HashAlgorithm::SHAKE512:
111-
return "shake-512";
110+
case HashAlgorithm::SHAKE128:
111+
return "shake-128";
112+
case HashAlgorithm::SHAKE256:
113+
return "shake-256";
112114
default:
113115
YACL_THROW("Unsupported hash algo: {}", static_cast<int>(hash_algo));
114116
}

yacl/crypto/hash/hash_utils.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
#include "c/blake3.h"
2222

23+
#include "yacl/base/byte_container_view.h"
2324
#include "yacl/base/exception.h"
25+
#include "yacl/crypto/hash/ssl_hash.h"
26+
#include "yacl/crypto/hash/ssl_hash_xof.h"
2427

2528
namespace yacl::crypto {
2629

@@ -40,6 +43,20 @@ std::array<uint8_t, 32> Sm3(ByteContainerView data) {
4043
return out;
4144
}
4245

46+
std::vector<uint8_t> Shake128(ByteContainerView data, size_t output_length) {
47+
YACL_ENFORCE(output_length > 0, "Output length must be positive");
48+
SslHashXof hash(HashAlgorithm::SHAKE128);
49+
hash.Update(data);
50+
return hash.CumulativeHash(output_length);
51+
}
52+
53+
std::vector<uint8_t> Shake256(ByteContainerView data, size_t output_length) {
54+
YACL_ENFORCE(output_length > 0, "Output length must be positive");
55+
SslHashXof hash(HashAlgorithm::SHAKE256);
56+
hash.Update(data);
57+
return hash.CumulativeHash(output_length);
58+
}
59+
4360
#ifndef YACL_WITH_TONGSUO
4461
std::array<uint8_t, 64> Blake2(ByteContainerView data) {
4562
auto buf = SslHash(HashAlgorithm::BLAKE2B).Update(data).CumulativeHash();

yacl/crypto/hash/hash_utils.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "yacl/base/int128.h"
2020
#include "yacl/crypto/hash/ssl_hash.h"
21+
#include "yacl/crypto/hash/ssl_hash_xof.h"
2122

2223
namespace yacl::crypto {
2324

@@ -27,6 +28,12 @@ std::array<uint8_t, 32> Sm3(ByteContainerView data); // 256-bits
2728

2829
std::array<uint8_t, 32> Blake3(ByteContainerView data); // 256-bits
2930

31+
// XOF (Extendable Output Function) hash functions
32+
std::vector<uint8_t> Shake128(ByteContainerView data,
33+
size_t output_length = 16); // default 128-bits
34+
std::vector<uint8_t> Shake256(ByteContainerView data,
35+
size_t output_length = 32); // default 256-bits
36+
3037
#define DECLARE_HASH_OUT_128(func) \
3138
inline uint128_t func##_128(ByteContainerView data) { \
3239
auto hash_bytes = func(data); \
@@ -39,7 +46,6 @@ std::array<uint8_t, 32> Blake3(ByteContainerView data); // 256-bits
3946

4047
DECLARE_HASH_OUT_128(Sha256); // uint128_t Sha256_128(ByteContainerView data);
4148
DECLARE_HASH_OUT_128(Sm3); // uint128_t Sm3_128(ByteContainerView data);
42-
4349
DECLARE_HASH_OUT_128(Blake3); // uint128_t Blake3_128(ByteContainerView data);
4450

4551
#ifndef YACL_WITH_TONGSUO

yacl/crypto/hash/ssl_hash_xof.cc

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2025 Ant Group Co., Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "yacl/crypto/hash/ssl_hash_xof.h"
16+
17+
#include <openssl/evp.h>
18+
19+
#include "yacl/base/exception.h"
20+
21+
namespace yacl::crypto {
22+
23+
SslHashXof::SslHashXof(HashAlgorithm hash_algo)
24+
: hash_algo_(hash_algo),
25+
md_(openssl::FetchEvpMd(ToString(hash_algo))),
26+
context_(EVP_MD_CTX_new()) {
27+
YACL_ENFORCE(context_ != nullptr, "Failed to create EVP_MD_CTX");
28+
YACL_ENFORCE(EVP_DigestInit_ex(context_.get(), md_.get(), nullptr) == 1,
29+
"Failed to initialize XOF hash");
30+
switch (hash_algo) {
31+
case HashAlgorithm::SHAKE128:
32+
digest_size_ = 32;
33+
break;
34+
case HashAlgorithm::SHAKE256:
35+
digest_size_ = 64;
36+
break;
37+
default:
38+
YACL_THROW("Unsupported XOF algorithm: {}", static_cast<int>(hash_algo));
39+
}
40+
}
41+
42+
HashInterface& SslHashXof::Reset() {
43+
OSSL_RET_1(EVP_DigestInit_ex(context_.get(), md_.get(), nullptr));
44+
return *this;
45+
}
46+
47+
HashInterface& SslHashXof::Update(ByteContainerView data) {
48+
OSSL_RET_1(EVP_DigestUpdate(context_.get(), data.data(), data.size()));
49+
return *this;
50+
}
51+
52+
std::vector<uint8_t> SslHashXof::CumulativeHash() const {
53+
return CumulativeHash(digest_size_);
54+
}
55+
56+
std::vector<uint8_t> SslHashXof::CumulativeHash(size_t output_length) const {
57+
std::vector<uint8_t> output(output_length);
58+
auto ctx_snapshot = openssl::UniqueMdCtx(EVP_MD_CTX_new());
59+
YACL_ENFORCE(ctx_snapshot != nullptr);
60+
61+
EVP_MD_CTX_init(ctx_snapshot.get());
62+
OSSL_RET_1(EVP_MD_CTX_copy_ex(ctx_snapshot.get(), context_.get()));
63+
OSSL_RET_1(
64+
EVP_DigestFinalXOF(ctx_snapshot.get(), output.data(), output_length));
65+
66+
return output;
67+
}
68+
69+
} // namespace yacl::crypto

yacl/crypto/hash/ssl_hash_xof.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2025 Ant Group Co., Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "yacl/base/byte_container_view.h"
18+
#include "yacl/crypto/hash/hash_interface.h"
19+
#include "yacl/crypto/openssl_wrappers.h"
20+
21+
namespace yacl::crypto {
22+
23+
class SslHashXof : public HashInterface {
24+
public:
25+
explicit SslHashXof(HashAlgorithm hash_algo);
26+
// From HashInterface.
27+
HashAlgorithm GetHashAlgorithm() const override { return hash_algo_; }
28+
size_t DigestSize() const override { return digest_size_; }
29+
HashInterface& Reset() override;
30+
HashInterface& Update(ByteContainerView data) override;
31+
std::vector<uint8_t> CumulativeHash() const override;
32+
33+
// For XOF hash functions, this method allows requesting a specific output
34+
// length.
35+
std::vector<uint8_t> CumulativeHash(size_t output_length) const;
36+
37+
private:
38+
HashAlgorithm hash_algo_;
39+
openssl::UniqueMd md_;
40+
openssl::UniqueMdCtx context_;
41+
size_t digest_size_ = 32;
42+
};
43+
44+
// Shake128Hash implements HashInterface for the Shake128 hash function,
45+
// which is an extendable-output function (XOF) defined in FIPS 202.
46+
// SHAKE128 allows for variable-length output.
47+
class Shake128Hash final : public SslHashXof {
48+
public:
49+
Shake128Hash() : SslHashXof(HashAlgorithm::SHAKE128) {}
50+
};
51+
52+
// Shake256Hash implements HashInterface for the Shake256 hash function,
53+
// which is an extendable-output function (XOF) defined in FIPS 202.
54+
// SHAKE256 allows for variable-length output.
55+
class Shake256Hash final : public SslHashXof {
56+
public:
57+
Shake256Hash() : SslHashXof(HashAlgorithm::SHAKE256) {}
58+
};
59+
60+
} // namespace yacl::crypto

0 commit comments

Comments
 (0)