/* usbdsec.c - part of libxsm3 Copyright (C) 2013 oct0xor Copyright (C) 2022 InvoxiPlayGames This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include "excrypt.h" static uint8_t UsbdSecSboxData[256] __attribute__ ((aligned(4))) = { 0xB0, 0x3D, 0x9B, 0x70, 0xF3, 0xC7, 0x80, 0x60, 0x73, 0x9F, 0x6C, 0xC0, 0xF1, 0x3D, 0xBB, 0x40, 0xB3, 0xC8, 0x37, 0x14, 0xDF, 0x49, 0xDA, 0xD4, 0x48, 0x22, 0x78, 0x80, 0x6E, 0xCD, 0xE7, 0x00, 0x81, 0x86, 0x68, 0xE1, 0x5D, 0x7C, 0x54, 0x2C, 0x55, 0x7B, 0xEF, 0x48, 0x42, 0x7B, 0x3B, 0x68, 0xE3, 0xDB, 0xAA, 0xC0, 0x0F, 0xA9, 0x96, 0x20, 0x95, 0x05, 0x93, 0x94, 0x9A, 0xF6, 0xA3, 0x64, 0x5D, 0xCC, 0x76, 0x00, 0xE5, 0x08, 0x19, 0xE8, 0x8D, 0x29, 0xD7, 0x4C, 0x21, 0x91, 0x17, 0xF4, 0xBC, 0x6A, 0xB3, 0x80, 0x83, 0xC6, 0xD4, 0x90, 0x9B, 0xAE, 0x0E, 0xFE, 0x2E, 0x4A, 0xF2, 0x00, 0x73, 0x88, 0xD9, 0x40, 0x66, 0xC5, 0xD4, 0x08, 0x57, 0xB1, 0x89, 0x48, 0xDC, 0x54, 0xFC, 0x43, 0x6A, 0x26, 0x87, 0xB8, 0x09, 0x5F, 0xCE, 0x80, 0xE4, 0x0B, 0x05, 0x9C, 0x24, 0xF3, 0xDE, 0xE2, 0x3E, 0xEC, 0x38, 0x8A, 0xA2, 0x55, 0xA4, 0x50, 0x4E, 0x4B, 0xE9, 0x58, 0x7F, 0x9F, 0x7D, 0x80, 0x23, 0x0C, 0x4D, 0x80, 0x05, 0x44, 0x26, 0xB8, 0xE9, 0xD8, 0xBC, 0xE6, 0x76, 0x3A, 0x6E, 0xA4, 0x19, 0xDE, 0xC2, 0xD0, 0xC4, 0xBC, 0xC3, 0x5C, 0x59, 0xDF, 0x16, 0x46, 0x39, 0x70, 0xF4, 0xEE, 0x2D, 0x58, 0x5A, 0xA8, 0x17, 0x86, 0x6B, 0x60, 0x29, 0x58, 0x4D, 0xD2, 0x5F, 0x28, 0x7A, 0xD8, 0x8E, 0x79, 0xEA, 0x82, 0x94, 0x33, 0x31, 0x81, 0xD9, 0x22, 0xD5, 0x10, 0xDA, 0x92, 0xA0, 0x7D, 0x3D, 0xDA, 0xAC, 0x1C, 0xA2, 0x53, 0x31, 0xB8, 0x3C, 0x96, 0x52, 0x00, 0x82, 0x6B, 0x56, 0xA0, 0xD3, 0xC2, 0x40, 0xC7, 0x1B, 0x7F, 0xDC, 0x01, 0x72, 0x70, 0xB1, 0x8C, 0x01, 0x09, 0x09, 0x36, 0xFC, 0x97, 0xEA, 0xDE, 0xE3, 0x0D, 0xAE, 0x7E, 0xE3, 0x0D, 0xAE, 0x7E, 0x33, 0x69, 0x80, 0x40 }; static uint8_t UsbdSecPlainTextData[128] __attribute__ ((aligned(4))) = { 0xD1, 0xD2, 0xF2, 0x80, 0x6E, 0xBA, 0x0C, 0xC0, 0xB6, 0xC4, 0xC9, 0xD8, 0x61, 0x75, 0x1D, 0x1A, 0x3F, 0x95, 0x58, 0xBE, 0xD8, 0x0D, 0xE2, 0xC0, 0xD0, 0x21, 0x79, 0x20, 0x65, 0x2D, 0x99, 0x40, 0x3C, 0x96, 0x52, 0x00, 0x1B, 0x7F, 0xDC, 0x01, 0x82, 0x1C, 0x13, 0xD8, 0x33, 0x69, 0x80, 0x40, 0xFC, 0x97, 0xEA, 0xDE, 0x08, 0xEA, 0x14, 0xDC, 0xEB, 0x0F, 0x6A, 0x18, 0x6F, 0x78, 0x2C, 0xB0, 0xD3, 0xC2, 0x40, 0xC7, 0x82, 0x6B, 0x56, 0xA0, 0x19, 0x09, 0x36, 0xE0, 0x72, 0x70, 0xB1, 0x8C, 0xE3, 0x0D, 0xAE, 0x7E, 0x50, 0xA5, 0x2B, 0xE2, 0xC9, 0xAF, 0xC7, 0x70, 0x1C, 0x29, 0x80, 0x56, 0x24, 0xF0, 0x66, 0xFA, 0x02, 0x2B, 0x58, 0x98, 0x8F, 0xE4, 0xD1, 0x3C, 0x6E, 0x38, 0x2A, 0xFF, 0xB8, 0xFA, 0x35, 0xB0, 0x52, 0x49, 0xC5, 0xB4, 0x66, 0xFA, 0x47, 0x55, 0x6C, 0x8D, 0x40, 0x08 }; void UsbdSecXSM3AuthenticationCrypt(const uint8_t *key, const uint8_t *input, size_t length, uint8_t *output, uint8_t encrypt) { EXCRYPT_DES3_STATE des; uint64_t sk[3]; uint8_t iv[8]; // clear local variables memset(iv, 0, sizeof(iv)); // run parity on the key ExCryptDesParity(key, 0x10, (uint8_t *)sk); sk[2] = sk[0]; // set the key in the state and run triple-des cbc en/decryption on it ExCryptDes3Key(&des, sk); ExCryptDes3Cbc(&des, input, length, output, iv, encrypt); } void UsbdSecXSM3AuthenticationMac(const uint8_t *key, uint8_t *salt, uint8_t *input, size_t length, uint8_t *output) { EXCRYPT_DES3_STATE des3; EXCRYPT_DES_STATE des; uint64_t sk[3]; uint8_t iv[8]; uint8_t temp[8]; uint64_t input_temp; size_t i; // clear iv + temp value of stack junk memset(iv, 0, sizeof(iv)); memset(temp, 0, sizeof(temp)); // run parity on the key ExCryptDesParity(key, 0x10, (uint8_t *)sk); sk[2] = sk[0]; // set the key in our initial des state ExCryptDesKey(&des, (uint8_t *)&sk[0]); // if we have a salt, encrypt it into the temp value if (salt) { memcpy(&input_temp, salt, sizeof(input_temp)); input_temp = SWAP64(SWAP64(input_temp) + 1); memcpy(salt, &input_temp, sizeof(input_temp)); // no idea what this does ExCryptDesEcb(&des, salt, temp, 1); } // for every 8 byte input block, xor the temp value with it and encrypt over itself for (i = 0; i < length; i += 8) { memcpy(&input_temp, input+i, sizeof(input_temp)); *(uint64_t *)temp ^= input_temp; ExCryptDesEcb(&des, temp, temp, 1); } // xor the highest bit of the temp value temp[0] ^= 0x80; // set the key and perform the final triple-des encryption ExCryptDes3Key(&des3, sk); ExCryptDes3Cbc(&des3, temp, 8, output, iv, 1); // real kernel does the following, but the above works: // XeCryptDesEcb(des_state_1, temp, temp, 1); // XeCryptDesEcb(des_state_2, temp, temp, 0); // XeCryptDesEcb(des_state_1, temp, output, 1); } void UsbdSecXSMAuthenticationAcr(const uint8_t *console_id, const uint8_t *input, const uint8_t *key, uint8_t *output) { uint8_t block[8]; uint8_t iv[8]; uint8_t ab[8]; uint8_t cd[8]; // fill in the input block with the first 4 bytes of input data and the first 4 bytes of the console ID // *(uint32_t *)block = *(uint32_t *)input; // *(uint32_t *)(block + 4) = *(uint32_t *)console_id; memcpy(block, input, 4); memcpy(block+4, console_id, 4); // run custom "parve" crypto algorithms. idk whar they do ExCryptParveEcb(key, UsbdSecSboxData, input + 0x10, iv); ExCryptParveEcb(key, UsbdSecSboxData, block, cd); ExCryptParveCbcMac(key, UsbdSecSboxData, iv, UsbdSecPlainTextData, 0x80, ab); ExCryptChainAndSumMac((uint32_t *)cd, (uint32_t *)ab, (uint32_t *)UsbdSecPlainTextData, 0x20, (uint32_t *)output); uint64_t current; memcpy(¤t, output, sizeof(current)); current ^= *(uint64_t *)ab; // *(uint64_t *)output ^= *(uint64_t *)ab; memcpy(output, ¤t, sizeof(current)); }