acvptool: support HMAC revision 2.0

HMAC revision 2.0 moves msgLen, keyLen, and macLen from the test group
to each test case. Read these fields according to the vector set
revision, preserving support for 1.0.

The new test bz2 vectors/expected were fetched from the NIST demo server
and the answers validated there successfully before trimming.

See the ACVP HMAC specification for more information:

  https://pages.nist.gov/ACVP/draft-fussell-acvp-mac.html#section-13.4

Change-Id: Id54d306cfad8d93e17392f528ba995cf1c94ba30
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/105508
Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
SLSA-Policy-Verified: SLSA Policy Verification Service <devtools-gerritcodereview-exitgate@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/util/fipstools/acvp/acvptool/subprocess/hmac.go b/util/fipstools/acvp/acvptool/subprocess/hmac.go
index e4d5e40..65df966 100644
--- a/util/fipstools/acvp/acvptool/subprocess/hmac.go
+++ b/util/fipstools/acvp/acvptool/subprocess/hmac.go
@@ -25,7 +25,8 @@
 // https://pages.nist.gov/ACVP/draft-fussell-acvp-mac.html#name-test-vectors
 
 type hmacTestVectorSet struct {
-	Groups []hmacTestGroup `json:"testGroups"`
+	Revision string          `json:"revision"`
+	Groups   []hmacTestGroup `json:"testGroups"`
 }
 
 type hmacTestGroup struct {
@@ -35,9 +36,12 @@
 	KeyBits int    `json:"keyLen"` // maximum possible value is 524288
 	MACBits int    `json:"macLen"` // maximum possible value is 512
 	Tests   []struct {
-		ID     uint64 `json:"tcId"`
-		KeyHex string `json:"key"`
-		MsgHex string `json:"msg"`
+		ID      uint64 `json:"tcId"`
+		MsgBits int    `json:"msgLen"`
+		KeyBits int    `json:"keyLen"`
+		MACBits int    `json:"macLen"`
+		KeyHex  string `json:"key"`
+		MsgHex  string `json:"msg"`
 	} `json:"tests"`
 }
 
@@ -91,27 +95,32 @@
 		response := hmacTestGroupResponse{
 			ID: group.ID,
 		}
-		if group.MACBits > h.mdLen*8 {
-			return nil, fmt.Errorf("test group %d specifies MAC length should be %d, but maximum possible length is %d", group.ID, group.MACBits, h.mdLen*8)
-		}
-		if group.MACBits%8 != 0 {
-			return nil, fmt.Errorf("fractional-byte HMAC output length requested: %d", group.MACBits)
-		}
-		outBytes := group.MACBits / 8
-
 		for _, test := range group.Tests {
 			test := test
 
-			if len(test.MsgHex)*4 != group.MsgBits {
-				return nil, fmt.Errorf("test case %d/%d contains hex message of length %d but specifies a bit length of %d", group.ID, test.ID, len(test.MsgHex), group.MsgBits)
+			// Revision 2.0 specifies lengths per test, rather than per group.
+			msgBits, keyBits, macBits := group.MsgBits, group.KeyBits, group.MACBits
+			if parsed.Revision == "2.0" {
+				msgBits, keyBits, macBits = test.MsgBits, test.KeyBits, test.MACBits
+			}
+			if macBits > h.mdLen*8 {
+				return nil, fmt.Errorf("test case %d/%d specifies MAC length should be %d, but maximum possible length is %d", group.ID, test.ID, macBits, h.mdLen*8)
+			}
+			if macBits%8 != 0 {
+				return nil, fmt.Errorf("fractional-byte HMAC output length requested: %d", macBits)
+			}
+			outBytes := macBits / 8
+
+			if len(test.MsgHex)*4 != msgBits {
+				return nil, fmt.Errorf("test case %d/%d contains hex message of length %d but specifies a bit length of %d", group.ID, test.ID, len(test.MsgHex), msgBits)
 			}
 			msg, err := hex.DecodeString(test.MsgHex)
 			if err != nil {
 				return nil, fmt.Errorf("failed to decode hex in test case %d/%d: %s", group.ID, test.ID, err)
 			}
 
-			if len(test.KeyHex)*4 != group.KeyBits {
-				return nil, fmt.Errorf("test case %d/%d contains hex key of length %d but specifies a bit length of %d", group.ID, test.ID, len(test.KeyHex), group.KeyBits)
+			if len(test.KeyHex)*4 != keyBits {
+				return nil, fmt.Errorf("test case %d/%d contains hex key of length %d but specifies a bit length of %d", group.ID, test.ID, len(test.KeyHex), keyBits)
 			}
 			key, err := hex.DecodeString(test.KeyHex)
 			if err != nil {
diff --git a/util/fipstools/acvp/acvptool/test/expected/HMAC-SHA2-256-2.0.bz2 b/util/fipstools/acvp/acvptool/test/expected/HMAC-SHA2-256-2.0.bz2
new file mode 100644
index 0000000..df619e5
--- /dev/null
+++ b/util/fipstools/acvp/acvptool/test/expected/HMAC-SHA2-256-2.0.bz2
Binary files differ
diff --git a/util/fipstools/acvp/acvptool/test/tests.json b/util/fipstools/acvp/acvptool/test/tests.json
index c2e88c9..92aa111 100644
--- a/util/fipstools/acvp/acvptool/test/tests.json
+++ b/util/fipstools/acvp/acvptool/test/tests.json
@@ -16,6 +16,7 @@
 {"Wrapper": "modulewrapper", "In": "vectors/HMAC-SHA-1.bz2", "Out": "expected/HMAC-SHA-1.bz2"},
 {"Wrapper": "modulewrapper", "In": "vectors/HMAC-SHA2-224.bz2", "Out": "expected/HMAC-SHA2-224.bz2"},
 {"Wrapper": "modulewrapper", "In": "vectors/HMAC-SHA2-256.bz2", "Out": "expected/HMAC-SHA2-256.bz2"},
+{"Wrapper": "modulewrapper", "In": "vectors/HMAC-SHA2-256-2.0.bz2", "Out": "expected/HMAC-SHA2-256-2.0.bz2"},
 {"Wrapper": "modulewrapper", "In": "vectors/HMAC-SHA2-384.bz2", "Out": "expected/HMAC-SHA2-384.bz2"},
 {"Wrapper": "modulewrapper", "In": "vectors/HMAC-SHA2-512.bz2", "Out": "expected/HMAC-SHA2-512.bz2"},
 {"Wrapper": "modulewrapper", "In": "vectors/HMAC-SHA2-512-256.bz2", "Out": "expected/HMAC-SHA2-512-256.bz2"},
diff --git a/util/fipstools/acvp/acvptool/test/vectors/HMAC-SHA2-256-2.0.bz2 b/util/fipstools/acvp/acvptool/test/vectors/HMAC-SHA2-256-2.0.bz2
new file mode 100644
index 0000000..938854c
--- /dev/null
+++ b/util/fipstools/acvp/acvptool/test/vectors/HMAC-SHA2-256-2.0.bz2
Binary files differ