xref: /aoo42x/main/sal/inc/rtl/cipher.h (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _RTL_CIPHER_H_
29 #define _RTL_CIPHER_H_ "$Revision: 1.7 $"
30 
31 #include <sal/types.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /*========================================================================
38  *
39  * rtlCipher interface.
40  *
41  *======================================================================*/
42 /** Cipher Handle opaque type.
43  */
44 typedef void* rtlCipher;
45 
46 
47 /** Cipher Algorithm enumeration.
48 	@see rtl_cipher_create()
49  */
50 enum __rtl_CipherAlgorithm
51 {
52 	rtl_Cipher_AlgorithmBF,
53 	rtl_Cipher_AlgorithmARCFOUR,
54 	rtl_Cipher_AlgorithmInvalid,
55 	rtl_Cipher_Algorithm_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
56 };
57 
58 /** Cipher Algorithm type.
59  */
60 typedef enum __rtl_CipherAlgorithm rtlCipherAlgorithm;
61 
62 
63 /** Cipher Mode enumeration.
64 	@see rtl_cipher_create()
65  */
66 enum __rtl_CipherMode
67 {
68 	rtl_Cipher_ModeECB,
69 	rtl_Cipher_ModeCBC,
70 	rtl_Cipher_ModeStream,
71 	rtl_Cipher_ModeInvalid,
72 	rtl_Cipher_Mode_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
73 };
74 
75 /** Cipher Mode type.
76  */
77 typedef enum __rtl_CipherMode rtlCipherMode;
78 
79 
80 /** Cipher Direction enumeration.
81 	@see rtl_cipher_init()
82  */
83 enum __rtl_CipherDirection
84 {
85 	rtl_Cipher_DirectionBoth,
86 	rtl_Cipher_DirectionDecode,
87 	rtl_Cipher_DirectionEncode,
88 	rtl_Cipher_DirectionInvalid,
89 	rtl_Cipher_Direction_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
90 };
91 
92 /** Cipher Direction type.
93  */
94 typedef enum __rtl_CipherDirection rtlCipherDirection;
95 
96 
97 /** Error Code enumeration.
98  */
99 enum __rtl_CipherError
100 {
101 	rtl_Cipher_E_None,
102 	rtl_Cipher_E_Argument,
103 	rtl_Cipher_E_Algorithm,
104 	rtl_Cipher_E_Direction,
105 	rtl_Cipher_E_Mode,
106 	rtl_Cipher_E_BufferSize,
107 	rtl_Cipher_E_Memory,
108 	rtl_Cipher_E_Unknown,
109 	rtl_Cipher_E_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
110 };
111 
112 /** Error Code type.
113  */
114 typedef enum __rtl_CipherError rtlCipherError;
115 
116 
117 /** Create a cipher handle for the given algorithm and mode.
118     @see rtlCipherAlgorithm
119     @see rtlCipherMode
120 
121 	@param  Algorithm [in] cipher algorithm.
122 	@param  Mode      [in] cipher mode.
123 	@return Cipher handle, or 0 upon failure.
124  */
125 rtlCipher SAL_CALL rtl_cipher_create (
126 	rtlCipherAlgorithm Algorithm,
127 	rtlCipherMode      Mode
128 ) SAL_THROW_EXTERN_C();
129 
130 
131 /** Inititialize a cipher for the given direction.
132     @see rtlCipherDirection
133 
134 	@param  Cipher    [in] cipher handle.
135 	@param  Direction [in] cipher direction.
136 	@param  pKeyData  [in] key material buffer.
137 	@param  nKeyLen   [in] key material length in bytes.
138 	@param  pArgData  [in] initialization vector buffer.
139 	@param  nArgLen   [in] initialization vector length in bytes.
140 	@return rtl_Cipher_E_None upon success.
141  */
142 rtlCipherError SAL_CALL rtl_cipher_init (
143 	rtlCipher           Cipher,
144 	rtlCipherDirection  Direction,
145 	const sal_uInt8    *pKeyData, sal_Size nKeyLen,
146 	const sal_uInt8    *pArgData, sal_Size nArgLen
147 ) SAL_THROW_EXTERN_C();
148 
149 
150 /** Encode a buffer under a given cipher algorithm.
151     @precond Initialized for a compatible cipher direction.
152 	@see     rtl_cipher_init()
153 
154 	@param  Cipher  [in]  cipher handle.
155 	@param  pData   [in]  plaintext buffer.
156 	@param  nDatLen [in]  plaintext length in bytes.
157 	@param  pBuffer [out] ciphertext buffer.
158 	@param  nBufLen [in]  ciphertext length in bytes.
159 	@return rtl_Cipher_E_None upon success.
160  */
161 rtlCipherError SAL_CALL rtl_cipher_encode (
162 	rtlCipher   Cipher,
163 	const void *pData,   sal_Size nDatLen,
164 	sal_uInt8  *pBuffer, sal_Size nBufLen
165 ) SAL_THROW_EXTERN_C();
166 
167 
168 /** Decode a buffer under a given cipher algorithm.
169     @precond Initialized for a compatible cipher direction.
170 	@see     rtl_cipher_init()
171 
172 	@param  Cipher  [in]  cipher handle.
173 	@param  pData   [in]  ciphertext buffer.
174 	@param  nDatLen [in]  ciphertext length in bytes.
175 	@param  pBuffer [out] plaintext buffer.
176 	@param  nBufLen [in]  plaintext length in bytes.
177 	@return rtl_Cipher_E_None upon success.
178  */
179 rtlCipherError SAL_CALL rtl_cipher_decode (
180 	rtlCipher   Cipher,
181 	const void *pData,   sal_Size nDatLen,
182 	sal_uInt8  *pBuffer, sal_Size nBufLen
183 ) SAL_THROW_EXTERN_C();
184 
185 
186 /** Destroy a cipher handle.
187 	@param  Cipher [in] cipher handle to be destroyed.
188 	@return None. Cipher handle destroyed and invalid.
189  */
190 void SAL_CALL rtl_cipher_destroy (
191 	rtlCipher Cipher
192 ) SAL_THROW_EXTERN_C();
193 
194 
195 /*========================================================================
196  *
197  * rtl_cipherBF (Blowfish) interface.
198  *
199  *======================================================================*/
200 /** Create a Blowfish cipher handle for the given mode.
201     @descr The Blowfish block cipher algorithm is specified in
202 	Bruce Schneier: Applied Cryptography, 2nd edition, ch. 14.3
203 
204 	@see rtl_cipher_create()
205  */
206 rtlCipher SAL_CALL rtl_cipher_createBF (
207 	rtlCipherMode Mode
208 ) SAL_THROW_EXTERN_C();
209 
210 
211 /** Inititialize a Blowfish cipher for the given direction.
212 	@see rtl_cipher_init()
213  */
214 rtlCipherError SAL_CALL rtl_cipher_initBF (
215 	rtlCipher          Cipher,
216 	rtlCipherDirection Direction,
217 	const sal_uInt8 *pKeyData, sal_Size nKeyLen,
218 	const sal_uInt8 *pArgData, sal_Size nArgLen
219 ) SAL_THROW_EXTERN_C();
220 
221 
222 /** Encode a buffer under the Blowfish cipher algorithm.
223 	@see rtl_cipher_encode()
224  */
225 rtlCipherError SAL_CALL rtl_cipher_encodeBF (
226 	rtlCipher   Cipher,
227 	const void *pData,   sal_Size nDatLen,
228 	sal_uInt8  *pBuffer, sal_Size nBufLen
229 ) SAL_THROW_EXTERN_C();
230 
231 
232 /** Decode a buffer under the Blowfish cipher algorithm.
233 	@see rtl_cipher_decode()
234  */
235 rtlCipherError SAL_CALL rtl_cipher_decodeBF (
236 	rtlCipher   Cipher,
237 	const void *pData,   sal_Size nDatLen,
238 	sal_uInt8  *pBuffer, sal_Size nBufLen
239 ) SAL_THROW_EXTERN_C();
240 
241 
242 /** Destroy a Blowfish cipher handle.
243 	@see rtl_cipher_destroy()
244  */
245 void SAL_CALL rtl_cipher_destroyBF (
246 	rtlCipher Cipher
247 ) SAL_THROW_EXTERN_C();
248 
249 
250 /*========================================================================
251  *
252  * rtl_cipherARCFOUR (RC4) interface.
253  *
254  *======================================================================*/
255 /** Create a RC4 cipher handle for the given mode.
256     @descr The RC4 symmetric stream cipher algorithm is specified in
257 	Bruce Schneier: Applied Cryptography, 2nd edition, ch. 17.1
258 
259 	@see rtl_cipher_create()
260 
261 	@param  Mode [in] cipher mode. Must be rtl_Cipher_ModeStream.
262 	@return Cipher handle, or 0 upon failure.
263  */
264 rtlCipher SAL_CALL rtl_cipher_createARCFOUR (
265 	rtlCipherMode Mode
266 ) SAL_THROW_EXTERN_C();
267 
268 
269 /** Inititialize a RC4 cipher for the given direction.
270 	@see rtl_cipher_init()
271  */
272 rtlCipherError SAL_CALL rtl_cipher_initARCFOUR (
273 	rtlCipher          Cipher,
274 	rtlCipherDirection Direction,
275 	const sal_uInt8 *pKeyData, sal_Size nKeyLen,
276 	const sal_uInt8 *pArgData, sal_Size nArgLen
277 ) SAL_THROW_EXTERN_C();
278 
279 
280 /** Encode a buffer under the RC4 cipher algorithm.
281 	@see rtl_cipher_encode()
282  */
283 rtlCipherError SAL_CALL rtl_cipher_encodeARCFOUR (
284 	rtlCipher   Cipher,
285 	const void *pData,   sal_Size nDatLen,
286 	sal_uInt8  *pBuffer, sal_Size nBufLen
287 ) SAL_THROW_EXTERN_C();
288 
289 
290 /** Decode a buffer under the RC4 cipher algorithm.
291 	@see rtl_cipher_decode()
292  */
293 rtlCipherError SAL_CALL rtl_cipher_decodeARCFOUR (
294 	rtlCipher   Cipher,
295 	const void *pData,   sal_Size nDatLen,
296 	sal_uInt8  *pBuffer, sal_Size nBufLen
297 ) SAL_THROW_EXTERN_C();
298 
299 
300 /** Destroy a RC4 cipher handle.
301 	@see rtl_cipher_destroy()
302  */
303 void SAL_CALL rtl_cipher_destroyARCFOUR (
304 	rtlCipher Cipher
305 ) SAL_THROW_EXTERN_C();
306 
307 
308 /*========================================================================
309  *
310  * The End.
311  *
312  *======================================================================*/
313 
314 #ifdef __cplusplus
315 }
316 #endif
317 
318 #endif /* !_RTL_CIPHER_H_ */
319 
320