summaryrefslogtreecommitdiff
path: root/src/regress/lib
diff options
context:
space:
mode:
authortb <>2019-12-14 09:39:30 +0000
committertb <>2019-12-14 09:39:30 +0000
commitfb685d5184e992bcca5cf614af18a626f1c23189 (patch)
treeb45fe5f690773e414501bca267e131ab350291e8 /src/regress/lib
parentb516acc04822f037490ca0b19d35bf68d99d0275 (diff)
downloadopenbsd-fb685d5184e992bcca5cf614af18a626f1c23189.tar.gz
openbsd-fb685d5184e992bcca5cf614af18a626f1c23189.tar.bz2
openbsd-fb685d5184e992bcca5cf614af18a626f1c23189.zip
Run Wycheproof HMAC test vectors against libcrypto.
Diffstat (limited to 'src/regress/lib')
-rw-r--r--src/regress/lib/libcrypto/wycheproof/wycheproof.go97
1 files changed, 96 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/wycheproof/wycheproof.go b/src/regress/lib/libcrypto/wycheproof/wycheproof.go
index c1c35ba55a..a98640548f 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.113 2019/12/09 19:55:03 tb Exp $ */ 1/* $OpenBSD: wycheproof.go,v 1.114 2019/12/14 09:39:30 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 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2018, 2019 Theo Buehler <tb@openbsd.org>
@@ -34,6 +34,7 @@ package main
34#include <openssl/ecdsa.h> 34#include <openssl/ecdsa.h>
35#include <openssl/evp.h> 35#include <openssl/evp.h>
36#include <openssl/hkdf.h> 36#include <openssl/hkdf.h>
37#include <openssl/hmac.h>
37#include <openssl/objects.h> 38#include <openssl/objects.h>
38#include <openssl/pem.h> 39#include <openssl/pem.h>
39#include <openssl/x509.h> 40#include <openssl/x509.h>
@@ -284,6 +285,23 @@ type wycheproofTestGroupHkdf struct {
284 Tests []*wycheproofTestHkdf `json:"tests"` 285 Tests []*wycheproofTestHkdf `json:"tests"`
285} 286}
286 287
288type wycheproofTestHmac struct {
289 TCID int `json:"tcId"`
290 Comment string `json:"comment"`
291 Key string `json:"key"`
292 Msg string `json:"msg"`
293 Tag string `json:"tag"`
294 Result string `json:"result"`
295 Flags []string `json:"flags"`
296}
297
298type wycheproofTestGroupHmac struct {
299 KeySize int `json:"keySize"`
300 TagSize int `json:"tagSize"`
301 Type string `json:"type"`
302 Tests []*wycheproofTestHmac `json:"tests"`
303}
304
287type wycheproofTestKW struct { 305type wycheproofTestKW struct {
288 TCID int `json:"tcId"` 306 TCID int `json:"tcId"`
289 Comment string `json:"comment"` 307 Comment string `json:"comment"`
@@ -1936,6 +1954,76 @@ func runHkdfTestGroup(algorithm string, wtg *wycheproofTestGroupHkdf) bool {
1936 return success 1954 return success
1937} 1955}
1938 1956
1957func runHmacTest(md *C.EVP_MD, tagBytes int, wt *wycheproofTestHmac) bool {
1958 key, err := hex.DecodeString(wt.Key)
1959 if err != nil {
1960 log.Fatalf("failed to decode key %q: %v", wt.Key, err)
1961 }
1962
1963 msg, err := hex.DecodeString(wt.Msg)
1964 if err != nil {
1965 log.Fatalf("failed to decode msg %q: %v", wt.Msg, err)
1966 }
1967
1968 keyLen, msgLen := len(key), len(msg)
1969
1970 if keyLen == 0 {
1971 key = append(key, 0)
1972 }
1973
1974 if msgLen == 0 {
1975 msg = append(msg, 0)
1976 }
1977
1978 got := make([]byte, C.EVP_MAX_MD_SIZE)
1979 var gotLen C.uint
1980
1981 ret := C.HMAC(md, unsafe.Pointer(&key[0]), C.int(keyLen), (*C.uchar)(unsafe.Pointer(&msg[0])), C.size_t(msgLen), (*C.uchar)(unsafe.Pointer(&got[0])), &gotLen)
1982
1983 success := true
1984 if ret == nil {
1985 if wt.Result != "invalid" {
1986 success = false
1987 fmt.Printf("FAIL: Test case %d (%q) %v - HMAC: got nil, want %v\n", wt.TCID, wt.Comment, wt.Flags, wt.Result)
1988 }
1989 return success
1990 }
1991
1992 if int(gotLen) < tagBytes {
1993 fmt.Printf("FAIL: Test case %d (%q) %v - HMAC length: got %d, want %d, expected %v\n", wt.TCID, wt.Comment, wt.Flags, gotLen, tagBytes, wt.Result)
1994 return false
1995 }
1996
1997 tag, err := hex.DecodeString(wt.Tag)
1998 if err != nil {
1999 log.Fatalf("failed to decode tag %q: %v", wt.Tag, err)
2000 }
2001
2002 success = bytes.Equal(got[:tagBytes], tag) == (wt.Result == "valid")
2003
2004 if !success {
2005 fmt.Printf("FAIL: Test case %d (%q) %v - got %v want %v\n", wt.TCID, wt.Comment, wt.Flags, success, wt.Result)
2006 }
2007
2008 return success
2009}
2010
2011func runHmacTestGroup(algorithm string, wtg *wycheproofTestGroupHmac) bool {
2012 fmt.Printf("Running %v test group %v with key size %d and tag size %d...\n", algorithm, wtg.Type, wtg.KeySize, wtg.TagSize)
2013 md, err := hashEvpMdFromString("SHA-" + strings.TrimPrefix(algorithm, "HMACSHA"))
2014 if err != nil {
2015 log.Fatalf("Failed to get hash: %v", err)
2016 }
2017
2018 success := true
2019 for _, wt := range wtg.Tests {
2020 if !runHmacTest(md, wtg.TagSize / 8, wt) {
2021 success = false
2022 }
2023 }
2024 return success
2025}
2026
1939func runKWTestWrap(keySize int, key []byte, keyLen int, msg []byte, msgLen int, ct []byte, ctLen int, wt *wycheproofTestKW) bool { 2027func runKWTestWrap(keySize int, key []byte, keyLen int, msg []byte, msgLen int, ct []byte, ctLen int, wt *wycheproofTestKW) bool {
1940 var aesKey C.AES_KEY 2028 var aesKey C.AES_KEY
1941 2029
@@ -2508,6 +2596,8 @@ func runTestVectors(path string, variant testVariant) bool {
2508 } 2596 }
2509 case "HKDF-SHA-1", "HKDF-SHA-256", "HKDF-SHA-384", "HKDF-SHA-512": 2597 case "HKDF-SHA-1", "HKDF-SHA-256", "HKDF-SHA-384", "HKDF-SHA-512":
2510 wtg = &wycheproofTestGroupHkdf{} 2598 wtg = &wycheproofTestGroupHkdf{}
2599 case "HMACSHA1", "HMACSHA224", "HMACSHA256", "HMACSHA384", "HMACSHA512":
2600 wtg = &wycheproofTestGroupHmac{}
2511 case "KW": 2601 case "KW":
2512 wtg = &wycheproofTestGroupKW{} 2602 wtg = &wycheproofTestGroupKW{}
2513 case "RSAES-OAEP": 2603 case "RSAES-OAEP":
@@ -2581,6 +2671,10 @@ func runTestVectors(path string, variant testVariant) bool {
2581 if !runHkdfTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHkdf)) { 2671 if !runHkdfTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHkdf)) {
2582 success = false 2672 success = false
2583 } 2673 }
2674 case "HMACSHA1", "HMACSHA224", "HMACSHA256", "HMACSHA384", "HMACSHA512":
2675 if !runHmacTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupHmac)) {
2676 success = false
2677 }
2584 case "KW": 2678 case "KW":
2585 if !runKWTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupKW)) { 2679 if !runKWTestGroup(wtv.Algorithm, wtg.(*wycheproofTestGroupKW)) {
2586 success = false 2680 success = false
@@ -2646,6 +2740,7 @@ func main() {
2646 {"ECDSA P1363", "ecdsa_*_p1363_test.json", P1363}, 2740 {"ECDSA P1363", "ecdsa_*_p1363_test.json", P1363},
2647 {"ECDSA webcrypto", "ecdsa_webcrypto_test.json", Webcrypto}, 2741 {"ECDSA webcrypto", "ecdsa_webcrypto_test.json", Webcrypto},
2648 {"HKDF", "hkdf_sha*_test.json", Normal}, 2742 {"HKDF", "hkdf_sha*_test.json", Normal},
2743 {"HMAC", "hmac_sha*_test.json", Normal},
2649 {"KW", "kw_test.json", Normal}, 2744 {"KW", "kw_test.json", Normal},
2650 {"RSA", "rsa_*test.json", Normal}, 2745 {"RSA", "rsa_*test.json", Normal},
2651 {"X25519", "x25519_test.json", Normal}, 2746 {"X25519", "x25519_test.json", Normal},