thig_ims_enc.c

Go to the documentation of this file.
00001 
00054 #include "thig_ims_enc.h"
00055 #include <stdio.h>
00056 #include <stdlib.h>
00057 #include <string.h>
00058 #include <time.h>
00059 #include "../../mem/mem.h"
00060 #include "base64.h"
00061 
00063 #define USE_BASE64 1 
00064 
00065 keyInstance ki;
00066 cipherInstance ci;
00067 
00068 
00069 DWORD Here(DWORD x){
00070     unsigned int mask=~0U;
00071 
00072     return (* (((DWORD *)&x)-1)) & mask;
00073 }
00074 
00075 int keySize     =256;       
00076 int mode        =MODE_CFB1; 
00077 int byteCnt     =BLOCK_SIZE/8;  
00078 
00086 void printstr(char* text,str x)
00087 {
00088     int i;
00089     if(text!=NULL)
00090         printf("%s 0x",text);
00091     for(i=0;i<x.len;i++)
00092         printf("%.02x", (unsigned char)x.s[i]);
00093     printf("\n");
00094 }
00095 
00103 int thig_key_and_cipher_init(keyInstance *ki , cipherInstance *ci){
00104     int i;
00105     BYTE iv[BLOCK_SIZE/8];
00106     if (makeKey(ki,DIR_ENCRYPT,keySize,NULL) != TRUE)
00107         return 1;               /* 'dummy' setup for a 128-bit key */
00108     if (cipherInit(ci,mode,NULL) != TRUE)
00109         return 1;               /* 'dummy' setup for cipher */
00110     
00111     /* choose a key */
00112     for (i=0;i<keySize/32;i++)  
00113         (*ki).key32[i]=0x10003 * rand();
00114     reKey(ki);                  /* run the key schedule */
00115     
00116     /* set the initialization vector */
00117     for (i=0;i<sizeof(iv);i++)
00118             iv[i]=(BYTE) rand();
00119     memcpy((*ci).iv32,iv,sizeof((*ci).iv32));   /* copy the IV to ci */
00120     return 1;
00121 }
00122 
00123 
00124 
00125 
00126 
00133 str thig_encrypt(str src)
00134 {
00135     int padding;
00136     str my_text={0,0},enc_text={0,0};
00137     cipherInstance ci2 = ci;
00138     str encoded={0,0};
00139     
00140     padding = (src.len%byteCnt)==0?0:byteCnt-(src.len%byteCnt);
00141     LOG(L_ERR,"DBG:"M_NAME":encrypt:String has length %d so needs padding %d\n",src.len,padding);
00142 
00143     my_text.len = src.len+padding;
00144     my_text.s = pkg_malloc(my_text.len);
00145     if (!my_text.s){
00146         LOG(L_ERR,"ERR:"M_NAME":encrypt: error allocating %d bytes\n",my_text.len);
00147         goto error;
00148     }   
00149     memcpy(my_text.s,src.s,src.len);
00150     memset(my_text.s+src.len,0,my_text.len-src.len);
00151     
00152     enc_text.s = pkg_malloc(my_text.len);
00153     if (!enc_text.s){
00154         LOG(L_ERR,"ERR:"M_NAME":encrypt: error allocating %d bytes\n",my_text.len);
00155         goto error;     
00156     }
00157     enc_text.len = my_text.len;
00158 
00159     printstr("String bef :",my_text);
00160     if (blockEncrypt(&ci2,&ki,(unsigned char*)my_text.s,my_text.len*8,(unsigned char*)enc_text.s) != my_text.len*8){
00161         LOG(L_ERR,"DBG:"M_NAME":encrypt: Error in encryption phase\n");
00162         goto error;
00163     }
00164     printstr("String aft :",enc_text);
00165 
00166 #ifdef USE_BASE64
00167     encoded = base64_encode(enc_text);
00168 #else
00169     encoded = base16_encode(enc_text);
00170 #endif
00171     
00172     if (my_text.s) pkg_free(my_text.s);
00173     if (enc_text.s) pkg_free(enc_text.s);
00174     return encoded; 
00175 error:
00176     if (my_text.s) pkg_free(my_text.s);
00177     if (enc_text.s) pkg_free(enc_text.s);
00178     enc_text.s = 0;enc_text.len=0;
00179     return enc_text;        
00180 }
00181 
00189 str thig_decrypt(str encoded)
00190 {
00191     str src;
00192     str my_text={0,0},dec_text={0,0};
00193     cipherInstance ci2 = ci;
00194 
00195 #ifdef USE_BASE64
00196     src = base64_decode(encoded);
00197 #else
00198     src = base16_decode(encoded);
00199 #endif
00200     if (!src.len) return dec_text;
00201 
00202     my_text = src;
00203     dec_text.s = pkg_malloc(my_text.len);
00204     if (!dec_text.s){
00205         LOG(L_ERR,"ERR:"M_NAME":decrypt: error allocating %d bytes\n",my_text.len);
00206         goto error;     
00207     }
00208     dec_text.len = my_text.len;
00209     
00210     printstr("String bef :",my_text);
00211 
00212     if (blockDecrypt(&ci2,&ki,(unsigned char*)my_text.s,my_text.len*8,(unsigned char*)dec_text.s) != my_text.len*8){
00213         LOG(L_ERR,"DBG:"M_NAME":decrypt: Error in encryption phase\n");
00214         goto error;
00215     }
00216     while(dec_text.s[dec_text.len-1]==0 && dec_text.len>0)
00217         dec_text.len--;
00218     printstr("String aft :",dec_text);
00219     if (src.s) pkg_free(src.s);
00220     return dec_text;
00221 error:
00222     if (src.s) pkg_free(src.s);
00223     if (dec_text.s) pkg_free(dec_text.s);
00224     dec_text.s = 0;dec_text.len=0;
00225     return dec_text;    
00226 }
00227 
00228 
00229 #ifdef THIG_IMS_ENC_TEST
00230 
00231 int main (){
00232     
00233     
00234 
00235     BYTE  plainText[MAX_BLK_CNT*(BLOCK_SIZE/8)];
00236     BYTE *tmp;
00237     BYTE *ret_tmp;
00238     int i = 0;
00239     
00240     printf("*********************************************************************\n");
00241     printf("*********************************************************************\n");
00242     srand((unsigned) time(NULL));
00243     key_and_cipher_init(&ki,&ci);
00244     
00245     printf("byteCnt: %d\n",byteCnt);
00246     printf("-----------------------------------------------------------------------------\n");
00247     for (i=0;i<25;i++)      
00248         plainText[i]=(BYTE)((rand()%94)+32);
00249 
00250     tmp=enc(plainText,25);
00251     ret_tmp=dec(tmp,25);
00252     free(tmp);
00253     free(ret_tmp);
00254     /*printf("\n\n");printf("-----------------------------------------------------------------------------\n");
00255     
00256     int new_byteCnt=28;
00257     for (i=0;i<new_byteCnt;i++)     
00258         plainText[i]=(BYTE)('a');
00259 
00260     tmp=enc(plainText,new_byteCnt);
00261     ret_tmp=dec(tmp);
00262     free(tmp);
00263     free(ret_tmp);
00264 */
00265     return 1;
00266 }   
00267 
00268 #endif

Generated on Thu Oct 23 04:14:38 2008 for Open IMS Core CSCFs by  doxygen 1.5.2