Skip to content

Commit

Permalink
Core0/Core1 Sync USB Stall fix (#1146)
Browse files Browse the repository at this point in the history
* PS5 fix for auth

* What started as a simple fix has become a complete revamp of PS4/PS5 auth

- Simplified auth down to a single 1064 buffer for nonce + signed nonce passing
- All auth console<->dongle passing now use a single GPAuthState enum
- Lots of simplifications of design for PS4 and PS4-usb-listener

Still need to test against PS4 loaded keys but it should work

* Fix for USB stalling

* Revert sensor data back to what it was before

* Update PS4Auth.h

* Update PS4Driver.cpp
  • Loading branch information
arntsonl authored Sep 25, 2024
1 parent 6a7dc59 commit 9a72759
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 359 deletions.
27 changes: 15 additions & 12 deletions headers/drivers/ps4/PS4Auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
#define _PS4AUTH_H_

#include "drivers/shared/gpauthdriver.h"
#include "drivers/ps4/PS4Driver.h"
#include "usblistener.h"
#include "mbedtls/rsa.h"

// PS4 Auth Data in a single struct
typedef struct {
struct mbedtls_rsa_context rsa_context;
uint8_t ps4_auth_buffer[1064];
bool valid_rsa = false;
bool dongle_ready = false;
GPAuthState passthrough_state; // PS4 Encryption Passthrough State
uint8_t nonce_id; // for nonce passing
} PS4AuthData;

class PS4Auth : public GPAuthDriver {
public:
PS4Auth(InputModeAuthType inType) { authType = inType; }
virtual void initialize();
virtual bool available();
void process(PS4State, uint8_t, uint8_t*);
uint8_t * getAuthBuffer() { return ps4_auth_buffer; }
bool getAuthReady();
void process();
PS4AuthData * getAuthData() { return &ps4AuthData; }
void resetAuth();
private:
struct mbedtls_rsa_context rsa_context;
bool valid_rsa;

// buffer = 256 + 16 + 256 + 256 + 256 + 24
// == 1064 bytes (almost 1 kb)
uint8_t ps4_auth_buffer[1064];
bool ps4_keys_signature_ready;
void keyModeInitialize();
void keyModeProcess();
PS4AuthData ps4AuthData;
};

#endif
36 changes: 22 additions & 14 deletions headers/drivers/ps4/PS4AuthUSBListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@

#include "usblistener.h"
#include "PS4Driver.h"
#include "PS4Auth.h"

typedef enum {
no_nonce = 0,
receiving_nonce = 1,
nonce_ready = 2,
signed_nonce_ready = 3,
sending_nonce = 4
} PS4State;

class PS4AuthUSBListener : public USBListener {
public:
Expand All @@ -14,23 +23,22 @@ class PS4AuthUSBListener : public USBListener {
virtual void report_sent(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) {}
virtual void set_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len);
virtual void get_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len);
void process(PS4State pState, uint8_t pNonceId, uint8_t * pNonceBuffer); // add things to process
void setAuthBuffer(uint8_t * buffer) { ps4_auth_buffer = buffer; }
bool getHostAuthReady() { return ps4_auth_host_ready; }
void resetHostAuth();
void process(); // add things to process
void setAuthData(PS4AuthData * authData) { ps4AuthData = authData; }
void resetHostData();
private:
bool host_get_report(uint8_t report_id, void* report, uint16_t len);
bool host_set_report(uint8_t report_id, void* report, uint16_t len);
uint8_t ps_dev_addr;
uint8_t ps_instance;
int8_t nonce_page;
PS4State passthrough_state;
int8_t send_nonce_part;
uint8_t report_buffer[64];
bool awaiting_cb;
bool ps4_auth_host_ready;
uint8_t * ps4_auth_buffer;
bool ps_mounted;
uint8_t ps_dev_addr; // TinyUSB Address (USB)
uint8_t ps_instance; // TinyUSB Instance (USB)
PS4AuthData * ps4AuthData; // PS4 Authentication Data
uint8_t nonce_page; // PS4 Encryption Nonce Page (Max 5)
uint8_t nonce_chunk; // PS4 Encryption Nonce Chunk (Max 19)
uint8_t report_buffer[PS4_ENDPOINT_SIZE]; // Report buffer
bool awaiting_cb; // Global call-back wait
uint8_t noncelen; // process(): nonce-len
uint32_t crc32; // process(): crc32
PS4State dongle_state;
};

#endif // _PS4AUTHUSBLISTENER_H_
30 changes: 6 additions & 24 deletions headers/drivers/ps4/PS4Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@
#include "drivers/ps4/PS4Descriptors.h"

// Authentication
#include "drivers/shared/gpauthdriver.h"

typedef enum {
no_nonce = 0,
receiving_nonce = 1,
nonce_ready = 2,
signed_nonce_ready = 3,
sending_nonce = 4,
waiting_reset = 5
} PS4State;
#include "drivers/ps4/PS4Auth.h"

typedef enum
{
Expand All @@ -44,7 +35,6 @@ typedef enum
// 256 byte - RSA E
// 256 byte - ps4 signature
// 24 byte - zero padding

class PS4Driver : public GPDriver {
public:
PS4Driver(uint32_t type): controllerType(type) {}
Expand All @@ -62,35 +52,27 @@ class PS4Driver : public GPDriver {
virtual const uint8_t * get_descriptor_device_qualifier_cb();
virtual uint16_t GetJoystickMidValue();
virtual USBListener * get_usb_auth_listener();

bool getAuthSent() { return authsent;}
private:
// Lots of things here
void save_nonce(uint8_t nonce_id, uint8_t nonce_page, uint8_t * buffer, uint16_t buflen);
uint8_t last_report[CFG_TUD_ENDPOINT0_SIZE] = { };
uint8_t last_report_counter;
uint16_t last_axis_counter;
uint8_t cur_nonce_id;
PS4Report ps4Report;
TouchpadData touchpadData;
PSSensorData sensorData;
uint32_t last_report_timer;
uint8_t send_nonce_part;
uint32_t controllerType;
GPAuthDriver * authDriver;
PS4Auth * ps4AuthDriver;
PS4AuthData * ps4AuthData; // PS4 Authentication Data
uint8_t cur_nonce_chunk; // PS4 Encryption Nonce Chunk (Max 19)
uint8_t cur_nonce_id;
uint32_t controllerType; // PS4 DS4 / PS5 Third-Party
bool pointOneTouched = false;
bool pointTwoTouched = false;
uint8_t touchCounter;

PS4FeatureOutputReport ps4Features;
uint8_t lastFeatures[PS4_FEATURES_SIZE] = { };

uint8_t deviceDescriptor[sizeof(ps4_device_descriptor)];

PS4State ps4State;
bool authsent;
uint8_t nonce_buffer[256];
uint8_t nonce_id; // used in pass-through mode
};

#endif // _PS4_DRIVER_H_
9 changes: 8 additions & 1 deletion headers/drivers/shared/gpauthdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
#define _GPAUTHDRIVER_H_

#include "enums.pb.h"

#include "usblistener.h"

typedef enum {
auth_idle_state = 0,
send_auth_console_to_dongle = 1,
send_auth_dongle_to_console = 2,
wait_auth_console_to_dongle = 3,
wait_auth_dongle_to_console = 4,
} GPAuthState;

class GPAuthDriver {
public:
virtual void initialize() = 0;
Expand Down
10 changes: 1 addition & 9 deletions headers/drivers/xbone/XBOneAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
#include "drivers/shared/gpauthdriver.h"
#include "drivers/shared/xgip_protocol.h"

typedef enum {
auth_idle_state = 0,
send_auth_console_to_dongle = 1,
send_auth_dongle_to_console = 2,
wait_auth_console_to_dongle = 3,
wait_auth_dongle_to_console = 4,
} XboxOneState;

class XBOneAuthBuffer {
public:
XBOneAuthBuffer() {
Expand Down Expand Up @@ -51,7 +43,7 @@ class XBOneAuthBuffer {
};

typedef struct {
XboxOneState xboneState;
GPAuthState xboneState;

// Auth Buffer Queue
XBOneAuthBuffer consoleBuffer;
Expand Down
8 changes: 1 addition & 7 deletions headers/drivers/xinput/XInputAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@

#include "drivers/shared/gpauthdriver.h"

typedef enum {
auth_idle_state = 0,
send_auth_console_to_dongle = 1,
send_auth_dongle_to_console = 2
} XInputAuthState;

class XInputAuthBuffer {
public:
XInputAuthBuffer() {
Expand Down Expand Up @@ -49,7 +43,7 @@ class XInputAuthBuffer {
// Dongle Serial 29 bytes
// Console-Dongle Back and Forth 46 bytes & 22 bytes
typedef struct {
XInputAuthState xinputState;
GPAuthState xinputState;
uint8_t consoleInitialAuth[X360_AUTHLEN_CONSOLE_INIT]; // Console Init (Keep when Dongle Reboots)
uint8_t dongleSerial[X360_AUTHLEN_DONGLE_SERIAL]; // Dongle Serial
uint8_t passthruBuffer[X360_AUTHLEN_DONGLE_INIT]; // Back-and-Forth Buffer (46 or 22 bytes)
Expand Down
Loading

0 comments on commit 9a72759

Please sign in to comment.