#include #include #include #include #include "ippcp.h" void AES_sample(void); void dump(char * tittle, unsigned char * buffer, short len); int main(int argc, char * argv[]) { AES_sample(); return 0; } void dump(char * tittle, unsigned char * buffer, short len) { short i; short j; char * hexa_output; char * ascii_output; hexa_output = (char*) calloc(len*3+2,sizeof(unsigned char)); ascii_output = (char*) calloc(len*2+2,sizeof(unsigned char)); if(tittle) { printf(" // \\\\ %s // \\\\ \n",tittle); } printf(" ========================================== DUMP len (%04d) ======================================= \n",len); printf("HEXA Output:\n"); for(i=0,j=0; i < len;) { // printf("%02x ",buffer[i]); snprintf(hexa_output+j,4,"%02x ",buffer[i]); i++; j += 3; } printf("%s\n",hexa_output); printf("ASCII Output:\n"); for(i=0; i < len;) { if( isascii(buffer[i]) && isprint(buffer[i]) ) { snprintf(ascii_output+i,2,"%c",buffer[i]); } else { snprintf(ascii_output+i,2,"%c",'.'); } i++; } printf("%s\n",ascii_output); printf(" ================================================================================================== \n"); free(hexa_output); free(ascii_output); } void AES_sample(void) { // size of Rijndael-128 algorithm block is equal to 16 const int aesBlkSize = 16; // get the size of the context needed for the encryption/decryption operation int ctxSize; ippsRijndael128GetSize(&ctxSize); // and allocate one IppsRijndael128Spec* pCtx = (IppsRijndael128Spec*)( new Ipp8u [ctxSize] ); // define the key Ipp8u key[aesBlkSize] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15}; // and prepare the context for Rijndael128 usage ippsRijndael128Init(key,IppsRijndaelKey128, pCtx); // define the message to be encrypted Ipp8u ptext[] = {"quick brown fox jupm over lazy dog"}; // define an initial vector Ipp8u crt0[aesBlkSize] = {0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00}; Ipp8u crt[aesBlkSize]; // counter the variable number of bits int ctrNumBitSize = 64; // allocate enough memory for the ciphertext // note that // the size of the ciphertext is always equal to that of the planetext Ipp8u ctext[sizeof(ptext)]; // init the counter memcpy(crt, crt0, sizeof(crt0)); // encrypt (CTR mode) ptext message // pay attention to the 'length' parameter // it defines the number of bytes to be encrypted memcpy(crt, crt0, sizeof(crt0)); dump("Plain text before crypt ...", ptext, sizeof(ptext)); ippsRijndael128EncryptCTR(ptext, ctext, sizeof(ptext), pCtx, crt, ctrNumBitSize); // allocate memory for the decrypted message Ipp8u rtext[sizeof(ptext)]; // init the counter memcpy(crt, crt0, sizeof(crt0)); // decrypt (ECTR mode) ctext message dump("Cipher text after encrypt ...", ctext, sizeof(ptext)); // pay attention to the 'length' parameter // it defines the number of bytes to be decrypted ippsRijndael128DecryptCTR(ctext, rtext, sizeof(ptext), pCtx, crt, ctrNumBitSize); dump("Plain text before crypt ...", rtext, sizeof(ptext)); delete (Ipp8u*)pCtx; }