tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.54k stars 915 forks source link

sha512 HardFault #3608

Open hugolgst opened 1 year ago

hugolgst commented 1 year ago

Hello,

I am building on ATSAMD21E18A and for some reason when I use sha512.New in pbkdf2.Key, my program crashes and prints a single "f" in the logs. I tried using sha256 and it works perfectly.

I identified that the problem happens when pbkdf2.Key is calling the Sum function, here: https://cs.opensource.google/go/x/crypto/+/refs/tags/v0.7.0:pbkdf2/pbkdf2.go;l=61 https://cs.opensource.google/go/go/+/refs/tags/go1.20.2:src/crypto/sha512/sha512.go;l=282

I cannot seem to get more information on this issue, anyone got an idea? Thank you in advance

–hugo

hugolgst commented 1 year ago
Screenshot 2023-03-27 alle 16 31 17
dgryski commented 1 year ago

Does this reproduce for native code or any other platform?

hugolgst commented 1 year ago

@dgryski The code works well on my machine but does not either on the board I am adding to TinyGo nor the Raspberry Pico.

soypat commented 1 year ago

I am struggling to reproduce the issue on the RP2040 (pico). I've come up with the following:

package main

import (
    "crypto/sha512"
    "time"

    "golang.org/x/crypto/pbkdf2"
)

func main() {
    const last = 4 * 256
    time.Sleep(time.Second)
    println("start program")
    var (
        salt = []byte("add some salt and pepper")
    )

    for i := 0; i < 10000; i++ {
        b := make([]byte, last+i)
        for j := 0; j < last+i; j++ {
            b[j] = byte(i)
        }
        b = pbkdf2.Key(b, salt, 2048, 64, sha512.New)
        println(string(b))
        time.Sleep(time.Second / 4)
    }
}
ghost commented 1 year ago

This is a Go program that generates and prints 10,000 PBKDF2 hashed keys. PBKDF2 is a key derivation function that is used to generate cryptographic keys from a password. In this program, the PBKDF2 function takes a byte slice and a salt as inputs, along with a few other parameters like the number of iterations and the length of the output key. The program generates a byte slice of increasing length on each iteration and hashes it using PBKDF2. The resulting hash is printed to the console, and the program waits for a quarter of a second before continuing to the next iteration.