ed25519.go 2.7 KB

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