diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/wycheproof/wycheproof.go | 295 |
1 files changed, 151 insertions, 144 deletions
diff --git a/src/regress/lib/libcrypto/wycheproof/wycheproof.go b/src/regress/lib/libcrypto/wycheproof/wycheproof.go index 5567e02a6c..accd9ab9cb 100644 --- a/src/regress/lib/libcrypto/wycheproof/wycheproof.go +++ b/src/regress/lib/libcrypto/wycheproof/wycheproof.go | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: wycheproof.go,v 1.136 2023/03/08 05:17:33 jsing Exp $ */ | 1 | /* $OpenBSD: wycheproof.go,v 1.137 2023/03/08 05:41:08 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2018 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2018 Joel Sing <jsing@openbsd.org> |
4 | * Copyright (c) 2018,2019,2022 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2018,2019,2022 Theo Buehler <tb@openbsd.org> |
@@ -88,6 +88,7 @@ import ( | |||
88 | "os" | 88 | "os" |
89 | "path/filepath" | 89 | "path/filepath" |
90 | "regexp" | 90 | "regexp" |
91 | "runtime" | ||
91 | "strings" | 92 | "strings" |
92 | "unsafe" | 93 | "unsafe" |
93 | ) | 94 | ) |
@@ -121,9 +122,7 @@ func (variant testVariant) String() string { | |||
121 | return variants[variant] | 122 | return variants[variant] |
122 | } | 123 | } |
123 | 124 | ||
124 | var acceptableAudit = false | 125 | var testc *testCoordinator |
125 | var acceptableComments map[string]int | ||
126 | var acceptableFlags map[string]int | ||
127 | 126 | ||
128 | type wycheproofJWKPublic struct { | 127 | type wycheproofJWKPublic struct { |
129 | Crv string `json:"crv"` | 128 | Crv string `json:"crv"` |
@@ -2776,158 +2775,162 @@ func runTestVectors(path string, variant testVariant) bool { | |||
2776 | fmt.Printf("Loaded Wycheproof test vectors for %v with %d tests from %q\n", | 2775 | fmt.Printf("Loaded Wycheproof test vectors for %v with %d tests from %q\n", |
2777 | wtv.Algorithm, wtv.NumberOfTests, filepath.Base(path)) | 2776 | wtv.Algorithm, wtv.NumberOfTests, filepath.Base(path)) |
2778 | 2777 | ||
2779 | var wtg interface{} | ||
2780 | switch wtv.Algorithm { | ||
2781 | case "AES-CBC-PKCS5": | ||
2782 | wtg = &wycheproofTestGroupAesCbcPkcs5{} | ||
2783 | case "AES-CCM": | ||
2784 | wtg = &wycheproofTestGroupAead{} | ||
2785 | case "AES-CMAC": | ||
2786 | wtg = &wycheproofTestGroupAesCmac{} | ||
2787 | case "AES-GCM": | ||
2788 | wtg = &wycheproofTestGroupAead{} | ||
2789 | case "CHACHA20-POLY1305", "XCHACHA20-POLY1305": | ||
2790 | wtg = &wycheproofTestGroupAead{} | ||
2791 | case "DSA": | ||
2792 | wtg = &wycheproofTestGroupDSA{} | ||
2793 | case "ECDH": | ||
2794 | switch variant { | ||
2795 | case Webcrypto: | ||
2796 | wtg = &wycheproofTestGroupECDHWebCrypto{} | ||
2797 | default: | ||
2798 | wtg = &wycheproofTestGroupECDH{} | ||
2799 | } | ||
2800 | case "ECDSA": | ||
2801 | switch variant { | ||
2802 | case Webcrypto: | ||
2803 | wtg = &wycheproofTestGroupECDSAWebCrypto{} | ||
2804 | default: | ||
2805 | wtg = &wycheproofTestGroupECDSA{} | ||
2806 | } | ||
2807 | case "EDDSA": | ||
2808 | wtg = &wycheproofTestGroupEdDSA{} | ||
2809 | case "HKDF-SHA-1", "HKDF-SHA-256", "HKDF-SHA-384", "HKDF-SHA-512": | ||
2810 | wtg = &wycheproofTestGroupHkdf{} | ||
2811 | case "HMACSHA1", "HMACSHA224", "HMACSHA256", "HMACSHA384", "HMACSHA512": | ||
2812 | wtg = &wycheproofTestGroupHmac{} | ||
2813 | case "KW": | ||
2814 | wtg = &wycheproofTestGroupKW{} | ||
2815 | case "PrimalityTest": | ||
2816 | wtg = &wycheproofTestGroupPrimality{} | ||
2817 | case "RSAES-OAEP": | ||
2818 | wtg = &wycheproofTestGroupRsaesOaep{} | ||
2819 | case "RSAES-PKCS1-v1_5": | ||
2820 | wtg = &wycheproofTestGroupRsaesPkcs1{} | ||
2821 | case "RSASSA-PSS": | ||
2822 | wtg = &wycheproofTestGroupRsassa{} | ||
2823 | case "RSASSA-PKCS1-v1_5", "RSASig": | ||
2824 | wtg = &wycheproofTestGroupRSA{} | ||
2825 | case "XDH", "X25519": | ||
2826 | wtg = &wycheproofTestGroupX25519{} | ||
2827 | default: | ||
2828 | log.Printf("INFO: Unknown test vector algorithm %q", wtv.Algorithm) | ||
2829 | return false | ||
2830 | } | ||
2831 | |||
2832 | success := true | 2778 | success := true |
2833 | for _, tg := range wtv.TestGroups { | 2779 | for i := range wtv.TestGroups { |
2834 | if err := json.Unmarshal(tg, wtg); err != nil { | 2780 | testc.runTest(func() bool { |
2835 | log.Fatalf("Failed to unmarshal test groups JSON: %v", err) | 2781 | var wtg interface{} |
2836 | } | 2782 | switch wtv.Algorithm { |
2837 | switch wtv.Algorithm { | 2783 | case "AES-CBC-PKCS5": |
2838 | case "AES-CBC-PKCS5": | 2784 | wtg = &wycheproofTestGroupAesCbcPkcs5{} |
2839 | if !runAesCbcPkcs5TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAesCbcPkcs5)) { | 2785 | case "AES-CCM": |
2840 | success = false | 2786 | wtg = &wycheproofTestGroupAead{} |
2841 | } | 2787 | case "AES-CMAC": |
2842 | case "AES-CCM": | 2788 | wtg = &wycheproofTestGroupAesCmac{} |
2843 | if !runAesAeadTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAead)) { | 2789 | case "AES-GCM": |
2844 | success = false | 2790 | wtg = &wycheproofTestGroupAead{} |
2845 | } | 2791 | case "CHACHA20-POLY1305", "XCHACHA20-POLY1305": |
2846 | case "AES-CMAC": | 2792 | wtg = &wycheproofTestGroupAead{} |
2847 | if !runAesCmacTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAesCmac)) { | 2793 | case "DSA": |
2848 | success = false | 2794 | wtg = &wycheproofTestGroupDSA{} |
2849 | } | 2795 | case "ECDH": |
2850 | case "AES-GCM": | 2796 | switch variant { |
2851 | if !runAesAeadTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAead)) { | 2797 | case Webcrypto: |
2852 | success = false | 2798 | wtg = &wycheproofTestGroupECDHWebCrypto{} |
2853 | } | 2799 | default: |
2854 | case "CHACHA20-POLY1305", "XCHACHA20-POLY1305": | 2800 | wtg = &wycheproofTestGroupECDH{} |
2855 | if !runChaCha20Poly1305TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAead)) { | ||
2856 | success = false | ||
2857 | } | ||
2858 | case "DSA": | ||
2859 | if !runDSATestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupDSA)) { | ||
2860 | success = false | ||
2861 | } | ||
2862 | case "ECDH": | ||
2863 | switch variant { | ||
2864 | case Webcrypto: | ||
2865 | if !runECDHWebCryptoTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupECDHWebCrypto)) { | ||
2866 | success = false | ||
2867 | } | 2801 | } |
2868 | default: | 2802 | case "ECDSA": |
2869 | if !runECDHTestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupECDH)) { | 2803 | switch variant { |
2870 | success = false | 2804 | case Webcrypto: |
2871 | } | 2805 | wtg = &wycheproofTestGroupECDSAWebCrypto{} |
2872 | } | 2806 | default: |
2873 | case "ECDSA": | 2807 | wtg = &wycheproofTestGroupECDSA{} |
2874 | switch variant { | ||
2875 | case Webcrypto: | ||
2876 | if !runECDSAWebCryptoTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupECDSAWebCrypto)) { | ||
2877 | success = false | ||
2878 | } | 2808 | } |
2809 | case "EDDSA": | ||
2810 | wtg = &wycheproofTestGroupEdDSA{} | ||
2811 | case "HKDF-SHA-1", "HKDF-SHA-256", "HKDF-SHA-384", "HKDF-SHA-512": | ||
2812 | wtg = &wycheproofTestGroupHkdf{} | ||
2813 | case "HMACSHA1", "HMACSHA224", "HMACSHA256", "HMACSHA384", "HMACSHA512": | ||
2814 | wtg = &wycheproofTestGroupHmac{} | ||
2815 | case "KW": | ||
2816 | wtg = &wycheproofTestGroupKW{} | ||
2817 | case "PrimalityTest": | ||
2818 | wtg = &wycheproofTestGroupPrimality{} | ||
2819 | case "RSAES-OAEP": | ||
2820 | wtg = &wycheproofTestGroupRsaesOaep{} | ||
2821 | case "RSAES-PKCS1-v1_5": | ||
2822 | wtg = &wycheproofTestGroupRsaesPkcs1{} | ||
2823 | case "RSASSA-PSS": | ||
2824 | wtg = &wycheproofTestGroupRsassa{} | ||
2825 | case "RSASSA-PKCS1-v1_5", "RSASig": | ||
2826 | wtg = &wycheproofTestGroupRSA{} | ||
2827 | case "XDH", "X25519": | ||
2828 | wtg = &wycheproofTestGroupX25519{} | ||
2879 | default: | 2829 | default: |
2880 | if !runECDSATestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupECDSA)) { | 2830 | log.Printf("INFO: Unknown test vector algorithm %q", wtv.Algorithm) |
2881 | success = false | 2831 | return false |
2882 | } | ||
2883 | } | ||
2884 | case "EDDSA": | ||
2885 | if !runEdDSATestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupEdDSA)) { | ||
2886 | success = false | ||
2887 | } | ||
2888 | case "HKDF-SHA-1", "HKDF-SHA-256", "HKDF-SHA-384", "HKDF-SHA-512": | ||
2889 | if !runHkdfTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHkdf)) { | ||
2890 | success = false | ||
2891 | } | ||
2892 | case "HMACSHA1", "HMACSHA224", "HMACSHA256", "HMACSHA384", "HMACSHA512": | ||
2893 | if !runHmacTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHmac)) { | ||
2894 | success = false | ||
2895 | } | ||
2896 | case "KW": | ||
2897 | if !runKWTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupKW)) { | ||
2898 | success = false | ||
2899 | } | 2832 | } |
2900 | case "PrimalityTest": | 2833 | |
2901 | if !runPrimalityTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupPrimality)) { | 2834 | if err := json.Unmarshal(wtv.TestGroups[i], wtg); err != nil { |
2902 | success = false | 2835 | log.Fatalf("Failed to unmarshal test groups JSON: %v", err) |
2903 | } | ||
2904 | case "RSAES-OAEP": | ||
2905 | if !runRsaesOaepTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsaesOaep)) { | ||
2906 | success = false | ||
2907 | } | ||
2908 | case "RSAES-PKCS1-v1_5": | ||
2909 | if !runRsaesPkcs1TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsaesPkcs1)) { | ||
2910 | success = false | ||
2911 | } | ||
2912 | case "RSASSA-PSS": | ||
2913 | if !runRsassaTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsassa)) { | ||
2914 | success = false | ||
2915 | } | ||
2916 | case "RSASSA-PKCS1-v1_5", "RSASig": | ||
2917 | if !runRSATestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRSA)) { | ||
2918 | success = false | ||
2919 | } | 2836 | } |
2920 | case "XDH", "X25519": | 2837 | switch wtv.Algorithm { |
2921 | if !runX25519TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupX25519)) { | 2838 | case "AES-CBC-PKCS5": |
2922 | success = false | 2839 | return runAesCbcPkcs5TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAesCbcPkcs5)) |
2840 | case "AES-CCM": | ||
2841 | return runAesAeadTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAead)) | ||
2842 | case "AES-CMAC": | ||
2843 | return runAesCmacTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAesCmac)) | ||
2844 | case "AES-GCM": | ||
2845 | return runAesAeadTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAead)) | ||
2846 | case "CHACHA20-POLY1305", "XCHACHA20-POLY1305": | ||
2847 | return runChaCha20Poly1305TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAead)) | ||
2848 | case "DSA": | ||
2849 | return runDSATestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupDSA)) | ||
2850 | case "ECDH": | ||
2851 | switch variant { | ||
2852 | case Webcrypto: | ||
2853 | return runECDHWebCryptoTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupECDHWebCrypto)) | ||
2854 | default: | ||
2855 | return runECDHTestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupECDH)) | ||
2856 | } | ||
2857 | case "ECDSA": | ||
2858 | switch variant { | ||
2859 | case Webcrypto: | ||
2860 | return runECDSAWebCryptoTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupECDSAWebCrypto)) | ||
2861 | default: | ||
2862 | return runECDSATestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupECDSA)) | ||
2863 | } | ||
2864 | case "EDDSA": | ||
2865 | return runEdDSATestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupEdDSA)) | ||
2866 | case "HKDF-SHA-1", "HKDF-SHA-256", "HKDF-SHA-384", "HKDF-SHA-512": | ||
2867 | return runHkdfTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHkdf)) | ||
2868 | case "HMACSHA1", "HMACSHA224", "HMACSHA256", "HMACSHA384", "HMACSHA512": | ||
2869 | return runHmacTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHmac)) | ||
2870 | case "KW": | ||
2871 | return runKWTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupKW)) | ||
2872 | case "PrimalityTest": | ||
2873 | return runPrimalityTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupPrimality)) | ||
2874 | case "RSAES-OAEP": | ||
2875 | return runRsaesOaepTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsaesOaep)) | ||
2876 | case "RSAES-PKCS1-v1_5": | ||
2877 | return runRsaesPkcs1TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsaesPkcs1)) | ||
2878 | case "RSASSA-PSS": | ||
2879 | return runRsassaTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsassa)) | ||
2880 | case "RSASSA-PKCS1-v1_5", "RSASig": | ||
2881 | return runRSATestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRSA)) | ||
2882 | case "XDH", "X25519": | ||
2883 | return runX25519TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupX25519)) | ||
2884 | default: | ||
2885 | log.Fatalf("Unknown test vector algorithm %q", wtv.Algorithm) | ||
2886 | return false | ||
2923 | } | 2887 | } |
2924 | default: | 2888 | }) |
2925 | log.Fatalf("Unknown test vector algorithm %q", wtv.Algorithm) | 2889 | } |
2890 | for _ = range wtv.TestGroups { | ||
2891 | result := <-testc.resultCh | ||
2892 | if !result { | ||
2893 | success = false | ||
2926 | } | 2894 | } |
2927 | } | 2895 | } |
2928 | return success | 2896 | return success |
2929 | } | 2897 | } |
2930 | 2898 | ||
2899 | type testCoordinator struct { | ||
2900 | testFuncCh chan func() bool | ||
2901 | resultCh chan bool | ||
2902 | } | ||
2903 | |||
2904 | func newTestCoordinator() *testCoordinator { | ||
2905 | runnerCount := runtime.NumCPU() | ||
2906 | tc := &testCoordinator{ | ||
2907 | testFuncCh: make(chan func() bool, runnerCount), | ||
2908 | resultCh: make(chan bool, 1024), | ||
2909 | } | ||
2910 | for i := 0; i < runnerCount; i++ { | ||
2911 | go tc.testRunner(tc.testFuncCh, tc.resultCh) | ||
2912 | } | ||
2913 | return tc | ||
2914 | } | ||
2915 | |||
2916 | func (tc *testCoordinator) testRunner(testFuncCh <-chan func() bool, resultCh chan<- bool) { | ||
2917 | for testFunc := range testFuncCh { | ||
2918 | select { | ||
2919 | case resultCh <- testFunc(): | ||
2920 | default: | ||
2921 | log.Fatal("result channel is full") | ||
2922 | } | ||
2923 | } | ||
2924 | } | ||
2925 | |||
2926 | func (tc *testCoordinator) runTest(testFunc func() bool) { | ||
2927 | tc.testFuncCh <- testFunc | ||
2928 | } | ||
2929 | |||
2930 | func (tc *testCoordinator) shutdown() { | ||
2931 | close(tc.testFuncCh) | ||
2932 | } | ||
2933 | |||
2931 | func main() { | 2934 | func main() { |
2932 | if _, err := os.Stat(testVectorPath); os.IsNotExist(err) { | 2935 | if _, err := os.Stat(testVectorPath); os.IsNotExist(err) { |
2933 | fmt.Printf("package wycheproof-testvectors is required for this regress\n") | 2936 | fmt.Printf("package wycheproof-testvectors is required for this regress\n") |
@@ -2970,6 +2973,8 @@ func main() { | |||
2970 | 2973 | ||
2971 | success := true | 2974 | success := true |
2972 | 2975 | ||
2976 | testc = newTestCoordinator() | ||
2977 | |||
2973 | skipNormal := regexp.MustCompile(`_(ecpoint|p1363|sha3|sha512_(224|256))_`) | 2978 | skipNormal := regexp.MustCompile(`_(ecpoint|p1363|sha3|sha512_(224|256))_`) |
2974 | 2979 | ||
2975 | for _, test := range tests { | 2980 | for _, test := range tests { |
@@ -2991,6 +2996,8 @@ func main() { | |||
2991 | } | 2996 | } |
2992 | } | 2997 | } |
2993 | 2998 | ||
2999 | testc.shutdown() | ||
3000 | |||
2994 | if !success { | 3001 | if !success { |
2995 | os.Exit(1) | 3002 | os.Exit(1) |
2996 | } | 3003 | } |