Debian Codegraph @

algorithms_test.go

raw symbols SCIP
1 package httpsig
2
3 import (
4 "crypto"
5 "crypto/hmac"
6 "crypto/rand"
7 "crypto/rsa"
8 "hash"
9 doNotUseInProdCode "math/rand"
10 "strings"
11 "testing"
12 )
13
14 func readFullFromCrypto(b []byte) error {
15 n := len(b)
16 t := 0
17 for t < n {
18 d, err := rand.Reader.Read(b[t:])
19 if d == 0 && err != nil {
20 return err
21 }
22 t += d
23 }
24 return nil
25 }
26
27 func TestIsAvailable(t *testing.T) {
28 tests := []struct {
29 name string
30 algo string
31 expected bool
32 expectError bool
33 }{
34 {
35 name: sha224String,
36 algo: sha224String,
37 expected: true,
38 expectError: false,
39 },
40 {
41 name: sha256String,
42 algo: sha256String,
43 expected: true,
44 expectError: false,
45 },
46 {
47 name: sha384String,
48 algo: sha384String,
49 expected: true,
50 expectError: false,
51 },
52 {
53 name: sha512String,
54 algo: sha512String,
55 expected: true,
56 expectError: false,
57 },
58 {
59 name: sha3_224String,
60 algo: sha3_224String,
61 expected: true,
62 expectError: false,
63 },
64 {
65 name: sha3_256String,
66 algo: sha3_256String,
67 expected: true,
68 expectError: false,
69 },
70 {
71 name: sha3_384String,
72 algo: sha3_384String,
73 expected: true,
74 expectError: false,
75 },
76 {
77 name: sha3_512String,
78 algo: sha3_512String,
79 expected: true,
80 expectError: false,
81 },
82 {
83 name: sha512_224String,
84 algo: sha512_224String,
85 expected: true,
86 expectError: false,
87 },
88 {
89 name: sha512_256String,
90 algo: sha512_256String,
91 expected: true,
92 expectError: false,
93 },
94 {
95 name: blake2s_256String,
96 algo: blake2s_256String,
97 expected: true,
98 expectError: false,
99 },
100 {
101 name: blake2b_256String,
102 algo: blake2b_256String,
103 expected: true,
104 expectError: false,
105 },
106 {
107 name: blake2b_384String,
108 algo: blake2b_384String,
109 expected: true,
110 expectError: false,
111 },
112 {
113 name: blake2b_512String,
114 algo: blake2b_512String,
115 expected: true,
116 expectError: false,
117 },
118 }
119 for _, test := range tests {
120 got, err := isAvailable(test.algo)
121 gotErr := err != nil
122 if got != test.expected {
123 t.Fatalf("%q: got %v, want %v", test.name, got, test.expected)
124 } else if gotErr != test.expectError {
125 if test.expectError {
126 t.Fatalf("%q: expected error, got: %s", test.name, err)
127 } else {
128 t.Fatalf("%q: expected no error, got: %s", test.name, err)
129 }
130 }
131 }
132 }
133
134 func TestSignerFromString(t *testing.T) {
135 tests := []struct {
136 name string
137 input Algorithm
138 expectKind crypto.Hash
139 expectError bool
140 }{
141 {
142 name: "HMAC_SHA224",
143 input: HMAC_SHA224,
144 expectError: true,
145 },
146 {
147 name: "HMAC_SHA256",
148 input: HMAC_SHA256,
149 expectError: true,
150 },
151 {
152 name: "HMAC_SHA384",
153 input: HMAC_SHA384,
154 expectError: true,
155 },
156 {
157 name: "HMAC_SHA512",
158 input: HMAC_SHA512,
159 expectError: true,
160 },
161 {
162 name: "HMAC_SHA3_224",
163 input: HMAC_SHA3_224,
164 expectError: true,
165 },
166 {
167 name: "HMAC_SHA3_256",
168 input: HMAC_SHA3_256,
169 expectError: true,
170 },
171 {
172 name: "HMAC_SHA3_384",
173 input: HMAC_SHA3_384,
174 expectError: true,
175 },
176 {
177 name: "HMAC_SHA3_512",
178 input: HMAC_SHA3_512,
179 expectError: true,
180 },
181 {
182 name: "HMAC_SHA512_224",
183 input: HMAC_SHA512_224,
184 expectError: true,
185 },
186 {
187 name: "HMAC_SHA512_256",
188 input: HMAC_SHA512_256,
189 expectError: true,
190 },
191 {
192 name: "HMAC_BLAKE2S_256",
193 input: HMAC_BLAKE2S_256,
194 expectError: true,
195 },
196 {
197 name: "HMAC_BLAKE2B_256",
198 input: HMAC_BLAKE2B_256,
199 expectError: true,
200 },
201 {
202 name: "HMAC_BLAKE2B_384",
203 input: HMAC_BLAKE2B_384,
204 expectError: true,
205 },
206 {
207 name: "HMAC_BLAKE2B_512",
208 input: HMAC_BLAKE2B_512,
209 expectError: true,
210 },
211 {
212 name: "BLAKE2S_256",
213 input: BLAKE2S_256,
214 expectError: true,
215 },
216 {
217 name: "BLAKE2B_256",
218 input: BLAKE2B_256,
219 expectError: true,
220 },
221 {
222 name: "BLAKE2B_384",
223 input: BLAKE2B_384,
224 expectError: true,
225 },
226 {
227 name: "BLAKE2B_512",
228 input: BLAKE2B_512,
229 expectError: true,
230 },
231 {
232 name: "RSA_SHA224",
233 input: RSA_SHA224,
234 expectKind: crypto.SHA224,
235 },
236 {
237 name: "RSA_SHA256",
238 input: RSA_SHA256,
239 expectKind: crypto.SHA256,
240 },
241 {
242 name: "RSA_SHA384",
243 input: RSA_SHA384,
244 expectKind: crypto.SHA384,
245 },
246 {
247 name: "RSA_SHA512",
248 input: RSA_SHA512,
249 expectKind: crypto.SHA512,
250 },
251 {
252 name: "rsa_SHA3_224",
253 input: rsa_SHA3_224,
254 expectKind: crypto.SHA3_224,
255 },
256 {
257 name: "rsa_SHA3_256",
258 input: rsa_SHA3_256,
259 expectKind: crypto.SHA3_256,
260 },
261 {
262 name: "rsa_SHA3_384",
263 input: rsa_SHA3_384,
264 expectKind: crypto.SHA3_384,
265 },
266 {
267 name: "rsa_SHA3_512",
268 input: rsa_SHA3_512,
269 expectKind: crypto.SHA3_512,
270 },
271 {
272 name: "rsa_SHA512_224",
273 input: rsa_SHA512_224,
274 expectKind: crypto.SHA512_224,
275 },
276 {
277 name: "rsa_SHA512_256",
278 input: rsa_SHA512_256,
279 expectKind: crypto.SHA512_256,
280 },
281 {
282 name: "rsa_BLAKE2S_256",
283 input: rsa_BLAKE2S_256,
284 expectKind: crypto.BLAKE2s_256,
285 },
286 {
287 name: "rsa_BLAKE2B_256",
288 input: rsa_BLAKE2B_256,
289 expectKind: crypto.BLAKE2b_256,
290 },
291 {
292 name: "rsa_BLAKE2B_384",
293 input: rsa_BLAKE2B_384,
294 expectKind: crypto.BLAKE2b_384,
295 },
296 {
297 name: "rsa_BLAKE2B_512",
298 input: rsa_BLAKE2B_512,
299 expectKind: crypto.BLAKE2b_512,
300 },
301 }
302 for _, test := range tests {
303 s, err := signerFromString(string(test.input))
304 hasErr := err != nil
305 if hasErr != test.expectError {
306 if test.expectError {
307 t.Fatalf("%q: expected error, got: %s", test.name, err)
308 } else {
309 t.Fatalf("%q: expected no error, got: %s", test.name, err)
310 }
311 } else if err == nil {
312 want, ok := hashToDef[test.expectKind]
313 if !ok {
314 t.Fatalf("%q: Bad test setup, cannot find %q", test.name, test.expectKind)
315 }
316 if !strings.HasSuffix(s.String(), want.name) {
317 t.Fatalf("%q: expected suffix %q, got %q", test.name, want.name, s.String())
318 }
319 }
320 }
321 }
322
323 func TestMACerFromString(t *testing.T) {
324 tests := []struct {
325 name string
326 input Algorithm
327 expectKind crypto.Hash
328 expectError bool
329 }{
330 {
331 name: "HMAC_SHA224",
332 input: HMAC_SHA224,
333 expectKind: crypto.SHA224,
334 },
335 {
336 name: "HMAC_SHA256",
337 input: HMAC_SHA256,
338 expectKind: crypto.SHA256,
339 },
340 {
341 name: "HMAC_SHA384",
342 input: HMAC_SHA384,
343 expectKind: crypto.SHA384,
344 },
345 {
346 name: "HMAC_SHA512",
347 input: HMAC_SHA512,
348 expectKind: crypto.SHA512,
349 },
350 {
351 name: "HMAC_SHA3_224",
352 input: HMAC_SHA3_224,
353 expectKind: crypto.SHA3_224,
354 },
355 {
356 name: "HMAC_SHA3_256",
357 input: HMAC_SHA3_256,
358 expectKind: crypto.SHA3_256,
359 },
360 {
361 name: "HMAC_SHA3_384",
362 input: HMAC_SHA3_384,
363 expectKind: crypto.SHA3_384,
364 },
365 {
366 name: "HMAC_SHA3_512",
367 input: HMAC_SHA3_512,
368 expectKind: crypto.SHA3_512,
369 },
370 {
371 name: "HMAC_SHA512_224",
372 input: HMAC_SHA512_224,
373 expectKind: crypto.SHA512_224,
374 },
375 {
376 name: "HMAC_SHA512_256",
377 input: HMAC_SHA512_256,
378 expectKind: crypto.SHA512_256,
379 },
380 {
381 name: "HMAC_BLAKE2S_256",
382 input: HMAC_BLAKE2S_256,
383 expectKind: crypto.BLAKE2s_256,
384 },
385 {
386 name: "HMAC_BLAKE2B_256",
387 input: HMAC_BLAKE2B_256,
388 expectKind: crypto.BLAKE2b_256,
389 },
390 {
391 name: "HMAC_BLAKE2B_384",
392 input: HMAC_BLAKE2B_384,
393 expectKind: crypto.BLAKE2b_384,
394 },
395 {
396 name: "HMAC_BLAKE2B_512",
397 input: HMAC_BLAKE2B_512,
398 expectKind: crypto.BLAKE2b_512,
399 },
400 {
401 name: "BLAKE2S_256",
402 input: BLAKE2S_256,
403 expectKind: crypto.BLAKE2s_256,
404 },
405 {
406 name: "BLAKE2B_256",
407 input: BLAKE2B_256,
408 expectKind: crypto.BLAKE2b_256,
409 },
410 {
411 name: "BLAKE2B_384",
412 input: BLAKE2B_384,
413 expectKind: crypto.BLAKE2b_384,
414 },
415 {
416 name: "BLAKE2B_512",
417 input: BLAKE2B_512,
418 expectKind: crypto.BLAKE2b_512,
419 },
420 {
421 name: "RSA_SHA224",
422 input: RSA_SHA224,
423 expectError: true,
424 },
425 {
426 name: "RSA_SHA256",
427 input: RSA_SHA256,
428 expectError: true,
429 },
430 {
431 name: "RSA_SHA384",
432 input: RSA_SHA384,
433 expectError: true,
434 },
435 {
436 name: "RSA_SHA512",
437 input: RSA_SHA512,
438 expectError: true,
439 },
440 {
441 name: "rsa_SHA3_224",
442 input: rsa_SHA3_224,
443 expectError: true,
444 },
445 {
446 name: "rsa_SHA3_256",
447 input: rsa_SHA3_256,
448 expectError: true,
449 },
450 {
451 name: "rsa_SHA3_384",
452 input: rsa_SHA3_384,
453 expectError: true,
454 },
455 {
456 name: "rsa_SHA3_512",
457 input: rsa_SHA3_512,
458 expectError: true,
459 },
460 {
461 name: "rsa_SHA512_224",
462 input: rsa_SHA512_224,
463 expectError: true,
464 },
465 {
466 name: "rsa_SHA512_256",
467 input: rsa_SHA512_256,
468 expectError: true,
469 },
470 {
471 name: "rsa_BLAKE2S_256",
472 input: rsa_BLAKE2S_256,
473 expectError: true,
474 },
475 {
476 name: "rsa_BLAKE2B_256",
477 input: rsa_BLAKE2B_256,
478 expectError: true,
479 },
480 {
481 name: "rsa_BLAKE2B_384",
482 input: rsa_BLAKE2B_384,
483 expectError: true,
484 },
485 {
486 name: "rsa_BLAKE2B_512",
487 input: rsa_BLAKE2B_512,
488 expectError: true,
489 },
490 }
491 for _, test := range tests {
492 m, err := macerFromString(string(test.input))
493 hasErr := err != nil
494 if hasErr != test.expectError {
495 if test.expectError {
496 t.Fatalf("%q: expected error, got: %s", test.name, err)
497 } else {
498 t.Fatalf("%q: expected no error, got: %s", test.name, err)
499 }
500 } else if err == nil {
501 want, ok := hashToDef[test.expectKind]
502 if !ok {
503 t.Fatalf("%q: Bad test setup, cannot find %q", test.name, test.expectKind)
504 }
505 if !strings.HasSuffix(m.String(), want.name) {
506 t.Fatalf("%q: expected suffix %q, got %q", test.name, want.name, m.String())
507 }
508 }
509 }
510 }
511
512 func TestSignerSigns(t *testing.T) {
513 tests := []struct {
514 name string
515 input Algorithm
516 inputCryptoHash crypto.Hash
517 expectRSAUnsupported bool
518 }{
519 {
520 name: "RSA_SHA224",
521 input: RSA_SHA224,
522 inputCryptoHash: crypto.SHA224,
523 },
524 {
525 name: "RSA_SHA256",
526 input: RSA_SHA256,
527 inputCryptoHash: crypto.SHA256,
528 },
529 {
530 name: "RSA_SHA384",
531 input: RSA_SHA384,
532 inputCryptoHash: crypto.SHA384,
533 },
534 {
535 name: "RSA_SHA512",
536 input: RSA_SHA512,
537 inputCryptoHash: crypto.SHA512,
538 },
539 {
540 name: "rsa_SHA3_224",
541 input: rsa_SHA3_224,
542 inputCryptoHash: crypto.SHA3_224,
543 expectRSAUnsupported: true,
544 },
545 {
546 name: "rsa_SHA3_256",
547 input: rsa_SHA3_256,
548 inputCryptoHash: crypto.SHA3_256,
549 expectRSAUnsupported: true,
550 },
551 {
552 name: "rsa_SHA3_384",
553 input: rsa_SHA3_384,
554 inputCryptoHash: crypto.SHA3_384,
555 expectRSAUnsupported: true,
556 },
557 {
558 name: "rsa_SHA3_512",
559 input: rsa_SHA3_512,
560 inputCryptoHash: crypto.SHA3_512,
561 expectRSAUnsupported: true,
562 },
563 {
564 name: "rsa_SHA512_224",
565 input: rsa_SHA512_224,
566 inputCryptoHash: crypto.SHA512_224,
567 expectRSAUnsupported: true,
568 },
569 {
570 name: "rsa_SHA512_256",
571 input: rsa_SHA512_256,
572 inputCryptoHash: crypto.SHA512_256,
573 expectRSAUnsupported: true,
574 },
575 {
576 name: "rsa_BLAKE2S_256",
577 input: rsa_BLAKE2S_256,
578 inputCryptoHash: crypto.BLAKE2s_256,
579 expectRSAUnsupported: true,
580 },
581 {
582 name: "rsa_BLAKE2B_256",
583 input: rsa_BLAKE2B_256,
584 inputCryptoHash: crypto.BLAKE2b_256,
585 expectRSAUnsupported: true,
586 },
587 {
588 name: "rsa_BLAKE2B_384",
589 input: rsa_BLAKE2B_384,
590 inputCryptoHash: crypto.BLAKE2b_384,
591 expectRSAUnsupported: true,
592 },
593 {
594 name: "rsa_BLAKE2B_512",
595 input: rsa_BLAKE2B_512,
596 inputCryptoHash: crypto.BLAKE2b_512,
597 expectRSAUnsupported: true,
598 },
599 }
600 for _, test := range tests {
601 privKey, err := rsa.GenerateKey(rand.Reader, 2048)
602 if err != nil {
603 t.Fatalf("%q: Failed setup: %s", test.name, err)
604 }
605 sig := make([]byte, 65535)
606 n, err := doNotUseInProdCode.Read(sig)
607 if n != len(sig) {
608 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(sig))
609 } else if err != nil {
610 t.Fatalf("%q: Failed setup: %s", test.name, err)
611 }
612 s, err := signerFromString(string(test.input))
613 if err != nil {
614 t.Fatalf("%q: %s", test.name, err)
615 }
616 seed := doNotUseInProdCode.Int63()
617 doNotUseThisKindOfRandInProdCodeTest := doNotUseInProdCode.New(doNotUseInProdCode.NewSource(seed))
618 doNotUseThisKindOfRandInProdCodeTestVerify := doNotUseInProdCode.New(doNotUseInProdCode.NewSource(seed))
619 actual, err := s.Sign(doNotUseThisKindOfRandInProdCodeTest, privKey, sig)
620 hasErr := err != nil
621 if test.expectRSAUnsupported != hasErr {
622 if test.expectRSAUnsupported {
623 t.Fatalf("%q: expected error, got: %s", test.name, err)
624 } else {
625 t.Fatalf("%q: expected no error, got: %s", test.name, err)
626 }
627 } else if !test.expectRSAUnsupported && err != nil {
628 t.Fatalf("%q: %s", test.name, err)
629 } else if test.expectRSAUnsupported {
630 // Skip further testing -- just need to verify it is
631 // unsupported.
632 continue
633 }
634 testHash, err := hashToDef[test.inputCryptoHash].new(nil)
635 if err != nil {
636 t.Fatalf("%q: %s", test.name, err)
637 }
638 n, err = testHash.Write(sig)
639 if n != len(sig) {
640 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(sig))
641 } else if err != nil {
642 t.Fatalf("%q: Failed setup: %s", test.name, err)
643 }
644 want, err := rsa.SignPKCS1v15(doNotUseThisKindOfRandInProdCodeTestVerify, privKey, test.inputCryptoHash, testHash.Sum(nil))
645 if err != nil {
646 t.Fatalf("%q: %s", test.name, err)
647 }
648 if len(actual) != len(want) {
649 t.Fatalf("%q: len actual (%d) != len want (%d)", test.name, len(actual), len(want))
650 }
651 for i, v := range actual {
652 if v != want[i] {
653 t.Fatalf("%q: difference beginning at index %d:\nwant: %v\nactual: %v", test.name, i, want, actual)
654 }
655 }
656 }
657 }
658
659 func TestSignerVerifies(t *testing.T) {
660 tests := []struct {
661 name string
662 input Algorithm
663 inputCryptoHash crypto.Hash
664 }{
665 {
666 name: "RSA_SHA224",
667 input: RSA_SHA224,
668 inputCryptoHash: crypto.SHA224,
669 },
670 {
671 name: "RSA_SHA256",
672 input: RSA_SHA256,
673 inputCryptoHash: crypto.SHA256,
674 },
675 {
676 name: "RSA_SHA384",
677 input: RSA_SHA384,
678 inputCryptoHash: crypto.SHA384,
679 },
680 {
681 name: "RSA_SHA512",
682 input: RSA_SHA512,
683 inputCryptoHash: crypto.SHA512,
684 },
685 }
686 for _, test := range tests {
687 privKey, err := rsa.GenerateKey(rand.Reader, 2048)
688 if err != nil {
689 t.Fatalf("%q: Failed setup: %s", test.name, err)
690 }
691 toHash := make([]byte, 65535)
692 n, err := doNotUseInProdCode.Read(toHash)
693 if n != len(toHash) {
694 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(toHash))
695 } else if err != nil {
696 t.Fatalf("%q: Failed setup: %s", test.name, err)
697 }
698 testHash, err := hashToDef[test.inputCryptoHash].new(nil)
699 if err != nil {
700 t.Fatalf("%q: %s", test.name, err)
701 }
702 n, err = testHash.Write(toHash)
703 if n != len(toHash) {
704 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(toHash))
705 } else if err != nil {
706 t.Fatalf("%q: Failed setup: %s", test.name, err)
707 }
708 signature, err := rsa.SignPKCS1v15(rand.Reader, privKey, test.inputCryptoHash, testHash.Sum(nil))
709 if err != nil {
710 t.Fatalf("%q: %s", test.name, err)
711 }
712 s, err := signerFromString(string(test.input))
713 if err != nil {
714 t.Fatalf("%q: %s", test.name, err)
715 }
716 err = s.Verify(privKey.Public(), toHash, signature)
717 if err != nil {
718 t.Fatalf("%q: %s", test.name, err)
719 }
720 }
721 }
722
723 func TestMACerSigns(t *testing.T) {
724 tests := []struct {
725 name string
726 input Algorithm
727 inputCryptoHash crypto.Hash
728 isHMAC bool
729 keySize int
730 }{
731 {
732 name: "HMAC_SHA224",
733 input: HMAC_SHA224,
734 inputCryptoHash: crypto.SHA224,
735 isHMAC: true,
736 keySize: 128,
737 },
738 {
739 name: "HMAC_SHA256",
740 input: HMAC_SHA256,
741 inputCryptoHash: crypto.SHA256,
742 isHMAC: true,
743 keySize: 128,
744 },
745 {
746 name: "HMAC_SHA384",
747 input: HMAC_SHA384,
748 inputCryptoHash: crypto.SHA384,
749 isHMAC: true,
750 keySize: 128,
751 },
752 {
753 name: "HMAC_SHA512",
754 input: HMAC_SHA512,
755 inputCryptoHash: crypto.SHA512,
756 isHMAC: true,
757 keySize: 128,
758 },
759 {
760 name: "HMAC_SHA3_224",
761 input: HMAC_SHA3_224,
762 inputCryptoHash: crypto.SHA3_224,
763 isHMAC: true,
764 keySize: 128,
765 },
766 {
767 name: "HMAC_SHA3_256",
768 input: HMAC_SHA3_256,
769 inputCryptoHash: crypto.SHA3_256,
770 isHMAC: true,
771 keySize: 128,
772 },
773 {
774 name: "HMAC_SHA3_384",
775 input: HMAC_SHA3_384,
776 inputCryptoHash: crypto.SHA3_384,
777 isHMAC: true,
778 keySize: 128,
779 },
780 {
781 name: "HMAC_SHA3_512",
782 input: HMAC_SHA3_512,
783 inputCryptoHash: crypto.SHA3_512,
784 isHMAC: true,
785 keySize: 128,
786 },
787 {
788 name: "HMAC_SHA512_224",
789 input: HMAC_SHA512_224,
790 inputCryptoHash: crypto.SHA512_224,
791 isHMAC: true,
792 keySize: 128,
793 },
794 {
795 name: "HMAC_SHA512_256",
796 input: HMAC_SHA512_256,
797 inputCryptoHash: crypto.SHA512_256,
798 isHMAC: true,
799 keySize: 128,
800 },
801 {
802 name: "HMAC_BLAKE2S_256",
803 input: HMAC_BLAKE2S_256,
804 inputCryptoHash: crypto.BLAKE2s_256,
805 isHMAC: true,
806 keySize: 128,
807 },
808 {
809 name: "HMAC_BLAKE2B_256",
810 input: HMAC_BLAKE2B_256,
811 inputCryptoHash: crypto.BLAKE2b_256,
812 isHMAC: true,
813 keySize: 128,
814 },
815 {
816 name: "HMAC_BLAKE2B_384",
817 input: HMAC_BLAKE2B_384,
818 inputCryptoHash: crypto.BLAKE2b_384,
819 isHMAC: true,
820 keySize: 128,
821 },
822 {
823 name: "HMAC_BLAKE2B_512",
824 input: HMAC_BLAKE2B_512,
825 inputCryptoHash: crypto.BLAKE2b_512,
826 isHMAC: true,
827 keySize: 128,
828 },
829 {
830 name: "BLAKE2S_256",
831 input: BLAKE2S_256,
832 inputCryptoHash: crypto.BLAKE2s_256,
833 keySize: 32,
834 },
835 {
836 name: "BLAKE2B_256",
837 input: BLAKE2B_256,
838 inputCryptoHash: crypto.BLAKE2b_256,
839 keySize: 64,
840 },
841 {
842 name: "BLAKE2B_384",
843 input: BLAKE2B_384,
844 inputCryptoHash: crypto.BLAKE2b_384,
845 keySize: 64,
846 },
847 {
848 name: "BLAKE2B_512",
849 input: BLAKE2B_512,
850 inputCryptoHash: crypto.BLAKE2b_512,
851 keySize: 64,
852 },
853 }
854 for _, test := range tests {
855 privKey := make([]byte, test.keySize)
856 err := readFullFromCrypto(privKey)
857 if err != nil {
858 t.Fatalf("%q: Failed setup: %s", test.name, err)
859 }
860 sig := make([]byte, 65535)
861 n, err := doNotUseInProdCode.Read(sig)
862 if n != len(sig) {
863 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(sig))
864 } else if err != nil {
865 t.Fatalf("%q: Failed setup: %s", test.name, err)
866 }
867 m, err := macerFromString(string(test.input))
868 if err != nil {
869 t.Fatalf("%q: %s", test.name, err)
870 }
871 actual, err := m.Sign(sig, privKey)
872 if err != nil {
873 t.Fatalf("%q: %s", test.name, err)
874 }
875 var want []byte
876 if test.isHMAC {
877 hmacHash := hmac.New(func() hash.Hash {
878 testHash, err := hashToDef[test.inputCryptoHash].new(nil)
879 if err != nil {
880 t.Fatalf("%q: %s", test.name, err)
881 }
882 return testHash
883 }, privKey)
884 n, err = hmacHash.Write(sig)
885 if n != len(sig) {
886 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(sig))
887 } else if err != nil {
888 t.Fatalf("%q: Failed setup: %s", test.name, err)
889 }
890 want = hmacHash.Sum(nil)
891 } else {
892 testHash, err := hashToDef[test.inputCryptoHash].new(privKey)
893 if err != nil {
894 t.Fatalf("%q: %s", test.name, err)
895 }
896 n, err = testHash.Write(sig)
897 if n != len(sig) {
898 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(sig))
899 } else if err != nil {
900 t.Fatalf("%q: Failed setup: %s", test.name, err)
901 }
902 want = testHash.Sum(nil)
903 }
904 if len(actual) != len(want) {
905 t.Fatalf("%q: len actual (%d) != len want (%d)", test.name, len(actual), len(want))
906 }
907 for i, v := range actual {
908 if v != want[i] {
909 t.Fatalf("%q: difference beginning at index %d:\nwant: %v\nactual: %v", test.name, i, want, actual)
910 }
911 }
912 }
913 }
914
915 func TestMACerEquals(t *testing.T) {
916 tests := []struct {
917 name string
918 input Algorithm
919 inputCryptoHash crypto.Hash
920 isHMAC bool
921 keySize int
922 }{
923 {
924 name: "HMAC_SHA224",
925 input: HMAC_SHA224,
926 inputCryptoHash: crypto.SHA224,
927 isHMAC: true,
928 keySize: 128,
929 },
930 {
931 name: "HMAC_SHA256",
932 input: HMAC_SHA256,
933 inputCryptoHash: crypto.SHA256,
934 isHMAC: true,
935 keySize: 128,
936 },
937 {
938 name: "HMAC_SHA384",
939 input: HMAC_SHA384,
940 inputCryptoHash: crypto.SHA384,
941 isHMAC: true,
942 keySize: 128,
943 },
944 {
945 name: "HMAC_SHA512",
946 input: HMAC_SHA512,
947 inputCryptoHash: crypto.SHA512,
948 isHMAC: true,
949 keySize: 128,
950 },
951 {
952 name: "HMAC_SHA3_224",
953 input: HMAC_SHA3_224,
954 inputCryptoHash: crypto.SHA3_224,
955 isHMAC: true,
956 keySize: 128,
957 },
958 {
959 name: "HMAC_SHA3_256",
960 input: HMAC_SHA3_256,
961 inputCryptoHash: crypto.SHA3_256,
962 isHMAC: true,
963 keySize: 128,
964 },
965 {
966 name: "HMAC_SHA3_384",
967 input: HMAC_SHA3_384,
968 inputCryptoHash: crypto.SHA3_384,
969 isHMAC: true,
970 keySize: 128,
971 },
972 {
973 name: "HMAC_SHA3_512",
974 input: HMAC_SHA3_512,
975 inputCryptoHash: crypto.SHA3_512,
976 isHMAC: true,
977 keySize: 128,
978 },
979 {
980 name: "HMAC_SHA512_224",
981 input: HMAC_SHA512_224,
982 inputCryptoHash: crypto.SHA512_224,
983 isHMAC: true,
984 keySize: 128,
985 },
986 {
987 name: "HMAC_SHA512_256",
988 input: HMAC_SHA512_256,
989 inputCryptoHash: crypto.SHA512_256,
990 isHMAC: true,
991 keySize: 128,
992 },
993 {
994 name: "HMAC_BLAKE2S_256",
995 input: HMAC_BLAKE2S_256,
996 inputCryptoHash: crypto.BLAKE2s_256,
997 isHMAC: true,
998 keySize: 128,
999 },
1000 {
1001 name: "HMAC_BLAKE2B_256",
1002 input: HMAC_BLAKE2B_256,
1003 inputCryptoHash: crypto.BLAKE2b_256,
1004 isHMAC: true,
1005 keySize: 128,
1006 },
1007 {
1008 name: "HMAC_BLAKE2B_384",
1009 input: HMAC_BLAKE2B_384,
1010 inputCryptoHash: crypto.BLAKE2b_384,
1011 isHMAC: true,
1012 keySize: 128,
1013 },
1014 {
1015 name: "HMAC_BLAKE2B_512",
1016 input: HMAC_BLAKE2B_512,
1017 inputCryptoHash: crypto.BLAKE2b_512,
1018 isHMAC: true,
1019 keySize: 128,
1020 },
1021 {
1022 name: "BLAKE2S_256",
1023 input: BLAKE2S_256,
1024 inputCryptoHash: crypto.BLAKE2s_256,
1025 keySize: 32,
1026 },
1027 {
1028 name: "BLAKE2B_256",
1029 input: BLAKE2B_256,
1030 inputCryptoHash: crypto.BLAKE2b_256,
1031 keySize: 64,
1032 },
1033 {
1034 name: "BLAKE2B_384",
1035 input: BLAKE2B_384,
1036 inputCryptoHash: crypto.BLAKE2b_384,
1037 keySize: 64,
1038 },
1039 {
1040 name: "BLAKE2B_512",
1041 input: BLAKE2B_512,
1042 inputCryptoHash: crypto.BLAKE2b_512,
1043 keySize: 64,
1044 },
1045 }
1046 for _, test := range tests {
1047 privKey := make([]byte, test.keySize)
1048 err := readFullFromCrypto(privKey)
1049 if err != nil {
1050 t.Fatalf("%q: Failed setup: %s", test.name, err)
1051 }
1052 sig := make([]byte, 65535)
1053 n, err := doNotUseInProdCode.Read(sig)
1054 if n != len(sig) {
1055 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(sig))
1056 } else if err != nil {
1057 t.Fatalf("%q: Failed setup: %s", test.name, err)
1058 }
1059 var actual []byte
1060 if test.isHMAC {
1061 hmacHash := hmac.New(func() hash.Hash {
1062 testHash, err := hashToDef[test.inputCryptoHash].new(nil)
1063 if err != nil {
1064 t.Fatalf("%q: %s", test.name, err)
1065 }
1066 return testHash
1067 }, privKey)
1068 n, err = hmacHash.Write(sig)
1069 if n != len(sig) {
1070 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(sig))
1071 } else if err != nil {
1072 t.Fatalf("%q: Failed setup: %s", test.name, err)
1073 }
1074 actual = hmacHash.Sum(nil)
1075 } else {
1076 testHash, err := hashToDef[test.inputCryptoHash].new(privKey)
1077 if err != nil {
1078 t.Fatalf("%q: %s", test.name, err)
1079 }
1080 n, err = testHash.Write(sig)
1081 if n != len(sig) {
1082 t.Fatalf("%q: Failed setup: %d bytes != %d bytes", test.name, n, len(sig))
1083 } else if err != nil {
1084 t.Fatalf("%q: Failed setup: %s", test.name, err)
1085 }
1086 actual = testHash.Sum(nil)
1087 }
1088 m, err := macerFromString(string(test.input))
1089 if err != nil {
1090 t.Fatalf("%q: %s", test.name, err)
1091 }
1092 equal, err := m.Equal(sig, actual, privKey)
1093 if err != nil {
1094 t.Fatalf("%q: %s", test.name, err)
1095 } else if !equal {
1096 t.Fatalf("%q: signature is not verified", test.name)
1097 }
1098 }
1099 }