Skip to content

Commit 7e37e62

Browse files
authored
aes: add code to encrypt/decrypt using the aes engine (#132)
* aes: add code to encrypt/decrypt using the aes engine
1 parent d946593 commit 7e37e62

4 files changed

Lines changed: 184 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ OGCOBJ := \
147147
message.o card.o aram.o depackrnc.o decrementer_handler.o \
148148
depackrnc1.o dsp.o si.o tpl.o ipc.o ogc_crt0.o \
149149
console_font_8x16.o timesupp.o lock_supp.o newlibc.o usbgecko.o usbmouse.o \
150-
sbrk.o malloc_lock.o kprintf.o stm.o ios.o es.o isfs.o usb.o network_common.o \
150+
sbrk.o malloc_lock.o kprintf.o stm.o aes.o ios.o es.o isfs.o usb.o network_common.o \
151151
sdgecko_io.o sdgecko_buf.o gcsd.o argv.o network_wii.o wiisd.o conf.o usbstorage.o \
152152
texconv.o wiilaunch.o
153153

gc/gccore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ distribution.
6767

6868
#if defined(HW_RVL)
6969
#include "ogc/ipc.h"
70+
#include "ogc/aes.h"
7071
#include "ogc/es.h"
7172
#include "ogc/stm.h"
7273
#include "ogc/ios.h"

gc/ogc/aes.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*-------------------------------------------------------------
2+
3+
aes.h -- AES Engine
4+
5+
Copyright (C) 2022
6+
GaryOderNichts
7+
Joris 'DacoTaco' Vermeylen info@dacotaco.com
8+
9+
This software is provided 'as-is', without any express or implied
10+
warranty. In no event will the authors be held liable for any
11+
damages arising from the use of this software.
12+
13+
Permission is granted to anyone to use this software for any
14+
purpose, including commercial applications, and to alter it and
15+
redistribute it freely, subject to the following restrictions:
16+
17+
1. The origin of this software must not be misrepresented; you
18+
must not claim that you wrote the original software. If you use
19+
this software in a product, an acknowledgment in the product
20+
documentation would be appreciated but is not required.
21+
22+
2. Altered source versions must be plainly marked as such, and
23+
must not be misrepresented as being the original software.
24+
25+
3. This notice may not be removed or altered from any source
26+
distribution.
27+
28+
-------------------------------------------------------------*/
29+
30+
31+
#ifndef __AES_H___
32+
#define __AES_H___
33+
34+
#if defined(HW_RVL)
35+
#include <gctypes.h>
36+
#include <gcutil.h>
37+
38+
#define AES_BLOCK_SIZE 128
39+
40+
#ifdef __cplusplus
41+
extern "C" {
42+
#endif /* __cplusplus */
43+
44+
s32 AES_Init(void);
45+
s32 AES_Close(void);
46+
s32 AES_Decrypt(const void* key, u32 key_size, const void* iv, u32 iv_size, const void* in_data, void* out_data, u32 data_size);
47+
s32 AES_Encrypt(const void* key, u32 key_size, const void* iv, u32 iv_size, const void* in_data, void* out_data, u32 data_size);
48+
49+
#ifdef __cplusplus
50+
}
51+
#endif /* __cplusplus */
52+
#endif
53+
#endif

libogc/aes.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*-------------------------------------------------------------
2+
3+
aes.c -- AES Engine
4+
5+
Copyright (C) 2022
6+
GaryOderNichts
7+
Joris 'DacoTaco' Vermeylen info@dacotaco.com
8+
9+
This software is provided 'as-is', without any express or implied
10+
warranty. In no event will the authors be held liable for any
11+
damages arising from the use of this software.
12+
13+
Permission is granted to anyone to use this software for any
14+
purpose, including commercial applications, and to alter it and
15+
redistribute it freely, subject to the following restrictions:
16+
17+
1. The origin of this software must not be misrepresented; you
18+
must not claim that you wrote the original software. If you use
19+
this software in a product, an acknowledgment in the product
20+
documentation would be appreciated but is not required.
21+
22+
2. Altered source versions must be plainly marked as such, and
23+
must not be misrepresented as being the original software.
24+
25+
3. This notice may not be removed or altered from any source
26+
distribution.
27+
28+
-------------------------------------------------------------*/
29+
#if defined(HW_RVL)
30+
31+
#include <string.h>
32+
#include "gctypes.h"
33+
#include "gcutil.h"
34+
#include "ipc.h"
35+
#include "aes.h"
36+
37+
#define AES_HEAPSIZE 0x1000
38+
39+
#define AES_IOCTLV_ENCRYPT 2
40+
#define AES_IOCTLV_DECRYPT 3
41+
42+
static s32 __aes_fd = -1;
43+
static s32 __aes_hid = -1;
44+
45+
static s32 AES_ExecuteCommand(s32 command, const void* key, u32 key_size, const void* iv, u32 iv_size, const void* in_data, void* out_data, u32 data_size)
46+
{
47+
ioctlv* params = (ioctlv*)iosAlloc(__aes_hid, sizeof(ioctlv) * 4);
48+
void* tmpiv = iosAlloc(__aes_hid, 16);
49+
void* block = iosAlloc(__aes_hid, AES_BLOCK_SIZE);
50+
if (!params || !tmpiv || !block)
51+
return -1;
52+
53+
memcpy(tmpiv, iv, 16);
54+
55+
s32 ret = -1;
56+
for (u32 i = 0; i < data_size; i += AES_BLOCK_SIZE) {
57+
memcpy(block, (void*)((u32)in_data + i), AES_BLOCK_SIZE);
58+
59+
params[0].data = block;
60+
params[0].len = AES_BLOCK_SIZE;
61+
params[1].data = (void*) key;
62+
params[1].len = key_size;
63+
params[2].data = block;
64+
params[2].len = AES_BLOCK_SIZE;
65+
params[3].data = tmpiv;
66+
params[3].len = 16;
67+
ret = IOS_Ioctlv(__aes_fd, command, 2, 2, params);
68+
if (ret < 0)
69+
break;
70+
71+
memcpy((void*)((u32)out_data + i), block, AES_BLOCK_SIZE);
72+
}
73+
74+
iosFree(__aes_hid, block);
75+
iosFree(__aes_hid, tmpiv);
76+
iosFree(__aes_hid, params);
77+
return ret;
78+
}
79+
80+
s32 AES_Init(void)
81+
{
82+
if (__aes_fd >= 0)
83+
return -1;
84+
85+
__aes_fd = IOS_Open("/dev/aes", 0);
86+
if (__aes_fd < 0)
87+
return __aes_fd;
88+
89+
//only create heap if it wasn't created yet.
90+
//its never disposed, so only create once.
91+
if(__aes_hid < 0)
92+
__aes_hid = iosCreateHeap(AES_HEAPSIZE);
93+
94+
if (__aes_hid < 0) {
95+
AES_Close();
96+
return __aes_hid;
97+
}
98+
99+
return 0;
100+
}
101+
102+
s32 AES_Close(void)
103+
{
104+
if (__aes_fd < 0)
105+
return -1;
106+
107+
IOS_Close(__aes_fd);
108+
__aes_fd = -1;
109+
110+
return 0;
111+
}
112+
113+
s32 AES_Encrypt(const void* key, u32 key_size, const void* iv, u32 iv_size, const void* in_data, void* out_data, u32 data_size)
114+
{
115+
if (key_size != 16 || iv_size != 16)
116+
return -1;
117+
118+
return AES_ExecuteCommand(AES_IOCTLV_ENCRYPT, key, key_size, iv, iv_size, in_data, out_data, data_size);
119+
}
120+
121+
s32 AES_Decrypt(const void* key, u32 key_size, const void* iv, u32 iv_size, const void* in_data, void* out_data, u32 data_size)
122+
{
123+
if (key_size != 16 || iv_size != 16)
124+
return -1;
125+
126+
return AES_ExecuteCommand(AES_IOCTLV_DECRYPT, key, key_size, iv, iv_size, in_data, out_data, data_size);
127+
}
128+
129+
#endif

0 commit comments

Comments
 (0)