diff options
Diffstat (limited to '')
-rw-r--r-- | src/regress/lib/libcrypto/wycheproof/wycheproof.go | 468 |
1 files changed, 78 insertions, 390 deletions
diff --git a/src/regress/lib/libcrypto/wycheproof/wycheproof.go b/src/regress/lib/libcrypto/wycheproof/wycheproof.go index 6f33fe56c6..dea2f2e5f8 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.154 2023/11/07 16:37:02 tb Exp $ */ | 1 | /* $OpenBSD: wycheproof.go,v 1.155 2023/11/07 16:46:12 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> |
@@ -709,24 +709,33 @@ func aeadAes(size int) (*C.EVP_AEAD, error) { | |||
709 | return nil, fmt.Errorf("invalid key size: %d", size) | 709 | return nil, fmt.Errorf("invalid key size: %d", size) |
710 | } | 710 | } |
711 | 711 | ||
712 | func hashEvpDigestMessage(md *C.EVP_MD, msg []byte) ([]byte, int, error) { | 712 | func mustHashHexMessage(md *C.EVP_MD, message string) ([]byte, int) { |
713 | size := C.EVP_MD_size(md) | 713 | size := C.EVP_MD_size(md) |
714 | if size <= 0 || size > C.EVP_MAX_MD_SIZE { | 714 | if size <= 0 || size > C.EVP_MAX_MD_SIZE { |
715 | return nil, 0, fmt.Errorf("unexpected MD size %d", size) | 715 | log.Fatalf("unexpected MD size %d", size) |
716 | } | 716 | } |
717 | 717 | ||
718 | msgLen := len(msg) | 718 | msg, msgLen := mustDecodeHexString(message, "for hex message digest") |
719 | if msgLen == 0 { | ||
720 | msg = append(msg, 0) | ||
721 | } | ||
722 | 719 | ||
723 | digest := make([]byte, size) | 720 | digest := make([]byte, size) |
724 | 721 | ||
725 | if C.EVP_Digest(unsafe.Pointer(&msg[0]), C.size_t(msgLen), (*C.uchar)(unsafe.Pointer(&digest[0])), nil, md, nil) != 1 { | 722 | if C.EVP_Digest(unsafe.Pointer(&msg[0]), C.size_t(msgLen), (*C.uchar)(unsafe.Pointer(&digest[0])), nil, md, nil) != 1 { |
726 | return nil, 0, fmt.Errorf("EVP_Digest failed") | 723 | log.Fatalf("EVP_Digest failed") |
727 | } | 724 | } |
728 | 725 | ||
729 | return digest, int(size), nil | 726 | return digest, int(size) |
727 | } | ||
728 | |||
729 | func mustDecodeHexString(str, descr string) (out []byte, outLen int) { | ||
730 | out, err := hex.DecodeString(str) | ||
731 | if err != nil { | ||
732 | log.Fatalf("Failed to decode %s %q: %v", descr, str, err) | ||
733 | } | ||
734 | outLen = len(out) | ||
735 | if outLen == 0 { | ||
736 | out = append(out, 0) | ||
737 | } | ||
738 | return out, outLen | ||
730 | } | 739 | } |
731 | 740 | ||
732 | func checkAesCbcPkcs5(ctx *C.EVP_CIPHER_CTX, doEncrypt int, key []byte, keyLen int, | 741 | func checkAesCbcPkcs5(ctx *C.EVP_CIPHER_CTX, doEncrypt int, key []byte, keyLen int, |
@@ -790,37 +799,10 @@ func checkAesCbcPkcs5(ctx *C.EVP_CIPHER_CTX, doEncrypt int, key []byte, keyLen i | |||
790 | } | 799 | } |
791 | 800 | ||
792 | func runAesCbcPkcs5Test(ctx *C.EVP_CIPHER_CTX, wt *wycheproofTestAesCbcPkcs5) bool { | 801 | func runAesCbcPkcs5Test(ctx *C.EVP_CIPHER_CTX, wt *wycheproofTestAesCbcPkcs5) bool { |
793 | key, err := hex.DecodeString(wt.Key) | 802 | key, keyLen := mustDecodeHexString(wt.Key, "key") |
794 | if err != nil { | 803 | iv, ivLen := mustDecodeHexString(wt.IV, "iv") |
795 | log.Fatalf("Failed to decode key %q: %v", wt.Key, err) | 804 | ct, ctLen := mustDecodeHexString(wt.CT, "ct") |
796 | } | 805 | msg, msgLen := mustDecodeHexString(wt.Msg, "message") |
797 | iv, err := hex.DecodeString(wt.IV) | ||
798 | if err != nil { | ||
799 | log.Fatalf("Failed to decode IV %q: %v", wt.IV, err) | ||
800 | } | ||
801 | ct, err := hex.DecodeString(wt.CT) | ||
802 | if err != nil { | ||
803 | log.Fatalf("Failed to decode CT %q: %v", wt.CT, err) | ||
804 | } | ||
805 | msg, err := hex.DecodeString(wt.Msg) | ||
806 | if err != nil { | ||
807 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
808 | } | ||
809 | |||
810 | keyLen, ivLen, ctLen, msgLen := len(key), len(iv), len(ct), len(msg) | ||
811 | |||
812 | if keyLen == 0 { | ||
813 | key = append(key, 0) | ||
814 | } | ||
815 | if ivLen == 0 { | ||
816 | iv = append(iv, 0) | ||
817 | } | ||
818 | if ctLen == 0 { | ||
819 | ct = append(ct, 0) | ||
820 | } | ||
821 | if msgLen == 0 { | ||
822 | msg = append(msg, 0) | ||
823 | } | ||
824 | 806 | ||
825 | openSuccess := checkAesCbcPkcs5(ctx, 0, key, keyLen, iv, ivLen, ct, ctLen, msg, msgLen, wt) | 807 | openSuccess := checkAesCbcPkcs5(ctx, 0, key, keyLen, iv, ivLen, ct, ctLen, msg, msgLen, wt) |
826 | sealSuccess := checkAesCbcPkcs5(ctx, 1, key, keyLen, iv, ivLen, msg, msgLen, ct, ctLen, wt) | 808 | sealSuccess := checkAesCbcPkcs5(ctx, 1, key, keyLen, iv, ivLen, msg, msgLen, ct, ctLen, wt) |
@@ -997,56 +979,12 @@ func checkAesAead(algorithm string, ctx *C.EVP_CIPHER_CTX, doEncrypt int, | |||
997 | } | 979 | } |
998 | 980 | ||
999 | func runAesAeadTest(algorithm string, ctx *C.EVP_CIPHER_CTX, aead *C.EVP_AEAD, wt *wycheproofTestAead) bool { | 981 | func runAesAeadTest(algorithm string, ctx *C.EVP_CIPHER_CTX, aead *C.EVP_AEAD, wt *wycheproofTestAead) bool { |
1000 | key, err := hex.DecodeString(wt.Key) | 982 | key, keyLen := mustDecodeHexString(wt.Key, "key") |
1001 | if err != nil { | 983 | iv, ivLen := mustDecodeHexString(wt.IV, "iv") |
1002 | log.Fatalf("Failed to decode key %q: %v", wt.Key, err) | 984 | aad, aadLen := mustDecodeHexString(wt.AAD, "aad") |
1003 | } | 985 | msg, msgLen := mustDecodeHexString(wt.Msg, "msg") |
1004 | 986 | ct, ctLen := mustDecodeHexString(wt.CT, "CT") | |
1005 | iv, err := hex.DecodeString(wt.IV) | 987 | tag, tagLen := mustDecodeHexString(wt.Tag, "tag") |
1006 | if err != nil { | ||
1007 | log.Fatalf("Failed to decode IV %q: %v", wt.IV, err) | ||
1008 | } | ||
1009 | |||
1010 | aad, err := hex.DecodeString(wt.AAD) | ||
1011 | if err != nil { | ||
1012 | log.Fatalf("Failed to decode AAD %q: %v", wt.AAD, err) | ||
1013 | } | ||
1014 | |||
1015 | msg, err := hex.DecodeString(wt.Msg) | ||
1016 | if err != nil { | ||
1017 | log.Fatalf("Failed to decode msg %q: %v", wt.Msg, err) | ||
1018 | } | ||
1019 | |||
1020 | ct, err := hex.DecodeString(wt.CT) | ||
1021 | if err != nil { | ||
1022 | log.Fatalf("Failed to decode CT %q: %v", wt.CT, err) | ||
1023 | } | ||
1024 | |||
1025 | tag, err := hex.DecodeString(wt.Tag) | ||
1026 | if err != nil { | ||
1027 | log.Fatalf("Failed to decode tag %q: %v", wt.Tag, err) | ||
1028 | } | ||
1029 | |||
1030 | keyLen, ivLen, aadLen, msgLen, ctLen, tagLen := len(key), len(iv), len(aad), len(msg), len(ct), len(tag) | ||
1031 | |||
1032 | if keyLen == 0 { | ||
1033 | key = append(key, 0) | ||
1034 | } | ||
1035 | if ivLen == 0 { | ||
1036 | iv = append(iv, 0) | ||
1037 | } | ||
1038 | if aadLen == 0 { | ||
1039 | aad = append(aad, 0) | ||
1040 | } | ||
1041 | if msgLen == 0 { | ||
1042 | msg = append(msg, 0) | ||
1043 | } | ||
1044 | if ctLen == 0 { | ||
1045 | ct = append(ct, 0) | ||
1046 | } | ||
1047 | if tagLen == 0 { | ||
1048 | tag = append(tag, 0) | ||
1049 | } | ||
1050 | 988 | ||
1051 | openEvp := checkAesAead(algorithm, ctx, 0, key, keyLen, iv, ivLen, aad, aadLen, ct, ctLen, msg, msgLen, tag, tagLen, wt) | 989 | openEvp := checkAesAead(algorithm, ctx, 0, key, keyLen, iv, ivLen, aad, aadLen, ct, ctLen, msg, msgLen, tag, tagLen, wt) |
1052 | sealEvp := checkAesAead(algorithm, ctx, 1, key, keyLen, iv, ivLen, aad, aadLen, msg, msgLen, ct, ctLen, tag, tagLen, wt) | 990 | sealEvp := checkAesAead(algorithm, ctx, 1, key, keyLen, iv, ivLen, aad, aadLen, msg, msgLen, ct, ctLen, tag, tagLen, wt) |
@@ -1110,32 +1048,9 @@ func (wtg *wycheproofTestGroupAesAead) run(algorithm string, variant testVariant | |||
1110 | } | 1048 | } |
1111 | 1049 | ||
1112 | func runAesCmacTest(cipher *C.EVP_CIPHER, wt *wycheproofTestAesCmac) bool { | 1050 | func runAesCmacTest(cipher *C.EVP_CIPHER, wt *wycheproofTestAesCmac) bool { |
1113 | key, err := hex.DecodeString(wt.Key) | 1051 | key, keyLen := mustDecodeHexString(wt.Key, "key") |
1114 | if err != nil { | 1052 | msg, msgLen := mustDecodeHexString(wt.Msg, "msg") |
1115 | log.Fatalf("Failed to decode key %q: %v", wt.Key, err) | 1053 | tag, tagLen := mustDecodeHexString(wt.Tag, "tag") |
1116 | } | ||
1117 | |||
1118 | msg, err := hex.DecodeString(wt.Msg) | ||
1119 | if err != nil { | ||
1120 | log.Fatalf("Failed to decode msg %q: %v", wt.Msg, err) | ||
1121 | } | ||
1122 | |||
1123 | tag, err := hex.DecodeString(wt.Tag) | ||
1124 | if err != nil { | ||
1125 | log.Fatalf("Failed to decode tag %q: %v", wt.Tag, err) | ||
1126 | } | ||
1127 | |||
1128 | keyLen, msgLen, tagLen := len(key), len(msg), len(tag) | ||
1129 | |||
1130 | if keyLen == 0 { | ||
1131 | key = append(key, 0) | ||
1132 | } | ||
1133 | if msgLen == 0 { | ||
1134 | msg = append(msg, 0) | ||
1135 | } | ||
1136 | if tagLen == 0 { | ||
1137 | tag = append(tag, 0) | ||
1138 | } | ||
1139 | 1054 | ||
1140 | mdctx := C.EVP_MD_CTX_new() | 1055 | mdctx := C.EVP_MD_CTX_new() |
1141 | if mdctx == nil { | 1056 | if mdctx == nil { |
@@ -1291,22 +1206,13 @@ func runChaCha20Poly1305Test(algorithm string, wt *wycheproofTestAead) bool { | |||
1291 | aead = C.EVP_aead_xchacha20_poly1305() | 1206 | aead = C.EVP_aead_xchacha20_poly1305() |
1292 | } | 1207 | } |
1293 | 1208 | ||
1294 | key, err := hex.DecodeString(wt.Key) | 1209 | key, keyLen := mustDecodeHexString(wt.Key, "key") |
1295 | if err != nil { | 1210 | ct, ctLen := mustDecodeHexString(wt.CT, "CT") |
1296 | log.Fatalf("Failed to decode key %q: %v", wt.Key, err) | 1211 | iv, ivLen := mustDecodeHexString(wt.IV, "iv") |
1297 | } | 1212 | aad, aadLen := mustDecodeHexString(wt.AAD, "aad") |
1298 | iv, err := hex.DecodeString(wt.IV) | 1213 | msg, msgLen := mustDecodeHexString(wt.Msg, "msg") |
1299 | if err != nil { | 1214 | |
1300 | log.Fatalf("Failed to decode key %q: %v", wt.IV, err) | 1215 | // ct and tag are concatenated in checkAeadOpen(), so don't use mustDecodeHexString() |
1301 | } | ||
1302 | aad, err := hex.DecodeString(wt.AAD) | ||
1303 | if err != nil { | ||
1304 | log.Fatalf("Failed to decode AAD %q: %v", wt.AAD, err) | ||
1305 | } | ||
1306 | msg, err := hex.DecodeString(wt.Msg) | ||
1307 | if err != nil { | ||
1308 | log.Fatalf("Failed to decode msg %q: %v", wt.Msg, err) | ||
1309 | } | ||
1310 | ct, err := hex.DecodeString(wt.CT) | 1216 | ct, err := hex.DecodeString(wt.CT) |
1311 | if err != nil { | 1217 | if err != nil { |
1312 | log.Fatalf("Failed to decode ct %q: %v", wt.CT, err) | 1218 | log.Fatalf("Failed to decode ct %q: %v", wt.CT, err) |
@@ -1315,18 +1221,7 @@ func runChaCha20Poly1305Test(algorithm string, wt *wycheproofTestAead) bool { | |||
1315 | if err != nil { | 1221 | if err != nil { |
1316 | log.Fatalf("Failed to decode tag %q: %v", wt.Tag, err) | 1222 | log.Fatalf("Failed to decode tag %q: %v", wt.Tag, err) |
1317 | } | 1223 | } |
1318 | 1224 | ctLen, tagLen := len(ct), len(tag) | |
1319 | keyLen, ivLen, aadLen, msgLen, ctLen, tagLen := len(key), len(iv), len(aad), len(msg), len(ct), len(tag) | ||
1320 | |||
1321 | if ivLen == 0 { | ||
1322 | iv = append(iv, 0) | ||
1323 | } | ||
1324 | if aadLen == 0 { | ||
1325 | aad = append(aad, 0) | ||
1326 | } | ||
1327 | if msgLen == 0 { | ||
1328 | msg = append(msg, 0) | ||
1329 | } | ||
1330 | 1225 | ||
1331 | ctx := C.EVP_AEAD_CTX_new() | 1226 | ctx := C.EVP_AEAD_CTX_new() |
1332 | if ctx == nil { | 1227 | if ctx == nil { |
@@ -1410,15 +1305,7 @@ func encodeDSAP1363Sig(wtSig string) (*C.uchar, C.int) { | |||
1410 | } | 1305 | } |
1411 | 1306 | ||
1412 | func runDSATest(dsa *C.DSA, md *C.EVP_MD, variant testVariant, wt *wycheproofTestDSA) bool { | 1307 | func runDSATest(dsa *C.DSA, md *C.EVP_MD, variant testVariant, wt *wycheproofTestDSA) bool { |
1413 | msg, err := hex.DecodeString(wt.Msg) | 1308 | msg, msgLen := mustHashHexMessage(md, wt.Msg) |
1414 | if err != nil { | ||
1415 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
1416 | } | ||
1417 | |||
1418 | msg, msgLen, err := hashEvpDigestMessage(md, msg) | ||
1419 | if err != nil { | ||
1420 | log.Fatalf("%v", err) | ||
1421 | } | ||
1422 | 1309 | ||
1423 | var ret C.int | 1310 | var ret C.int |
1424 | if variant == P1363 { | 1311 | if variant == P1363 { |
@@ -1432,14 +1319,7 @@ func runDSATest(dsa *C.DSA, md *C.EVP_MD, variant testVariant, wt *wycheproofTes | |||
1432 | ret = C.DSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), | 1319 | ret = C.DSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), |
1433 | (*C.uchar)(unsafe.Pointer(cDer)), C.int(derLen), dsa) | 1320 | (*C.uchar)(unsafe.Pointer(cDer)), C.int(derLen), dsa) |
1434 | } else { | 1321 | } else { |
1435 | sig, err := hex.DecodeString(wt.Sig) | 1322 | sig, sigLen := mustDecodeHexString(wt.Sig, "sig") |
1436 | if err != nil { | ||
1437 | log.Fatalf("Failed to decode signature %q: %v", wt.Sig, err) | ||
1438 | } | ||
1439 | sigLen := len(sig) | ||
1440 | if sigLen == 0 { | ||
1441 | sig = append(msg, 0) | ||
1442 | } | ||
1443 | ret = C.DSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), | 1323 | ret = C.DSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), |
1444 | (*C.uchar)(unsafe.Pointer(&sig[0])), C.int(sigLen), dsa) | 1324 | (*C.uchar)(unsafe.Pointer(&sig[0])), C.int(sigLen), dsa) |
1445 | } | 1325 | } |
@@ -1505,15 +1385,7 @@ func (wtg *wycheproofTestGroupDSA) run(algorithm string, variant testVariant) bo | |||
1505 | log.Fatalf("Failed to get hash: %v", err) | 1385 | log.Fatalf("Failed to get hash: %v", err) |
1506 | } | 1386 | } |
1507 | 1387 | ||
1508 | der, err := hex.DecodeString(wtg.KeyDER) | 1388 | der, derLen := mustDecodeHexString(wtg.KeyDER, "DER encoded key") |
1509 | if err != nil { | ||
1510 | log.Fatalf("Failed to decode DER encoded key: %v", err) | ||
1511 | } | ||
1512 | |||
1513 | derLen := len(der) | ||
1514 | if derLen == 0 { | ||
1515 | der = append(der, 0) | ||
1516 | } | ||
1517 | 1389 | ||
1518 | Cder := (*C.uchar)(C.malloc(C.ulong(derLen))) | 1390 | Cder := (*C.uchar)(C.malloc(C.ulong(derLen))) |
1519 | if Cder == nil { | 1391 | if Cder == nil { |
@@ -1576,15 +1448,7 @@ func runECDHTest(nid int, variant testVariant, wt *wycheproofTestECDH) bool { | |||
1576 | return false | 1448 | return false |
1577 | } | 1449 | } |
1578 | 1450 | ||
1579 | pub, err := hex.DecodeString(wt.Public) | 1451 | pub, pubLen := mustDecodeHexString(wt.Public, "public key") |
1580 | if err != nil { | ||
1581 | log.Fatalf("Failed to decode public key: %v", err) | ||
1582 | } | ||
1583 | |||
1584 | pubLen := len(pub) | ||
1585 | if pubLen == 0 { | ||
1586 | pub = append(pub, 0) | ||
1587 | } | ||
1588 | 1452 | ||
1589 | Cpub := (*C.uchar)(C.malloc(C.ulong(pubLen))) | 1453 | Cpub := (*C.uchar)(C.malloc(C.ulong(pubLen))) |
1590 | if Cpub == nil { | 1454 | if Cpub == nil { |
@@ -1634,13 +1498,10 @@ func runECDHTest(nid int, variant testVariant, wt *wycheproofTestECDH) bool { | |||
1634 | return false | 1498 | return false |
1635 | } | 1499 | } |
1636 | 1500 | ||
1637 | shared, err := hex.DecodeString(wt.Shared) | 1501 | shared, sharedLen := mustDecodeHexString(wt.Shared, "shared secret") |
1638 | if err != nil { | ||
1639 | log.Fatalf("Failed to decode shared secret: %v", err) | ||
1640 | } | ||
1641 | 1502 | ||
1642 | // XXX The shared fields of the secp224k1 test cases have a 0 byte preprended. | 1503 | // XXX The shared fields of the secp224k1 test cases have a 0 byte preprended. |
1643 | if len(shared) == int(secLen)+1 && shared[0] == 0 { | 1504 | if sharedLen == int(secLen)+1 && shared[0] == 0 { |
1644 | fmt.Printf("INFO: %s - prepending 0 byte.\n", wt) | 1505 | fmt.Printf("INFO: %s - prepending 0 byte.\n", wt) |
1645 | // shared = shared[1:]; | 1506 | // shared = shared[1:]; |
1646 | zero := make([]byte, 1, secLen+1) | 1507 | zero := make([]byte, 1, secLen+1) |
@@ -1750,10 +1611,7 @@ func runECDHWebCryptoTest(nid int, wt *wycheproofTestECDHWebCrypto) bool { | |||
1750 | return false | 1611 | return false |
1751 | } | 1612 | } |
1752 | 1613 | ||
1753 | shared, err := hex.DecodeString(wt.Shared) | 1614 | shared, _ := mustDecodeHexString(wt.Shared, "shared secret") |
1754 | if err != nil { | ||
1755 | log.Fatalf("Failed to decode shared secret: %v", err) | ||
1756 | } | ||
1757 | 1615 | ||
1758 | success := true | 1616 | success := true |
1759 | if !bytes.Equal(shared, secret) { | 1617 | if !bytes.Equal(shared, secret) { |
@@ -1782,15 +1640,7 @@ func (wtg *wycheproofTestGroupECDHWebCrypto) run(algorithm string, variant testV | |||
1782 | } | 1640 | } |
1783 | 1641 | ||
1784 | func runECDSATest(ecKey *C.EC_KEY, md *C.EVP_MD, nid int, variant testVariant, wt *wycheproofTestECDSA) bool { | 1642 | func runECDSATest(ecKey *C.EC_KEY, md *C.EVP_MD, nid int, variant testVariant, wt *wycheproofTestECDSA) bool { |
1785 | msg, err := hex.DecodeString(wt.Msg) | 1643 | msg, msgLen := mustHashHexMessage(md, wt.Msg) |
1786 | if err != nil { | ||
1787 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
1788 | } | ||
1789 | |||
1790 | msg, msgLen, err := hashEvpDigestMessage(md, msg) | ||
1791 | if err != nil { | ||
1792 | log.Fatalf("%v", err) | ||
1793 | } | ||
1794 | 1644 | ||
1795 | var ret C.int | 1645 | var ret C.int |
1796 | if variant == Webcrypto || variant == P1363 { | 1646 | if variant == Webcrypto || variant == P1363 { |
@@ -1804,15 +1654,8 @@ func runECDSATest(ecKey *C.EC_KEY, md *C.EVP_MD, nid int, variant testVariant, w | |||
1804 | ret = C.ECDSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), | 1654 | ret = C.ECDSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), |
1805 | (*C.uchar)(unsafe.Pointer(cDer)), C.int(derLen), ecKey) | 1655 | (*C.uchar)(unsafe.Pointer(cDer)), C.int(derLen), ecKey) |
1806 | } else { | 1656 | } else { |
1807 | sig, err := hex.DecodeString(wt.Sig) | 1657 | sig, sigLen := mustDecodeHexString(wt.Sig, "sig") |
1808 | if err != nil { | ||
1809 | log.Fatalf("Failed to decode signature %q: %v", wt.Sig, err) | ||
1810 | } | ||
1811 | 1658 | ||
1812 | sigLen := len(sig) | ||
1813 | if sigLen == 0 { | ||
1814 | sig = append(sig, 0) | ||
1815 | } | ||
1816 | ret = C.ECDSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), | 1659 | ret = C.ECDSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), |
1817 | (*C.uchar)(unsafe.Pointer(&sig[0])), C.int(sigLen), ecKey) | 1660 | (*C.uchar)(unsafe.Pointer(&sig[0])), C.int(sigLen), ecKey) |
1818 | } | 1661 | } |
@@ -1993,23 +1836,8 @@ func runEdDSATest(pkey *C.EVP_PKEY, wt *wycheproofTestEdDSA) bool { | |||
1993 | log.Fatal("EVP_DigestVerifyInit failed") | 1836 | log.Fatal("EVP_DigestVerifyInit failed") |
1994 | } | 1837 | } |
1995 | 1838 | ||
1996 | msg, err := hex.DecodeString(wt.Msg) | 1839 | msg, msgLen := mustDecodeHexString(wt.Msg, "msg") |
1997 | if err != nil { | 1840 | sig, sigLen := mustDecodeHexString(wt.Sig, "sig") |
1998 | log.Fatalf("Failed to decode Message %q: %v", wt.Msg, err) | ||
1999 | } | ||
2000 | msgLen := len(msg) | ||
2001 | if msgLen == 0 { | ||
2002 | msg = append(msg, 0) | ||
2003 | } | ||
2004 | |||
2005 | sig, err := hex.DecodeString(wt.Sig) | ||
2006 | if err != nil { | ||
2007 | log.Fatalf("Failed to decode Signature %q: %v", wt.Sig, err) | ||
2008 | } | ||
2009 | sigLen := len(sig) | ||
2010 | if sigLen == 0 { | ||
2011 | sig = append(sig, 0) | ||
2012 | } | ||
2013 | 1841 | ||
2014 | ret := C.EVP_DigestVerify(mdctx, (*C.uchar)(unsafe.Pointer(&sig[0])), (C.size_t)(sigLen), (*C.uchar)(unsafe.Pointer(&msg[0])), (C.size_t)(msgLen)) | 1842 | ret := C.EVP_DigestVerify(mdctx, (*C.uchar)(unsafe.Pointer(&sig[0])), (C.size_t)(sigLen), (*C.uchar)(unsafe.Pointer(&msg[0])), (C.size_t)(msgLen)) |
2015 | 1843 | ||
@@ -2029,12 +1857,9 @@ func (wtg *wycheproofTestGroupEdDSA) run(algorithm string, variant testVariant) | |||
2029 | return false | 1857 | return false |
2030 | } | 1858 | } |
2031 | 1859 | ||
2032 | pubKey, err := hex.DecodeString(wtg.Key.Pk) | 1860 | pubKey, pubKeyLen := mustDecodeHexString(wtg.Key.Pk, "pubkey") |
2033 | if err != nil { | ||
2034 | log.Fatalf("Failed to decode Pubkey %q: %v", wtg.Key.Pk, err) | ||
2035 | } | ||
2036 | 1861 | ||
2037 | pkey := C.EVP_PKEY_new_raw_public_key(C.EVP_PKEY_ED25519, nil, (*C.uchar)(unsafe.Pointer(&pubKey[0])), (C.size_t)(len(pubKey))) | 1862 | pkey := C.EVP_PKEY_new_raw_public_key(C.EVP_PKEY_ED25519, nil, (*C.uchar)(unsafe.Pointer(&pubKey[0])), (C.size_t)(pubKeyLen)) |
2038 | if pkey == nil { | 1863 | if pkey == nil { |
2039 | log.Fatal("EVP_PKEY_new_raw_public_key failed") | 1864 | log.Fatal("EVP_PKEY_new_raw_public_key failed") |
2040 | } | 1865 | } |
@@ -2050,29 +1875,9 @@ func (wtg *wycheproofTestGroupEdDSA) run(algorithm string, variant testVariant) | |||
2050 | } | 1875 | } |
2051 | 1876 | ||
2052 | func runHkdfTest(md *C.EVP_MD, wt *wycheproofTestHkdf) bool { | 1877 | func runHkdfTest(md *C.EVP_MD, wt *wycheproofTestHkdf) bool { |
2053 | ikm, err := hex.DecodeString(wt.Ikm) | 1878 | ikm, ikmLen := mustDecodeHexString(wt.Ikm, "ikm") |
2054 | if err != nil { | 1879 | salt, saltLen := mustDecodeHexString(wt.Salt, "salt") |
2055 | log.Fatalf("Failed to decode ikm %q: %v", wt.Ikm, err) | 1880 | info, infoLen := mustDecodeHexString(wt.Info, "info") |
2056 | } | ||
2057 | salt, err := hex.DecodeString(wt.Salt) | ||
2058 | if err != nil { | ||
2059 | log.Fatalf("Failed to decode salt %q: %v", wt.Salt, err) | ||
2060 | } | ||
2061 | info, err := hex.DecodeString(wt.Info) | ||
2062 | if err != nil { | ||
2063 | log.Fatalf("Failed to decode info %q: %v", wt.Info, err) | ||
2064 | } | ||
2065 | |||
2066 | ikmLen, saltLen, infoLen := len(ikm), len(salt), len(info) | ||
2067 | if ikmLen == 0 { | ||
2068 | ikm = append(ikm, 0) | ||
2069 | } | ||
2070 | if saltLen == 0 { | ||
2071 | salt = append(salt, 0) | ||
2072 | } | ||
2073 | if infoLen == 0 { | ||
2074 | info = append(info, 0) | ||
2075 | } | ||
2076 | 1881 | ||
2077 | outLen := wt.Size | 1882 | outLen := wt.Size |
2078 | out := make([]byte, outLen) | 1883 | out := make([]byte, outLen) |
@@ -2120,10 +1925,7 @@ func runHkdfTest(md *C.EVP_MD, wt *wycheproofTestHkdf) bool { | |||
2120 | return success | 1925 | return success |
2121 | } | 1926 | } |
2122 | 1927 | ||
2123 | okm, err := hex.DecodeString(wt.Okm) | 1928 | okm, _ := mustDecodeHexString(wt.Okm, "okm") |
2124 | if err != nil { | ||
2125 | log.Fatalf("Failed to decode okm %q: %v", wt.Okm, err) | ||
2126 | } | ||
2127 | if !bytes.Equal(out[:outLen], okm) { | 1929 | if !bytes.Equal(out[:outLen], okm) { |
2128 | fmt.Printf("FAIL: %s - expected and computed output don't match.\n", wt) | 1930 | fmt.Printf("FAIL: %s - expected and computed output don't match.\n", wt) |
2129 | } | 1931 | } |
@@ -2148,25 +1950,8 @@ func (wtg *wycheproofTestGroupHkdf) run(algorithm string, variant testVariant) b | |||
2148 | } | 1950 | } |
2149 | 1951 | ||
2150 | func runHmacTest(md *C.EVP_MD, tagBytes int, wt *wycheproofTestHmac) bool { | 1952 | func runHmacTest(md *C.EVP_MD, tagBytes int, wt *wycheproofTestHmac) bool { |
2151 | key, err := hex.DecodeString(wt.Key) | 1953 | key, keyLen := mustDecodeHexString(wt.Key, "key") |
2152 | if err != nil { | 1954 | msg, msgLen := mustDecodeHexString(wt.Msg, "msg") |
2153 | log.Fatalf("failed to decode key %q: %v", wt.Key, err) | ||
2154 | } | ||
2155 | |||
2156 | msg, err := hex.DecodeString(wt.Msg) | ||
2157 | if err != nil { | ||
2158 | log.Fatalf("failed to decode msg %q: %v", wt.Msg, err) | ||
2159 | } | ||
2160 | |||
2161 | keyLen, msgLen := len(key), len(msg) | ||
2162 | |||
2163 | if keyLen == 0 { | ||
2164 | key = append(key, 0) | ||
2165 | } | ||
2166 | |||
2167 | if msgLen == 0 { | ||
2168 | msg = append(msg, 0) | ||
2169 | } | ||
2170 | 1955 | ||
2171 | got := make([]byte, C.EVP_MAX_MD_SIZE) | 1956 | got := make([]byte, C.EVP_MAX_MD_SIZE) |
2172 | var gotLen C.uint | 1957 | var gotLen C.uint |
@@ -2187,11 +1972,7 @@ func runHmacTest(md *C.EVP_MD, tagBytes int, wt *wycheproofTestHmac) bool { | |||
2187 | return false | 1972 | return false |
2188 | } | 1973 | } |
2189 | 1974 | ||
2190 | tag, err := hex.DecodeString(wt.Tag) | 1975 | tag, _ := mustDecodeHexString(wt.Tag, "tag") |
2191 | if err != nil { | ||
2192 | log.Fatalf("failed to decode tag %q: %v", wt.Tag, err) | ||
2193 | } | ||
2194 | |||
2195 | success = bytes.Equal(got[:tagBytes], tag) == (wt.Result == "valid") | 1976 | success = bytes.Equal(got[:tagBytes], tag) == (wt.Result == "valid") |
2196 | 1977 | ||
2197 | if !success { | 1978 | if !success { |
@@ -2279,30 +2060,9 @@ func runKWTestUnWrap(keySize int, key []byte, keyLen int, msg []byte, msgLen int | |||
2279 | } | 2060 | } |
2280 | 2061 | ||
2281 | func runKWTest(keySize int, wt *wycheproofTestKW) bool { | 2062 | func runKWTest(keySize int, wt *wycheproofTestKW) bool { |
2282 | key, err := hex.DecodeString(wt.Key) | 2063 | key, keyLen := mustDecodeHexString(wt.Key, "key") |
2283 | if err != nil { | 2064 | msg, msgLen := mustDecodeHexString(wt.Msg, "msg") |
2284 | log.Fatalf("Failed to decode key %q: %v", wt.Key, err) | 2065 | ct, ctLen := mustDecodeHexString(wt.CT, "CT") |
2285 | } | ||
2286 | msg, err := hex.DecodeString(wt.Msg) | ||
2287 | if err != nil { | ||
2288 | log.Fatalf("Failed to decode msg %q: %v", wt.Msg, err) | ||
2289 | } | ||
2290 | ct, err := hex.DecodeString(wt.CT) | ||
2291 | if err != nil { | ||
2292 | log.Fatalf("Failed to decode ct %q: %v", wt.CT, err) | ||
2293 | } | ||
2294 | |||
2295 | keyLen, msgLen, ctLen := len(key), len(msg), len(ct) | ||
2296 | |||
2297 | if keyLen == 0 { | ||
2298 | key = append(key, 0) | ||
2299 | } | ||
2300 | if msgLen == 0 { | ||
2301 | msg = append(msg, 0) | ||
2302 | } | ||
2303 | if ctLen == 0 { | ||
2304 | ct = append(ct, 0) | ||
2305 | } | ||
2306 | 2066 | ||
2307 | wrapSuccess := runKWTestWrap(keySize, key, keyLen, msg, msgLen, ct, ctLen, wt) | 2067 | wrapSuccess := runKWTestWrap(keySize, key, keyLen, msg, msgLen, ct, ctLen, wt) |
2308 | unwrapSuccess := runKWTestUnWrap(keySize, key, keyLen, msg, msgLen, ct, ctLen, wt) | 2068 | unwrapSuccess := runKWTestUnWrap(keySize, key, keyLen, msg, msgLen, ct, ctLen, wt) |
@@ -2353,14 +2113,7 @@ func (wtg *wycheproofTestGroupPrimality) run(algorithm string, variant testVaria | |||
2353 | } | 2113 | } |
2354 | 2114 | ||
2355 | func runRsaesOaepTest(rsa *C.RSA, sha *C.EVP_MD, mgfSha *C.EVP_MD, wt *wycheproofTestRsaes) bool { | 2115 | func runRsaesOaepTest(rsa *C.RSA, sha *C.EVP_MD, mgfSha *C.EVP_MD, wt *wycheproofTestRsaes) bool { |
2356 | ct, err := hex.DecodeString(wt.CT) | 2116 | ct, ctLen := mustDecodeHexString(wt.CT, "CT") |
2357 | if err != nil { | ||
2358 | log.Fatalf("Failed to decode cipher text %q: %v", wt.CT, err) | ||
2359 | } | ||
2360 | ctLen := len(ct) | ||
2361 | if ctLen == 0 { | ||
2362 | ct = append(ct, 0) | ||
2363 | } | ||
2364 | 2117 | ||
2365 | rsaSize := C.RSA_size(rsa) | 2118 | rsaSize := C.RSA_size(rsa) |
2366 | decrypted := make([]byte, rsaSize) | 2119 | decrypted := make([]byte, rsaSize) |
@@ -2378,20 +2131,8 @@ func runRsaesOaepTest(rsa *C.RSA, sha *C.EVP_MD, mgfSha *C.EVP_MD, wt *wycheproo | |||
2378 | return success | 2131 | return success |
2379 | } | 2132 | } |
2380 | 2133 | ||
2381 | label, err := hex.DecodeString(wt.Label) | 2134 | label, labelLen := mustDecodeHexString(wt.Label, "label") |
2382 | if err != nil { | 2135 | msg, msgLen := mustDecodeHexString(wt.Msg, "msg") |
2383 | log.Fatalf("Failed to decode label %q: %v", wt.Label, err) | ||
2384 | } | ||
2385 | labelLen := len(label) | ||
2386 | if labelLen == 0 { | ||
2387 | label = append(label, 0) | ||
2388 | } | ||
2389 | |||
2390 | msg, err := hex.DecodeString(wt.Msg) | ||
2391 | if err != nil { | ||
2392 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
2393 | } | ||
2394 | msgLen := len(msg) | ||
2395 | 2136 | ||
2396 | to := make([]byte, rsaSize) | 2137 | to := make([]byte, rsaSize) |
2397 | 2138 | ||
@@ -2407,7 +2148,7 @@ func runRsaesOaepTest(rsa *C.RSA, sha *C.EVP_MD, mgfSha *C.EVP_MD, wt *wycheproo | |||
2407 | } | 2148 | } |
2408 | 2149 | ||
2409 | to = to[:msgLen] | 2150 | to = to[:msgLen] |
2410 | if !bytes.Equal(msg, to) { | 2151 | if !bytes.Equal(msg[:msgLen], to) { |
2411 | success = false | 2152 | success = false |
2412 | fmt.Printf("FAIL: %s - expected and calculated message differ.\n", wt) | 2153 | fmt.Printf("FAIL: %s - expected and calculated message differ.\n", wt) |
2413 | } | 2154 | } |
@@ -2476,14 +2217,7 @@ func (wtg *wycheproofTestGroupRsaesOaep) run(algorithm string, variant testVaria | |||
2476 | } | 2217 | } |
2477 | 2218 | ||
2478 | func runRsaesPkcs1Test(rsa *C.RSA, wt *wycheproofTestRsaes) bool { | 2219 | func runRsaesPkcs1Test(rsa *C.RSA, wt *wycheproofTestRsaes) bool { |
2479 | ct, err := hex.DecodeString(wt.CT) | 2220 | ct, ctLen := mustDecodeHexString(wt.CT, "CT") |
2480 | if err != nil { | ||
2481 | log.Fatalf("Failed to decode cipher text %q: %v", wt.CT, err) | ||
2482 | } | ||
2483 | ctLen := len(ct) | ||
2484 | if ctLen == 0 { | ||
2485 | ct = append(ct, 0) | ||
2486 | } | ||
2487 | 2221 | ||
2488 | rsaSize := C.RSA_size(rsa) | 2222 | rsaSize := C.RSA_size(rsa) |
2489 | decrypted := make([]byte, rsaSize) | 2223 | decrypted := make([]byte, rsaSize) |
@@ -2501,15 +2235,12 @@ func runRsaesPkcs1Test(rsa *C.RSA, wt *wycheproofTestRsaes) bool { | |||
2501 | return success | 2235 | return success |
2502 | } | 2236 | } |
2503 | 2237 | ||
2504 | msg, err := hex.DecodeString(wt.Msg) | 2238 | msg, msgLen := mustDecodeHexString(wt.Msg, "msg") |
2505 | if err != nil { | ||
2506 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
2507 | } | ||
2508 | 2239 | ||
2509 | if int(ret) != len(msg) { | 2240 | if int(ret) != msgLen { |
2510 | success = false | 2241 | success = false |
2511 | fmt.Printf("FAIL: %s - got %d, want %d.\n", wt, ret, len(msg)) | 2242 | fmt.Printf("FAIL: %s - got %d, want %d\n", wt, ret, len(msg)) |
2512 | } else if !bytes.Equal(msg, decrypted[:len(msg)]) { | 2243 | } else if !bytes.Equal(msg[:msgLen], decrypted[:msgLen]) { |
2513 | success = false | 2244 | success = false |
2514 | fmt.Printf("FAIL: %s - expected and calculated message differ.\n", wt) | 2245 | fmt.Printf("FAIL: %s - expected and calculated message differ.\n", wt) |
2515 | } | 2246 | } |
@@ -2566,25 +2297,8 @@ func (wtg *wycheproofTestGroupRsaesPkcs1) run(algorithm string, variant testVari | |||
2566 | } | 2297 | } |
2567 | 2298 | ||
2568 | func runRsassaTest(rsa *C.RSA, sha *C.EVP_MD, mgfSha *C.EVP_MD, sLen int, wt *wycheproofTestRsassa) bool { | 2299 | func runRsassaTest(rsa *C.RSA, sha *C.EVP_MD, mgfSha *C.EVP_MD, sLen int, wt *wycheproofTestRsassa) bool { |
2569 | msg, err := hex.DecodeString(wt.Msg) | 2300 | msg, _ := mustHashHexMessage(sha, wt.Msg) |
2570 | if err != nil { | 2301 | sig, sigLen := mustDecodeHexString(wt.Sig, "sig") |
2571 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
2572 | } | ||
2573 | |||
2574 | msg, _, err = hashEvpDigestMessage(sha, msg) | ||
2575 | if err != nil { | ||
2576 | log.Fatalf("%v", err) | ||
2577 | } | ||
2578 | |||
2579 | sig, err := hex.DecodeString(wt.Sig) | ||
2580 | if err != nil { | ||
2581 | log.Fatalf("Failed to decode signature %q: %v", wt.Sig, err) | ||
2582 | } | ||
2583 | |||
2584 | sigLen := len(sig) | ||
2585 | if sigLen == 0 { | ||
2586 | sig = append(sig, 0) | ||
2587 | } | ||
2588 | 2302 | ||
2589 | sigOut := make([]byte, C.RSA_size(rsa)-11) | 2303 | sigOut := make([]byte, C.RSA_size(rsa)-11) |
2590 | if sigLen == 0 { | 2304 | if sigLen == 0 { |
@@ -2668,25 +2382,8 @@ func (wtg *wycheproofTestGroupRsassa) run(algorithm string, variant testVariant) | |||
2668 | } | 2382 | } |
2669 | 2383 | ||
2670 | func runRSATest(rsa *C.RSA, md *C.EVP_MD, nid int, wt *wycheproofTestRSA) bool { | 2384 | func runRSATest(rsa *C.RSA, md *C.EVP_MD, nid int, wt *wycheproofTestRSA) bool { |
2671 | msg, err := hex.DecodeString(wt.Msg) | 2385 | msg, msgLen := mustHashHexMessage(md, wt.Msg) |
2672 | if err != nil { | 2386 | sig, sigLen := mustDecodeHexString(wt.Sig, "sig") |
2673 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
2674 | } | ||
2675 | |||
2676 | msg, msgLen, err := hashEvpDigestMessage(md, msg) | ||
2677 | if err != nil { | ||
2678 | log.Fatalf("%v", err) | ||
2679 | } | ||
2680 | |||
2681 | sig, err := hex.DecodeString(wt.Sig) | ||
2682 | if err != nil { | ||
2683 | log.Fatalf("Failed to decode signature %q: %v", wt.Sig, err) | ||
2684 | } | ||
2685 | |||
2686 | sigLen := len(sig) | ||
2687 | if sigLen == 0 { | ||
2688 | sig = append(sig, 0) | ||
2689 | } | ||
2690 | 2387 | ||
2691 | ret := C.RSA_verify(C.int(nid), (*C.uchar)(unsafe.Pointer(&msg[0])), C.uint(msgLen), | 2388 | ret := C.RSA_verify(C.int(nid), (*C.uchar)(unsafe.Pointer(&msg[0])), C.uint(msgLen), |
2692 | (*C.uchar)(unsafe.Pointer(&sig[0])), C.uint(sigLen), rsa) | 2389 | (*C.uchar)(unsafe.Pointer(&sig[0])), C.uint(sigLen), rsa) |
@@ -2751,18 +2448,9 @@ func (wtg *wycheproofTestGroupRSA) run(algorithm string, variant testVariant) bo | |||
2751 | } | 2448 | } |
2752 | 2449 | ||
2753 | func runX25519Test(wt *wycheproofTestX25519) bool { | 2450 | func runX25519Test(wt *wycheproofTestX25519) bool { |
2754 | public, err := hex.DecodeString(wt.Public) | 2451 | public, _ := mustDecodeHexString(wt.Public, "public") |
2755 | if err != nil { | 2452 | private, _ := mustDecodeHexString(wt.Private, "private") |
2756 | log.Fatalf("Failed to decode public %q: %v", wt.Public, err) | 2453 | shared, _ := mustDecodeHexString(wt.Shared, "shared") |
2757 | } | ||
2758 | private, err := hex.DecodeString(wt.Private) | ||
2759 | if err != nil { | ||
2760 | log.Fatalf("Failed to decode private %q: %v", wt.Private, err) | ||
2761 | } | ||
2762 | shared, err := hex.DecodeString(wt.Shared) | ||
2763 | if err != nil { | ||
2764 | log.Fatalf("Failed to decode shared %q: %v", wt.Shared, err) | ||
2765 | } | ||
2766 | 2454 | ||
2767 | got := make([]byte, C.X25519_KEY_LENGTH) | 2455 | got := make([]byte, C.X25519_KEY_LENGTH) |
2768 | result := true | 2456 | result := true |