33#include "sha1.h"
44#include "crypto.h"
55//#include "ticket0.h"
6+ #include "tonccpy.h"
67#include "utils.h"
78
89// more info:
1314static const uint32_t DSi_NAND_KEY_Y [4 ] =
1415 {0x0ab9dc76u , 0xbd4dc4d3u , 0x202ddd1du , 0xe1a00005u };
1516
17+ static const uint32_t DSi_DEV_3DS_NAND_KEY_Y [4 ] =
18+ {0xf176bfaau , 0x66e8b87au , 0x266a6497u , 0xe1a00005u };
19+
1620static const uint32_t DSi_ES_KEY_Y [4 ] =
1721 {0x8b5acce5u , 0x72c9d056u , 0xdce8179cu , 0xa9361239u };
1822
@@ -78,33 +82,56 @@ static inline void rol42_128(uint32_t *a){
7882 a [0 ] = (t3 << 10 ) | (t2 >> 22 );
7983}
8084
85+ void populate_dsi_nand_key_y (uint8_t * out , bool is_dev_3DS ) {
86+ if (out == NULL )
87+ return ;
88+
89+ const uint32_t * nand_key_y = DSi_NAND_KEY_Y ;
90+ if (is_dev_3DS )
91+ nand_key_y = DSi_DEV_3DS_NAND_KEY_Y ;
92+
93+ // This would not work in a Big Endian platform...
94+ tonccpy (out , nand_key_y , 16 );
95+ }
96+
8197static void dsi_aes_set_key (uint32_t * rk , const uint32_t * console_id , key_mode_t mode ) {
8298 uint32_t key [4 ];
99+ const uint32_t * dsi_key_y = NULL ;
83100 switch (mode ) {
84101 case NAND :
85102 key [0 ] = console_id [0 ];
86103 key [1 ] = console_id [0 ] ^ 0x24ee6906 ;
87104 key [2 ] = console_id [1 ] ^ 0xe65b601d ;
88105 key [3 ] = console_id [1 ];
106+ dsi_key_y = DSi_NAND_KEY_Y ;
89107 break ;
90108 case NAND_3DS :
91109 key [0 ] = console_id [0 ];
92110 key [1 ] = 0x544e494e ;
93111 key [2 ] = 0x4f444e45 ;
94112 key [3 ] = console_id [1 ];
113+ dsi_key_y = DSi_NAND_KEY_Y ;
114+ break ;
115+ case NAND_DEV_3DS :
116+ key [0 ] = console_id [0 ];
117+ key [1 ] = 0xee7a4b1e ;
118+ key [2 ] = 0xaf42c08b ;
119+ key [3 ] = console_id [1 ];
120+ dsi_key_y = DSi_DEV_3DS_NAND_KEY_Y ;
95121 break ;
96122 case ES :
97123 key [0 ] = 0x4e00004a ;
98124 key [1 ] = 0x4a00004e ;
99125 key [2 ] = console_id [1 ] ^ 0xc80c4b72 ;
100126 key [3 ] = console_id [0 ];
127+ dsi_key_y = DSi_ES_KEY_Y ;
101128 break ;
102129 default :
103130 break ;
104131 }
105132 // Key = ((Key_X XOR Key_Y) + FFFEFB4E295902582A680F5F1A4F3E79h) ROL 42
106133 // equivalent to F_XY in twltool/f_xy.c
107- xor_128 (key , key , mode == ES ? DSi_ES_KEY_Y : DSi_NAND_KEY_Y );
134+ xor_128 (key , key , dsi_key_y );
108135 // iprintf("AES KEY: XOR KEY_Y:\n");
109136 // print_bytes(key, 16);
110137 add_128 (key , DSi_KEY_MAGIC );
@@ -138,7 +165,7 @@ static uint32_t boot2_rk[RK_LEN];
138165
139166static int tables_generated = 0 ;
140167
141- void dsi_crypt_init (const uint8_t * console_id_be , const uint8_t * emmc_cid , int is3DS ) {
168+ void dsi_crypt_init (const uint8_t * console_id_be , const uint8_t * emmc_cid , int is3DS , bool is_dev_3DS ) {
142169 if (tables_generated == 0 ) {
143170 aes_gen_tables ();
144171 tables_generated = 1 ;
@@ -148,7 +175,7 @@ void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int i
148175 GET_UINT32_BE (console_id [0 ], console_id_be , 4 );
149176 GET_UINT32_BE (console_id [1 ], console_id_be , 0 );
150177
151- dsi_aes_set_key (nand_rk , console_id , is3DS ? NAND_3DS : NAND );
178+ dsi_aes_set_key (nand_rk , console_id , is_dev_3DS ? NAND_DEV_3DS : ( is3DS ? NAND_3DS : NAND ) );
152179 dsi_aes_set_key (es_rk , console_id , ES );
153180
154181 aes_set_key_enc_128_be (boot2_rk , (uint8_t * )DSi_BOOT2_KEY );
0 commit comments