-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add KDA OneStep testing to ACVP #1792
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,14 +92,14 @@ func (a *aead) Process(vectorSet []byte, m Transactable) (interface{}, error) { | |
// We automatically assume the IV is given (external) if the IV generation method is not defined. | ||
var external_iv bool = true | ||
if group.IvGen != "" { | ||
switch group.IvGen { | ||
case "external": | ||
external_iv = true | ||
case "internal": | ||
external_iv = false | ||
default: | ||
return nil, fmt.Errorf("test group %d has unknown iv generation method %q", group.ID, group.IvGen) | ||
} | ||
switch group.IvGen { | ||
case "external": | ||
external_iv = true | ||
case "internal": | ||
external_iv = false | ||
default: | ||
return nil, fmt.Errorf("test group %d has unknown iv generation method %q", group.ID, group.IvGen) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oof thats a lot of formatting changes |
||
} | ||
|
||
op := a.algo + "/seal" | ||
|
@@ -198,21 +198,21 @@ func (a *aead) Process(vectorSet []byte, m Transactable) (interface{}, error) { | |
testResp.CiphertextHex = &ciphertextHex | ||
} else { | ||
if external_iv { | ||
ciphertext := result[0][:len(result[0])-tagBytes] | ||
ciphertextHex := hex.EncodeToString(ciphertext) | ||
testResp.CiphertextHex = &ciphertextHex | ||
tag := result[0][len(result[0])-tagBytes:] | ||
testResp.TagHex = hex.EncodeToString(tag) | ||
ciphertext := result[0][:len(result[0])-tagBytes] | ||
ciphertextHex := hex.EncodeToString(ciphertext) | ||
testResp.CiphertextHex = &ciphertextHex | ||
tag := result[0][len(result[0])-tagBytes:] | ||
testResp.TagHex = hex.EncodeToString(tag) | ||
} else { | ||
// internal IV length is always 12 bytes long | ||
var ivBytes int = 12 | ||
ciphertext := result[0][:len(result[0])-(tagBytes+ivBytes)] | ||
ciphertextHex := hex.EncodeToString(ciphertext) | ||
testResp.CiphertextHex = &ciphertextHex | ||
tag := result[0][len(result[0])-(tagBytes+ivBytes):len(result[0])-ivBytes] | ||
testResp.TagHex = hex.EncodeToString(tag) | ||
iv := result[0][len(result[0])-ivBytes:] | ||
testResp.IVHex = hex.EncodeToString(iv) | ||
// internal IV length is always 12 bytes long | ||
var ivBytes int = 12 | ||
ciphertext := result[0][:len(result[0])-(tagBytes+ivBytes)] | ||
ciphertextHex := hex.EncodeToString(ciphertext) | ||
testResp.CiphertextHex = &ciphertextHex | ||
tag := result[0][len(result[0])-(tagBytes+ivBytes) : len(result[0])-ivBytes] | ||
testResp.TagHex = hex.EncodeToString(tag) | ||
iv := result[0][len(result[0])-ivBytes:] | ||
testResp.IVHex = hex.EncodeToString(iv) | ||
} | ||
} | ||
} else { | ||
|
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,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR ISC | ||
package subprocess | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type kdaPrimitive struct { | ||
modes map[string]kdaModePrimitive | ||
} | ||
|
||
type kdaModePrimitive interface { | ||
ProcessKDA(vectorSet []byte, t Transactable) (interface{}, error) | ||
} | ||
|
||
func (k *kdaPrimitive) Process(vectorSet []byte, t Transactable) (interface{}, error) { | ||
var partial struct { | ||
Mode string `json:"mode"` | ||
} | ||
|
||
if err := json.Unmarshal(vectorSet, &partial); err != nil { | ||
return nil, err | ||
} | ||
|
||
prim, ok := k.modes[partial.Mode] | ||
if !ok { | ||
return nil, fmt.Errorf("unsupported KDA mode(%v)", partial.Mode) | ||
} | ||
|
||
return prim.ProcessKDA(vectorSet, t) | ||
} |
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,179 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR ISC | ||
|
||
package subprocess | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Processes Vectors defined by https://pages.nist.gov/ACVP/draft-hammett-acvp-kas-kdf-onestep.html | ||
type kdaOneStepMode struct{} | ||
|
||
func (k *kdaOneStepMode) ProcessKDA(vectorSet []byte, m Transactable) (interface{}, error) { | ||
var parsed kdaOneStepTestVectorSet | ||
|
||
if err := json.Unmarshal(vectorSet, &parsed); err != nil { | ||
return nil, err | ||
} | ||
|
||
var respGroups []kdaOneStepTestGroupResponse | ||
for _, group := range parsed.Groups { | ||
group := group | ||
groupResp := kdaOneStepTestGroupResponse{ID: group.ID} | ||
|
||
// determine the test type | ||
var isValidationTest bool | ||
switch group.Type { | ||
case "VAL": | ||
isValidationTest = true | ||
case "AFT": | ||
isValidationTest = false | ||
default: | ||
return nil, fmt.Errorf("unknown test type %q", group.Type) | ||
} | ||
|
||
// get the number of bytes to output and the auxillary function we're using | ||
outBytes, auxFunctionName, auxFuncType, err := group.Configuration.extract() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, test := range group.Tests { | ||
test := test | ||
testResp := kdaOneStepTestCaseResponse{ID: test.ID} | ||
|
||
info := test.fixedInfoBytes() | ||
|
||
var args [][]byte | ||
|
||
args = append(args, test.KDFParameter.Z) | ||
if auxFuncType == hmacAuxFunction { | ||
args = append(args, test.KDFParameter.Salt) | ||
} | ||
args = append(args, info) | ||
args = append(args, uint32le(outBytes)) | ||
|
||
resp, err := m.Transact("KDA/OneStep/"+auxFunctionName, 1, args...) | ||
if err != nil { | ||
return nil, fmt.Errorf("KDA_OneStep operation failed: %s", err) | ||
} | ||
|
||
if isValidationTest { | ||
passed := bytes.Equal(test.DerivedKeyMaterial, resp[0]) | ||
testResp.Passed = &passed | ||
} else { | ||
testResp.DerivedKeyMaterial = resp[0] | ||
} | ||
|
||
groupResp.Tests = append(groupResp.Tests, testResp) | ||
} | ||
respGroups = append(respGroups, groupResp) | ||
|
||
} | ||
|
||
return respGroups, nil | ||
} | ||
|
||
type kdaOneStepTestVectorSet struct { | ||
Groups []kdaOneStepTestGroup `json:"testGroups"` | ||
} | ||
|
||
type kdaOneStepTestGroup struct { | ||
ID uint64 `json:"tgId"` | ||
Type string `json:"testType"` | ||
Configuration kdaOneStepConfiguration `json:"kdfConfiguration"` | ||
Tests []kdaOneStepTest `json:"tests"` | ||
} | ||
|
||
type kdaOneStepConfiguration struct { | ||
Type string `json:"kdfType"` | ||
SaltMethod string `json:"saltMethod"` | ||
FixedInfoPattern string `json:"fixedInfoPattern"` | ||
FixedInfoEncoding string `json:"fixedInfoEncoding"` | ||
AuxFunction string `json:"auxFunction"` | ||
L uint32 `json:"l"` | ||
} | ||
|
||
func (k *kdaOneStepConfiguration) extract() (outLen uint32, auxFunction string, auxFuncType auxFunctionType, err error) { | ||
if k.Type != "oneStep" { | ||
return 0, "", unknownAuxFunction, fmt.Errorf("unexpected kdfType: %v", k.Type) | ||
} | ||
|
||
auxFuncType.setFromString(k.AuxFunction) | ||
|
||
if auxFuncType == unknownAuxFunction { | ||
return 0, "", unknownAuxFunction, fmt.Errorf("unknown auxillary function: %v", k.AuxFunction) | ||
} | ||
|
||
if k.FixedInfoPattern != "uPartyInfo||vPartyInfo" || k.FixedInfoEncoding != "concatenation" { | ||
return 0, "", unknownAuxFunction, fmt.Errorf("unsupported FixedInfo construction, pattern(%v) with encoding(%v)", k.FixedInfoPattern, k.FixedInfoEncoding) | ||
} | ||
|
||
return k.L / 8, k.AuxFunction, auxFuncType, nil | ||
} | ||
|
||
type kdaOneStepTest struct { | ||
ID uint64 `json:"tcId"` | ||
KDFParameter struct { | ||
Type string `json:"kdfType"` | ||
Salt hexEncodedByteString `json:"salt,omitempty"` | ||
Z hexEncodedByteString `json:"z"` | ||
} `json:"kdfParameter"` | ||
FixedInfoPartyU kdaOneStepFixedInfoParty `json:"fixedInfoPartyU"` | ||
FixedInfoPartyV kdaOneStepFixedInfoParty `json:"fixedInfoPartyV"` | ||
DerivedKeyMaterial hexEncodedByteString `json:"dkm,omitempty"` | ||
} | ||
|
||
func (k *kdaOneStepTest) fixedInfoBytes() []byte { | ||
uBytes := k.FixedInfoPartyU.concatenatedBytes() | ||
vBytes := k.FixedInfoPartyV.concatenatedBytes() | ||
v := make([]byte, 0, len(uBytes)+len(vBytes)) | ||
v = append(v, uBytes...) | ||
v = append(v, vBytes...) | ||
return v | ||
} | ||
|
||
type kdaOneStepFixedInfoParty struct { | ||
PartyID hexEncodedByteString `json:"partyId"` | ||
EphemeralData hexEncodedByteString `json:"ephemeralData"` | ||
} | ||
|
||
func (k *kdaOneStepFixedInfoParty) concatenatedBytes() []byte { | ||
v := make([]byte, 0, len(k.PartyID)+len(k.EphemeralData)) | ||
v = append(v, k.PartyID...) | ||
v = append(v, k.EphemeralData...) | ||
return v | ||
} | ||
|
||
type kdaOneStepTestGroupResponse struct { | ||
ID uint64 `json:"tgId"` | ||
Tests []kdaOneStepTestCaseResponse `json:"tests"` | ||
} | ||
|
||
type kdaOneStepTestCaseResponse struct { | ||
ID uint64 `json:"tcId"` | ||
Passed *bool `json:"testPassed,omitempty"` | ||
DerivedKeyMaterial hexEncodedByteString `json:"dkm,omitempty"` | ||
} | ||
|
||
type auxFunctionType uint | ||
|
||
func (a *auxFunctionType) setFromString(v string) { | ||
if strings.HasPrefix(v, "HMAC-") { | ||
*a = hmacAuxFunction | ||
} else if strings.HasPrefix(v, "SHA-") || strings.HasPrefix(v, "SHA2-") || strings.HasPrefix(v, "SHA3-") { | ||
*a = digestAuxFunction | ||
} else { | ||
*a = unknownAuxFunction | ||
} | ||
} | ||
|
||
const ( | ||
unknownAuxFunction auxFunctionType = iota | ||
digestAuxFunction | ||
hmacAuxFunction | ||
) |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is smart, but maybe in a separate PR?