ed25519_go113.go 2.8 KB

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