@@ -16,7 +16,6 @@ package subprocess
1616
1717import (
1818 "bytes"
19- "encoding/hex"
2019 "encoding/json"
2120 "fmt"
2221)
@@ -37,15 +36,15 @@ type kasTestGroup struct {
3736type kasTest struct {
3837 ID uint64 `json:"tcId"`
3938
40- EphemeralXHex string `json:"ephemeralPublicServerX"`
41- EphemeralYHex string `json:"ephemeralPublicServerY"`
42- EphemeralPrivateKeyHex string `json:"ephemeralPrivateIut"`
39+ EphemeralXHex hexEncodedByteString `json:"ephemeralPublicServerX"`
40+ EphemeralYHex hexEncodedByteString `json:"ephemeralPublicServerY"`
41+ EphemeralPrivateKeyHex hexEncodedByteString `json:"ephemeralPrivateIut"`
4342
44- StaticXHex string `json:"staticPublicServerX"`
45- StaticYHex string `json:"staticPublicServerY"`
46- StaticPrivateKeyHex string `json:"staticPrivateIut"`
43+ StaticXHex hexEncodedByteString `json:"staticPublicServerX"`
44+ StaticYHex hexEncodedByteString `json:"staticPublicServerY"`
45+ StaticPrivateKeyHex hexEncodedByteString `json:"staticPrivateIut"`
4746
48- ResultHex string `json:"z"`
47+ Result hexEncodedByteString `json:"z"`
4948}
5049
5150type kasTestGroupResponse struct {
@@ -56,14 +55,14 @@ type kasTestGroupResponse struct {
5655type kasTestResponse struct {
5756 ID uint64 `json:"tcId"`
5857
59- EphemeralXHex string `json:"ephemeralPublicIutX,omitempty"`
60- EphemeralYHex string `json:"ephemeralPublicIutY,omitempty"`
58+ EphemeralXHex hexEncodedByteString `json:"ephemeralPublicIutX,omitempty"`
59+ EphemeralYHex hexEncodedByteString `json:"ephemeralPublicIutY,omitempty"`
6160
62- StaticXHex string `json:"staticPublicIutX,omitempty"`
63- StaticYHex string `json:"staticPublicIutY,omitempty"`
61+ StaticXHex hexEncodedByteString `json:"staticPublicIutX,omitempty"`
62+ StaticYHex hexEncodedByteString `json:"staticPublicIutY,omitempty"`
6463
65- ResultHex string `json:"z,omitempty"`
66- Passed * bool `json:"testPassed,omitempty"`
64+ Result hexEncodedByteString `json:"z,omitempty"`
65+ Passed * bool `json:"testPassed,omitempty"`
6766}
6867
6968type kas struct {}
@@ -94,76 +93,72 @@ func (k *kas) Process(vectorSet []byte, m Transactable) (interface{}, error) {
9493
9594 switch group .Curve {
9695 case "P-224" , "P-256" , "P-384" , "P-521" :
97- break
9896 default :
9997 return nil , fmt .Errorf ("unknown curve %q" , group .Curve )
10098 }
10199
102100 switch group .Role {
103101 case "initiator" , "responder" :
104- break
105102 default :
106103 return nil , fmt .Errorf ("unknown role %q" , group .Role )
107104 }
108105
109- var useStaticNamedFields bool
106+ var useEphemeralPeerKeys bool
107+ var useEphemeralPrivateKey bool
110108 switch group .Scheme {
111109 case "ephemeralUnified" :
112- break
110+ useEphemeralPeerKeys = true
111+ useEphemeralPrivateKey = true
113112 case "staticUnified" :
114- useStaticNamedFields = true
115- break
113+ useEphemeralPeerKeys = false
114+ useEphemeralPrivateKey = false
115+ case "onePassDh" :
116+ if group .Role == "initiator" {
117+ useEphemeralPeerKeys = false
118+ useEphemeralPrivateKey = true
119+ } else {
120+ useEphemeralPeerKeys = true
121+ useEphemeralPrivateKey = false
122+ }
116123 default :
117124 return nil , fmt .Errorf ("unknown scheme %q" , group .Scheme )
118125 }
119126
127+ generateEphemeralKeys := useEphemeralPrivateKey && group .Role != "staticUnified"
128+
120129 method := "ECDH/" + group .Curve
121130
122131 for _ , test := range group .Tests {
123132 test := test
124133
125- var xHex , yHex , privateKeyHex string
126- if useStaticNamedFields {
127- xHex , yHex , privateKeyHex = test .StaticXHex , test .StaticYHex , test . StaticPrivateKeyHex
134+ var peerX , peerY , privateKey hexEncodedByteString
135+ if useEphemeralPeerKeys {
136+ peerX , peerY = test .EphemeralXHex , test .EphemeralYHex
128137 } else {
129- xHex , yHex , privateKeyHex = test .EphemeralXHex , test .EphemeralYHex , test . EphemeralPrivateKeyHex
138+ peerX , peerY = test .StaticXHex , test .StaticYHex
130139 }
131140
132- if len (xHex ) == 0 || len (yHex ) == 0 {
133- return nil , fmt .Errorf ("%d/%d is missing peer's point" , group .ID , test .ID )
134- }
135-
136- peerX , err := hex .DecodeString (xHex )
137- if err != nil {
138- return nil , err
141+ if useEphemeralPrivateKey {
142+ privateKey = test .EphemeralPrivateKeyHex
143+ } else {
144+ privateKey = test .StaticPrivateKeyHex
139145 }
140146
141- peerY , err := hex .DecodeString (yHex )
142- if err != nil {
143- return nil , err
147+ if len (peerX ) == 0 || len (peerY ) == 0 {
148+ return nil , fmt .Errorf ("%d/%d is missing peer's point" , group .ID , test .ID )
144149 }
145150
146- if (len (privateKeyHex ) != 0 ) != privateKeyGiven {
151+ if (len (privateKey ) != 0 ) != privateKeyGiven {
147152 return nil , fmt .Errorf ("%d/%d incorrect private key presence" , group .ID , test .ID )
148153 }
149154
150155 if privateKeyGiven {
151- privateKey , err := hex .DecodeString (privateKeyHex )
152- if err != nil {
153- return nil , err
154- }
155-
156- expectedOutput , err := hex .DecodeString (test .ResultHex )
157- if err != nil {
158- return nil , err
159- }
160-
161156 result , err := m .Transact (method , 3 , peerX , peerY , privateKey )
162157 if err != nil {
163158 return nil , err
164159 }
165160
166- ok := bytes .Equal (result [2 ], expectedOutput )
161+ ok := bytes .Equal (result [2 ], test . Result )
167162 response .Tests = append (response .Tests , kasTestResponse {
168163 ID : test .ID ,
169164 Passed : & ok ,
@@ -175,16 +170,16 @@ func (k *kas) Process(vectorSet []byte, m Transactable) (interface{}, error) {
175170 }
176171
177172 testResponse := kasTestResponse {
178- ID : test .ID ,
179- ResultHex : hex . EncodeToString ( result [2 ]) ,
173+ ID : test .ID ,
174+ Result : result [2 ],
180175 }
181176
182- if useStaticNamedFields {
183- testResponse .StaticXHex = hex . EncodeToString ( result [0 ])
184- testResponse .StaticYHex = hex . EncodeToString ( result [1 ])
177+ if generateEphemeralKeys {
178+ testResponse .EphemeralXHex = result [0 ]
179+ testResponse .EphemeralYHex = result [1 ]
185180 } else {
186- testResponse .EphemeralXHex = hex . EncodeToString ( result [0 ])
187- testResponse .EphemeralYHex = hex . EncodeToString ( result [1 ])
181+ testResponse .StaticXHex = result [0 ]
182+ testResponse .StaticYHex = result [1 ]
188183 }
189184
190185 response .Tests = append (response .Tests , testResponse )
0 commit comments