github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/accounts/scwallet/apdu.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package scwallet
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/binary"
    22  	"fmt"
    23  )
    24  
    25  // commandAPDU represents an application data unit sent to a smartcard.
    26  type commandAPDU struct {
    27  	Cla, Ins, P1, P2 uint8  // Class, Instruction, Parameter 1, Parameter 2
    28  	Data             []byte // Command data
    29  	Le               uint8  // Command data length
    30  }
    31  
    32  // serialize serializes a command APDU.
    33  func (ca commandAPDU) serialize() ([]byte, error) {
    34  	buf := new(bytes.Buffer)
    35  
    36  	if err := binary.Write(buf, binary.BigEndian, ca.Cla); err != nil {
    37  		return nil, err
    38  	}
    39  	if err := binary.Write(buf, binary.BigEndian, ca.Ins); err != nil {
    40  		return nil, err
    41  	}
    42  	if err := binary.Write(buf, binary.BigEndian, ca.P1); err != nil {
    43  		return nil, err
    44  	}
    45  	if err := binary.Write(buf, binary.BigEndian, ca.P2); err != nil {
    46  		return nil, err
    47  	}
    48  	if len(ca.Data) > 0 {
    49  		if err := binary.Write(buf, binary.BigEndian, uint8(len(ca.Data))); err != nil {
    50  			return nil, err
    51  		}
    52  		if err := binary.Write(buf, binary.BigEndian, ca.Data); err != nil {
    53  			return nil, err
    54  		}
    55  	}
    56  	if err := binary.Write(buf, binary.BigEndian, ca.Le); err != nil {
    57  		return nil, err
    58  	}
    59  	return buf.Bytes(), nil
    60  }
    61  
    62  // responseAPDU represents an application data unit received from a smart card.
    63  type responseAPDU struct {
    64  	Data     []byte // response data
    65  	Sw1, Sw2 uint8  // status words 1 and 2
    66  }
    67  
    68  // deserialize deserializes a response APDU.
    69  func (ra *responseAPDU) deserialize(data []byte) error {
    70  	if len(data) < 2 {
    71  		return fmt.Errorf("can not deserialize data: payload too short (%d < 2)", len(data))
    72  	}
    73  
    74  	ra.Data = make([]byte, len(data)-2)
    75  
    76  	buf := bytes.NewReader(data)
    77  	if err := binary.Read(buf, binary.BigEndian, &ra.Data); err != nil {
    78  		return err
    79  	}
    80  	if err := binary.Read(buf, binary.BigEndian, &ra.Sw1); err != nil {
    81  		return err
    82  	}
    83  	if err := binary.Read(buf, binary.BigEndian, &ra.Sw2); err != nil {
    84  		return err
    85  	}
    86  	return nil
    87  }