1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
// SPDX-FileCopyrightText: 2022 Tillitis AB <tillitis.se>
// SPDX-License-Identifier: BSD-2-Clause
// Package tkeysign provides a connection to the ed25519 signer app
// running on the TKey. You're expected to pass an existing connection
// to it, so use it like this:
//
// tk := tkeyclient.New()
// err := tk.Connect(port)
// err := tk.LoadApp(appBinary, secretPhrase)
// signer := tkeysign.New(tk)
//
// Then use it like this to get the public key of the TKey:
//
// pubkey, err := signer.GetPubkey()
//
// And like this to sign a message:
//
// signature, err := signer.Sign(message)
package tkeysign
import (
"fmt"
"github.com/tillitis/tkeyclient"
)
var (
cmdGetPubkey = appCmd{0x01, "cmdGetPubkey", tkeyclient.CmdLen1}
rspGetPubkey = appCmd{0x02, "rspGetPubkey", tkeyclient.CmdLen128}
cmdSetSize = appCmd{0x03, "cmdSetSize", tkeyclient.CmdLen32}
rspSetSize = appCmd{0x04, "rspSetSize", tkeyclient.CmdLen4}
cmdSignData = appCmd{0x05, "cmdSignData", tkeyclient.CmdLen128}
rspSignData = appCmd{0x06, "rspSignData", tkeyclient.CmdLen4}
cmdGetSig = appCmd{0x07, "cmdGetSig", tkeyclient.CmdLen1}
rspGetSig = appCmd{0x08, "rspGetSig", tkeyclient.CmdLen128}
cmdGetNameVersion = appCmd{0x09, "cmdGetNameVersion", tkeyclient.CmdLen1}
rspGetNameVersion = appCmd{0x0a, "rspGetNameVersion", tkeyclient.CmdLen32}
cmdGetFirmwareHash = appCmd{0x0b, "cmdGetFirmwareHash", tkeyclient.CmdLen32}
rspGetFirmwareHash = appCmd{0x0c, "rspGetFirmwareHash", tkeyclient.CmdLen128}
)
const MaxSignSize = 4096
type appCmd struct {
code byte
name string
cmdLen tkeyclient.CmdLen
}
func (c appCmd) Code() byte {
return c.code
}
func (c appCmd) CmdLen() tkeyclient.CmdLen {
return c.cmdLen
}
func (c appCmd) Endpoint() tkeyclient.Endpoint {
return tkeyclient.DestApp
}
func (c appCmd) String() string {
return c.name
}
type Signer struct {
tk *tkeyclient.TillitisKey // A connection to a TKey
}
// New allocates a struct for communicating with the ed25519 signer
// app running on the TKey. You're expected to pass an existing
// connection to it, so use it like this:
//
// tk := tkeyclient.New()
// err := tk.Connect(port)
// signer := tk1sign.New(tk)
func New(tk *tkeyclient.TillitisKey) Signer {
var signer Signer
signer.tk = tk
return signer
}
// Close closes the connection to the TKey
func (s Signer) Close() error {
if err := s.tk.Close(); err != nil {
return fmt.Errorf("tk.Close: %w", err)
}
return nil
}
// GetAppNameVersion gets the name and version of the running app in
// the same style as the stick itself.
func (s Signer) GetAppNameVersion() (*tkeyclient.NameVersion, error) {
id := 2
tx, err := tkeyclient.NewFrameBuf(cmdGetNameVersion, id)
if err != nil {
return nil, fmt.Errorf("NewFrameBuf: %w", err)
}
tkeyclient.Dump("GetAppNameVersion tx", tx)
if err = s.tk.Write(tx); err != nil {
return nil, fmt.Errorf("Write: %w", err)
}
s.tk.SetReadTimeoutNoErr(2)
defer s.tk.SetReadTimeoutNoErr(0)
rx, _, err := s.tk.ReadFrame(rspGetNameVersion, id)
if err != nil {
return nil, fmt.Errorf("ReadFrame: %w", err)
}
nameVer := &tkeyclient.NameVersion{}
nameVer.Unpack(rx[2:])
return nameVer, nil
}
// GetPubkey fetches the public key of the signer.
func (s Signer) GetPubkey() ([]byte, error) {
id := 2
tx, err := tkeyclient.NewFrameBuf(cmdGetPubkey, id)
if err != nil {
return nil, fmt.Errorf("NewFrameBuf: %w", err)
}
tkeyclient.Dump("GetPubkey tx", tx)
if err = s.tk.Write(tx); err != nil {
return nil, fmt.Errorf("Write: %w", err)
}
rx, _, err := s.tk.ReadFrame(rspGetPubkey, id)
tkeyclient.Dump("GetPubKey rx", rx)
if err != nil {
return nil, fmt.Errorf("ReadFrame: %w", err)
}
// Skip frame header & app header, returning size of ed25519 pubkey
return rx[2 : 2+32], nil
}
// Sign signs the message in data and returns an ed25519 signature.
func (s Signer) Sign(data []byte) ([]byte, error) {
err := s.setSize(len(data))
if err != nil {
return nil, fmt.Errorf("setSize: %w", err)
}
var offset int
for nsent := 0; offset < len(data); offset += nsent {
nsent, err = s.signLoad(data[offset:])
if err != nil {
return nil, fmt.Errorf("signLoad: %w", err)
}
}
if offset > len(data) {
return nil, fmt.Errorf("transmitted more than expected")
}
signature, err := s.getSig()
if err != nil {
return nil, fmt.Errorf("getSig: %w", err)
}
return signature, nil
}
// SetSize sets the size of the data to sign.
func (s Signer) setSize(size int) error {
id := 2
tx, err := tkeyclient.NewFrameBuf(cmdSetSize, id)
if err != nil {
return fmt.Errorf("NewFrameBuf: %w", err)
}
// Set size
tx[2] = byte(size)
tx[3] = byte(size >> 8)
tx[4] = byte(size >> 16)
tx[5] = byte(size >> 24)
tkeyclient.Dump("SetSignSize tx", tx)
if err = s.tk.Write(tx); err != nil {
return fmt.Errorf("Write: %w", err)
}
rx, _, err := s.tk.ReadFrame(rspSetSize, id)
tkeyclient.Dump("SetSignSize rx", rx)
if err != nil {
return fmt.Errorf("ReadFrame: %w", err)
}
if rx[2] != tkeyclient.StatusOK {
return fmt.Errorf("SetSignSize NOK")
}
return nil
}
// signload loads a chunk of a message to sign and waits for a
// response from the signer.
func (s Signer) signLoad(content []byte) (int, error) {
id := 2
tx, err := tkeyclient.NewFrameBuf(cmdSignData, id)
if err != nil {
return 0, fmt.Errorf("NewFrameBuf: %w", err)
}
payload := make([]byte, cmdSignData.CmdLen().Bytelen()-1)
copied := copy(payload, content)
// Add padding if not filling the payload buffer.
if copied < len(payload) {
padding := make([]byte, len(payload)-copied)
copy(payload[copied:], padding)
}
copy(tx[2:], payload)
tkeyclient.Dump("LoadSignData tx", tx)
if err = s.tk.Write(tx); err != nil {
return 0, fmt.Errorf("Write: %w", err)
}
// Wait for reply
rx, _, err := s.tk.ReadFrame(rspSignData, id)
if err != nil {
return 0, fmt.Errorf("ReadFrame: %w", err)
}
if rx[2] != tkeyclient.StatusOK {
return 0, fmt.Errorf("SignData NOK")
}
return copied, nil
}
// getSig gets the ed25519 signature from the signer app, if
// available.
func (s Signer) getSig() ([]byte, error) {
id := 2
tx, err := tkeyclient.NewFrameBuf(cmdGetSig, id)
if err != nil {
return nil, fmt.Errorf("NewFrameBuf: %w", err)
}
tkeyclient.Dump("getSig tx", tx)
if err = s.tk.Write(tx); err != nil {
return nil, fmt.Errorf("Write: %w", err)
}
rx, _, err := s.tk.ReadFrame(rspGetSig, id)
if err != nil {
return nil, fmt.Errorf("ReadFrame: %w", err)
}
if rx[2] != tkeyclient.StatusOK {
return nil, fmt.Errorf("getSig NOK")
}
// Skip frame header, app header, and status; returning size of
// ed25519 signature
return rx[3 : 3+64], nil
}
// GetFWDigest asks the signer app to hash len bytes of the firmware.
//
// It returns the resulting SHA512 digest or an error.
func (s Signer) GetFWDigest(len int) ([]byte, error) {
id := 2
tx, err := tkeyclient.NewFrameBuf(cmdGetFirmwareHash, id)
if err != nil {
return nil, fmt.Errorf("NewFrameBuf: %w", err)
}
// Set firmware length as 32 bit LE
tx[2] = byte(len)
tx[3] = byte(len >> 8)
tx[4] = byte(len >> 16)
tx[5] = byte(len >> 24)
tkeyclient.Dump("GetFirmwareHash tx", tx)
if err = s.tk.Write(tx); err != nil {
return nil, fmt.Errorf("Write: %w", err)
}
// Wait for reply
rx, _, err := s.tk.ReadFrame(rspGetFirmwareHash, id)
if err != nil {
return nil, fmt.Errorf("ReadFrame: %w", err)
}
tkeyclient.Dump("GetFirmwareHash rx", rx)
if rx[2] != tkeyclient.StatusOK {
return nil, fmt.Errorf("GetFirmwareHash NOK")
}
// Skip frame header, app header, and status; returning
// firmware SHA512 digest
return rx[3 : 3+64], nil
}
|