1 /*---------------------------------------------------------------------------*
2   Project:  RevolutionSDK Extensions - Network base library
3   File:     NETCrypto.h
4 
5   Copyright (C)2006 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   $Log: NETCrypto.h,v $
14   Revision 1.6  2006/12/11 07:15:31  yosizaki
15   fixed around definition of NETAESBlockMode.
16 
17   Revision 1.5  2006/12/11 04:17:48  yosizaki
18   added NETAESBlockMode.
19 
20   Revision 1.4  2006/10/27 07:59:55  yosizaki
21   changed AES context.
22 
23   Revision 1.3  2006/10/14 00:06:42  yosizaki
24   added paddding for u64 in SHA1Context.
25 
26   Revision 1.2  2006/10/12 03:39:10  yosizaki
27   changed prototypes of ECC.
28 
29   Revision 1.1  2006/10/11 10:06:02  yosizaki
30   initial upload.
31 
32   $NoKeywords: $
33  *---------------------------------------------------------------------------*/
34 
35 /*---------------------------------------------------------------------------*
36     Header file for network base API.
37  *---------------------------------------------------------------------------*/
38 
39 #ifndef __NETCRYPTO_H__
40 #define __NETCRYPTO_H__
41 
42 #ifdef RVL_OS
43 #include <revolution/os.h>
44 #else
45 #include <rvl/iosc.h>
46 #endif // RVL_OS
47 
48 #include <revolution/net/NETDigest.h>
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 /*---------------------------------------------------------------------------*/
54 
55 
56 /*---------------------------------------------------------------------------*/
57 /* crypto functions */
58 
59 BOOL NETLockCrypto(void);
60 void NETUnlockCrypto(void);
61 BOOL NETIsCryptoAvailable(void);
62 
63 
64 /*---------------------------------------------------------------------------*/
65 /* AES encrypt / decrypt functions */
66 
67 struct NETAESContext;
68 struct NETAESBlockModeInterface;
69 
70 #define NET_AES_128_ROUND_MAX   (u32)((128 / 32) + 6)
71 #define NET_AES_192_ROUND_MAX   (u32)((192 / 32) + 6)
72 #define NET_AES_256_ROUND_MAX   (u32)((256 / 32) + 6)
73 
74 #define NET_AES_BLOCK_LENGTH    16UL
75 #define NET_AES_BLOCK_WORDS     (u32)(NET_AES_BLOCK_LENGTH / sizeof(u32))
76 
77 
78 typedef struct NETAESBlockModeInterface
79 {
80     void (*Encrypt)(struct NETAESContext *context, void *dst, const void *src);
81     void (*Decrypt)(struct NETAESContext *context, void *dst, const void *src);
82 }
83 NETAESBlockModeInterface;
84 typedef const NETAESBlockModeInterface *NETAESBlockMode;
85 
86 extern const NETAESBlockModeInterface NET_AES_BLOCK_MODE_CBC[1];
87 extern const NETAESBlockModeInterface NET_AES_BLOCK_MODE_OFB[1];
88 
89 
90 typedef struct NETAESContext
91 {
92 /* private: */
93     u32     iv[NET_AES_BLOCK_WORDS];
94     u32     roundkey[NET_AES_BLOCK_WORDS * (NET_AES_256_ROUND_MAX + 1)];
95     u32     round:8;
96     u32     encrypt:1;
97     u32     flags:23;
98     const NETAESBlockModeInterface *mode;
99 }
100 NETAESContext;
101 
102 
103 BOOL NETAESCreate(NETAESContext *context, const void *key, u32 keylen, const void *iv);
104 BOOL NETAESCreateEx(NETAESContext *context, const void *key, u32 keylen,
105                     const void *iv, NETAESBlockMode mode);
106 void NETAESDelete(NETAESContext *context);
107 BOOL NETAESEncrypt(NETAESContext *context, void *dst, const void *src, u32 len);
108 BOOL NETAESDecrypt(NETAESContext *context, void *dst, const void *src, u32 len);
109 
110 
111 /* for internal use */
112 void AESiEncryptBlock(NETAESContext *context, u32 *dst, const u32 *src);
113 void AESiDecryptBlock(NETAESContext *context, u32 *dst, const u32 *src);
114 
115 
116 /*---------------------------------------------------------------------------*/
117 /* ECC sign / verify functions */
118 
119 typedef struct NETECCSignInfo
120 {
121 /* private: */
122     u8 cert[384];
123     u8 signature[60];
124 }
125 NETECCSignInfo;
126 
127 BOOL NETCreateSign(const void *buffer, u32 length, NETECCSignInfo *sign);
128 BOOL NETVerifySign(const void *buffer, u32 length, const NETECCSignInfo *sign);
129 
130 typedef struct NETECCContext
131 {
132 /* private: */
133     NETSHA1Context      sha1[1];
134 }
135 NETECCContext;
136 
137 BOOL NETECCCreate(NETECCContext *context);
138 void NETECCDelete(NETECCContext *context);
139 BOOL NETECCUpdate(NETECCContext *context, const void *src, u32 len);
140 BOOL NETECCGetSign(NETECCContext *context, NETECCSignInfo *sign);
141 BOOL NETECCVerifySign(NETECCContext *context, const NETECCSignInfo *sign);
142 
143 
144 /*---------------------------------------------------------------------------*/
145 /* RSA verify functions */
146 
147 #ifndef RVL_OS
148 
149 typedef struct NETRSAContext
150 {
151 /* private: */
152     IOSCPublicKeyHandle handle;
153     u8                  padding[4];
154     NETSHA1Context      sha1[1];
155 }
156 NETRSAContext;
157 
158 IOSError NETRSACreate(NETRSAContext *context, const void *publickey, int exponent);
159 void     NETRSADelete(NETRSAContext *context);
160 void     NETRSAUpdate(NETRSAContext *context, const void *message, u32 length);
161 IOSError NETRSAVerify(NETRSAContext *context, const void *sign);
162 
163 #endif /* RVL_OS */
164 
165 
166 /*---------------------------------------------------------------------------*/
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif // __NETCRYPTO_H__
173