package subprocess
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
)
type hashTestVectorSet struct {
Groups []hashTestGroup `json:"testGroups"`
}
type hashTestGroup struct {
ID uint64 `json:"tgId"`
Type string `json:"testType"`
Tests []struct {
ID uint64 `json:"tcId"`
BitLength uint64 `json:"len"`
MsgHex string `json:"msg"`
LargeMsg hashLargeMsg `json:"largeMsg"`
} `json:"tests"`
}
type hashLargeMsg struct {
ContentHex string `json:"content"`
ContentLength uint64 `json:"contentLength"`
FullLength uint64 `json:"fullLength"`
ExpansionTech string `json:"expansionTechnique"`
}
type hashTestGroupResponse struct {
ID uint64 `json:"tgId"`
Tests []hashTestResponse `json:"tests"`
}
type hashTestResponse struct {
ID uint64 `json:"tcId"`
DigestHex string `json:"md,omitempty"`
MCTResults []hashMCTResult `json:"resultsArray,omitempty"`
}
type hashMCTResult struct {
DigestHex string `json:"md"`
}
type hashPrimitive struct {
algo string
size int
}
func (h *hashPrimitive) Process(vectorSet []byte, m Transactable) (interface{}, error) {
var parsed hashTestVectorSet
if err := json.Unmarshal(vectorSet, &parsed); err != nil {
return nil, err
}
var ret []hashTestGroupResponse
for _, group := range parsed.Groups {
response := hashTestGroupResponse{
ID: group.ID,
}
for _, test := range group.Tests {
if uint64(len(test.MsgHex))*4 != test.BitLength {
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), test.BitLength)
}
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)
}
switch group.Type {
case "AFT":
result, err := m.Transact(h.algo, 1, msg)
if err != nil {
panic(h.algo + " hash operation failed: " + err.Error())
}
response.Tests = append(response.Tests, hashTestResponse{
ID: test.ID,
DigestHex: hex.EncodeToString(result[0]),
})
case "MCT":
if len(msg) != h.size {
return nil, fmt.Errorf("MCT test case %d/%d contains message of length %d but the digest length is %d", group.ID, test.ID, len(msg), h.size)
}
testResponse := hashTestResponse{ID: test.ID}
digest := msg
for i := 0; i < 100; i++ {
result, err := m.Transact(h.algo+"/MCT", 1, digest)
if err != nil {
panic(h.algo + " hash operation failed: " + err.Error())
}
digest = result[0]
testResponse.MCTResults = append(testResponse.MCTResults, hashMCTResult{hex.EncodeToString(digest)})
}
response.Tests = append(response.Tests, testResponse)
case "LDT":
if test.LargeMsg.ExpansionTech != "repeating" {
return nil, fmt.Errorf("test case %d has invalid expantion technique", test.ID)
}
times := test.LargeMsg.FullLength / test.LargeMsg.ContentLength
content, err := hex.DecodeString(test.LargeMsg.ContentHex)
if err != nil {
return nil, fmt.Errorf("failed to decode hex in test case %d/%d: %s", group.ID, test.ID, err)
}
timesByteArr := make([]byte, 8)
binary.LittleEndian.PutUint64(timesByteArr, times)
result, err := m.Transact(h.algo+"/LDT", 1, content, timesByteArr)
if err != nil {
panic(h.algo + " LDT hash operation failed: " + err.Error())
}
response.Tests = append(response.Tests, hashTestResponse{
ID: test.ID,
DigestHex: hex.EncodeToString(result[0]),
})
default:
return nil, fmt.Errorf("test group %d has unknown type %q", group.ID, group.Type)
}
}
ret = append(ret, response)
}
return ret, nil
}