Files
tinygo/src/machine/machine_tkey_rom.go
T
Ron Evans 17302ca762 targets: add implementation for Tillitis TKey device (#4631)
* initial implementation for Tillitis TKey device
* add UART implementation for TKey
* add Pin interface implementation for TKey touch sensor
* add RNG interface implementation for TKey
* add helpful machine package functions to return identifiers such as name and version for TKey
* use built-in timer for sleep timing on TKey
* modify UART implementation for TKey to implement Serialer interface
* implement BLAKE2s ROM function call for TKey device
* handle abort by triggering TKey device fault using illegal instruction to halt CPU
* simplify TKey implementation by inheriting from existing riscv32 target
* return error for trying to configure invalid baudrates on UART
* add tkey to builder test
* be very specific for features passed to LLVM for specific config in use for TKey
* handle feedback items from TKey device code review

Signed-off-by: deadprogram <ron@hybridgroup.com>
2024-12-14 14:26:03 +01:00

60 lines
1.4 KiB
Go

//go:build tkey
package machine
/*
#define TK1_MMIO_TK1_BLAKE2S 0xff000040
typedef unsigned char uint8_t;
typedef unsigned long uint32_t;
typedef unsigned long size_t;
// blake2s state context
typedef struct {
uint8_t b[64]; // input buffer
uint32_t h[8]; // chained state
uint32_t t[2]; // total number of bytes
size_t c; // pointer for b[]
size_t outlen; // digest size
} blake2s_ctx;
typedef int (*fw_blake2s_p)(void *out, unsigned long outlen, const void *key,
unsigned long keylen, const void *in,
unsigned long inlen, blake2s_ctx *ctx);
int blake2s(void *out, unsigned long outlen, const void *key, unsigned long keylen, const void *in, unsigned long inlen)
{
fw_blake2s_p const fw_blake2s =
(fw_blake2s_p) * (volatile uint32_t *)TK1_MMIO_TK1_BLAKE2S;
blake2s_ctx ctx;
return fw_blake2s(out, outlen, key, keylen, in, inlen, &ctx);
}
*/
import "C"
import (
"errors"
"unsafe"
)
var (
ErrBLAKE2sInvalid = errors.New("invalid params for call to BLAKE2s")
ErrBLAKE2sFailed = errors.New("call to BLAKE2s failed")
)
func BLAKE2s(output []byte, key []byte, input []byte) error {
if len(output) == 0 || len(input) == 0 {
return ErrBLAKE2sInvalid
}
op := unsafe.Pointer(&output[0])
kp := unsafe.Pointer(&key[0])
ip := unsafe.Pointer(&input[0])
if res := C.blake2s(op, C.size_t(len(output)), kp, C.size_t(len(key)), ip, C.size_t(len(input))); res != 0 {
return ErrBLAKE2sFailed
}
return nil
}