cj-tauri 2.3.2 → 2.3.6
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.
- package/cjpm.toml +1 -1
- package/cli/cjpm.toml +12 -1
- package/cli/src/main.cj +8 -0
- package/cli/src/project.cj +15 -16
- package/cli/src/scaffold.cj +1 -1
- package/cli/src/sign_agc.cj +386 -0
- package/cli/src/sign_auto.cj +592 -0
- package/cli/src/sign_cloud.cj +724 -0
- package/cli/src/sign_cmd.cj +444 -0
- package/cli/src/sign_gcm.cj +703 -0
- package/cli/src/sign_login.cj +602 -0
- package/cli/src/sign_offline.cj +1183 -0
- package/cli/src/sign_test.cj +298 -0
- package/cli/src/sign_tool.cj +235 -0
- package/cli/src/sync.cj +90 -25
- package/cli/src/util.cj +26 -1
- package/cli/src/util_path_test.cj +72 -0
- package/cli/templates/app/README.md +1 -0
- package/cli/templates/app-react/README.md +1 -0
- package/cli/templates/app-vue/README.md +1 -0
- package/cli/templates/ohos-skeleton/README.md +19 -1
- package/cli/templates/ohos-skeleton/build-hap.bat +219 -0
- package/package.json +1 -1
- package/src/host_arkweb.cj +10 -0
- package/src/version.cj +1 -1
- package/cli/templates/ohos-skeleton/build-hap.ps1 +0 -205
|
@@ -0,0 +1,703 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* hvigor `AesGcmKit` 兼容的口令加解密 + AES-128-GCM 纯仓颉实现。
|
|
3
|
+
*
|
|
4
|
+
* 写进 build-profile.json5 的 storePassword / keyPassword 不是明文,而是 DevEco/hvigor
|
|
5
|
+
* 的 AesGcmKit 密文;hvigor 在 SignHap 时用它解开 .p12。要"自己生成一套签名材料",
|
|
6
|
+
* 就必须能自己造这个密文(移植自灵渠 0.39.13 的实现,算法逐条对齐
|
|
7
|
+
* hvigor-ohos-plugin 的 decipher-util.js):
|
|
8
|
+
*
|
|
9
|
+
* material/{ac, ce, fd/0,1,2} 每个目录恰好一个文件(随机 32 hex 文件名)
|
|
10
|
+
* passphrase = lossyUtf8( XOR(fd0, fd1, fd2, FIXED16) )
|
|
11
|
+
* rootKey = PBKDF2-HMAC-SHA256(passphrase, salt, 10000, 16)
|
|
12
|
+
* workKey = AES-128-GCM-decrypt(rootKey, payload(ce))
|
|
13
|
+
* ciphertext = hex( payload( AES-128-GCM-encrypt(workKey, password) ) )
|
|
14
|
+
* payload(x) = [ len(4B BE)=|ct|+16 | IV(12) | ct | tag(16) ]
|
|
15
|
+
*
|
|
16
|
+
* 两个源码级细节:
|
|
17
|
+
* ① lossyUtf8:Node 的 Buffer.toString('utf8') 把非法序列换成 U+FFFD 再编码回 UTF-8
|
|
18
|
+
* 才喂给 PBKDF2;直接拿原始字节做 PBKDF2 会得到不同的 rootKey;
|
|
19
|
+
* ② material/ 是 ~/.ohos/config 级共享的(不是每个 p12 一套),所以同一台机器上
|
|
20
|
+
* 新造的口令密文 hvigor 也解得开。
|
|
21
|
+
*
|
|
22
|
+
* AES-128-GCM 为什么自己写:仓颉 std/stdx 没有 AES(stdx.crypto 只有 SM4,SM4-GCM 实测抛
|
|
23
|
+
* create tag error)。这里只支持 AES-128(16B key),逐 bit GHASH,只用于口令/令牌这种
|
|
24
|
+
* 几十字节的数据,不要拿去加密大文件。
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
package cli
|
|
28
|
+
|
|
29
|
+
import std.collection.*
|
|
30
|
+
import std.fs.*
|
|
31
|
+
import std.time.*
|
|
32
|
+
import stdx.crypto.crypto.SecureRandom
|
|
33
|
+
import stdx.crypto.digest.*
|
|
34
|
+
|
|
35
|
+
/** AesGcmKit 的 16 字节固定分量(hvigor 源码常量,别改) */
|
|
36
|
+
public let AES_GCM_KIT_FIXED: Array<Byte> = [
|
|
37
|
+
0x31, 0xf3, 0x09, 0x73, 0xd6, 0xaf, 0x5b, 0xb8, 0xd3, 0xbe, 0xb1, 0x58, 0x65, 0x83, 0xc0, 0x77
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
public let AES_GCM_IV_LEN: Int64 = 12
|
|
41
|
+
public let AES_GCM_TAG_LEN: Int64 = 16
|
|
42
|
+
public let AES_GCM_KEY_LEN: Int64 = 16
|
|
43
|
+
public let PBKDF2_ITERATIONS: Int64 = 10000
|
|
44
|
+
|
|
45
|
+
// ============================================================
|
|
46
|
+
// AES-128(ECB 核心,仅供 GCM 内部使用)
|
|
47
|
+
// ============================================================
|
|
48
|
+
|
|
49
|
+
let AES_SBOX: Array<Byte> = [
|
|
50
|
+
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
|
|
51
|
+
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
|
|
52
|
+
0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
|
|
53
|
+
0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
|
|
54
|
+
0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
|
|
55
|
+
0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
|
|
56
|
+
0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
|
|
57
|
+
0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
|
|
58
|
+
0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
|
|
59
|
+
0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
|
|
60
|
+
0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
|
|
61
|
+
0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
|
|
62
|
+
0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
|
|
63
|
+
0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
|
|
64
|
+
0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
|
|
65
|
+
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
func gcmXtime(x: Int64): Int64 {
|
|
69
|
+
let v = (x << 1) & 0x1FF
|
|
70
|
+
if ((v & 0x100) != 0) {
|
|
71
|
+
return (v & 0xFF) ^ 0x1B
|
|
72
|
+
}
|
|
73
|
+
return v & 0xFF
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func gcmExpandKey128(key: Array<Byte>): Array<Byte> {
|
|
77
|
+
let w = Array<Byte>(176, repeat: 0)
|
|
78
|
+
for (i in 0..16) {
|
|
79
|
+
w[i] = key[i]
|
|
80
|
+
}
|
|
81
|
+
var rcon: Int64 = 1
|
|
82
|
+
var i: Int64 = 16
|
|
83
|
+
while (i < 176) {
|
|
84
|
+
var t = Array<Int64>(4, repeat: 0)
|
|
85
|
+
for (j in 0..4) {
|
|
86
|
+
t[j] = Int64(w[i - 4 + j])
|
|
87
|
+
}
|
|
88
|
+
if (i % 16 == 0) {
|
|
89
|
+
let tmp = t[0]
|
|
90
|
+
t[0] = Int64(AES_SBOX[t[1]])
|
|
91
|
+
t[1] = Int64(AES_SBOX[t[2]])
|
|
92
|
+
t[2] = Int64(AES_SBOX[t[3]])
|
|
93
|
+
t[3] = Int64(AES_SBOX[tmp])
|
|
94
|
+
t[0] = t[0] ^ rcon
|
|
95
|
+
rcon = gcmXtime(rcon)
|
|
96
|
+
}
|
|
97
|
+
for (j in 0..4) {
|
|
98
|
+
w[i + j] = UInt8((Int64(w[i - 16 + j]) ^ t[j]) & 0xFF)
|
|
99
|
+
}
|
|
100
|
+
i = i + 4
|
|
101
|
+
}
|
|
102
|
+
return w
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
func gcmEncryptBlock128(rk: Array<Byte>, input: Array<Byte>): Array<Byte> {
|
|
106
|
+
var s = Array<Int64>(16, repeat: 0)
|
|
107
|
+
for (i in 0..16) {
|
|
108
|
+
s[i] = Int64(input[i]) ^ Int64(rk[i])
|
|
109
|
+
}
|
|
110
|
+
// state 按列存放:s[4*c + r]
|
|
111
|
+
for (round in 1..=10) {
|
|
112
|
+
let t = Array<Int64>(16, repeat: 0)
|
|
113
|
+
for (c in 0..4) {
|
|
114
|
+
for (r in 0..4) {
|
|
115
|
+
t[4 * c + r] = Int64(AES_SBOX[s[4 * ((c + r) % 4) + r]])
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (k in 0..16) {
|
|
119
|
+
s[k] = t[k]
|
|
120
|
+
}
|
|
121
|
+
if (round != 10) {
|
|
122
|
+
for (c in 0..4) {
|
|
123
|
+
let a0 = s[4 * c + 0]
|
|
124
|
+
let a1 = s[4 * c + 1]
|
|
125
|
+
let a2 = s[4 * c + 2]
|
|
126
|
+
let a3 = s[4 * c + 3]
|
|
127
|
+
s[4 * c + 0] = gcmXtime(a0) ^ (gcmXtime(a1) ^ a1) ^ a2 ^ a3
|
|
128
|
+
s[4 * c + 1] = a0 ^ gcmXtime(a1) ^ (gcmXtime(a2) ^ a2) ^ a3
|
|
129
|
+
s[4 * c + 2] = a0 ^ a1 ^ gcmXtime(a2) ^ (gcmXtime(a3) ^ a3)
|
|
130
|
+
s[4 * c + 3] = (gcmXtime(a0) ^ a0) ^ a1 ^ a2 ^ gcmXtime(a3)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
for (k in 0..16) {
|
|
134
|
+
s[k] = s[k] ^ Int64(rk[16 * round + k])
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
let out = Array<Byte>(16, repeat: 0)
|
|
138
|
+
for (i in 0..16) {
|
|
139
|
+
out[i] = UInt8(s[i] & 0xFF)
|
|
140
|
+
}
|
|
141
|
+
return out
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ============================================================
|
|
145
|
+
// GF(2^128) 乘法(GHASH 用)
|
|
146
|
+
// ============================================================
|
|
147
|
+
|
|
148
|
+
func gcmXor16(a: Array<Byte>, b: Array<Byte>): Array<Byte> {
|
|
149
|
+
let r = Array<Byte>(16, repeat: 0)
|
|
150
|
+
for (i in 0..16) {
|
|
151
|
+
r[i] = a[i] ^ b[i]
|
|
152
|
+
}
|
|
153
|
+
return r
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
func gcmGf128Mul(x: Array<Byte>, y: Array<Byte>): Array<Byte> {
|
|
157
|
+
var z = Array<Int64>(16, repeat: 0)
|
|
158
|
+
var v = Array<Int64>(16, repeat: 0)
|
|
159
|
+
for (i in 0..16) {
|
|
160
|
+
v[i] = Int64(y[i])
|
|
161
|
+
}
|
|
162
|
+
for (i in 0..128) {
|
|
163
|
+
let bit = (Int64(x[i / 8]) >> (7 - (i % 8))) & 1
|
|
164
|
+
if (bit == 1) {
|
|
165
|
+
for (k in 0..16) {
|
|
166
|
+
z[k] = z[k] ^ v[k]
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
let lsb = v[15] & 1
|
|
170
|
+
var carry: Int64 = 0
|
|
171
|
+
for (k in 0..16) {
|
|
172
|
+
let cur = v[k]
|
|
173
|
+
v[k] = (cur >> 1) | (carry << 7)
|
|
174
|
+
carry = cur & 1
|
|
175
|
+
}
|
|
176
|
+
if (lsb == 1) {
|
|
177
|
+
v[0] = v[0] ^ 0xE1
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
let out = Array<Byte>(16, repeat: 0)
|
|
181
|
+
for (i in 0..16) {
|
|
182
|
+
out[i] = UInt8(z[i] & 0xFF)
|
|
183
|
+
}
|
|
184
|
+
return out
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
func gcmAbsorb(s0: Array<Byte>, h: Array<Byte>, data: Array<Byte>): Array<Byte> {
|
|
188
|
+
var s = s0
|
|
189
|
+
var i: Int64 = 0
|
|
190
|
+
while (i < data.size) {
|
|
191
|
+
let blk = Array<Byte>(16, repeat: 0)
|
|
192
|
+
var n: Int64 = 0
|
|
193
|
+
while (n < 16 && i + n < data.size) {
|
|
194
|
+
blk[n] = data[i + n]
|
|
195
|
+
n = n + 1
|
|
196
|
+
}
|
|
197
|
+
s = gcmGf128Mul(gcmXor16(s, blk), h)
|
|
198
|
+
i = i + 16
|
|
199
|
+
}
|
|
200
|
+
return s
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
func gcmPutBe64(buf: Array<Byte>, off: Int64, value: Int64) {
|
|
204
|
+
for (i in 0..8) {
|
|
205
|
+
buf[off + i] = UInt8((value >> (56 - 8 * i)) & 0xFF)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
func gcmInc32(ctr: Array<Byte>) {
|
|
210
|
+
for (i in 0..4) {
|
|
211
|
+
let idx = 15 - i
|
|
212
|
+
if (ctr[idx] == 0xFF) {
|
|
213
|
+
ctr[idx] = 0
|
|
214
|
+
} else {
|
|
215
|
+
ctr[idx] = ctr[idx] + 1
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
func gcmJ0(nonce: Array<Byte>, h: Array<Byte>): Array<Byte> {
|
|
222
|
+
if (nonce.size == 12) {
|
|
223
|
+
let j = Array<Byte>(16, repeat: 0)
|
|
224
|
+
for (i in 0..12) {
|
|
225
|
+
j[i] = nonce[i]
|
|
226
|
+
}
|
|
227
|
+
j[15] = 1
|
|
228
|
+
return j
|
|
229
|
+
}
|
|
230
|
+
var s = gcmAbsorb(Array<Byte>(16, repeat: 0), h, nonce)
|
|
231
|
+
let lenBlk = Array<Byte>(16, repeat: 0)
|
|
232
|
+
gcmPutBe64(lenBlk, 8, nonce.size * 8)
|
|
233
|
+
return gcmGf128Mul(gcmXor16(s, lenBlk), h)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ============================================================
|
|
237
|
+
// 对外 API:加密输出 ciphertext || tag(16B);解密 tag 不符抛异常
|
|
238
|
+
// ============================================================
|
|
239
|
+
|
|
240
|
+
/** AES-128-GCM 加密:返回 ciphertext || tag(16B) */
|
|
241
|
+
public func aes128GcmEncrypt(key: Array<Byte>, nonce: Array<Byte>, plaintext: Array<Byte>,
|
|
242
|
+
aad: Array<Byte>): Array<Byte> {
|
|
243
|
+
if (key.size != 16) {
|
|
244
|
+
throw IllegalArgumentException("AES-128 key must be 16 bytes, got ${key.size}")
|
|
245
|
+
}
|
|
246
|
+
if (nonce.size == 0) {
|
|
247
|
+
throw IllegalArgumentException("nonce must not be empty")
|
|
248
|
+
}
|
|
249
|
+
let rk = gcmExpandKey128(key)
|
|
250
|
+
let h = gcmEncryptBlock128(rk, Array<Byte>(16, repeat: 0))
|
|
251
|
+
let j0 = gcmJ0(nonce, h)
|
|
252
|
+
// CTR 模式加密
|
|
253
|
+
let out = ArrayList<Byte>()
|
|
254
|
+
let ctr = Array<Byte>(16, repeat: 0)
|
|
255
|
+
for (i in 0..16) {
|
|
256
|
+
ctr[i] = j0[i]
|
|
257
|
+
}
|
|
258
|
+
var i: Int64 = 0
|
|
259
|
+
while (i < plaintext.size) {
|
|
260
|
+
gcmInc32(ctr)
|
|
261
|
+
let ks = gcmEncryptBlock128(rk, ctr)
|
|
262
|
+
var n: Int64 = 0
|
|
263
|
+
while (n < 16 && i + n < plaintext.size) {
|
|
264
|
+
out.add(plaintext[i + n] ^ ks[n])
|
|
265
|
+
n = n + 1
|
|
266
|
+
}
|
|
267
|
+
i = i + 16
|
|
268
|
+
}
|
|
269
|
+
let ct = out.toArray()
|
|
270
|
+
// tag = E(k, J0) XOR GHASH(aad || ct || len)
|
|
271
|
+
var s = gcmAbsorb(Array<Byte>(16, repeat: 0), h, aad)
|
|
272
|
+
s = gcmAbsorb(s, h, ct)
|
|
273
|
+
let lenBlk = Array<Byte>(16, repeat: 0)
|
|
274
|
+
gcmPutBe64(lenBlk, 0, aad.size * 8)
|
|
275
|
+
gcmPutBe64(lenBlk, 8, ct.size * 8)
|
|
276
|
+
s = gcmGf128Mul(gcmXor16(s, lenBlk), h)
|
|
277
|
+
let tag = gcmXor16(gcmEncryptBlock128(rk, j0), s)
|
|
278
|
+
let sealed = ArrayList<Byte>()
|
|
279
|
+
sealed.add(all: ct)
|
|
280
|
+
sealed.add(all: tag)
|
|
281
|
+
return sealed.toArray()
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** AES-128-GCM 解密:输入 ciphertext || tag(16B),tag 不符抛 IllegalArgumentException */
|
|
285
|
+
public func aes128GcmDecrypt(key: Array<Byte>, nonce: Array<Byte>, sealed: Array<Byte>,
|
|
286
|
+
aad: Array<Byte>): Array<Byte> {
|
|
287
|
+
if (sealed.size < 16) {
|
|
288
|
+
throw IllegalArgumentException("sealed data shorter than 16-byte tag")
|
|
289
|
+
}
|
|
290
|
+
let ctLen = sealed.size - 16
|
|
291
|
+
let ct = sealed[0..ctLen]
|
|
292
|
+
let tag = sealed[ctLen..sealed.size]
|
|
293
|
+
let rk = gcmExpandKey128(key)
|
|
294
|
+
let h = gcmEncryptBlock128(rk, Array<Byte>(16, repeat: 0))
|
|
295
|
+
let j0 = gcmJ0(nonce, h)
|
|
296
|
+
var s = gcmAbsorb(Array<Byte>(16, repeat: 0), h, aad)
|
|
297
|
+
s = gcmAbsorb(s, h, ct)
|
|
298
|
+
let lenBlk = Array<Byte>(16, repeat: 0)
|
|
299
|
+
gcmPutBe64(lenBlk, 0, aad.size * 8)
|
|
300
|
+
gcmPutBe64(lenBlk, 8, ct.size * 8)
|
|
301
|
+
s = gcmGf128Mul(gcmXor16(s, lenBlk), h)
|
|
302
|
+
let expect = gcmXor16(gcmEncryptBlock128(rk, j0), s)
|
|
303
|
+
// 常数时间比较
|
|
304
|
+
var diff: Int64 = 0
|
|
305
|
+
for (i in 0..16) {
|
|
306
|
+
diff = diff | (Int64(expect[i] ^ tag[i]))
|
|
307
|
+
}
|
|
308
|
+
if (diff != 0) {
|
|
309
|
+
throw IllegalArgumentException("GCM tag mismatch")
|
|
310
|
+
}
|
|
311
|
+
let out = ArrayList<Byte>()
|
|
312
|
+
let ctr = Array<Byte>(16, repeat: 0)
|
|
313
|
+
for (i in 0..16) {
|
|
314
|
+
ctr[i] = j0[i]
|
|
315
|
+
}
|
|
316
|
+
var i: Int64 = 0
|
|
317
|
+
while (i < ct.size) {
|
|
318
|
+
gcmInc32(ctr)
|
|
319
|
+
let ks = gcmEncryptBlock128(rk, ctr)
|
|
320
|
+
var n: Int64 = 0
|
|
321
|
+
while (n < 16 && i + n < ct.size) {
|
|
322
|
+
out.add(ct[i + n] ^ ks[n])
|
|
323
|
+
n = n + 1
|
|
324
|
+
}
|
|
325
|
+
i = i + 16
|
|
326
|
+
}
|
|
327
|
+
return out.toArray()
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// ============================================================
|
|
331
|
+
// PBKDF2-HMAC-SHA256(RFC 8018)——stdx 没有现成实现,自己写
|
|
332
|
+
// ============================================================
|
|
333
|
+
|
|
334
|
+
/** HMAC-SHA256(32B) */
|
|
335
|
+
public func signHmacSha256(key: Array<Byte>, data: Array<Byte>): Array<Byte> {
|
|
336
|
+
let mac = HMAC(key, HashType.SHA256)
|
|
337
|
+
mac.write(data)
|
|
338
|
+
return mac.finish()
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
public func signPbkdf2HmacSha256(password: Array<Byte>, salt: Array<Byte>, iterations: Int64,
|
|
342
|
+
dkLen: Int64): Array<Byte> {
|
|
343
|
+
let hLen: Int64 = 32
|
|
344
|
+
let blocks: Int64 = (dkLen + hLen - 1) / hLen
|
|
345
|
+
let out = ArrayList<Byte>()
|
|
346
|
+
var i: Int64 = 1
|
|
347
|
+
while (i <= blocks) {
|
|
348
|
+
// U1 = HMAC(password, salt || INT32BE(i))
|
|
349
|
+
let first = ArrayList<Byte>()
|
|
350
|
+
first.add(all: salt)
|
|
351
|
+
first.add(UInt8((i >> 24) & 0xFF))
|
|
352
|
+
first.add(UInt8((i >> 16) & 0xFF))
|
|
353
|
+
first.add(UInt8((i >> 8) & 0xFF))
|
|
354
|
+
first.add(UInt8(i & 0xFF))
|
|
355
|
+
var u: Array<Byte> = signHmacSha256(password, first.toArray())
|
|
356
|
+
let acc = u.clone()
|
|
357
|
+
var j: Int64 = 1
|
|
358
|
+
while (j < iterations) {
|
|
359
|
+
u = signHmacSha256(password, u)
|
|
360
|
+
for (k in 0..acc.size) {
|
|
361
|
+
acc[k] = acc[k] ^ u[k]
|
|
362
|
+
}
|
|
363
|
+
j += 1
|
|
364
|
+
}
|
|
365
|
+
out.add(all: acc)
|
|
366
|
+
i += 1
|
|
367
|
+
}
|
|
368
|
+
let all = out.toArray()
|
|
369
|
+
return all[0..dkLen]
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* 有损 UTF-8 规范化:合法序列原样保留,非法序列按一个 U+FFFD(EF BF BD)替换。
|
|
374
|
+
*
|
|
375
|
+
* 与 Node Buffer.toString('utf8') 同语义——AesGcmKit 的 passphrase 就是这么来的。
|
|
376
|
+
* 不这么做的后果:rootKey 对不上,解出的 workKey 是错的,hvigor 只报"签名失败"。
|
|
377
|
+
*/
|
|
378
|
+
public func lossyUtf8Bytes(data: Array<Byte>): Array<Byte> {
|
|
379
|
+
let out = ArrayList<Byte>()
|
|
380
|
+
var i: Int64 = 0
|
|
381
|
+
let n = Int64(data.size)
|
|
382
|
+
while (i < n) {
|
|
383
|
+
let b = Int64(data[i])
|
|
384
|
+
var need: Int64 = 0
|
|
385
|
+
var cp: Int64 = 0
|
|
386
|
+
if (b < 0x80) {
|
|
387
|
+
out.add(UInt8(b))
|
|
388
|
+
i += 1
|
|
389
|
+
continue
|
|
390
|
+
} else if ((b & 0xE0) == 0xC0) {
|
|
391
|
+
need = 1
|
|
392
|
+
cp = b & 0x1F
|
|
393
|
+
} else if ((b & 0xF0) == 0xE0) {
|
|
394
|
+
need = 2
|
|
395
|
+
cp = b & 0x0F
|
|
396
|
+
} else if ((b & 0xF8) == 0xF0) {
|
|
397
|
+
need = 3
|
|
398
|
+
cp = b & 0x07
|
|
399
|
+
} else {
|
|
400
|
+
out.add(all: [0xEF, 0xBF, 0xBD])
|
|
401
|
+
i += 1
|
|
402
|
+
continue
|
|
403
|
+
}
|
|
404
|
+
if (i + need >= n) {
|
|
405
|
+
out.add(all: [0xEF, 0xBF, 0xBD])
|
|
406
|
+
i += 1
|
|
407
|
+
continue
|
|
408
|
+
}
|
|
409
|
+
var ok = true
|
|
410
|
+
var k: Int64 = 1
|
|
411
|
+
while (k <= need) {
|
|
412
|
+
let c = Int64(data[i + k])
|
|
413
|
+
if ((c & 0xC0) != 0x80) {
|
|
414
|
+
ok = false
|
|
415
|
+
break
|
|
416
|
+
}
|
|
417
|
+
cp = (cp << 6) | (c & 0x3F)
|
|
418
|
+
k += 1
|
|
419
|
+
}
|
|
420
|
+
// 过长编码 / 代理区 / 超范围都算非法(Node 同款判断)
|
|
421
|
+
if (ok) {
|
|
422
|
+
let minCp = if (need == 1) { 0x80 } else if (need == 2) { 0x800 } else { 0x10000 }
|
|
423
|
+
if (cp < minCp || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) {
|
|
424
|
+
ok = false
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (!ok) {
|
|
428
|
+
out.add(all: [0xEF, 0xBF, 0xBD])
|
|
429
|
+
i += 1
|
|
430
|
+
continue
|
|
431
|
+
}
|
|
432
|
+
for (m in 0..(need + 1)) {
|
|
433
|
+
out.add(data[i + m])
|
|
434
|
+
}
|
|
435
|
+
i = i + need + 1
|
|
436
|
+
}
|
|
437
|
+
return out.toArray()
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// ============================================================
|
|
441
|
+
// 随机源
|
|
442
|
+
// ============================================================
|
|
443
|
+
|
|
444
|
+
/** 随机字节(口令 / IV 用;系统随机源拿不到时退到时间种子,仅影响不可预测性) */
|
|
445
|
+
public func signRandomBytes(n: Int64): Array<Byte> {
|
|
446
|
+
try {
|
|
447
|
+
let buf = Array<Byte>(n, repeat: 0)
|
|
448
|
+
SecureRandom().nextBytes(buf)
|
|
449
|
+
return buf
|
|
450
|
+
} catch (e: Exception) {
|
|
451
|
+
let _ = e
|
|
452
|
+
let out = Array<Byte>(n, repeat: 0)
|
|
453
|
+
var seed = Int64(DateTime.now().toUnixTimeStamp().toNanoseconds()) & 0x7FFFFFFFFFFFFFFF
|
|
454
|
+
for (i in 0..n) {
|
|
455
|
+
seed = (seed * 6364136223846793005 + 1442695040888963407) & 0x7FFFFFFFFFFFFFFF
|
|
456
|
+
out[i] = UInt8((seed >> 33) & 0xFF)
|
|
457
|
+
}
|
|
458
|
+
return out
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// ============================================================
|
|
463
|
+
// AesGcmKit 载荷布局与 workKey
|
|
464
|
+
// ============================================================
|
|
465
|
+
|
|
466
|
+
/** 一段口令密文的三个组成部分 */
|
|
467
|
+
public class GcmPayload {
|
|
468
|
+
public var iv: Array<Byte> = []
|
|
469
|
+
public var ciphertext: Array<Byte> = []
|
|
470
|
+
public var tag: Array<Byte> = []
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** 按 AesGcmKit 布局打包:[len(4BE) | IV(12) | ct | tag(16)],len = |ct| + 16 */
|
|
474
|
+
public func packGcmPayload(iv: Array<Byte>, ciphertext: Array<Byte>,
|
|
475
|
+
tag: Array<Byte>): Option<Array<Byte>> {
|
|
476
|
+
if (iv.size != AES_GCM_IV_LEN || tag.size != AES_GCM_TAG_LEN) {
|
|
477
|
+
return None
|
|
478
|
+
}
|
|
479
|
+
let bodyLen = Int64(ciphertext.size) + AES_GCM_TAG_LEN
|
|
480
|
+
let out = ArrayList<Byte>()
|
|
481
|
+
out.add(UInt8((bodyLen >> 24) & 0xFF))
|
|
482
|
+
out.add(UInt8((bodyLen >> 16) & 0xFF))
|
|
483
|
+
out.add(UInt8((bodyLen >> 8) & 0xFF))
|
|
484
|
+
out.add(UInt8(bodyLen & 0xFF))
|
|
485
|
+
for (b in iv) {
|
|
486
|
+
out.add(b)
|
|
487
|
+
}
|
|
488
|
+
for (b in ciphertext) {
|
|
489
|
+
out.add(b)
|
|
490
|
+
}
|
|
491
|
+
for (b in tag) {
|
|
492
|
+
out.add(b)
|
|
493
|
+
}
|
|
494
|
+
return Some(out.toArray())
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** 拆包(太短 / 声明长度与实到不符 → None) */
|
|
498
|
+
public func unpackGcmPayload(raw: Array<Byte>): Option<GcmPayload> {
|
|
499
|
+
let minLen = 4 + AES_GCM_IV_LEN + AES_GCM_TAG_LEN
|
|
500
|
+
if (Int64(raw.size) < minLen) {
|
|
501
|
+
return None
|
|
502
|
+
}
|
|
503
|
+
let bodyLen = (Int64(raw[0]) << 24) | (Int64(raw[1]) << 16) | (Int64(raw[2]) << 8) |
|
|
504
|
+
Int64(raw[3])
|
|
505
|
+
if (bodyLen < AES_GCM_TAG_LEN || 4 + AES_GCM_IV_LEN + bodyLen > Int64(raw.size)) {
|
|
506
|
+
return None
|
|
507
|
+
}
|
|
508
|
+
let p = GcmPayload()
|
|
509
|
+
p.iv = raw[4..(4 + AES_GCM_IV_LEN)]
|
|
510
|
+
let body = raw[(4 + AES_GCM_IV_LEN)..(4 + AES_GCM_IV_LEN + bodyLen)]
|
|
511
|
+
let ctLen = Int64(body.size) - AES_GCM_TAG_LEN
|
|
512
|
+
p.ciphertext = body[0..ctLen]
|
|
513
|
+
p.tag = body[ctLen..body.size]
|
|
514
|
+
return Some(p)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** 小写 hex 编码 */
|
|
518
|
+
public func toHexLower(data: Array<Byte>): String {
|
|
519
|
+
let digits = "0123456789abcdef".toArray()
|
|
520
|
+
let out = ArrayList<Byte>()
|
|
521
|
+
for (b in data) {
|
|
522
|
+
let v = Int64(b)
|
|
523
|
+
out.add(digits[(v >> 4) & 15])
|
|
524
|
+
out.add(digits[v & 15])
|
|
525
|
+
}
|
|
526
|
+
return String.fromUtf8(out.toArray())
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** hex → 字节(长度奇数或含非 hex 字符时为 None) */
|
|
530
|
+
public func fromHexLower(s: String): Option<Array<Byte>> {
|
|
531
|
+
let text = s.trimAscii()
|
|
532
|
+
let bytes = text.toArray()
|
|
533
|
+
if (bytes.size % 2 != 0 || bytes.size == 0) {
|
|
534
|
+
return None
|
|
535
|
+
}
|
|
536
|
+
let out = ArrayList<Byte>()
|
|
537
|
+
var i: Int64 = 0
|
|
538
|
+
while (i < bytes.size) {
|
|
539
|
+
let hi = signHexNibbleOf(bytes[i])
|
|
540
|
+
let lo = signHexNibbleOf(bytes[i + 1])
|
|
541
|
+
if (hi < 0 || lo < 0) {
|
|
542
|
+
return None
|
|
543
|
+
}
|
|
544
|
+
out.add(UInt8((hi << 4) | lo))
|
|
545
|
+
i += 2
|
|
546
|
+
}
|
|
547
|
+
return Some(out.toArray())
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** 单个 hex 字符的数值(非 hex 给 -1) */
|
|
551
|
+
public func signHexNibbleOf(b: Byte): Int64 {
|
|
552
|
+
if (b >= 48 && b <= 57) {
|
|
553
|
+
return Int64(b - 48)
|
|
554
|
+
}
|
|
555
|
+
if (b >= 97 && b <= 102) {
|
|
556
|
+
return Int64(b - 87)
|
|
557
|
+
}
|
|
558
|
+
if (b >= 65 && b <= 70) {
|
|
559
|
+
return Int64(b - 55)
|
|
560
|
+
}
|
|
561
|
+
return -1
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** 拼两段字节 */
|
|
565
|
+
public func signConcatBytes(a: Array<Byte>, b: Array<Byte>): Array<Byte> {
|
|
566
|
+
let out = ArrayList<Byte>()
|
|
567
|
+
out.add(all: a)
|
|
568
|
+
out.add(all: b)
|
|
569
|
+
return out.toArray()
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// ============================================================
|
|
573
|
+
// workKey 读取与口令加密(~/.ohos/config/material)
|
|
574
|
+
// ============================================================
|
|
575
|
+
|
|
576
|
+
/** material 目录(与 .p12 同级、~/.ohos/config 级共享) */
|
|
577
|
+
public func materialDirOfConfig(configDir: String): String {
|
|
578
|
+
return "${configDir}/material"
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** 读目录里恰好一个文件的字节(0 个或多个都算错:DevEco 的约定是"一个目录一个文件") */
|
|
582
|
+
func readUniqueFileBytes(dir: String): (Array<Byte>, String) {
|
|
583
|
+
if (!exists(Path(dir))) {
|
|
584
|
+
return ([], "目录不存在:${dir}")
|
|
585
|
+
}
|
|
586
|
+
var entries: Array<FileInfo>
|
|
587
|
+
try {
|
|
588
|
+
entries = Directory.readFrom(dir)
|
|
589
|
+
} catch (e: Exception) {
|
|
590
|
+
return ([], "读目录失败 ${dir}:${e}")
|
|
591
|
+
}
|
|
592
|
+
let files = ArrayList<String>()
|
|
593
|
+
for (info in entries) {
|
|
594
|
+
if (!info.isDirectory() && info.name != ".DS_Store") {
|
|
595
|
+
files.add("${dir}/${info.name}")
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
if (files.size != 1) {
|
|
599
|
+
return ([], "期望 ${dir} 下恰好 1 个文件,实际 ${files.size} 个")
|
|
600
|
+
}
|
|
601
|
+
try {
|
|
602
|
+
return (File.readFrom(files[0]), "")
|
|
603
|
+
} catch (e: Exception) {
|
|
604
|
+
return ([], "读文件失败 ${files[0]}:${e}")
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* 从 material/ 解出 workKey(AES-128 的 16 字节)。
|
|
610
|
+
* 失败一律带原因返回(材料缺文件、长度不对、GCM 认证失败都要说清是哪一步)。
|
|
611
|
+
*/
|
|
612
|
+
public func loadWorkKey(materialDir: String): (Array<Byte>, String) {
|
|
613
|
+
let (salt, e1) = readUniqueFileBytes("${materialDir}/ac")
|
|
614
|
+
if (e1 != "") {
|
|
615
|
+
return ([], "盐(material/ac):${e1}")
|
|
616
|
+
}
|
|
617
|
+
if (salt.size != AES_GCM_KEY_LEN) {
|
|
618
|
+
return ([], "盐长度应为 ${AES_GCM_KEY_LEN},实际 ${salt.size}")
|
|
619
|
+
}
|
|
620
|
+
let (ce, e2) = readUniqueFileBytes("${materialDir}/ce")
|
|
621
|
+
if (e2 != "") {
|
|
622
|
+
return ([], "workKey 密文(material/ce):${e2}")
|
|
623
|
+
}
|
|
624
|
+
let comps = ArrayList<Array<Byte>>()
|
|
625
|
+
for (i in 0..3) {
|
|
626
|
+
let (c, ec) = readUniqueFileBytes("${materialDir}/fd/${i}")
|
|
627
|
+
if (ec != "") {
|
|
628
|
+
return ([], "随机分量 fd/${i}:${ec}")
|
|
629
|
+
}
|
|
630
|
+
if (c.size != AES_GCM_KEY_LEN) {
|
|
631
|
+
return ([], "随机分量 fd/${i} 长度应为 ${AES_GCM_KEY_LEN},实际 ${c.size}")
|
|
632
|
+
}
|
|
633
|
+
comps.add(c)
|
|
634
|
+
}
|
|
635
|
+
// passphrase = lossyUtf8(XOR(三个分量, 固定分量))
|
|
636
|
+
let xored = Array<Byte>(AES_GCM_KEY_LEN, repeat: 0)
|
|
637
|
+
for (c in comps) {
|
|
638
|
+
for (i in 0..AES_GCM_KEY_LEN) {
|
|
639
|
+
xored[i] = xored[i] ^ c[i]
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
for (i in 0..AES_GCM_KEY_LEN) {
|
|
643
|
+
xored[i] = xored[i] ^ AES_GCM_KIT_FIXED[i]
|
|
644
|
+
}
|
|
645
|
+
let passphrase = lossyUtf8Bytes(xored)
|
|
646
|
+
let rootKey = signPbkdf2HmacSha256(passphrase, salt, PBKDF2_ITERATIONS, AES_GCM_KEY_LEN)
|
|
647
|
+
match (unpackGcmPayload(ce)) {
|
|
648
|
+
case Some(p) =>
|
|
649
|
+
var workKey: Array<Byte> = []
|
|
650
|
+
try {
|
|
651
|
+
workKey = aes128GcmDecrypt(rootKey, p.iv,
|
|
652
|
+
signConcatBytes(p.ciphertext, p.tag), Array<Byte>())
|
|
653
|
+
} catch (e: Exception) {
|
|
654
|
+
return ([], "解 workKey 失败(material/ 与口令不配套):${e}")
|
|
655
|
+
}
|
|
656
|
+
return (workKey, "")
|
|
657
|
+
case None =>
|
|
658
|
+
return ([], "material/ce 载荷格式不对(应为 len|iv|ct|tag)")
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* 口令 → build-profile.json5 里能直接写的那串 hex(AesGcmKit 兼容)。
|
|
664
|
+
* 每次调用 IV 随机 → 同一口令两次加密结果不同(与 DevEco 一致,别写相等断言)。
|
|
665
|
+
*/
|
|
666
|
+
public func encryptPasswordHex(workKey: Array<Byte>, password: String): (String, String) {
|
|
667
|
+
if (workKey.size != AES_GCM_KEY_LEN) {
|
|
668
|
+
return ("", "workKey 长度应为 ${AES_GCM_KEY_LEN},实际 ${workKey.size}")
|
|
669
|
+
}
|
|
670
|
+
let iv = signRandomBytes(AES_GCM_IV_LEN)
|
|
671
|
+
let sealed = aes128GcmEncrypt(workKey, iv, password.toArray(), Array<Byte>())
|
|
672
|
+
let ctLen = Int64(sealed.size) - AES_GCM_TAG_LEN
|
|
673
|
+
if (ctLen < 0) {
|
|
674
|
+
return ("", "GCM 输出异常(长度 ${sealed.size})")
|
|
675
|
+
}
|
|
676
|
+
let ct = sealed[0..ctLen]
|
|
677
|
+
let tag = sealed[ctLen..sealed.size]
|
|
678
|
+
match (packGcmPayload(iv, ct, tag)) {
|
|
679
|
+
case Some(raw) => return (toHexLower(raw), "")
|
|
680
|
+
case None => return ("", "载荷打包失败(IV/tag 长度异常)")
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/** 反向:hex 密文 + workKey → 明文口令(自检用:证明我们写的密文 hvigor 也解得开) */
|
|
685
|
+
public func decryptPasswordHex(workKey: Array<Byte>, hex: String): (String, String) {
|
|
686
|
+
match (fromHexLower(hex)) {
|
|
687
|
+
case Some(raw) =>
|
|
688
|
+
match (unpackGcmPayload(raw)) {
|
|
689
|
+
case Some(p) =>
|
|
690
|
+
try {
|
|
691
|
+
let plain = aes128GcmDecrypt(workKey, p.iv,
|
|
692
|
+
signConcatBytes(p.ciphertext, p.tag), Array<Byte>())
|
|
693
|
+
return (String.fromUtf8(plain), "")
|
|
694
|
+
} catch (e: Exception) {
|
|
695
|
+
return ("", "口令解密失败:${e}")
|
|
696
|
+
}
|
|
697
|
+
case None =>
|
|
698
|
+
return ("", "口令密文载荷格式不对")
|
|
699
|
+
}
|
|
700
|
+
case None =>
|
|
701
|
+
return ("", "口令密文不是合法 hex")
|
|
702
|
+
}
|
|
703
|
+
}
|