forked from dgryski/dkeyczar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_test.go
537 lines (417 loc) · 13 KB
/
key_test.go
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package dkeyczar
import (
"bytes"
"testing"
"time"
)
const INPUT = "This is some test data"
const TESTDATA = "testdata/existing-data/cpp/"
func testEncrypt(t *testing.T, keytype string, f KeyReader) {
kz, err := NewEncrypter(f)
if err != nil {
t.Fatal("failed to load key for keytype " + keytype + ": " + err.Error())
}
c, err := kz.Encrypt([]byte(INPUT))
if err != nil {
t.Fatal("failed to encrypt key for keytype " + keytype + ": " + err.Error())
}
crypter, err := NewCrypter(f)
if err != nil {
t.Fatal("failed to create crypter with keyreader for keytype " + keytype + ": " + err.Error())
}
p, err := crypter.Decrypt(c)
if err != nil {
t.Fatal("failed to decrypt keytype " + keytype + ": " + err.Error())
}
if string(p) != INPUT {
t.Error(keytype + " encryption failed")
}
}
func testEncryptDecrypt(t *testing.T, keytype string, f KeyReader) {
kz, err := NewCrypter(f)
if err != nil {
t.Fatal("failed to create crypter keytype " + keytype + ": " + err.Error())
}
c, err := kz.Encrypt([]byte(INPUT))
if err != nil {
t.Fatal("failed to encrypt key for keytype " + keytype + ": " + err.Error())
}
p, err := kz.Decrypt(c)
if err != nil {
t.Fatal("failed to decrypt keytype " + keytype + ": " + err.Error())
}
if string(p) != INPUT {
t.Error(keytype + " decrypt(encrypt(p)) != p")
}
}
func testVerify(t *testing.T, keytype string, f KeyReader) {
kz, err := NewVerifier(f)
if err != nil {
t.Fatal("failed to create verifier for keytype " + keytype + ": " + err.Error())
}
for _, out := range []string{"1.out", "2.out"} {
c, err := slurp(TESTDATA + keytype + "/" + out)
if err != nil {
t.Fatal("failed to load " + out + " for keytype " + keytype + ": " + err.Error())
}
goodsig, _ := kz.Verify([]byte(INPUT), c)
if err != nil {
t.Fatal("failed to verify " + out + " for keytype " + keytype + ": " + err.Error())
}
if !goodsig {
t.Error("failed signature for " + keytype + "/" + out)
}
}
}
func testSignVerify(t *testing.T, keytype string, f KeyReader) {
kz, err := NewSigner(f)
if err != nil {
t.Fatal("failed to create signer for keytype " + keytype + ": " + err.Error())
}
s, err := kz.Sign([]byte(INPUT))
if err != nil {
t.Fatal("failed to sign for keytype " + keytype + ": " + err.Error())
}
kv, err := NewVerifier(f)
if err != nil {
t.Fatal("failed to create verifier for keytype " + keytype + ": " + err.Error())
}
b, _ := kv.Verify([]byte(INPUT), s)
if err != nil {
t.Fatal("failed to verify for keytype " + keytype + ": " + err.Error())
}
if !b {
t.Error(keytype + " verify failed")
}
s, err = kz.AttachedSign([]byte(INPUT), []byte{0, 1, 2, 3, 4, 5, 6, 7})
if err != nil {
t.Fatal("failed to attachedsign for keytype " + keytype + ": " + err.Error())
}
msg, _ := kv.AttachedVerify(s, []byte{0, 1, 2, 3, 4, 5, 6, 7})
if err != nil {
t.Fatal("failed to attachedverify for keytype " + keytype + ": " + err.Error())
}
if msg == nil || !bytes.Equal(msg, []byte(INPUT)) {
t.Error(keytype + " attachedverify failed")
}
s, err = kz.AttachedSign([]byte(INPUT), nil)
if err != nil {
t.Fatal("failed to attachedsign (no nonce) for keytype " + keytype + ": " + err.Error())
}
msg, _ = kv.AttachedVerify(s, nil)
if err != nil {
t.Fatal("failed to attachedverify (no nonce) for keytype " + keytype + ": " + err.Error())
}
if msg == nil || !bytes.Equal(msg, []byte(INPUT)) {
t.Error(keytype + " attachedverify (no nonce) failed")
}
futureMillis := int64(2*time.Minute) + time.Now().UnixNano()/int64(time.Millisecond)
s, err = kz.TimeoutSign([]byte(INPUT), futureMillis)
if err != nil {
t.Fatal("failed to timeoutSign(future) for keytype " + keytype + ": " + err.Error())
}
b, err = kv.TimeoutVerify([]byte(INPUT), s)
if err != nil {
t.Fatal("failed to timeoutverify(future) for keytype " + keytype + ": " + err.Error())
}
if !b {
t.Error(keytype + " timeoutverify(future) failed")
}
pastMillis := int64(-2*time.Minute) + time.Now().UnixNano()/int64(time.Millisecond)
s, err = kz.TimeoutSign([]byte(INPUT), pastMillis)
if err != nil {
t.Fatal("failed to timeoutSign(past) for keytype " + keytype + ": " + err.Error())
}
b, err = kv.TimeoutVerify([]byte(INPUT), s)
if err != nil {
t.Fatal("failed to timeoutverify(past) for keytype " + keytype + ": " + err.Error())
}
if b {
t.Error(keytype + " timeoutverify(past) didn't fail!")
}
s, err = kz.UnversionedSign([]byte(INPUT))
if err != nil {
t.Fatal("failed to unversionedsign for keytype " + keytype + ": " + err.Error())
}
b, err = kv.UnversionedVerify([]byte(INPUT), s)
if err != nil {
t.Fatal("failed to unversionedverify for keytype " + keytype + ": " + err.Error())
}
if !b {
t.Error(keytype + " unversionedverify failed")
}
}
func testDecrypt(t *testing.T, keytype string, f KeyReader) {
kz, err := NewCrypter(f)
if err != nil {
t.Fatal("failed to create crypter for keytype " + keytype + ": " + err.Error())
}
for _, out := range []string{"1.out", "2.out"} {
c, err := slurp(TESTDATA + keytype + "/" + out)
if err != nil {
t.Fatal("failed slurp " + out + " for keytype " + keytype + ": " + err.Error())
}
p, err := kz.Decrypt(c)
if err != nil {
t.Fatal("failed decrypt for keytype " + keytype + ": " + err.Error())
}
if string(p) != INPUT {
t.Error("decrypt failed for " + keytype + "/" + out)
}
}
}
func TestAESEncrypt(t *testing.T) {
keytype := "aes"
testEncrypt(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestAESEncryptDecrypt(t *testing.T) {
keytype := "aes"
testEncryptDecrypt(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestAESDecrypt(t *testing.T) {
keytype := "aes"
testDecrypt(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestHMACVerify(t *testing.T) {
keytype := "hmac"
testVerify(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestHMACSign(t *testing.T) {
keytype := "hmac"
testSignVerify(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestDSASign(t *testing.T) {
keytype := "dsa"
testSignVerify(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestDSAVerify(t *testing.T) {
keytype := "dsa"
testVerify(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestDSAPublicVerifyPublic(t *testing.T) {
keytype := "dsa"
testVerify(t, keytype, NewFileReader(TESTDATA+keytype+".public"))
}
func TestRSAsignSign(t *testing.T) {
keytype := "rsa-sign"
testSignVerify(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestRSAsignVerify(t *testing.T) {
keytype := "rsa-sign"
testVerify(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestRSAsignPublicVerifyPublic(t *testing.T) {
keytype := "rsa-sign"
testVerify(t, keytype, NewFileReader(TESTDATA+keytype+".public"))
}
func TestRSAEncrypt(t *testing.T) {
keytype := "rsa"
testEncrypt(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestRSAEncryptDecrypt(t *testing.T) {
keytype := "rsa"
testEncryptDecrypt(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestRSADecrypt(t *testing.T) {
keytype := "rsa"
testDecrypt(t, keytype, NewFileReader(TESTDATA+keytype))
}
func TestRSAPEMImportDecrypt(t *testing.T) {
r, err := ImportRSAKeyFromPEMForCrypt(TESTDATA + "rsa_pem/rsa_priv.pem")
if err != nil {
t.Fatal("failed to create import for rsa_pem")
}
testEncryptDecrypt(t, "rsa pem import", r)
}
func TestRSAPEMImportSign(t *testing.T) {
r, err := ImportRSAKeyFromPEMForSigning(TESTDATA + "rsa_pem/rsa_priv.pem")
if err != nil {
t.Fatal("failed to create import for rsa_pem")
}
testSignVerify(t, "rsa pem import", r)
}
// commented until I get around to pointing this at a cert that exists elsewhere than on my machine
/*
func TestRSACertImport(t *testing.T) {
r, err := ImportRSAPublicKeyFromCertificateForVerify("thawte.crt")
fmt.Println("err=", err)
kz, err := NewVerifier(r)
fmt.Println("err=", err)
fmt.Println("kz=", kz)
}
*/
func TestGeneratedAESEncryptDecrypt(t *testing.T) {
k, _ := generateAESKey(0)
r := newImportedAESKeyReader(k)
testEncryptDecrypt(t, "aes generated", r)
}
// too slow
/*
func TestGeneratedRSA(t *testing.T) {
t.Log("generating rsa key...")
k := generateRSAKey()
r := newImportedRSAPrivateKeyReader(&k.key, P_DECRYPT_AND_ENCRYPT)
testEncryptDecrypt(t, "rsa generated", r)
r = newImportedRSAPrivateKeyReader(&k.key, P_SIGN_AND_VERIFY)
testSignVerify(t, "rsa generated", r)
}
*/
func TestGeneratedDSA(t *testing.T) {
k, _ := generateDSAKey(0)
r := newImportedDSAPrivateKeyReader(&k.key)
testSignVerify(t, "dsa generated", r)
}
func TestEncryptedReader(t *testing.T) {
f := NewFileReader(TESTDATA + "aes")
cr, err := NewCrypter(f)
if err != nil {
t.Fatal("failed to create crypter for keytype aes")
}
er := NewEncryptedReader(NewFileReader(TESTDATA+"aes-crypted"), cr)
testDecrypt(t, "aes-crypted", er)
}
func TestPBEReader(t *testing.T) {
f := NewFileReader(TESTDATA + "pbe_json")
er := NewPBEReader(f, []byte("cartman"))
testEncryptDecrypt(t, "pbe_json", er)
}
func TestSessionEncryptDecrypt(t *testing.T) {
f := NewFileReader(TESTDATA + "rsa")
kz, err := NewCrypter(f)
if err != nil {
t.Fatal("failed to create crypter with rsa: " + err.Error())
}
sess1, keys, err := NewSessionEncrypter(kz)
if err != nil {
t.Fatal("failed to create session encrypter: " + err.Error())
}
c, err := sess1.Encrypt([]byte(INPUT))
if err != nil {
t.Fatal("failed to session encrypt: " + err.Error())
}
sess2, err := NewSessionDecrypter(kz, keys)
if err != nil {
t.Fatal("failed to create session decrypter: " + err.Error())
}
p, err := sess2.Decrypt(c)
if err != nil {
t.Fatal("failed to session decrypt: " + err.Error())
}
if string(p) != INPUT {
t.Error("session decrypt(encrypt(p)) != p")
}
}
func TestEncryptDecryptCompressed(t *testing.T) {
f := NewFileReader(TESTDATA + "aes")
longinput := INPUT + INPUT + INPUT + INPUT + INPUT
longinput = longinput + INPUT + INPUT + INPUT + INPUT + INPUT
longinput = longinput + INPUT + INPUT + INPUT + INPUT + INPUT
longinput = longinput + INPUT + INPUT + INPUT + INPUT + INPUT
longinput = longinput + INPUT + INPUT + INPUT + INPUT + INPUT
longinput = longinput + INPUT + INPUT + INPUT + INPUT + INPUT
kz, err := NewCrypter(f)
if err != nil {
t.Fatal("failed to create crypter for aes: " + err.Error())
}
c, err := kz.Encrypt([]byte(longinput))
if err != nil {
t.Fatal("failed to encrypt: " + err.Error())
}
uncompressedLen := len(c)
p, err := kz.Decrypt(c)
if err != nil {
t.Fatal("failed to decrypt: " + err.Error())
}
if string(p) != longinput {
t.Error("aes decrypt(encrypt(p)) != p")
}
for _, compression := range []struct {
kc KeyczarCompression
ctype string
}{{GZIP, "gzip"}, {ZLIB, "zlib"}} {
kz.SetCompression(compression.kc)
c, err = kz.Encrypt([]byte(longinput))
if err != nil {
t.Fatal("failed to encrypt with compression " + compression.ctype)
}
compressedLen := len(c)
p, err = kz.Decrypt(c)
if err != nil {
t.Fatal("failed to decrypt with compression " + compression.ctype)
}
if string(p) != longinput {
t.Error(compression.ctype + " raw decrypt(encrypt(p)) != p")
}
if compressedLen >= uncompressedLen {
t.Error(compression.ctype + " failed to compress")
}
}
}
func TestSignVerifyBase64(t *testing.T) {
f := NewFileReader(TESTDATA + "dsa")
kz, err := NewSigner(f)
if err != nil {
t.Fatal("failed to create signer: " + err.Error())
}
s, err := kz.Sign([]byte(INPUT))
if err != nil {
t.Fatal("failed to sign: " + err.Error())
}
if s[0] != 'A' { // first byte will 0, so first byte of base64 will be 'A'
t.Error("bad version byte for base64 signature")
}
kz.SetEncoding(NO_ENCODING)
s, err = kz.Sign([]byte(INPUT))
if err != nil {
t.Fatal("failed to sign with no encoding: " + err.Error())
}
if s[0] != 0 {
t.Error("bad version byte for raw signature")
}
kv, err := NewVerifier(f)
if err != nil {
t.Fatal("failed to create verifier")
}
kv.SetEncoding(NO_ENCODING)
b, err := kv.Verify([]byte(INPUT), s)
if err != nil {
t.Fatal("failed to verify with no encoding: " + err.Error())
}
if !b {
t.Error("dsa raw encoding verify failed")
}
}
var pkcs5padtests = []struct {
s []byte
pad int
r []byte
}{
{[]byte{0, 0, 0, 0, 0, 0, 0, 0}, 8, []byte{0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8}},
{[]byte{0, 0, 0, 0, 0, 0, 0}, 8, []byte{0, 0, 0, 0, 0, 0, 0, 1}},
{[]byte{0, 0, 0, 0, 0, 0}, 8, []byte{0, 0, 0, 0, 0, 0, 2, 2}},
{[]byte{0, 0, 0, 0, 0}, 8, []byte{0, 0, 0, 0, 0, 3, 3, 3}},
{[]byte{0, 0, 0, 0}, 8, []byte{0, 0, 0, 0, 4, 4, 4, 4}},
}
func TestPKCS5Pad(t *testing.T) {
for _, pkcs := range pkcs5padtests {
unpad := make([]byte, len(pkcs.s))
copy(unpad, pkcs.s)
r := pkcs5pad(pkcs.s, pkcs.pad)
if bytes.Compare(pkcs.r, r) != 0 {
t.Error("pkcs5pad: got: ", r, "expected: ", pkcs.r)
}
u := pkcs5unpad(r)
if bytes.Compare(unpad, u) != 0 {
t.Error("pkcs5unpad: got: ", u, "expected: ", unpad)
}
}
}
func TestLenPrefixPack(t *testing.T) {
b := lenPrefixPack([]byte{4, 5, 6, 2, 1}, []byte{1, 4, 2, 8, 5, 7}, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{1})
arrays := lenPrefixUnpack(b)
// FIXME: make this test more complete
if len(arrays[3]) != 1 || arrays[3][0] != 1 {
t.Error("unpack error")
}
}
// FIXME: DecodeWeb64String / EncodeWeb64String