-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbign_test.go
More file actions
457 lines (388 loc) · 15.2 KB
/
Copy pathbign_test.go
File metadata and controls
457 lines (388 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package bee2go
import (
"bytes"
"testing"
)
// ────────────────────────────────────────────────────────────────────────────
// OID → DER encoding
// ────────────────────────────────────────────────────────────────────────────
func TestBignOidToDER(t *testing.T) {
// The DER encoding of "1.2.112.0.2.0.34.101.31.81" must be 11 bytes.
der, err := BignOidToDER(OIDBeltHash)
if err != nil {
t.Fatal(err)
}
if len(der) != 11 {
t.Fatalf("DER length=%d want 11", len(der))
}
// BeltHashOID is a convenience alias.
der2, err := BeltHashOID()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(der, der2) {
t.Fatal("BeltHashOID != BignOidToDER(OIDBeltHash)")
}
// Invalid OID must return an error.
if _, err := BignOidToDER("99.99.99.notanoid"); err == nil {
t.Error("invalid OID should fail")
}
}
// ────────────────────────────────────────────────────────────────────────────
// Helpers used across bign tests
// ────────────────────────────────────────────────────────────────────────────
func newParams256(t *testing.T) *BignParams {
t.Helper()
p, err := NewBignParams256v1()
if err != nil {
t.Fatalf("NewBignParams256v1: %v", err)
}
return p
}
func beltHashOf(t *testing.T, data []byte) []byte {
t.Helper()
h, err := BeltHash(data)
if err != nil {
t.Fatalf("BeltHash: %v", err)
}
return h
}
// ────────────────────────────────────────────────────────────────────────────
// Params accessors
// ────────────────────────────────────────────────────────────────────────────
func TestBignParamsAccessors(t *testing.T) {
p := newParams256(t)
defer p.Free()
if p.L() != 128 {
t.Errorf("L()=%d want 128", p.L())
}
if p.PrivKeyLen() != 32 {
t.Errorf("PrivKeyLen()=%d want 32", p.PrivKeyLen())
}
if p.PubKeyLen() != 64 {
t.Errorf("PubKeyLen()=%d want 64", p.PubKeyLen())
}
if p.SigLen() != 48 {
t.Errorf("SigLen()=%d want 48 (3*128/8)", p.SigLen())
}
}
// Verify all three standard curve OIDs load successfully.
func TestBignParamsAllCurves(t *testing.T) {
for _, f := range []func() (*BignParams, error){
NewBignParams256v1,
NewBignParams384v1,
NewBignParams512v1,
} {
p, err := f()
if err != nil {
t.Errorf("params load failed: %v", err)
continue
}
p.Free()
}
}
// ────────────────────────────────────────────────────────────────────────────
// KAT: keypair generation (STB 34.101.45 table Г.1)
// ────────────────────────────────────────────────────────────────────────────
func TestBignKeypairGenKAT(t *testing.T) {
// The expected values from the STB standard are generated with a specific
// PRNG seed. We cannot reproduce them without the same seed, so we just
// check that generation produces keys of the right length and that
// PubkeyCalc is consistent.
p := newParams256(t)
defer p.Free()
priv, pub, err := BignKeypairGen(p)
if err != nil {
t.Fatal(err)
}
if len(priv) != 32 {
t.Errorf("privKey len=%d want 32", len(priv))
}
if len(pub) != 64 {
t.Errorf("pubKey len=%d want 64", len(pub))
}
// Derived public key must match.
pub2, err := BignPubkeyCalc(p, priv)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(pub, pub2) {
t.Fatal("PubkeyCalc result differs from keypair generation")
}
}
// Nil params must be rejected.
func TestBignKeypairGenNilParams(t *testing.T) {
if _, _, err := BignKeypairGen(nil); err == nil {
t.Error("nil params should fail")
}
if _, err := BignPubkeyCalc(nil, make([]byte, 32)); err == nil {
t.Error("nil params should fail")
}
}
// ────────────────────────────────────────────────────────────────────────────
// DH shared-secret symmetry
// ────────────────────────────────────────────────────────────────────────────
func TestBignDH(t *testing.T) {
p := newParams256(t)
defer p.Free()
privA, pubA, _ := BignKeypairGen(p)
privB, pubB, _ := BignKeypairGen(p)
sharedA, err := BignDH(p, privA, pubB, 32)
if err != nil {
t.Fatal(err)
}
sharedB, err := BignDH(p, privB, pubA, 32)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(sharedA, sharedB) {
t.Fatal("DH: Alice and Bob computed different shared secrets")
}
if bytes.Equal(sharedA, make([]byte, 32)) {
t.Fatal("DH: shared secret is all zeros")
}
}
// ────────────────────────────────────────────────────────────────────────────
// Sign / Verify round-trip (randomised)
// ────────────────────────────────────────────────────────────────────────────
func TestBignSignVerify(t *testing.T) {
p := newParams256(t)
defer p.Free()
oid, _ := BeltHashOID()
priv, pub, _ := BignKeypairGen(p)
msg := beltHBytes()[:48]
hash := beltHashOf(t, msg)
sig, err := BignSign(p, oid, hash, priv)
if err != nil {
t.Fatal(err)
}
if len(sig) != p.SigLen() {
t.Errorf("sig len=%d want %d", len(sig), p.SigLen())
}
if err := BignVerify(p, oid, hash, sig, pub); err != nil {
t.Fatalf("Verify: %v", err)
}
}
// Corrupted signature must fail.
func TestBignVerifyTamper(t *testing.T) {
p := newParams256(t)
defer p.Free()
oid, _ := BeltHashOID()
priv, pub, _ := BignKeypairGen(p)
hash := beltHashOf(t, []byte("test"))
sig, _ := BignSign(p, oid, hash, priv)
sig[0] ^= 0xFF
if err := BignVerify(p, oid, hash, sig, pub); err == nil {
t.Fatal("tampered sig must fail Verify")
}
}
// Corrupted public key must fail.
func TestBignVerifyBadPubKey(t *testing.T) {
p := newParams256(t)
defer p.Free()
oid, _ := BeltHashOID()
priv, pub, _ := BignKeypairGen(p)
hash := beltHashOf(t, []byte("test"))
sig, _ := BignSign(p, oid, hash, priv)
pub[0] ^= 0xFF
if err := BignVerify(p, oid, hash, sig, pub); err == nil {
t.Fatal("bad pubKey must fail Verify")
}
}
// ────────────────────────────────────────────────────────────────────────────
// Sign2 (deterministic) round-trip
// ────────────────────────────────────────────────────────────────────────────
func TestBignSign2Verify(t *testing.T) {
p := newParams256(t)
defer p.Free()
oid, _ := BeltHashOID()
priv, pub, _ := BignKeypairGen(p)
hash := beltHashOf(t, beltHBytes()[:13])
sig1, err := BignSign2(p, oid, hash, priv, nil)
if err != nil {
t.Fatal(err)
}
sig2, _ := BignSign2(p, oid, hash, priv, nil)
// Deterministic: same hash + key + no t → same signature.
if !bytes.Equal(sig1, sig2) {
t.Fatal("Sign2 with t=nil must be deterministic")
}
if err := BignVerify(p, oid, hash, sig1, pub); err != nil {
t.Fatalf("Verify Sign2 result: %v", err)
}
// With additional data t, signature changes.
extra := []byte("extra")
sig3, _ := BignSign2(p, oid, hash, priv, extra)
if bytes.Equal(sig1, sig3) {
t.Fatal("Sign2 with t!=nil produced same sig as t=nil")
}
if err := BignVerify(p, oid, hash, sig3, pub); err != nil {
t.Fatalf("Verify Sign2 with t: %v", err)
}
}
// ────────────────────────────────────────────────────────────────────────────
// Sign / Verify across all three curves
// ────────────────────────────────────────────────────────────────────────────
func TestBignSignAllCurves(t *testing.T) {
oid, _ := BeltHashOID()
for _, f := range []func() (*BignParams, error){
NewBignParams256v1,
NewBignParams384v1,
NewBignParams512v1,
} {
p, err := f()
if err != nil {
t.Errorf("params: %v", err)
continue
}
hashLen := p.PrivKeyLen()
hash := make([]byte, hashLen)
hash[0] = 0x42 // non-zero
priv, pub, err := BignKeypairGen(p)
if err != nil {
t.Errorf("l=%d keygen: %v", p.L(), err)
p.Free()
continue
}
sig, err := BignSign(p, oid, hash, priv)
if err != nil {
t.Errorf("l=%d Sign: %v", p.L(), err)
p.Free()
continue
}
if err := BignVerify(p, oid, hash, sig, pub); err != nil {
t.Errorf("l=%d Verify: %v", p.L(), err)
}
p.Free()
}
}
// ────────────────────────────────────────────────────────────────────────────
// Key wrap / unwrap round-trip
// ────────────────────────────────────────────────────────────────────────────
func TestBignKeyWrapUnwrap(t *testing.T) {
p := newParams256(t)
defer p.Free()
priv, pub, _ := BignKeypairGen(p)
H := beltHBytes()
innerKey := H[:18] // 18-byte key to exercise non-block-aligned length
token, err := BignKeyWrap(p, innerKey, nil, pub)
if err != nil {
t.Fatal(err)
}
// Token must be len(key) + PrivKeyLen + 16
wantLen := len(innerKey) + p.PrivKeyLen() + 16
if len(token) != wantLen {
t.Errorf("token len=%d want %d", len(token), wantLen)
}
recovered, err := BignKeyUnwrap(p, token, nil, priv)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(recovered, innerKey) {
t.Fatal("KeyUnwrap did not recover original key")
}
}
// Wrong private key must fail unwrap.
func TestBignKeyUnwrapWrongKey(t *testing.T) {
p := newParams256(t)
defer p.Free()
_, pub, _ := BignKeypairGen(p)
privWrong, _, _ := BignKeypairGen(p)
token, _ := BignKeyWrap(p, make([]byte, 32), nil, pub)
if _, err := BignKeyUnwrap(p, token, nil, privWrong); err == nil {
t.Fatal("Unwrap with wrong privKey must fail")
}
}
// ────────────────────────────────────────────────────────────────────────────
// Identity-based signature extract / sign / verify
// ────────────────────────────────────────────────────────────────────────────
func TestBignIdSignVerify(t *testing.T) {
p := newParams256(t)
defer p.Free()
oid, _ := BeltHashOID()
// Master keypair (trusted party).
masterPriv, masterPub, _ := BignKeypairGen(p)
// Sign the identity hash with the master key.
identity := []byte("user@example.com")
idHash := beltHashOf(t, identity)
idSig, err := BignSign(p, oid, idHash, masterPriv)
if err != nil {
t.Fatal(err)
}
// Extract the IBS keypair from the master signature.
idPriv, idPub, err := BignIdExtract(p, oid, idHash, idSig, masterPub)
if err != nil {
t.Fatalf("IdExtract: %v", err)
}
if len(idPriv) != p.PrivKeyLen() || len(idPub) != p.PubKeyLen() {
t.Fatal("IdExtract returned wrong-length keys")
}
// Sign a message with the IBS key.
msg := beltHBytes()[:48]
msgHash := beltHashOf(t, msg)
ibsSig, err := BignIdSign(p, oid, idHash, msgHash, idPriv)
if err != nil {
t.Fatalf("IdSign: %v", err)
}
// Verify the IBS signature.
if err := BignIdVerify(p, oid, idHash, msgHash, ibsSig, idPub, masterPub); err != nil {
t.Fatalf("IdVerify: %v", err)
}
// Tamper with the IBS signature.
ibsSig[0] ^= 0xFF
if err := BignIdVerify(p, oid, idHash, msgHash, ibsSig, idPub, masterPub); err == nil {
t.Fatal("tampered IBS sig must fail")
}
ibsSig[0] ^= 0xFF
// Tamper with the IBS pubkey.
idPub[0] ^= 0xFF
if err := BignIdVerify(p, oid, idHash, msgHash, ibsSig, idPub, masterPub); err == nil {
t.Fatal("bad IBS pubKey must fail")
}
}
// IdSign2 must be deterministic with t=nil.
func TestBignIdSign2Deterministic(t *testing.T) {
p := newParams256(t)
defer p.Free()
oid, _ := BeltHashOID()
masterPriv, masterPub, _ := BignKeypairGen(p)
idHash := beltHashOf(t, []byte("alice"))
idSig, _ := BignSign(p, oid, idHash, masterPriv)
idPriv, idPub, _ := BignIdExtract(p, oid, idHash, idSig, masterPub)
msgHash := beltHashOf(t, []byte("hello"))
s1, _ := BignIdSign2(p, oid, idHash, msgHash, idPriv, nil)
s2, _ := BignIdSign2(p, oid, idHash, msgHash, idPriv, nil)
if !bytes.Equal(s1, s2) {
t.Fatal("IdSign2 with t=nil must be deterministic")
}
if err := BignIdVerify(p, oid, idHash, msgHash, s1, idPub, masterPub); err != nil {
t.Fatalf("IdVerify Sign2: %v", err)
}
}
// ────────────────────────────────────────────────────────────────────────────
// Argument validation
// ────────────────────────────────────────────────────────────────────────────
func TestBignArgValidation(t *testing.T) {
p := newParams256(t)
defer p.Free()
oid, _ := BeltHashOID()
dummy32 := make([]byte, 32)
dummy64 := make([]byte, 64)
dummy48 := make([]byte, 48)
// nil params
if err := BignVerify(nil, oid, dummy32, dummy48, dummy64); err == nil {
t.Error("Verify nil params should fail")
}
// empty OID
if err := BignVerify(p, nil, dummy32, dummy48, dummy64); err == nil {
t.Error("Verify empty OID should fail")
}
// short hash
if _, err := BignSign(p, oid, make([]byte, 1), dummy32); err == nil {
t.Error("Sign short hash should fail")
}
// short privKey
if _, err := BignSign(p, oid, dummy32, make([]byte, 1)); err == nil {
t.Error("Sign short privKey should fail")
}
}