thig_ims_enc.c File Reference


Detailed Description

I-CSCF Module - THIG Operations.

Author:
Florin Dinu fdi -at- fokus dot fraunhofer dot de

Definition in file thig_ims_enc.c.

#include "thig_ims_enc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../../mem/mem.h"
#include "base64.h"

Go to the source code of this file.

Defines

#define USE_BASE64   1
 Using base64 encoding reduces the length of the encrypted strings.

Functions

DWORD Here (DWORD x)
void printstr (char *text, str x)
 prints a message and the contents of the str structure
int thig_key_and_cipher_init (keyInstance *ki, cipherInstance *ci)
 Initializes the structures that hold the keys and the cipher.
str thig_encrypt (str src)
 Encrypts a string (str struct).
str thig_decrypt (str encoded)
 Decrypts a string(str struct).

Variables

keyInstance ki
cipherInstance ci
int keySize = 256
 key size
int mode = MODE_CFB1
 Twofish mode.
int byteCnt = BLOCK_SIZE/8


Define Documentation

#define USE_BASE64   1

Using base64 encoding reduces the length of the encrypted strings.

Definition at line 63 of file thig_ims_enc.c.


Function Documentation

DWORD Here ( DWORD  x  ) 

Definition at line 69 of file thig_ims_enc.c.

00069                    {
00070     unsigned int mask=~0U;
00071 
00072     return (* (((DWORD *)&x)-1)) & mask;
00073 }

void printstr ( char *  text,
str  x 
)

prints a message and the contents of the str structure

Parameters:
text - some text
x - some string

Definition at line 86 of file thig_ims_enc.c.

References NULL.

Referenced by thig_decrypt(), and thig_encrypt().

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 }

int thig_key_and_cipher_init ( keyInstance ki,
cipherInstance ci 
)

Initializes the structures that hold the keys and the cipher.

Parameters:
ki - the structure where the key information is to be stored
ci - the structure where the cipher information is to be stored
Returns:
1

Definition at line 103 of file thig_ims_enc.c.

References BLOCK_SIZE, ci, cipherInit(), DIR_ENCRYPT, keySize, ki, makeKey(), mode, NULL, reKey(), and TRUE.

Referenced by icscf_mod_init().

00103                                                                   {
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 }

str thig_encrypt ( str  src  ) 

Encrypts a string (str struct).

Parameters:
src - the string that needs to be encrypted.Must NOT contain
Returns:
- the encrypted string, allocated in this function.

Definition at line 133 of file thig_ims_enc.c.

References base16_encode(), base64_encode(), blockEncrypt(), byteCnt, ci, ki, M_NAME, and printstr().

Referenced by I_THIG_encrypt_header().

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 }

str thig_decrypt ( str  encoded  ) 

Decrypts a string(str struct).

Parameters:
encoded - the string that needs to be decrypted
Returns:
- the decoded string.Memory allocation takes place in this function.

Definition at line 189 of file thig_ims_enc.c.

References base16_decode(), base64_decode(), blockDecrypt(), ci, ki, M_NAME, and printstr().

Referenced by I_THIG_decrypt_header().

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 }


Variable Documentation

keyInstance ki

Definition at line 65 of file thig_ims_enc.c.

cipherInstance ci

Definition at line 66 of file thig_ims_enc.c.

int keySize = 256

key size

Definition at line 75 of file thig_ims_enc.c.

Referenced by thig_key_and_cipher_init().

int mode = MODE_CFB1

Twofish mode.

Definition at line 76 of file thig_ims_enc.c.

Referenced by thig_key_and_cipher_init().

int byteCnt = BLOCK_SIZE/8

Definition at line 77 of file thig_ims_enc.c.

Referenced by thig_encrypt().


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