int encode_SNM_hdr( uint8_t *base, size_t *idx, const snm_hdr_t *hdr ) { int retval = 0; retval += encode_uint8(base, idx, hdr->type); retval += encode_uint8(base, idx, hdr->flags); retval += encode_uint16(base, idx, hdr->seq_num); return retval; }
void test_command_with_args() { uint8_t buf[255]; int pos = 0; int rc; uint8_t dest; uint8_t code; uint16_t mref; rc = encode_cmd(buf, &pos, LS1P_ADDR_ARM, LS1P_REQ_CODE_CMDLOG, 314); assert(rc == LS1P_API_OK); assert(pos == 3); assert(buf[0] == 0x01); rc = encode_uint8(buf, &pos, 123); assert(rc == LS1P_API_OK); assert(pos == 4); rc = encode_uint16(buf, &pos, 4023); assert(rc == LS1P_API_OK); assert(pos == 6); assert(buf[0] == 0x01); assert(buf[1] == 0x01); assert(buf[2] == 0x3A); assert(buf[3] == 0x7B); assert(buf[4] == 0x0F); assert(buf[5] == 0xB7); rc = decode_cmd(buf, &pos, &dest, &code, &mref); assert(rc == LS1P_API_OK); assert(dest == LS1P_ADDR_ARM); assert(code == LS1P_REQ_CODE_CMDLOG); assert(mref == 314); assert(pos == 3); }
int encode_SNM_comm( uint8_t *base, size_t *idx, const snm_comm_name_t *comm_name ) { int retval = 0; retval += encode_uint8(base, idx, comm_name->size); retval += encode_buf(base, idx, comm_name->name, comm_name->size); return retval; }
/** The twofish packet format consists of: * * - a 8-bit twofish encoding version in clear text * - a 32-bit SA number in clear text * - ciphertext encrypted from a 32-bit nonce followed by the payload. * * [V|SSSS|nnnnDDDDDDDDDDDDDDDDDDDDD] * |<------ encrypted ------>| */ static int transop_encode_twofish( ntvl_trans_op_t * arg, uint8_t * outbuf, size_t out_len, const uint8_t * inbuf, size_t in_len ) { int len=-1; transop_tf_t * priv = (transop_tf_t *)arg->priv; uint8_t assembly[NTVL_PKT_BUF_SIZE]; uint32_t * pnonce; if ( (in_len + TRANSOP_TF_NONCE_SIZE) <= NTVL_PKT_BUF_SIZE ) { if ( (in_len + TRANSOP_TF_NONCE_SIZE + TRANSOP_TF_SA_SIZE + TRANSOP_TF_VER_SIZE) <= out_len ) { size_t idx=0; sa_twofish_t * sa; size_t tx_sa_num = 0; /* The transmit sa is periodically updated */ tx_sa_num = tf_choose_tx_sa( priv ); sa = &(priv->sa[tx_sa_num]); /* Proper Tx SA index */ traceEvent( TRACE_DEBUG, "encode_twofish %lu with SA %lu.", in_len, sa->sa_id ); /* Encode the twofish format version. */ encode_uint8( outbuf, &idx, NTVL_TWOFISH_TRANSFORM_VERSION ); /* Encode the security association (SA) number */ encode_uint32( outbuf, &idx, sa->sa_id ); /* The assembly buffer is a source for encrypting data. The nonce is * written in first followed by the packet payload. The whole * contents of assembly are encrypted. */ pnonce = (uint32_t *)assembly; *pnonce = rand(); memcpy( assembly + TRANSOP_TF_NONCE_SIZE, inbuf, in_len ); /* Encrypt the assembly contents and write the ciphertext after the SA. */ len = TwoFishEncryptRaw( assembly, /* source */ outbuf + TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE, in_len + TRANSOP_TF_NONCE_SIZE, /* enc size */ sa->enc_tf); if ( len > 0 ) len += TRANSOP_TF_VER_SIZE + TRANSOP_TF_SA_SIZE; /* size of data carried in UDP. */ else traceEvent( TRACE_ERROR, "encode_twofish encryption failed." ); } else traceEvent( TRACE_ERROR, "encode_twofish outbuf too small." ); } else traceEvent( TRACE_ERROR, "encode_twofish inbuf too big to encrypt." ); return len; }