1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - include
3   File:     crypto/rc4.h
4 
5   Copyright 2005-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-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_CRYPTO_RC4_H_
19 #define NITRO_CRYPTO_RC4_H_
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 
26 #include <nitro/types.h>
27 
28 
29 typedef struct CRYPTORC4Context
30 {
31     u8 i, j;
32     u8 padd[2];
33     u8 s[256];
34 } CRYPTORC4Context;
35 
36 /*---------------------------------------------------------------------------*
37   Name:         CRYPTO_RC4Init
38 
39   Description:  Performs initializations for encryption/decryption using the RC4 algorithm
40 
41   Arguments:    context - the context structure where the RC4 key information, etc., is kept
42                 key - the key data
43                 key_len - the key length
44 
45   Returns:      None.
46  *---------------------------------------------------------------------------*/
47 void CRYPTO_RC4Init(
48     CRYPTORC4Context* context,
49     const void* key,
50     u32 key_len
51 );
52 
53 /*---------------------------------------------------------------------------*
54   Name:         CRYPTO_RC4Encrypt
55 
56   Description:  Performs encryption/decryption with the RC4 algorithm
57 
58   Arguments:    context - the context structure where the RC4 key information, etc., is to be kept
59                 in      - input data
60                 length  - data length
61                 out     - output data
62 
63   Returns:      None.
64  *---------------------------------------------------------------------------*/
65 void CRYPTO_RC4Encrypt(
66     CRYPTORC4Context* context,
67     const void* in,
68     u32 length,
69     void* out
70 );
71 
72 /*---------------------------------------------------------------------------*
73   Name:         CRYPTO_RC4Decrypt
74 
75   Description:  Performs encryption/decryption with the RC4 algorithm
76 
77   Arguments:    context - the context structure where the RC4 key information, etc., is to be kept
78                 in      - input data
79                 length  - data length
80                 out     - output data
81 
82   Returns:      None.
83  *---------------------------------------------------------------------------*/
CRYPTO_RC4Decrypt(CRYPTORC4Context * context,const void * in,u32 length,void * out)84 static inline void CRYPTO_RC4Decrypt(
85     CRYPTORC4Context* context,
86     const void* in,
87     u32 length,
88     void* out
89 )
90 {
91     CRYPTO_RC4Encrypt(context, in, length, out);
92 }
93 
94 /*---------------------------------------------------------------------------*
95   Name:         CRYPTO_RC4
96 
97   Description:  Performs encryption/decryption with the RC4 algorithm
98 
99   Arguments:    key - the key data
100                 key_len - the key data length
101                 data - the data to be converted (overwritten)
102                 data_len: Data length
103 
104   Returns:      None.
105  *---------------------------------------------------------------------------*/
CRYPTO_RC4(const void * key,u32 key_len,void * data,u32 data_len)106 static inline void CRYPTO_RC4(
107     const void* key,
108     u32 key_len,
109     void* data,
110     u32 data_len
111 )
112 {
113     CRYPTORC4Context context;
114     CRYPTO_RC4Init(&context, key, key_len);
115     CRYPTO_RC4Encrypt(&context, data, data_len, data);
116 }
117 
118 
119 ///////////////////////////////////////////////////////////////////////////////
120 // RC4 Fast
121 ///////////////////////////////////////////////////////////////////////////////
122 
123 // CRYPTORC4FastContext uses roughly 4 times as much memory as CRYPTORC4Context
124 typedef struct CRYPTORC4FastContext
125 {
126     u32 i, j;
127     u32 s[256];
128 } CRYPTORC4FastContext;
129 
130 /*---------------------------------------------------------------------------*
131   Name:         CRYPTO_RC4FastInit
132 
133   Description:  Performs initializations for encryption/decryption using the RC4 algorithm
134 
135   Arguments:    context - the context structure where the RC4 key information, etc., is kept
136                 key - the key data
137                 key_len - the key length
138 
139   Returns:      None.
140  *---------------------------------------------------------------------------*/
141 void CRYPTO_RC4FastInit(
142     CRYPTORC4FastContext* context,
143     const void* key,
144     u32 key_len
145 );
146 
147 /*---------------------------------------------------------------------------*
148   Name:         CRYPTO_RC4FastEncrypt
149 
150   Description:  Performs encryption/decryption with the RC4Fast algorithm
151                 CRYPTO_RC4FastEncrypt uses roughly four times as much memory and processes at roughly 1.5 times the speed as CRYPTO_RC4Crypt
152 
153   Arguments:    context - the context structure where the RC4 key information, etc., is to be kept
154                 in      - input data
155                 length  - data length
156                 out     - output data
157 
158   Returns:      None.
159  *---------------------------------------------------------------------------*/
160 void CRYPTO_RC4FastEncrypt(
161     CRYPTORC4FastContext* context,
162     const void* in,
163     u32 length,
164     void* out
165 );
166 
167 /*---------------------------------------------------------------------------*
168   Name:         CRYPTO_RC4FastDecrypt
169 
170   Description:  Performs encryption/decryption with the RC4Fast algorithm
171                 CRYPTO_RC4FastDecrypt uses roughly four times as much memory and processes at roughly 1.5 times the speed as CRYPTO_RC4Decrypt.
172 
173   Arguments:    context - the context structure where the RC4 key information, etc., is to be kept
174                 in      - input data
175                 length  - data length
176                 out     - output data
177 
178   Returns:      None.
179  *---------------------------------------------------------------------------*/
CRYPTO_RC4FastDecrypt(CRYPTORC4FastContext * context,const void * in,u32 length,void * out)180 static inline void CRYPTO_RC4FastDecrypt(
181     CRYPTORC4FastContext* context,
182     const void* in,
183     u32 length,
184     void* out
185 )
186 {
187     CRYPTO_RC4FastEncrypt(context, in, length, out);
188 }
189 
190 /*---------------------------------------------------------------------------*
191   Name:         CRYPTO_RC4Fast
192 
193   Description:  Performs encryption/decryption with the RC4Fast algorithm
194                 CRYPTO_RC4Fast uses roughly four times as much memory and processes at roughly 1.5 times the speed as CRYPTO_RC4.
195 
196   Arguments:    key - the key data
197                 key_len - the key data length
198                 data - the data to be converted (overwritten)
199                 data_len: Data length
200 
201   Returns:      None.
202  *---------------------------------------------------------------------------*/
CRYPTO_RC4Fast(const void * key,u32 key_len,void * data,u32 data_len)203 static inline void CRYPTO_RC4Fast(
204     const void* key,
205     u32 key_len,
206     void* data,
207     u32 data_len
208 )
209 {
210     CRYPTORC4FastContext context;
211     CRYPTO_RC4FastInit(&context, key, key_len);
212     CRYPTO_RC4FastEncrypt(&context, data, data_len, data);
213 }
214 
215 #ifdef __cplusplus
216 }
217 #endif
218 
219 #endif //_NITRO_CRYPTO_RC4_H_
220