github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/soc/nxp/caam/hash.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  	"github.com/usbarmory/tamago/dma"
    14  )
    15  
    16  func (hw *CAAM) hash(buf []byte, mode int, size int, init bool, term bool) (sum []byte, err error) {
    17  	sourceBufferAddress := dma.Alloc(buf, 4)
    18  	defer dma.Free(sourceBufferAddress)
    19  
    20  	op := Operation{}
    21  	op.SetDefaults()
    22  	op.OpType(OPTYPE_ALG_CLASS2)
    23  	op.Algorithm(mode, 0)
    24  
    25  	switch {
    26  	case init && term:
    27  		op.State(AS_INITIALIZE | AS_FINALIZE)
    28  	case init:
    29  		op.State(AS_INITIALIZE)
    30  	case term:
    31  		op.State(AS_FINALIZE)
    32  	default:
    33  		op.State(AS_UPDATE)
    34  	}
    35  
    36  	src := FIFOLoad{}
    37  	src.SetDefaults()
    38  	src.Class(2)
    39  	src.DataType(INPUT_DATA_TYPE_MESSAGE_DATA | INPUT_DATA_TYPE_LC2)
    40  	src.Pointer(sourceBufferAddress, len(buf))
    41  
    42  	jd := op.Bytes()
    43  	jd = append(jd, src.Bytes()...)
    44  
    45  	if term {
    46  		sum = make([]byte, size)
    47  
    48  		destinationBufferAddress := dma.Alloc(sum, 4)
    49  		defer dma.Free(destinationBufferAddress)
    50  
    51  		dst := Store{}
    52  		dst.SetDefaults()
    53  		dst.Class(2)
    54  		dst.Source(CTX)
    55  		dst.Pointer(destinationBufferAddress, len(sum))
    56  
    57  		jd = append(jd, dst.Bytes()...)
    58  
    59  		defer func() {
    60  			dma.Read(destinationBufferAddress, 0, sum)
    61  		}()
    62  	}
    63  
    64  	err = hw.job(nil, jd)
    65  
    66  	return
    67  }