1 /*---------------------------------------------------------------------------*
2 Project: RevolutionSDK Extensions - Network base library
3 File: NETDigest.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: NETDigest.h,v $
14 Revision 1.6 2007/10/31 00:09:00 seiki_masashi
15 Changed the types of the arguments of the NETCalcHMAC* functions.
16
17 Revision 1.5 2006/10/17 08:11:07 yosizaki
18 Fixed NETCalcHMAC
19
20 Revision 1.4 2006/10/14 01:44:07 yosizaki
21 Reorganized the SHA1 and HMAC API.
22
23 Revision 1.3 2006/10/11 07:27:04 yosizaki
24 Added support for settings other than RVL_OS.
25
26 Revision 1.2 2006/10/11 04:28:26 yosizaki
27 Added functions for NETHMACContext.
28
29 Revision 1.1 2006/09/08 12:00:22 seiki_masashi
30 Split net.h multiple header files
31
32 $NoKeywords: $
33 *---------------------------------------------------------------------------*/
34
35 /*---------------------------------------------------------------------------*
36 Header file for network base API.
37 *---------------------------------------------------------------------------*/
38
39 #ifndef __NETDIGEST_H__
40 #define __NETDIGEST_H__
41
42 #ifdef RVL_OS
43 #include <revolution/os.h>
44 #endif // RVL_OS
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 /*---------------------------------------------------------------------------*/
50
51 #define NET_MD5_DIGEST_SIZE 16
52 #define NET_SHA1_DIGEST_SIZE 20
53 #define NET_MD5_BLOCK_SIZE 64
54 #define NET_SHA1_BLOCK_SIZE 64UL
55
56 /*---------------------------------------------------------------------------*/
57
58 u16 NETCalcCRC16( const void* datap, u32 size );
59 u32 NETCalcCRC32( const void* datap, u32 size );
60
61 /*---------------------------------------------------------------------------*/
62
NETSwapBytes16(u16 val)63 inline static u16 NETSwapBytes16( u16 val )
64 {
65 return (u16)( (((val) >> 8UL) & 0x00FFUL) |
66 (((val) << 8UL) & 0xFF00UL) );
67 }
68
NETSwapBytes32(u32 val)69 inline static u32 NETSwapBytes32( u32 val )
70 {
71 return (u32)( (((val) >> 24UL) & 0x000000FFUL) |
72 (((val) >> 8UL) & 0x0000FF00UL) |
73 (((val) << 8UL) & 0x00FF0000UL) |
74 (((val) << 24UL) & 0xFF000000UL) );
75 }
76
77 #ifdef RVL_OS
NETReadSwappedBytes16(const u16 * src)78 inline static u16 NETReadSwappedBytes16( const u16* src )
79 {
80 return (u16)__lhbrx((void*)(src), 0);
81 }
82
NETReadSwappedBytes32(const u32 * src)83 inline static u32 NETReadSwappedBytes32( const u32* src )
84 {
85 return (u32)__lwbrx((void*)(src), 0);
86 }
87
NETWriteSwappedBytes16(u16 * dst,u16 val)88 inline static void NETWriteSwappedBytes16( u16* dst, u16 val )
89 {
90 __sthbrx(val, (void*)(dst), 0);
91 }
92
NETWriteSwappedBytes32(u32 * dst,u32 val)93 inline static void NETWriteSwappedBytes32( u32* dst, u32 val )
94 {
95 __stwbrx(val, (void*)(dst), 0);
96 }
97 #else // RVL_OS
NETReadSwappedBytes16(const u16 * src)98 inline static u16 NETReadSwappedBytes16( const u16* src )
99 {
100 return NETSwapBytes16(*src);
101 }
102
NETReadSwappedBytes32(const u32 * src)103 inline static u32 NETReadSwappedBytes32( const u32* src )
104 {
105 return NETSwapBytes32(*src);
106 }
107
NETWriteSwappedBytes16(u16 * dst,u16 val)108 inline static void NETWriteSwappedBytes16( u16* dst, u16 val )
109 {
110 *dst = NETSwapBytes16(val);
111 }
112
NETWriteSwappedBytes32(u32 * dst,u32 val)113 inline static void NETWriteSwappedBytes32( u32* dst, u32 val )
114 {
115 *dst = NETSwapBytes32(val);
116 }
117 #endif // RVL_OS
118
119 /*---------------------------------------------------------------------------*/
120
121 typedef struct NETHashInterface
122 {
123 u32 hashLength;
124 u32 blockLength;
125 u32 workLength;
126 void *context;
127 void (*Init)(void *context);
128 void (*Update)(void *context, const void *message, u32 length);
129 void (*GetDigest)(void *context, void *digest);
130 }
131 NETHashInterface;
132
133 #define NET_HASH_INTERFACE_MD5 NETGetMD5Interface()
134 #define NET_HASH_INTERFACE_SHA1 NETGetSHA1Interface()
135
136 /*---------------------------------------------------------------------------*/
137
138 typedef struct NETMD5Context
139 {
140 union
141 {
142 struct
143 {
144 unsigned long a, b, c, d;
145 };
146 unsigned long state[4];
147 };
148 unsigned long long length;
149 union
150 {
151 unsigned long buffer32[16];
152 unsigned char buffer8[64];
153 };
154 } NETMD5Context;
155
156 void NETMD5Init(NETMD5Context* context);
157 void NETMD5Update(NETMD5Context* context, const void* input, u32 length);
158 void NETMD5GetDigest(NETMD5Context* context, void* digest);
159 const NETHashInterface* NETGetMD5Interface(void);
160
NETCalcMD5(void * digest,const void * input,u32 length)161 inline static void NETCalcMD5(void* digest, const void* input, u32 length)
162 {
163 NETMD5Context context;
164
165 NETMD5Init(&context);
166 NETMD5Update(&context, input, length);
167 NETMD5GetDigest(&context, digest);
168 }
169
170 /*---------------------------------------------------------------------------*/
171
172 typedef struct NETSHA1Context
173 {
174 u32 h[5]; /* H0,H1,H2,H3,H4 */
175 u8 block[NET_SHA1_BLOCK_SIZE]; /* current message block */
176 u32 pool; /* message length in 'block' */
177 u32 blocks_low; /* total blocks (in bytes) */
178 u32 blocks_high;
179 }
180 NETSHA1Context;
181
182 void NETSHA1Init(NETSHA1Context* context);
183 void NETSHA1Update(NETSHA1Context* context, const void* input, u32 length);
184 void NETSHA1Fill(NETSHA1Context* context, u8 input, u32 length);
185 void NETSHA1GetDigest(NETSHA1Context* context, void* digest);
186 const NETHashInterface* NETGetSHA1Interface(void);
187
NETCalcSHA1(void * digest,const void * input,u32 length)188 inline static void NETCalcSHA1(void* digest, const void* input, u32 length)
189 {
190 NETSHA1Context context;
191
192 NETSHA1Init(&context);
193 NETSHA1Update(&context, input, length);
194 NETSHA1GetDigest(&context, digest);
195 }
196
197 /*---------------------------------------------------------------------------*/
198
199 typedef struct NETHMACContext
200 {
201 NETHashInterface hashInterface[1];
202 u32 keyLength;
203 union
204 {
205 NETMD5Context md5;
206 NETSHA1Context sha1;
207 u8 buffer[1];
208 }
209 hashContext;
210 union
211 {
212 u8 md5[NET_MD5_BLOCK_SIZE];
213 u8 sha1[NET_SHA1_BLOCK_SIZE];
214 u8 buffer[1];
215 }
216 key;
217 u8 padding[8];
218 }
219 NETHMACContext;
220
221 void NETHMACInit(NETHMACContext* context,
222 const NETHashInterface *hashInterface,
223 const void *key, u32 keyLength);
224 void NETHMACUpdate(NETHMACContext* context, const void *message, u32 length);
225 void NETHMACGetDigest(NETHMACContext* context, void *digest);
226
NETCalcHMACMD5(void * digest,const void * data,u32 dataLength,const void * key,u32 keyLength)227 inline static void NETCalcHMACMD5(void *digest, const void *data, u32 dataLength, const void *key, u32 keyLength)
228 {
229 NETHMACContext hmac[1];
230 NETHMACInit(hmac, NET_HASH_INTERFACE_MD5, key, keyLength);
231 NETHMACUpdate(hmac, data, dataLength);
232 NETHMACGetDigest(hmac, digest);
233 }
234
NETCalcHMACSHA1(void * digest,const void * data,u32 dataLength,const void * key,u32 keyLength)235 inline static void NETCalcHMACSHA1(void *digest, const void *data, u32 dataLength, const void *key, u32 keyLength)
236 {
237 NETHMACContext hmac[1];
238 NETHMACInit(hmac, NET_HASH_INTERFACE_SHA1, key, keyLength);
239 NETHMACUpdate(hmac, data, dataLength);
240 NETHMACGetDigest(hmac, digest);
241 }
242
243
244 /*---------------------------------------------------------------------------*/
245
246 #ifdef __cplusplus
247 }
248 #endif
249
250 #endif // __NETDIGEST_H__
251
252