|
| 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