-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
384 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR ISC | ||
|
||
package subprocess | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type mlDsa struct{} | ||
|
||
func (*mlDsa) Process(vectorSet []byte, m Transactable) (interface{}, error) { | ||
var vs struct { | ||
Mode string `json:"mode"` | ||
TestGroups json.RawMessage `json:"testGroups"` | ||
} | ||
|
||
if err := json.Unmarshal(vectorSet, &vs); err != nil { | ||
return nil, err | ||
} | ||
|
||
switch { | ||
case strings.EqualFold(vs.Mode, "keyGen"): | ||
return processMlDsaKeyGen(vs.TestGroups, m) | ||
case strings.EqualFold(vs.Mode, "sigGen"): | ||
return processMlDsaSigGen(vs.TestGroups, m) | ||
case strings.EqualFold(vs.Mode, "sigVer"): | ||
return processMlDsaSigVer(vs.TestGroups, m) | ||
} | ||
|
||
return nil, fmt.Errorf("unknown ML-DSA mode: %v", vs.Mode) | ||
} | ||
|
||
type mlDsaKeyGenTestGroup struct { | ||
ID uint64 `json:"tgId"` | ||
Type string `json:"testType"` | ||
ParameterSet string `json:"parameterSet"` | ||
Tests []struct { | ||
ID uint64 `json:"tcId"` | ||
SEED hexEncodedByteString `json:"seed"` | ||
} | ||
} | ||
|
||
type mlDsaKeyGenTestGroupResponse struct { | ||
ID uint64 `json:"tgId"` | ||
Tests []mlDsaKeyGenTestCaseResponse `json:"tests"` | ||
} | ||
|
||
type mlDsaKeyGenTestCaseResponse struct { | ||
ID uint64 `json:"tcId"` | ||
PK hexEncodedByteString `json:"pk"` | ||
SK hexEncodedByteString `json:"sk` | ||
} | ||
|
||
func processMlDsaKeyGen(vectors json.RawMessage, m Transactable) (interface{}, error) { | ||
var groups []mlDsaKeyGenTestGroup | ||
|
||
if err := json.Unmarshal(vectors, &groups); err != nil { | ||
return nil, err | ||
} | ||
|
||
var responses []mlDsaKeyGenTestGroupResponse | ||
|
||
for _, group := range groups { | ||
if !strings.EqualFold(group.Type, "AFT") { | ||
return nil, fmt.Errorf("unsupported keyGen test type: %v", group.Type) | ||
} | ||
|
||
response := mlDsaKeyGenTestGroupResponse{ | ||
ID: group.ID, | ||
} | ||
|
||
for _, test := range group.Tests { | ||
results, err := m.Transact("ML-DSA/"+group.ParameterSet+"/keyGen", 1, test.SEED) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pk := results[0] | ||
sk := results[1] | ||
|
||
response.Tests = append(response.Tests, mlDsaKeyGenTestCaseResponse{ | ||
ID: test.ID, | ||
PK: pk, | ||
SK: sk, | ||
}) | ||
} | ||
|
||
responses = append(responses, response) | ||
} | ||
|
||
return responses, nil | ||
} | ||
|
||
type mlDsaSigGenTestGroup struct { | ||
ID uint64 `json:"tgId"` | ||
Type string `json:"testType"` | ||
ParameterSet string `json:"parameterSet"` | ||
Deterministic bool `json:"deterministic"` | ||
Tests []struct { | ||
ID uint64 `json:"tcId"` | ||
MESSAGE hexEncodedByteString `json:"message"` | ||
SK hexEncodedByteString `json:"sk"` | ||
RND hexEncodedByteString `json:"rnd"` | ||
} | ||
} | ||
|
||
|
||
type mlDsaSigGenTestGroupResponse struct { | ||
ID uint64 `json:"tgId"` | ||
Tests []mlDsaSigGenTestCaseResponse `json:"tests"` | ||
} | ||
|
||
type mlDsaSigGenTestCaseResponse struct { | ||
ID uint64 `json:"tcId"` | ||
SIGNATURE hexEncodedByteString `json:"signature"` | ||
} | ||
|
||
func processMlDsaSigGen(vectors json.RawMessage, m Transactable) (interface{}, error) { | ||
var groups []mlDsaSigGenTestGroup | ||
|
||
if err := json.Unmarshal(vectors, &groups); err != nil { | ||
return nil, err | ||
} | ||
|
||
var responses []mlDsaSigGenTestGroupResponse | ||
|
||
for _, group := range groups { | ||
if !strings.EqualFold(group.Type, "AFT") { | ||
return nil, fmt.Errorf("unsupported sigGen test type: %v", group.Type) | ||
} | ||
|
||
response := mlDsaSigGenTestGroupResponse{ | ||
ID: group.ID, | ||
} | ||
|
||
for _, test := range group.Tests { | ||
results, err := m.Transact("ML-DSA/"+group.ParameterSet+"/sigGen", 3, test.SK, test.MESSAGE, test.RND) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signature := results[0] | ||
|
||
response.Tests = append(response.Tests, mlDsaSigGenTestCaseResponse{ | ||
ID: test.ID, | ||
SIGNATURE: signature, | ||
}) | ||
} | ||
|
||
responses = append(responses, response) | ||
} | ||
return responses, nil | ||
} | ||
|
||
type mlDsaSigVerTestGroup struct { | ||
ID uint64 `json:"tgId"` | ||
Type string `json:"testType"` | ||
ParameterSet string `json:"parameterSet"` | ||
PK hexEncodedByteString `json:"pk"` | ||
Tests []struct { | ||
ID uint64 `json:"tcId"` | ||
MESSAGE hexEncodedByteString `json:"message"` | ||
SIGNATURE hexEncodedByteString `json:"signature"` | ||
} | ||
} | ||
|
||
type mlDsaSigVerTestGroupResponse struct { | ||
ID uint64 `json:"tgId"` | ||
Tests []mlDsaSigVerTestCaseResponse `json:"tests"` | ||
} | ||
|
||
type mlDsaSigVerTestCaseResponse struct { | ||
ID uint64 `json:"tcId"` | ||
TESTPASSED *bool `json:"testPassed"` | ||
} | ||
|
||
|
||
func processMlDsaSigVer(vectors json.RawMessage, m Transactable) (interface{}, error) { | ||
var groups []mlDsaSigVerTestGroup | ||
|
||
if err := json.Unmarshal(vectors, &groups); err != nil { | ||
return nil, err | ||
} | ||
|
||
var responses []mlDsaSigVerTestGroupResponse | ||
|
||
for _, group := range groups { | ||
if !strings.EqualFold(group.Type, "AFT") { | ||
return nil, fmt.Errorf("unsupported sigVer test type: %v", group.Type) | ||
} | ||
|
||
response := mlDsaSigVerTestGroupResponse{ | ||
ID: group.ID, | ||
} | ||
|
||
for _, test := range group.Tests { | ||
results, err := m.Transact("ML-DSA/"+group.ParameterSet+"/sigVer", 3, test.SIGNATURE, group.PK, test.MESSAGE) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var passed *bool | ||
if len(results[0]) == 1 { | ||
val := results[0][0] == 1 | ||
passed = &val | ||
} | ||
|
||
response.Tests = append(response.Tests, mlDsaSigVerTestCaseResponse{ | ||
ID: test.ID, | ||
TESTPASSED: passed, | ||
}) | ||
} | ||
|
||
responses = append(responses, response) | ||
} | ||
return responses, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.