ed25519_public_key.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_WEB_PACKAGE_SIGNED_WEB_BUNDLES_ED25519_PUBLIC_KEY_H_
  5. #define COMPONENTS_WEB_PACKAGE_SIGNED_WEB_BUNDLES_ED25519_PUBLIC_KEY_H_
  6. #include <array>
  7. #include <cstdint>
  8. #include <string>
  9. #include <vector>
  10. #include "base/containers/span.h"
  11. #include "base/types/expected.h"
  12. namespace web_package {
  13. // This class wraps an Ed25519 public key. New instances must be created via the
  14. // static `Create` function, which will validate the length of the key before
  15. // creating a new instance. This guarantees that an instance of this class
  16. // always contains a public key of the correct length. This makes the key safe
  17. // to use with functions like BoringSSL's `ED25519_sign`. Note that the public
  18. // key might still be invalid, even though it has the correct length. This will
  19. // be checked and caught by BoringSSL when trying to use the key.
  20. class Ed25519PublicKey {
  21. public:
  22. static constexpr size_t kLength = 32;
  23. // Attempts to parse the bytes as an Ed25519 public key. Returns an instance
  24. // of this class on success, and an error message on failure.
  25. static base::expected<Ed25519PublicKey, std::string> Create(
  26. base::span<const uint8_t> key);
  27. // Constructs an instance of this class from the provided bytes.
  28. static Ed25519PublicKey Create(base::span<const uint8_t, kLength> key);
  29. Ed25519PublicKey(const Ed25519PublicKey&);
  30. ~Ed25519PublicKey();
  31. const std::array<uint8_t, kLength>& bytes() const { return bytes_; }
  32. private:
  33. explicit Ed25519PublicKey(std::array<uint8_t, kLength> bytes);
  34. const std::array<uint8_t, kLength> bytes_;
  35. };
  36. } // namespace web_package
  37. #endif // COMPONENTS_WEB_PACKAGE_SIGNED_WEB_BUNDLES_ED25519_PUBLIC_KEY_H_