add GenerateMnemonic

This commit is contained in:
Andrea Franz 2021-10-19 11:22:39 +02:00
parent 8877e09ca4
commit 2eea99ee0c
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
2 changed files with 40 additions and 0 deletions

View File

@ -1,8 +1,10 @@
package keycard
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
@ -14,6 +16,7 @@ import (
)
var ErrNoAvailablePairingSlots = errors.New("no available pairing slots")
var ErrBadChecksumSize = errors.New("bad checksum size")
type WrongPINError struct {
RemainingAttempts int
@ -231,6 +234,32 @@ func (cs *CommandSet) GenerateKey() ([]byte, error) {
return resp.Data, nil
}
func (cs *CommandSet) GenerateMnemonic(checksumSize int) ([]int, error) {
if checksumSize < 4 || checksumSize > 8 {
return nil, ErrBadChecksumSize
}
cmd := NewCommandGenerateMnemonic(byte(checksumSize))
resp, err := cs.sc.Send(cmd)
if err = cs.checkOK(resp, err); err != nil {
return nil, err
}
buf := bytes.NewBuffer(resp.Data)
indexes := make([]int, 0)
for {
var index int16
err := binary.Read(buf, binary.BigEndian, &index)
if err != nil {
break
}
indexes = append(indexes, int(index))
}
return indexes, nil
}
func (cs *CommandSet) RemoveKey() error {
cmd := NewCommandRemoveKey()
resp, err := cs.sc.Send(cmd)

View File

@ -26,6 +26,7 @@ const (
InsSign = 0xC0
InsSetPinlessPath = 0xC1
InsLoadKey = 0xD0
InsGenerateMnemonic = 0xD2
P1PairingFirstStep = 0x00
P1PairingFinalStep = 0x01
@ -131,6 +132,16 @@ func NewCommandGenerateKey() *apdu.Command {
)
}
func NewCommandGenerateMnemonic(checksumSize byte) *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,
InsGenerateMnemonic,
checksumSize,
0,
[]byte{},
)
}
func NewCommandRemoveKey() *apdu.Command {
return apdu.NewCommand(
globalplatform.ClaGp,