Skip to content

Commit

Permalink
Fix Wiimote crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
xerpi committed Oct 26, 2021
1 parent 46a4e40 commit 4d0921a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 8 additions & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ static inline int memmismatch(const void *restrict a, const void *restrict b, in
return i;
}

static inline void reverse_memcpy(void *restrict dst, const void *restrict src, int size)
{
u8 *d = dst;
const u8 *s = src;
for (int i = 0; i < size; i++)
d[i] = s[size - 1 - i];
}

/* Message injection helpers */
int inject_msg_to_usb_intr_ready_queue(void *msg);
int inject_msg_to_usb_bulk_in_ready_queue(void *msg);
Expand Down
6 changes: 4 additions & 2 deletions source/wiimote_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Copyright (C) Hector Martin "marcan" ([email protected])

#include <string.h>
#include "utils.h"
#include "wiimote_crypto.h"

/* Code ported from:
Expand Down Expand Up @@ -196,6 +197,7 @@ static const sbox_t sboxes_1st_party[] = {

static inline u8 ror8(u8 word, unsigned int shift)
{
shift %= 8;
return (word >> shift) | (word << (8 - shift));
}

Expand Down Expand Up @@ -249,8 +251,8 @@ void wiimote_crypto_generate_key_from_extension_key_data(struct wiimote_encrypti
u8 key[6], check_key[6];
u32 idx;

memcpy(rand, key_data, sizeof(rand));
memcpy(key, key_data + sizeof(rand), sizeof(key));
reverse_memcpy(rand, key_data, sizeof(rand));
reverse_memcpy(key, key_data + sizeof(rand), sizeof(key));

/* Bruteforcing */
for (idx = 0; idx != 7; ++idx) {
Expand Down

0 comments on commit 4d0921a

Please sign in to comment.