Skip to content

Commit d4f4492

Browse files
committed
Update GmSSL v3.2.0 compatibility
1 parent 1a4d65b commit d4f4492

2 files changed

Lines changed: 112 additions & 11 deletions

File tree

.github/workflows/ci.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
branches: [main, master]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
name: Python ${{ matrix.python-version }} on ${{ matrix.os }}
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
os: [ubuntu-22.04, macos-14]
18+
python-version: ["3.10", "3.11", "3.12"]
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
24+
- name: Setup Python
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: ${{ matrix.python-version }}
28+
29+
- name: Install build dependencies
30+
run: |
31+
if [ "$RUNNER_OS" = "Linux" ]; then
32+
sudo apt-get update
33+
sudo apt-get install -y build-essential cmake
34+
else
35+
brew install cmake
36+
fi
37+
38+
- name: Build and install GmSSL v3.2.0
39+
run: |
40+
git clone --depth 1 --branch v3.2.0 https://github.com/guanzhi/GmSSL.git "$RUNNER_TEMP/GmSSL"
41+
cmake -S "$RUNNER_TEMP/GmSSL" -B "$RUNNER_TEMP/GmSSL/build" \
42+
-DCMAKE_BUILD_TYPE=Release
43+
cmake --build "$RUNNER_TEMP/GmSSL/build" --parallel 4
44+
sudo cmake --install "$RUNNER_TEMP/GmSSL/build"
45+
if [ "$RUNNER_OS" = "Linux" ]; then
46+
sudo ldconfig
47+
fi
48+
49+
- name: Run tests
50+
env:
51+
LD_LIBRARY_PATH: /usr/local/lib
52+
DYLD_LIBRARY_PATH: /usr/local/lib
53+
run: python -m unittest -v

gmssl.py

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ 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+
if gmssl.sm3_pbkdf2(c_char_p(passwd), c_size_t(len(passwd)),
137137
salt, c_size_t(len(salt)), c_size_t(iterator), c_size_t(keylen), key) != 1:
138138
raise NativeError('libgmssl inner error')
139139

@@ -328,7 +328,8 @@ class Sm4Gcm(Structure):
328328
("Y", c_uint8 * 16),
329329
("taglen", c_size_t),
330330
("mac", c_uint8 * 16),
331-
("maclen", c_size_t)
331+
("maclen", c_size_t),
332+
("encedlen", c_uint64)
332333
]
333334

334335
def __init__(self, key, iv, aad, taglen = SM4_GCM_DEFAULT_TAG_SIZE, encrypt = True):
@@ -387,16 +388,17 @@ def finish(self):
387388

388389
class Sm2Point(Structure):
389390
_fields_ = [
390-
("x", c_uint8 * 32),
391-
("y", c_uint8 * 32)
391+
("X", c_uint64 * 4),
392+
("Y", c_uint64 * 4),
393+
("Z", c_uint64 * 4)
392394
]
393395

394396

395397
class Sm2Key(Structure):
396398

397399
_fields_ = [
398400
("public_key", Sm2Point),
399-
("private_key", c_uint8 * 32)
401+
("private_key", c_uint64 * 4)
400402
]
401403

402404
def __init__(self):
@@ -503,17 +505,61 @@ def decrypt(self, ciphertext):
503505
raise NativeError('libgmssl inner error')
504506
return outbuf[:outlen.value]
505507

508+
class X509Key(Structure):
509+
510+
_fields_ = [
511+
("algor", c_int),
512+
("algor_param", c_int),
513+
("sm2_key", Sm2Key),
514+
("_padding", c_uint8 * 20000)
515+
]
516+
506517

507518
DO_ENCRYPT = True
508519
DO_DECRYPT = False
509520
DO_SIGN = True
510521
DO_VERIFY = False
511522

512-
class Sm2Signature(Structure):
523+
class Sm2Z256Point(Structure):
524+
525+
_fields_ = [
526+
("X", c_uint64 * 4),
527+
("Y", c_uint64 * 4),
528+
("Z", c_uint64 * 4)
529+
]
530+
531+
class Sm2SignPreComp(Structure):
532+
533+
_fields_ = [
534+
("k", c_uint64 * 4),
535+
("x1_modn", c_uint64 * 4)
536+
]
537+
538+
class Sm2Sign(Structure):
513539

514540
_fields_ = [
515541
("sm3_ctx", Sm3),
516-
("key", Sm2Key)
542+
("saved_sm3_ctx", Sm3),
543+
("key", Sm2Key),
544+
("fast_sign_private", c_uint64 * 4),
545+
("pre_comp", Sm2SignPreComp * 32),
546+
("num_pre_comp", c_uint),
547+
("public_point_table", Sm2Z256Point * 16)
548+
]
549+
550+
class Sm2Verify(Structure):
551+
552+
_fields_ = [
553+
("sm3_ctx", Sm3),
554+
("saved_sm3_ctx", Sm3),
555+
("key", Sm2Key),
556+
("public_point_table", Sm2Z256Point * 16)
557+
]
558+
559+
class Sm2Signature(Structure):
560+
561+
_fields_ = [
562+
("ctx", Sm2Sign)
517563
]
518564

519565
def __init__(self, sm2_key, signer_id = SM2_DEFAULT_ID, sign = DO_SIGN):
@@ -559,7 +605,7 @@ def verify(self, signature):
559605

560606
class sm9_bn_t(Structure):
561607
_fields_ = [
562-
("d", c_uint64 * 8)
608+
("d", c_uint64 * 4)
563609
]
564610

565611
class sm9_fp2_t(Structure):
@@ -1013,7 +1059,11 @@ def get_subject(self):
10131059

10141060
def get_subject_public_key(self):
10151061
public_key = Sm2Key()
1016-
gmssl.x509_cert_get_subject_public_key(self._cert, c_size_t(len(self._cert)), byref(public_key))
1062+
x509_key = X509Key()
1063+
if gmssl.x509_cert_get_subject_public_key(self._cert, c_size_t(len(self._cert)), byref(x509_key)) != 1:
1064+
raise NativeError('libgmssl inner error')
1065+
public_key.public_key = x509_key.sm2_key.public_key
1066+
public_key.private_key = x509_key.sm2_key.private_key
10171067
public_key._has_private_key = False
10181068
public_key._has_public_key = True
10191069
return public_key
@@ -1037,5 +1087,3 @@ def verify_by_ca_certificate(self, cacert, sm2_id):
10371087
cacert_raw, c_size_t(len(cacert_raw)), c_char_p(sm2_id), c_size_t(len(sm2_id))) != 1:
10381088
return False
10391089
return True
1040-
1041-

0 commit comments

Comments
 (0)