-/* $OpenBSD: wycheproof.go,v 1.135 2023/03/01 12:34:12 tb Exp $ */
+/* $OpenBSD: wycheproof.go,v 1.136 2023/03/08 05:17:33 jsing Exp $ */
/*
* Copyright (c) 2018 Joel Sing <jsing@openbsd.org>
* Copyright (c) 2018,2019,2022 Theo Buehler <tb@openbsd.org>
"encoding/base64"
"encoding/hex"
"encoding/json"
- "flag"
"fmt"
"hash"
"io/ioutil"
"os"
"path/filepath"
"regexp"
- "sort"
"strings"
"unsafe"
)
"SHA-512": C.NID_sha512,
}
-func gatherAcceptableStatistics(testcase int, comment string, flags []string) {
- fmt.Printf("AUDIT: Test case %d (%q) %v\n", testcase, comment, flags)
-
- if comment == "" {
- acceptableComments["No comment"]++
- } else {
- acceptableComments[comment]++
- }
-
- if len(flags) == 0 {
- acceptableFlags["NoFlag"]++
- } else {
- for _, flag := range flags {
- acceptableFlags[flag]++
- }
- }
-}
-
-func printAcceptableStatistics() {
- fmt.Printf("\nComment statistics:\n")
-
- var comments []string
- for comment := range acceptableComments {
- comments = append(comments, comment)
- }
- sort.Strings(comments)
- for _, comment := range comments {
- prcomment := comment
- if len(comment) > 45 {
- prcomment = comment[0:42] + "..."
- }
- fmt.Printf("%-45v %5d\n", prcomment, acceptableComments[comment])
- }
-
- fmt.Printf("\nFlag statistics:\n")
- var flags []string
- for flag := range acceptableFlags {
- flags = append(flags, flag)
- }
- sort.Strings(flags)
- for _, flag := range flags {
- fmt.Printf("%-45v %5d\n", flag, acceptableFlags[flag])
- }
-}
-
func nidFromString(ns string) (int, error) {
nid, ok := nids[ns]
if ok {
success := false
if bytes.Equal(openedMsg, out) == (wt.Result != "invalid") {
success = true
- if acceptableAudit && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
} else {
fmt.Printf("FAIL: Test case %d (%q) [%v] %v - msg match: %t; want %v\n",
wt.TCID, wt.Comment, action, wt.Flags, bytes.Equal(openedMsg, out), wt.Result)
wt.TCID, wt.Comment, action, wt.Flags, ret, wt.Result)
success = false
}
- if acceptableAudit && bytes.Equal(tagOut, tag) && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
}
return success
}
success := false
if bytes.Equal(openedMsg, msg) == (wt.Result != "invalid") {
- if acceptableAudit && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
success = true
} else {
fmt.Printf("FAIL: Test case %d (%q) %v - msg match: %t; want %v\n",
success := false
if (bytes.Equal(sealedCt, ct) && bytes.Equal(sealedTag, tag)) == (wt.Result != "invalid") {
- if acceptableAudit && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
success = true
} else {
fmt.Printf("FAIL: Test case %d (%q) %v - EVP_AEAD_CTX_seal() = %d, ct match: %t, tag match: %t; want %v\n",
wt.TCID, wt.Comment, wt.Flags, wt.Result)
success = false
}
- if acceptableAudit && success && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
return success
}
wt.TCID, wt.Comment, wt.Flags, wt.Result)
success = false
}
- if acceptableAudit && success && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
return success
}
wt.TCID, wt.Comment, wt.Flags, int(ret), wt.Result)
success = false
}
- if acceptableAudit && ret == 1 && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
return success
}
ret = C.AES_wrap_key((*C.AES_KEY)(unsafe.Pointer(&aesKey)), nil, (*C.uchar)(unsafe.Pointer(&out[0])), (*C.uchar)(unsafe.Pointer(&out[0])), (C.uint)(msgLen))
success := false
if ret == C.int(len(out)) && bytes.Equal(out, ct) {
- if acceptableAudit && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
if wt.Result != "invalid" {
success = true
}
ret = C.AES_unwrap_key((*C.AES_KEY)(unsafe.Pointer(&aesKey)), nil, (*C.uchar)(unsafe.Pointer(&out[0])), (*C.uchar)(unsafe.Pointer(&out[0])), (C.uint)(ctLen))
success := false
if ret == C.int(ctLen-8) && bytes.Equal(out[0:ret], msg[0:ret]) {
- if acceptableAudit && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
if wt.Result != "invalid" {
success = true
}
if ret == 1 && (wt.Result == "valid" || wt.Result == "acceptable") {
// All acceptable cases that pass use SHA-1 and are flagged:
// "WeakHash" : "The key for this test vector uses a weak hash function."
- if acceptableAudit && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
success = true
} else if ret == 0 && (wt.Result == "invalid" || wt.Result == "acceptable") {
success = true
wt.TCID, wt.Comment, wt.Flags, int(ret), wt.Result)
success = false
}
- if acceptableAudit && ret == 1 && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
return success
}
wt.TCID, wt.Comment, wt.Flags, wt.Result)
success = false
}
- if acceptableAudit && result && wt.Result == "acceptable" {
- gatherAcceptableStatistics(wt.TCID, wt.Comment, wt.Flags)
- }
return success
}
os.Exit(0)
}
- flag.BoolVar(&acceptableAudit, "v", false, "audit acceptable cases")
- flag.Parse()
-
- acceptableComments = make(map[string]int)
- acceptableFlags = make(map[string]int)
-
tests := []struct {
name string
pattern string
}
}
- if acceptableAudit {
- printAcceptableStatistics()
- }
-
if !success {
os.Exit(1)
}