Easy async socket library with HELIX encryption and authentication
Copyright (C) 2004-2005 Chris Porter.
+ v1.03
+ - changed nonce logic
v1.02
- added some \n stripping in crypto code
v1.01
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
-#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
socklist = p->next;
}
+
deregisterhandler(p->fd, 1);
for(;pkt;) {
nw->next = NULL;
nw->size = bytes;
+ nw->startpos = 0;
memcpy(nw->line, buffer, bytes);
} else if(buf->count) { /* if we're just flushing the buffer */
int ret;
for(;;) {
- ret = write(sock->fd, buf->head->line, buf->head->size);
+ ret = write(sock->fd, buf->head->line + buf->head->startpos, buf->head->size);
if(!ret) {
return 0;
} else if(ret == buf->head->size) {
break;
}
} else {
- buf->head->line+=ret;
+ buf->head->startpos+=ret;
buf->head->size-=ret;
}
}
return 1;
}
+unsigned char *increase_nonce(unsigned char *nonce) {
+ u_int64_t *inonce = (u_int64_t *)(nonce + 8);
+ *inonce = htonq(ntohq(*inonce) + 1);
+ return nonce;
+}
+
int esocket_write(struct esocket *sock, char *buffer, int bytes) {
int ret;
if(sock->in.on_parse == buffer_parse_ascii) {
packetlength = htons(bytes + USED_MAC_LEN);
memcpy(newbuf, &packetlength, sizeof(packet_t));
+ h_nonce(&sock->keysend, increase_nonce(sock->sendnonce));
h_encrypt(&sock->keysend, (unsigned char *)buffer, bytes, mac);
memcpy(newbuf + sizeof(packet_t), buffer, bytes);
if(buf->packet_length <= buf->writepos - buf->startpos) {
int ret;
char *newline, *p;
+ h_nonce(&sock->keyreceive, increase_nonce(sock->recvnonce));
h_decrypt(&sock->keyreceive, (unsigned char *)buf->startpos, buf->packet_length - USED_MAC_LEN, mac);
if(memcmp(mac, buf->startpos + buf->packet_length - USED_MAC_LEN, USED_MAC_LEN))
buf->packet_length = 0;
ret = sock->events.on_line(sock, newline);
- if(ret) {
- printf("returning error\n");
+ if(ret)
return ret;
- }
return BUF_CONT;
}
unsigned char ukey[20];
SHA1_CTX context;
+ memcpy(sock->sendnonce, ournonce, sizeof(sock->sendnonce));
+ memcpy(sock->recvnonce, theirnonce, sizeof(sock->recvnonce));
+
SHA1Init(&context);
SHA1Update(&context, (unsigned char *)key, strlen(key));
SHA1Update(&context, (unsigned char *)" ", 1);
sock->in.on_parse = buffer_parse_crypt;
sock->in.buffer_size = MAX_BINARY_LINE_SIZE;
-
h_key(&sock->keysend, ukey, sizeof(ukey));
- h_nonce(&sock->keysend, ournonce);
SHA1Init(&context);
SHA1Update(&context, (unsigned char *)key, strlen(key));
SHA1Final(ukey, &context);
h_key(&sock->keyreceive, ukey, sizeof(ukey));
- h_nonce(&sock->keyreceive, theirnonce);
}