verify.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !ts_omit_tailnetlock
  4. package tka
  5. import (
  6. "crypto/ed25519"
  7. "errors"
  8. "fmt"
  9. "github.com/hdevalence/ed25519consensus"
  10. "tailscale.com/types/tkatype"
  11. )
  12. // signatureVerify returns a nil error if the signature is valid over the
  13. // provided AUM BLAKE2s digest, using the given key.
  14. func signatureVerify(s *tkatype.Signature, aumDigest tkatype.AUMSigHash, key Key) error {
  15. // NOTE(tom): Even if we can compute the public from the KeyID,
  16. // it's possible for the KeyID to be attacker-controlled
  17. // so we should use the public contained in the state machine.
  18. switch key.Kind {
  19. case Key25519:
  20. if len(key.Public) != ed25519.PublicKeySize {
  21. return fmt.Errorf("ed25519 key has wrong length: %d", len(key.Public))
  22. }
  23. if ed25519consensus.Verify(ed25519.PublicKey(key.Public), aumDigest[:], s.Signature) {
  24. return nil
  25. }
  26. return errors.New("invalid signature")
  27. default:
  28. return fmt.Errorf("unhandled key type: %v", key.Kind)
  29. }
  30. }