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