keypair.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2018 The NATS Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package nkeys
  14. import (
  15. "bytes"
  16. "crypto/rand"
  17. "io"
  18. "golang.org/x/crypto/ed25519"
  19. )
  20. // kp is the internal struct for a kepypair using seed.
  21. type kp struct {
  22. seed []byte
  23. }
  24. // CreatePair will create a KeyPair based on the rand entropy and a type/prefix byte. rand can be nil.
  25. func CreatePair(prefix PrefixByte) (KeyPair, error) {
  26. var rawSeed [32]byte
  27. _, err := io.ReadFull(rand.Reader, rawSeed[:])
  28. if err != nil {
  29. return nil, err
  30. }
  31. seed, err := EncodeSeed(prefix, rawSeed[:])
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &kp{seed}, nil
  36. }
  37. // rawSeed will return the raw, decoded 64 byte seed.
  38. func (pair *kp) rawSeed() ([]byte, error) {
  39. _, raw, err := DecodeSeed(pair.seed)
  40. return raw, err
  41. }
  42. // keys will return a 32 byte public key and a 64 byte private key utilizing the seed.
  43. func (pair *kp) keys() (ed25519.PublicKey, ed25519.PrivateKey, error) {
  44. raw, err := pair.rawSeed()
  45. if err != nil {
  46. return nil, nil, err
  47. }
  48. return ed25519.GenerateKey(bytes.NewReader(raw))
  49. }
  50. // Wipe will randomize the contents of the seed key
  51. func (pair *kp) Wipe() {
  52. io.ReadFull(rand.Reader, pair.seed)
  53. pair.seed = nil
  54. }
  55. // Seed will return the encoded seed.
  56. func (pair *kp) Seed() ([]byte, error) {
  57. return pair.seed, nil
  58. }
  59. // PublicKey will return the encoded public key associated with the KeyPair.
  60. // All KeyPairs have a public key.
  61. func (pair *kp) PublicKey() (string, error) {
  62. public, raw, err := DecodeSeed(pair.seed)
  63. if err != nil {
  64. return "", err
  65. }
  66. pub, _, err := ed25519.GenerateKey(bytes.NewReader(raw))
  67. if err != nil {
  68. return "", err
  69. }
  70. pk, err := Encode(public, pub)
  71. if err != nil {
  72. return "", err
  73. }
  74. return string(pk), nil
  75. }
  76. // PrivateKey will return the encoded private key for KeyPair.
  77. func (pair *kp) PrivateKey() ([]byte, error) {
  78. _, priv, err := pair.keys()
  79. if err != nil {
  80. return nil, err
  81. }
  82. return Encode(PrefixBytePrivate, priv)
  83. }
  84. // Sign will sign the input with KeyPair's private key.
  85. func (pair *kp) Sign(input []byte) ([]byte, error) {
  86. _, priv, err := pair.keys()
  87. if err != nil {
  88. return nil, err
  89. }
  90. return ed25519.Sign(priv, input), nil
  91. }
  92. // Verify will verify the input against a signature utilizing the public key.
  93. func (pair *kp) Verify(input []byte, sig []byte) error {
  94. pub, _, err := pair.keys()
  95. if err != nil {
  96. return err
  97. }
  98. if !ed25519.Verify(pub, input, sig) {
  99. return ErrInvalidSignature
  100. }
  101. return nil
  102. }