1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - AES - include
3   File:     util.h
4 
5   Copyright 2007-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef	TWL_AES_ARM9_UTIL_H_
19 #define	TWL_AES_ARM9_UTIL_H_
20 
21 #ifdef	__cplusplus
22 extern "C" {
23 #endif
24 
25 /*===========================================================================*/
26 
27 #include	<nitro/os/common/emulator.h>
28 #include	<twl/aes/common/types.h>
29 
30 
31 /*---------------------------------------------------------------------------*
32     Constant Definitions
33  *---------------------------------------------------------------------------*/
34 
35 #define AES_ENCRYPT_HEADER_SIZE 16
36 #define AES_SIGN_HEADER_SIZE    32
37 
38 
39 
40 /*---------------------------------------------------------------------------*
41 	Function Declarations
42  *---------------------------------------------------------------------------*/
43 
44 void AESi_InitRand(void);
45 void AESi_PrepairEncryptAndSign(AESNonce* pNonce, u32 srcSize, void* dst);
46 AESResult AESi_PrepairDecryptAndVerify(AESNonce* pNonce, const void* src, u32 srcSize);
47 
48 AESResult AESi_Rand(void* pBuffer, u32 size);
49 
50 // dst size = srcSize + 16
51 AESResult AESi_Encrypt(
52             const void* src,
53             u32         srcSize,
54             void*       dst,
55             AESCallback callback,
56             void*       arg );
57 
58 // dst size = srcSize - 16
59 AESResult AESi_Decrypt(
60             const void* src,
61             u32         srcSize,
62             void*       dst,
63             AESCallback callback,
64             void*       arg );
65 
66 // dst size = srcSize + 32
67 AESResult AESi_EncryptAndSign(
68             const void* src,
69             u32         srcSize,
70             void*       dst,
71             AESCallback callback,
72             void*       arg );
73 
74 // dst size = srcSize - 32
75 AESResult AESi_DecryptAndVerify(
76             const void* src,
77             u32         srcSize,
78             void*       dst,
79             AESCallback callback,
80             void*       arg );
81 
82 
83 
84 /*---------------------------------------------------------------------------*
85 	Inline Function Definitions
86  *---------------------------------------------------------------------------*/
87 
AES_Rand(void * pBuffer,u32 size)88 SDK_INLINE AESResult AES_Rand(void* pBuffer, u32 size)
89 {
90     if( OS_IsRunOnTwl() )
91     {
92         return AESi_Rand(pBuffer, size);
93     }
94     else
95     {
96         return AES_RESULT_ON_DS;
97     }
98 }
99 
100 // dst size = srcSize + 16
AES_Encrypt(const void * src,u32 srcSize,void * dst,AESCallback callback,void * arg)101 SDK_INLINE AESResult AES_Encrypt(
102             const void* src,
103             u32         srcSize,
104             void*       dst,
105             AESCallback callback,
106             void*       arg )
107 {
108     if( OS_IsRunOnTwl() )
109     {
110         return AESi_Encrypt(src, srcSize, dst, callback, arg);
111     }
112     else
113     {
114         return AES_RESULT_ON_DS;
115     }
116 }
117 
118 // dst size = srcSize - 16
AES_Decrypt(const void * src,u32 srcSize,void * dst,AESCallback callback,void * arg)119 SDK_INLINE AESResult AES_Decrypt(
120             const void* src,
121             u32         srcSize,
122             void*       dst,
123             AESCallback callback,
124             void*       arg )
125 {
126     if( OS_IsRunOnTwl() )
127     {
128         return AESi_Decrypt(src, srcSize, dst, callback, arg);
129     }
130     else
131     {
132         return AES_RESULT_ON_DS;
133     }
134 }
135 
136 // dst size = srcSize + 32
AES_EncryptAndSign(const void * src,u32 srcSize,void * dst,AESCallback callback,void * arg)137 SDK_INLINE AESResult AES_EncryptAndSign(
138             const void* src,
139             u32         srcSize,
140             void*       dst,
141             AESCallback callback,
142             void*       arg )
143 {
144     if( OS_IsRunOnTwl() )
145     {
146         return AESi_EncryptAndSign(src, srcSize, dst, callback, arg);
147     }
148     else
149     {
150         return AES_RESULT_ON_DS;
151     }
152 }
153 
154 // dst size = srcSize - 32
AES_DecryptAndVerify(const void * src,u32 srcSize,void * dst,AESCallback callback,void * arg)155 SDK_INLINE AESResult AES_DecryptAndVerify(
156             const void* src,
157             u32         srcSize,
158             void*       dst,
159             AESCallback callback,
160             void*       arg )
161 {
162     if( OS_IsRunOnTwl() )
163     {
164         return AESi_DecryptAndVerify(src, srcSize, dst, callback, arg);
165     }
166     else
167     {
168         return AES_RESULT_ON_DS;
169     }
170 }
171 
172 
173 /*===========================================================================*/
174 
175 #ifdef	__cplusplus
176 }          /* extern "C" */
177 #endif
178 
179 #endif /* TWL_AES_ARM9_UTIL_H_ */
180 
181 /*---------------------------------------------------------------------------*
182   End of file
183  *---------------------------------------------------------------------------*/
184