| 1 |
// Copyright 2019 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. |
| 4 |
|
| 5 |
// +build go1.13 |
| 6 |
|
| 7 |
// Package ed25519 implements the Ed25519 signature algorithm. See |
| 8 |
// https://ed25519.cr.yp.to/. |
| 9 |
// |
| 10 |
// These functions are also compatible with the “Ed25519” function defined in |
| 11 |
// RFC 8032. However, unlike RFC 8032's formulation, this package's private key |
| 12 |
// representation includes a public key suffix to make multiple signing |
| 13 |
// operations with the same key more efficient. This package refers to the RFC |
| 14 |
// 8032 private key as the “seed”. |
| 15 |
// |
| 16 |
// Beginning with Go 1.13, the functionality of this package was moved to the |
| 17 |
// standard library as crypto/ed25519. This package only acts as a compatibility |
| 18 |
// wrapper. |
| 19 |
package ed25519 |
| 20 |
|
| 21 |
import ( |
| 22 |
"crypto/ed25519" |
| 23 |
"io" |
| 24 |
) |
| 25 |
|
| 26 |
const ( |
| 27 |
// PublicKeySize is the size, in bytes, of public keys as used in this package. |
| 28 |
PublicKeySize = 32 |
| 29 |
// PrivateKeySize is the size, in bytes, of private keys as used in this package. |
| 30 |
PrivateKeySize = 64 |
| 31 |
// SignatureSize is the size, in bytes, of signatures generated and verified by this package. |
| 32 |
SignatureSize = 64 |
| 33 |
// SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032. |
| 34 |
SeedSize = 32 |
| 35 |
) |
| 36 |
|
| 37 |
// PublicKey is the type of Ed25519 public keys. |
| 38 |
// |
| 39 |
// This type is an alias for crypto/ed25519's PublicKey type. |
| 40 |
// See the crypto/ed25519 package for the methods on this type. |
| 41 |
type PublicKey = ed25519.PublicKey |
| 42 |
|
| 43 |
// PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer. |
| 44 |
// |
| 45 |
// This type is an alias for crypto/ed25519's PrivateKey type. |
| 46 |
// See the crypto/ed25519 package for the methods on this type. |
| 47 |
type PrivateKey = ed25519.PrivateKey |
| 48 |
|
| 49 |
// GenerateKey generates a public/private key pair using entropy from rand. |
| 50 |
// If rand is nil, crypto/rand.Reader will be used. |
| 51 |
func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) { |
| 52 |
return ed25519.GenerateKey(rand) |
| 53 |
} |
| 54 |
|
| 55 |
// NewKeyFromSeed calculates a private key from a seed. It will panic if |
| 56 |
// len(seed) is not SeedSize. This function is provided for interoperability |
| 57 |
// with RFC 8032. RFC 8032's private keys correspond to seeds in this |
| 58 |
// package. |
| 59 |
func NewKeyFromSeed(seed []byte) PrivateKey { |
| 60 |
return ed25519.NewKeyFromSeed(seed) |
| 61 |
} |
| 62 |
|
| 63 |
// Sign signs the message with privateKey and returns a signature. It will |
| 64 |
// panic if len(privateKey) is not PrivateKeySize. |
| 65 |
func Sign(privateKey PrivateKey, message []byte) []byte { |
| 66 |
return ed25519.Sign(privateKey, message) |
| 67 |
} |
| 68 |
|
| 69 |
// Verify reports whether sig is a valid signature of message by publicKey. It |
| 70 |
// will panic if len(publicKey) is not PublicKeySize. |
| 71 |
func Verify(publicKey PublicKey, message, sig []byte) bool { |
| 72 |
return ed25519.Verify(publicKey, message, sig) |
| 73 |
} |