mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-29 09:26:17 -05:00
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package util
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"io"
|
|
"math/big"
|
|
"net"
|
|
|
|
"golang.org/x/crypto/hkdf"
|
|
)
|
|
|
|
// DeriveKey derives a key from a secret.
|
|
//
|
|
// TODO: decide on a secure key derivation function.
|
|
func DeriveKey(secret, salt, info []byte, length uint) ([]byte, error) {
|
|
hkdf := hkdf.New(sha256.New, secret, salt, info)
|
|
key := make([]byte, length)
|
|
if _, err := io.ReadFull(hkdf, key); err != nil {
|
|
return nil, err
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
// GenerateCertificateSerialNumber generates a random serial number for an X.509 certificate.
|
|
func GenerateCertificateSerialNumber() (*big.Int, error) {
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
return rand.Int(rand.Reader, serialNumberLimit)
|
|
}
|
|
|
|
// GenerateRandomBytes reads length bytes from getrandom(2) if available, /dev/urandom otherwise.
|
|
func GenerateRandomBytes(length int) ([]byte, error) {
|
|
nonce := make([]byte, length)
|
|
if _, err := rand.Read(nonce); err != nil {
|
|
return nil, err
|
|
}
|
|
return nonce, nil
|
|
}
|
|
|
|
func GetIPAddr() (string, error) {
|
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer conn.Close()
|
|
|
|
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
return localAddr.IP.String(), nil
|
|
}
|