| 1 | // Package ed25519 implements Tor/BitTorrent-like ed25519 keys. |
| 2 | // |
| 3 | // See the following stack overflow post for details on why |
| 4 | // golang.org/x/crypto/ed25519 can't be used: |
| 5 | // https://stackoverflow.com/questions/44810708/ed25519-public-result-is-different |
| 6 | package ed25519 |
| 7 | |
| 8 | import ( |
| 9 | "crypto" |
| 10 | "crypto/rand" |
| 11 | "crypto/sha512" |
| 12 | "errors" |
| 13 | "io" |
| 14 | |
| 15 | "github.com/cretz/bine/torutil/ed25519/internal/edwards25519" |
| 16 | "golang.org/x/crypto/ed25519" |
| 17 | ) |
| 18 | |
| 19 | const ( |
| 20 | // PublicKeySize is the size, in bytes, of public keys as used in this package. |
| 21 | PublicKeySize = 32 |
| 22 | // PrivateKeySize is the size, in bytes, of private keys as used in this package. |
| 23 | PrivateKeySize = 64 |
| 24 | // SignatureSize is the size, in bytes, of signatures generated and verified by this package. |
| 25 | SignatureSize = 64 |
| 26 | ) |
| 27 | |
| 28 | // PrivateKey is a 64-byte Ed25519 private key. Unlike |
| 29 | // golang.org/x/crypto/ed25519, this is just the digest and does not contain |
| 30 | // the public key within it. Instead call PublicKey() or better, call KeyPair() |
| 31 | // which stores the precomputed public key. |
| 32 | type PrivateKey []byte |
| 33 | |
| 34 | // PublicKey is a 32-byte Ed25519 public key. |
| 35 | type PublicKey []byte |
| 36 | |
| 37 | // FromCryptoPrivateKey converts a Go private key to the one in this package. |
| 38 | func FromCryptoPrivateKey(key ed25519.PrivateKey) KeyPair { |
| 39 | digest := sha512.Sum512(key[:32]) |
| 40 | digest[0] &= 248 |
| 41 | digest[31] &= 127 |
| 42 | digest[31] |= 64 |
| 43 | return &precomputedKeyPair{PrivateKeyBytes: digest[:], PublicKeyBytes: PublicKey(key[32:])} |
| 44 | } |
| 45 | |
| 46 | // FromCryptoPublicKey converts a Go public key to the one in this package. |
| 47 | func FromCryptoPublicKey(key ed25519.PublicKey) PublicKey { |
| 48 | return PublicKey(key) |
| 49 | } |
| 50 | |
| 51 | // KeyPair returns a new key pair with the public key precomputed. |
| 52 | func (p PrivateKey) KeyPair() KeyPair { |
| 53 | return &precomputedKeyPair{PrivateKeyBytes: p, PublicKeyBytes: p.PublicKey()} |
| 54 | } |
| 55 | |
| 56 | // PrivateKey simply returns itself. Implements KeyPair.PrivateKey. |
| 57 | func (p PrivateKey) PrivateKey() PrivateKey { return p } |
| 58 | |
| 59 | // Public simply delegates to PublicKey() to satisfy crypto.Signer. This method |
| 60 | // does a bit more work than the traditional Go ed25519's private key's Public() |
| 61 | // method so developers are encouraged to reuse the result or use KeyPair() |
| 62 | // which stores this value. |
| 63 | func (p PrivateKey) Public() crypto.PublicKey { return p.PublicKey() } |
| 64 | |
| 65 | // PublicKey generates a public key for this private key. This method does a bit |
| 66 | // more work than the traditional Go ed25519's private key's Public() method so |
| 67 | // developers are encouraged to reuse the result or use KeyPair() which stores |
| 68 | // this value. Implements KeyPair.PublicKey. |
| 69 | func (p PrivateKey) PublicKey() PublicKey { |
| 70 | var A edwards25519.ExtendedGroupElement |
| 71 | var hBytes [32]byte |
| 72 | copy(hBytes[:], p[:]) |
| 73 | edwards25519.GeScalarMultBase(&A, &hBytes) |
| 74 | var publicKeyBytes [32]byte |
| 75 | A.ToBytes(&publicKeyBytes) |
| 76 | return publicKeyBytes[:] |
| 77 | } |
| 78 | |
| 79 | // Sign signs the given message with priv. Ed25519 performs two passes over |
| 80 | // messages to be signed and therefore cannot handle pre-hashed messages. Thus |
| 81 | // opts.HashFunc() must return zero to indicate the message hasn't been hashed. |
| 82 | // This can be achieved by passing crypto.Hash(0) as the value for opts. |
| 83 | func (p PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 84 | if opts.HashFunc() != crypto.Hash(0) { |
| 85 | return nil, errors.New("ed25519: cannot sign hashed message") |
| 86 | } |
| 87 | return Sign(p, message), nil |
| 88 | } |
| 89 | |
| 90 | // Verify simply calls PublicKey().Verify(). Callers are encouraged to instead |
| 91 | // store a precomputed KeyPair (via KeyPair() or GenerateKey()) and call Verify |
| 92 | // on that. |
| 93 | func (p PrivateKey) Verify(message []byte, sig []byte) bool { |
| 94 | return p.PublicKey().Verify(message, sig) |
| 95 | } |
| 96 | |
| 97 | // Verify simply calls the package-level function Verify(). |
| 98 | func (p PublicKey) Verify(message []byte, sig []byte) bool { |
| 99 | return Verify(p, message, sig) |
| 100 | } |
| 101 | |
| 102 | // KeyPair is an interface for types with both keys. While PrivateKey does |
| 103 | // implement this, it generates the PublicKey on demand. For better performance, |
| 104 | // use the result of GenerateKey directly or call PrivateKey.KeyPair(). |
| 105 | type KeyPair interface { |
| 106 | crypto.Signer |
| 107 | PrivateKey() PrivateKey |
| 108 | PublicKey() PublicKey |
| 109 | Verify(message []byte, sig []byte) bool |
| 110 | } |
| 111 | |
| 112 | type precomputedKeyPair struct { |
| 113 | PrivateKeyBytes PrivateKey |
| 114 | PublicKeyBytes PublicKey |
| 115 | } |
| 116 | |
| 117 | func (p *precomputedKeyPair) PrivateKey() PrivateKey { return p.PrivateKeyBytes } |
| 118 | func (p *precomputedKeyPair) PublicKey() PublicKey { return p.PublicKeyBytes } |
| 119 | func (p *precomputedKeyPair) Public() crypto.PublicKey { return p.PublicKey() } |
| 120 | func (p *precomputedKeyPair) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 121 | if opts.HashFunc() != crypto.Hash(0) { |
| 122 | return nil, errors.New("ed25519: cannot sign hashed message") |
| 123 | } |
| 124 | return Sign(p, message), nil |
| 125 | } |
| 126 | func (p *precomputedKeyPair) Verify(message []byte, sig []byte) bool { |
| 127 | return p.PublicKeyBytes.Verify(message, sig) |
| 128 | } |
| 129 | |
| 130 | // GenerateKey generates a public/private key pair using entropy from rand. |
| 131 | // If rand is nil, crypto/rand.Reader will be used. |
| 132 | func GenerateKey(rnd io.Reader) (KeyPair, error) { |
| 133 | if rnd == nil { |
| 134 | rnd = rand.Reader |
| 135 | } |
| 136 | rndByts := make([]byte, 32) |
| 137 | if _, err := io.ReadFull(rnd, rndByts); err != nil { |
| 138 | return nil, err |
| 139 | } |
| 140 | digest := sha512.Sum512(rndByts) |
| 141 | digest[0] &= 248 |
| 142 | digest[31] &= 127 |
| 143 | digest[31] |= 64 |
| 144 | return PrivateKey(digest[:]).KeyPair(), nil |
| 145 | } |
| 146 | |
| 147 | // Sign signs the message with the given key pair. |
| 148 | func Sign(keyPair KeyPair, message []byte) []byte { |
| 149 | // Ref: https://stackoverflow.com/questions/44810708/ed25519-public-result-is-different |
| 150 | |
| 151 | var privateKeyA [32]byte |
| 152 | copy(privateKeyA[:], keyPair.PrivateKey()) // we need this in an array later |
| 153 | var messageDigest, hramDigest [64]byte |
| 154 | |
| 155 | h := sha512.New() |
| 156 | h.Write(keyPair.PrivateKey()[32:]) |
| 157 | h.Write(message) |
| 158 | h.Sum(messageDigest[:0]) |
| 159 | |
| 160 | var messageDigestReduced [32]byte |
| 161 | edwards25519.ScReduce(&messageDigestReduced, &messageDigest) |
| 162 | var R edwards25519.ExtendedGroupElement |
| 163 | edwards25519.GeScalarMultBase(&R, &messageDigestReduced) |
| 164 | |
| 165 | var encodedR [32]byte |
| 166 | R.ToBytes(&encodedR) |
| 167 | |
| 168 | h.Reset() |
| 169 | h.Write(encodedR[:]) |
| 170 | h.Write(keyPair.PublicKey()) |
| 171 | h.Write(message) |
| 172 | h.Sum(hramDigest[:0]) |
| 173 | var hramDigestReduced [32]byte |
| 174 | edwards25519.ScReduce(&hramDigestReduced, &hramDigest) |
| 175 | |
| 176 | var s [32]byte |
| 177 | edwards25519.ScMulAdd(&s, &hramDigestReduced, &privateKeyA, &messageDigestReduced) |
| 178 | |
| 179 | signature := make([]byte, 64) |
| 180 | copy(signature[:], encodedR[:]) |
| 181 | copy(signature[32:], s[:]) |
| 182 | |
| 183 | return signature |
| 184 | } |
| 185 | |
| 186 | // Verify verifies a signed message. |
| 187 | func Verify(p PublicKey, message []byte, sig []byte) bool { |
| 188 | return ed25519.Verify(ed25519.PublicKey(p), message, sig) |
| 189 | } |