summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2023-11-06 15:14:52 +0000
committertb <>2023-11-06 15:14:52 +0000
commit582bb3a80d27e2c82069e50309c478f7fe127886 (patch)
treed807c8d1bb9567c8e7a25ace0177cfd1a29e60d5 /src
parentb2bba3751a9e24c0a6a3bc5acf3b6d5219e88cba (diff)
downloadopenbsd-582bb3a80d27e2c82069e50309c478f7fe127886.tar.gz
openbsd-582bb3a80d27e2c82069e50309c478f7fe127886.tar.bz2
openbsd-582bb3a80d27e2c82069e50309c478f7fe127886.zip
Add a wycheproofTestGroupRunner interface
This allows us to use a simpler way of running the individual test groups and gets rid of an ugly mostly copy-pasted switch inside a closure.
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libcrypto/wycheproof/wycheproof.go98
1 files changed, 26 insertions, 72 deletions
diff --git a/src/regress/lib/libcrypto/wycheproof/wycheproof.go b/src/regress/lib/libcrypto/wycheproof/wycheproof.go
index d9eda15f07..ef88532a15 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.148 2023/11/06 15:07:57 tb Exp $ */ 1/* $OpenBSD: wycheproof.go,v 1.149 2023/11/06 15:14:52 tb 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>
@@ -506,6 +506,10 @@ type wycheproofTestGroupX25519 struct {
506 Tests []*wycheproofTestX25519 `json:"tests"` 506 Tests []*wycheproofTestX25519 `json:"tests"`
507} 507}
508 508
509type wycheproofTestGroupRunner interface {
510 run(string, testVariant) bool
511}
512
509type wycheproofTestVectors struct { 513type wycheproofTestVectors struct {
510 Algorithm string `json:"algorithm"` 514 Algorithm string `json:"algorithm"`
511 GeneratorVersion string `json:"generatorVersion"` 515 GeneratorVersion string `json:"generatorVersion"`
@@ -722,7 +726,7 @@ func runAesCbcPkcs5Test(ctx *C.EVP_CIPHER_CTX, wt *wycheproofTestAesCbcPkcs5) bo
722 return openSuccess && sealSuccess 726 return openSuccess && sealSuccess
723} 727}
724 728
725func runAesCbcPkcs5TestGroup(algorithm string, wtg *wycheproofTestGroupAesCbcPkcs5) bool { 729func (wtg *wycheproofTestGroupAesCbcPkcs5) run(algorithm string, variant testVariant) bool {
726 fmt.Printf("Running %v test group %v with IV size %d and key size %d...\n", 730 fmt.Printf("Running %v test group %v with IV size %d and key size %d...\n",
727 algorithm, wtg.Type, wtg.IVSize, wtg.KeySize) 731 algorithm, wtg.Type, wtg.IVSize, wtg.KeySize)
728 732
@@ -987,7 +991,7 @@ func runAesAeadTest(algorithm string, ctx *C.EVP_CIPHER_CTX, aead *C.EVP_AEAD, w
987 return openEvp && sealEvp && openAead && sealAead 991 return openEvp && sealEvp && openAead && sealAead
988} 992}
989 993
990func runAesAeadTestGroup(algorithm string, wtg *wycheproofTestGroupAesAead) bool { 994func (wtg *wycheproofTestGroupAesAead) run(algorithm string, variant testVariant) bool {
991 fmt.Printf("Running %v test group %v with IV size %d, key size %d and tag size %d...\n", 995 fmt.Printf("Running %v test group %v with IV size %d, key size %d and tag size %d...\n",
992 algorithm, wtg.Type, wtg.IVSize, wtg.KeySize, wtg.TagSize) 996 algorithm, wtg.Type, wtg.IVSize, wtg.KeySize, wtg.TagSize)
993 997
@@ -1109,7 +1113,7 @@ func runAesCmacTest(cipher *C.EVP_CIPHER, wt *wycheproofTestAesCmac) bool {
1109 return success 1113 return success
1110} 1114}
1111 1115
1112func runAesCmacTestGroup(algorithm string, wtg *wycheproofTestGroupAesCmac) bool { 1116func (wtg *wycheproofTestGroupAesCmac) run(algorithm string, variant testVariant) bool {
1113 fmt.Printf("Running %v test group %v with key size %d and tag size %d...\n", 1117 fmt.Printf("Running %v test group %v with key size %d and tag size %d...\n",
1114 algorithm, wtg.Type, wtg.KeySize, wtg.TagSize) 1118 algorithm, wtg.Type, wtg.KeySize, wtg.TagSize)
1115 var cipher *C.EVP_CIPHER 1119 var cipher *C.EVP_CIPHER
@@ -1291,7 +1295,7 @@ func runChaCha20Poly1305Test(algorithm string, wt *wycheproofTestAead) bool {
1291 return openSuccess && sealSuccess 1295 return openSuccess && sealSuccess
1292} 1296}
1293 1297
1294func runChaCha20Poly1305TestGroup(algorithm string, wtg *wycheproofTestGroupChaCha) bool { 1298func (wtg *wycheproofTestGroupChaCha) run(algorithm string, variant testVariant) bool {
1295 // ChaCha20-Poly1305 currently only supports nonces of length 12 (96 bits) 1299 // ChaCha20-Poly1305 currently only supports nonces of length 12 (96 bits)
1296 if algorithm == "CHACHA20-POLY1305" && wtg.IVSize != 96 { 1300 if algorithm == "CHACHA20-POLY1305" && wtg.IVSize != 96 {
1297 return true 1301 return true
@@ -1401,7 +1405,7 @@ func runDSATest(dsa *C.DSA, md *C.EVP_MD, variant testVariant, wt *wycheproofTes
1401 return success 1405 return success
1402} 1406}
1403 1407
1404func runDSATestGroup(algorithm string, variant testVariant, wtg *wycheproofTestGroupDSA) bool { 1408func (wtg *wycheproofTestGroupDSA) run(algorithm string, variant testVariant) bool {
1405 fmt.Printf("Running %v test group %v, key size %d and %v...\n", 1409 fmt.Printf("Running %v test group %v, key size %d and %v...\n",
1406 algorithm, wtg.Type, wtg.Key.KeySize, wtg.SHA) 1410 algorithm, wtg.Type, wtg.Key.KeySize, wtg.SHA)
1407 1411
@@ -1608,7 +1612,7 @@ func runECDHTest(nid int, variant testVariant, wt *wycheproofTestECDH) bool {
1608 return success 1612 return success
1609} 1613}
1610 1614
1611func runECDHTestGroup(algorithm string, variant testVariant, wtg *wycheproofTestGroupECDH) bool { 1615func (wtg *wycheproofTestGroupECDH) run(algorithm string, variant testVariant) bool {
1612 fmt.Printf("Running %v test group %v with curve %v and %v encoding...\n", 1616 fmt.Printf("Running %v test group %v with curve %v and %v encoding...\n",
1613 algorithm, wtg.Type, wtg.Curve, wtg.Encoding) 1617 algorithm, wtg.Type, wtg.Curve, wtg.Encoding)
1614 1618
@@ -1720,7 +1724,7 @@ func runECDHWebCryptoTest(nid int, wt *wycheproofTestECDHWebCrypto) bool {
1720 return success 1724 return success
1721} 1725}
1722 1726
1723func runECDHWebCryptoTestGroup(algorithm string, wtg *wycheproofTestGroupECDHWebCrypto) bool { 1727func (wtg *wycheproofTestGroupECDHWebCrypto) run(algorithm string, variant testVariant) bool {
1724 fmt.Printf("Running %v test group %v with curve %v and %v encoding...\n", 1728 fmt.Printf("Running %v test group %v with curve %v and %v encoding...\n",
1725 algorithm, wtg.Type, wtg.Curve, wtg.Encoding) 1729 algorithm, wtg.Type, wtg.Curve, wtg.Encoding)
1726 1730
@@ -1784,7 +1788,7 @@ func runECDSATest(ecKey *C.EC_KEY, md *C.EVP_MD, nid int, variant testVariant, w
1784 return success 1788 return success
1785} 1789}
1786 1790
1787func runECDSATestGroup(algorithm string, variant testVariant, wtg *wycheproofTestGroupECDSA) bool { 1791func (wtg *wycheproofTestGroupECDSA) run(algorithm string, variant testVariant) bool {
1788 fmt.Printf("Running %v test group %v with curve %v, key size %d and %v...\n", 1792 fmt.Printf("Running %v test group %v with curve %v, key size %d and %v...\n",
1789 algorithm, wtg.Type, wtg.Key.Curve, wtg.Key.KeySize, wtg.SHA) 1793 algorithm, wtg.Type, wtg.Key.Curve, wtg.Key.KeySize, wtg.SHA)
1790 1794
@@ -1884,7 +1888,7 @@ func encodeECDSAWebCryptoSig(wtSig string) (*C.uchar, C.int) {
1884 return cDer, derLen 1888 return cDer, derLen
1885} 1889}
1886 1890
1887func runECDSAWebCryptoTestGroup(algorithm string, wtg *wycheproofTestGroupECDSAWebCrypto) bool { 1891func (wtg *wycheproofTestGroupECDSAWebCrypto) run(algorithm string, variant testVariant) bool {
1888 fmt.Printf("Running %v test group %v with curve %v, key size %d and %v...\n", 1892 fmt.Printf("Running %v test group %v with curve %v, key size %d and %v...\n",
1889 algorithm, wtg.Type, wtg.Key.Curve, wtg.Key.KeySize, wtg.SHA) 1893 algorithm, wtg.Type, wtg.Key.Curve, wtg.Key.KeySize, wtg.SHA)
1890 1894
@@ -1980,7 +1984,7 @@ func runEdDSATest(pkey *C.EVP_PKEY, wt *wycheproofTestEdDSA) bool {
1980 return success 1984 return success
1981} 1985}
1982 1986
1983func runEdDSATestGroup(algorithm string, wtg *wycheproofTestGroupEdDSA) bool { 1987func (wtg *wycheproofTestGroupEdDSA) run(algorithm string, variant testVariant) bool {
1984 fmt.Printf("Running %v test group %v...\n", algorithm, wtg.Type) 1988 fmt.Printf("Running %v test group %v...\n", algorithm, wtg.Type)
1985 1989
1986 if wtg.Key.Curve != "edwards25519" || wtg.Key.KeySize != 255 { 1990 if wtg.Key.Curve != "edwards25519" || wtg.Key.KeySize != 255 {
@@ -2090,7 +2094,7 @@ func runHkdfTest(md *C.EVP_MD, wt *wycheproofTestHkdf) bool {
2090 return wt.Result == "valid" 2094 return wt.Result == "valid"
2091} 2095}
2092 2096
2093func runHkdfTestGroup(algorithm string, wtg *wycheproofTestGroupHkdf) bool { 2097func (wtg *wycheproofTestGroupHkdf) run(algorithm string, variant testVariant) bool {
2094 fmt.Printf("Running %v test group %v with key size %d...\n", algorithm, wtg.Type, wtg.KeySize) 2098 fmt.Printf("Running %v test group %v with key size %d...\n", algorithm, wtg.Type, wtg.KeySize)
2095 md, err := hashEvpMdFromString(strings.TrimPrefix(algorithm, "HKDF-")) 2099 md, err := hashEvpMdFromString(strings.TrimPrefix(algorithm, "HKDF-"))
2096 if err != nil { 2100 if err != nil {
@@ -2160,7 +2164,7 @@ func runHmacTest(md *C.EVP_MD, tagBytes int, wt *wycheproofTestHmac) bool {
2160 return success 2164 return success
2161} 2165}
2162 2166
2163func runHmacTestGroup(algorithm string, wtg *wycheproofTestGroupHmac) bool { 2167func (wtg *wycheproofTestGroupHmac) run(algorithm string, variant testVariant) bool {
2164 fmt.Printf("Running %v test group %v with key size %d and tag size %d...\n", algorithm, wtg.Type, wtg.KeySize, wtg.TagSize) 2168 fmt.Printf("Running %v test group %v with key size %d and tag size %d...\n", algorithm, wtg.Type, wtg.KeySize, wtg.TagSize)
2165 prefix := "SHA-" 2169 prefix := "SHA-"
2166 if strings.HasPrefix(algorithm, "HMACSHA3-") { 2170 if strings.HasPrefix(algorithm, "HMACSHA3-") {
@@ -2273,7 +2277,7 @@ func runKWTest(keySize int, wt *wycheproofTestKW) bool {
2273 return wrapSuccess && unwrapSuccess 2277 return wrapSuccess && unwrapSuccess
2274} 2278}
2275 2279
2276func runKWTestGroup(algorithm string, wtg *wycheproofTestGroupKW) bool { 2280func (wtg *wycheproofTestGroupKW) run(algorithm string, variant testVariant) bool {
2277 fmt.Printf("Running %v test group %v with key size %d...\n", 2281 fmt.Printf("Running %v test group %v with key size %d...\n",
2278 algorithm, wtg.Type, wtg.KeySize) 2282 algorithm, wtg.Type, wtg.KeySize)
2279 2283
@@ -2303,7 +2307,7 @@ func runPrimalityTest(wt *wycheproofTestPrimality) bool {
2303 return success 2307 return success
2304} 2308}
2305 2309
2306func runPrimalityTestGroup(algorithm string, wtg *wycheproofTestGroupPrimality) bool { 2310func (wtg *wycheproofTestGroupPrimality) run(algorithm string, variant testVariant) bool {
2307 fmt.Printf("Running %v test group...\n", algorithm) 2311 fmt.Printf("Running %v test group...\n", algorithm)
2308 2312
2309 success := true 2313 success := true
@@ -2378,7 +2382,7 @@ func runRsaesOaepTest(rsa *C.RSA, sha *C.EVP_MD, mgfSha *C.EVP_MD, wt *wycheproo
2378 return success 2382 return success
2379} 2383}
2380 2384
2381func runRsaesOaepTestGroup(algorithm string, wtg *wycheproofTestGroupRsaesOaep) bool { 2385func (wtg *wycheproofTestGroupRsaesOaep) run(algorithm string, variant testVariant) bool {
2382 fmt.Printf("Running %v test group %v with key size %d MGF %v and %v...\n", 2386 fmt.Printf("Running %v test group %v with key size %d MGF %v and %v...\n",
2383 algorithm, wtg.Type, wtg.KeySize, wtg.MGFSHA, wtg.SHA) 2387 algorithm, wtg.Type, wtg.KeySize, wtg.MGFSHA, wtg.SHA)
2384 2388
@@ -2480,7 +2484,7 @@ func runRsaesPkcs1Test(rsa *C.RSA, wt *wycheproofTestRsaes) bool {
2480 return success 2484 return success
2481} 2485}
2482 2486
2483func runRsaesPkcs1TestGroup(algorithm string, wtg *wycheproofTestGroupRsaesPkcs1) bool { 2487func (wtg *wycheproofTestGroupRsaesPkcs1) run(algorithm string, variant testVariant) bool {
2484 fmt.Printf("Running %v test group %v with key size %d...\n", algorithm, wtg.Type, wtg.KeySize) 2488 fmt.Printf("Running %v test group %v with key size %d...\n", algorithm, wtg.Type, wtg.KeySize)
2485 rsa := C.RSA_new() 2489 rsa := C.RSA_new()
2486 if rsa == nil { 2490 if rsa == nil {
@@ -2582,7 +2586,7 @@ func runRsassaTest(rsa *C.RSA, sha *C.EVP_MD, mgfSha *C.EVP_MD, sLen int, wt *wy
2582 return success 2586 return success
2583} 2587}
2584 2588
2585func runRsassaTestGroup(algorithm string, wtg *wycheproofTestGroupRsassa) bool { 2589func (wtg *wycheproofTestGroupRsassa) run(algorithm string, variant testVariant) bool {
2586 fmt.Printf("Running %v test group %v with key size %d and %v...\n", 2590 fmt.Printf("Running %v test group %v with key size %d and %v...\n",
2587 algorithm, wtg.Type, wtg.KeySize, wtg.SHA) 2591 algorithm, wtg.Type, wtg.KeySize, wtg.SHA)
2588 rsa := C.RSA_new() 2592 rsa := C.RSA_new()
@@ -2666,7 +2670,7 @@ func runRSATest(rsa *C.RSA, md *C.EVP_MD, nid int, wt *wycheproofTestRSA) bool {
2666 return success 2670 return success
2667} 2671}
2668 2672
2669func runRSATestGroup(algorithm string, wtg *wycheproofTestGroupRSA) bool { 2673func (wtg *wycheproofTestGroupRSA) run(algorithm string, variant testVariant) bool {
2670 fmt.Printf("Running %v test group %v with key size %d and %v...\n", 2674 fmt.Printf("Running %v test group %v with key size %d and %v...\n",
2671 algorithm, wtg.Type, wtg.KeySize, wtg.SHA) 2675 algorithm, wtg.Type, wtg.KeySize, wtg.SHA)
2672 2676
@@ -2749,7 +2753,7 @@ func runX25519Test(wt *wycheproofTestX25519) bool {
2749 return success 2753 return success
2750} 2754}
2751 2755
2752func runX25519TestGroup(algorithm string, wtg *wycheproofTestGroupX25519) bool { 2756func (wtg *wycheproofTestGroupX25519) run(algorithm string, variant testVariant) bool {
2753 fmt.Printf("Running %v test group with curve %v...\n", algorithm, wtg.Curve) 2757 fmt.Printf("Running %v test group with curve %v...\n", algorithm, wtg.Curve)
2754 2758
2755 success := true 2759 success := true
@@ -2777,7 +2781,7 @@ func runTestVectors(path string, variant testVariant) bool {
2777 for _, tg := range wtv.TestGroups { 2781 for _, tg := range wtv.TestGroups {
2778 testGroupJSON := tg 2782 testGroupJSON := tg
2779 testc.runTest(func() bool { 2783 testc.runTest(func() bool {
2780 var wtg interface{} 2784 var wtg wycheproofTestGroupRunner
2781 switch wtv.Algorithm { 2785 switch wtv.Algorithm {
2782 case "AES-CBC-PKCS5": 2786 case "AES-CBC-PKCS5":
2783 wtg = &wycheproofTestGroupAesCbcPkcs5{} 2787 wtg = &wycheproofTestGroupAesCbcPkcs5{}
@@ -2833,57 +2837,7 @@ func runTestVectors(path string, variant testVariant) bool {
2833 if err := json.Unmarshal(testGroupJSON, wtg); err != nil { 2837 if err := json.Unmarshal(testGroupJSON, wtg); err != nil {
2834 log.Fatalf("Failed to unmarshal test groups JSON: %v", err) 2838 log.Fatalf("Failed to unmarshal test groups JSON: %v", err)
2835 } 2839 }
2836 switch wtv.Algorithm { 2840 return wtg.run(wtv.Algorithm, variant)
2837 case "AES-CBC-PKCS5":
2838 return runAesCbcPkcs5TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAesCbcPkcs5))
2839 case "AES-CCM":
2840 return runAesAeadTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAesAead))
2841 case "AES-CMAC":
2842 return runAesCmacTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAesCmac))
2843 case "AES-GCM":
2844 return runAesAeadTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupAesAead))
2845 case "CHACHA20-POLY1305", "XCHACHA20-POLY1305":
2846 return runChaCha20Poly1305TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupChaCha))
2847 case "DSA":
2848 return runDSATestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupDSA))
2849 case "ECDH":
2850 switch variant {
2851 case Webcrypto:
2852 return runECDHWebCryptoTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupECDHWebCrypto))
2853 default:
2854 return runECDHTestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupECDH))
2855 }
2856 case "ECDSA":
2857 switch variant {
2858 case Webcrypto:
2859 return runECDSAWebCryptoTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupECDSAWebCrypto))
2860 default:
2861 return runECDSATestGroup(wtv.Algorithm, variant, wtg.(*wycheproofTestGroupECDSA))
2862 }
2863 case "EDDSA":
2864 return runEdDSATestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupEdDSA))
2865 case "HKDF-SHA-1", "HKDF-SHA-256", "HKDF-SHA-384", "HKDF-SHA-512":
2866 return runHkdfTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHkdf))
2867 case "HMACSHA1", "HMACSHA224", "HMACSHA256", "HMACSHA384", "HMACSHA512", "HMACSHA3-224", "HMACSHA3-256", "HMACSHA3-384", "HMACSHA3-512":
2868 return runHmacTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHmac))
2869 case "KW":
2870 return runKWTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupKW))
2871 case "PrimalityTest":
2872 return runPrimalityTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupPrimality))
2873 case "RSAES-OAEP":
2874 return runRsaesOaepTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsaesOaep))
2875 case "RSAES-PKCS1-v1_5":
2876 return runRsaesPkcs1TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsaesPkcs1))
2877 case "RSASSA-PSS":
2878 return runRsassaTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRsassa))
2879 case "RSASSA-PKCS1-v1_5", "RSASig":
2880 return runRSATestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupRSA))
2881 case "XDH", "X25519":
2882 return runX25519TestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupX25519))
2883 default:
2884 log.Fatalf("Unknown test vector algorithm %q", wtv.Algorithm)
2885 return false
2886 }
2887 }) 2841 })
2888 } 2842 }
2889 for _ = range wtv.TestGroups { 2843 for _ = range wtv.TestGroups {