xref: /AOO41X/main/sal/rtl/source/cipher.c (revision 647f063d49501903f1667b75f5634541fc603283)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #define _RTL_CIPHER_C_ "$Revision: 1.5 $"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <sal/types.h>
27cdf0e10cSrcweir #include <rtl/alloc.h>
28cdf0e10cSrcweir #include <rtl/memory.h>
29cdf0e10cSrcweir #include <rtl/cipher.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /*========================================================================
32cdf0e10cSrcweir  *
33cdf0e10cSrcweir  * rtlCipher internals.
34cdf0e10cSrcweir  *
35cdf0e10cSrcweir  *======================================================================*/
36cdf0e10cSrcweir #define RTL_CIPHER_NTOHL(c, l) \
37cdf0e10cSrcweir 	((l)  = ((sal_uInt32)(*((c)++))) << 24L, \
38cdf0e10cSrcweir 	 (l) |= ((sal_uInt32)(*((c)++))) << 16L, \
39cdf0e10cSrcweir 	 (l) |= ((sal_uInt32)(*((c)++))) <<  8L, \
40cdf0e10cSrcweir 	 (l) |= ((sal_uInt32)(*((c)++))))
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #define RTL_CIPHER_HTONL(l, c) \
43cdf0e10cSrcweir 	(*((c)++) = (sal_uInt8)(((l) >> 24L) & 0xff), \
44cdf0e10cSrcweir 	 *((c)++) = (sal_uInt8)(((l) >> 16L) & 0xff), \
45cdf0e10cSrcweir 	 *((c)++) = (sal_uInt8)(((l) >>  8L) & 0xff), \
46cdf0e10cSrcweir 	 *((c)++) = (sal_uInt8)(((l)       ) & 0xff))
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #define RTL_CIPHER_NTOHL64(c, xl, xr, n) \
49cdf0e10cSrcweir { \
50cdf0e10cSrcweir 	(xl) = (xr) = 0; \
51cdf0e10cSrcweir 	(c) += (n);  \
52cdf0e10cSrcweir 	switch ((n)) \
53cdf0e10cSrcweir 	{ \
54cdf0e10cSrcweir 		case 8: (xr)  = ((sal_uInt32)(*(--(c)))); \
55cdf0e10cSrcweir 		case 7: (xr) |= ((sal_uInt32)(*(--(c)))) <<  8L; \
56cdf0e10cSrcweir 		case 6: (xr) |= ((sal_uInt32)(*(--(c)))) << 16L; \
57cdf0e10cSrcweir 		case 5: (xr) |= ((sal_uInt32)(*(--(c)))) << 24L; \
58cdf0e10cSrcweir 		case 4: (xl)  = ((sal_uInt32)(*(--(c)))); \
59cdf0e10cSrcweir 		case 3: (xl) |= ((sal_uInt32)(*(--(c)))) <<  8L; \
60cdf0e10cSrcweir 		case 2: (xl) |= ((sal_uInt32)(*(--(c)))) << 16L; \
61cdf0e10cSrcweir 		case 1: (xl) |= ((sal_uInt32)(*(--(c)))) << 24L; \
62cdf0e10cSrcweir 	} \
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #define RTL_CIPHER_HTONL64(xl, xr, c, n) \
66cdf0e10cSrcweir { \
67cdf0e10cSrcweir 	(c) += (n);  \
68cdf0e10cSrcweir 	switch ((n)) \
69cdf0e10cSrcweir 	{ \
70cdf0e10cSrcweir 		case 8: *(--(c)) = (sal_uInt8)(((xr)       ) & 0xff); \
71cdf0e10cSrcweir 		case 7: *(--(c)) = (sal_uInt8)(((xr) >>  8L) & 0xff); \
72cdf0e10cSrcweir 		case 6: *(--(c)) = (sal_uInt8)(((xr) >> 16L) & 0xff); \
73cdf0e10cSrcweir 		case 5: *(--(c)) = (sal_uInt8)(((xr) >> 24L) & 0xff); \
74cdf0e10cSrcweir 		case 4: *(--(c)) = (sal_uInt8)(((xl)       ) & 0xff); \
75cdf0e10cSrcweir 		case 3: *(--(c)) = (sal_uInt8)(((xl) >>  8L) & 0xff); \
76cdf0e10cSrcweir 		case 2: *(--(c)) = (sal_uInt8)(((xl) >> 16L) & 0xff); \
77cdf0e10cSrcweir 		case 1: *(--(c)) = (sal_uInt8)(((xl) >> 24L) & 0xff); \
78cdf0e10cSrcweir 	} \
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir typedef rtlCipherError (SAL_CALL cipher_init_t) (
82cdf0e10cSrcweir 	rtlCipher          Cipher,
83cdf0e10cSrcweir 	rtlCipherDirection Direction,
84cdf0e10cSrcweir 	const sal_uInt8 *pKeyData, sal_Size nKeyLen,
85cdf0e10cSrcweir 	const sal_uInt8 *pArgData, sal_Size nArgLen);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir typedef rtlCipherError (SAL_CALL cipher_update_t) (
88cdf0e10cSrcweir 	rtlCipher   Cipher,
89cdf0e10cSrcweir 	const void *pData,   sal_Size nDatLen,
90cdf0e10cSrcweir 	sal_uInt8  *pBuffer, sal_Size nBufLen);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir typedef void (SAL_CALL cipher_delete_t) (rtlCipher Cipher);
93cdf0e10cSrcweir 
94cdf0e10cSrcweir /** Cipher_Impl.
95cdf0e10cSrcweir  */
96cdf0e10cSrcweir typedef struct cipher_impl_st
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	rtlCipherAlgorithm  m_algorithm;
99cdf0e10cSrcweir 	rtlCipherDirection  m_direction;
100cdf0e10cSrcweir 	rtlCipherMode       m_mode;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	cipher_init_t      *m_init;
103cdf0e10cSrcweir 	cipher_update_t    *m_encode;
104cdf0e10cSrcweir 	cipher_update_t    *m_decode;
105cdf0e10cSrcweir 	cipher_delete_t    *m_delete;
106cdf0e10cSrcweir } Cipher_Impl;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir /*========================================================================
109cdf0e10cSrcweir  *
110cdf0e10cSrcweir  * rtlCipher implementation.
111cdf0e10cSrcweir  *
112cdf0e10cSrcweir  *======================================================================*/
113cdf0e10cSrcweir /*
114cdf0e10cSrcweir  * rtl_cipher_create.
115cdf0e10cSrcweir  */
rtl_cipher_create(rtlCipherAlgorithm Algorithm,rtlCipherMode Mode)116cdf0e10cSrcweir rtlCipher SAL_CALL rtl_cipher_create (
117cdf0e10cSrcweir 	rtlCipherAlgorithm Algorithm,
118cdf0e10cSrcweir 	rtlCipherMode      Mode)
119cdf0e10cSrcweir {
120cdf0e10cSrcweir 	rtlCipher Cipher = (rtlCipher)NULL;
121cdf0e10cSrcweir 	switch (Algorithm)
122cdf0e10cSrcweir 	{
123cdf0e10cSrcweir 		case rtl_Cipher_AlgorithmBF:
124cdf0e10cSrcweir 			Cipher = rtl_cipher_createBF (Mode);
125cdf0e10cSrcweir 			break;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 		case rtl_Cipher_AlgorithmARCFOUR:
128cdf0e10cSrcweir 			Cipher = rtl_cipher_createARCFOUR (Mode);
129cdf0e10cSrcweir 			break;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 		default: /* rtl_Cipher_AlgorithmInvalid */
132cdf0e10cSrcweir 			break;
133cdf0e10cSrcweir 	}
134cdf0e10cSrcweir 	return Cipher;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir /*
138cdf0e10cSrcweir  * rtl_cipher_init.
139cdf0e10cSrcweir  */
rtl_cipher_init(rtlCipher Cipher,rtlCipherDirection Direction,const sal_uInt8 * pKeyData,sal_Size nKeyLen,const sal_uInt8 * pArgData,sal_Size nArgLen)140cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_init (
141cdf0e10cSrcweir 	rtlCipher          Cipher,
142cdf0e10cSrcweir 	rtlCipherDirection Direction,
143cdf0e10cSrcweir 	const sal_uInt8 *pKeyData, sal_Size nKeyLen,
144cdf0e10cSrcweir 	const sal_uInt8 *pArgData, sal_Size nArgLen)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	Cipher_Impl *pImpl = (Cipher_Impl*)Cipher;
147cdf0e10cSrcweir 	if (pImpl == NULL)
148cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
149cdf0e10cSrcweir 	if (pImpl->m_init == NULL)
150cdf0e10cSrcweir 		return rtl_Cipher_E_Unknown;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	return (pImpl->m_init)(
153cdf0e10cSrcweir 		Cipher, Direction, pKeyData, nKeyLen, pArgData, nArgLen);
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir /*
157cdf0e10cSrcweir  * rtl_cipher_encode.
158cdf0e10cSrcweir  */
rtl_cipher_encode(rtlCipher Cipher,const void * pData,sal_Size nDatLen,sal_uInt8 * pBuffer,sal_Size nBufLen)159cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_encode (
160cdf0e10cSrcweir 	rtlCipher   Cipher,
161cdf0e10cSrcweir 	const void *pData,   sal_Size nDatLen,
162cdf0e10cSrcweir 	sal_uInt8  *pBuffer, sal_Size nBufLen)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir 	Cipher_Impl *pImpl = (Cipher_Impl*)Cipher;
165cdf0e10cSrcweir 	if (pImpl == NULL)
166cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
167cdf0e10cSrcweir 	if (pImpl->m_encode == NULL)
168cdf0e10cSrcweir 		return rtl_Cipher_E_Unknown;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 	return (pImpl->m_encode)(Cipher, pData, nDatLen, pBuffer, nBufLen);
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir /*
174cdf0e10cSrcweir  * rtl_cipher_decode.
175cdf0e10cSrcweir  */
rtl_cipher_decode(rtlCipher Cipher,const void * pData,sal_Size nDatLen,sal_uInt8 * pBuffer,sal_Size nBufLen)176cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_decode (
177cdf0e10cSrcweir 	rtlCipher   Cipher,
178cdf0e10cSrcweir 	const void *pData,   sal_Size nDatLen,
179cdf0e10cSrcweir 	sal_uInt8  *pBuffer, sal_Size nBufLen)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir 	Cipher_Impl *pImpl = (Cipher_Impl*)Cipher;
182cdf0e10cSrcweir 	if (pImpl == NULL)
183cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
184cdf0e10cSrcweir 	if (pImpl->m_decode == NULL)
185cdf0e10cSrcweir 		return rtl_Cipher_E_Unknown;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	return (pImpl->m_decode)(Cipher, pData, nDatLen, pBuffer, nBufLen);
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir /*
191cdf0e10cSrcweir  * rtl_cipher_destroy.
192cdf0e10cSrcweir  */
rtl_cipher_destroy(rtlCipher Cipher)193cdf0e10cSrcweir void SAL_CALL rtl_cipher_destroy (rtlCipher Cipher)
194cdf0e10cSrcweir {
195cdf0e10cSrcweir 	Cipher_Impl *pImpl = (Cipher_Impl*)Cipher;
196cdf0e10cSrcweir 	if (pImpl && pImpl->m_delete)
197cdf0e10cSrcweir 		pImpl->m_delete (Cipher);
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir /*========================================================================
201cdf0e10cSrcweir  *
202cdf0e10cSrcweir  * rtl_cipherBF (Blowfish) internals.
203cdf0e10cSrcweir  *
204cdf0e10cSrcweir  *======================================================================*/
205cdf0e10cSrcweir #define CIPHER_ROUNDS_BF 16
206cdf0e10cSrcweir 
207cdf0e10cSrcweir typedef struct cipherBF_key_st
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	sal_uInt32 m_S[4][256];
210cdf0e10cSrcweir 	sal_uInt32 m_P[CIPHER_ROUNDS_BF + 2];
211cdf0e10cSrcweir } CipherKeyBF;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir typedef struct cipherBF_context_st
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	CipherKeyBF    m_key;
216cdf0e10cSrcweir 	union
217cdf0e10cSrcweir 	{
218cdf0e10cSrcweir 		sal_uInt32 m_long[2];
219cdf0e10cSrcweir 		sal_uInt8  m_byte[8];
220cdf0e10cSrcweir 	} m_iv;
221cdf0e10cSrcweir 	sal_uInt32     m_offset;
222cdf0e10cSrcweir } CipherContextBF;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir typedef struct cipherBF_impl_st
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	Cipher_Impl     m_cipher;
227cdf0e10cSrcweir 	CipherContextBF m_context;
228cdf0e10cSrcweir } CipherBF_Impl;
229cdf0e10cSrcweir 
230cdf0e10cSrcweir /** __rtl_cipherBF_init.
231cdf0e10cSrcweir  */
232cdf0e10cSrcweir static rtlCipherError __rtl_cipherBF_init (
233cdf0e10cSrcweir 	CipherContextBF *ctx,
234cdf0e10cSrcweir 	rtlCipherMode    eMode,
235cdf0e10cSrcweir 	const sal_uInt8 *pKeyData, sal_Size nKeyLen,
236cdf0e10cSrcweir 	const sal_uInt8 *pArgData, sal_Size nArgLen);
237cdf0e10cSrcweir 
238cdf0e10cSrcweir /** __rtl_cipherBF_update.
239cdf0e10cSrcweir  */
240cdf0e10cSrcweir static rtlCipherError __rtl_cipherBF_update (
241cdf0e10cSrcweir 	CipherContextBF    *ctx,
242cdf0e10cSrcweir 	rtlCipherMode       eMode,
243cdf0e10cSrcweir 	rtlCipherDirection  eDirection,
244cdf0e10cSrcweir 	const sal_uInt8    *pData,   sal_Size nDatLen,
245cdf0e10cSrcweir 	sal_uInt8          *pBuffer, sal_Size nBufLen);
246cdf0e10cSrcweir 
247cdf0e10cSrcweir /** __rtl_cipherBF_updateECB.
248cdf0e10cSrcweir  */
249cdf0e10cSrcweir static void __rtl_cipherBF_updateECB (
250cdf0e10cSrcweir 	CipherContextBF    *ctx,
251cdf0e10cSrcweir 	rtlCipherDirection  direction,
252cdf0e10cSrcweir 	const sal_uInt8    *pData,
253cdf0e10cSrcweir 	sal_uInt8          *pBuffer,
254cdf0e10cSrcweir 	sal_Size            nLength);
255cdf0e10cSrcweir 
256cdf0e10cSrcweir /** __rtl_cipherBF_updateCBC.
257cdf0e10cSrcweir  */
258cdf0e10cSrcweir static void __rtl_cipherBF_updateCBC (
259cdf0e10cSrcweir 	CipherContextBF    *ctx,
260cdf0e10cSrcweir 	rtlCipherDirection  direction,
261cdf0e10cSrcweir 	const sal_uInt8    *pData,
262cdf0e10cSrcweir 	sal_uInt8          *pBuffer,
263cdf0e10cSrcweir 	sal_Size          nLength);
264cdf0e10cSrcweir 
265cdf0e10cSrcweir /** __rtl_cipherBF_updateCFB.
266cdf0e10cSrcweir  */
267cdf0e10cSrcweir static void __rtl_cipherBF_updateCFB (
268cdf0e10cSrcweir 	CipherContextBF    *ctx,
269cdf0e10cSrcweir 	rtlCipherDirection  direction,
270cdf0e10cSrcweir 	const sal_uInt8    *pData,
271cdf0e10cSrcweir 	sal_uInt8          *pBuffer);
272cdf0e10cSrcweir 
273cdf0e10cSrcweir /** __rtl_cipher_encode.
274cdf0e10cSrcweir  */
275cdf0e10cSrcweir static void __rtl_cipherBF_encode (
276cdf0e10cSrcweir 	CipherKeyBF *key, sal_uInt32 *xl, sal_uInt32 *xr);
277cdf0e10cSrcweir 
278cdf0e10cSrcweir /** __rtl_cipherBF_decode.
279cdf0e10cSrcweir  */
280cdf0e10cSrcweir static void __rtl_cipherBF_decode (
281cdf0e10cSrcweir 	CipherKeyBF *key, sal_uInt32 *xl, sal_uInt32 *xr);
282cdf0e10cSrcweir 
283cdf0e10cSrcweir /** __rtl_cipherBF.
284cdf0e10cSrcweir  */
285cdf0e10cSrcweir static sal_uInt32 __rtl_cipherBF (
286cdf0e10cSrcweir 	CipherKeyBF *key, sal_uInt32 x);
287cdf0e10cSrcweir 
288cdf0e10cSrcweir /** __rtl_cipherBF_key.
289cdf0e10cSrcweir  */
290cdf0e10cSrcweir static const CipherKeyBF __rtl_cipherBF_key =
291cdf0e10cSrcweir {
292cdf0e10cSrcweir 	/* S */
293cdf0e10cSrcweir 	{
294cdf0e10cSrcweir 		/* S[0] */
295cdf0e10cSrcweir 		{
296cdf0e10cSrcweir 			/* 0 */
297cdf0e10cSrcweir 			0xD1310BA6L, 0x98DFB5ACL, 0x2FFD72DBL, 0xD01ADFB7L,
298cdf0e10cSrcweir 			0xB8E1AFEDL, 0x6A267E96L, 0xBA7C9045L, 0xF12C7F99L,
299cdf0e10cSrcweir 			0x24A19947L, 0xB3916CF7L, 0x0801F2E2L, 0x858EFC16L,
300cdf0e10cSrcweir 			0x636920D8L, 0x71574E69L, 0xA458FEA3L, 0xF4933D7EL,
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 			/* 1 */
303cdf0e10cSrcweir 			0x0D95748FL, 0x728EB658L, 0x718BCD58L, 0x82154AEEL,
304cdf0e10cSrcweir 			0x7B54A41DL, 0xC25A59B5L, 0x9C30D539L, 0x2AF26013L,
305cdf0e10cSrcweir 			0xC5D1B023L, 0x286085F0L, 0xCA417918L, 0xB8DB38EFL,
306cdf0e10cSrcweir 			0x8E79DCB0L, 0x603A180EL, 0x6C9E0E8BL, 0xB01E8A3EL,
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 			/* 2 */
309cdf0e10cSrcweir 			0xD71577C1L, 0xBD314B27L, 0x78AF2FDAL, 0x55605C60L,
310cdf0e10cSrcweir 			0xE65525F3L, 0xAA55AB94L, 0x57489862L, 0x63E81440L,
311cdf0e10cSrcweir 			0x55CA396AL, 0x2AAB10B6L, 0xB4CC5C34L, 0x1141E8CEL,
312cdf0e10cSrcweir 			0xA15486AFL, 0x7C72E993L, 0xB3EE1411L, 0x636FBC2AL,
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 			/* 3 */
315cdf0e10cSrcweir 			0x2BA9C55DL, 0x741831F6L, 0xCE5C3E16L, 0x9B87931EL,
316cdf0e10cSrcweir 			0xAFD6BA33L, 0x6C24CF5CL, 0x7A325381L, 0x28958677L,
317cdf0e10cSrcweir 			0x3B8F4898L, 0x6B4BB9AFL, 0xC4BFE81BL, 0x66282193L,
318cdf0e10cSrcweir 			0x61D809CCL, 0xFB21A991L, 0x487CAC60L, 0x5DEC8032L,
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 			/* 4 */
321cdf0e10cSrcweir 			0xEF845D5DL, 0xE98575B1L, 0xDC262302L, 0xEB651B88L,
322cdf0e10cSrcweir 			0x23893E81L, 0xD396ACC5L, 0x0F6D6FF3L, 0x83F44239L,
323cdf0e10cSrcweir 			0x2E0B4482L, 0xA4842004L, 0x69C8F04AL, 0x9E1F9B5EL,
324cdf0e10cSrcweir 			0x21C66842L, 0xF6E96C9AL, 0x670C9C61L, 0xABD388F0L,
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 			/* 5 */
327cdf0e10cSrcweir 			0x6A51A0D2L, 0xD8542F68L, 0x960FA728L, 0xAB5133A3L,
328cdf0e10cSrcweir 			0x6EEF0B6CL, 0x137A3BE4L, 0xBA3BF050L, 0x7EFB2A98L,
329cdf0e10cSrcweir 			0xA1F1651DL, 0x39AF0176L, 0x66CA593EL, 0x82430E88L,
330cdf0e10cSrcweir 			0x8CEE8619L, 0x456F9FB4L, 0x7D84A5C3L, 0x3B8B5EBEL,
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 			/* 6 */
333cdf0e10cSrcweir 			0xE06F75D8L, 0x85C12073L, 0x401A449FL, 0x56C16AA6L,
334cdf0e10cSrcweir 			0x4ED3AA62L, 0x363F7706L, 0x1BFEDF72L, 0x429B023DL,
335cdf0e10cSrcweir 			0x37D0D724L, 0xD00A1248L, 0xDB0FEAD3L, 0x49F1C09BL,
336cdf0e10cSrcweir 			0x075372C9L, 0x80991B7BL, 0x25D479D8L, 0xF6E8DEF7L,
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 			/* 7 */
339cdf0e10cSrcweir 			0xE3FE501AL, 0xB6794C3BL, 0x976CE0BDL, 0x04C006BAL,
340cdf0e10cSrcweir 			0xC1A94FB6L, 0x409F60C4L, 0x5E5C9EC2L, 0x196A2463L,
341cdf0e10cSrcweir 			0x68FB6FAFL, 0x3E6C53B5L, 0x1339B2EBL, 0x3B52EC6FL,
342cdf0e10cSrcweir 			0x6DFC511FL, 0x9B30952CL, 0xCC814544L, 0xAF5EBD09L,
343cdf0e10cSrcweir 
344cdf0e10cSrcweir 			/* 8 */
345cdf0e10cSrcweir 			0xBEE3D004L, 0xDE334AFDL, 0x660F2807L, 0x192E4BB3L,
346cdf0e10cSrcweir 			0xC0CBA857L, 0x45C8740FL, 0xD20B5F39L, 0xB9D3FBDBL,
347cdf0e10cSrcweir 			0x5579C0BDL, 0x1A60320AL, 0xD6A100C6L, 0x402C7279L,
348cdf0e10cSrcweir 			0x679F25FEL, 0xFB1FA3CCL, 0x8EA5E9F8L, 0xDB3222F8L,
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 			/* 9 */
351cdf0e10cSrcweir 			0x3C7516DFL, 0xFD616B15L, 0x2F501EC8L, 0xAD0552ABL,
352cdf0e10cSrcweir 			0x323DB5FAL, 0xFD238760L, 0x53317B48L, 0x3E00DF82L,
353cdf0e10cSrcweir 			0x9E5C57BBL, 0xCA6F8CA0L, 0x1A87562EL, 0xDF1769DBL,
354cdf0e10cSrcweir 			0xD542A8F6L, 0x287EFFC3L, 0xAC6732C6L, 0x8C4F5573L,
355cdf0e10cSrcweir 
356cdf0e10cSrcweir 			/* A */
357cdf0e10cSrcweir 			0x695B27B0L, 0xBBCA58C8L, 0xE1FFA35DL, 0xB8F011A0L,
358cdf0e10cSrcweir 			0x10FA3D98L, 0xFD2183B8L, 0x4AFCB56CL, 0x2DD1D35BL,
359cdf0e10cSrcweir 			0x9A53E479L, 0xB6F84565L, 0xD28E49BCL, 0x4BFB9790L,
360cdf0e10cSrcweir 			0xE1DDF2DAL, 0xA4CB7E33L, 0x62FB1341L, 0xCEE4C6E8L,
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 			/* B */
363cdf0e10cSrcweir 			0xEF20CADAL, 0x36774C01L, 0xD07E9EFEL, 0x2BF11FB4L,
364cdf0e10cSrcweir 			0x95DBDA4DL, 0xAE909198L, 0xEAAD8E71L, 0x6B93D5A0L,
365cdf0e10cSrcweir 			0xD08ED1D0L, 0xAFC725E0L, 0x8E3C5B2FL, 0x8E7594B7L,
366cdf0e10cSrcweir 			0x8FF6E2FBL, 0xF2122B64L, 0x8888B812L, 0x900DF01CL,
367cdf0e10cSrcweir 
368cdf0e10cSrcweir 			/* C */
369cdf0e10cSrcweir 			0x4FAD5EA0L, 0x688FC31CL, 0xD1CFF191L, 0xB3A8C1ADL,
370cdf0e10cSrcweir 			0x2F2F2218L, 0xBE0E1777L, 0xEA752DFEL, 0x8B021FA1L,
371cdf0e10cSrcweir 			0xE5A0CC0FL, 0xB56F74E8L, 0x18ACF3D6L, 0xCE89E299L,
372cdf0e10cSrcweir 			0xB4A84FE0L, 0xFD13E0B7L, 0x7CC43B81L, 0xD2ADA8D9L,
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 			/* D */
375cdf0e10cSrcweir 			0x165FA266L, 0x80957705L, 0x93CC7314L, 0x211A1477L,
376cdf0e10cSrcweir 			0xE6AD2065L, 0x77B5FA86L, 0xC75442F5L, 0xFB9D35CFL,
377cdf0e10cSrcweir 			0xEBCDAF0CL, 0x7B3E89A0L, 0xD6411BD3L, 0xAE1E7E49L,
378cdf0e10cSrcweir 			0x00250E2DL, 0x2071B35EL, 0x226800BBL, 0x57B8E0AFL,
379cdf0e10cSrcweir 
380cdf0e10cSrcweir 			/* E */
381cdf0e10cSrcweir 			0x2464369BL, 0xF009B91EL, 0x5563911DL, 0x59DFA6AAL,
382cdf0e10cSrcweir 			0x78C14389L, 0xD95A537FL, 0x207D5BA2L, 0x02E5B9C5L,
383cdf0e10cSrcweir 			0x83260376L, 0x6295CFA9L, 0x11C81968L, 0x4E734A41L,
384cdf0e10cSrcweir 			0xB3472DCAL, 0x7B14A94AL, 0x1B510052L, 0x9A532915L,
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 			/* F */
387cdf0e10cSrcweir 			0xD60F573FL, 0xBC9BC6E4L, 0x2B60A476L, 0x81E67400L,
388cdf0e10cSrcweir 			0x08BA6FB5L, 0x571BE91FL, 0xF296EC6BL, 0x2A0DD915L,
389cdf0e10cSrcweir 			0xB6636521L, 0xE7B9F9B6L, 0xFF34052EL, 0xC5855664L,
390cdf0e10cSrcweir 			0x53B02D5DL, 0xA99F8FA1L, 0x08BA4799L, 0x6E85076AL
391cdf0e10cSrcweir 		},
392cdf0e10cSrcweir 
393cdf0e10cSrcweir 		/* S[1] */
394cdf0e10cSrcweir 		{
395cdf0e10cSrcweir 			0x4B7A70E9L, 0xB5B32944L, 0xDB75092EL, 0xC4192623L,
396cdf0e10cSrcweir 			0xAD6EA6B0L, 0x49A7DF7DL, 0x9CEE60B8L, 0x8FEDB266L,
397cdf0e10cSrcweir 			0xECAA8C71L, 0x699A17FFL, 0x5664526CL, 0xC2B19EE1L,
398cdf0e10cSrcweir 			0x193602A5L, 0x75094C29L, 0xA0591340L, 0xE4183A3EL,
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 			0x3F54989AL, 0x5B429D65L, 0x6B8FE4D6L, 0x99F73FD6L,
401cdf0e10cSrcweir 			0xA1D29C07L, 0xEFE830F5L, 0x4D2D38E6L, 0xF0255DC1L,
402cdf0e10cSrcweir 			0x4CDD2086L, 0x8470EB26L, 0x6382E9C6L, 0x021ECC5EL,
403cdf0e10cSrcweir 			0x09686B3FL, 0x3EBAEFC9L, 0x3C971814L, 0x6B6A70A1L,
404cdf0e10cSrcweir 
405cdf0e10cSrcweir 			0x687F3584L, 0x52A0E286L, 0xB79C5305L, 0xAA500737L,
406cdf0e10cSrcweir 			0x3E07841CL, 0x7FDEAE5CL, 0x8E7D44ECL, 0x5716F2B8L,
407cdf0e10cSrcweir 			0xB03ADA37L, 0xF0500C0DL, 0xF01C1F04L, 0x0200B3FFL,
408cdf0e10cSrcweir 			0xAE0CF51AL, 0x3CB574B2L, 0x25837A58L, 0xDC0921BDL,
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 			0xD19113F9L, 0x7CA92FF6L, 0x94324773L, 0x22F54701L,
411cdf0e10cSrcweir 			0x3AE5E581L, 0x37C2DADCL, 0xC8B57634L, 0x9AF3DDA7L,
412cdf0e10cSrcweir 			0xA9446146L, 0x0FD0030EL, 0xECC8C73EL, 0xA4751E41L,
413cdf0e10cSrcweir 			0xE238CD99L, 0x3BEA0E2FL, 0x3280BBA1L, 0x183EB331L,
414cdf0e10cSrcweir 
415cdf0e10cSrcweir 			0x4E548B38L, 0x4F6DB908L, 0x6F420D03L, 0xF60A04BFL,
416cdf0e10cSrcweir 			0x2CB81290L, 0x24977C79L, 0x5679B072L, 0xBCAF89AFL,
417cdf0e10cSrcweir 			0xDE9A771FL, 0xD9930810L, 0xB38BAE12L, 0xDCCF3F2EL,
418cdf0e10cSrcweir 			0x5512721FL, 0x2E6B7124L, 0x501ADDE6L, 0x9F84CD87L,
419cdf0e10cSrcweir 
420cdf0e10cSrcweir 			0x7A584718L, 0x7408DA17L, 0xBC9F9ABCL, 0xE94B7D8CL,
421cdf0e10cSrcweir 			0xEC7AEC3AL, 0xDB851DFAL, 0x63094366L, 0xC464C3D2L,
422cdf0e10cSrcweir 			0xEF1C1847L, 0x3215D908L, 0xDD433B37L, 0x24C2BA16L,
423cdf0e10cSrcweir 			0x12A14D43L, 0x2A65C451L, 0x50940002L, 0x133AE4DDL,
424cdf0e10cSrcweir 
425cdf0e10cSrcweir 			0x71DFF89EL, 0x10314E55L, 0x81AC77D6L, 0x5F11199BL,
426cdf0e10cSrcweir 			0x043556F1L, 0xD7A3C76BL, 0x3C11183BL, 0x5924A509L,
427cdf0e10cSrcweir 			0xF28FE6EDL, 0x97F1FBFAL, 0x9EBABF2CL, 0x1E153C6EL,
428cdf0e10cSrcweir 			0x86E34570L, 0xEAE96FB1L, 0x860E5E0AL, 0x5A3E2AB3L,
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 			0x771FE71CL, 0x4E3D06FAL, 0x2965DCB9L, 0x99E71D0FL,
431cdf0e10cSrcweir 			0x803E89D6L, 0x5266C825L, 0x2E4CC978L, 0x9C10B36AL,
432cdf0e10cSrcweir 			0xC6150EBAL, 0x94E2EA78L, 0xA5FC3C53L, 0x1E0A2DF4L,
433cdf0e10cSrcweir 			0xF2F74EA7L, 0x361D2B3DL, 0x1939260FL, 0x19C27960L,
434cdf0e10cSrcweir 
435cdf0e10cSrcweir 			0x5223A708L, 0xF71312B6L, 0xEBADFE6EL, 0xEAC31F66L,
436cdf0e10cSrcweir 			0xE3BC4595L, 0xA67BC883L, 0xB17F37D1L, 0x018CFF28L,
437cdf0e10cSrcweir 			0xC332DDEFL, 0xBE6C5AA5L, 0x65582185L, 0x68AB9802L,
438cdf0e10cSrcweir 			0xEECEA50FL, 0xDB2F953BL, 0x2AEF7DADL, 0x5B6E2F84L,
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 			0x1521B628L, 0x29076170L, 0xECDD4775L, 0x619F1510L,
441cdf0e10cSrcweir 			0x13CCA830L, 0xEB61BD96L, 0x0334FE1EL, 0xAA0363CFL,
442cdf0e10cSrcweir 			0xB5735C90L, 0x4C70A239L, 0xD59E9E0BL, 0xCBAADE14L,
443cdf0e10cSrcweir 			0xEECC86BCL, 0x60622CA7L, 0x9CAB5CABL, 0xB2F3846EL,
444cdf0e10cSrcweir 
445cdf0e10cSrcweir 			0x648B1EAFL, 0x19BDF0CAL, 0xA02369B9L, 0x655ABB50L,
446cdf0e10cSrcweir 			0x40685A32L, 0x3C2AB4B3L, 0x319EE9D5L, 0xC021B8F7L,
447cdf0e10cSrcweir 			0x9B540B19L, 0x875FA099L, 0x95F7997EL, 0x623D7DA8L,
448cdf0e10cSrcweir 			0xF837889AL, 0x97E32D77L, 0x11ED935FL, 0x16681281L,
449cdf0e10cSrcweir 
450cdf0e10cSrcweir 			0x0E358829L, 0xC7E61FD6L, 0x96DEDFA1L, 0x7858BA99L,
451cdf0e10cSrcweir 			0x57F584A5L, 0x1B227263L, 0x9B83C3FFL, 0x1AC24696L,
452cdf0e10cSrcweir 			0xCDB30AEBL, 0x532E3054L, 0x8FD948E4L, 0x6DBC3128L,
453cdf0e10cSrcweir 			0x58EBF2EFL, 0x34C6FFEAL, 0xFE28ED61L, 0xEE7C3C73L,
454cdf0e10cSrcweir 
455cdf0e10cSrcweir 			0x5D4A14D9L, 0xE864B7E3L, 0x42105D14L, 0x203E13E0L,
456cdf0e10cSrcweir 			0x45EEE2B6L, 0xA3AAABEAL, 0xDB6C4F15L, 0xFACB4FD0L,
457cdf0e10cSrcweir 			0xC742F442L, 0xEF6ABBB5L, 0x654F3B1DL, 0x41CD2105L,
458cdf0e10cSrcweir 			0xD81E799EL, 0x86854DC7L, 0xE44B476AL, 0x3D816250L,
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 			0xCF62A1F2L, 0x5B8D2646L, 0xFC8883A0L, 0xC1C7B6A3L,
461cdf0e10cSrcweir 			0x7F1524C3L, 0x69CB7492L, 0x47848A0BL, 0x5692B285L,
462cdf0e10cSrcweir 			0x095BBF00L, 0xAD19489DL, 0x1462B174L, 0x23820E00L,
463cdf0e10cSrcweir 			0x58428D2AL, 0x0C55F5EAL, 0x1DADF43EL, 0x233F7061L,
464cdf0e10cSrcweir 
465cdf0e10cSrcweir 			0x3372F092L, 0x8D937E41L, 0xD65FECF1L, 0x6C223BDBL,
466cdf0e10cSrcweir 			0x7CDE3759L, 0xCBEE7460L, 0x4085F2A7L, 0xCE77326EL,
467cdf0e10cSrcweir 			0xA6078084L, 0x19F8509EL, 0xE8EFD855L, 0x61D99735L,
468cdf0e10cSrcweir 			0xA969A7AAL, 0xC50C06C2L, 0x5A04ABFCL, 0x800BCADCL,
469cdf0e10cSrcweir 
470cdf0e10cSrcweir 			0x9E447A2EL, 0xC3453484L, 0xFDD56705L, 0x0E1E9EC9L,
471cdf0e10cSrcweir 			0xDB73DBD3L, 0x105588CDL, 0x675FDA79L, 0xE3674340L,
472cdf0e10cSrcweir 			0xC5C43465L, 0x713E38D8L, 0x3D28F89EL, 0xF16DFF20L,
473cdf0e10cSrcweir 			0x153E21E7L, 0x8FB03D4AL, 0xE6E39F2BL, 0xDB83ADF7L
474cdf0e10cSrcweir 		},
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 		/* S[2] */
477cdf0e10cSrcweir 		{
478cdf0e10cSrcweir 			0xE93D5A68L, 0x948140F7L, 0xF64C261CL, 0x94692934L,
479cdf0e10cSrcweir 			0x411520F7L, 0x7602D4F7L, 0xBCF46B2EL, 0xD4A20068L,
480cdf0e10cSrcweir 			0xD4082471L, 0x3320F46AL, 0x43B7D4B7L, 0x500061AFL,
481cdf0e10cSrcweir 			0x1E39F62EL, 0x97244546L, 0x14214F74L, 0xBF8B8840L,
482cdf0e10cSrcweir 
483cdf0e10cSrcweir 			0x4D95FC1DL, 0x96B591AFL, 0x70F4DDD3L, 0x66A02F45L,
484cdf0e10cSrcweir 			0xBFBC09ECL, 0x03BD9785L, 0x7FAC6DD0L, 0x31CB8504L,
485cdf0e10cSrcweir 			0x96EB27B3L, 0x55FD3941L, 0xDA2547E6L, 0xABCA0A9AL,
486cdf0e10cSrcweir 			0x28507825L, 0x530429F4L, 0x0A2C86DAL, 0xE9B66DFBL,
487cdf0e10cSrcweir 
488cdf0e10cSrcweir 			0x68DC1462L, 0xD7486900L, 0x680EC0A4L, 0x27A18DEEL,
489cdf0e10cSrcweir 			0x4F3FFEA2L, 0xE887AD8CL, 0xB58CE006L, 0x7AF4D6B6L,
490cdf0e10cSrcweir 			0xAACE1E7CL, 0xD3375FECL, 0xCE78A399L, 0x406B2A42L,
491cdf0e10cSrcweir 			0x20FE9E35L, 0xD9F385B9L, 0xEE39D7ABL, 0x3B124E8BL,
492cdf0e10cSrcweir 
493cdf0e10cSrcweir 			0x1DC9FAF7L, 0x4B6D1856L, 0x26A36631L, 0xEAE397B2L,
494cdf0e10cSrcweir 			0x3A6EFA74L, 0xDD5B4332L, 0x6841E7F7L, 0xCA7820FBL,
495cdf0e10cSrcweir 			0xFB0AF54EL, 0xD8FEB397L, 0x454056ACL, 0xBA489527L,
496cdf0e10cSrcweir 			0x55533A3AL, 0x20838D87L, 0xFE6BA9B7L, 0xD096954BL,
497cdf0e10cSrcweir 
498cdf0e10cSrcweir 			0x55A867BCL, 0xA1159A58L, 0xCCA92963L, 0x99E1DB33L,
499cdf0e10cSrcweir 			0xA62A4A56L, 0x3F3125F9L, 0x5EF47E1CL, 0x9029317CL,
500cdf0e10cSrcweir 			0xFDF8E802L, 0x04272F70L, 0x80BB155CL, 0x05282CE3L,
501cdf0e10cSrcweir 			0x95C11548L, 0xE4C66D22L, 0x48C1133FL, 0xC70F86DCL,
502cdf0e10cSrcweir 
503cdf0e10cSrcweir 			0x07F9C9EEL, 0x41041F0FL, 0x404779A4L, 0x5D886E17L,
504cdf0e10cSrcweir 			0x325F51EBL, 0xD59BC0D1L, 0xF2BCC18FL, 0x41113564L,
505cdf0e10cSrcweir 			0x257B7834L, 0x602A9C60L, 0xDFF8E8A3L, 0x1F636C1BL,
506cdf0e10cSrcweir 			0x0E12B4C2L, 0x02E1329EL, 0xAF664FD1L, 0xCAD18115L,
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 			0x6B2395E0L, 0x333E92E1L, 0x3B240B62L, 0xEEBEB922L,
509cdf0e10cSrcweir 			0x85B2A20EL, 0xE6BA0D99L, 0xDE720C8CL, 0x2DA2F728L,
510cdf0e10cSrcweir 			0xD0127845L, 0x95B794FDL, 0x647D0862L, 0xE7CCF5F0L,
511cdf0e10cSrcweir 			0x5449A36FL, 0x877D48FAL, 0xC39DFD27L, 0xF33E8D1EL,
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 			0x0A476341L, 0x992EFF74L, 0x3A6F6EABL, 0xF4F8FD37L,
514cdf0e10cSrcweir 			0xA812DC60L, 0xA1EBDDF8L, 0x991BE14CL, 0xDB6E6B0DL,
515cdf0e10cSrcweir 			0xC67B5510L, 0x6D672C37L, 0x2765D43BL, 0xDCD0E804L,
516cdf0e10cSrcweir 			0xF1290DC7L, 0xCC00FFA3L, 0xB5390F92L, 0x690FED0BL,
517cdf0e10cSrcweir 
518cdf0e10cSrcweir 			0x667B9FFBL, 0xCEDB7D9CL, 0xA091CF0BL, 0xD9155EA3L,
519cdf0e10cSrcweir 			0xBB132F88L, 0x515BAD24L, 0x7B9479BFL, 0x763BD6EBL,
520cdf0e10cSrcweir 			0x37392EB3L, 0xCC115979L, 0x8026E297L, 0xF42E312DL,
521cdf0e10cSrcweir 			0x6842ADA7L, 0xC66A2B3BL, 0x12754CCCL, 0x782EF11CL,
522cdf0e10cSrcweir 
523cdf0e10cSrcweir 			0x6A124237L, 0xB79251E7L, 0x06A1BBE6L, 0x4BFB6350L,
524cdf0e10cSrcweir 			0x1A6B1018L, 0x11CAEDFAL, 0x3D25BDD8L, 0xE2E1C3C9L,
525cdf0e10cSrcweir 			0x44421659L, 0x0A121386L, 0xD90CEC6EL, 0xD5ABEA2AL,
526cdf0e10cSrcweir 			0x64AF674EL, 0xDA86A85FL, 0xBEBFE988L, 0x64E4C3FEL,
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 			0x9DBC8057L, 0xF0F7C086L, 0x60787BF8L, 0x6003604DL,
529cdf0e10cSrcweir 			0xD1FD8346L, 0xF6381FB0L, 0x7745AE04L, 0xD736FCCCL,
530cdf0e10cSrcweir 			0x83426B33L, 0xF01EAB71L, 0xB0804187L, 0x3C005E5FL,
531cdf0e10cSrcweir 			0x77A057BEL, 0xBDE8AE24L, 0x55464299L, 0xBF582E61L,
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 			0x4E58F48FL, 0xF2DDFDA2L, 0xF474EF38L, 0x8789BDC2L,
534cdf0e10cSrcweir 			0x5366F9C3L, 0xC8B38E74L, 0xB475F255L, 0x46FCD9B9L,
535cdf0e10cSrcweir 			0x7AEB2661L, 0x8B1DDF84L, 0x846A0E79L, 0x915F95E2L,
536cdf0e10cSrcweir 			0x466E598EL, 0x20B45770L, 0x8CD55591L, 0xC902DE4CL,
537cdf0e10cSrcweir 
538cdf0e10cSrcweir 			0xB90BACE1L, 0xBB8205D0L, 0x11A86248L, 0x7574A99EL,
539cdf0e10cSrcweir 			0xB77F19B6L, 0xE0A9DC09L, 0x662D09A1L, 0xC4324633L,
540cdf0e10cSrcweir 			0xE85A1F02L, 0x09F0BE8CL, 0x4A99A025L, 0x1D6EFE10L,
541cdf0e10cSrcweir 			0x1AB93D1DL, 0x0BA5A4DFL, 0xA186F20FL, 0x2868F169L,
542cdf0e10cSrcweir 
543cdf0e10cSrcweir 			0xDCB7DA83L, 0x573906FEL, 0xA1E2CE9BL, 0x4FCD7F52L,
544cdf0e10cSrcweir 			0x50115E01L, 0xA70683FAL, 0xA002B5C4L, 0x0DE6D027L,
545cdf0e10cSrcweir 			0x9AF88C27L, 0x773F8641L, 0xC3604C06L, 0x61A806B5L,
546cdf0e10cSrcweir 			0xF0177A28L, 0xC0F586E0L, 0x006058AAL, 0x30DC7D62L,
547cdf0e10cSrcweir 
548cdf0e10cSrcweir 			0x11E69ED7L, 0x2338EA63L, 0x53C2DD94L, 0xC2C21634L,
549cdf0e10cSrcweir 			0xBBCBEE56L, 0x90BCB6DEL, 0xEBFC7DA1L, 0xCE591D76L,
550cdf0e10cSrcweir 			0x6F05E409L, 0x4B7C0188L, 0x39720A3DL, 0x7C927C24L,
551cdf0e10cSrcweir 			0x86E3725FL, 0x724D9DB9L, 0x1AC15BB4L, 0xD39EB8FCL,
552cdf0e10cSrcweir 
553cdf0e10cSrcweir 			0xED545578L, 0x08FCA5B5L, 0xD83D7CD3L, 0x4DAD0FC4L,
554cdf0e10cSrcweir 			0x1E50EF5EL, 0xB161E6F8L, 0xA28514D9L, 0x6C51133CL,
555cdf0e10cSrcweir 			0x6FD5C7E7L, 0x56E14EC4L, 0x362ABFCEL, 0xDDC6C837L,
556cdf0e10cSrcweir 			0xD79A3234L, 0x92638212L, 0x670EFA8EL, 0x406000E0L
557cdf0e10cSrcweir 		},
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 		/* S[3] */
560cdf0e10cSrcweir 		{
561cdf0e10cSrcweir 			0x3A39CE37L, 0xD3FAF5CFL, 0xABC27737L, 0x5AC52D1BL,
562cdf0e10cSrcweir 			0x5CB0679EL, 0x4FA33742L, 0xD3822740L, 0x99BC9BBEL,
563cdf0e10cSrcweir 			0xD5118E9DL, 0xBF0F7315L, 0xD62D1C7EL, 0xC700C47BL,
564cdf0e10cSrcweir 			0xB78C1B6BL, 0x21A19045L, 0xB26EB1BEL, 0x6A366EB4L,
565cdf0e10cSrcweir 
566cdf0e10cSrcweir 			0x5748AB2FL, 0xBC946E79L, 0xC6A376D2L, 0x6549C2C8L,
567cdf0e10cSrcweir 			0x530FF8EEL, 0x468DDE7DL, 0xD5730A1DL, 0x4CD04DC6L,
568cdf0e10cSrcweir 			0x2939BBDBL, 0xA9BA4650L, 0xAC9526E8L, 0xBE5EE304L,
569cdf0e10cSrcweir 			0xA1FAD5F0L, 0x6A2D519AL, 0x63EF8CE2L, 0x9A86EE22L,
570cdf0e10cSrcweir 
571cdf0e10cSrcweir 			0xC089C2B8L, 0x43242EF6L, 0xA51E03AAL, 0x9CF2D0A4L,
572cdf0e10cSrcweir 			0x83C061BAL, 0x9BE96A4DL, 0x8FE51550L, 0xBA645BD6L,
573cdf0e10cSrcweir 			0x2826A2F9L, 0xA73A3AE1L, 0x4BA99586L, 0xEF5562E9L,
574cdf0e10cSrcweir 			0xC72FEFD3L, 0xF752F7DAL, 0x3F046F69L, 0x77FA0A59L,
575cdf0e10cSrcweir 
576cdf0e10cSrcweir 			0x80E4A915L, 0x87B08601L, 0x9B09E6ADL, 0x3B3EE593L,
577cdf0e10cSrcweir 			0xE990FD5AL, 0x9E34D797L, 0x2CF0B7D9L, 0x022B8B51L,
578cdf0e10cSrcweir 			0x96D5AC3AL, 0x017DA67DL, 0xD1CF3ED6L, 0x7C7D2D28L,
579cdf0e10cSrcweir 			0x1F9F25CFL, 0xADF2B89BL, 0x5AD6B472L, 0x5A88F54CL,
580cdf0e10cSrcweir 
581cdf0e10cSrcweir 			0xE029AC71L, 0xE019A5E6L, 0x47B0ACFDL, 0xED93FA9BL,
582cdf0e10cSrcweir 			0xE8D3C48DL, 0x283B57CCL, 0xF8D56629L, 0x79132E28L,
583cdf0e10cSrcweir 			0x785F0191L, 0xED756055L, 0xF7960E44L, 0xE3D35E8CL,
584cdf0e10cSrcweir 			0x15056DD4L, 0x88F46DBAL, 0x03A16125L, 0x0564F0BDL,
585cdf0e10cSrcweir 
586cdf0e10cSrcweir 			0xC3EB9E15L, 0x3C9057A2L, 0x97271AECL, 0xA93A072AL,
587cdf0e10cSrcweir 			0x1B3F6D9BL, 0x1E6321F5L, 0xF59C66FBL, 0x26DCF319L,
588cdf0e10cSrcweir 			0x7533D928L, 0xB155FDF5L, 0x03563482L, 0x8ABA3CBBL,
589cdf0e10cSrcweir 			0x28517711L, 0xC20AD9F8L, 0xABCC5167L, 0xCCAD925FL,
590cdf0e10cSrcweir 
591cdf0e10cSrcweir 			0x4DE81751L, 0x3830DC8EL, 0x379D5862L, 0x9320F991L,
592cdf0e10cSrcweir 			0xEA7A90C2L, 0xFB3E7BCEL, 0x5121CE64L, 0x774FBE32L,
593cdf0e10cSrcweir 			0xA8B6E37EL, 0xC3293D46L, 0x48DE5369L, 0x6413E680L,
594cdf0e10cSrcweir 			0xA2AE0810L, 0xDD6DB224L, 0x69852DFDL, 0x09072166L,
595cdf0e10cSrcweir 
596cdf0e10cSrcweir 			0xB39A460AL, 0x6445C0DDL, 0x586CDECFL, 0x1C20C8AEL,
597cdf0e10cSrcweir 			0x5BBEF7DDL, 0x1B588D40L, 0xCCD2017FL, 0x6BB4E3BBL,
598cdf0e10cSrcweir 			0xDDA26A7EL, 0x3A59FF45L, 0x3E350A44L, 0xBCB4CDD5L,
599cdf0e10cSrcweir 			0x72EACEA8L, 0xFA6484BBL, 0x8D6612AEL, 0xBF3C6F47L,
600cdf0e10cSrcweir 
601cdf0e10cSrcweir 			0xD29BE463L, 0x542F5D9EL, 0xAEC2771BL, 0xF64E6370L,
602cdf0e10cSrcweir 			0x740E0D8DL, 0xE75B1357L, 0xF8721671L, 0xAF537D5DL,
603cdf0e10cSrcweir 			0x4040CB08L, 0x4EB4E2CCL, 0x34D2466AL, 0x0115AF84L,
604cdf0e10cSrcweir 			0xE1B00428L, 0x95983A1DL, 0x06B89FB4L, 0xCE6EA048L,
605cdf0e10cSrcweir 
606cdf0e10cSrcweir 			0x6F3F3B82L, 0x3520AB82L, 0x011A1D4BL, 0x277227F8L,
607cdf0e10cSrcweir 			0x611560B1L, 0xE7933FDCL, 0xBB3A792BL, 0x344525BDL,
608cdf0e10cSrcweir 			0xA08839E1L, 0x51CE794BL, 0x2F32C9B7L, 0xA01FBAC9L,
609cdf0e10cSrcweir 			0xE01CC87EL, 0xBCC7D1F6L, 0xCF0111C3L, 0xA1E8AAC7L,
610cdf0e10cSrcweir 
611cdf0e10cSrcweir 			0x1A908749L, 0xD44FBD9AL, 0xD0DADECBL, 0xD50ADA38L,
612cdf0e10cSrcweir 			0x0339C32AL, 0xC6913667L, 0x8DF9317CL, 0xE0B12B4FL,
613cdf0e10cSrcweir 			0xF79E59B7L, 0x43F5BB3AL, 0xF2D519FFL, 0x27D9459CL,
614cdf0e10cSrcweir 			0xBF97222CL, 0x15E6FC2AL, 0x0F91FC71L, 0x9B941525L,
615cdf0e10cSrcweir 
616cdf0e10cSrcweir 			0xFAE59361L, 0xCEB69CEBL, 0xC2A86459L, 0x12BAA8D1L,
617cdf0e10cSrcweir 			0xB6C1075EL, 0xE3056A0CL, 0x10D25065L, 0xCB03A442L,
618cdf0e10cSrcweir 			0xE0EC6E0EL, 0x1698DB3BL, 0x4C98A0BEL, 0x3278E964L,
619cdf0e10cSrcweir 			0x9F1F9532L, 0xE0D392DFL, 0xD3A0342BL, 0x8971F21EL,
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 			0x1B0A7441L, 0x4BA3348CL, 0xC5BE7120L, 0xC37632D8L,
622cdf0e10cSrcweir 			0xDF359F8DL, 0x9B992F2EL, 0xE60B6F47L, 0x0FE3F11DL,
623cdf0e10cSrcweir 			0xE54CDA54L, 0x1EDAD891L, 0xCE6279CFL, 0xCD3E7E6FL,
624cdf0e10cSrcweir 			0x1618B166L, 0xFD2C1D05L, 0x848FD2C5L, 0xF6FB2299L,
625cdf0e10cSrcweir 
626cdf0e10cSrcweir 			0xF523F357L, 0xA6327623L, 0x93A83531L, 0x56CCCD02L,
627cdf0e10cSrcweir 			0xACF08162L, 0x5A75EBB5L, 0x6E163697L, 0x88D273CCL,
628cdf0e10cSrcweir 			0xDE966292L, 0x81B949D0L, 0x4C50901BL, 0x71C65614L,
629cdf0e10cSrcweir 			0xE6C6C7BDL, 0x327A140AL, 0x45E1D006L, 0xC3F27B9AL,
630cdf0e10cSrcweir 
631cdf0e10cSrcweir 			0xC9AA53FDL, 0x62A80F00L, 0xBB25BFE2L, 0x35BDD2F6L,
632cdf0e10cSrcweir 			0x71126905L, 0xB2040222L, 0xB6CBCF7CL, 0xCD769C2BL,
633cdf0e10cSrcweir 			0x53113EC0L, 0x1640E3D3L, 0x38ABBD60L, 0x2547ADF0L,
634cdf0e10cSrcweir 			0xBA38209CL, 0xF746CE76L, 0x77AFA1C5L, 0x20756060L,
635cdf0e10cSrcweir 
636cdf0e10cSrcweir 			0x85CBFE4EL, 0x8AE88DD8L, 0x7AAAF9B0L, 0x4CF9AA7EL,
637cdf0e10cSrcweir 			0x1948C25CL, 0x02FB8A8CL, 0x01C36AE4L, 0xD6EBE1F9L,
638cdf0e10cSrcweir 			0x90D4F869L, 0xA65CDEA0L, 0x3F09252DL, 0xC208E69FL,
639cdf0e10cSrcweir 			0xB74E6132L, 0xCE77E25BL, 0x578FDFE3L, 0x3AC372E6L
640cdf0e10cSrcweir 		}
641cdf0e10cSrcweir 	},
642cdf0e10cSrcweir 
643cdf0e10cSrcweir 	/* P */
644cdf0e10cSrcweir 	{
645cdf0e10cSrcweir 		0x243F6A88L, 0x85A308D3L, 0x13198A2EL, 0x03707344L,
646cdf0e10cSrcweir 		0xA4093822L, 0x299F31D0L, 0x082EFA98L, 0xEC4E6C89L,
647cdf0e10cSrcweir 		0x452821E6L, 0x38D01377L, 0xBE5466CFL, 0x34E90C6CL,
648cdf0e10cSrcweir 		0xC0AC29B7L, 0xC97C50DDL, 0x3F84D5B5L, 0xB5470917L,
649cdf0e10cSrcweir 		0x9216D5D9L, 0x8979FB1BL
650cdf0e10cSrcweir 	}
651cdf0e10cSrcweir };
652cdf0e10cSrcweir 
653cdf0e10cSrcweir /*
654cdf0e10cSrcweir  * __rtl_cipherBF_init.
655cdf0e10cSrcweir  */
__rtl_cipherBF_init(CipherContextBF * ctx,rtlCipherMode eMode,const sal_uInt8 * pKeyData,sal_Size nKeyLen,const sal_uInt8 * pArgData,sal_Size nArgLen)656cdf0e10cSrcweir static rtlCipherError __rtl_cipherBF_init (
657cdf0e10cSrcweir 	CipherContextBF *ctx,
658cdf0e10cSrcweir 	rtlCipherMode    eMode,
659cdf0e10cSrcweir 	const sal_uInt8 *pKeyData, sal_Size nKeyLen,
660cdf0e10cSrcweir 	const sal_uInt8 *pArgData, sal_Size nArgLen)
661cdf0e10cSrcweir {
662cdf0e10cSrcweir 	CipherKeyBF *key;
663cdf0e10cSrcweir 	sal_uInt32   D, DL, DR;
664cdf0e10cSrcweir 	sal_uInt16   i, j, k;
665cdf0e10cSrcweir 
666cdf0e10cSrcweir 	key = &(ctx->m_key);
667cdf0e10cSrcweir 
668cdf0e10cSrcweir 	rtl_copyMemory (key, &__rtl_cipherBF_key, sizeof (CipherKeyBF));
669cdf0e10cSrcweir 	rtl_zeroMemory (&(ctx->m_iv), sizeof(ctx->m_iv));
670cdf0e10cSrcweir 	ctx->m_offset = 0;
671cdf0e10cSrcweir 
672cdf0e10cSrcweir 	for (i = 0, k = 0; i < CIPHER_ROUNDS_BF + 2; ++i)
673cdf0e10cSrcweir 	{
674cdf0e10cSrcweir 		D = 0;
675cdf0e10cSrcweir 		for (j = 0; j < 4; ++j)
676cdf0e10cSrcweir 		{
677cdf0e10cSrcweir 			D = ((D << 8) | pKeyData[k]);
678cdf0e10cSrcweir 			k++;
679cdf0e10cSrcweir 			if (k >= nKeyLen)
680cdf0e10cSrcweir 				k = 0;
681cdf0e10cSrcweir 		}
682cdf0e10cSrcweir 		key->m_P[i] ^= D;
683cdf0e10cSrcweir 	}
684cdf0e10cSrcweir 
685cdf0e10cSrcweir 	DL = 0;
686cdf0e10cSrcweir 	DR = 0;
687cdf0e10cSrcweir 
688cdf0e10cSrcweir 	for (i = 0; i < CIPHER_ROUNDS_BF + 2; i += 2)
689cdf0e10cSrcweir 	{
690cdf0e10cSrcweir 		__rtl_cipherBF_encode (key, &DL, &DR);
691cdf0e10cSrcweir 		key->m_P[i    ] = DL;
692cdf0e10cSrcweir 		key->m_P[i + 1] = DR;
693cdf0e10cSrcweir 	}
694cdf0e10cSrcweir 
695cdf0e10cSrcweir 	for (i = 0; i < 4; ++i)
696cdf0e10cSrcweir 	{
697cdf0e10cSrcweir 		for (k = 0; k < 256; k += 2)
698cdf0e10cSrcweir 		{
699cdf0e10cSrcweir 			__rtl_cipherBF_encode (key, &DL, &DR);
700cdf0e10cSrcweir 			key->m_S[i][k    ] = DL;
701cdf0e10cSrcweir 			key->m_S[i][k + 1] = DR;
702cdf0e10cSrcweir 		}
703cdf0e10cSrcweir 	}
704cdf0e10cSrcweir 
705cdf0e10cSrcweir 	if (pArgData && nArgLen)
706cdf0e10cSrcweir 	{
707cdf0e10cSrcweir 		nArgLen = ((nArgLen < 8) ? nArgLen : 8);
708cdf0e10cSrcweir 		if (eMode == rtl_Cipher_ModeStream)
709cdf0e10cSrcweir 		{
710cdf0e10cSrcweir 			rtl_copyMemory (ctx->m_iv.m_byte, pArgData, nArgLen);
711cdf0e10cSrcweir 		}
712cdf0e10cSrcweir 		else
713cdf0e10cSrcweir 		{
714cdf0e10cSrcweir 			RTL_CIPHER_NTOHL64 (pArgData, DL, DR, nArgLen);
715cdf0e10cSrcweir 			ctx->m_iv.m_long[0] = DL;
716cdf0e10cSrcweir 			ctx->m_iv.m_long[1] = DR;
717cdf0e10cSrcweir 		}
718cdf0e10cSrcweir 	}
719cdf0e10cSrcweir 
720cdf0e10cSrcweir 	D = DL = DR = 0;
721cdf0e10cSrcweir 	return rtl_Cipher_E_None;
722cdf0e10cSrcweir }
723cdf0e10cSrcweir 
724cdf0e10cSrcweir /*
725cdf0e10cSrcweir  * __rtl_cipherBF_update.
726cdf0e10cSrcweir  */
__rtl_cipherBF_update(CipherContextBF * ctx,rtlCipherMode eMode,rtlCipherDirection eDirection,const sal_uInt8 * pData,sal_Size nDatLen,sal_uInt8 * pBuffer,sal_Size nBufLen)727cdf0e10cSrcweir static rtlCipherError __rtl_cipherBF_update (
728cdf0e10cSrcweir 	CipherContextBF    *ctx,
729cdf0e10cSrcweir 	rtlCipherMode       eMode,
730cdf0e10cSrcweir 	rtlCipherDirection  eDirection,
731cdf0e10cSrcweir 	const sal_uInt8    *pData,   sal_Size nDatLen,
732cdf0e10cSrcweir 	sal_uInt8          *pBuffer, sal_Size nBufLen)
733cdf0e10cSrcweir {
734cdf0e10cSrcweir 	/* Check arguments. */
735cdf0e10cSrcweir 	if ((pData == NULL) || (pBuffer == NULL))
736cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
737cdf0e10cSrcweir 
738cdf0e10cSrcweir 	if (!((0 < nDatLen) && (nDatLen <= nBufLen)))
739cdf0e10cSrcweir 		return rtl_Cipher_E_BufferSize;
740cdf0e10cSrcweir 
741cdf0e10cSrcweir 	/* Update. */
742cdf0e10cSrcweir 	if (eMode == rtl_Cipher_ModeECB)
743cdf0e10cSrcweir 	{
744cdf0e10cSrcweir 		/* Block mode. */
745cdf0e10cSrcweir 		while (nDatLen > 8)
746cdf0e10cSrcweir 		{
747cdf0e10cSrcweir 			__rtl_cipherBF_updateECB (ctx, eDirection, pData, pBuffer, 8);
748cdf0e10cSrcweir 			nDatLen -= 8;
749cdf0e10cSrcweir 			pData   += 8;
750cdf0e10cSrcweir 			pBuffer += 8;
751cdf0e10cSrcweir 		}
752cdf0e10cSrcweir 		__rtl_cipherBF_updateECB (ctx, eDirection, pData, pBuffer, nDatLen);
753cdf0e10cSrcweir 	}
754cdf0e10cSrcweir 	else if (eMode == rtl_Cipher_ModeCBC)
755cdf0e10cSrcweir 	{
756cdf0e10cSrcweir 		/* Block mode. */
757cdf0e10cSrcweir 		while (nDatLen > 8)
758cdf0e10cSrcweir 		{
759cdf0e10cSrcweir 			__rtl_cipherBF_updateCBC (ctx, eDirection, pData, pBuffer, 8);
760cdf0e10cSrcweir 			nDatLen -= 8;
761cdf0e10cSrcweir 			pData   += 8;
762cdf0e10cSrcweir 			pBuffer += 8;
763cdf0e10cSrcweir 		}
764cdf0e10cSrcweir 		__rtl_cipherBF_updateCBC (ctx, eDirection, pData, pBuffer, nDatLen);
765cdf0e10cSrcweir 	}
766cdf0e10cSrcweir 	else
767cdf0e10cSrcweir 	{
768cdf0e10cSrcweir 		/* Stream mode. */
769cdf0e10cSrcweir 		while (nDatLen > 0)
770cdf0e10cSrcweir 		{
771cdf0e10cSrcweir 			__rtl_cipherBF_updateCFB (ctx, eDirection, pData, pBuffer);
772cdf0e10cSrcweir 			nDatLen -= 1;
773cdf0e10cSrcweir 			pData   += 1;
774cdf0e10cSrcweir 			pBuffer += 1;
775cdf0e10cSrcweir 		}
776cdf0e10cSrcweir 	}
777cdf0e10cSrcweir 	return rtl_Cipher_E_None;
778cdf0e10cSrcweir }
779cdf0e10cSrcweir 
780cdf0e10cSrcweir /*
781cdf0e10cSrcweir  * __rtl_cipherBF_updateECB.
782cdf0e10cSrcweir  */
__rtl_cipherBF_updateECB(CipherContextBF * ctx,rtlCipherDirection direction,const sal_uInt8 * pData,sal_uInt8 * pBuffer,sal_Size nLength)783cdf0e10cSrcweir static void __rtl_cipherBF_updateECB (
784cdf0e10cSrcweir 	CipherContextBF    *ctx,
785cdf0e10cSrcweir 	rtlCipherDirection  direction,
786cdf0e10cSrcweir 	const sal_uInt8    *pData,
787cdf0e10cSrcweir 	sal_uInt8          *pBuffer,
788cdf0e10cSrcweir 	sal_Size            nLength)
789cdf0e10cSrcweir {
790cdf0e10cSrcweir 	CipherKeyBF *key;
791cdf0e10cSrcweir 	sal_uInt32   DL, DR;
792cdf0e10cSrcweir 
793cdf0e10cSrcweir 	key = &(ctx->m_key);
794cdf0e10cSrcweir 	if (direction == rtl_Cipher_DirectionEncode)
795cdf0e10cSrcweir 	{
796cdf0e10cSrcweir 		RTL_CIPHER_NTOHL64(pData, DL, DR, nLength);
797cdf0e10cSrcweir 
798cdf0e10cSrcweir 		__rtl_cipherBF_encode (key, &DL, &DR);
799cdf0e10cSrcweir 
800cdf0e10cSrcweir 		RTL_CIPHER_HTONL(DL, pBuffer);
801cdf0e10cSrcweir 		RTL_CIPHER_HTONL(DR, pBuffer);
802cdf0e10cSrcweir 	}
803cdf0e10cSrcweir 	else
804cdf0e10cSrcweir 	{
805cdf0e10cSrcweir 		RTL_CIPHER_NTOHL(pData, DL);
806cdf0e10cSrcweir 		RTL_CIPHER_NTOHL(pData, DR);
807cdf0e10cSrcweir 
808cdf0e10cSrcweir 		__rtl_cipherBF_decode (key, &DL, &DR);
809cdf0e10cSrcweir 
810cdf0e10cSrcweir 		RTL_CIPHER_HTONL64(DL, DR, pBuffer, nLength);
811cdf0e10cSrcweir 	}
812cdf0e10cSrcweir 	DL = DR = 0;
813cdf0e10cSrcweir }
814cdf0e10cSrcweir 
815cdf0e10cSrcweir /*
816cdf0e10cSrcweir  * __rtl_cipherBF_updateCBC.
817cdf0e10cSrcweir  */
__rtl_cipherBF_updateCBC(CipherContextBF * ctx,rtlCipherDirection direction,const sal_uInt8 * pData,sal_uInt8 * pBuffer,sal_Size nLength)818cdf0e10cSrcweir static void __rtl_cipherBF_updateCBC (
819cdf0e10cSrcweir 	CipherContextBF    *ctx,
820cdf0e10cSrcweir 	rtlCipherDirection  direction,
821cdf0e10cSrcweir 	const sal_uInt8    *pData,
822cdf0e10cSrcweir 	sal_uInt8          *pBuffer,
823cdf0e10cSrcweir 	sal_Size            nLength)
824cdf0e10cSrcweir {
825cdf0e10cSrcweir 	CipherKeyBF *key;
826cdf0e10cSrcweir 	sal_uInt32   DL, DR;
827cdf0e10cSrcweir 
828cdf0e10cSrcweir 	key = &(ctx->m_key);
829cdf0e10cSrcweir 	if (direction == rtl_Cipher_DirectionEncode)
830cdf0e10cSrcweir 	{
831cdf0e10cSrcweir 		RTL_CIPHER_NTOHL64(pData, DL, DR, nLength);
832cdf0e10cSrcweir 
833cdf0e10cSrcweir 		DL ^= ctx->m_iv.m_long[0];
834cdf0e10cSrcweir 		DR ^= ctx->m_iv.m_long[1];
835cdf0e10cSrcweir 
836cdf0e10cSrcweir 		__rtl_cipherBF_encode (key, &DL, &DR);
837cdf0e10cSrcweir 
838cdf0e10cSrcweir 		ctx->m_iv.m_long[0] = DL;
839cdf0e10cSrcweir 		ctx->m_iv.m_long[1] = DR;
840cdf0e10cSrcweir 
841cdf0e10cSrcweir 		RTL_CIPHER_HTONL(DL, pBuffer);
842cdf0e10cSrcweir 		RTL_CIPHER_HTONL(DR, pBuffer);
843cdf0e10cSrcweir 	}
844cdf0e10cSrcweir 	else
845cdf0e10cSrcweir 	{
846cdf0e10cSrcweir 		sal_uInt32 IVL, IVR;
847cdf0e10cSrcweir 
848cdf0e10cSrcweir 		RTL_CIPHER_NTOHL(pData, DL);
849cdf0e10cSrcweir 		RTL_CIPHER_NTOHL(pData, DR);
850cdf0e10cSrcweir 
851cdf0e10cSrcweir 		IVL = DL;
852cdf0e10cSrcweir 		IVR = DR;
853cdf0e10cSrcweir 
854cdf0e10cSrcweir 		__rtl_cipherBF_decode (key, &DL, &DR);
855cdf0e10cSrcweir 
856cdf0e10cSrcweir 		DL ^= ctx->m_iv.m_long[0];
857cdf0e10cSrcweir 		DR ^= ctx->m_iv.m_long[1];
858cdf0e10cSrcweir 
859cdf0e10cSrcweir 		ctx->m_iv.m_long[0] = IVL;
860cdf0e10cSrcweir 		ctx->m_iv.m_long[1] = IVR;
861cdf0e10cSrcweir 
862cdf0e10cSrcweir 		RTL_CIPHER_HTONL64(DL, DR, pBuffer, nLength);
863cdf0e10cSrcweir 		IVL = IVR = 0;
864cdf0e10cSrcweir 	}
865cdf0e10cSrcweir 	DL = DR = 0;
866cdf0e10cSrcweir }
867cdf0e10cSrcweir 
868cdf0e10cSrcweir /*
869cdf0e10cSrcweir  * __rtl_cipherBF_updateCFB.
870cdf0e10cSrcweir  */
__rtl_cipherBF_updateCFB(CipherContextBF * ctx,rtlCipherDirection direction,const sal_uInt8 * pData,sal_uInt8 * pBuffer)871cdf0e10cSrcweir static void __rtl_cipherBF_updateCFB (
872cdf0e10cSrcweir 	CipherContextBF    *ctx,
873cdf0e10cSrcweir 	rtlCipherDirection  direction,
874cdf0e10cSrcweir 	const sal_uInt8    *pData,
875cdf0e10cSrcweir 	sal_uInt8          *pBuffer)
876cdf0e10cSrcweir {
877cdf0e10cSrcweir 	sal_uInt8  *iv;
878cdf0e10cSrcweir 	sal_uInt32  k;
879cdf0e10cSrcweir 
880cdf0e10cSrcweir 	iv = ctx->m_iv.m_byte;
881cdf0e10cSrcweir 	k  = ctx->m_offset;
882cdf0e10cSrcweir 
883cdf0e10cSrcweir 	if (k == 0)
884cdf0e10cSrcweir 	{
885cdf0e10cSrcweir 		sal_uInt32 IVL, IVR;
886cdf0e10cSrcweir 
887cdf0e10cSrcweir 		RTL_CIPHER_NTOHL64(iv, IVL, IVR, 8);
888cdf0e10cSrcweir 		__rtl_cipherBF_encode (&(ctx->m_key), &IVL, &IVR);
889cdf0e10cSrcweir 		RTL_CIPHER_HTONL64(IVL, IVR, iv, 8);
890cdf0e10cSrcweir 
891cdf0e10cSrcweir 		IVL = IVR = 0;
892cdf0e10cSrcweir 	}
893cdf0e10cSrcweir 
894cdf0e10cSrcweir 	if (direction == rtl_Cipher_DirectionEncode)
895cdf0e10cSrcweir 	{
896cdf0e10cSrcweir 		iv[k] ^= *pData;
897cdf0e10cSrcweir 		*pBuffer = iv[k];
898cdf0e10cSrcweir 	}
899cdf0e10cSrcweir 	else
900cdf0e10cSrcweir 	{
901cdf0e10cSrcweir 		sal_uInt8 c = iv[k];
902cdf0e10cSrcweir 		iv[k] = *pData;
903cdf0e10cSrcweir 		*pBuffer = *pData ^ c;
904cdf0e10cSrcweir 		c = 0;
905cdf0e10cSrcweir 	}
906cdf0e10cSrcweir 
907cdf0e10cSrcweir 	ctx->m_offset = ((k + 1) & 0x07);
908cdf0e10cSrcweir 	iv = NULL;
909cdf0e10cSrcweir }
910cdf0e10cSrcweir 
911cdf0e10cSrcweir /*
912cdf0e10cSrcweir  * __rtl_cipherBF_encode.
913cdf0e10cSrcweir  */
__rtl_cipherBF_encode(CipherKeyBF * key,sal_uInt32 * xl,sal_uInt32 * xr)914cdf0e10cSrcweir static void __rtl_cipherBF_encode (
915cdf0e10cSrcweir 	CipherKeyBF *key, sal_uInt32 *xl, sal_uInt32 *xr)
916cdf0e10cSrcweir {
917cdf0e10cSrcweir 	sal_uInt32  t, XL, XR;
918cdf0e10cSrcweir 	sal_uInt16  i;
919cdf0e10cSrcweir 
920cdf0e10cSrcweir 	XL = *xl;
921cdf0e10cSrcweir 	XR = *xr;
922cdf0e10cSrcweir 
923cdf0e10cSrcweir 	for (i = 0; i < CIPHER_ROUNDS_BF; ++i)
924cdf0e10cSrcweir 	{
925cdf0e10cSrcweir 		XL ^= key->m_P[i];
926cdf0e10cSrcweir 		XR ^= __rtl_cipherBF (key, XL);
927cdf0e10cSrcweir 
928cdf0e10cSrcweir 		t  = XL;
929cdf0e10cSrcweir 		XL = XR;
930cdf0e10cSrcweir 		XR = t;
931cdf0e10cSrcweir 	}
932cdf0e10cSrcweir 
933cdf0e10cSrcweir 	t  = XL;
934cdf0e10cSrcweir 	XL = XR;
935cdf0e10cSrcweir 	XR = t;
936cdf0e10cSrcweir 
937cdf0e10cSrcweir 	XR ^= key->m_P[CIPHER_ROUNDS_BF    ];
938cdf0e10cSrcweir 	XL ^= key->m_P[CIPHER_ROUNDS_BF + 1];
939cdf0e10cSrcweir 
940cdf0e10cSrcweir 	*xl = XL;
941cdf0e10cSrcweir 	*xr = XR;
942cdf0e10cSrcweir 
943cdf0e10cSrcweir 	t = XL = XR = 0;
944cdf0e10cSrcweir }
945cdf0e10cSrcweir 
946cdf0e10cSrcweir /*
947cdf0e10cSrcweir  * __rtl_cipherBF_decode.
948cdf0e10cSrcweir  */
__rtl_cipherBF_decode(CipherKeyBF * key,sal_uInt32 * xl,sal_uInt32 * xr)949cdf0e10cSrcweir static void __rtl_cipherBF_decode (
950cdf0e10cSrcweir 	CipherKeyBF *key, sal_uInt32 *xl, sal_uInt32 *xr)
951cdf0e10cSrcweir {
952cdf0e10cSrcweir 	sal_uInt32  t, XL, XR;
953cdf0e10cSrcweir 	sal_uInt16  i;
954cdf0e10cSrcweir 
955cdf0e10cSrcweir 	XL = *xl;
956cdf0e10cSrcweir 	XR = *xr;
957cdf0e10cSrcweir 
958cdf0e10cSrcweir 	for (i = CIPHER_ROUNDS_BF + 1; i > 1; --i)
959cdf0e10cSrcweir 	{
960cdf0e10cSrcweir 		XL ^= key->m_P[i];
961cdf0e10cSrcweir 		XR ^= __rtl_cipherBF (key, XL);
962cdf0e10cSrcweir 
963cdf0e10cSrcweir 		t  = XL;
964cdf0e10cSrcweir 		XL = XR;
965cdf0e10cSrcweir 		XR = t;
966cdf0e10cSrcweir 	}
967cdf0e10cSrcweir 
968cdf0e10cSrcweir 	t  = XL;
969cdf0e10cSrcweir 	XL = XR;
970cdf0e10cSrcweir 	XR = t;
971cdf0e10cSrcweir 
972cdf0e10cSrcweir 	XR ^= key->m_P[1];
973cdf0e10cSrcweir 	XL ^= key->m_P[0];
974cdf0e10cSrcweir 
975cdf0e10cSrcweir 	*xl = XL;
976cdf0e10cSrcweir 	*xr = XR;
977cdf0e10cSrcweir 
978cdf0e10cSrcweir 	t = XL = XR = 0;
979cdf0e10cSrcweir }
980cdf0e10cSrcweir 
981cdf0e10cSrcweir /*
982cdf0e10cSrcweir  * __rtl_cipherBF.
983cdf0e10cSrcweir  */
__rtl_cipherBF(CipherKeyBF * key,sal_uInt32 x)984cdf0e10cSrcweir static sal_uInt32 __rtl_cipherBF (CipherKeyBF *key, sal_uInt32 x)
985cdf0e10cSrcweir {
986cdf0e10cSrcweir 	sal_uInt16 a, b, c, d;
987cdf0e10cSrcweir 	sal_uInt32 y;
988cdf0e10cSrcweir 
989cdf0e10cSrcweir 	d = (sal_uInt16)(x & 0x00ff);
990cdf0e10cSrcweir 	x >>= 8;
991cdf0e10cSrcweir 	c = (sal_uInt16)(x & 0x00ff);
992cdf0e10cSrcweir 	x >>= 8;
993cdf0e10cSrcweir 	b = (sal_uInt16)(x & 0x00ff);
994cdf0e10cSrcweir 	x >>= 8;
995cdf0e10cSrcweir 	a = (sal_uInt16)(x & 0x00ff);
996cdf0e10cSrcweir 
997cdf0e10cSrcweir 	y  = key->m_S[0][a];
998cdf0e10cSrcweir 	y += key->m_S[1][b];
999cdf0e10cSrcweir 	y ^= key->m_S[2][c];
1000cdf0e10cSrcweir 	y += key->m_S[3][d];
1001cdf0e10cSrcweir 
1002cdf0e10cSrcweir 	return y;
1003cdf0e10cSrcweir }
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir /*========================================================================
1006cdf0e10cSrcweir  *
1007cdf0e10cSrcweir  * rtl_cipherBF (Blowfish) implementation.
1008cdf0e10cSrcweir  *
1009cdf0e10cSrcweir  * Reference:
1010cdf0e10cSrcweir  *   Bruce Schneier: Applied Cryptography, 2nd edition, ch. 14.3
1011cdf0e10cSrcweir  *
1012cdf0e10cSrcweir  *======================================================================*/
1013cdf0e10cSrcweir /*
1014cdf0e10cSrcweir  * rtl_cipher_createBF.
1015cdf0e10cSrcweir  */
rtl_cipher_createBF(rtlCipherMode Mode)1016cdf0e10cSrcweir rtlCipher SAL_CALL rtl_cipher_createBF (rtlCipherMode Mode)
1017cdf0e10cSrcweir {
1018cdf0e10cSrcweir 	CipherBF_Impl *pImpl = (CipherBF_Impl*)NULL;
1019cdf0e10cSrcweir 
1020cdf0e10cSrcweir 	if (Mode == rtl_Cipher_ModeInvalid)
1021cdf0e10cSrcweir 		return ((rtlCipher)NULL);
1022cdf0e10cSrcweir 
1023cdf0e10cSrcweir 	pImpl = ((CipherBF_Impl*)rtl_allocateZeroMemory (sizeof (CipherBF_Impl)));
1024cdf0e10cSrcweir 	if (pImpl)
1025cdf0e10cSrcweir 	{
1026cdf0e10cSrcweir 		pImpl->m_cipher.m_algorithm = rtl_Cipher_AlgorithmBF;
1027cdf0e10cSrcweir 		pImpl->m_cipher.m_direction = rtl_Cipher_DirectionInvalid;
1028cdf0e10cSrcweir 		pImpl->m_cipher.m_mode      = Mode;
1029cdf0e10cSrcweir 
1030cdf0e10cSrcweir 		pImpl->m_cipher.m_init      = rtl_cipher_initBF;
1031cdf0e10cSrcweir 		pImpl->m_cipher.m_encode    = rtl_cipher_encodeBF;
1032cdf0e10cSrcweir 		pImpl->m_cipher.m_decode    = rtl_cipher_decodeBF;
1033cdf0e10cSrcweir 		pImpl->m_cipher.m_delete    = rtl_cipher_destroyBF;
1034cdf0e10cSrcweir 	}
1035cdf0e10cSrcweir 	return ((rtlCipher)pImpl);
1036cdf0e10cSrcweir }
1037cdf0e10cSrcweir 
1038cdf0e10cSrcweir /*
1039cdf0e10cSrcweir  * rtl_cipher_initBF.
1040cdf0e10cSrcweir  */
rtl_cipher_initBF(rtlCipher Cipher,rtlCipherDirection Direction,const sal_uInt8 * pKeyData,sal_Size nKeyLen,const sal_uInt8 * pArgData,sal_Size nArgLen)1041cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_initBF (
1042cdf0e10cSrcweir 	rtlCipher          Cipher,
1043cdf0e10cSrcweir 	rtlCipherDirection Direction,
1044cdf0e10cSrcweir 	const sal_uInt8 *pKeyData, sal_Size nKeyLen,
1045cdf0e10cSrcweir 	const sal_uInt8 *pArgData, sal_Size nArgLen)
1046cdf0e10cSrcweir {
1047cdf0e10cSrcweir 	CipherBF_Impl *pImpl = (CipherBF_Impl*)Cipher;
1048cdf0e10cSrcweir 
1049cdf0e10cSrcweir 	if ((pImpl == NULL) || (pKeyData == NULL))
1050cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
1051cdf0e10cSrcweir 
1052cdf0e10cSrcweir 	if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmBF))
1053cdf0e10cSrcweir 		return rtl_Cipher_E_Algorithm;
1054cdf0e10cSrcweir 
1055cdf0e10cSrcweir 	if (!(Direction == rtl_Cipher_DirectionInvalid))
1056cdf0e10cSrcweir 		pImpl->m_cipher.m_direction = Direction;
1057cdf0e10cSrcweir 	else
1058cdf0e10cSrcweir 		return rtl_Cipher_E_Direction;
1059cdf0e10cSrcweir 
1060cdf0e10cSrcweir 	return __rtl_cipherBF_init (
1061cdf0e10cSrcweir 		&(pImpl->m_context), pImpl->m_cipher.m_mode,
1062cdf0e10cSrcweir 		pKeyData, nKeyLen, pArgData, nArgLen);
1063cdf0e10cSrcweir }
1064cdf0e10cSrcweir 
1065cdf0e10cSrcweir /*
1066cdf0e10cSrcweir  * rtl_cipher_encodeBF.
1067cdf0e10cSrcweir  */
rtl_cipher_encodeBF(rtlCipher Cipher,const void * pData,sal_Size nDatLen,sal_uInt8 * pBuffer,sal_Size nBufLen)1068cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_encodeBF (
1069cdf0e10cSrcweir 	rtlCipher   Cipher,
1070cdf0e10cSrcweir 	const void *pData,   sal_Size nDatLen,
1071cdf0e10cSrcweir 	sal_uInt8  *pBuffer, sal_Size nBufLen)
1072cdf0e10cSrcweir {
1073cdf0e10cSrcweir 	CipherBF_Impl *pImpl = (CipherBF_Impl*)Cipher;
1074cdf0e10cSrcweir 	if (pImpl == NULL)
1075cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
1076cdf0e10cSrcweir 
1077cdf0e10cSrcweir 	if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmBF))
1078cdf0e10cSrcweir 		return rtl_Cipher_E_Algorithm;
1079cdf0e10cSrcweir 
1080cdf0e10cSrcweir 	if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionInvalid)
1081cdf0e10cSrcweir 		return rtl_Cipher_E_Direction;
1082cdf0e10cSrcweir 	if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionDecode)
1083cdf0e10cSrcweir 		return rtl_Cipher_E_Direction;
1084cdf0e10cSrcweir 
1085cdf0e10cSrcweir 	return __rtl_cipherBF_update (
1086cdf0e10cSrcweir 		&(pImpl->m_context), pImpl->m_cipher.m_mode,
1087cdf0e10cSrcweir 		rtl_Cipher_DirectionEncode,
1088cdf0e10cSrcweir 		(const sal_uInt8*)pData, nDatLen, pBuffer, nBufLen);
1089cdf0e10cSrcweir }
1090cdf0e10cSrcweir 
1091cdf0e10cSrcweir /*
1092cdf0e10cSrcweir  * rtl_cipher_decodeBF.
1093cdf0e10cSrcweir  */
rtl_cipher_decodeBF(rtlCipher Cipher,const void * pData,sal_Size nDatLen,sal_uInt8 * pBuffer,sal_Size nBufLen)1094cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_decodeBF (
1095cdf0e10cSrcweir 	rtlCipher   Cipher,
1096cdf0e10cSrcweir 	const void *pData,   sal_Size nDatLen,
1097cdf0e10cSrcweir 	sal_uInt8  *pBuffer, sal_Size nBufLen)
1098cdf0e10cSrcweir {
1099cdf0e10cSrcweir 	CipherBF_Impl *pImpl = (CipherBF_Impl*)Cipher;
1100cdf0e10cSrcweir 	if (pImpl == NULL)
1101cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
1102cdf0e10cSrcweir 
1103cdf0e10cSrcweir 	if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmBF))
1104cdf0e10cSrcweir 		return rtl_Cipher_E_Algorithm;
1105cdf0e10cSrcweir 
1106cdf0e10cSrcweir 	if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionInvalid)
1107cdf0e10cSrcweir 		return rtl_Cipher_E_Direction;
1108cdf0e10cSrcweir 	if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionEncode)
1109cdf0e10cSrcweir 		return rtl_Cipher_E_Direction;
1110cdf0e10cSrcweir 
1111cdf0e10cSrcweir 	return __rtl_cipherBF_update (
1112cdf0e10cSrcweir 		&(pImpl->m_context), pImpl->m_cipher.m_mode,
1113cdf0e10cSrcweir 		rtl_Cipher_DirectionDecode,
1114cdf0e10cSrcweir 		(const sal_uInt8*)pData, nDatLen, pBuffer, nBufLen);
1115cdf0e10cSrcweir }
1116cdf0e10cSrcweir 
1117cdf0e10cSrcweir /*
1118cdf0e10cSrcweir  * rtl_cipher_destroyBF.
1119cdf0e10cSrcweir  */
rtl_cipher_destroyBF(rtlCipher Cipher)1120cdf0e10cSrcweir void SAL_CALL rtl_cipher_destroyBF (rtlCipher Cipher)
1121cdf0e10cSrcweir {
1122cdf0e10cSrcweir 	CipherBF_Impl *pImpl = (CipherBF_Impl*)Cipher;
1123cdf0e10cSrcweir 	if (pImpl)
1124cdf0e10cSrcweir 	{
1125cdf0e10cSrcweir 		if (pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmBF)
1126cdf0e10cSrcweir 			rtl_freeZeroMemory (pImpl, sizeof (CipherBF_Impl));
1127cdf0e10cSrcweir 		else
1128cdf0e10cSrcweir 			rtl_freeMemory (pImpl);
1129cdf0e10cSrcweir 	}
1130cdf0e10cSrcweir }
1131cdf0e10cSrcweir 
1132cdf0e10cSrcweir /*========================================================================
1133cdf0e10cSrcweir  *
1134cdf0e10cSrcweir  * rtl_cipher_ARCFOUR (RC4) internals.
1135cdf0e10cSrcweir  *
1136cdf0e10cSrcweir  *======================================================================*/
1137cdf0e10cSrcweir #define CIPHER_CBLOCK_ARCFOUR 256
1138cdf0e10cSrcweir 
1139cdf0e10cSrcweir typedef struct cipherARCFOUR_context_st
1140cdf0e10cSrcweir {
1141cdf0e10cSrcweir 	unsigned int m_S[CIPHER_CBLOCK_ARCFOUR];
1142cdf0e10cSrcweir 	unsigned int m_X, m_Y;
1143cdf0e10cSrcweir } ContextARCFOUR_Impl;
1144cdf0e10cSrcweir 
1145cdf0e10cSrcweir typedef struct cipherARCFOUR_impl_st
1146cdf0e10cSrcweir {
1147cdf0e10cSrcweir 	Cipher_Impl         m_cipher;
1148cdf0e10cSrcweir 	ContextARCFOUR_Impl m_context;
1149cdf0e10cSrcweir } CipherARCFOUR_Impl;
1150cdf0e10cSrcweir 
1151cdf0e10cSrcweir /** rtl_cipherARCFOUR_update_Impl.
1152cdf0e10cSrcweir  */
1153cdf0e10cSrcweir static rtlCipherError rtl_cipherARCFOUR_update_Impl (
1154cdf0e10cSrcweir 	ContextARCFOUR_Impl *ctx,
1155cdf0e10cSrcweir 	const sal_uInt8     *pData,   sal_Size nDatLen,
1156cdf0e10cSrcweir 	sal_uInt8           *pBuffer, sal_Size nBufLen);
1157cdf0e10cSrcweir 
1158cdf0e10cSrcweir /*
1159cdf0e10cSrcweir  * rtl_cipherARCFOUR_init_Impl.
1160cdf0e10cSrcweir  */
rtl_cipherARCFOUR_init_Impl(ContextARCFOUR_Impl * ctx,const sal_uInt8 * pKeyData,sal_Size nKeyLen)1161cdf0e10cSrcweir static rtlCipherError rtl_cipherARCFOUR_init_Impl (
1162cdf0e10cSrcweir 	ContextARCFOUR_Impl *ctx,
1163cdf0e10cSrcweir 	const sal_uInt8     *pKeyData, sal_Size nKeyLen)
1164cdf0e10cSrcweir {
1165cdf0e10cSrcweir 	unsigned int  K[CIPHER_CBLOCK_ARCFOUR];
1166cdf0e10cSrcweir 	unsigned int *L, *S;
1167cdf0e10cSrcweir 	unsigned int  x, y, t;
1168cdf0e10cSrcweir 	sal_Size      n, k;
1169cdf0e10cSrcweir 
1170cdf0e10cSrcweir 	S = &(ctx->m_S[0]);
1171cdf0e10cSrcweir 
1172cdf0e10cSrcweir 	/* Initialize S linearly. */
1173cdf0e10cSrcweir 	for (x = 0; x < CIPHER_CBLOCK_ARCFOUR; x++)
1174cdf0e10cSrcweir 		S[x] = x;
1175cdf0e10cSrcweir 
1176cdf0e10cSrcweir 	/* Initialize K with key, repeat key as necessary. */
1177cdf0e10cSrcweir 	for (L = K, n = CIPHER_CBLOCK_ARCFOUR; n > nKeyLen; n -= nKeyLen)
1178cdf0e10cSrcweir 	{
1179cdf0e10cSrcweir 		for (k = 0; k < nKeyLen; k++) L[k] = pKeyData[k];
1180cdf0e10cSrcweir 		L += nKeyLen;
1181cdf0e10cSrcweir 	}
1182cdf0e10cSrcweir 	for (k = 0; k < n; k++) L[k] = pKeyData[k];
1183cdf0e10cSrcweir 
1184cdf0e10cSrcweir 	/* Initialize S with K. */
1185cdf0e10cSrcweir 	for (x = 0, y = 0; x < CIPHER_CBLOCK_ARCFOUR; x++)
1186cdf0e10cSrcweir 	{
1187cdf0e10cSrcweir 		y = (y + S[x] + K[x]) % CIPHER_CBLOCK_ARCFOUR;
1188cdf0e10cSrcweir 		t = S[x], S[x] = S[y], S[y] = t; /* swap S[x] and S[y] */
1189cdf0e10cSrcweir 	}
1190cdf0e10cSrcweir 
1191cdf0e10cSrcweir 	/* Initialize counters X and Y. */
1192cdf0e10cSrcweir 	ctx->m_X = 0;
1193cdf0e10cSrcweir 	ctx->m_Y = 0;
1194cdf0e10cSrcweir 
1195cdf0e10cSrcweir 	return rtl_Cipher_E_None;
1196cdf0e10cSrcweir }
1197cdf0e10cSrcweir 
1198cdf0e10cSrcweir /*
1199cdf0e10cSrcweir  * rtl_cipherARCFOUR_update_Impl.
1200cdf0e10cSrcweir  */
rtl_cipherARCFOUR_update_Impl(ContextARCFOUR_Impl * ctx,const sal_uInt8 * pData,sal_Size nDatLen,sal_uInt8 * pBuffer,sal_Size nBufLen)1201cdf0e10cSrcweir static rtlCipherError rtl_cipherARCFOUR_update_Impl (
1202cdf0e10cSrcweir 	ContextARCFOUR_Impl *ctx,
1203cdf0e10cSrcweir 	const sal_uInt8     *pData,   sal_Size nDatLen,
1204cdf0e10cSrcweir 	sal_uInt8           *pBuffer, sal_Size nBufLen)
1205cdf0e10cSrcweir {
1206cdf0e10cSrcweir 	register unsigned int *S;
1207cdf0e10cSrcweir 	register unsigned int  x, y, t;
1208cdf0e10cSrcweir 	sal_Size               k;
1209cdf0e10cSrcweir 
1210cdf0e10cSrcweir 	/* Check arguments. */
1211cdf0e10cSrcweir 	if ((pData == NULL) || (pBuffer == NULL))
1212cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
1213cdf0e10cSrcweir 
1214cdf0e10cSrcweir 	if (!((0 < nDatLen) && (nDatLen <= nBufLen)))
1215cdf0e10cSrcweir 		return rtl_Cipher_E_BufferSize;
1216cdf0e10cSrcweir 
1217cdf0e10cSrcweir 	/* Update. */
1218cdf0e10cSrcweir 	S = &(ctx->m_S[0]);
1219cdf0e10cSrcweir 	for (k = 0; k < nDatLen; k++)
1220cdf0e10cSrcweir 	{
1221cdf0e10cSrcweir 		/* Update counters X and Y. */
1222cdf0e10cSrcweir 		x = ctx->m_X;
1223cdf0e10cSrcweir 		y = ctx->m_Y;
1224cdf0e10cSrcweir 		x = (x + 1   ) % CIPHER_CBLOCK_ARCFOUR;
1225cdf0e10cSrcweir 		y = (y + S[x]) % CIPHER_CBLOCK_ARCFOUR;
1226cdf0e10cSrcweir 		ctx->m_X = x;
1227cdf0e10cSrcweir 		ctx->m_Y = y;
1228cdf0e10cSrcweir 
1229cdf0e10cSrcweir 		/* Swap S[x] and S[y]. */
1230cdf0e10cSrcweir 		t = S[x], S[x] = S[y], S[y] = t;
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir 		/* Evaluate next key byte S[t]. */
1233cdf0e10cSrcweir 		t = (S[x] + S[y]) % CIPHER_CBLOCK_ARCFOUR;
1234cdf0e10cSrcweir 		pBuffer[k] = pData[k] ^ ((sal_uInt8)(S[t] & 0xff));
1235cdf0e10cSrcweir 	}
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir 	return rtl_Cipher_E_None;
1238cdf0e10cSrcweir }
1239cdf0e10cSrcweir 
1240cdf0e10cSrcweir /*========================================================================
1241cdf0e10cSrcweir  *
1242cdf0e10cSrcweir  * rtl_cipher_ARCFOUR (RC4) implementation.
1243cdf0e10cSrcweir  *
1244cdf0e10cSrcweir  * Reference:
1245cdf0e10cSrcweir  *   Bruce Schneier: Applied Cryptography, 2nd edition, ch. 17.1
1246cdf0e10cSrcweir  *
1247cdf0e10cSrcweir  *======================================================================*/
1248cdf0e10cSrcweir /*
1249cdf0e10cSrcweir  * rtl_cipher_createARCFOUR.
1250cdf0e10cSrcweir  */
rtl_cipher_createARCFOUR(rtlCipherMode Mode)1251cdf0e10cSrcweir rtlCipher SAL_CALL rtl_cipher_createARCFOUR (rtlCipherMode Mode)
1252cdf0e10cSrcweir {
1253cdf0e10cSrcweir 	CipherARCFOUR_Impl *pImpl = (CipherARCFOUR_Impl*)NULL;
1254cdf0e10cSrcweir 
1255cdf0e10cSrcweir 	if (!(Mode == rtl_Cipher_ModeStream))
1256cdf0e10cSrcweir 		return ((rtlCipher)NULL);
1257cdf0e10cSrcweir 
1258cdf0e10cSrcweir 	pImpl = ((CipherARCFOUR_Impl*)rtl_allocateZeroMemory (sizeof (CipherARCFOUR_Impl)));
1259cdf0e10cSrcweir 	if (pImpl)
1260cdf0e10cSrcweir 	{
1261cdf0e10cSrcweir 		pImpl->m_cipher.m_algorithm = rtl_Cipher_AlgorithmARCFOUR;
1262cdf0e10cSrcweir 		pImpl->m_cipher.m_direction = rtl_Cipher_DirectionInvalid;
1263cdf0e10cSrcweir 		pImpl->m_cipher.m_mode      = rtl_Cipher_ModeStream;
1264cdf0e10cSrcweir 
1265cdf0e10cSrcweir 		pImpl->m_cipher.m_init      = rtl_cipher_initARCFOUR;
1266cdf0e10cSrcweir 		pImpl->m_cipher.m_encode    = rtl_cipher_encodeARCFOUR;
1267cdf0e10cSrcweir 		pImpl->m_cipher.m_decode    = rtl_cipher_decodeARCFOUR;
1268cdf0e10cSrcweir 		pImpl->m_cipher.m_delete    = rtl_cipher_destroyARCFOUR;
1269cdf0e10cSrcweir 	}
1270cdf0e10cSrcweir 	return ((rtlCipher)pImpl);
1271cdf0e10cSrcweir }
1272cdf0e10cSrcweir 
1273cdf0e10cSrcweir /*
1274cdf0e10cSrcweir  * rtl_cipher_initARCFOUR.
1275cdf0e10cSrcweir  */
rtl_cipher_initARCFOUR(rtlCipher Cipher,rtlCipherDirection Direction,const sal_uInt8 * pKeyData,sal_Size nKeyLen,const sal_uInt8 * pArgData,sal_Size nArgLen)1276cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_initARCFOUR (
1277cdf0e10cSrcweir 	rtlCipher          Cipher,
1278cdf0e10cSrcweir 	rtlCipherDirection Direction,
1279cdf0e10cSrcweir 	const sal_uInt8 *pKeyData, sal_Size nKeyLen,
1280cdf0e10cSrcweir 	const sal_uInt8 *pArgData, sal_Size nArgLen)
1281cdf0e10cSrcweir {
1282cdf0e10cSrcweir 	CipherARCFOUR_Impl *pImpl = (CipherARCFOUR_Impl*)Cipher;
1283cdf0e10cSrcweir     (void) pArgData; // avoid warning
1284cdf0e10cSrcweir     (void) nArgLen; // avoid warning
1285cdf0e10cSrcweir 
1286cdf0e10cSrcweir 	if ((pImpl == NULL) || (pKeyData == NULL))
1287cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
1288cdf0e10cSrcweir 
1289cdf0e10cSrcweir 	if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmARCFOUR))
1290cdf0e10cSrcweir 		return rtl_Cipher_E_Algorithm;
1291cdf0e10cSrcweir 
1292cdf0e10cSrcweir 	if (!(Direction == rtl_Cipher_DirectionInvalid))
1293cdf0e10cSrcweir 		pImpl->m_cipher.m_direction = Direction;
1294cdf0e10cSrcweir 	else
1295cdf0e10cSrcweir 		return rtl_Cipher_E_Direction;
1296cdf0e10cSrcweir 
1297cdf0e10cSrcweir 	return rtl_cipherARCFOUR_init_Impl (&(pImpl->m_context), pKeyData, nKeyLen);
1298cdf0e10cSrcweir }
1299cdf0e10cSrcweir 
1300cdf0e10cSrcweir /*
1301cdf0e10cSrcweir  * rtl_cipher_encodeARCFOUR.
1302cdf0e10cSrcweir  */
rtl_cipher_encodeARCFOUR(rtlCipher Cipher,const void * pData,sal_Size nDatLen,sal_uInt8 * pBuffer,sal_Size nBufLen)1303cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_encodeARCFOUR (
1304cdf0e10cSrcweir 	rtlCipher   Cipher,
1305cdf0e10cSrcweir 	const void *pData,   sal_Size nDatLen,
1306cdf0e10cSrcweir 	sal_uInt8  *pBuffer, sal_Size nBufLen)
1307cdf0e10cSrcweir {
1308cdf0e10cSrcweir 	CipherARCFOUR_Impl *pImpl = (CipherARCFOUR_Impl*)Cipher;
1309cdf0e10cSrcweir 	if (pImpl == NULL)
1310cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
1311cdf0e10cSrcweir 
1312cdf0e10cSrcweir 	if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmARCFOUR))
1313cdf0e10cSrcweir 		return rtl_Cipher_E_Algorithm;
1314cdf0e10cSrcweir 
1315cdf0e10cSrcweir 	if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionInvalid)
1316cdf0e10cSrcweir 		return rtl_Cipher_E_Direction;
1317cdf0e10cSrcweir 
1318cdf0e10cSrcweir 	return rtl_cipherARCFOUR_update_Impl (
1319cdf0e10cSrcweir 		&(pImpl->m_context),
1320cdf0e10cSrcweir 		(const sal_uInt8*)pData, nDatLen, pBuffer, nBufLen);
1321cdf0e10cSrcweir }
1322cdf0e10cSrcweir 
1323cdf0e10cSrcweir /*
1324cdf0e10cSrcweir  * rtl_cipher_decodeARCFOUR.
1325cdf0e10cSrcweir  */
rtl_cipher_decodeARCFOUR(rtlCipher Cipher,const void * pData,sal_Size nDatLen,sal_uInt8 * pBuffer,sal_Size nBufLen)1326cdf0e10cSrcweir rtlCipherError SAL_CALL rtl_cipher_decodeARCFOUR (
1327cdf0e10cSrcweir 	rtlCipher   Cipher,
1328cdf0e10cSrcweir 	const void *pData,   sal_Size nDatLen,
1329cdf0e10cSrcweir 	sal_uInt8  *pBuffer, sal_Size nBufLen)
1330cdf0e10cSrcweir {
1331cdf0e10cSrcweir 	CipherARCFOUR_Impl *pImpl = (CipherARCFOUR_Impl*)Cipher;
1332cdf0e10cSrcweir 	if (pImpl == NULL)
1333cdf0e10cSrcweir 		return rtl_Cipher_E_Argument;
1334cdf0e10cSrcweir 
1335cdf0e10cSrcweir 	if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmARCFOUR))
1336cdf0e10cSrcweir 		return rtl_Cipher_E_Algorithm;
1337cdf0e10cSrcweir 
1338cdf0e10cSrcweir 	if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionInvalid)
1339cdf0e10cSrcweir 		return rtl_Cipher_E_Direction;
1340cdf0e10cSrcweir 
1341cdf0e10cSrcweir 	return rtl_cipherARCFOUR_update_Impl (
1342cdf0e10cSrcweir 		&(pImpl->m_context),
1343cdf0e10cSrcweir 		(const sal_uInt8*)pData, nDatLen, pBuffer, nBufLen);
1344cdf0e10cSrcweir }
1345cdf0e10cSrcweir 
1346cdf0e10cSrcweir /*
1347cdf0e10cSrcweir  * rtl_cipher_destroyARCFOUR.
1348cdf0e10cSrcweir  */
rtl_cipher_destroyARCFOUR(rtlCipher Cipher)1349cdf0e10cSrcweir void SAL_CALL rtl_cipher_destroyARCFOUR (rtlCipher Cipher)
1350cdf0e10cSrcweir {
1351cdf0e10cSrcweir 	CipherARCFOUR_Impl *pImpl = (CipherARCFOUR_Impl*)Cipher;
1352cdf0e10cSrcweir 	if (pImpl)
1353cdf0e10cSrcweir 	{
1354cdf0e10cSrcweir 		if (pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmARCFOUR)
1355cdf0e10cSrcweir 			rtl_freeZeroMemory (pImpl, sizeof (CipherARCFOUR_Impl));
1356cdf0e10cSrcweir 		else
1357cdf0e10cSrcweir 			rtl_freeMemory (pImpl);
1358cdf0e10cSrcweir 	}
1359cdf0e10cSrcweir }
1360