Disclaimer THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. Author: Hector Ho Fuentes e-mail: hhofuent@ccs.neu.edu Date of DESLib.prc: May 7, 2001 Version: 0.1 Modified by Duncan S. Wong on Oct 30, 2001 If you have any suggestions please write to the author. Description: DESLib is a Palm OS System Library that implements DES, DESX and TripleDES To use this Palm Library, you need to: 1. Download the DESLib.prc 2. Download the DESLib.h 3. Only include the DESLib.h in your project. How to use the DESLib: 1. First you need to open the Library. The sample code is as follows: err = SysLibFind("DESLibrary", &DESLibRefNum); if (err) err = SysLibLoad('libr', 'HDES', &DESLibRefNum); ErrFatalDisplayIf(err, "Cannot load DES Library"); err = DESOpen(DESLibRefNum); where DESLibRefNum is a static UInt16, used to store where is the DESLibrary. We sugest that you put this code in your StartApplication() function, or in the function you use to initialize your application. 2. How to use DES? First we need to declare a variable (e.g. key_ctx) of the following type to store some DES information. DES_CTX key_ctx; Then we initialize the parameters in this context which is given in Sect. 3. In Sect. 4, we will see how to use the library to do encryption and decryption. And finally in Sect. 5, we will learn how to close the library. 3. How to Initialize DES? This is done via the function: DESInitialize(UInt16 refNum, unsigned char *keystring, unsigned char *iv, int desmode, int destype, int encrypt, DES_CTX *key_ctx) It has the following parameters: UInt16 refNum: the reference number of the library unsigned char *keystring: a binary string of the key. unsigned char *iv: the Initialization Vector (If used). int desmode: This is which DES Mode you want to use. They are: ECB CBC CFB OFBISO OFBFIPS81 int destype: This tells which variant of DES you want to use: DES DESX DES3 int encrypt: There are two modes: ENCRYPT: To encrypt. DECRYPT: To decrypt. DES_CTX *key_ctx: Where this information is to be stored. Also, if you plan to use either CFB, OFBISO or OFBFIPS81 you need to call: DESInitializeFB(UInt16 refNum, int shift, DES_CTX *key_ctx) This is to set the number of bits you want to shift and the size of block to be encrypted/decrypted. The parameters are: UInt16 refNum: The reference number for the DES Library. int shift: The number of bits to shift: 8, 16, 32 or 64. Anything else will cause error. DES_CTX *key_ctx: The variable for the context for DES. 4. How to Encrypt/Decrypt? Use DESEncrypt and DESDecrypt to encrypt and decrypt respectively. Please note that the DES context has to be initialized (see Sect. 3) everytime before calling the encryption or decryption function because key scheduling is done in the initialization function. Encryption ---------- DESEncrypt(UInt16 refNum, DES_CTX *key_ctx, unsigned char *in, unsigned char *out, unsigned long size) The parameters are as follows: UInt16 refNum: The reference number of this library. Obtain when you open the library. DES_CTX *key_ctx: the context information about DES unsigned char *in: a pointer to the input string unsigned char *out: a pointer to where the output should be written. (The user has to allocate this memory.) unsigned long size: This is the size of the data to be encrypted. This size is denoted in number of bytes to be encrypted. Decryption ---------- DESDecrypt(UInt16 refNum, DES_CTX *key_ctx, unsigned char *in, unsigned char *out, unsigned long size) and the parameters are the same. NOTE: The user is responsible in deallocating any memory created for the input and output strings. 5. How to Close the Library? You can close the library when you finished. This is accomplished via: error = DESClose(DESLibRefNum, &numapps); // check for erros in the Close() routine if (numapps == 0) SysLibRemove(DESLibRefNum); We suggest you include this code in the StopApplication() function or your function used to handle the case of closing the application.