Trace lines are now shorter

This commit is contained in:
Luca Deri 2018-06-14 12:30:38 +02:00
parent 6c611ea306
commit 29de24c5d9
2 changed files with 235 additions and 238 deletions

4
n2n.c
View File

@ -117,7 +117,7 @@ void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...) {
snprintf(out_buf, sizeof(out_buf), "%s%s", extra_msg, buf); snprintf(out_buf, sizeof(out_buf), "%s%s", extra_msg, buf);
syslog(LOG_INFO, "%s", out_buf); syslog(LOG_INFO, "%s", out_buf);
} else { } else {
snprintf(out_buf, sizeof(out_buf), "%s [%11s:%4d] %s%s", theDate, file, line, extra_msg, buf); snprintf(out_buf, sizeof(out_buf), "%s [%s:%d] %s%s", theDate, file, line, extra_msg, buf);
#ifdef __ANDROID_NDK__ #ifdef __ANDROID_NDK__
switch (eventTraceLevel) { switch (eventTraceLevel) {
case 0: // ERROR case 0: // ERROR
@ -148,7 +148,7 @@ void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...) {
#else #else
/* this is the WIN32 code */ /* this is the WIN32 code */
for(i=strlen(file)-1; i>0; i--) if(file[i] == '\\') { i++; break; }; for(i=strlen(file)-1; i>0; i--) if(file[i] == '\\') { i++; break; };
snprintf(out_buf, sizeof(out_buf), "%s [%11s:%4d] %s%s", theDate, &file[i], line, extra_msg, buf); snprintf(out_buf, sizeof(out_buf), "%s [%s:%d] %s%s", theDate, &file[i], line, extra_msg, buf);
printf("%s\n", out_buf); printf("%s\n", out_buf);
fflush(stdout); fflush(stdout);
#endif #endif

View File

@ -30,10 +30,10 @@
struct sa_twofish struct sa_twofish
{ {
n2n_cipherspec_t spec; /* cipher spec parameters */ n2n_cipherspec_t spec; /* cipher spec parameters */
n2n_sa_t sa_id; /* security association index */ n2n_sa_t sa_id; /* security association index */
TWOFISH * enc_tf; /* tx state */ TWOFISH * enc_tf; /* tx state */
TWOFISH * dec_tf; /* rx state */ TWOFISH * dec_tf; /* rx state */
}; };
typedef struct sa_twofish sa_twofish_t; typedef struct sa_twofish sa_twofish_t;
@ -48,48 +48,48 @@ typedef struct sa_twofish sa_twofish_t;
*/ */
struct transop_tf struct transop_tf
{ {
ssize_t tx_sa; ssize_t tx_sa;
size_t num_sa; size_t num_sa;
sa_twofish_t sa[N2N_TWOFISH_NUM_SA]; sa_twofish_t sa[N2N_TWOFISH_NUM_SA];
}; };
typedef struct transop_tf transop_tf_t; typedef struct transop_tf transop_tf_t;
static int transop_deinit_twofish( n2n_trans_op_t * arg ) static int transop_deinit_twofish( n2n_trans_op_t * arg )
{ {
transop_tf_t * priv = (transop_tf_t *)arg->priv; transop_tf_t * priv = (transop_tf_t *)arg->priv;
size_t i; size_t i;
if ( priv ) if ( priv )
{ {
/* Memory was previously allocated */ /* Memory was previously allocated */
for (i=0; i<N2N_TWOFISH_NUM_SA; ++i ) for (i=0; i<N2N_TWOFISH_NUM_SA; ++i )
{ {
sa_twofish_t * sa = &(priv->sa[i]); sa_twofish_t * sa = &(priv->sa[i]);
TwoFishDestroy(sa->enc_tf); /* deallocate TWOFISH */ TwoFishDestroy(sa->enc_tf); /* deallocate TWOFISH */
sa->enc_tf=NULL; sa->enc_tf=NULL;
TwoFishDestroy(sa->dec_tf); /* deallocate TWOFISH */ TwoFishDestroy(sa->dec_tf); /* deallocate TWOFISH */
sa->dec_tf=NULL; sa->dec_tf=NULL;
sa->sa_id=0; sa->sa_id=0;
} }
priv->num_sa=0; priv->num_sa=0;
priv->tx_sa=-1; priv->tx_sa=-1;
free(priv); free(priv);
} }
arg->priv=NULL; /* return to fully uninitialised state */ arg->priv=NULL; /* return to fully uninitialised state */
return 0; return 0;
} }
static size_t tf_choose_tx_sa( transop_tf_t * priv ) static size_t tf_choose_tx_sa( transop_tf_t * priv )
{ {
return priv->tx_sa; /* set in tick */ return priv->tx_sa; /* set in tick */
} }
#define TRANSOP_TF_VER_SIZE 1 /* Support minor variants in encoding in one module. */ #define TRANSOP_TF_VER_SIZE 1 /* Support minor variants in encoding in one module. */
@ -111,65 +111,65 @@ static int transop_encode_twofish( n2n_trans_op_t * arg,
const uint8_t * inbuf, const uint8_t * inbuf,
size_t in_len ) size_t in_len )
{ {
int len=-1; int len=-1;
transop_tf_t * priv = (transop_tf_t *)arg->priv; transop_tf_t * priv = (transop_tf_t *)arg->priv;
uint8_t assembly[N2N_PKT_BUF_SIZE]; uint8_t assembly[N2N_PKT_BUF_SIZE];
uint32_t * pnonce; uint32_t * pnonce;
if ( (in_len + TRANSOP_TF_NONCE_SIZE) <= N2N_PKT_BUF_SIZE ) if ( (in_len + TRANSOP_TF_NONCE_SIZE) <= N2N_PKT_BUF_SIZE )
{ {
if ( (in_len + TRANSOP_TF_NONCE_SIZE + TRANSOP_TF_SA_SIZE + TRANSOP_TF_VER_SIZE) <= out_len ) if ( (in_len + TRANSOP_TF_NONCE_SIZE + TRANSOP_TF_SA_SIZE + TRANSOP_TF_VER_SIZE) <= out_len )
{ {
size_t idx=0; size_t idx=0;
sa_twofish_t * sa; sa_twofish_t * sa;
size_t tx_sa_num = 0; size_t tx_sa_num = 0;
/* The transmit sa is periodically updated */ /* The transmit sa is periodically updated */
tx_sa_num = tf_choose_tx_sa( priv ); tx_sa_num = tf_choose_tx_sa( priv );
sa = &(priv->sa[tx_sa_num]); /* Proper Tx SA index */ sa = &(priv->sa[tx_sa_num]); /* Proper Tx SA index */
traceEvent( TRACE_DEBUG, "encode_twofish %lu with SA %lu.", in_len, sa->sa_id ); traceEvent( TRACE_DEBUG, "encode_twofish %lu with SA %lu.", in_len, sa->sa_id );
/* Encode the twofish format version. */ /* Encode the twofish format version. */
encode_uint8( outbuf, &idx, N2N_TWOFISH_TRANSFORM_VERSION ); encode_uint8( outbuf, &idx, N2N_TWOFISH_TRANSFORM_VERSION );
/* Encode the security association (SA) number */ /* Encode the security association (SA) number */
encode_uint32( outbuf, &idx, sa->sa_id ); encode_uint32( outbuf, &idx, sa->sa_id );
/* The assembly buffer is a source for encrypting data. The nonce is /* The assembly buffer is a source for encrypting data. The nonce is
* written in first followed by the packet payload. The whole * written in first followed by the packet payload. The whole
* contents of assembly are encrypted. */ * contents of assembly are encrypted. */
pnonce = (uint32_t *)assembly; pnonce = (uint32_t *)assembly;
*pnonce = rand(); *pnonce = rand();
memcpy( assembly + TRANSOP_TF_NONCE_SIZE, inbuf, in_len ); memcpy( assembly + TRANSOP_TF_NONCE_SIZE, inbuf, in_len );
/* Encrypt the assembly contents and write the ciphertext after the SA. */ /* Encrypt the assembly contents and write the ciphertext after the SA. */
len = TwoFishEncryptRaw( assembly, /* source */ len = TwoFishEncryptRaw( assembly, /* source */
outbuf + TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE, outbuf + TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE,
in_len + TRANSOP_TF_NONCE_SIZE, /* enc size */ in_len + TRANSOP_TF_NONCE_SIZE, /* enc size */
sa->enc_tf); sa->enc_tf);
if ( len > 0 ) if ( len > 0 )
{ {
len += TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE; /* size of data carried in UDP. */ len += TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE; /* size of data carried in UDP. */
} }
else else
{ {
traceEvent( TRACE_ERROR, "encode_twofish encryption failed." ); traceEvent( TRACE_ERROR, "encode_twofish encryption failed." );
} }
} }
else else
{ {
traceEvent( TRACE_ERROR, "encode_twofish outbuf too small." ); traceEvent( TRACE_ERROR, "encode_twofish outbuf too small." );
} }
} }
else else
{ {
traceEvent( TRACE_ERROR, "encode_twofish inbuf too big to encrypt." ); traceEvent( TRACE_ERROR, "encode_twofish inbuf too big to encrypt." );
} }
return len; return len;
} }
@ -179,20 +179,20 @@ static int transop_encode_twofish( n2n_trans_op_t * arg,
*/ */
static ssize_t twofish_find_sa( const transop_tf_t * priv, const n2n_sa_t req_id ) static ssize_t twofish_find_sa( const transop_tf_t * priv, const n2n_sa_t req_id )
{ {
size_t i; size_t i;
for (i=0; i < priv->num_sa; ++i) for (i=0; i < priv->num_sa; ++i)
{ {
const sa_twofish_t * sa=NULL; const sa_twofish_t * sa=NULL;
sa = &(priv->sa[i]); sa = &(priv->sa[i]);
if (req_id == sa->sa_id) if (req_id == sa->sa_id)
{ {
return i; return i;
} }
} }
return -1; return -1;
} }
@ -211,177 +211,177 @@ static int transop_decode_twofish( n2n_trans_op_t * arg,
const uint8_t * inbuf, const uint8_t * inbuf,
size_t in_len ) size_t in_len )
{ {
int len=0; int len=0;
transop_tf_t * priv = (transop_tf_t *)arg->priv; transop_tf_t * priv = (transop_tf_t *)arg->priv;
uint8_t assembly[N2N_PKT_BUF_SIZE]; uint8_t assembly[N2N_PKT_BUF_SIZE];
if ( ( (in_len - (TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE)) <= N2N_PKT_BUF_SIZE ) /* Cipher text fits in assembly */ if ( ( (in_len - (TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE)) <= N2N_PKT_BUF_SIZE ) /* Cipher text fits in assembly */
&& (in_len >= (TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE + TRANSOP_TF_NONCE_SIZE) ) /* Has at least version, SA and nonce */ && (in_len >= (TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE + TRANSOP_TF_NONCE_SIZE) ) /* Has at least version, SA and nonce */
) )
{ {
n2n_sa_t sa_rx; n2n_sa_t sa_rx;
ssize_t sa_idx=-1; ssize_t sa_idx=-1;
size_t rem=in_len; size_t rem=in_len;
size_t idx=0; size_t idx=0;
uint8_t tf_enc_ver=0; uint8_t tf_enc_ver=0;
/* Get the encoding version to make sure it is supported */ /* Get the encoding version to make sure it is supported */
decode_uint8( &tf_enc_ver, inbuf, &rem, &idx ); decode_uint8( &tf_enc_ver, inbuf, &rem, &idx );
if ( N2N_TWOFISH_TRANSFORM_VERSION == tf_enc_ver ) if ( N2N_TWOFISH_TRANSFORM_VERSION == tf_enc_ver )
{ {
/* Get the SA number and make sure we are decrypting with the right one. */ /* Get the SA number and make sure we are decrypting with the right one. */
decode_uint32( &sa_rx, inbuf, &rem, &idx ); decode_uint32( &sa_rx, inbuf, &rem, &idx );
sa_idx = twofish_find_sa(priv, sa_rx); sa_idx = twofish_find_sa(priv, sa_rx);
if ( sa_idx >= 0 ) if ( sa_idx >= 0 )
{ {
sa_twofish_t * sa = &(priv->sa[sa_idx]); sa_twofish_t * sa = &(priv->sa[sa_idx]);
traceEvent( TRACE_DEBUG, "decode_twofish %lu with SA %lu.", in_len, sa_rx, sa->sa_id ); traceEvent( TRACE_DEBUG, "decode_twofish %lu with SA %lu.", in_len, sa_rx, sa->sa_id );
len = TwoFishDecryptRaw( (void *)(inbuf + TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE), len = TwoFishDecryptRaw( (void *)(inbuf + TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE),
assembly, /* destination */ assembly, /* destination */
(in_len - (TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE)), (in_len - (TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE)),
sa->dec_tf); sa->dec_tf);
if ( len > 0 ) if ( len > 0 )
{ {
/* Step over 4-byte random nonce value */ /* Step over 4-byte random nonce value */
len -= TRANSOP_TF_NONCE_SIZE; /* size of ethernet packet */ len -= TRANSOP_TF_NONCE_SIZE; /* size of ethernet packet */
memcpy( outbuf, memcpy( outbuf,
assembly + TRANSOP_TF_NONCE_SIZE, assembly + TRANSOP_TF_NONCE_SIZE,
len ); len );
} }
else else
{ {
traceEvent( TRACE_ERROR, "decode_twofish decryption failed." ); traceEvent( TRACE_ERROR, "decode_twofish decryption failed." );
} }
} }
else else
{ {
/* Wrong security association; drop the packet as it is undecodable. */ /* Wrong security association; drop the packet as it is undecodable. */
traceEvent( TRACE_ERROR, "decode_twofish SA number %lu not found.", sa_rx ); traceEvent( TRACE_ERROR, "decode_twofish SA number %lu not found.", sa_rx );
/* REVISIT: should be able to load a new SA at this point to complete the decoding. */ /* REVISIT: should be able to load a new SA at this point to complete the decoding. */
} }
} }
else else
{ {
/* Wrong security association; drop the packet as it is undecodable. */ /* Wrong security association; drop the packet as it is undecodable. */
traceEvent( TRACE_ERROR, "decode_twofish unsupported twofish version %u.", tf_enc_ver ); traceEvent( TRACE_ERROR, "decode_twofish unsupported twofish version %u.", tf_enc_ver );
/* REVISIT: should be able to load a new SA at this point to complete the decoding. */ /* REVISIT: should be able to load a new SA at this point to complete the decoding. */
} }
} }
else else
{ {
traceEvent( TRACE_ERROR, "decode_twofish inbuf wrong size (%ul) to decrypt.", in_len ); traceEvent( TRACE_ERROR, "decode_twofish inbuf wrong size (%ul) to decrypt.", in_len );
} }
return len; return len;
} }
static int transop_addspec_twofish( n2n_trans_op_t * arg, const n2n_cipherspec_t * cspec ) static int transop_addspec_twofish( n2n_trans_op_t * arg, const n2n_cipherspec_t * cspec )
{ {
int retval = 1; int retval = 1;
ssize_t pstat=-1; ssize_t pstat=-1;
transop_tf_t * priv = (transop_tf_t *)arg->priv; transop_tf_t * priv = (transop_tf_t *)arg->priv;
uint8_t keybuf[N2N_MAX_KEYSIZE]; uint8_t keybuf[N2N_MAX_KEYSIZE];
if ( priv->num_sa < N2N_TWOFISH_NUM_SA ) if ( priv->num_sa < N2N_TWOFISH_NUM_SA )
{ {
const char * op = (const char *)cspec->opaque; const char * op = (const char *)cspec->opaque;
#ifdef __ANDROID_NDK__ #ifdef __ANDROID_NDK__
const char *sep = strchr(op, '_'); const char *sep = strchr(op, '_');
#else #else
const char * sep = index( op, '_' ); const char * sep = index( op, '_' );
#endif // __ANDROID_NDK__ #endif // __ANDROID_NDK__
if ( sep ) if ( sep )
{ {
char tmp[256]; char tmp[256];
size_t s; size_t s;
s = sep - op; s = sep - op;
memcpy( tmp, cspec->opaque, s ); memcpy( tmp, cspec->opaque, s );
tmp[s]=0; tmp[s]=0;
s = strlen(sep+1); /* sep is the _ which might be immediately followed by NULL */ s = strlen(sep+1); /* sep is the _ which might be immediately followed by NULL */
priv->sa[priv->num_sa].spec = *cspec; priv->sa[priv->num_sa].spec = *cspec;
priv->sa[priv->num_sa].sa_id = strtoul(tmp, NULL, 10); priv->sa[priv->num_sa].sa_id = strtoul(tmp, NULL, 10);
pstat = n2n_parse_hex( keybuf, N2N_MAX_KEYSIZE, sep+1, s ); pstat = n2n_parse_hex( keybuf, N2N_MAX_KEYSIZE, sep+1, s );
if ( pstat > 0 ) if ( pstat > 0 )
{ {
priv->sa[priv->num_sa].enc_tf = TwoFishInit( keybuf, pstat); priv->sa[priv->num_sa].enc_tf = TwoFishInit( keybuf, pstat);
priv->sa[priv->num_sa].dec_tf = TwoFishInit( keybuf, pstat); priv->sa[priv->num_sa].dec_tf = TwoFishInit( keybuf, pstat);
traceEvent( TRACE_DEBUG, "transop_addspec_twofish sa_id=%u data=%s.\n", traceEvent( TRACE_DEBUG, "transop_addspec_twofish sa_id=%u data=%s.\n",
priv->sa[priv->num_sa].sa_id, sep+1); priv->sa[priv->num_sa].sa_id, sep+1);
++(priv->num_sa); ++(priv->num_sa);
retval = 0; retval = 0;
} }
} }
else else
{ {
traceEvent( TRACE_ERROR, "transop_addspec_twofish : bad key data - missing '_'.\n"); traceEvent( TRACE_ERROR, "transop_addspec_twofish : bad key data - missing '_'.\n");
} }
} }
else else
{ {
traceEvent( TRACE_ERROR, "transop_addspec_twofish : full.\n"); traceEvent( TRACE_ERROR, "transop_addspec_twofish : full.\n");
} }
return retval; return retval;
} }
static n2n_tostat_t transop_tick_twofish( n2n_trans_op_t * arg, time_t now ) static n2n_tostat_t transop_tick_twofish( n2n_trans_op_t * arg, time_t now )
{ {
transop_tf_t * priv = (transop_tf_t *)arg->priv; transop_tf_t * priv = (transop_tf_t *)arg->priv;
size_t i; size_t i;
int found=0; int found=0;
n2n_tostat_t r; n2n_tostat_t r;
memset( &r, 0, sizeof(r) ); memset( &r, 0, sizeof(r) );
traceEvent( TRACE_DEBUG, "transop_tf tick num_sa=%u", priv->num_sa ); traceEvent( TRACE_DEBUG, "transop_tf tick num_sa=%u", priv->num_sa );
for ( i=0; i < priv->num_sa; ++i ) for ( i=0; i < priv->num_sa; ++i )
{ {
if ( 0 == validCipherSpec( &(priv->sa[i].spec), now ) ) if ( 0 == validCipherSpec( &(priv->sa[i].spec), now ) )
{ {
time_t remaining = priv->sa[i].spec.valid_until - now; time_t remaining = priv->sa[i].spec.valid_until - now;
traceEvent( TRACE_INFO, "transop_tf choosing tx_sa=%u (valid for %lu sec)", priv->sa[i].sa_id, remaining ); traceEvent( TRACE_INFO, "transop_tf choosing tx_sa=%u (valid for %lu sec)", priv->sa[i].sa_id, remaining );
priv->tx_sa=i; priv->tx_sa=i;
found=1; found=1;
break; break;
} }
else else
{ {
traceEvent( TRACE_DEBUG, "transop_tf tick rejecting sa=%u %lu -> %lu", traceEvent( TRACE_DEBUG, "transop_tf tick rejecting sa=%u %lu -> %lu",
priv->sa[i].sa_id, priv->sa[i].spec.valid_from, priv->sa[i].spec.valid_until ); priv->sa[i].sa_id, priv->sa[i].spec.valid_from, priv->sa[i].spec.valid_until );
} }
} }
if ( 0==found) if ( 0==found)
{ {
traceEvent( TRACE_INFO, "transop_tf no keys are currently valid. Keeping tx_sa=%u", priv->tx_sa ); traceEvent( TRACE_INFO, "transop_tf no keys are currently valid. Keeping tx_sa=%u", priv->tx_sa );
} }
else else
{ {
r.can_tx = 1; r.can_tx = 1;
r.tx_spec.t = N2N_TRANSFORM_ID_TWOFISH; r.tx_spec.t = N2N_TRANSFORM_ID_TWOFISH;
r.tx_spec = priv->sa[priv->tx_sa].spec; r.tx_spec = priv->sa[priv->tx_sa].spec;
} }
return r; return r;
} }
@ -390,118 +390,115 @@ int transop_twofish_setup( n2n_trans_op_t * ttt,
uint8_t * encrypt_pwd, uint8_t * encrypt_pwd,
uint32_t encrypt_pwd_len ) uint32_t encrypt_pwd_len )
{ {
int retval = 1; int retval = 1;
transop_tf_t * priv = NULL; transop_tf_t * priv = NULL;
if ( ttt->priv ) if ( ttt->priv )
{ {
transop_deinit_twofish( ttt ); transop_deinit_twofish( ttt );
} }
memset( ttt, 0, sizeof( n2n_trans_op_t ) ); memset( ttt, 0, sizeof( n2n_trans_op_t ) );
priv = (transop_tf_t *) malloc( sizeof(transop_tf_t) ); priv = (transop_tf_t *) malloc( sizeof(transop_tf_t) );
if ( NULL != priv ) if ( NULL != priv )
{ {
size_t i; size_t i;
sa_twofish_t * sa=NULL; sa_twofish_t * sa=NULL;
/* install the private structure. */ /* install the private structure. */
ttt->priv = priv; ttt->priv = priv;
for(i=0; i<N2N_TWOFISH_NUM_SA; ++i) for(i=0; i<N2N_TWOFISH_NUM_SA; ++i)
{ {
sa = &(priv->sa[i]); sa = &(priv->sa[i]);
sa->sa_id=0; sa->sa_id=0;
memset( &(sa->spec), 0, sizeof(n2n_cipherspec_t) ); memset( &(sa->spec), 0, sizeof(n2n_cipherspec_t) );
sa->enc_tf=NULL; sa->enc_tf=NULL;
sa->dec_tf=NULL; sa->dec_tf=NULL;
} }
priv->num_sa=1; /* There is one SA in the array. */ priv->num_sa=1; /* There is one SA in the array. */
priv->tx_sa=0; priv->tx_sa=0;
sa = &(priv->sa[priv->tx_sa]); sa = &(priv->sa[priv->tx_sa]);
sa->sa_id=sa_num; sa->sa_id=sa_num;
sa->spec.valid_until = 0x7fffffff; sa->spec.valid_until = 0x7fffffff;
/* This is a preshared key setup. Both Tx and Rx are using the same security association. */ /* This is a preshared key setup. Both Tx and Rx are using the same security association. */
sa->enc_tf = TwoFishInit(encrypt_pwd, encrypt_pwd_len); sa->enc_tf = TwoFishInit(encrypt_pwd, encrypt_pwd_len);
sa->dec_tf = TwoFishInit(encrypt_pwd, encrypt_pwd_len); sa->dec_tf = TwoFishInit(encrypt_pwd, encrypt_pwd_len);
if ( (sa->enc_tf) && (sa->dec_tf) ) if ( (sa->enc_tf) && (sa->dec_tf) )
{ {
ttt->transform_id = N2N_TRANSFORM_ID_TWOFISH; ttt->transform_id = N2N_TRANSFORM_ID_TWOFISH;
ttt->deinit = transop_deinit_twofish; ttt->deinit = transop_deinit_twofish;
ttt->addspec = transop_addspec_twofish; ttt->addspec = transop_addspec_twofish;
ttt->tick = transop_tick_twofish; /* chooses a new tx_sa */ ttt->tick = transop_tick_twofish; /* chooses a new tx_sa */
ttt->fwd = transop_encode_twofish; ttt->fwd = transop_encode_twofish;
ttt->rev = transop_decode_twofish; ttt->rev = transop_decode_twofish;
retval = 0; retval = 0;
} }
else else
{ {
traceEvent( TRACE_ERROR, "TwoFishInit failed" ); traceEvent( TRACE_ERROR, "TwoFishInit failed" );
} }
} }
else else
{ {
memset( ttt, 0, sizeof(n2n_trans_op_t) ); memset( ttt, 0, sizeof(n2n_trans_op_t) );
traceEvent( TRACE_ERROR, "Failed to allocate priv for twofish" ); traceEvent( TRACE_ERROR, "Failed to allocate priv for twofish" );
} }
return retval; return retval;
} }
int transop_twofish_init( n2n_trans_op_t * ttt ) int transop_twofish_init( n2n_trans_op_t * ttt )
{ {
int retval = 1; int retval = 1;
transop_tf_t * priv = NULL; transop_tf_t * priv = NULL;
if ( ttt->priv ) if ( ttt->priv )
{ {
transop_deinit_twofish( ttt ); transop_deinit_twofish( ttt );
} }
memset( ttt, 0, sizeof( n2n_trans_op_t ) ); memset( ttt, 0, sizeof( n2n_trans_op_t ) );
priv = (transop_tf_t *) malloc( sizeof(transop_tf_t) ); priv = (transop_tf_t *) malloc( sizeof(transop_tf_t) );
if ( NULL != priv ) if ( NULL != priv ) {
{ size_t i;
size_t i; sa_twofish_t * sa=NULL;
sa_twofish_t * sa=NULL;
/* install the private structure. */ /* install the private structure. */
ttt->priv = priv; ttt->priv = priv;
priv->num_sa=0; priv->num_sa=0;
priv->tx_sa=0; /* We will use this sa index for encoding. */ priv->tx_sa=0; /* We will use this sa index for encoding. */
ttt->transform_id = N2N_TRANSFORM_ID_TWOFISH; ttt->transform_id = N2N_TRANSFORM_ID_TWOFISH;
ttt->addspec = transop_addspec_twofish; ttt->addspec = transop_addspec_twofish;
ttt->tick = transop_tick_twofish; /* chooses a new tx_sa */ ttt->tick = transop_tick_twofish; /* chooses a new tx_sa */
ttt->deinit = transop_deinit_twofish; ttt->deinit = transop_deinit_twofish;
ttt->fwd = transop_encode_twofish; ttt->fwd = transop_encode_twofish;
ttt->rev = transop_decode_twofish; ttt->rev = transop_decode_twofish;
for(i=0; i<N2N_TWOFISH_NUM_SA; ++i) for(i=0; i<N2N_TWOFISH_NUM_SA; ++i)
{ {
sa = &(priv->sa[i]); sa = &(priv->sa[i]);
sa->sa_id=0; sa->sa_id=0;
memset( &(sa->spec), 0, sizeof(n2n_cipherspec_t) ); memset( &(sa->spec), 0, sizeof(n2n_cipherspec_t) );
sa->enc_tf=NULL; sa->enc_tf=NULL;
sa->dec_tf=NULL; sa->dec_tf=NULL;
} }
retval = 0; retval = 0;
} } else {
else memset( ttt, 0, sizeof(n2n_trans_op_t) );
{ traceEvent( TRACE_ERROR, "Failed to allocate priv for twofish" );
memset( ttt, 0, sizeof(n2n_trans_op_t) );
traceEvent( TRACE_ERROR, "Failed to allocate priv for twofish" );
} }
return retval; return retval;
} }