// Set local PeerID and sequence number (required to complete auth protocol) static void authSetLocalData(struct s_auth_state *authstate, const int peerid, const int64_t seq, const int64_t flags) { authstate->local_peerid = peerid; cryptoRand(authstate->local_keygen_nonce, auth_NONCESIZE); cryptoRand(authstate->s4msg_nonce, auth_NONCESIZE); utilWriteInt64(authstate->local_seq, seq); utilWriteInt64(authstate->local_flags, flags); authstate->local_cneg_set = 1; authGenMsg(authstate); }
// encode packet static int packetEncode(unsigned char *pbuf, const int pbuf_size, const struct s_packet_data *data, struct s_crypto *ctx) { unsigned char dec_buf[packet_CRHDR_SIZE + data->pl_buf_size]; int32_t *scr_peerid = ((int32_t *)pbuf); int32_t ne_peerid; int len; // check if enough space is available for the operation if(data->pl_length > data->pl_buf_size) { return 0; } // prepare buffer utilWriteInt64(&dec_buf[packet_CRHDR_SEQ_START], data->seq); utilWriteInt16(&dec_buf[packet_CRHDR_PLLEN_START], data->pl_length); dec_buf[packet_CRHDR_PLTYPE_START] = data->pl_type; dec_buf[packet_CRHDR_PLOPT_START] = data->pl_options; memcpy(&dec_buf[packet_CRHDR_SIZE], data->pl_buf, data->pl_length); // encrypt buffer len = cryptoEnc(ctx, &pbuf[packet_PEERID_SIZE], (pbuf_size - packet_PEERID_SIZE), dec_buf, (packet_CRHDR_SIZE + data->pl_length), packet_HMAC_SIZE, packet_IV_SIZE); if(len < (packet_HMAC_SIZE + packet_IV_SIZE + packet_CRHDR_SIZE)) { return 0; } // write the scrambled peer ID utilWriteInt32((unsigned char *)&ne_peerid, data->peerid); scr_peerid[0] = (ne_peerid ^ (scr_peerid[1] ^ scr_peerid[2])); // return length of encoded packet return (packet_PEERID_SIZE + len); }