mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 21:03:23 +00:00
ateccx08: inttial implementation for ATECCx08
This working implementation for the ATECCx08 family of cryptgraphic processors has random number generation and other needed supporting functions. It also includes a sample of how to connect it to the Go crypto/rand package. More cryptographic functions await a future interation. Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// connects the Go crypto/rand package to the random number generation
|
||||
// on the ATECCx08 cryptographic processor.
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
errNoATECC = errors.New("no ATECCx08")
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Reader = &reader{}
|
||||
}
|
||||
|
||||
type reader struct{}
|
||||
|
||||
func (r *reader) Read(b []byte) (n int, err error) {
|
||||
if len(b) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if atecc == nil {
|
||||
return 0, errNoATECC
|
||||
}
|
||||
|
||||
if !atecc.IsLocked() {
|
||||
panic("ATECCx08 is not locked and cannot produce random numbers!")
|
||||
}
|
||||
|
||||
rnds, err := atecc.Random()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(b); i += 32 {
|
||||
if i+32 > len(b) {
|
||||
copy(b[i:], rnds[:(len(b)-i)])
|
||||
break
|
||||
}
|
||||
|
||||
copy(b[i:], rnds[:])
|
||||
rnds, err = atecc.Random()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
Reference in New Issue
Block a user