Skip to content

Commit 6485566

Browse files
authored
crypto: add aes/sha example (#9)
1 parent 38a1592 commit 6485566

2 files changed

Lines changed: 287 additions & 0 deletions

File tree

crypto/Makefile

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#---------------------------------------------------------------------------------
2+
# Clear the implicit built in rules
3+
#---------------------------------------------------------------------------------
4+
.SUFFIXES:
5+
#---------------------------------------------------------------------------------
6+
ifeq ($(strip $(DEVKITPPC)),)
7+
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
8+
endif
9+
10+
include $(DEVKITPPC)/wii_rules
11+
12+
#---------------------------------------------------------------------------------
13+
# TARGET is the name of the output
14+
# BUILD is the directory where object files & intermediate files will be placed
15+
# SOURCES is a list of directories containing source code
16+
# INCLUDES is a list of directories containing extra header files
17+
#---------------------------------------------------------------------------------
18+
TARGET := $(notdir $(CURDIR))
19+
BUILD := build
20+
SOURCES := source
21+
DATA := data
22+
INCLUDES :=
23+
24+
#---------------------------------------------------------------------------------
25+
# options for code generation
26+
#---------------------------------------------------------------------------------
27+
28+
CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
29+
CXXFLAGS = $(CFLAGS)
30+
31+
LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
32+
33+
#---------------------------------------------------------------------------------
34+
# any extra libraries we wish to link with the project
35+
#---------------------------------------------------------------------------------
36+
LIBS := -lwiiuse -lbte -logc -lm
37+
38+
#---------------------------------------------------------------------------------
39+
# list of directories containing libraries, this must be the top level containing
40+
# include and lib
41+
#---------------------------------------------------------------------------------
42+
LIBDIRS :=
43+
44+
#---------------------------------------------------------------------------------
45+
# no real need to edit anything past this point unless you need to add additional
46+
# rules for different file extensions
47+
#---------------------------------------------------------------------------------
48+
ifneq ($(BUILD),$(notdir $(CURDIR)))
49+
#---------------------------------------------------------------------------------
50+
51+
export OUTPUT := $(CURDIR)/$(TARGET)
52+
53+
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
54+
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
55+
56+
export DEPSDIR := $(CURDIR)/$(BUILD)
57+
58+
#---------------------------------------------------------------------------------
59+
# automatically build a list of object files for our project
60+
#---------------------------------------------------------------------------------
61+
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
62+
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
63+
sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
64+
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
65+
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
66+
67+
#---------------------------------------------------------------------------------
68+
# use CXX for linking C++ projects, CC for standard C
69+
#---------------------------------------------------------------------------------
70+
ifeq ($(strip $(CPPFILES)),)
71+
export LD := $(CC)
72+
else
73+
export LD := $(CXX)
74+
endif
75+
76+
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
77+
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
78+
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)
79+
80+
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
81+
82+
#---------------------------------------------------------------------------------
83+
# build a list of include paths
84+
#---------------------------------------------------------------------------------
85+
export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
86+
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
87+
-I$(CURDIR)/$(BUILD) \
88+
-I$(LIBOGC_INC)
89+
90+
#---------------------------------------------------------------------------------
91+
# build a list of library paths
92+
#---------------------------------------------------------------------------------
93+
export LIBPATHS := -L$(LIBOGC_LIB) $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
94+
95+
export OUTPUT := $(CURDIR)/$(TARGET)
96+
.PHONY: $(BUILD) clean
97+
98+
#---------------------------------------------------------------------------------
99+
$(BUILD):
100+
@[ -d $@ ] || mkdir -p $@
101+
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
102+
103+
#---------------------------------------------------------------------------------
104+
clean:
105+
@echo clean ...
106+
@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol
107+
108+
#---------------------------------------------------------------------------------
109+
run:
110+
wiiload $(TARGET).dol
111+
112+
113+
#---------------------------------------------------------------------------------
114+
else
115+
116+
DEPENDS := $(OFILES:.o=.d)
117+
118+
#---------------------------------------------------------------------------------
119+
# main targets
120+
#---------------------------------------------------------------------------------
121+
$(OUTPUT).dol: $(OUTPUT).elf
122+
$(OUTPUT).elf: $(OFILES)
123+
124+
$(OFILES_SOURCES) : $(HFILES)
125+
126+
#---------------------------------------------------------------------------------
127+
# This rule links in binary data with the .jpg extension
128+
#---------------------------------------------------------------------------------
129+
%.jpg.o %_jpg.h : %.jpg
130+
#---------------------------------------------------------------------------------
131+
@echo $(notdir $<)
132+
$(bin2o)
133+
134+
-include $(DEPENDS)
135+
136+
#---------------------------------------------------------------------------------
137+
endif
138+
#---------------------------------------------------------------------------------

crypto/source/crypto.c

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <malloc.h>
4+
#include <string.h>
5+
#include <gccore.h>
6+
#include <wiiuse/wpad.h>
7+
8+
static const s32 messageSize = 0x20000;
9+
static void *xfb = NULL;
10+
static GXRModeObj *rmode = NULL;
11+
static void* input = NULL;
12+
static void* buffer = NULL;
13+
static sha_context context ATTRIBUTE_ALIGN(32);
14+
static const u8 key[0x10] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
15+
static const u8 iv[0x10] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F };
16+
static const u32 expectedSha1[4] = { 0x9E4EE857, 0x2F0568A6, 0x206743F4, 0x2BD62721 };
17+
static u32 hash[4] = {};
18+
void wait_for_input()
19+
{
20+
while(1) {
21+
22+
// Call WPAD_ScanPads each loop, this reads the latest controller states
23+
WPAD_ScanPads();
24+
25+
// WPAD_ButtonsDown tells us which buttons were pressed in this loop
26+
// this is a "one shot" state which will not fire again until the button has been released
27+
u32 pressed = WPAD_ButtonsDown(0);
28+
29+
// We return to the launcher application via exit
30+
if ( pressed & WPAD_BUTTON_HOME ) exit(0);
31+
32+
// Wait for the next frame
33+
VIDEO_WaitVSync();
34+
}
35+
}
36+
void cleanup_and_wait()
37+
{
38+
if(input != NULL)
39+
free(input);
40+
if(buffer != NULL)
41+
free(buffer);
42+
AES_Close();
43+
SHA_Close();
44+
wait_for_input();
45+
}
46+
//---------------------------------------------------------------------------------
47+
int main(int argc, char **argv) {
48+
//---------------------------------------------------------------------------------
49+
50+
// Initialise the video system
51+
VIDEO_Init();
52+
53+
// This function initialises the attached controllers
54+
WPAD_Init();
55+
56+
// Obtain the preferred video mode from the system
57+
// This will correspond to the settings in the Wii menu
58+
rmode = VIDEO_GetPreferredMode(NULL);
59+
60+
// Allocate memory for the display in the uncached region
61+
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
62+
63+
// Initialise the console, required for printf
64+
console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
65+
66+
// Set up the video registers with the chosen mode
67+
VIDEO_Configure(rmode);
68+
69+
// Tell the video hardware where our display memory is
70+
VIDEO_SetNextFramebuffer(xfb);
71+
72+
// Make the display visible
73+
VIDEO_SetBlack(FALSE);
74+
75+
// Flush the video register changes to the hardware
76+
VIDEO_Flush();
77+
78+
// Wait for Video setup to complete
79+
VIDEO_WaitVSync();
80+
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
81+
82+
// The console understands VT terminal escape codes
83+
// This positions the cursor on row 2, column 0
84+
// we can use variables for this with format codes too
85+
// e.g. printf ("\x1b[%d;%dH", row, column );
86+
printf("\x1b[2;0H");
87+
88+
// Applications should init the AES & SHA1 code before usage
89+
if (AES_Init() < 0)
90+
{
91+
printf("failed to init AES engine");
92+
cleanup_and_wait();
93+
}
94+
95+
if (SHA_Init() < 0)
96+
{
97+
printf("failed to init SHA engine");
98+
cleanup_and_wait();
99+
}
100+
101+
if(SHA_InitializeContext(&context) < 0)
102+
{
103+
printf("failed to setup sha1 context");
104+
cleanup_and_wait();
105+
}
106+
107+
input = memalign(32, messageSize);
108+
buffer = memalign(32, messageSize);
109+
if(input == NULL || buffer == NULL)
110+
{
111+
printf("failed to allocate the buffers");
112+
cleanup_and_wait();
113+
}
114+
115+
memset(buffer, 0, messageSize);
116+
memset(input, 0xA5A5A5A5, messageSize);
117+
118+
s32 ret = AES_Encrypt(key, 0x10, iv, 0x10, input, buffer, messageSize);
119+
if(ret < 0)
120+
{
121+
printf("failed to encrypt input: %d", ret);
122+
cleanup_and_wait();
123+
}
124+
125+
ret = SHA_Calculate(&context, buffer, messageSize, hash);
126+
if(ret < 0)
127+
{
128+
printf("failed to calculate hash : %d", ret);
129+
cleanup_and_wait();
130+
}
131+
132+
printf("calculated hash : %08X%08X%08X%08X\n", hash[0], hash[1], hash[2], hash[3]);
133+
printf("expected hash : %08X%08X%08X%08X\n", expectedSha1[0], expectedSha1[1], expectedSha1[2], expectedSha1[3]);
134+
135+
ret = AES_Decrypt(key, 0x10, iv, 0x10, buffer, input, messageSize);
136+
if(ret < 0)
137+
{
138+
printf("failed to decrypt input");
139+
cleanup_and_wait();
140+
}
141+
142+
SHA_InitializeContext(&context);
143+
SHA_Calculate(&context, input, messageSize, hash);
144+
printf("decrypted hash : %08X%08X%08X%08X\n", hash[0], hash[1], hash[2], hash[3]);
145+
146+
//cleanup everything
147+
cleanup_and_wait();
148+
return 0;
149+
}

0 commit comments

Comments
 (0)