github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/soc/nxp/caam/command.go (about)

     1  // NXP Cryptographic Acceleration and Assurance Module (CAAM) driver
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // Copyright (c) WithSecure Corporation
     5  // https://foundry.withsecure.com
     6  //
     7  // Use of this source code is governed by the license
     8  // that can be found in the LICENSE file.
     9  
    10  package caam
    11  
    12  import (
    13  	"bytes"
    14  	"encoding/binary"
    15  
    16  	"github.com/usbarmory/tamago/bits"
    17  )
    18  
    19  // p266, 6.6.7.3 Command types, IMX6ULSRM
    20  const (
    21  	CTYPE = 27
    22  
    23  	CTYPE_KEY         = 0b00000
    24  	CTYPE_LOAD        = 0b00010
    25  	CTYPE_FIFO_LOAD   = 0b00100
    26  	CTYPE_STORE       = 0b01010
    27  	CTYPE_FIFO_STORE  = 0b01100
    28  	CTYPE_OPERATION   = 0b10000
    29  	CTYPE_JUMP        = 0b10100
    30  	CTYPE_HEADER      = 0b10110
    31  	CTYPE_SEQ_IN_PTR  = 0b11110
    32  	CTYPE_SEQ_OUT_PTR = 0b11111
    33  )
    34  
    35  // Fields common across multiple commands
    36  const (
    37  	// KEY, LOAD, STORE, JUMP commands
    38  	CLASS = 25
    39  
    40  	// ALGORITHM, PROTOCOL, PKHA OPERATION commands
    41  	OPERATION_OPTYPE = 24
    42  
    43  	// LOAD, STORE commands
    44  	EXT       = 22
    45  	DATA_TYPE = 16
    46  	LENGTH    = 0
    47  )
    48  
    49  // Field values common across multiple commands
    50  const (
    51  	// LOAD, STORE commands
    52  	CLRW = 0x08
    53  	CTX  = 0x20
    54  )
    55  
    56  // p285, 6.6.10 JUMP command, IMX6ULSRM
    57  const (
    58  	LOAD_IMM = 23
    59  )
    60  
    61  // p296, 6.6.11 FIFO LOAD command, IMX6ULSRM
    62  // p313, 6.6.14 FIFO STORE command, IMX6ULSRM
    63  const (
    64  	INPUT_DATA_TYPE_PKHA_Ax      = 0b000000
    65  	INPUT_DATA_TYPE_IV           = 0b100000
    66  	INPUT_DATA_TYPE_MESSAGE_DATA = 0b010000
    67  	INPUT_DATA_TYPE_LC2          = 1 << 2
    68  	INPUT_DATA_TYPE_LC1          = 1 << 1
    69  
    70  	OUTPUT_DATA_TYPE_MESSAGE_DATA = 0x30
    71  )
    72  
    73  // p328, 6.6.16 ALGORITHM OPERATION command, IMX6ULSRM
    74  const (
    75  	OPTYPE_ALG_CLASS1 = 0b010
    76  	OPTYPE_ALG_CLASS2 = 0b100
    77  
    78  	OPERATION_ALG = 16
    79  	ALG_AES       = 0x10
    80  	ALG_SHA256    = 0x43
    81  	ALG_SHA512    = 0x45
    82  	ALG_RNG       = 0x50
    83  
    84  	OPERATION_AAI = 4
    85  	AAI_AES_CBC   = 0x10
    86  	AAI_AES_CMAC  = 0x60
    87  	AAI_RNG_SK    = 8
    88  
    89  	OPERATION_AS  = 2
    90  	AS_UPDATE     = 0b00
    91  	AS_INITIALIZE = 0b01
    92  	AS_FINALIZE   = 0b10
    93  
    94  	OPERATION_ENC = 0
    95  )
    96  
    97  // p333, 6.6.17 PROTOCOL OPERATION command, IMX6ULSRM
    98  const (
    99  	OPTYPE_PROT_UNI = 0b000
   100  	OPTYPE_PROT_DEC = 0b110
   101  	OPTYPE_PROT_ENC = 0b111
   102  
   103  	OPERATION_PROTID  = 16
   104  	PROTID_BLOB       = 0x0d
   105  	PROTID_ECDSA_SIGN = 0x15
   106  
   107  	OPERATION_PROTINFO = 0
   108  
   109  	PROTINFO_BLOB_FORMAT = 0
   110  	BLOB_FORMAT_MKV      = 0b10
   111  
   112  	PROTINFO_SIGN_NO_TEQ = 12
   113  	PROTINFO_ECC         = 1
   114  )
   115  
   116  // p356, 6.6.20 JUMP command, IMX6ULSRM
   117  const (
   118  	JUMP_OFFSET = 0
   119  )
   120  
   121  // p276, 6.6.8 HEADER command, IMX6ULSRM
   122  const (
   123  	HEADER_ONE         = 23
   124  	HEADER_START_INDEX = 16
   125  	HEADER_DESCLEN     = 0
   126  )
   127  
   128  // Command represents a CAAM command
   129  // (p266, 6.6.7.3 Command types, IMX6ULSRM).
   130  type Command struct {
   131  	// Main fields
   132  	Word0 uint32
   133  	// Optional words
   134  	Words []uint32
   135  }
   136  
   137  // Class sets the command CLASS field.
   138  func (c *Command) Class(class int) {
   139  	bits.SetN(&c.Word0, CLASS, 0b11, uint32(class))
   140  }
   141  
   142  // Bytes converts the descriptor non-optional words structure to byte array
   143  // format.
   144  func (c *Command) Bytes() []byte {
   145  	buf := new(bytes.Buffer)
   146  	binary.Write(buf, binary.LittleEndian, c.Word0)
   147  	binary.Write(buf, binary.LittleEndian, c.Words)
   148  
   149  	return buf.Bytes()
   150  }
   151  
   152  // LengthCommand represents a CAAM command with Length and Pointer fields.
   153  type LengthCommand struct {
   154  	Command
   155  }
   156  
   157  // Pointer sets the command POINTER and EXT LENGTH fields.
   158  func (c *LengthCommand) Pointer(addr uint, n int) {
   159  	bits.SetN(&c.Word0, LENGTH, 0xff, uint32(n))
   160  	c.Words = []uint32{uint32(addr)}
   161  }
   162  
   163  // ExtendedLengthCommand represents a CAAM command with Extended Length and
   164  // Pointer fields.
   165  type ExtendedLengthCommand struct {
   166  	Command
   167  }
   168  
   169  // Pointer sets the command POINTER and EXT LENGTH fields.
   170  func (c *ExtendedLengthCommand) Pointer(addr uint, n int) {
   171  	bits.Set(&c.Word0, EXT)
   172  	c.Words = []uint32{
   173  		uint32(addr),
   174  		uint32(n),
   175  	}
   176  }
   177  
   178  // Key represents a KEY command
   179  // (p281, 6.6.9 KEY commands, IMX6ULSRM).
   180  type Key struct {
   181  	LengthCommand
   182  }
   183  
   184  // SetDefaults initializes default values for the KEY command.
   185  func (c *Key) SetDefaults() {
   186  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_KEY)
   187  }
   188  
   189  // Load represents a LOAD command
   190  // (p285, 6.6.10 LOAD commands, IMX6ULSRM).
   191  type Load struct {
   192  	LengthCommand
   193  }
   194  
   195  // SetDefaults initializes default values for the LOAD command.
   196  func (c *Load) SetDefaults() {
   197  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_LOAD)
   198  }
   199  
   200  // Destination sets the LOAD command DST field.
   201  func (c *Load) Destination(dst int) {
   202  	bits.SetN(&c.Word0, DATA_TYPE, 0x7f, uint32(dst))
   203  }
   204  
   205  // Immediate sets the LOAD command IMM, LENGTH and Value fields.
   206  func (c *Load) Immediate(imm uint32) {
   207  	bits.Set(&c.Word0, LOAD_IMM)
   208  	bits.SetN(&c.Word0, LENGTH, 0xff, 4)
   209  	c.Words = []uint32{imm}
   210  }
   211  
   212  // FIFOLoad represents a FIFO LOAD command
   213  // (p296, 6.6.11 FIFO LOAD command, IMX6ULSRM).
   214  type FIFOLoad struct {
   215  	ExtendedLengthCommand
   216  }
   217  
   218  // SetDefaults initializes default values for the FIFO LOAD command.
   219  func (c *FIFOLoad) SetDefaults() {
   220  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_FIFO_LOAD)
   221  }
   222  
   223  // DataType sets the FIFO LOAD command INPUT DATA TYPE field.
   224  func (c *FIFOLoad) DataType(dt int) {
   225  	bits.SetN(&c.Word0, DATA_TYPE, 0x3f, uint32(dt))
   226  }
   227  
   228  // Store represents a STORE command
   229  // (p306, 6.6.13 STORE command, IMX6ULSRM).
   230  type Store struct {
   231  	LengthCommand
   232  }
   233  
   234  // SetDefaults initializes default values for the STORE command.
   235  func (c *Store) SetDefaults() {
   236  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_STORE)
   237  }
   238  
   239  // Source sets the STORE command SRC field.
   240  func (c *Store) Source(src int) {
   241  	bits.SetN(&c.Word0, DATA_TYPE, 0x7f, uint32(src))
   242  }
   243  
   244  // FIFOStore represents a FIFO STORE command
   245  // (p313, 6.6.14 FIFO STORE command, IMX6ULSRM).
   246  type FIFOStore struct {
   247  	ExtendedLengthCommand
   248  }
   249  
   250  // SetDefaults initializes default values for the FIFO STORE command.
   251  func (c *FIFOStore) SetDefaults() {
   252  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_FIFO_STORE)
   253  }
   254  
   255  // DataType sets the FIFO STORE command OUTPUT DATA TYPE field.
   256  func (c *FIFOStore) DataType(dt int) {
   257  	bits.SetN(&c.Word0, DATA_TYPE, 0x3f, uint32(dt))
   258  }
   259  
   260  // Operation represents a CAAM OPERATION command.
   261  type Operation struct {
   262  	Command
   263  }
   264  
   265  // SetDefaults initializes default values for the OPERATION command.
   266  func (c *Operation) SetDefaults() {
   267  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_OPERATION)
   268  }
   269  
   270  // OpType sets the OPERATION command OPTYPE field.
   271  func (c *Operation) OpType(op int) {
   272  	bits.SetN(&c.Word0, OPERATION_OPTYPE, 0b111, uint32(op))
   273  }
   274  
   275  // Algorithm sets the ALGORITHM OPERATION command ALG and AAI fields.
   276  func (c *Operation) Algorithm(alg int, aai uint32) {
   277  	bits.SetN(&c.Word0, OPERATION_ALG, 0xff, uint32(alg))
   278  	bits.SetN(&c.Word0, OPERATION_AAI, 0x1ff, aai)
   279  }
   280  
   281  // Algorithm sets the ALGORITHM OPERATION command AS field.
   282  func (c *Operation) State(as int) {
   283  	bits.SetN(&c.Word0, OPERATION_AS, 0b11, uint32(as))
   284  }
   285  
   286  // Algorithm sets the ALGORITHM OPERATION command ENC field.
   287  func (c *Operation) Encrypt(enc bool) {
   288  	bits.SetTo(&c.Word0, OPERATION_ENC, enc)
   289  }
   290  
   291  // Protocol sets the PROTOCOL OPERATION command PROTID and PROTINFO fields.
   292  func (c *Operation) Protocol(id int, info uint32) {
   293  	bits.SetN(&c.Word0, OPERATION_PROTID, 0b111, uint32(id))
   294  	bits.SetN(&c.Word0, OPERATION_PROTINFO, 0xffff, info)
   295  }
   296  
   297  // Header represents a CAAM HEADER command.
   298  // (p276, 6.6.8 HEADER command, IMX6ULSRM).
   299  type Header struct {
   300  	Command
   301  }
   302  
   303  // SetDefaults initializes default values for the HEADER command.
   304  func (c *Header) SetDefaults() {
   305  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_HEADER)
   306  	bits.Set(&c.Word0, HEADER_ONE)
   307  }
   308  
   309  // Length sets the HEADER command DESCLEN field.
   310  func (c *Header) Length(words int) {
   311  	bits.SetN(&c.Word0, HEADER_DESCLEN, 0x7f, uint32(words))
   312  }
   313  
   314  // StartIndex sets the HEADER command START INDEX field.
   315  func (c *Header) StartIndex(off int) {
   316  	bits.SetN(&c.Word0, HEADER_START_INDEX, 0x3f, uint32(off))
   317  }
   318  
   319  // Jump represents a CAAM JUMP command.
   320  // (p356, 6.6.20 JUMP command, IMX6ULSRM).
   321  type Jump struct {
   322  	Command
   323  }
   324  
   325  // SetDefaults initializes default values for the JUMP command.
   326  func (c *Jump) SetDefaults() {
   327  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_JUMP)
   328  }
   329  
   330  // Offset sets the JUMP command LOCAL OFFSET field.
   331  func (c *Jump) Offset(off int) {
   332  	bits.SetN(&c.Word0, JUMP_OFFSET, 0xff, uint32(off))
   333  }
   334  
   335  // SeqInPtr represents a CAAM SEQ IN PTR command
   336  // (p372, 6.6.22 SEQ  IN PTR command, IMX6ULSRM).
   337  type SeqInPtr struct {
   338  	ExtendedLengthCommand
   339  }
   340  
   341  // SetDefaults initializes default values for the SEQ IN PTR command.
   342  func (c *SeqInPtr) SetDefaults() {
   343  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_SEQ_IN_PTR)
   344  }
   345  
   346  // SeqOutPtr represents a CAAM SEQ OUT PTR command
   347  // (p375, 6.6.23 SEQ OUT PTR command, IMX6ULSRM).
   348  type SeqOutPtr struct {
   349  	ExtendedLengthCommand
   350  }
   351  
   352  // SetDefaults initializes default values for the SEQ OUT PTR command.
   353  func (c *SeqOutPtr) SetDefaults() {
   354  	bits.SetN(&c.Word0, CTYPE, 0x1f, CTYPE_SEQ_OUT_PTR)
   355  }