1
// Copyright 2016 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// Package ed25519 implements the Ed25519 signature algorithm. See
6
// http://ed25519.cr.yp.to/.
8
// These functions are also compatible with the “Ed25519” function defined in
9
// https://tools.ietf.org/html/draft-irtf-cfrg-eddsa-05.
12
// This code is a port of the public domain, “ref10” implementation of ed25519
17
cryptorand "crypto/rand"
24
"golang.org/x/crypto/ed25519/internal/edwards25519"
28
// PublicKeySize is the size, in bytes, of public keys as used in this package.
30
// PrivateKeySize is the size, in bytes, of private keys as used in this package.
32
// SignatureSize is the size, in bytes, of signatures generated and verified by this package.
36
// PublicKey is the type of Ed25519 public keys.
39
// PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
40
type PrivateKey []byte
42
// Public returns the PublicKey corresponding to priv.
43
func (priv PrivateKey) Public() crypto.PublicKey {
44
publicKey := make([]byte, PublicKeySize)
45
copy(publicKey, priv[32:])
46
return PublicKey(publicKey)
49
// Sign signs the given message with priv.
50
// Ed25519 performs two passes over messages to be signed and therefore cannot
51
// handle pre-hashed messages. Thus opts.HashFunc() must return zero to
52
// indicate the message hasn't been hashed. This can be achieved by passing
53
// crypto.Hash(0) as the value for opts.
54
func (priv PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) {
55
if opts.HashFunc() != crypto.Hash(0) {
56
return nil, errors.New("ed25519: cannot sign hashed message")
59
return Sign(priv, message), nil
62
// GenerateKey generates a public/private key pair using entropy from rand.
63
// If rand is nil, crypto/rand.Reader will be used.
64
func GenerateKey(rand io.Reader) (publicKey PublicKey, privateKey PrivateKey, err error) {
66
rand = cryptorand.Reader
69
privateKey = make([]byte, PrivateKeySize)
70
publicKey = make([]byte, PublicKeySize)
71
_, err = io.ReadFull(rand, privateKey[:32])
76
digest := sha512.Sum512(privateKey[:32])
81
var A edwards25519.ExtendedGroupElement
83
copy(hBytes[:], digest[:])
84
edwards25519.GeScalarMultBase(&A, &hBytes)
85
var publicKeyBytes [32]byte
86
A.ToBytes(&publicKeyBytes)
88
copy(privateKey[32:], publicKeyBytes[:])
89
copy(publicKey, publicKeyBytes[:])
91
return publicKey, privateKey, nil
94
// Sign signs the message with privateKey and returns a signature. It will
95
// panic if len(privateKey) is not PrivateKeySize.
96
func Sign(privateKey PrivateKey, message []byte) []byte {
97
if l := len(privateKey); l != PrivateKeySize {
98
panic("ed25519: bad private key length: " + strconv.Itoa(l))
102
h.Write(privateKey[:32])
104
var digest1, messageDigest, hramDigest [64]byte
105
var expandedSecretKey [32]byte
107
copy(expandedSecretKey[:], digest1[:])
108
expandedSecretKey[0] &= 248
109
expandedSecretKey[31] &= 63
110
expandedSecretKey[31] |= 64
113
h.Write(digest1[32:])
115
h.Sum(messageDigest[:0])
117
var messageDigestReduced [32]byte
118
edwards25519.ScReduce(&messageDigestReduced, &messageDigest)
119
var R edwards25519.ExtendedGroupElement
120
edwards25519.GeScalarMultBase(&R, &messageDigestReduced)
122
var encodedR [32]byte
127
h.Write(privateKey[32:])
129
h.Sum(hramDigest[:0])
130
var hramDigestReduced [32]byte
131
edwards25519.ScReduce(&hramDigestReduced, &hramDigest)
134
edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced)
136
signature := make([]byte, SignatureSize)
137
copy(signature[:], encodedR[:])
138
copy(signature[32:], s[:])
143
// Verify reports whether sig is a valid signature of message by publicKey. It
144
// will panic if len(publicKey) is not PublicKeySize.
145
func Verify(publicKey PublicKey, message, sig []byte) bool {
146
if l := len(publicKey); l != PublicKeySize {
147
panic("ed25519: bad public key length: " + strconv.Itoa(l))
150
if len(sig) != SignatureSize || sig[63]&224 != 0 {
154
var A edwards25519.ExtendedGroupElement
155
var publicKeyBytes [32]byte
156
copy(publicKeyBytes[:], publicKey)
157
if !A.FromBytes(&publicKeyBytes) {
160
edwards25519.FeNeg(&A.X, &A.X)
161
edwards25519.FeNeg(&A.T, &A.T)
165
h.Write(publicKey[:])
170
var hReduced [32]byte
171
edwards25519.ScReduce(&hReduced, &digest)
173
var R edwards25519.ProjectiveGroupElement
176
edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &b)
180
return subtle.ConstantTimeCompare(sig[:32], checkR[:]) == 1