OSDN Git Service

new repo
[bytom/vapor.git] / crypto / ed25519 / ed25519.go
1 // Package ed25519 implements the Ed25519 signature algorithm. See
2 // http://ed25519.cr.yp.to/.
3 //
4 // These functions are also compatible with the “Ed25519” function defined in
5 // https://tools.ietf.org/html/draft-irtf-cfrg-eddsa-05.
6 package ed25519
7
8 // This code is a port of the public domain, “ref10” implementation of ed25519
9 // from SUPERCOP.
10
11 import (
12         "crypto"
13         cryptorand "crypto/rand"
14         "crypto/sha512"
15         "crypto/subtle"
16         "errors"
17         "io"
18         "strconv"
19
20         "github.com/vapor/crypto/ed25519/internal/edwards25519"
21 )
22
23 const (
24         // PublicKeySize is the size, in bytes, of public keys as used in this package.
25         PublicKeySize = 32
26         // PrivateKeySize is the size, in bytes, of private keys as used in this package.
27         PrivateKeySize = 64
28         // SignatureSize is the size, in bytes, of signatures generated and verified by this package.
29         SignatureSize = 64
30 )
31
32 // PublicKey is the type of Ed25519 public keys.
33 type PublicKey []byte
34
35 // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
36 type PrivateKey []byte
37
38 // Public returns the PublicKey corresponding to priv.
39 func (priv PrivateKey) Public() crypto.PublicKey {
40         publicKey := make([]byte, PublicKeySize)
41         copy(publicKey, priv[32:])
42         return PublicKey(publicKey)
43 }
44
45 // Sign signs the given message with priv.
46 // Ed25519 performs two passes over messages to be signed and therefore cannot
47 // handle pre-hashed messages. Thus opts.HashFunc() must return zero to
48 // indicate the message hasn't been hashed. This can be achieved by passing
49 // crypto.Hash(0) as the value for opts.
50 func (priv PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) {
51         if opts.HashFunc() != crypto.Hash(0) {
52                 return nil, errors.New("ed25519: cannot sign hashed message")
53         }
54
55         return Sign(priv, message), nil
56 }
57
58 // GenerateKey generates a public/private key pair using entropy from rand.
59 // If rand is nil, crypto/rand.Reader will be used.
60 func GenerateKey(rand io.Reader) (publicKey PublicKey, privateKey PrivateKey, err error) {
61         if rand == nil {
62                 rand = cryptorand.Reader
63         }
64
65         privateKey = make([]byte, PrivateKeySize)
66         publicKey = make([]byte, PublicKeySize)
67         _, err = io.ReadFull(rand, privateKey[:32])
68         if err != nil {
69                 return nil, nil, err
70         }
71
72         digest := sha512.Sum512(privateKey[:32])
73         digest[0] &= 248
74         digest[31] &= 127
75         digest[31] |= 64
76
77         var A edwards25519.ExtendedGroupElement
78         var hBytes [32]byte
79         copy(hBytes[:], digest[:])
80         edwards25519.GeScalarMultBase(&A, &hBytes)
81         var publicKeyBytes [32]byte
82         A.ToBytes(&publicKeyBytes)
83
84         copy(privateKey[32:], publicKeyBytes[:])
85         copy(publicKey, publicKeyBytes[:])
86
87         return publicKey, privateKey, nil
88 }
89
90 // Sign signs the message with privateKey and returns a signature. It will
91 // panic if len(privateKey) is not PrivateKeySize.
92 func Sign(privateKey PrivateKey, message []byte) []byte {
93         if l := len(privateKey); l != PrivateKeySize {
94                 panic("ed25519: bad private key length: " + strconv.Itoa(l))
95         }
96
97         h := sha512.New()
98         h.Write(privateKey[:32])
99
100         var digest1, messageDigest, hramDigest [64]byte
101         var expandedSecretKey [32]byte
102         h.Sum(digest1[:0])
103         copy(expandedSecretKey[:], digest1[:])
104         expandedSecretKey[0] &= 248
105         expandedSecretKey[31] &= 63
106         expandedSecretKey[31] |= 64
107
108         h.Reset()
109         h.Write(digest1[32:])
110         h.Write(message)
111         h.Sum(messageDigest[:0])
112
113         var messageDigestReduced [32]byte
114         edwards25519.ScReduce(&messageDigestReduced, &messageDigest)
115         var R edwards25519.ExtendedGroupElement
116         edwards25519.GeScalarMultBase(&R, &messageDigestReduced)
117
118         var encodedR [32]byte
119         R.ToBytes(&encodedR)
120
121         h.Reset()
122         h.Write(encodedR[:])
123         h.Write(privateKey[32:])
124         h.Write(message)
125         h.Sum(hramDigest[:0])
126         var hramDigestReduced [32]byte
127         edwards25519.ScReduce(&hramDigestReduced, &hramDigest)
128
129         var s [32]byte
130         edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced)
131
132         signature := make([]byte, SignatureSize)
133         copy(signature[:], encodedR[:])
134         copy(signature[32:], s[:])
135
136         return signature
137 }
138
139 // Verify reports whether sig is a valid signature of message by publicKey. It
140 // will panic if len(publicKey) is not PublicKeySize.
141 func Verify(publicKey PublicKey, message, sig []byte) bool {
142         if l := len(publicKey); l != PublicKeySize {
143                 panic("ed25519: bad public key length: " + strconv.Itoa(l))
144         }
145
146         if len(sig) != SignatureSize || sig[63]&224 != 0 {
147                 return false
148         }
149
150         var A edwards25519.ExtendedGroupElement
151         var publicKeyBytes [32]byte
152         copy(publicKeyBytes[:], publicKey)
153         if !A.FromBytes(&publicKeyBytes) {
154                 return false
155         }
156         edwards25519.FeNeg(&A.X, &A.X)
157         edwards25519.FeNeg(&A.T, &A.T)
158
159         h := sha512.New()
160         h.Write(sig[:32])
161         h.Write(publicKey[:])
162         h.Write(message)
163         var digest [64]byte
164         h.Sum(digest[:0])
165
166         var hReduced [32]byte
167         edwards25519.ScReduce(&hReduced, &digest)
168
169         var R edwards25519.ProjectiveGroupElement
170         var b [32]byte
171         copy(b[:], sig[32:])
172         edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &b)
173
174         var checkR [32]byte
175         R.ToBytes(&checkR)
176         return subtle.ConstantTimeCompare(sig[:32], checkR[:]) == 1
177 }