Skip to content

Commit

Permalink
C version re-added and made a release for it
Browse files Browse the repository at this point in the history
Might be more noob friendly than a python script.
  • Loading branch information
zoogie committed Oct 31, 2022
1 parent f31cbc0 commit 66d029d
Show file tree
Hide file tree
Showing 22 changed files with 8,597 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE-ctrtool
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2012 neimod
Copyright (c) 2014 3DSGuy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SOURCES = source source/polarssl

CFILES := $(foreach dir, $(SOURCES), $(wildcard $(dir)/*.c))
CPPFILES := $(foreach dir, $(SOURCES), $(wildcard $(dir)/*.cpp))

OBJS = $(CFILES:.c=.o) $(CPPFILES:.cpp=.o)

LIBS = -static-libstdc++ -static
CXXFLAGS = -Isource
CFLAGS = -std=c11 -Wall -Wextra -Os -posix -Isource -D_GNU_SOURCE
OUTPUT = mkey
CC = gcc

main: $(OBJS)
g++ -o $(OUTPUT) $(LIBS) $(OBJS)

clean:
rm -f $(OUTPUT) $(OUTPUT).exe $(OBJS)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Compatible with: 1.0-11.16 US,EU,JP,KR,TW,CN</br>
- Attempt to unlock your parental controls from your 3DS's System Settings until it gives you an "Inquiry Number" (chose "I forgot" twice).
- Next, read the rem comments in the "mkey.bat" script in a text editor for the remaining instructions, then run the same .bat script.
- Your masterkey should be generated, which you can use to unlock your 3DS in System Settings.
Note: A windows exe is also provided in the release tab. You simply launch it and follow directions.

mkey
====
Expand Down
155 changes: 155 additions & 0 deletions source/ctr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "ctr.h"

void ctr_set_iv( ctr_aes_context* ctx,
u8 iv[16] )
{
memcpy(ctx->iv, iv, 16);
}

void ctr_add_counter( ctr_aes_context* ctx,
u32 carry )
{
u32 counter[4];
u32 sum;
int i;

for(i=0; i<4; i++)
counter[i] = (ctx->ctr[i*4+0]<<24) | (ctx->ctr[i*4+1]<<16) | (ctx->ctr[i*4+2]<<8) | (ctx->ctr[i*4+3]<<0);

for(i=3; i>=0; i--)
{
sum = counter[i] + carry;

if (sum < counter[i])
carry = 1;
else
carry = 0;

counter[i] = sum;
}

for(i=0; i<4; i++)
{
ctx->ctr[i*4+0] = counter[i]>>24;
ctx->ctr[i*4+1] = counter[i]>>16;
ctx->ctr[i*4+2] = counter[i]>>8;
ctx->ctr[i*4+3] = counter[i]>>0;
}
}

void ctr_set_counter( ctr_aes_context* ctx,
u8 ctr[16] )
{
memcpy(ctx->ctr, ctr, 16);
}


void ctr_init_counter( ctr_aes_context* ctx,
u8 key[16],
u8 ctr[16] )
{
aes_setkey_enc(&ctx->aes, key, 128);
ctr_set_counter(ctx, ctr);
}


void ctr_crypt_counter_block( ctr_aes_context* ctx,
u8 input[16],
u8 output[16] )
{
int i;
u8 stream[16];


aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->ctr, stream);


if (input)
{
for(i=0; i<16; i++)
{
output[i] = stream[i] ^ input[i];
}
}
else
{
for(i=0; i<16; i++)
output[i] = stream[i];
}

ctr_add_counter(ctx, 1);
}


void ctr_crypt_counter( ctr_aes_context* ctx,
u8* input,
u8* output,
u32 size )
{
u8 stream[16];
u32 i;

while(size >= 16)
{
ctr_crypt_counter_block(ctx, input, output);

if (input)
input += 16;
if (output)
output += 16;

size -= 16;
}

if (size)
{
memset(stream, 0, 16);
ctr_crypt_counter_block(ctx, stream, stream);

if (input)
{
for(i=0; i<size; i++)
output[i] = input[i] ^ stream[i];
}
else
{
memcpy(output, stream, size);
}
}
}

void ctr_init_cbc_encrypt( ctr_aes_context* ctx,
u8 key[16],
u8 iv[16] )
{
aes_setkey_enc(&ctx->aes, key, 128);
ctr_set_iv(ctx, iv);
}

void ctr_init_cbc_decrypt( ctr_aes_context* ctx,
u8 key[16],
u8 iv[16] )
{
aes_setkey_dec(&ctx->aes, key, 128);
ctr_set_iv(ctx, iv);
}

void ctr_encrypt_cbc( ctr_aes_context* ctx,
u8* input,
u8* output,
u32 size )
{
aes_crypt_cbc(&ctx->aes, AES_ENCRYPT, size, ctx->iv, input, output);
}

void ctr_decrypt_cbc( ctr_aes_context* ctx,
u8* input,
u8* output,
u32 size )
{
aes_crypt_cbc(&ctx->aes, AES_DECRYPT, size, ctx->iv, input, output);
}
59 changes: 59 additions & 0 deletions source/ctr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef _CTR_H
#define _CTR_H

#include "utils.h"

#include "polarssl/aes.h"

typedef struct
{
u8 ctr[16];
u8 iv[16];
aes_context aes;
} ctr_aes_context;

void ctr_set_iv( ctr_aes_context* ctx,
u8 iv[16] );

void ctr_add_counter( ctr_aes_context* ctx,
u32 carry );

void ctr_set_counter( ctr_aes_context* ctx,
u8 ctr[16] );


void ctr_init_counter( ctr_aes_context* ctx,
u8 key[16],
u8 ctr[16] );


void ctr_crypt_counter_block( ctr_aes_context* ctx,
u8 input[16],
u8 output[16] );


void ctr_crypt_counter( ctr_aes_context* ctx,
u8* input,
u8* output,
u32 size );


void ctr_init_cbc_encrypt( ctr_aes_context* ctx,
u8 key[16],
u8 iv[16] );

void ctr_init_cbc_decrypt( ctr_aes_context* ctx,
u8 key[16],
u8 iv[16] );

void ctr_encrypt_cbc( ctr_aes_context* ctx,
u8* input,
u8* output,
u32 size );

void ctr_decrypt_cbc( ctr_aes_context* ctx,
u8* input,
u8* output,
u32 size );

#endif
Loading

0 comments on commit 66d029d

Please sign in to comment.