Skip to content

Commit

Permalink
Merge pull request #11 from gdbelvin/putwrapkey
Browse files Browse the repository at this point in the history
PutWrapKey
  • Loading branch information
hendrikhofstadt authored Mar 19, 2021
2 parents 7f6fc39 + fe9ad5f commit 1734a5c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
*.iml
*.swp
*.swo
42 changes: 42 additions & 0 deletions commands/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,45 @@ func CreateGetPseudoRandomCommand(numBytes uint16) *CommandMessage {

return command
}

func CreatePutWrapkeyCommand(objID uint16, label []byte, domains uint16, capabilities uint64, algorithm Algorithm, delegated uint64, wrapkey []byte) (*CommandMessage, error) {
if len(label) > LabelLength {
return nil, errors.New("label is too long")
}
if len(label) < LabelLength {
label = append(label, bytes.Repeat([]byte{0x00}, LabelLength-len(label))...)
}
switch algorithm {
case AlgorithmAES128CCMWrap:
if keyLen := len(wrapkey); keyLen != 16 {
return nil, errors.New("wrapkey is wrong length")
}
case AlgorithmAES192CCMWrap:
if keyLen := len(wrapkey); keyLen != 24 {
return nil, errors.New("wrapkey is wrong length")
}
case AlgorithmAES256CCMWrap:
if keyLen := len(wrapkey); keyLen != 32 {
return nil, errors.New("wrapkey is wrong length")
}
default:
return nil, errors.New("invalid algorithm")
}

command := &CommandMessage{
CommandType: CommandTypePutWrapKey,
}

payload := bytes.NewBuffer([]byte{})
binary.Write(payload, binary.BigEndian, objID)
payload.Write(label)
binary.Write(payload, binary.BigEndian, domains)
binary.Write(payload, binary.BigEndian, capabilities)
binary.Write(payload, binary.BigEndian, algorithm)
binary.Write(payload, binary.BigEndian, delegated)
payload.Write(wrapkey)

command.Data = payload.Bytes()

return command, nil
}
19 changes: 19 additions & 0 deletions commands/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ type (
ChangeAuthenticationKeyResponse struct {
ObjectID uint16
}

PutWrapkeyResponse struct {
ObjectID uint16
}
)

// ParseResponse parses the binary response from the card to the relevant Response type.
Expand Down Expand Up @@ -139,6 +143,8 @@ func ParseResponse(data []byte) (Response, error) {
return parseChangeAuthenticationKeyResponse(payload)
case CommandTypeGetPseudoRandom:
return parseGetPseudoRandomResponse(payload), nil
case CommandTypePutWrapKey:
return parsePutWrapkeyResponse(payload)
case ErrorResponseCode:
return nil, parseErrorResponse(payload)
default:
Expand Down Expand Up @@ -288,6 +294,19 @@ func parseGetPseudoRandomResponse(payload []byte) Response {
return payload
}

func parsePutWrapkeyResponse(payload []byte) (Response, error) {
if len(payload) != 2 {
return nil, errors.New("invalid response payload length")
}

var objectID uint16
err := binary.Read(bytes.NewReader(payload), binary.BigEndian, &objectID)
if err != nil {
return nil, err
}
return &PutWrapkeyResponse{ObjectID: objectID}, nil
}

// Error formats a card error message into a human readable format
func (e *Error) Error() string {
message := ""
Expand Down
3 changes: 3 additions & 0 deletions commands/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const (
AlgorithmSecp256k1 Algorithm = 15
AlgorithmYubicoAESAuthentication Algorithm = 38
AlgorighmED25519 Algorithm = 46
AlgorithmAES128CCMWrap Algorithm = 29
AlgorithmAES192CCMWrap Algorithm = 41
AlgorithmAES256CCMWrap Algorithm = 42

// Capabilities
CapabilityGetOpaque uint64 = 0x0000000000000001
Expand Down

0 comments on commit 1734a5c

Please sign in to comment.