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