Skip to content

Commit 0181f67

Browse files
committed
fix: align ctypes structures and add CI
1 parent 1a4d65b commit 0181f67

3 files changed

Lines changed: 106 additions & 7 deletions

File tree

.github/workflows/ci.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
branches: [main, master]
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
test:
15+
name: ${{ matrix.name }}
16+
runs-on: ${{ matrix.os }}
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
include:
21+
- name: Linux x64
22+
os: ubuntu-latest
23+
python-version: "3.12"
24+
- name: macOS ARM64
25+
os: macos-latest
26+
python-version: "3.12"
27+
- name: Windows x64
28+
os: windows-latest
29+
python-version: "3.12"
30+
31+
steps:
32+
- name: Checkout GmSSL-Python
33+
uses: actions/checkout@v4
34+
35+
- name: Set up Python
36+
uses: actions/setup-python@v5
37+
with:
38+
python-version: ${{ matrix.python-version }}
39+
40+
- name: Checkout GmSSL
41+
uses: actions/checkout@v4
42+
with:
43+
repository: guanzhi/GmSSL
44+
path: GmSSL
45+
46+
- name: Build and install GmSSL on Linux
47+
if: runner.os == 'Linux'
48+
run: |
49+
cmake -S GmSSL -B GmSSL/build -DCMAKE_BUILD_TYPE=Release
50+
cmake --build GmSSL/build --parallel 2
51+
sudo cmake --install GmSSL/build
52+
sudo ldconfig
53+
54+
- name: Build and install GmSSL on macOS
55+
if: runner.os == 'macOS'
56+
run: |
57+
cmake -S GmSSL -B GmSSL/build -DCMAKE_BUILD_TYPE=Release
58+
cmake --build GmSSL/build --parallel 2
59+
sudo cmake --install GmSSL/build
60+
echo "DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH" >> "$GITHUB_ENV"
61+
62+
- name: Build GmSSL on Windows
63+
if: runner.os == 'Windows'
64+
run: |
65+
cmake -S GmSSL -B GmSSL\build -A x64 -DCMAKE_BUILD_TYPE=Release
66+
cmake --build GmSSL\build --config Release --parallel 2
67+
$dll = Get-ChildItem -Path GmSSL\build -Filter gmssl.dll -Recurse | Select-Object -First 1
68+
if (-not $dll) { throw "gmssl.dll was not built" }
69+
$dll.Directory.FullName | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
70+
71+
- name: Install package
72+
run: python -m pip install .
73+
74+
- name: Verify library loading
75+
run: python -c "import gmssl; print(gmssl.GMSSL_LIBRARY_VERSION); print(gmssl.GMSSL_PYTHON_VERSION)"
76+
77+
- name: Run tests
78+
run: python -m unittest -v

gmssl.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ def sm3_pbkdf2(passwd, salt, iterator, keylen):
133133
passwd = passwd.encode('utf-8')
134134
key = create_string_buffer(keylen)
135135

136-
if gmssl.pbkdf2_hmac_sm3_genkey(c_char_p(passwd), c_size_t(len(passwd)),
136+
try:
137+
pbkdf2_func = gmssl.pbkdf2_hmac_sm3_genkey
138+
except AttributeError:
139+
pbkdf2_func = gmssl.sm3_pbkdf2
140+
141+
if pbkdf2_func(c_char_p(passwd), c_size_t(len(passwd)),
137142
salt, c_size_t(len(salt)), c_size_t(iterator), c_size_t(keylen), key) != 1:
138143
raise NativeError('libgmssl inner error')
139144

@@ -387,8 +392,9 @@ def finish(self):
387392

388393
class Sm2Point(Structure):
389394
_fields_ = [
390-
("x", c_uint8 * 32),
391-
("y", c_uint8 * 32)
395+
("x", c_uint64 * 4),
396+
("y", c_uint64 * 4),
397+
("z", c_uint64 * 4)
392398
]
393399

394400

@@ -509,11 +515,24 @@ def decrypt(self, ciphertext):
509515
DO_SIGN = True
510516
DO_VERIFY = False
511517

518+
class Sm2SignPreComp(Structure):
519+
520+
_fields_ = [
521+
("k", c_uint64 * 4),
522+
("x1_modn", c_uint64 * 4)
523+
]
524+
525+
512526
class Sm2Signature(Structure):
513527

514528
_fields_ = [
515529
("sm3_ctx", Sm3),
516-
("key", Sm2Key)
530+
("saved_sm3_ctx", Sm3),
531+
("key", Sm2Key),
532+
("fast_sign_private", c_uint64 * 4),
533+
("pre_comp", Sm2SignPreComp * 32),
534+
("num_pre_comp", c_uint),
535+
("public_point_table", Sm2Point * 16)
517536
]
518537

519538
def __init__(self, sm2_key, signer_id = SM2_DEFAULT_ID, sign = DO_SIGN):
@@ -1037,5 +1056,3 @@ def verify_by_ca_certificate(self, cacert, sm2_id):
10371056
cacert_raw, c_size_t(len(cacert_raw)), c_char_p(sm2_id), c_size_t(len(sm2_id))) != 1:
10381057
return False
10391058
return True
1040-
1041-

test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ def test_version(self):
1818
self.assertTrue(len(GMSSL_LIBRARY_VERSION) > 0)
1919
self.assertTrue(len(GMSSL_PYTHON_VERSION) > 0)
2020

21+
def test_sm2_struct_sizes(self):
22+
self.assertEqual(sizeof(Sm2Point), 96)
23+
self.assertEqual(sizeof(Sm2Key), 128)
24+
self.assertEqual(sizeof(Sm2Signature), 3976)
25+
2126
def test_rand(self):
2227
keylen = 20
2328
key = rand_bytes(keylen)
@@ -229,4 +234,3 @@ def test_sm2_cert(self):
229234

230235
if __name__ == '__main__':
231236
unittest.main()
232-

0 commit comments

Comments
 (0)