Skip to content

Commit 51cac8c

Browse files
committed
[cryptotest] Add RSA sign SHA-3 test vectors
Although there are no test vectors available that use SHA-3, reuse some SHA-2 test vectors and manually override the mode to SHA-3. This works as the verify-after-sign testing approach enable us to do this. Signed-off-by: Pascal Nasahl <nasahlpa@lowrisc.org> (cherry picked from commit d493164)
1 parent 058d04c commit 51cac8c

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

sw/host/cryptotest/testvectors/data/BUILD

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -734,17 +734,26 @@ WYCHEPROOF_RSA_PSS_SUFFIXES = {
734734
# the output file name; the parser reads group["sha"] for the actual alg.
735735
WYCHEPROOF_RSA_OAEP_SIGN_SOURCES = {
736736
2048: [
737-
("sha256_mgf1sha256", "sha256"),
738-
("sha384_mgf1sha384", "sha384"),
739-
("sha512_mgf1sha512", "sha512"),
737+
("sha256_mgf1sha256", "sha256", None),
738+
("sha384_mgf1sha384", "sha384", None),
739+
("sha512_mgf1sha512", "sha512", None),
740+
("sha256_mgf1sha256", "sha3_256", "sha3-256"),
741+
("sha256_mgf1sha256", "sha3_384", "sha3-384"),
742+
("sha256_mgf1sha256", "sha3_512", "sha3-512"),
740743
],
741744
3072: [
742-
("sha256_mgf1sha256", "sha256"),
743-
("sha512_mgf1sha512", "sha512"),
745+
("sha256_mgf1sha256", "sha256", None),
746+
("sha512_mgf1sha512", "sha512", None),
747+
("sha256_mgf1sha256", "sha3_256", "sha3-256"),
748+
("sha256_mgf1sha256", "sha3_384", "sha3-384"),
749+
("sha256_mgf1sha256", "sha3_512", "sha3-512"),
744750
],
745751
4096: [
746-
("sha256_mgf1sha256", "sha256"),
747-
("sha512_mgf1sha512", "sha512"),
752+
("sha256_mgf1sha256", "sha256", None),
753+
("sha512_mgf1sha512", "sha512", None),
754+
("sha256_mgf1sha256", "sha3_256", "sha3-256"),
755+
("sha256_mgf1sha256", "sha3_384", "sha3-384"),
756+
("sha256_mgf1sha256", "sha3_512", "sha3-512"),
748757
],
749758
}
750759

@@ -787,15 +796,18 @@ WYCHEPROOF_RSA_OAEP_SIGN_SOURCES = {
787796
padding_mode,
788797
"--security_level",
789798
str(security_level),
790-
],
799+
] + ([
800+
"--hash",
801+
hash_override,
802+
] if hash_override else []),
791803
tool = "//sw/host/cryptotest/testvectors/parsers:wycheproof_rsa_parser",
792804
)
793805
for security_level in [
794806
2048,
795807
3072,
796808
4096,
797809
]
798-
for oaep_suffix, hash_suffix in WYCHEPROOF_RSA_OAEP_SIGN_SOURCES[security_level]
810+
for oaep_suffix, hash_suffix, hash_override in WYCHEPROOF_RSA_OAEP_SIGN_SOURCES[security_level]
799811
for padding_mode in [
800812
"pkcs1_1.5",
801813
"pss",

sw/host/cryptotest/testvectors/parsers/wycheproof_rsa_parser.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ def parse_test_vectors(raw_data, args):
1919
# Parse tests within the group
2020
for test in group["tests"]:
2121
logging.debug(f"Parsing tcId {test['tcId']}")
22+
hash_alg = (
23+
args.hash
24+
if args.operation == "sign" and args.hash
25+
else group["sha"].lower().replace("shake", "shake-")
26+
)
2227
test_vec = {
2328
"vendor": "wycheproof",
2429
"test_case_id": test["tcId"],
2530
"algorithm": "rsa",
2631
"operation": args.operation,
2732
"padding": args.padding,
2833
"security_level": int(args.security_level),
29-
"hash_alg": group["sha"].lower().replace("shake", "shake-"),
34+
"hash_alg": hash_alg,
3035
"message": str_to_byte_array(test["msg"]),
3136
}
3237

@@ -51,7 +56,12 @@ def parse_test_vectors(raw_data, args):
5156
else:
5257
raise ValueError(f"Unsupported RSA operation: {args.operation}")
5358

54-
if test["result"] == "valid":
59+
if args.operation == "sign":
60+
# Sign-then-verify always succeeds regardless of the source
61+
# vector's result; the OAEP result reflects ciphertext validity,
62+
# not key validity.
63+
test_vec["result"] = True
64+
elif test["result"] == "valid":
5565
test_vec["result"] = True
5666
elif test["result"] == "invalid":
5767
test_vec["result"] = False
@@ -118,6 +128,13 @@ def main():
118128
help = "RSA security level",
119129
choices = ["2048", "3072", "4096"],
120130
)
131+
parser.add_argument(
132+
"--hash",
133+
type = str,
134+
help = "Hash algorithm override for sign operation (overrides group sha field)",
135+
choices = ["sha-256", "sha-384", "sha-512", "sha3-256", "sha3-384", "sha3-512"],
136+
default = None,
137+
)
121138
args = parser.parse_args()
122139

123140
testvecs = parse_test_vectors(json.load(args.src), args)

0 commit comments

Comments
 (0)