-
Notifications
You must be signed in to change notification settings - Fork 4
/
crypto_test.go
1430 lines (1396 loc) · 35.4 KB
/
crypto_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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package httpsignatures
import (
"encoding/base64"
"encoding/hex"
"fmt"
"testing"
)
const testErrCryptoType = "*httpsignatures.ErrCrypto"
const testHashData = "hello world"
type argsVerify struct {
alg SignatureHashAlgorithm
sig string
data []byte
secret Secret
}
func verify(args argsVerify) (bool, error) {
sig, _ := base64.StdEncoding.DecodeString(args.sig)
err := args.alg.Verify(args.secret, args.data, sig)
return err == nil, err
}
func TestHashAlgorithm(t *testing.T) {
tests := []struct {
name string
arg DigestHashAlgorithm
want string
}{
{
name: "MD5 OK",
arg: Md5{},
want: "MD5",
},
{
name: "SHA256 OK",
arg: Sha256{},
want: "SHA-256",
},
{
name: "SHA512 OK",
arg: Sha512{},
want: "SHA-512",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.arg.Algorithm() != tt.want {
t.Errorf("algorithm not match. Got = %s, want = %s", tt.arg.Algorithm(), tt.want)
}
})
}
}
func TestHashAlgorithmCreate(t *testing.T) {
type args struct {
alg DigestHashAlgorithm
data []byte
}
tests := []struct {
name string
args args
want string
wantErrType string
wantErrMsg string
}{
{
name: "MD5 create ok",
args: args{
alg: Md5{},
data: []byte(testHashData),
},
want: "5eb63bbbe01eeed093cb22bb8f5acdc3",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "SHA256 create ok",
args: args{
alg: Sha256{},
data: []byte(testHashData),
},
want: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "SHA512 create ok",
args: args{
alg: Sha512{},
data: []byte(testHashData),
},
want: "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd8" +
"30e81f605dcf7dc5542e93ae9cd76f",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.args.alg.Create(tt.args.data)
digest := hex.EncodeToString(got)
assert(t, digest, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestHashAlgorithmVerify(t *testing.T) {
type args struct {
alg DigestHashAlgorithm
digest string
data []byte
}
tests := []struct {
name string
args args
want bool
wantErrType string
wantErrMsg string
}{
{
name: "MD5 verify ok",
args: args{
alg: Md5{},
digest: "5eb63bbbe01eeed093cb22bb8f5acdc3",
data: []byte(testHashData),
},
want: true,
},
{
name: "MD5 wrong hash",
args: args{
alg: Md5{},
digest: "5eb",
data: []byte(testHashData),
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: wrong hash",
},
{
name: "Sha256 verify ok",
args: args{
alg: Sha256{},
digest: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
data: []byte(testHashData),
},
want: true,
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "Sha256 wrong hash",
args: args{
alg: Sha256{},
digest: "b94",
data: []byte(testHashData),
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: wrong hash",
},
{
name: "Sha512 verify ok",
args: args{
alg: Sha512{},
digest: "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45" +
"b0cfd830e81f605dcf7dc5542e93ae9cd76f",
data: []byte(testHashData),
},
want: true,
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "Sha512 wrong hash",
args: args{
alg: Sha512{},
digest: "309",
data: []byte(testHashData),
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: wrong hash",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b, _ := hex.DecodeString(tt.args.digest)
err := tt.args.alg.Verify(tt.args.data, b)
got := err == nil
assert(t, got, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestHmacAlgorithm(t *testing.T) {
tests := []struct {
name string
arg SignatureHashAlgorithm
want string
}{
{
name: "HMAC-SHA256 OK",
arg: HmacSha256{},
want: "HMAC-SHA256",
},
{
name: "HMAC-SHA512 OK",
arg: HmacSha512{},
want: "HMAC-SHA512",
},
{
name: "RSA-SHA256 OK",
arg: RsaSha256{},
want: "RSA-SHA256",
},
{
name: "RSA-SHA512 OK",
arg: RsaSha512{},
want: "RSA-SHA512",
},
{
name: "RSASSA-PSS-SHA256 OK",
arg: RsaSsaPssSha256{},
want: "RSASSA-PSS-SHA256",
},
{
name: "RSASSA-PSS-SHA512 OK",
arg: RsaSsaPssSha512{},
want: "RSASSA-PSS-SHA512",
},
{
name: "ECDSA-SHA256 OK",
arg: EcdsaSha256{},
want: "ECDSA-SHA256",
},
{
name: "ECDSA-SHA512 OK",
arg: EcdsaSha512{},
want: "ECDSA-SHA512",
},
{
name: "ED25519 OK",
arg: ED25519{},
want: "ED25519",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.arg.Algorithm() != tt.want {
t.Errorf("algorithm not match. Got = %s, want = %s", tt.arg.Algorithm(), tt.want)
}
})
}
}
func TestSignatureHashHmacAlgorithmCreate(t *testing.T) {
type args struct {
alg SignatureHashAlgorithm
data []byte
secret Secret
}
tests := []struct {
name string
args args
want string
wantErrType string
wantErrMsg string
}{
{
name: "HMAC-SHA256 create ok",
args: args{
alg: HmacSha256{},
data: []byte("(request-target): post /foo?param=value&pet=dog"),
secret: Secret{
PrivateKey: "secret",
Algorithm: algHmacSha256,
},
},
want: "7lksEgztUSEk34sJ8vGQpE0i+UK+ZexCQ0L8HpHBBJY=",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "HMAC-SHA256 no private key found",
args: args{
alg: HmacSha256{},
data: []byte{},
secret: Secret{},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: no private key found",
},
{
name: "HMAC-SHA512 create ok",
args: args{
alg: HmacSha512{},
data: []byte("(request-target): post /foo?param=value&pet=dog"),
secret: Secret{
PrivateKey: "secret",
Algorithm: algHmacSha512,
},
},
want: "xhrfZlhd8heV7O4w1nPbNRYdWSc2Qg8RuruZ5jDDHbVzSgd4NQOePJWN5xIKz74U/HhlLe138G8VLcH5atTZTg==",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "HMAC-SHA512 no private key found",
args: args{
alg: HmacSha512{},
data: []byte{},
secret: Secret{},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: no private key found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.args.alg.Create(tt.args.secret, tt.args.data)
sig := base64.StdEncoding.EncodeToString(got)
assert(t, sig, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestSignatureHashRsaAlgorithmCreate(t *testing.T) {
type args struct {
alg SignatureHashAlgorithm
data []byte
secret Secret
}
tests := []struct {
name string
args args
want string
wantErrType string
wantErrMsg string
}{
{
name: "RSA-SHA256 create ok",
args: args{
alg: RsaSha256{},
data: []byte(
"(request-target): post /foo?param=value&pet=dog\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key1",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: algRsaSha256,
},
},
want: "qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdK" +
"FYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0=",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "RSA-SHA256 no private key found",
args: args{
alg: RsaSha256{},
data: []byte{},
secret: Secret{},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: no private key found",
},
{
name: "RSA-SHA256 unsupported key type",
args: args{
alg: RsaSha256{},
data: []byte{},
secret: Secret{
PrivateKey: `-----BEGIN SSH PRIVATE KEY-----
-----END SSH PRIVATE KEY-----`,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: unsupported private key type SSH PRIVATE KEY",
},
{
name: "RSA-SHA256 error ParsePKCS1PrivateKey",
args: args{
alg: RsaSha256{},
data: []byte{},
secret: Secret{
PrivateKey: `-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF
-----END RSA PRIVATE KEY-----`,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: unsupported private key type RSA PRIVATE KEY",
},
{
name: "RSA-SHA512 create ok",
args: args{
alg: RsaSha512{},
data: []byte(
"(request-target): post /foo\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key1",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: algRsaSha512,
},
},
want: "iz8UWbpy9oK5R2sdD5fIj8VphjkGTeMZ2YKGOiW77yBYS8TB5R/T3Knet4DlnvjAqZrWBDbN75d8/Ttf/bIMoZO0NFr60SBngB" +
"zya6xnVIQ+0zoidBXpNjlttV2BDc44mrLvemk8Ar5NIiySNvKvKl7UNJxgKfT5UtGKDdry8qU=",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "RSA-DUMMY unsupported algorithm type",
args: args{
alg: RsaDummy{},
data: nil,
secret: Secret{
KeyID: "key1",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: testRsaDummyName,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: unsupported algorithm type RSA-DUMMY",
},
{
name: "RSA-SHA256 create ok",
args: args{
alg: RsaSha256{},
data: []byte(
"(request-target): post /foo?param=value&pet=dog\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key1",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: algRsaSha256,
},
},
want: "qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdK" +
"FYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0=",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.args.alg.Create(tt.args.secret, tt.args.data)
sig := base64.StdEncoding.EncodeToString(got)
assert(t, sig, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestSignatureHashRsaSsaPssAlgorithmCreate(t *testing.T) {
type args struct {
alg SignatureHashAlgorithm
data []byte
secret Secret
}
tests := []struct {
name string
args args
want string
wantErrType string
wantErrMsg string
}{
{
name: "RSASSA-PSS-SHA256 create ok",
args: args{
alg: RsaSsaPssSha256{},
data: []byte(
"(request-target): post /foo\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key1",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: algRsaSsaPssSha256,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "RSASSA-PSS-SHA512 create ok",
args: args{
alg: RsaSsaPssSha512{},
data: []byte(
"(request-target): post /foo\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key2",
PrivateKey: testRsaPrivateKey2048,
PublicKey: testRsaPublicKey2048,
Algorithm: algRsaSsaPssSha512,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.args.alg.Create(tt.args.secret, tt.args.data)
sig := base64.StdEncoding.EncodeToString(got)
// RSASSA-PSS Algorithm generate always different signature
var length = 0
switch tt.args.alg.Algorithm() {
case algRsaSsaPssSha256:
length = 172
case algRsaSsaPssSha512:
length = 344
}
if len(sig) < length {
t.Errorf(tt.name+"\ngot = %v, expected length = %d symbols, but got = %d", sig, length, len(sig))
}
tt.want = sig
assert(t, sig, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestSignatureHashEcdsaAlgorithmCreate(t *testing.T) {
type args struct {
alg SignatureHashAlgorithm
data []byte
secret Secret
}
tests := []struct {
name string
args args
want string
wantErrType string
wantErrMsg string
}{
{
name: "ECDSA-SHA256 create ok",
args: args{
alg: EcdsaSha256{},
data: []byte(
"(request-target): post /foo\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key1",
PrivateKey: testECDSAPrivateKey,
PublicKey: testECDSAPublicKey,
Algorithm: algEcdsaSha256,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "ECDSA-SHA256 no private key found",
args: args{
alg: EcdsaSha256{},
data: []byte{},
secret: Secret{},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: no private key found",
},
{
name: "ECDSA-SHA256 unsupported key type",
args: args{
alg: EcdsaSha256{},
data: []byte{},
secret: Secret{
PrivateKey: `-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----`,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: unsupported private key type RSA PRIVATE KEY",
},
{
name: "ECDSA-SHA256 error ParseECPrivateKey",
args: args{
alg: EcdsaSha256{},
data: []byte{},
secret: Secret{
PrivateKey: `-----BEGIN EC PRIVATE KEY-----
MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF
-----END EC PRIVATE KEY-----`,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: unsupported private key type EC PRIVATE KEY",
},
{
name: "ECDSA-DUMMY unsupported algorithm type",
args: args{
alg: EcdsaDummy{},
data: nil,
secret: Secret{
KeyID: "key1",
PrivateKey: testECDSAPrivateKey,
PublicKey: testECDSAPublicKey,
Algorithm: testEcdsaDummyName,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: unsupported algorithm type ECDSA-DUMMY",
},
{
name: "ECDSA-SHA256 unknown private key type",
args: args{
alg: EcdsaSha256{},
data: []byte{},
secret: Secret{
KeyID: "key1",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: algEcdsaSha256,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: unknown private key type",
},
{
name: "ECDSA-SHA512 create ok",
args: args{
alg: EcdsaSha512{},
data: []byte(
"(request-target): post /foo\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key1",
PrivateKey: testECDSAPrivateKey,
PublicKey: testECDSAPublicKey,
Algorithm: algEcdsaSha512,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.args.alg.Create(tt.args.secret, tt.args.data)
sig := base64.StdEncoding.EncodeToString(got)
// Ecdsa Algorithm generate always different signature
if err == nil && len(tt.wantErrMsg) == 0 {
if len(sig) <= 0 {
t.Errorf(tt.name+"\ngot = %v, expected length <= 0 symbols", sig)
}
tt.want = sig
}
assert(t, sig, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestSignatureHashED25519AlgorithmCreate(t *testing.T) {
type args struct {
alg SignatureHashAlgorithm
data []byte
secret Secret
}
tests := []struct {
name string
args args
want string
wantErrType string
wantErrMsg string
}{
{
name: "ED25519 create ok",
args: args{
alg: ED25519{},
data: []byte(
"(request-target): post /foo\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key1",
PrivateKey: testED25519PrivateKey,
PublicKey: testED25519PublicKey,
Algorithm: algED25519,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "ED25519 no private key found",
args: args{
alg: ED25519{},
data: []byte{},
secret: Secret{},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: no private key found",
},
{
name: "ED25519 error unmarshal private key",
args: args{
alg: ED25519{},
data: []byte{},
secret: Secret{
PrivateKey: `-----BEGIN ED25519 PRIVATE KEY-----
OMke+7A8ArZBjbrDyYWtYN8qxGaDmrkziIl1ogfA2P597XhmBSCXqN6EFouVx79mfggiZGvhl0Z4DxFnNolE1w==
-----END ED25519 PRIVATE KEY-----`,
},
},
want: "",
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: error unmarshal private key: asn1: structure error: length too large",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.args.alg.Create(tt.args.secret, tt.args.data)
sig := base64.StdEncoding.EncodeToString(got)
fmt.Println(sig)
// Ecdsa Algorithm generate always different signature
if err == nil && len(tt.wantErrMsg) == 0 {
if len(sig) <= 0 {
t.Errorf(tt.name+"\ngot = %v, expected length <= 0 symbols", sig)
}
tt.want = sig
}
assert(t, sig, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestSignatureHashHmacAlgorithmVerify(t *testing.T) {
tests := []struct {
name string
args argsVerify
want bool
wantErrType string
wantErrMsg string
}{
{
name: "HMAC-SHA256 verify ok",
args: argsVerify{
alg: HmacSha256{},
sig: "7lksEgztUSEk34sJ8vGQpE0i+UK+ZexCQ0L8HpHBBJY=",
data: []byte("(request-target): post /foo?param=value&pet=dog"),
secret: Secret{
PrivateKey: "secret",
Algorithm: algHmacSha256,
},
},
want: true,
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "HMAC-SHA256 wrong signature",
args: argsVerify{
alg: HmacSha256{},
sig: "MTIz",
data: []byte("xx"),
secret: Secret{
PrivateKey: "secret",
Algorithm: algHmacSha256,
},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: wrong signature",
},
{
name: "HMAC-SHA256 no private key found",
args: argsVerify{
alg: HmacSha256{},
sig: "",
data: []byte{},
secret: Secret{},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: no private key found",
},
{
name: "HMAC-SHA512 verify ok",
args: argsVerify{
alg: HmacSha512{},
sig: "xhrfZlhd8heV7O4w1nPbNRYdWSc2Qg8RuruZ5jDDHbVzSgd4NQOePJWN5xIKz74U/HhlLe138G8VLcH5atTZTg==",
data: []byte("(request-target): post /foo?param=value&pet=dog"),
secret: Secret{
PrivateKey: "secret",
Algorithm: algHmacSha512,
},
},
want: true,
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "HMAC-SHA512 wrong signature",
args: argsVerify{
alg: HmacSha512{},
sig: "MTIz",
data: []byte("xx"),
secret: Secret{
PrivateKey: "secret",
Algorithm: algHmacSha512,
},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: wrong signature",
},
{
name: "HMAC-SHA512 no private key found",
args: argsVerify{
alg: HmacSha512{},
sig: "",
data: []byte{},
secret: Secret{},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: no private key found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := verify(tt.args)
assert(t, got, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestSignatureHashRsaAlgorithmVerify(t *testing.T) {
tests := []struct {
name string
args argsVerify
want bool
wantErrType string
wantErrMsg string
}{
{
name: "RSA-SHA256 verify ok",
args: argsVerify{
alg: RsaSha256{},
sig: "qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8V" +
"fEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0=",
data: []byte(
"(request-target): post /foo?param=value&pet=dog\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key2",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: algRsaSha256,
},
},
want: true,
wantErrType: testErrCryptoType,
wantErrMsg: "",
},
{
name: "RSA-SHA256 wrong signature",
args: argsVerify{
alg: RsaSha256{},
sig: "MTIz",
data: []byte("test"),
secret: Secret{
KeyID: "key3",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: algRsaSha256,
},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: error verify signature: crypto/rsa: verification error",
},
{
name: "RSA-SHA256 no public key found",
args: argsVerify{
alg: RsaSha256{},
sig: "",
data: []byte{},
secret: Secret{},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: no public key found",
},
{
name: "RSA-SHA256 unsupported key type",
args: argsVerify{
alg: RsaSha256{},
data: []byte{},
secret: Secret{
PublicKey: `-----BEGIN NO PUBLIC KEY-----
-----END NO PUBLIC KEY-----`,
},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: error ParsePKIXPublicKey: asn1: syntax error: sequence truncated",
},
{
name: "RSA-SHA256 error ParsePKIXPublicKey",
args: argsVerify{
alg: RsaSha256{},
data: []byte{},
secret: Secret{
PublicKey: `-----BEGIN PUBLIC KEY-----
MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF
-----END PUBLIC KEY-----`,
},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: error ParsePKIXPublicKey: asn1: syntax error: data truncated",
},
{
name: "RSA-SHA256 unknown type of public key",
args: argsVerify{
alg: RsaSha256{},
data: []byte{},
secret: Secret{
PublicKey: `-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE8xOUetsCa8EfOlDEBAfREhJqspDo
yEh6Szz2in47Tv5n52m9dLYyPCbqZkOB5nTSqtscpkQD/HpykCggvx09iQ==
-----END PUBLIC KEY-----`,
},
},
want: false,
wantErrType: testErrCryptoType,
wantErrMsg: "ErrCrypto: unknown type of public key",
},
{
name: "RSA-SHA512 verify ok",
args: argsVerify{
alg: RsaSha512{},
sig: "iz8UWbpy9oK5R2sdD5fIj8VphjkGTeMZ2YKGOiW77yBYS8TB5R/T3Knet4DlnvjAqZrWBDbN75d8/Ttf/bIMoZO0NFr60SB" +
"ngBzya6xnVIQ+0zoidBXpNjlttV2BDc44mrLvemk8Ar5NIiySNvKvKl7UNJxgKfT5UtGKDdry8qU=",
data: []byte(
"(request-target): post /foo\n" +
"host: " + testHostExample + "\n" +
"date: " + testDateExample,
),
secret: Secret{
KeyID: "key1",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: algRsaSha512,
},
},
want: true,