1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - include
3   File:     crypto/rsa.h
4 
5   Copyright 2003-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::$
14   $Rev:$
15   $Author:$
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_CRYPTO_RSA_H_
19 #define NITRO_CRYPTO_RSA_H_
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 
26 #define CRYPTO_RSA_VERIFY    // Define this to enable RSA signature verification
27 
28 
29 /*---------------------------------------------------------------------------*
30     Constant Definitions
31  *---------------------------------------------------------------------------*/
32 
33 #define CRYPTO_RSA_CONTEXT_SIZE         (4 * 1)
34 #define CRYPTO_RSA_SIGN_CONTEXT_SIZE    (4 * 1)
35 
36 
37 
38 /*---------------------------------------------------------------------------*
39     Type Definitions
40  *---------------------------------------------------------------------------*/
41 
42 // RSA processing context
43 //   The size must match CRYPTORSAContext_local.
44 //   This must reflect any added or removed members (CRYPTO_RSA_CONTEXT_SIZE).
45 typedef struct CRYPTORSAContext
46 {
47 /* private: */
48     u8      mem[CRYPTO_RSA_CONTEXT_SIZE];
49 }
50 CRYPTORSAContext;
51 
52 // I/O stream parameters for CRYPTO_RSA_EncryptInit
53 typedef struct CRYPTORSAEncryptInitParam
54 {
55     void    *key;       // [in] Public key string
56     u32     key_len;    // [in] Public key string length
57 }
58 CRYPTORSAEncryptInitParam;
59 
60 // I/O stream parameters for CRYPTO_RSA_EncryptInit_PrivateKey
61 typedef struct CRYPTORSAEncryptInitPKParam
62 {
63     void    *privkey;       // [in] Private key string
64     u32     privkey_len;    // [in] Private key string length
65 }
66 CRYPTORSAEncryptInitPKParam;
67 
68 // I/O stream parameters for CRYPTO_RSA_Encrypt
69 typedef struct CRYPTORSAEncryptParam
70 {
71     void    *in;        // [in] Encryption string
72     u32     in_len;     // [in] Encryption string length
73     void    *out;       // [out] Output string buffer
74     u32     out_size;   // [in] Output string buffer size
75 }
76 CRYPTORSAEncryptParam;
77 
78 // I/O stream parameters for CRYPTO_RSA_DecryptInit
79 typedef struct CRYPTORSADecryptInitParam
80 {
81     void    *key;       // [in] Public key string
82     u32     key_len;    // [in] Public key string length
83 }
84 CRYPTORSADecryptInitParam;
85 
86 // I/O stream parameters for CRYPTO_RSA_Decrypt
87 typedef struct CRYPTORSADecryptParam
88 {
89     void    *in;        // [in] Decryption string
90     u32     in_len;     // [in] Decryption string length
91     void    *out;       // [out] Output string buffer
92     u32     out_size;   // [in] Output string buffer size
93 }
94 CRYPTORSADecryptParam;
95 
96 // Processing context related to digital RSA signatures
97 //   The size must match CRYPTORSASignContext_local.
98 //   This must reflect any added or removed members (CRYPTO_RSA_SIGN_CONTEXT_SIZE).
99 typedef struct CRYPTORSASignContext
100 {
101 /* private: */
102     u8      mem[CRYPTO_RSA_SIGN_CONTEXT_SIZE];
103 }
104 CRYPTORSASignContext;
105 
106 // I/O stream parameters for CRYPTO_RSA_SignInit
107 typedef struct CRYPTORSASignInitParam
108 {
109     void    *key;       // [in] Private key string
110     u32     key_len;    // [in] Private key string length
111 }
112 CRYPTORSASignInitParam;
113 
114 // I/O stream parameters for CRYPTO_RSA_Sign
115 typedef struct CRYPTORSASignParam
116 {
117     void    *in;        // [in] Target signature string
118     u32     in_len;     // [in] Target signature string length
119     void    *out;       // [out] Buffer for the output signature string
120     u32     out_size;   // [in] Buffer size for the output signature string
121 }
122 CRYPTORSASignParam;
123 
124 #if defined(CRYPTO_RSA_VERIFY)
125 // I/O stream parameters for CRYPTO_RSA_VerifyInt
126 typedef struct CRYPTORSAVerifyInitParam
127 {
128     void    *key;       // [in] Public key string
129     u32     key_len;    // [in] Public key string length
130 }
131 CRYPTORSAVerifyInitParam;
132 
133 // I/O stream parameters for CRYPTO_RSA_Verify
134 typedef struct CRYPTORSAVerifyParam
135 {
136     void    *in;        // [in] String to verify
137     u32     in_len;     // [in] String length to verify
138     void    *sign;      // [in] Signature string
139     u32     sign_len;   // [in] Signature string length
140 }
141 CRYPTORSAVerifyParam;
142 #endif
143 
144 
145 /*---------------------------------------------------------------------------*
146     Constant Structure Declarations
147  *---------------------------------------------------------------------------*/
148 
149 
150 
151 /*---------------------------------------------------------------------------*
152     Function Declarations
153  *---------------------------------------------------------------------------*/
154 
155 /*---------------------------------------------------------------------------*
156   Name:         CRYPTO_RSA_EncryptInit
157 
158   Description:  Runs initialization processing for RSA encryption.
159 
160   Arguments:    context:    Library context
161                 param:      I/O stream parameters
162 
163   Returns:      0 on success and a non-zero value on failure
164  *---------------------------------------------------------------------------*/
165 s32 CRYPTO_RSA_EncryptInit(CRYPTORSAContext *context, CRYPTORSAEncryptInitParam *param);
166 
167 /*---------------------------------------------------------------------------*
168   Name:         CRYPTO_RSA_EncryptInit_PrivateKey
169 
170   Description:  Runs initialization processing (using private keys) for RSA encryption.
171 
172   Arguments:    context:    Library context
173                 param:      I/O stream parameters
174 
175   Returns:      0 on success and a non-zero value on failure
176  *---------------------------------------------------------------------------*/
177 s32 CRYPTO_RSA_EncryptInit_PrivateKey(CRYPTORSAContext *context, CRYPTORSAEncryptInitPKParam *param);
178 
179 /*---------------------------------------------------------------------------*
180   Name:         CRYPTO_RSA_Encrypt
181 
182   Description:  Runs RSA encryption.
183 
184   Arguments:    context:    Library context
185                 param:      I/O stream parameters
186 
187   Returns:      A positive value indicates the string length and -1 indicates failure.
188  *---------------------------------------------------------------------------*/
189 s32 CRYPTO_RSA_Encrypt(CRYPTORSAContext *context, CRYPTORSAEncryptParam *param);
190 
191 /*---------------------------------------------------------------------------*
192   Name:         CRYPTO_RSA_EncryptTerminate
193 
194   Description:  Runs shutdown processing for RSA encryption.
195 
196   Arguments:    context:    Library context
197 
198   Returns:      0 on success and a non-zero value on failure
199  *---------------------------------------------------------------------------*/
200 s32 CRYPTO_RSA_EncryptTerminate(CRYPTORSAContext *context);
201 
202 /*---------------------------------------------------------------------------*
203   Name:         CRYPTO_RSA_DecryptInit
204 
205   Description:  Runs initialization processing for RSA decryption.
206 
207   Arguments:    context:    Library context
208                 param:      I/O stream parameters
209 
210   Returns:      0 on success and a non-zero value on failure
211  *---------------------------------------------------------------------------*/
212 s32 CRYPTO_RSA_DecryptInit(CRYPTORSAContext *context, CRYPTORSADecryptInitParam *param);
213 
214 /*---------------------------------------------------------------------------*
215   Name:         CRYPTO_RSA_Decrypt
216 
217   Description:  Runs RSA decryption.
218 
219   Arguments:    context:    Library context
220                 param:      I/O stream parameters
221 
222   Returns:      A positive value indicates the string length and -1 indicates failure.
223  *---------------------------------------------------------------------------*/
224 s32 CRYPTO_RSA_Decrypt(CRYPTORSAContext *context, CRYPTORSADecryptParam *param);
225 
226 /*---------------------------------------------------------------------------*
227   Name:         CRYPTO_RSA_DecryptTerminate
228 
229   Description:  Runs shutdown processing for RSA decryption.
230 
231   Arguments:    context:    Library context
232 
233   Returns:      0 on success and a non-zero value on failure
234  *---------------------------------------------------------------------------*/
235 s32 CRYPTO_RSA_DecryptTerminate(CRYPTORSAContext *context);
236 
237 
238 /*---------------------------------------------------------------------------*
239   Name:         CRYPTO_RSA_SignInit
240 
241   Description:  Initializes RSA signature processing.
242 
243   Arguments:    context:    Library context
244                 param:      I/O stream parameters
245 
246   Returns:      0 on success and a non-zero value on failure
247  *---------------------------------------------------------------------------*/
248 s32 CRYPTO_RSA_SignInit(CRYPTORSASignContext *context, CRYPTORSASignInitParam *param);
249 
250 /*---------------------------------------------------------------------------*
251   Name:         CRYPTO_RSA_Sign
252 
253   Description:  Generates an RSA signature.
254 
255   Arguments:    context:    Library context
256                 param:      I/O stream parameters
257 
258   Returns:      A positive value indicates the string length of the generated signature and -1 indicates failure.
259  *---------------------------------------------------------------------------*/
260 s32 CRYPTO_RSA_Sign(CRYPTORSASignContext *context, CRYPTORSASignParam *param);
261 
262 /*---------------------------------------------------------------------------*
263   Name:         CRYPTO_RSA_SignTerminate
264 
265   Description:  Shuts down RSA signature processing.
266 
267   Arguments:    context:    Library context
268 
269   Returns:      0 on success and a non-zero value on failure
270  *---------------------------------------------------------------------------*/
271 s32 CRYPTO_RSA_SignTerminate(CRYPTORSASignContext *context);
272 
273 
274 #if defined(CRYPTO_RSA_VERIFY)
275 /*---------------------------------------------------------------------------*
276   Name:         CRYPTO_RSA_VerifyInit
277 
278   Description:  Runs initialization processing for RSA signature verification.
279 
280   Arguments:    context:    Library context
281                 param:      I/O stream parameters
282 
283   Returns:      0 on success and a non-zero value on failure
284  *---------------------------------------------------------------------------*/
285 s32 CRYPTO_RSA_VerifyInit(CRYPTORSASignContext *context, CRYPTORSAVerifyInitParam *param);
286 
287 /*---------------------------------------------------------------------------*
288   Name:         CRYPTO_RSA_Verify
289 
290   Description:  Verifies an RSA signature.
291 
292   Arguments:    context:    Library context
293                 param:      I/O stream parameters
294 
295   Returns:      0 on success and a non-zero value on failure
296  *---------------------------------------------------------------------------*/
297 s32 CRYPTO_RSA_Verify(CRYPTORSASignContext *context, CRYPTORSAVerifyParam *param);
298 
299 /*---------------------------------------------------------------------------*
300   Name:         CRYPTO_RSA_VerifyTerminate
301 
302   Description:  Runs shutdown processing for RSA signature verification.
303 
304   Arguments:    context:    Library context
305 
306   Returns:      0 on success and a non-zero value on failure
307  *---------------------------------------------------------------------------*/
308 s32 CRYPTO_RSA_VerifyTerminate(CRYPTORSASignContext *context);
309 #endif
310 
311 
312 /* for internal use */
313 
314 
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 #endif //NITRO_CRYPTO_RSA_H_
321