light-vllm-security 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- light_vllm_security-0.0.1/LICENSE +161 -0
- light_vllm_security-0.0.1/MANIFEST.in +4 -0
- light_vllm_security-0.0.1/PKG-INFO +222 -0
- light_vllm_security-0.0.1/README.md +186 -0
- light_vllm_security-0.0.1/light_vllm_security.egg-info/PKG-INFO +222 -0
- light_vllm_security-0.0.1/light_vllm_security.egg-info/SOURCES.txt +21 -0
- light_vllm_security-0.0.1/light_vllm_security.egg-info/dependency_links.txt +1 -0
- light_vllm_security-0.0.1/light_vllm_security.egg-info/entry_points.txt +4 -0
- light_vllm_security-0.0.1/light_vllm_security.egg-info/requires.txt +9 -0
- light_vllm_security-0.0.1/light_vllm_security.egg-info/top_level.txt +1 -0
- light_vllm_security-0.0.1/pyproject.toml +87 -0
- light_vllm_security-0.0.1/setup.cfg +4 -0
- light_vllm_security-0.0.1/vllm/model_security/__init__.py +80 -0
- light_vllm_security-0.0.1/vllm/model_security/cli/__init__.py +4 -0
- light_vllm_security-0.0.1/vllm/model_security/cli/encrypt_model.py +283 -0
- light_vllm_security-0.0.1/vllm/model_security/cli/gen_license.py +263 -0
- light_vllm_security-0.0.1/vllm/model_security/cli/get_fingerprint.py +84 -0
- light_vllm_security-0.0.1/vllm/model_security/crypto.py +231 -0
- light_vllm_security-0.0.1/vllm/model_security/encrypted_loader.py +285 -0
- light_vllm_security-0.0.1/vllm/model_security/fingerprint.py +205 -0
- light_vllm_security-0.0.1/vllm/model_security/license.py +444 -0
- light_vllm_security-0.0.1/vllm/model_security/py.typed +0 -0
- light_vllm_security-0.0.1/vllm_model_security.pth +1 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or otherwise designated in writing by the copyright owner as
|
|
57
|
+
"Not a Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and subsequently
|
|
61
|
+
incorporated within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a
|
|
80
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
81
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
82
|
+
or contributory patent infringement, then any patent licenses
|
|
83
|
+
granted to You under this License for that Work shall terminate
|
|
84
|
+
as of the date such litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or
|
|
92
|
+
Derivative Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the
|
|
105
|
+
attribution notices contained within such NOTICE file, in
|
|
106
|
+
at least one of the following places: within a NOTICE text
|
|
107
|
+
file distributed as part of the Derivative Works; within
|
|
108
|
+
the Source form or documentation, if provided along with the
|
|
109
|
+
Derivative Works; or, within a display generated by the
|
|
110
|
+
Derivative Works, if and wherever such third-party notices
|
|
111
|
+
normally appear. The contents of the NOTICE file are for
|
|
112
|
+
informational purposes only and do not modify the License.
|
|
113
|
+
You may add Your own attribution notices within Derivative
|
|
114
|
+
Works that You distribute, alongside or as an addendum to
|
|
115
|
+
the NOTICE text from the Work, provided that such additional
|
|
116
|
+
attribution notices cannot be construed as modifying the License.
|
|
117
|
+
|
|
118
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
119
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
120
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
121
|
+
this License, without any additional terms or conditions.
|
|
122
|
+
|
|
123
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
124
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
125
|
+
except as required for reasonable and customary use in describing the
|
|
126
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
127
|
+
|
|
128
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
129
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
130
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
131
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
132
|
+
implied, including, without limitation, any warranties or conditions
|
|
133
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
134
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
135
|
+
appropriateness of using or reproducing the Work and assume any
|
|
136
|
+
risks associated with Your exercise of permissions under this License.
|
|
137
|
+
|
|
138
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
139
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
140
|
+
unless required by applicable law (such as deliberate and grossly
|
|
141
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
142
|
+
liable to You for damages, including any direct, indirect, special,
|
|
143
|
+
incidental, or exemplary damages of any character arising as a
|
|
144
|
+
result of this License or out of the use or inability to use the
|
|
145
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
146
|
+
work stoppage, computer failure or malfunction, or all other
|
|
147
|
+
commercial damages or losses), even if such Contributor has been
|
|
148
|
+
advised of the possibility of such damages.
|
|
149
|
+
|
|
150
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
151
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
152
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
153
|
+
or other liability obligations and/or rights consistent with this
|
|
154
|
+
License. However, in accepting such obligations, You may act only
|
|
155
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
156
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
157
|
+
defend, and hold each Contributor harmless for any liability
|
|
158
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
159
|
+
of your accepting any such warranty or additional liability.
|
|
160
|
+
|
|
161
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: light-vllm-security
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Model encryption and authorization extension for vLLM
|
|
5
|
+
Author-email: Lightr <lightr@88.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/W-Lightr/light-vllm-security
|
|
8
|
+
Project-URL: Documentation, https://github.com/W-Lightr/light-vllm-security#readme
|
|
9
|
+
Project-URL: Repository, https://github.com/W-Lightr/light-vllm-security
|
|
10
|
+
Project-URL: Issues, https://github.com/yourusername/light-vllm-security/issues
|
|
11
|
+
Keywords: vllm,llm,model-encryption,authorization,security
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
23
|
+
Classifier: Topic :: Security :: Cryptography
|
|
24
|
+
Requires-Python: >=3.12
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: vllm==0.8.5.post1
|
|
28
|
+
Requires-Dist: cryptography>=41.0.0
|
|
29
|
+
Requires-Dist: psutil>=5.9.0
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
33
|
+
Requires-Dist: black>=23.0; extra == "dev"
|
|
34
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
# light-vllm-security
|
|
38
|
+
|
|
39
|
+
vLLM 模型加密与授权扩展包。为 vLLM 提供模型权重加密、离线授权验证和机器指纹绑定功能。
|
|
40
|
+
|
|
41
|
+
## 特性
|
|
42
|
+
|
|
43
|
+
- 🔐 **AES-256-GCM 加密** — 流式加密,支持大模型分片
|
|
44
|
+
- 📜 **RSA 签名授权** — 离线验证,无需联网
|
|
45
|
+
- 🖥️ **机器指纹绑定** — 防止授权文件被复制到其他机器
|
|
46
|
+
- 🤖 **自动检测** — 检测到 `.enc` 文件自动启用加密加载
|
|
47
|
+
- 🔌 **零侵入** — 不修改官方 vllm 源码,通过命名空间包扩展
|
|
48
|
+
|
|
49
|
+
## 安装
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# 1. 安装官方 vllm
|
|
53
|
+
pip install vllm==0.8.5.post2
|
|
54
|
+
|
|
55
|
+
# 2. 安装本扩展包
|
|
56
|
+
pip install light-vllm-security
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
安装后自动生效,无需任何额外配置。
|
|
60
|
+
|
|
61
|
+
## 快速开始
|
|
62
|
+
|
|
63
|
+
### 第一步:生成厂商密钥对(一次性操作)
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
vllm-gen-license --gen-keypair \
|
|
67
|
+
--private-key vendor_private.pem \
|
|
68
|
+
--public-key vendor_public.pem
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
> ⚠️ **保管好 `vendor_private.pem`!** 这是你的签名私钥,泄露后无法撤销已签发的授权。
|
|
72
|
+
|
|
73
|
+
### 第二步:加密模型
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# 加密 HuggingFace 格式模型目录
|
|
77
|
+
vllm-encrypt-model \
|
|
78
|
+
--input ./my_model \
|
|
79
|
+
--output ./my_model_encrypted
|
|
80
|
+
|
|
81
|
+
# 加密单个 GGUF 文件
|
|
82
|
+
vllm-encrypt-model \
|
|
83
|
+
--input ./model.gguf \
|
|
84
|
+
--output ./model.gguf.enc
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
加密后目录结构:
|
|
88
|
+
```
|
|
89
|
+
my_model_encrypted/
|
|
90
|
+
├── config.json # 原样复制
|
|
91
|
+
├── tokenizer.json # 原样复制
|
|
92
|
+
├── model.safetensors.enc # 加密的权重文件
|
|
93
|
+
└── .model_key # 密钥元数据(需妥善保管)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 第三步:获取客户机器指纹(可选,用于机器绑定)
|
|
97
|
+
|
|
98
|
+
在客户机器上运行:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
vllm-get-fingerprint
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
输出示例:
|
|
105
|
+
```
|
|
106
|
+
Machine Fingerprint: a1b2c3d4e5f6...
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 第四步:生成授权文件
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
# 无机器绑定(测试用)
|
|
113
|
+
vllm-gen-license \
|
|
114
|
+
--model-dir ./my_model_encrypted \
|
|
115
|
+
--vendor-key vendor_private.pem \
|
|
116
|
+
--customer "客户名称" \
|
|
117
|
+
--policy none \
|
|
118
|
+
--out customer.lic
|
|
119
|
+
|
|
120
|
+
# 绑定到特定机器
|
|
121
|
+
vllm-gen-license \
|
|
122
|
+
--model-dir ./my_model_encrypted \
|
|
123
|
+
--vendor-key vendor_private.pem \
|
|
124
|
+
--customer "客户名称" \
|
|
125
|
+
--fingerprint a1b2c3d4e5f6... \
|
|
126
|
+
--policy single \
|
|
127
|
+
--expire 2027-12-31 \
|
|
128
|
+
--out customer.lic
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### 第五步:分发给客户
|
|
132
|
+
|
|
133
|
+
将以下文件发给客户:
|
|
134
|
+
- `my_model_encrypted/` — 加密模型目录
|
|
135
|
+
- `vendor_public.pem` — 厂商公钥(用于验证授权签名)
|
|
136
|
+
- `customer.lic` — 授权文件
|
|
137
|
+
- `vendor_secret.bin` — 厂商密钥(用于解密 DEK,**通过安全渠道传递**)
|
|
138
|
+
|
|
139
|
+
### 第六步:客户运行模型
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
# 方式一:环境变量(推荐)
|
|
143
|
+
export VLLM_VENDOR_SECRET=<vendor_secret_hex>
|
|
144
|
+
vllm serve ./my_model_encrypted \
|
|
145
|
+
--load-format encrypted
|
|
146
|
+
|
|
147
|
+
# 方式二:命令行参数
|
|
148
|
+
vllm serve ./my_model_encrypted \
|
|
149
|
+
--load-format encrypted \
|
|
150
|
+
--model-loader-extra-config '{"vendor_secret": "<hex>", "license_path": "./customer.lic", "vendor_pubkey_path": "./vendor_public.pem"}'
|
|
151
|
+
|
|
152
|
+
# 方式三:自动检测(模型目录包含 .enc 文件时自动启用)
|
|
153
|
+
export VLLM_VENDOR_SECRET=<vendor_secret_hex>
|
|
154
|
+
vllm serve ./my_model_encrypted
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
授权文件和公钥默认从模型目录自动发现:
|
|
158
|
+
- `<model_dir>/model.lic` — 授权文件
|
|
159
|
+
- `<model_dir>/vendor_public.pem` — 厂商公钥
|
|
160
|
+
|
|
161
|
+
## 文件格式
|
|
162
|
+
|
|
163
|
+
### 加密文件格式(`.enc`)
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
[Header 32B]
|
|
167
|
+
magic: 8B "VLLMENC\x00"
|
|
168
|
+
version: 1B 0x01
|
|
169
|
+
algorithm: 1B 0x01 (AES-256-GCM)
|
|
170
|
+
key_id: 16B UUID
|
|
171
|
+
reserved: 6B
|
|
172
|
+
|
|
173
|
+
[Chunk 1]
|
|
174
|
+
size: 4B (little-endian uint32, ciphertext+tag 长度)
|
|
175
|
+
nonce: 12B
|
|
176
|
+
data: N B (ciphertext + 16B GCM tag)
|
|
177
|
+
|
|
178
|
+
[Chunk 2] ...
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### 授权文件格式(`.lic`)
|
|
182
|
+
|
|
183
|
+
JSON 格式,包含:
|
|
184
|
+
- `license_id` — 唯一授权 ID
|
|
185
|
+
- `customer` — 客户名称
|
|
186
|
+
- `model_key_id` — 对应模型的密钥 ID
|
|
187
|
+
- `dek_encrypted` — 加密的数据加密密钥
|
|
188
|
+
- `dek_hash` — DEK 完整性校验
|
|
189
|
+
- `issued_at` / `expire_at` — 签发/过期日期
|
|
190
|
+
- `fingerprint_policy` — 机器绑定策略
|
|
191
|
+
- `allowed_fingerprints` — 允许的机器指纹列表
|
|
192
|
+
- `signature` — RSA-PSS 签名
|
|
193
|
+
|
|
194
|
+
## 安全说明
|
|
195
|
+
|
|
196
|
+
| 组件 | 安全级别 | 说明 |
|
|
197
|
+
|------|---------|------|
|
|
198
|
+
| 模型权重 | AES-256-GCM | 工业级对称加密 |
|
|
199
|
+
| 授权签名 | RSA-3072 PSS | 防篡改 |
|
|
200
|
+
| DEK 保护 | HKDF + AES-256-GCM | 基于 vendor_secret 派生 |
|
|
201
|
+
| 机器绑定 | SHA-256 指纹 | 防止授权文件复制 |
|
|
202
|
+
|
|
203
|
+
**重要:** `vendor_secret` 的安全性等同于 `vendor_private.pem`,必须通过安全渠道传递给客户(如加密邮件、密钥管理服务等)。
|
|
204
|
+
|
|
205
|
+
## 与 vllm 版本兼容性
|
|
206
|
+
|
|
207
|
+
| light-vllm-security | 兼容 vllm 版本 |
|
|
208
|
+
|--------------------|--------------|
|
|
209
|
+
| 0.1.x | 0.8.5.post1+ |
|
|
210
|
+
|
|
211
|
+
## 开发
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
git clone https://github.com/yourusername/light-vllm-security
|
|
215
|
+
cd light-vllm-security
|
|
216
|
+
pip install -e ".[dev]"
|
|
217
|
+
pytest tests/
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## 许可证
|
|
221
|
+
|
|
222
|
+
Apache License 2.0
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# light-vllm-security
|
|
2
|
+
|
|
3
|
+
vLLM 模型加密与授权扩展包。为 vLLM 提供模型权重加密、离线授权验证和机器指纹绑定功能。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🔐 **AES-256-GCM 加密** — 流式加密,支持大模型分片
|
|
8
|
+
- 📜 **RSA 签名授权** — 离线验证,无需联网
|
|
9
|
+
- 🖥️ **机器指纹绑定** — 防止授权文件被复制到其他机器
|
|
10
|
+
- 🤖 **自动检测** — 检测到 `.enc` 文件自动启用加密加载
|
|
11
|
+
- 🔌 **零侵入** — 不修改官方 vllm 源码,通过命名空间包扩展
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# 1. 安装官方 vllm
|
|
17
|
+
pip install vllm==0.8.5.post2
|
|
18
|
+
|
|
19
|
+
# 2. 安装本扩展包
|
|
20
|
+
pip install light-vllm-security
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
安装后自动生效,无需任何额外配置。
|
|
24
|
+
|
|
25
|
+
## 快速开始
|
|
26
|
+
|
|
27
|
+
### 第一步:生成厂商密钥对(一次性操作)
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
vllm-gen-license --gen-keypair \
|
|
31
|
+
--private-key vendor_private.pem \
|
|
32
|
+
--public-key vendor_public.pem
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
> ⚠️ **保管好 `vendor_private.pem`!** 这是你的签名私钥,泄露后无法撤销已签发的授权。
|
|
36
|
+
|
|
37
|
+
### 第二步:加密模型
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# 加密 HuggingFace 格式模型目录
|
|
41
|
+
vllm-encrypt-model \
|
|
42
|
+
--input ./my_model \
|
|
43
|
+
--output ./my_model_encrypted
|
|
44
|
+
|
|
45
|
+
# 加密单个 GGUF 文件
|
|
46
|
+
vllm-encrypt-model \
|
|
47
|
+
--input ./model.gguf \
|
|
48
|
+
--output ./model.gguf.enc
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
加密后目录结构:
|
|
52
|
+
```
|
|
53
|
+
my_model_encrypted/
|
|
54
|
+
├── config.json # 原样复制
|
|
55
|
+
├── tokenizer.json # 原样复制
|
|
56
|
+
├── model.safetensors.enc # 加密的权重文件
|
|
57
|
+
└── .model_key # 密钥元数据(需妥善保管)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 第三步:获取客户机器指纹(可选,用于机器绑定)
|
|
61
|
+
|
|
62
|
+
在客户机器上运行:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
vllm-get-fingerprint
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
输出示例:
|
|
69
|
+
```
|
|
70
|
+
Machine Fingerprint: a1b2c3d4e5f6...
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### 第四步:生成授权文件
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# 无机器绑定(测试用)
|
|
77
|
+
vllm-gen-license \
|
|
78
|
+
--model-dir ./my_model_encrypted \
|
|
79
|
+
--vendor-key vendor_private.pem \
|
|
80
|
+
--customer "客户名称" \
|
|
81
|
+
--policy none \
|
|
82
|
+
--out customer.lic
|
|
83
|
+
|
|
84
|
+
# 绑定到特定机器
|
|
85
|
+
vllm-gen-license \
|
|
86
|
+
--model-dir ./my_model_encrypted \
|
|
87
|
+
--vendor-key vendor_private.pem \
|
|
88
|
+
--customer "客户名称" \
|
|
89
|
+
--fingerprint a1b2c3d4e5f6... \
|
|
90
|
+
--policy single \
|
|
91
|
+
--expire 2027-12-31 \
|
|
92
|
+
--out customer.lic
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 第五步:分发给客户
|
|
96
|
+
|
|
97
|
+
将以下文件发给客户:
|
|
98
|
+
- `my_model_encrypted/` — 加密模型目录
|
|
99
|
+
- `vendor_public.pem` — 厂商公钥(用于验证授权签名)
|
|
100
|
+
- `customer.lic` — 授权文件
|
|
101
|
+
- `vendor_secret.bin` — 厂商密钥(用于解密 DEK,**通过安全渠道传递**)
|
|
102
|
+
|
|
103
|
+
### 第六步:客户运行模型
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
# 方式一:环境变量(推荐)
|
|
107
|
+
export VLLM_VENDOR_SECRET=<vendor_secret_hex>
|
|
108
|
+
vllm serve ./my_model_encrypted \
|
|
109
|
+
--load-format encrypted
|
|
110
|
+
|
|
111
|
+
# 方式二:命令行参数
|
|
112
|
+
vllm serve ./my_model_encrypted \
|
|
113
|
+
--load-format encrypted \
|
|
114
|
+
--model-loader-extra-config '{"vendor_secret": "<hex>", "license_path": "./customer.lic", "vendor_pubkey_path": "./vendor_public.pem"}'
|
|
115
|
+
|
|
116
|
+
# 方式三:自动检测(模型目录包含 .enc 文件时自动启用)
|
|
117
|
+
export VLLM_VENDOR_SECRET=<vendor_secret_hex>
|
|
118
|
+
vllm serve ./my_model_encrypted
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
授权文件和公钥默认从模型目录自动发现:
|
|
122
|
+
- `<model_dir>/model.lic` — 授权文件
|
|
123
|
+
- `<model_dir>/vendor_public.pem` — 厂商公钥
|
|
124
|
+
|
|
125
|
+
## 文件格式
|
|
126
|
+
|
|
127
|
+
### 加密文件格式(`.enc`)
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
[Header 32B]
|
|
131
|
+
magic: 8B "VLLMENC\x00"
|
|
132
|
+
version: 1B 0x01
|
|
133
|
+
algorithm: 1B 0x01 (AES-256-GCM)
|
|
134
|
+
key_id: 16B UUID
|
|
135
|
+
reserved: 6B
|
|
136
|
+
|
|
137
|
+
[Chunk 1]
|
|
138
|
+
size: 4B (little-endian uint32, ciphertext+tag 长度)
|
|
139
|
+
nonce: 12B
|
|
140
|
+
data: N B (ciphertext + 16B GCM tag)
|
|
141
|
+
|
|
142
|
+
[Chunk 2] ...
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 授权文件格式(`.lic`)
|
|
146
|
+
|
|
147
|
+
JSON 格式,包含:
|
|
148
|
+
- `license_id` — 唯一授权 ID
|
|
149
|
+
- `customer` — 客户名称
|
|
150
|
+
- `model_key_id` — 对应模型的密钥 ID
|
|
151
|
+
- `dek_encrypted` — 加密的数据加密密钥
|
|
152
|
+
- `dek_hash` — DEK 完整性校验
|
|
153
|
+
- `issued_at` / `expire_at` — 签发/过期日期
|
|
154
|
+
- `fingerprint_policy` — 机器绑定策略
|
|
155
|
+
- `allowed_fingerprints` — 允许的机器指纹列表
|
|
156
|
+
- `signature` — RSA-PSS 签名
|
|
157
|
+
|
|
158
|
+
## 安全说明
|
|
159
|
+
|
|
160
|
+
| 组件 | 安全级别 | 说明 |
|
|
161
|
+
|------|---------|------|
|
|
162
|
+
| 模型权重 | AES-256-GCM | 工业级对称加密 |
|
|
163
|
+
| 授权签名 | RSA-3072 PSS | 防篡改 |
|
|
164
|
+
| DEK 保护 | HKDF + AES-256-GCM | 基于 vendor_secret 派生 |
|
|
165
|
+
| 机器绑定 | SHA-256 指纹 | 防止授权文件复制 |
|
|
166
|
+
|
|
167
|
+
**重要:** `vendor_secret` 的安全性等同于 `vendor_private.pem`,必须通过安全渠道传递给客户(如加密邮件、密钥管理服务等)。
|
|
168
|
+
|
|
169
|
+
## 与 vllm 版本兼容性
|
|
170
|
+
|
|
171
|
+
| light-vllm-security | 兼容 vllm 版本 |
|
|
172
|
+
|--------------------|--------------|
|
|
173
|
+
| 0.1.x | 0.8.5.post1+ |
|
|
174
|
+
|
|
175
|
+
## 开发
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
git clone https://github.com/yourusername/light-vllm-security
|
|
179
|
+
cd light-vllm-security
|
|
180
|
+
pip install -e ".[dev]"
|
|
181
|
+
pytest tests/
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## 许可证
|
|
185
|
+
|
|
186
|
+
Apache License 2.0
|