ushort control = *(ushort *) (msgbuf + 8);
int rval = 0;
msglen = parse_packet_get_expected_payload_len(0x1FFCu, msgbuf);
if (msglen >= 16 && msglen <= 8188 && msglen == (int) r && HIBYTE(control) >= 0 ) {
*(ushort *)(msgbuf + 8) = control | 0x8000;
/* populate the keys */
call_fill_pseudorandom_bytes(msgbuf, 8);
*(int *)(msgbuf + msglen) = compute_checksum(msgbuf, msglen);
rval = msglen + 4;
encrypt_decrypt_message(rval - 8, msgbuf + 8, *(int *)msgbuf, *(int *)
(msgbuf + 4));
int noise_len = (8192 - rval) & 0x1FF;
/* compute_random_noise length */
for (i=0; i < 4; i++ ) {
noise_len = critical_generate_random() % (unsigned int)(noise_len + 1);
}
/* append random noise */
if ( noise_len ) {
call_fill_pseudorandom_bytes(msgbuf + rval, noise_len);
rval += noise_len;
}
}
return rval;
}
int encrypt_decrypt_message(int buflen, char* buf, int key1, int key2) {
int remlen;
int64 key64;
int keyrot;
uchar carry;
for (remlen=buflen; remlen > 0; remlen--, buf++) {
*((int *)&key64 + 1) = key2;
*(int *)&key64 = key1;
key64 << 1;
keyrot = 2 * key1 | (key2 >> 31);
*buf = *buf ^ (uchar) keyrot;
/* generate carry flag for addition */
carry = __MKCADD__((uchar) keyrot, keyrot);
key1 = (uchar) keyrot + keyrot;
key2 = carry + *((int *)&key64 + 1);
}
return (uchar) keyrot;
}
SOURCE
LISTING 4: P2P message packing and encryption