noise.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2020 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 DEVICE_FIDO_CABLE_NOISE_H_
  5. #define DEVICE_FIDO_CABLE_NOISE_H_
  6. #include <stdint.h>
  7. #include <array>
  8. #include <tuple>
  9. #include "base/component_export.h"
  10. #include "base/containers/span.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. #include "third_party/boringssl/src/include/openssl/base.h"
  13. namespace device {
  14. // Noise implements a small subset of the Noise Protocol Framework [1].
  15. //
  16. // http://www.noiseprotocol.org/noise.html#the-handshakestate-object
  17. class COMPONENT_EXPORT(DEVICE_FIDO) Noise {
  18. public:
  19. // HandshakeType enumerates the supported handshake patterns.
  20. enum class HandshakeType {
  21. kKNpsk0, // https://noiseexplorer.com/patterns/KNpsk0/
  22. kNKpsk0, // https://noiseexplorer.com/patterns/NKpsk0/
  23. };
  24. Noise();
  25. ~Noise();
  26. // Init must be called immediately after construction to initialise values.
  27. void Init(HandshakeType type);
  28. // The following functions reflect the functions of the same name from
  29. // http://www.noiseprotocol.org/noise.html#the-symmetricstate-object
  30. void MixHash(base::span<const uint8_t> in);
  31. void MixKey(base::span<const uint8_t> ikm);
  32. void MixKeyAndHash(base::span<const uint8_t> ikm);
  33. std::vector<uint8_t> EncryptAndHash(base::span<const uint8_t> plaintext);
  34. absl::optional<std::vector<uint8_t>> DecryptAndHash(
  35. base::span<const uint8_t> ciphertext);
  36. std::array<uint8_t, 32> handshake_hash() const;
  37. // MaxHashPoint calls |MixHash| with the uncompressed, X9.62 serialization of
  38. // |point|.
  39. void MixHashPoint(const EC_POINT* point);
  40. // traffic_keys() calls Split from the protocol spec but, rather than
  41. // returning CipherState objects, returns the raw keys.
  42. std::tuple<std::array<uint8_t, 32>, std::array<uint8_t, 32>> traffic_keys()
  43. const;
  44. private:
  45. void InitializeKey(base::span<const uint8_t, 32> key);
  46. std::array<uint8_t, 32> chaining_key_;
  47. std::array<uint8_t, 32> h_;
  48. std::array<uint8_t, 32> symmetric_key_;
  49. uint32_t symmetric_nonce_;
  50. };
  51. } // namespace device
  52. #endif // DEVICE_FIDO_CABLE_NOISE_H_