Files
xone_macos/include/auth/auth.hpp
T
portersky 2d4366f454 feat: port GIP protocol and auth stack
Port the GIP protocol and authentication layers from medusalix/xone
into the C++ stack:

- crypto: SHA-256/HMAC (CommonCrypto), RSA PKCS#1 (Security.framework),
  and a self-contained P-256 ECDH validated against OpenSSL vectors
- auth: v1 (RSA) and v2 (ECDH) handshake state machine
- gip: header/varint/chunk handling and packet dispatch, with the kernel
  device model replaced by transport + client_listener interfaces

Adds test_crypto, test_auth, and test_gip suites.

Co-Authored-By: deepseek (deepseek/deepseek-v4-pro-0813): ported crypto, auth, and GIP
2026-08-17 15:04:44 +02:00

93 lines
3.3 KiB
C++

#pragma once
// ==============================================================================
// GIP authentication handshake
// ==============================================================================
// Port target: medusalix/xone auth/auth.c + auth/auth.h.
//
// The kernel driver runs the RSA/ECDH exchanges on workqueues and speaks to
// the controller through gip_send_authenticate(). Here the exchanges run
// synchronously and the GIP layer is reached through the auth_sink callback
// interface, which the gip::client implements.
// ==============================================================================
#include <array>
#include <cstdint>
#include <span>
#include "auth/crypto.hpp"
namespace xone::auth {
using u8 = std::uint8_t;
using u16 = std::uint16_t;
// Wire sizes from the upstream auth.h.
inline constexpr auto k_trailer_len = 8;
inline constexpr auto k_random_len = 32;
inline constexpr auto k_certificate_max_len = 1024;
inline constexpr auto k_pubkey_len = 270; // v1 RSA public key
inline constexpr auto k_secret_len = 48;
inline constexpr auto k_encrypted_pms_len = 256;
inline constexpr auto k_transcript_len = 32;
inline constexpr auto k_session_key_len = 16;
inline constexpr auto k_pubkey2_len = 64; // v2 EC public key (X || Y)
inline constexpr auto k_secret2_len = 32;
// Callback interface implemented by the GIP layer.
class auth_sink {
public:
virtual ~auth_sink() = default;
// Send an AUTHENTICATE command packet to the controller.
virtual auto send(std::span<u8 const> pkt, bool acknowledge) -> int = 0;
// Install the derived AES-CCMP session key (16 bytes).
virtual auto set_encryption_key(std::span<u8 const> key) -> int = 0;
};
// Runs the handshake for one controller. Port of struct gip_auth.
class auth {
public:
explicit auth(auth_sink& sink);
// Begin the handshake by sending the (v1) host hello.
auto start() -> int;
// Process one incoming AUTHENTICATE packet from the controller.
auto process_pkt(std::span<u8 const> data) -> int;
private:
auto send_pkt(u8 cmd, void const* pkt, std::size_t len) -> int;
auto request_pkt(u8 cmd, std::uint16_t len) -> int;
auto send_hello() -> int;
auto send_hello2() -> int;
auto send_finish(u8 cmd) -> int;
auto send_complete() -> int;
auto exchange_rsa() -> void;
auto exchange_ecdh() -> void;
auto complete_handshake() -> void;
auto handle_pkt_acknowledge() -> int;
auto handle_pkt_data(std::span<u8 const> data) -> int;
auto dispatch_pkt(u8 cmd, std::span<u8 const> data) -> int;
auto handle_hello(std::span<u8 const> data) -> int;
auto handle_certificate(std::span<u8 const> data) -> int;
auto handle_finish(std::span<u8 const> data) -> int;
auto handle_hello2(std::span<u8 const> data) -> int;
auto handle_certificate2(std::span<u8 const> data) -> int;
auto handle_pubkey(std::span<u8 const> data) -> int;
auto get_transcript() -> std::array<u8, k_transcript_len>;
auth_sink& sink_;
sha256 transcript_;
u8 last_sent_command_ = 0;
std::array<u8, k_random_len> random_host_{};
std::array<u8, k_random_len> random_client_{};
std::array<u8, k_pubkey_len> pubkey_client_{};
std::array<u8, k_pubkey2_len> pubkey_client2_{};
std::array<u8, k_secret_len> master_secret_{};
};
} // namespace xone::auth