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