Example of Using Twofish Functions

Twofish Encryption and Decryption 

// use of the FCB mode
void TF_sample(void){
   // size of the Twofish algorithm block is equal to 16
   const int tfBlkSize = 16;
 
   // get the size of the context needed for the encryption/decryption operation
   int ctxSize;
   ippsTwofishGetSize(&ctxSize);
 
   // and allocate one
   IppsTwofishSpec* pCtx = (IppsTwofishSpec*)( new Ipp8u [ctxSize] );
 
   // define the key
   // note that the key length may vary from 16 to 32 bytes
   Ipp8u key[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
                  0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,
                  0x16,0x17};
 
   // and prepare the context for Twofish usage
   ippsTwofishInit(key,sizeof(key), pCtx);
 
   // define the message to be encrypted
   Ipp8u ptext[] = {"quick brown fox jupm over lazy dog"};
 
   // fcb value (bytes)
   const int cfbBlkSize = 2;
 
 
   // define an initial vector
   Ipp8u iv[tfBlkSize] = {0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,
                          0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00}; 
 
   // allocate enough memory for the ciphertext
   // note that
   // the size of the ciphertext is always multiple of the cfbBlkSize size
   Ipp8u ctext[(sizeof(ptext)+cfbBlkSize-1) &~(cfbBlkSize-1)];
 
   // encrypt (CFB mode) ptext message
   // pay attention to the 'length' parameter
   // it defines the number of bytes to be encrypted
   ippsTwofishEncryptCFB(ptext, ctext, sizeof(ptext), cfbBlkSize,
                         pCtx,
                         iv,
                         IppsCPPaddingPKCS7);
   // allocate memory for the decrypted message
   Ipp8u rtext[sizeof(ptext)];
   // decrypt (CFB mode) ctext message
		
 
 
// pay attention to the 'length' parameter
   // it defines the number of bytes to be decrypted
ippsTwofishDecryptCFB(ctext, rtext, sizeof(ptext), cfbBlkSize,
                         pCtx,
                         iv,
                         IppsCPPaddingPKCS7);
   delete (Ipp8u*)pCtx;
}
 

Submit feedback on this help topic

Copyright © 2000 - 2011, Intel Corporation. All rights reserved.