1// Copyright 2019 The BoringSSL Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package subprocess 16 17import ( 18 "encoding/hex" 19 "encoding/json" 20 "fmt" 21) 22 23// The following structures reflect the JSON of ACVP hash tests. See 24// https://pages.nist.gov/ACVP/draft-celi-acvp-sha.html#name-test-vectors 25 26type hashTestVectorSet struct { 27 Groups []hashTestGroup `json:"testGroups"` 28} 29 30type hashTestGroup struct { 31 ID uint64 `json:"tgId"` 32 Type string `json:"testType"` 33 Tests []struct { 34 ID uint64 `json:"tcId"` 35 BitLength uint64 `json:"len"` 36 MsgHex string `json:"msg"` 37 } `json:"tests"` 38} 39 40type hashTestGroupResponse struct { 41 ID uint64 `json:"tgId"` 42 Tests []hashTestResponse `json:"tests"` 43} 44 45type hashTestResponse struct { 46 ID uint64 `json:"tcId"` 47 DigestHex string `json:"md,omitempty"` 48 MCTResults []hashMCTResult `json:"resultsArray,omitempty"` 49} 50 51type hashMCTResult struct { 52 DigestHex string `json:"md"` 53} 54 55// hashPrimitive implements an ACVP algorithm by making requests to the 56// subprocess to hash strings. 57type hashPrimitive struct { 58 // algo is the ACVP name for this algorithm and also the command name 59 // given to the subprocess to hash with this hash function. 60 algo string 61 // size is the number of bytes of digest that the hash produces. 62 size int 63} 64 65func (h *hashPrimitive) Process(vectorSet []byte, m Transactable) (any, error) { 66 var parsed hashTestVectorSet 67 if err := json.Unmarshal(vectorSet, &parsed); err != nil { 68 return nil, err 69 } 70 71 var ret []hashTestGroupResponse 72 // See 73 // https://pages.nist.gov/ACVP/draft-celi-acvp-sha.html#name-test-vectors 74 // for details about the tests. 75 for _, group := range parsed.Groups { 76 group := group 77 response := hashTestGroupResponse{ 78 ID: group.ID, 79 } 80 81 for _, test := range group.Tests { 82 test := test 83 84 if uint64(len(test.MsgHex))*4 != test.BitLength { 85 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) 86 } 87 msg, err := hex.DecodeString(test.MsgHex) 88 if err != nil { 89 return nil, fmt.Errorf("failed to decode hex in test case %d/%d: %s", group.ID, test.ID, err) 90 } 91 92 // http://usnistgov.github.io/ACVP/artifacts/draft-celi-acvp-sha-00.html#rfc.section.3 93 switch group.Type { 94 case "AFT": 95 m.TransactAsync(h.algo, 1, [][]byte{msg}, func(result [][]byte) error { 96 response.Tests = append(response.Tests, hashTestResponse{ 97 ID: test.ID, 98 DigestHex: hex.EncodeToString(result[0]), 99 }) 100 return nil 101 }) 102 103 case "MCT": 104 if len(msg) != h.size { 105 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) 106 } 107 108 testResponse := hashTestResponse{ID: test.ID} 109 110 digest := msg 111 for i := 0; i < 100; i++ { 112 result, err := m.Transact(h.algo+"/MCT", 1, digest) 113 if err != nil { 114 panic(h.algo + " hash operation failed: " + err.Error()) 115 } 116 117 digest = result[0] 118 testResponse.MCTResults = append(testResponse.MCTResults, hashMCTResult{hex.EncodeToString(digest)}) 119 } 120 121 response.Tests = append(response.Tests, testResponse) 122 123 default: 124 return nil, fmt.Errorf("test group %d has unknown type %q", group.ID, group.Type) 125 } 126 } 127 128 m.Barrier(func() { 129 ret = append(ret, response) 130 }) 131 } 132 133 if err := m.Flush(); err != nil { 134 return nil, err 135 } 136 137 return ret, nil 138} 139