ed25519.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // This package is a wrapper around the standard library crypto/ed25519 package.
  14. package ed25519
  15. import (
  16. "crypto/ed25519"
  17. "io"
  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. // SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
  27. SeedSize = 32
  28. )
  29. // PublicKey is the type of Ed25519 public keys.
  30. //
  31. // This type is an alias for crypto/ed25519's PublicKey type.
  32. // See the crypto/ed25519 package for the methods on this type.
  33. type PublicKey = ed25519.PublicKey
  34. // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
  35. //
  36. // This type is an alias for crypto/ed25519's PrivateKey type.
  37. // See the crypto/ed25519 package for the methods on this type.
  38. type PrivateKey = ed25519.PrivateKey
  39. // GenerateKey generates a public/private key pair using entropy from rand.
  40. // If rand is nil, crypto/rand.Reader will be used.
  41. func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) {
  42. return ed25519.GenerateKey(rand)
  43. }
  44. // NewKeyFromSeed calculates a private key from a seed. It will panic if
  45. // len(seed) is not SeedSize. This function is provided for interoperability
  46. // with RFC 8032. RFC 8032's private keys correspond to seeds in this
  47. // package.
  48. func NewKeyFromSeed(seed []byte) PrivateKey {
  49. return ed25519.NewKeyFromSeed(seed)
  50. }
  51. // Sign signs the message with privateKey and returns a signature. It will
  52. // panic if len(privateKey) is not PrivateKeySize.
  53. func Sign(privateKey PrivateKey, message []byte) []byte {
  54. return ed25519.Sign(privateKey, message)
  55. }
  56. // Verify reports whether sig is a valid signature of message by publicKey. It
  57. // will panic if len(publicKey) is not PublicKeySize.
  58. func Verify(publicKey PublicKey, message, sig []byte) bool {
  59. return ed25519.Verify(publicKey, message, sig)
  60. }