dns-api/ed25519/utils.go

40 lines
991 B
Go

package ed25519
import (
goEd25519 "crypto/ed25519"
"crypto/sha512"
"filippo.io/edwards25519"
"strconv"
)
func NewKeyFromStandard(key goEd25519.PrivateKey) PrivateKey {
if l := len(key); l != goEd25519.PrivateKeySize {
panic("ed25519: bad private key length: " + strconv.Itoa(l))
}
seed, publicKey := key[:goEd25519.SeedSize], key[goEd25519.SeedSize:]
h := sha512.Sum512(seed)
s, _ := edwards25519.NewScalar().SetBytesWithClamping(h[:32])
// (a || RH)
priv := s.Bytes()
priv = append(priv, publicKey...)
return append(priv, h[32:]...)
}
func NewKeyFromRaw(h []byte) PrivateKey {
if l := len(h); l != PrivateKeyFormSize {
panic("ed25519: bad private key form length: " + strconv.Itoa(l))
}
s, _ := edwards25519.NewScalar().SetBytesWithClamping(h[:32])
A := (&edwards25519.Point{}).ScalarBaseMult(s)
publicKey := A.Bytes()
privateKey := make(PrivateKey, PrivateKeySize)
copy(privateKey, h)
copy(privateKey[PrivateKeyFormSize:], publicKey)
return privateKey
}