diff -uNr a/makefile b/makefile --- a/makefile 77469f9c4c5bc3c62adb0fd854e0bf604a0079b012949e47f33d93fa4843c477349a4717d9aca0bb66d5523238e98c8d6c46cb2a7847b4a3d84b5b47b717f1f1 +++ b/makefile 61ff0ac23d53c7a2b8a2b221933ab0178457071cca1f024df8f30de6de8d394ecf1c58ce1636a7ad4dd7186ee4922714a7cd0a01aae34482653fbb5f1e513e5d @@ -3,7 +3,7 @@ OUT := smalpest CFLAGS := -std=c99 -O3 -DNDEBUG -fdata-sections -ffunction-sections -Wno-parentheses -LDFLAGS := -Wl,--gc-sections +LDFLAGS := -static -Wl,--gc-sections LDLIBS := OBJ := \ src/addr.o \ @@ -17,7 +17,6 @@ src/getdata.o \ src/hash.o \ src/hearsay.o \ - src/hmac.o \ src/ht.o \ src/idx.o \ src/ignore.o \ @@ -36,9 +35,7 @@ src/msg.o \ src/net.o \ src/otherpplgode/base64.o \ - src/otherpplgode/hmac.o \ src/otherpplgode/serpent.o \ - src/otherpplgode/sha.o \ src/packetlog.o \ src/peer.o \ src/peerio.o \ @@ -50,10 +47,12 @@ src/send.o \ src/serial.o \ src/serpent.o \ - src/sha.o \ + src/sha256.o \ + src/sha512.o \ src/state.o \ src/string.o \ src/time.o \ + src/utf8.o \ src/util.o \ src/wipe.o \ diff -uNr a/src/addrcast.c b/src/addrcast.c --- a/src/addrcast.c 25eb337605e7903b6e7c5df09ba40fd7296b8890f3b08cbf3f1342cc367e073aff6df3b78aade85b20f6398abf7ef7f21e2d4068367cd561c101acde44eb5cde +++ b/src/addrcast.c 3f8b09bf6a9437da1e4ddf3d58a3f6f6fd7a61308ea17295d1a24a41196744f1ee1bee3949d529138f04946ebd2b738713f8aa65fbc577adc2afb32b1df8b099 @@ -27,14 +27,14 @@ static void ac_black_sign(uint8_t *ab, uint8_t const *key){ - hmac384(key,KEY_SIGN_SIZE,ab+AC_SIGN_OFF,ab+AC_RED_OFF,AC_RED_SIZE); + hmac_sha384(ab+AC_SIGN_OFF,key,KEY_SIGN_SIZE,ab+AC_RED_OFF,AC_RED_SIZE); } static bool ac_black_verify(uint8_t const *ab, uint8_t const *key){ uint8_t sign[AC_SIGN_SIZE]; - hmac384(key,KEY_SIGN_SIZE,sign,ab+AC_RED_OFF,AC_RED_SIZE); + hmac_sha384(sign,key,KEY_SIGN_SIZE,ab+AC_RED_OFF,AC_RED_SIZE); return memcmp(sign,ab+AC_SIGN_OFF,AC_SIGN_SIZE) == 0; } @@ -53,21 +53,23 @@ stamp = mono_sec(); assert(sizeof(ab) == TEXT_SIZE); - return send_text(CMD_ADDRESSCAST,stamp,ab,sizeof(ab),0); + return send_text(CMD_ADDRESSCAST,stamp,ab,sizeof(ab),0,0); } static void ac_red_write(uint8_t *ar,addr_t const *addr){ uint32_t cmd = 0; + uint32_t ip = byteswap32(addr->ip); write_u32(ar+AC_CMD_OFF, &cmd); write_u16(ar+AC_PORT_OFF, &addr->port); - write_u32(ar+AC_IP_OFF, &addr->ip); + write_u32(ar+AC_IP_OFF, &ip); memset(ar+AC_RPAD_OFF,0x00,AC_RPAD_SIZE); } static void ac_red_read(uint8_t const *ar,addr_t *addr){ read_u16(ar+AC_PORT_OFF, &addr->port); read_u32(ar+AC_IP_OFF, &addr->ip); + addr->ip = byteswap32(addr->ip); } void addrcast_broadcast(uint64_t time){ diff -uNr a/src/banner.c b/src/banner.c --- a/src/banner.c 03a51c4160f927450152a4145b5cb00da63b8662e2e1dcf39789453104eedd92fc3ff940a99497730ce648f9c368eb20811f261684ae3e0e417d616cf74c67a5 +++ b/src/banner.c a2184cbe694780fc432a260a0850972fd75240d93b4ac4f8dd8fa5b9bf42e31529063612b1aef45e64a8782fb3385525c487b60a6d7b8b4a5c20f512035a0075 @@ -4,15 +4,23 @@ #include "log.h" #include "time.h" #include "util.h" +#include "utf8.h" #include "state.h" #include #include -void banner_handle(peer_t *p,uint8_t const *text){ +void banner_handle(peer_t *p,uint8_t const *m){ char banner[BANNER_BUF]; //TODO: vat do vith this - cpy_text(banner,sizeof(banner),text); + cpy_text(banner,sizeof(banner),m); + + if(!utf8_valid(banner)){ + log_err("banner from peer '%s' contains invalid UTF-8 sequence", + p->handle[0]); + return; + } + if(strcmp(banner,p->banner) != 0){ peer_banner_set(p,banner); } @@ -27,9 +35,9 @@ if(banner_last != 0){ stamp = mono_sec(); - text = *banner ? banner : PROG_FULLNAME; + text = *banner ? banner : PROG_FULLERNAME; text_len = strlen(text); - send_text(CMD_BANNER,stamp,(uint8_t const *)text,text_len,0); + send_text(CMD_BANNER,stamp,(uint8_t const *)text,text_len,0,0); } banner_last = time; diff -uNr a/src/black.c b/src/black.c --- a/src/black.c 1c265e1e316d5c6ec3ae36a835284f043b0adbe621e73c0a6f795f6c6c88828fcfb4dddb8d29116873e63db0e176a203f0c056cfdfd40e32c6c4b33343ac627f +++ b/src/black.c e760b9a20f7ee7ea41a1859db202e8d0501d60c722c46a98d91c29b8680b61a5f118ae53ff4c0ae6197c550a0d642db792eab3edf58acc68774596e3fec0edd6 @@ -12,12 +12,12 @@ } void black_sign(uint8_t *b,uint8_t const *key){ - hmac384(key,KEY_SIGN_SIZE,b+SIGN_OFF,b+RED_OFF,RED_SIZE); + hmac_sha384(b+SIGN_OFF,key,KEY_SIGN_SIZE,b+RED_OFF,RED_SIZE); } bool black_verify(uint8_t const *b,uint8_t const *key){ uint8_t sign[SIGN_SIZE]; - hmac384(key,KEY_SIGN_SIZE,sign,b+RED_OFF,RED_SIZE); + hmac_sha384(sign,key,KEY_SIGN_SIZE,b+RED_OFF,RED_SIZE); return memcmp(sign,b+SIGN_OFF,SIGN_SIZE) == 0; } diff -uNr a/src/chain.c b/src/chain.c --- a/src/chain.c d8ac0a8af55fce7f0ff03873aa6b0c97501573fc77ae0181fa686e053f6e1fb8942ada184a83257e1460b43371a252c986ca96c33be474a1889e78dbc8c3b4f1 +++ b/src/chain.c dc77e2f29c9efbadeeb16b273454dbdfac4716d5cf412f824e75928e31c19488096b040b7bda8baffda053ed7989a8acf6aca2de06be6a85ac79cbc70089d745 @@ -382,7 +382,7 @@ for(size_t x = 0;x < c->cha_cnt;x++){ char path_del[PATH_BUF]; cha_path(path_del,sizeof(path_del),c,x); - if(!file_delete(path_del)){ + if(!file_delete_zero(path_del)){ ret = 0; goto ret; } diff -uNr a/src/config.h b/src/config.h --- a/src/config.h bc0c706f51b41ef94144396c301e73c72521c03aaa65fb27bc02f2779bc52f666d4c53da877faa081b7163176403097c2ef3abd573b9dae5c1ba83f6b35de54c +++ b/src/config.h 6b094b264c5e4e31d80f16998d83a1b9eb444b81245319e790db98e6573a593dfabf9dff739ca18c7b7ede6b9cd5b53c9c6cdd8e76584b0aa5c9be52b04d2904 @@ -1,8 +1,9 @@ #ifndef CONFIG_H #define CONFIG_H -#define RNG_PATH "/dev/random" //entropy source file -#define STACK_WIPE_SIZE (4*MEGABYTE) //amount of stack to zeroize/randomize +#define RNG_SLOW_PATH "/dev/random" //entropy source file (for key gen strictly) +#define RNG_FAST_PATH "/dev/urandom" //entropy source file (for everything else) +#define STACK_WIPE_SIZE (4*MEGABYTE) //amount of stack to zeroize/randomize //#define BIG_ENDIAN //system endianess #define NET_SILENT_SLEEP 10 //milliseconds to sleep when no tcp/udp @@ -28,7 +29,6 @@ #define MESS_MSG_POW2 15 //message files max entries in (2^x, filesize = 2^x*428) #define MESS_MSG_ZEROS 5 //message files number of zeros in -#define NOTICE_CHAN //send NOTICE messages to #pest channel (chain warn. always sent) #define CONSOLE_STAMP_FMT "%F %T" //stamp format for console output #define GETDATA_STAMP_FMT "[%T]" //stamp format for old messages #define GETDATA_OLDSTAMP_FMT "[%F %T]" //stamp format for >24h old messages diff -uNr a/src/console.c b/src/console.c --- a/src/console.c 94cf38d774c4c504af62439657e89c7be689e44b3becf399c146ebe3bea015f4c5f51da780291a040d21f5c6f3e55ecd59bb545550f880bf1727deb9a5946dc2 +++ b/src/console.c 67ec039ed9cef40b22c7cee1f0dfd7721487267b31b1b20ab99c570c067cca120d1eba8d1fa0c5f70be7a98411f17fdf7b2b24ec59eff578c5699cf83ad25349 @@ -1,18 +1,19 @@ #include "pest.h" +#include "irc.h" #include "time.h" #include "serial.h" #include "log.h" #include "def.h" #include "util.h" #include "string.h" +#include "utf8.h" #include "config.h" #include "state.h" #define TEXT_STAMP_BUF 256 -extern pestev_f pest_func; - -void console_send(peer_t *chan, +void console_send(peer_t *p, + peer_t *chan, char const *speaker, uint8_t const *m, bool stampold){ @@ -22,7 +23,6 @@ char *t = text; uint64_t stamp; uint64_t *con; - unsigned e = chan ? PEST_DIRECT : PEST_BROADCAST; con = chan ? &chan->con_stamp : &con_stamp; read_u64(m+TIMESTAMP_OFF,&stamp); @@ -35,8 +35,19 @@ } cpy_text(t,text_end-t,m); - if(pest_func){ - pest_func(e,speaker,text); + + //put error but send message anyway + if(!utf8_valid(t)){ + irclog_err("message from peer '%s' contains invalid UTF-8 sequence", + p->handle[0]); + } + + if(chan){ + //direct message + irc_privmsg(speaker,text,self_handle); + }else if(irc_joined){ + //broadcast + irc_privmsg(speaker,text,irc_chan); } if(!stampold){ diff -uNr a/src/crypto.h b/src/crypto.h --- a/src/crypto.h 84f4e59b2bf2ab51ccce05f2480fe2beb4da2715dc436094f983a0a1ced837f441be52dec5b5b7a3a68b3365cbf0e7b97e1437a7e0253e41077cb0307baebd4d +++ b/src/crypto.h 3a3b59be0a98bf4ea49825354a2f0533dfca9f6d92631043d24715e2460276b80a4a6d90d5476d8a41f3ec6e8a8ab4a837b881476c03dd397c52d3474ad81154 @@ -12,20 +12,20 @@ void serpent_cbc_ciph (uint8_t const *skey,uint8_t *dst,uint8_t const *src,size_t size); void serpent_cbc_deciph(uint8_t const *skey,uint8_t *dst,uint8_t const *src,size_t size); -void sha256 (uint8_t *sha,uint8_t const *data,size_t data_len); -void sha384 (uint8_t *sha,uint8_t const *data,size_t data_len); -void sha512 (uint8_t *sha,uint8_t const *data,size_t data_len); +void sha256 (uint8_t *out,uint8_t const *data,size_t size); +void sha384 (uint8_t *out,uint8_t const *data,size_t size); +void sha512 (uint8_t *out,uint8_t const *data,size_t size); -void hmac384 (uint8_t const *key,size_t key_size,uint8_t *mac,uint8_t const *msg,size_t msg_size); +void hmac_sha384 (uint8_t *out,uint8_t const *key,size_t key_size,uint8_t const *data,size_t data_size); size_t base64_dec_size (char const *in,bool nullterm); size_t base64_dec (char const *in,uint8_t *out,size_t out_size,bool nullterm); size_t base64_enc (uint8_t const *in,size_t in_size,char *out,size_t out_size); -bool rng_open (char const *path); -void rng_close (void); -bool rng_read (void *data,size_t size); -bool rng_read_nolog (void *data,size_t size); +int rng_open (char const *path); +void rng_close (int fd); +bool rng_read (int fd,void *data,size_t size); +bool rng_read_nolog (int fd,void *data,size_t size); inline void hash_data (hash_t h,void const *data,size_t size); inline void hash_zero (hash_t h); diff -uNr a/src/dedup.c b/src/dedup.c --- a/src/dedup.c 04b85ef6ded28c54755d831d7b361b90a231b7a5ed267ccfb34ae73a0e6454e721b12a34ed2442f60d5a606d8027e5ef3698d0541d4ea06eec3f4c75fadfaf42 +++ b/src/dedup.c b515dad9c4bb7cc5d0619b1baeca8f0bea80e0ce72281bd9fa73661a8fdef15cd213f297912cf00e37b84ce508bc1392e5ed40eafc21a41b2a6fe9116bcfc95b @@ -208,7 +208,7 @@ static size_t dedup_lookup_idx(uint8_t const *hash){ size_t cnt = dedup_cnt(); - for(size_t x = 0;x < cnt;x++){ + for(size_t x = cnt-1;x < (size_t)-1;x--){ size_t idx = dedup_f+x & DEDUP_MAX-1; hash_t *h = dedup_h+idx; if(memcmp(h,hash,sizeof(*h)) == 0) return idx; diff -uNr a/src/def.h b/src/def.h --- a/src/def.h b2cc3c0e4af257e1fcee75219f9496ba9a32a67b8d32edc14974bf67815a28d04b37ca404b1ec82c04bec57b7e899f66ae1537eb5eaf44a7d2e2dcb2dbb2f1ff +++ b/src/def.h 1638ad2755d86caee6036468b97b9daac5b734ac339b5cfc39e7ab9215fdd54533d1ba203e325aee144083bc5faf3eb67f3901f32b59e7e74f2fac6308228445 @@ -2,8 +2,9 @@ #define DEF_H #define PROG_NAME "smalpest" -#define PROG_VERSION "98K" +#define PROG_VERSION "97K" #define PROG_FULLNAME PROG_NAME "-" PROG_VERSION +#define PROG_FULLERNAME PROG_FULLNAME " (spec " STR(PEST_VERSION) ")" #define INT64_LEN 20 //18446744073709551616 #define KILOBYTE (1024) @@ -78,9 +79,11 @@ #define IRC_PEERHOST "pest.net" #define PEST_PORT_DEF 7778 +#define PEST_ARG_MAX 2 +#define PEST_CMD_LEN 31 #define HANDLE_LEN SPEAKER_SIZE #define HANDLE_MAX 8 -#define HANDLE_CHARSET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-" +#define HANDLE_CHARSET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_" #define KEY_MAX 8 #define DEDUP_MAX (1<hash,HASH_SIZE,chan); + send_text(CMD_GETDATA,mono_sec(),g->hash,HASH_SIZE,chan,0); g->ntime = mono_nsec(); g->attempt++; } @@ -131,12 +131,10 @@ if(mess_lookup(&mess,hash,m) == LOOKUP_FOUND){ send_message(CMD_BROADCAST,r,hash,p,0); - return; - } - - if(mess_lookup(&p->mess,hash,m) == LOOKUP_FOUND){ + }else if(mess_lookup(&p->mess,hash,m) == LOOKUP_FOUND){ send_message(CMD_DIRECT,r,hash,p,0); - return; } + + BUF_ZERO(r); } diff -uNr a/src/hmac.c b/src/hmac.c --- a/src/hmac.c ae4aaf9b4f8f0cb090f97b09437beda1f8920fdb2fd9b41482145962f4bb1364c3674ee7f3d1d40a38600554fc6dc17533f0bcdc8aba3f5ae6c92d3c8eb47b81 +++ b/src/hmac.c false @@ -1,17 +0,0 @@ -#include "crypto.h" -#include "otherpplgode/hmac.h" -#include - -void hmac384(uint8_t const *key, - size_t key_size, - uint8_t *mac, - uint8_t const *msg, - size_t msg_size){ - - hmac_sha384_ctx ctx; - hmac_sha384_init(&ctx,key,key_size); - hmac_sha384_update(&ctx,msg,msg_size); - hmac_sha384_final(&ctx,mac,HMAC384_SIZE); - VAR_ZERO(ctx); -} - diff -uNr a/src/ht.c b/src/ht.c --- a/src/ht.c c06466da05d832e6924802547162c73052e4e6e3e5f4d8bc14cd8691dafc227df5fca093c1bdab933d5776979ef469950cbfe29d469f43410ed53c0bfb9a261d +++ b/src/ht.c b749592f4dd037c308d2e67ceb9e2915ad787a105585a66d2b9bae76ee8aebfeadf8a7bf10678f8b7b51b081217aaa931828bada235e4d7ea57513322dba722e @@ -17,7 +17,7 @@ } bool ht_store(ht_t *ht){ - if(!hash_store(ht->path,(hash_t const *)ht->hash,ht->cnt)){ + if(!hashlist_store(ht->path,(hash_t const *)ht->hash,ht->cnt)){ disk_unsync(); return 0; } @@ -27,11 +27,11 @@ bool ht_load(ht_t *ht){ ht->last = -1; if(file_exists(ht->path)){ - return hash_load(ht->path,ht->hash,&ht->cnt,HEADTAIL_MAX); + return hashlist_load(ht->path,ht->hash,&ht->cnt,HEADTAIL_MAX); }else{ ht->cnt = 1; hash_zero(ht->hash[0]); - return hash_store(ht->path,(hash_t const *)ht->hash,ht->cnt); + return ht_store(ht); } } diff -uNr a/src/ignore.c b/src/ignore.c --- a/src/ignore.c c8e9ecb54d9133b64f7a2dd5ace6d36961863ef7db8356668f9be22c0d8e3971bb81861df01030e825bb147f6dbd739398f716efd3fdc97bd9d0ba9e79417641 +++ b/src/ignore.c 5dd8c8f76d5925a38883422a1c44cce70dea42a7b26f2fa177b29644cb99d4561071495c9380a1694cf199b9cdfcf21f64fd415e128ff7e5ac72812130f70798 @@ -7,13 +7,9 @@ #include bool ignore_send(peer_t *chan,pkey_t *pk){ - uint8_t r[RED_SIZE]; - uint8_t *m = r+MESSAGE_OFF; - hash_t hash; - - if(!rng_read(m,MESSAGE_SIZE)) return 0; - hash_data(hash,m,MESSAGE_SIZE); - return send_message(CMD_IGNORE,r,hash,chan,pk); + uint8_t text[TEXT_SIZE]; + if(!rng_read(rng_fast_fd,text,sizeof(text))) return 0; + return send_text(CMD_IGNORE,mono_sec(),text,sizeof(text),chan,pk); } void ignore_broadcast(uint64_t ntime){ @@ -23,7 +19,7 @@ if(ignore_next > 0){ ignore_send(0,0); } - if(!rng_read(&next,sizeof(next))) return; + if(!rng_read(rng_fast_fd,&next,sizeof(next))) return; next %= igint*NSEC_MSEC*2; ignore_next = ntime+next; } diff -uNr a/src/io.c b/src/io.c --- a/src/io.c e679727077b82fe8ead7b7b37700f69d742d89cafc48fd9a50ff7ee222240bf3160c8ec2e893e0311451d5449d48254dd47548102dbac0e4dc9f9746e3622eb0 +++ b/src/io.c 28f5d89bab364223b4fe92e34001da03b823c3b6fd9ce7aa7514409fd7d1f24d9001867cb29f33bd27d57e0a3712320f1127c283a70789ebbe1c111a8074d46a @@ -5,8 +5,10 @@ #include "log.h" #include "util.h" #include "string.h" +#include "utf8.h" #include "def.h" #include "config.h" +#include "state.h" #include #include #include @@ -15,6 +17,135 @@ #include #include +static FILE *fp_open(char const *path,char const *mode,bool log){ + FILE *f = fopen(path,mode); + if(!f && log){ + log_errno("fopen"); + irclog_err("failed to open file: '%s'",path); + } + return f; +} + +static bool fp_close(FILE *f,bool log){ + int r = fclose(f); + if(r != 0){ + if(log){ + log_errno("fclose"); + irclog_err("failed to close file handle"); + } + return 0; + } + return 1; +} + +static bool fp_read(FILE *f,void *data,size_t size,bool log){ + size_t r = fread(data,1,size,f); + if(r != size){ + if(ferror(f)){ + if(log){ + log_errno("fread"); + irclog_err("failed to read from file"); + } + return 0; + }else{ + if(log){ + irclog_err("unexpected eof reached"); + } + return 0; + } + } + return 1; +} + +static bool fp_write(FILE *f,void const *data,size_t size,bool log){ + if(fwrite(data,1,size,f) != size){ + if(log){ + log_errno("fwrite"); + irclog_err("failed to write to file"); + } + return 0; + } + return 1; +} + +static bool fp_seek(FILE *f,size_t off,int origin,bool log){ + if(fseek(f,off,origin) != 0){ + if(log){ + log_errno("fseek"); + irclog_err("failed to seek in file"); + } + return 0; + } + return 1; +} + +static bool fp_tell(FILE *f,size_t *pos,bool log){ + intptr_t ipos; + ipos = ftell(f); + if(ipos < 0){ + if(log){ + log_errno("ftell"); + irclog_err("failed to get file stream position"); + } + return 0; + } + *pos = ipos; + return 1; +} + +static bool fp_gets(FILE *f,void *buf,size_t buf_size,bool log){ + char *r = fgets(buf,buf_size,f); + if(!r && ferror(f)){ + if(log){ + log_errno("fgets"); + irclog_err("failed to read from file"); + } + } + return r != 0; +} + +static bool fp_printf(FILE *f,char const *fmt,va_list args,bool log){ + int r = vfprintf(f,fmt,args); + if(r < 0){ + if(log){ + log_errno("fprintf"); + irclog_err("failed to write to file"); + } + return 0; + } + return 1; +} + +static bool fp_size(FILE *f,size_t *size,bool log){ + if(!fp_seek(f,0,SEEK_END,log)) return 0; + if(!fp_tell(f,size,log)) return 0; + if(!fp_seek(f,0,SEEK_SET,log)) return 0; + return 1; +} + +static bool fp_zero(FILE *f,bool log){ + uint8_t buf[4096]; + size_t size; + + if(!fp_size(f,&size,log)) return 0; + + BUF_ZERO(buf); + while(size > 0){ + size_t w_size = size; + if(w_size > sizeof(buf)){ + w_size = sizeof(buf); + } + if(!fp_write(f,buf,w_size,log)){ + return 0; + } + size -= w_size; + } + + if(!fp_seek(f,0,SEEK_SET,log)) return 0; + + return 1; +} + //TODO: chek errno osv bool file_exists(char const *path){ return access(path,F_OK) == 0; @@ -41,6 +172,17 @@ } } +bool file_delete_zero(char const *path){ + if(file_exists(path) && !file_zero(path)){ + return 0; + } + return file_delete(path); +} + +bool file_delete_nolog(char const *path){ + return remove(path) == 0; +} + bool file_rename(char const *from,char const *to){ if(rename(from,to) == 0){ #ifdef LOG_IOSPAM @@ -68,80 +210,43 @@ } FILE *file_open(char const *path,char const *mode){ - FILE *f = fopen(path,mode); - if(!f){ - log_errno("fopen"); - irclog_err("failed to open file: '%s'",path); - } - return f; + return fp_open(path,mode,1); } -bool file_close(FILE *f){ - int r = fclose(f); - if(r != 0){ - log_errno("fclose"); - irclog_err("failed to close file handle"); +FILE *file_open_zero(char const *path,char const *mode){ + if(file_exists(path) && !file_zero(path)){ return 0; } - return 1; + return fp_open(path,mode,1); +} + +bool file_close(FILE *f){ + return fp_close(f,1); } bool file_seek(FILE *f,size_t off,int origin){ - if(fseek(f,off,origin) != 0){ - log_errno("fseek"); - irclog_err("failed to seek in file"); - return 0; - } - return 1; + return fp_seek(f,off,origin,1); } bool file_read(FILE *f,void *data,size_t size){ - size_t r = fread(data,1,size,f); - if(r != size){ - if(ferror(f)){ - log_errno("fread"); - irclog_err("failed to read from file"); - return 0; - }else{ - irclog_err("unexpected eof reached"); - return 0; - } - } - return 1; + return fp_read(f,data,size,1); } bool file_write(FILE *f,void const *data,size_t size){ - size_t r = fwrite(data,1,size,f); - if(r != size){ - log_errno("fwrite"); - irclog_err("failed to write to file"); - return 0; - } - return 1; + return fp_write(f,data,size,1); } bool file_gets(FILE *f,void *buf,size_t buf_size){ - char *r = fgets(buf,buf_size,f); - if(!r && ferror(f)){ - log_errno("fgets"); - irclog_err("failed to read from file"); - } - return r; + return fp_gets(f,buf,buf_size,1); } bool file_printf(FILE *f,char const *fmt,...){ + bool r; va_list args; - int r; - va_start(args,fmt); - r = vfprintf(f,fmt,args); + r = fp_printf(f,fmt,args,1); va_end(args); - if(r < 0){ - log_errno("fprintf"); - irclog_err("failed to write to file"); - return 0; - } - return 1; + return r; } bool mkdir_p(char const *path){ @@ -180,6 +285,17 @@ return file_delete(path) ? 0 : -1; } +static int ftw_rm_rf_nolog(char const *path, + struct stat const *sb, + int flag, + struct FTW *ftw){ + + (void)sb; + (void)flag; + (void)ftw; + return file_delete_nolog(path) ? 0 : -1; +} + bool rm_rf(char const *path){ if(nftw(path,ftw_rm_rf,64,FTW_DEPTH | FTW_PHYS) == 0){ return 1; @@ -190,11 +306,107 @@ } } +bool rm_rf_nolog(char const *path){ + return nftw(path,ftw_rm_rf_nolog,64,FTW_DEPTH | FTW_PHYS) == 0; +} + +static bool _file_zero(char const *path,bool log){ + FILE *f = fp_open(path,"rb+",log); + if(!f) return 0; + if(!fp_zero(f,log)){ + fclose(f); + return 0; + } + return fp_close(f,log); +} + +bool file_zero(char const *path){ + return _file_zero(path,1); +} + +bool file_zero_nolog(char const *path){ + return _file_zero(path,0); +} + +bool file_rand_nolog(char const *path){ + FILE *f; + size_t size; + uint8_t buf[4096]; + + f = fp_open(path,"rb+",0); + if(!f) return 0; + + if(!fp_size(f,&size,0)) return 0; + + while(size > 0){ + size_t w_size = size; + if(w_size > sizeof(buf)){ + w_size = sizeof(buf); + } + + if(!rng_read_nolog(rng_fast_fd,buf,w_size)){ + fclose(f); + return 0; + } + + if(!fp_write(f,buf,w_size,0)){ + fclose(f); + return 0; + } + + size -= w_size; + } + + if(fclose(f) != 0) return 0; + return 1; +} + +static int ftw_zero(char const *path, + struct stat const *sb, + int flag, + struct FTW *ftw){ + + (void)sb; + (void)ftw; + if(flag == FTW_F){ + file_zero_nolog(path); + } + return 0; +} + +static int ftw_rand(char const *path, + struct stat const *sb, + int flag, + struct FTW *ftw){ + + (void)sb; + (void)ftw; + if(flag == FTW_F){ + file_rand_nolog(path); + } + return 0; +} + +static bool dir_wipe(char const *path,bool rand){ + return nftw(path, + rand ? ftw_rand : ftw_zero, + 64, + FTW_PHYS) == 0; +} + +bool dir_zero_nolog(char const *path){ + return dir_wipe(path,0); +} + +bool dir_rand_nolog(char const *path){ + return dir_wipe(path,1); +} + bool str_store(char const *path,char const *str){ FILE *f; size_t str_len; - f = file_open(path,"wb"); + f = file_open_zero(path,"wb"); if(!f) return 0; str_len = strlen(str); @@ -222,8 +434,8 @@ size = (size_t)sb.st_size; if(size+1 > buf_size){ - irclog_err("file size + terminator exceeds " - "buffer size: '%s' (%zu > %zu)", + irclog_err("file '%s' size + terminator exceeds " + "buffer size: %zu > %zu", path,size+1,buf_size); return 0; } @@ -241,6 +453,12 @@ file_close(f); disk_read(path); + + if(!utf8_valid(buf)){ + irclog_err("file '%s' contains invalid UTF-8 sequence"); + return 0; + } + return 1; } @@ -270,10 +488,21 @@ return 1; } -bool hash_store(char const *path,hash_t const *h,size_t cnt){ +bool hash_store(char const *path,hash_t const h){ + return hashlist_store(path,(hash_t const *)h,1); +} + +bool hash_load(char const *path,hash_t h){ + size_t cnt; + if(!hashlist_load(path,(hash_t *)h,&cnt,1)) return 0; + if(cnt != 1) return 0; + return 1; +} + +bool hashlist_store(char const *path,hash_t const *h,size_t cnt){ FILE *f; - f = file_open(path,"wb"); + f = file_open_zero(path,"wb"); if(!f) return 0; for(size_t x = 0;x < cnt;x++){ @@ -303,7 +532,7 @@ return 1; } -bool hash_load(char const *path,hash_t *h,size_t *cnt,size_t max){ +bool hashlist_load(char const *path,hash_t *h,size_t *cnt,size_t max){ FILE *f; char buf[HASH_B64_LEN+WHITE_LEN+1+1]; //base64(hash)\n\0 char *b; diff -uNr a/src/io.h b/src/io.h --- a/src/io.h 233728f1fe2e5612be28ab7d7f659b0f75657707b6cb7996469f966fe59032908f5083fb78710e5f02ab927a93edf909c4f952ec466653d2eb8698bae87b76b5 +++ b/src/io.h 16e2598a4feddf8fcb509e4d7991c5b2469eb79d5988d3be8a98e520e515fabfbdcf7252cd9d5cc99bc7e3077ae157bce2e040ac640f69ab384f1529d11e17bf @@ -8,39 +8,51 @@ #include #include -bool file_exists (char const *path); -bool file_touch (char const *path); -bool file_delete (char const *path); -bool file_rename (char const *from,char const *to); -bool file_stat (char const *path,struct stat *sb); - -FILE *file_open (char const *path,char const *mode); -bool file_close (FILE *f); -bool file_seek (FILE *f,size_t off,int origin); -bool file_read (FILE *f,void *data,size_t size); -bool file_write (FILE *f,void const *data,size_t size); -bool file_gets (FILE *f,void *buf,size_t buf_size); -bool file_printf (FILE *f,char const *fmt,...); - -bool mkdir_p (char const *path); -bool rm_rf (char const *path); - -bool str_store (char const *path,char const *str); -bool str_load (char const *path,char *buf,size_t buf_size); -bool int_store (char const *path,int64_t i); -bool int_load (char const *path,int64_t *i); -bool bool_store (char const *path,bool b); -bool bool_load (char const *path,bool *b); -bool hash_store (char const *path,hash_t const *h,size_t cnt); -bool hash_load (char const *path,hash_t *h,size_t *cnt,size_t max); -bool pass_store (char const *path,uint8_t const *pass); -bool pass_load (char const *path,uint8_t *pass); -bool addr_store (char const *path,addr_t const *addr); -bool addr_load (char const *path,addr_t *addr); - -void disk_read (char const *path); -void disk_written (char const *path); -void disk_unsync (void); +bool file_exists (char const *path); +bool file_touch (char const *path); +bool file_delete (char const *path); +bool file_delete_zero (char const *path); +bool file_delete_nolog (char const *path); +bool file_rename (char const *from,char const *to); +bool file_stat (char const *path,struct stat *sb); + +FILE *file_open (char const *path,char const *mode); +FILE *file_open_zero (char const *path,char const *mode); +bool file_close (FILE *f); +bool file_seek (FILE *f,size_t off,int origin); +bool file_read (FILE *f,void *data,size_t size); +bool file_write (FILE *f,void const *data,size_t size); +bool file_gets (FILE *f,void *buf,size_t buf_size); +bool file_printf (FILE *f,char const *fmt,...); + +bool mkdir_p (char const *path); +bool rm_rf (char const *path); +bool rm_rf_nolog (char const *path); + +bool file_zero (char const *path); +bool file_zero_nolog (char const *path); +bool file_rand_nolog (char const *path); +bool dir_zero_nolog (char const *path); +bool dir_rand_nolog (char const *path); + +bool str_store (char const *path,char const *str); +bool str_load (char const *path,char *buf,size_t buf_size); +bool int_store (char const *path,int64_t i); +bool int_load (char const *path,int64_t *i); +bool bool_store (char const *path,bool b); +bool bool_load (char const *path,bool *b); +bool hash_store (char const *path,hash_t const h); +bool hash_load (char const *path,hash_t h); +bool hashlist_store (char const *path,hash_t const *h,size_t cnt); +bool hashlist_load (char const *path,hash_t *h,size_t *cnt,size_t max); +bool pass_store (char const *path,uint8_t const *pass); +bool pass_load (char const *path,uint8_t *pass); +bool addr_store (char const *path,addr_t const *addr); +bool addr_load (char const *path,addr_t *addr); + +void disk_read (char const *path); +void disk_written (char const *path); +void disk_unsync (void); #endif diff -uNr a/src/irc.c b/src/irc.c --- a/src/irc.c a6584a2c9a42d35faeb4306e3da03c3b6f02dbfa880d252cf7facd2d545e998c481059eab15e8d52091c19d086e6ea3fd80bc41522fcbd5cc5af224dbe139c66 +++ b/src/irc.c 6f114c7375a568b3f81b5232e4440012270066aba26e47e0688855f9b6b2b95e4e9fe65f824c08048d9425aa1d67225a866e695842853b916e80c0a3c5588e77 @@ -1,5 +1,5 @@ +#include "irc.h" #include "ircd.h" -#include "addr.h" #include "pest.h" #include "peer.h" #include "crypto.h" @@ -13,6 +13,28 @@ #include #include +void irc_privmsg(char const *speaker, + char const *text, + char const *chan){ + + if(!irc_ready) return; + ircd_sendf(":%s PRIVMSG %s :%s",speaker,chan,text); +} + +void irc_join(char const *speaker){ + char host[IRC_HOST_BUF]; + if(!irc_ready) return; + fmt_peerhost(host,sizeof(host),speaker); + ircd_sendf(":%s JOIN %s",host,irc_chan); +} + +void irc_part(char const *speaker){ + char host[IRC_HOST_BUF]; + if(!irc_ready) return; + fmt_peerhost(host,sizeof(host),speaker); + ircd_sendf(":%s PART %s",host,irc_chan); +} + static void irc_sendmotd(void){ ircd_sendf(":%s 375 %s :- %s Message of the Day -", IRC_SERV,self_handle,IRC_SERV); @@ -32,8 +54,8 @@ ircd_sendf(":%s 001 %s :Welcome to pest %s!%s@%s", IRC_SERV,self_handle,self_handle,irc_user,irc_host); - ircd_sendf(":%s 002 %s :Your host is %s, running version " - PROG_FULLNAME, + ircd_sendf(":%s 002 %s :Your host is %s, " + "running version " PROG_FULLNAME, IRC_SERV,self_handle,IRC_SERV,PROG_VERSION); ircd_sendf(":%s 003 %s :This server was created at some point", IRC_SERV,self_handle); @@ -68,7 +90,7 @@ static void ircop_user(ircmsg_t const *m){ if(irc_usered) return; - if(m->argc < 1 || m->argc > 3){ + if(m->argc < 1 || m->argc > 4){ return; } @@ -115,13 +137,13 @@ char *handle = m->argv[0]; if(!str_valid(handle,1,HANDLE_LEN,HANDLE_CHARSET)){ ircd_sendf(":%s 432 %s :Erroneus handle",IRC_SERV,handle); - irclog_err("invalid handle: '%s'",handle); + irc_err("invalid handle: '%s'",handle); return; } if(peer_lookup_noerr(handle)){ ircd_sendf(":%s 433 %s :Nickname is already in use",IRC_SERV,handle); - irclog_err("handle '%s' already exists in WOT",handle); + irc_err("handle '%s' already exists in WOT",handle); return; } @@ -133,14 +155,14 @@ } static void ircop_join(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: JOIN channel"); + if(m->argc < 1 || m->argc > 2){ + irc_out("usage: JOIN channel [key]"); return; } char *chan = m->argv[0]; if(chan[0] != '#' || strlen(chan) > IRC_CHAN_LEN){ - irclog_err("invalid channel name: '%s'",chan); + irc_err("invalid channel name: '%s'",chan); return; } @@ -157,8 +179,8 @@ } static void ircop_part(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: PART channel"); + if(m->argc < 1 || m->argc > 2){ + irc_out("usage: PART channel [message]"); return; } @@ -175,13 +197,13 @@ } static void ircop_privmsg(ircmsg_t const *m){ - if(m->argc != 1 || !m->trail){ - irc_out("usage: PRIVMSG [channel | handle] :message"); + if(m->argc != 2){ + irc_out("usage: PRIVMSG channel|handle message"); return; } char *chan = m->argv[0]; - char *msg = m->trail; + char *msg = m->argv[1]; if(chan[0] == '#'){ if(strncmpci(chan,irc_chan,IRC_CHAN_LEN) != 0){ @@ -189,14 +211,14 @@ return; } pest_broadcast(msg); - }else if(!pest_direct(chan,msg)){ - ircd_sendf(":%s 401 %s :No such nick",IRC_SERV,chan); + }else{ + pest_direct(chan,msg); } } static void ircop_names(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: NAMES channel"); + if(m->argc < 1 || m->argc > 2){ + irc_out("usage: NAMES channel [target]"); return; } @@ -205,13 +227,13 @@ } static void ircop_version(ircmsg_t const *m){ - if(m->argc != 0){ - irc_out("usage: VERSION"); + if(m->argc > 1){ + irc_out("usage: VERSION [target]"); return; } - ircd_sendf(":%s 351 %s Pest-0x%02X. %s :" PROG_FULLNAME, - IRC_SERV,self_handle,PEST_VERSION,IRC_SERV); + ircd_sendf(":%s 351 %s %s. %s :spec " STR(PEST_VERSION), + IRC_SERV,self_handle,PROG_FULLNAME,IRC_SERV); } static void ircop_motd(ircmsg_t const *m){ @@ -224,409 +246,23 @@ } static void ircop_ping(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: PING server"); + if(m->argc > 2){ + irc_out("usage: PING [server1 [server2]]"); return; } - char *serv = m->argv[0]; (void)serv; ircd_sendf(":%s PONG %s :%s",IRC_SERV,IRC_SERV,IRC_SERV); } static void ircop_quit(ircmsg_t const *m){ - (void)m; - log_out("operator disconnected"); - ircd_ckill(); -} - -static void ircop_wot(ircmsg_t const *m){ - if(m->argc > 1){ - irc_out("usage: WOT [handle]"); - return; - } - - if(m->argc > 0){ - char *handle = m->argv[0]; - peer_t *p = peer_lookup(handle); - if(!p) return; - peer_print(p); - }else{ - peer_wot_print(); - } -} - -static void ircop_peer(ircmsg_t const *m){ - char host[IRC_HOST_BUF]; - peer_t *p; - - if(m->argc != 1){ - irc_out("usage: PEER handle"); - return; - } - - char *handle = m->argv[0]; - p = peer_add(handle); - if(!p) return; - fmt_peerhost(host,sizeof(host),p->handle[0]); - //ircd_sendf(":%s JOIN %s",host,irc_chan); -} - -static void ircop_unpeer(ircmsg_t const *m){ - char host[IRC_HOST_BUF]; - - if(m->argc != 1){ - irc_out("usage: UNPEER handle"); - return; - } - - char *handle = m->argv[0]; - peer_t *p = peer_lookup(handle); - if(!p) return; - fmt_peerhost(host,sizeof(host),p->handle[0]); - if(p->active){ - ircd_sendf(":%s PART %s",host,irc_chan); - } - peer_del(p); -} - -static void ircop_aka(ircmsg_t const *m){ - if(m->argc != 2){ - irc_out("usage: AKA handle alias"); - return; - } - - char *handle = m->argv[0]; - char *alias = m->argv[1]; - peer_t *p = peer_lookup(handle); - if(!p) return; - if(!peer_alias_add(p,alias)) return; -} - -static void ircop_unaka(ircmsg_t const *m){ - char host[IRC_HOST_BUF]; - - if(m->argc != 1){ - irc_out("usage: UNAKA handle"); - return; - } - - char *handle = m->argv[0]; - peer_t *p = peer_lookup(handle); - if(!p) return; - - fmt_peerhost(host,sizeof(host),p->handle[0]); - unsigned r = peer_alias_del(p,handle); - if(!r) return; - if(r == 2){ //TODO: this is disgusting but works - ircd_sendf(":%s NICK :%s",host,p->handle[0]); - } -} - -static void ircop_pause(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: PAUSE handle"); - return; - } - - char *handle = m->argv[0]; - peer_t *p = peer_lookup(handle); - if(!p) return; - peer_pause(p,1); -} - -static void ircop_unpause(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: UNPAUSE handle"); - return; - } - - char *handle = m->argv[0]; - peer_t *p = peer_lookup(handle); - if(!p) return; - peer_pause(p,0); -} - -static void ircop_key(ircmsg_t const *m){ - if(m->argc != 2){ - irc_out("usage: KEY handle key"); - return; - } - - uint8_t key[KEY_SIZE]; - char *handle = m->argv[0]; - char *b64_key = m->argv[1]; - peer_t *p = peer_lookup(handle); - if(!p) return; - - if(!key_from_b64(key,b64_key)){ - irc_err("invalid key: '%s'",b64_key); - key_zero(key); - return; - } - - peer_key_add(p,key,0); - key_zero(key); -} - -static void ircop_unkey(ircmsg_t const *m){ - uint8_t key[KEY_SIZE]; - - if(m->argc != 1){ - irc_out("usage: UNKEY key"); - return; - } - - char *b64_key = m->argv[0]; - if(!key_from_b64(key,b64_key)){ - irc_err("invalid key: '%s'",b64_key); - key_zero(key); - return; - } - - peer_key_del(key,0); - key_zero(key); -} - -static void ircop_genkey(ircmsg_t const *m){ - uint8_t key [KEY_SIZE]; - char b64_key [B64_KEY_BUF]; - - if(m->argc != 0){ - irc_out("usage: GENKEY"); - return; - } - - if(!key_gen(key)){ - irclog_err("could not generate key"); - return; - } - - b64_from_key(b64_key,sizeof(b64_key),key); - irc_out("key generated from '%s': %s",RNG_PATH,b64_key); - key_zero(key); - BUF_ZERO(b64_key); -} - -static void ircop_rekey(ircmsg_t const *m){ if(m->argc > 1){ - irc_out("usage: REKEY [handle]"); + irc_out("usage: QUIT [message]"); return; } - if(m->argc == 0){ - rekey_initiate(0,1); - }else{ - char *handle = m->argv[0]; - peer_t *p = peer_lookup(handle); - if(!p) return; - rekey_initiate(p,1); - } -} - -static void ircop_rktog(ircmsg_t const *m){ - if(m->argc > 1){ - usage: - irc_out("usage: RKTOG ['ENABLE'/'DISABLE']"); - return; - } - - if(m->argc == 0){ - knob_display(FILE_RKRECV,0); - }else if(strcmpci(m->argv[0],"ENABLE") == 0){ - knob_set(FILE_RKRECV,"1"); - }else if(strcmpci(m->argv[0],"DISABLE") == 0){ - knob_set(FILE_RKRECV,"0"); - }else{ - goto usage; - } -} - -static void ircop_gag(ircmsg_t const *m){ - if(m->argc > 1){ - irc_out("usage: GAG [handle]"); - return; - } - - if(m->argc == 0){ - killfile_print(); - }else{ - char *handle = m->argv[0]; - killfile_add(handle); - } -} - -static void ircop_ungag(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: UNGAG handle"); - return; - } - - char *handle = m->argv[0]; - killfile_del(handle); -} - -static void ircop_at(ircmsg_t const *m){ - if(m->argc > 2){ - irc_out("usage: AT [handle [ip:port]]"); - return; - } - - if(m->argc == 0){ - peer_at_print(); - }else if(m->argc == 1){ - char *handle = m->argv[0]; - peer_t *p = peer_lookup(handle); - if(!p) return; - if(addr_null(&p->addr)){ - irclog_out("no address stored for peer '%s'",handle); - return; - } - peer_addr_print(p); - }else{ - char *handle = m->argv[0]; - char *at = m->argv[1]; - peer_t *p = peer_lookup(handle); - addr_t addr; - - if(!p) return; - if(!addr_from_at(&addr,at)){ - irclog_err("invalid address (format is ip:port): '%s'",at); - return; - } - - //char host[IRC_HOST_BUF]; - //fmt_peerhost(host,sizeof(host),p->handle[0]); - //ircd_sendf(":%s PART %s",host,irc_chan); - //peer_addr_set(p,&addr); - //fmt_peerhost(host,sizeof(host),p->handle[0]); - //ircd_sendf(":%s JOIN %s",host,irc_chan); - peer_addr_set(p,&addr,1); - } -} - -static void ircop_knob(ircmsg_t const *m){ - if((!m->trail && m->argc > 2) || - ( m->trail && m->argc != 1)){ - irc_out("usage: KNOB [knob [[:]value]]"); - return; - } - - if(m->argc == 0){ - knob_display(0,0); - }else if(m->trail){ - char *knob = m->argv[0]; - char *value = m->trail; - knob_set(knob,value); - }else if(m->argc == 1){ - char *knob = m->argv[0]; - knob_display(knob,0); - }else{ - char *knob = m->argv[0]; - char *value = m->argv[1]; - knob_set(knob,value); - } -} - -static void ircop_knobinfo(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: KNOBINFO knob"); - return; - } - - char *knob = m->argv[0]; - knob_display(knob,1); -} - -static void ircop_cut(ircmsg_t const *m){ - if(m->argc > 1){ - irc_out("usage: CUT [cutoff]"); - return; - } - - if(m->argc == 0){ - knob_display(FILE_CUTOFF,0); - }else{ - knob_set(FILE_CUTOFF,m->argv[0]); - } -} - -static void ircop_resolve(ircmsg_t const *m){ - if(m->argc != 1){ - irc_out("usage: RESOLVE handle"); - return; - } - - char *handle = m->argv[0]; - pest_resolve(handle); -} - -static void ircop_slave(ircmsg_t const *m){ - if(m->argc > 1){ - irc_out("usage: SLAVE [handle]"); - return; - } - - if(!m->argc){ - if(*master){ - irc_out("slave mode is enabled (master = '%s')",master); - }else{ - irc_out("slave mode is disabled"); - } - return; - } - - if(*master){ - irc_err("slave mode already enabled"); - return; - } - - char *handle = m->argv[0]; - knob_set(FILE_MASTER,handle); - irc_out("slave mode enabled"); -} - -static void ircop_unslave(ircmsg_t const *m){ - if(m->argc != 0){ - irc_out("usage: UNSLAVE"); - return; - } - - if(!*master){ - irc_err("slave mode already disabled"); - return; - } - - knob_set(FILE_MASTER,""); - irc_out("slave mode disabled"); -} - -static void ircop_achtung(ircmsg_t const *m){ - if(m->argc != 0 || !m->trail){ - irc_out("usage: ACHTUNG :message"); - return; - } - - char *msg = m->trail; - pest_achtung(msg); -} - -static void ircop_scram(ircmsg_t const *m){ - char *pwd; - uint8_t pass[SHA512_SIZE]; - - if(m->argc != 1){ - irc_out("usage: SCRAM scrampass"); - return; - } - - pwd = m->argv[0]; - sha512(pass,(uint8_t *)pwd,strlen(pwd)); - if(memcmp(pass,scram_pass,SHA512_SIZE) != 0){ - irclog_err("SCRAM password mismatch"); - return; - } - - scram_push(); + (void)m; + log_out("operator disconnected"); + ircd_ckill(); } #define IRC_OP(N) { #N, 0, ircop_##N }, @@ -659,33 +295,8 @@ IRC_OP_IGNORE (whois) IRC_OP_IGNORE (whowas) IRC_OP (quit) - - IRC_OP (wot) - IRC_OP (peer) - IRC_OP (unpeer) - IRC_OP (aka) - IRC_OP (unaka) - IRC_OP (pause) - IRC_OP (unpause) - IRC_OP (key) - IRC_OP (unkey) - IRC_OP (genkey) - IRC_OP (rekey) - IRC_OP (rktog) - IRC_OP (gag) - IRC_OP (ungag) - IRC_OP (at) - IRC_OP (knob) - IRC_OP (knobinfo) - IRC_OP (cut) - IRC_OP (resolve) - IRC_OP (slave) - IRC_OP (unslave) - IRC_OP (achtung) - IRC_OP (scram) }; -void irc_ev(unsigned e,ircmsg_t const *msg); void irc_ev(unsigned e,ircmsg_t const *msg){ switch(e){ char cmd[IRCD_CMD_BUF]; @@ -708,16 +319,16 @@ case IRCD_MESSAGE: for(size_t x = 0;x < ARR_CNT(irc_op);x++){ ircop_t const *op = irc_op+x; - if(strncmp(msg->cmd,op->cmd,IRCD_CMD_BUF) != 0) continue; + if(strncmpci(msg->cmd,op->cmd,IRCD_CMD_BUF) != 0) continue; if(op->init || irc_ready){ if(op->func) op->func(msg); } return; } - strncpy(cmd,msg->cmd,sizeof(cmd)); + strzcpy(cmd,msg->cmd,sizeof(cmd)); str_toupper(cmd); - irclog_err("unknown/unhandled IRC command received: %s",cmd); + irc_err("unknown/unhandled IRC command received: %s",cmd); break; default: diff -uNr a/src/irc.h b/src/irc.h --- a/src/irc.h false +++ b/src/irc.h 455d42600aafb0abf044d8640af21c116aa1b7b23ae48dc11d2b2ea6384da40e30e47f47ebd17bf5fedee9a37eb1dcace38c8c41725a139b98aa79cae252256c @@ -0,0 +1,13 @@ +#ifndef IRC_H +#define IRC_H + +#include "type.h" + +void irc_privmsg (char const *speaker,char const *text,char const *chan); +void irc_join (char const *speaker); +void irc_part (char const *speaker); + +void irc_ev (unsigned e,ircmsg_t const *msg); + +#endif + diff -uNr a/src/ircd.c b/src/ircd.c --- a/src/ircd.c a9db131d8fbb8502f14679302097bd60ea5051728b404b20c37c8cd52b040ec673406b52f8b138f75df56f3e63652558c7ee7ba5cdaeb546851fcad460210329 +++ b/src/ircd.c a19d47e908d15f7719d74c87bfe84bc037a0fd2bfb3d4c97d3fad4e8fd4ef25cf275650e3f01005f40f8cb7a2398396ab60af6a0578e0fb1cedc81ad7b450b39 @@ -3,6 +3,7 @@ #include "crypto.h" #include "util.h" #include "string.h" +#include "utf8.h" #include "log.h" #include "config.h" #include "state.h" @@ -18,6 +19,7 @@ case TCP_DISCONNECT: if(ircd_func) ircd_func(IRCD_DISCONNECT,0); + ircd_buf_p = ircd_buf; break; default: @@ -85,19 +87,16 @@ while(1){ c = strnotchr0(c,' '); if(*c == 0) break; - if(*c == ':'){ - c++; - m->trail = c; - break; - } if(m->argc >= IRCD_ARG_MAX){ log_err("IRC parse_msg: max args exceeded"); return 0; } - m->argv[m->argc] = c; + m->argv[m->argc] = *c == ':' ? c+1 : c; m->argc++; + if(*c == ':') break; + c = strchr(c,' '); if(!c){ //RFC borken? @@ -124,11 +123,11 @@ } static bool ircd_recv(void){ - static char buf[IRC_MSG_BUF]; - static char *buf_e = buf+sizeof(buf); - static char *buf_p = buf; - char *r; - size_t n; + char *buf = ircd_buf; + char *buf_e = ircd_buf+sizeof(ircd_buf); + char *buf_p = ircd_buf_p; + char *r; + size_t n; n = tcp_recv(&ircd_src_ip, &ircd_src_port, @@ -136,18 +135,37 @@ (uint8_t *)buf_p, buf_e-buf_p); if(!n) return 0; + + if(memchr(buf_p,0x00,n)){ + log_err("IRC command contains null byte"); + ircd_ckill(); + return 0; + } + buf_p += n; while(r = str_nstr(buf,"\r\n",buf_p-buf)){ *r = 0; r += 2; - recv_msg(buf); + + if(utf8_valid(buf)){ + recv_msg(buf); + }else{ + log_err("IRC command contains invalid UTF-8 sequence"); + } + assert(r <= buf_p); memmove(buf,r,buf_p-r); buf_p = buf+(buf_p-r); } - BUF_ZERO(buf); + if(buf_p-buf >= IRC_MSG_LEN){ + log_err("IRC command too long"); + ircd_ckill(); + return 0; + } + + ircd_buf_p = buf_p; return 1; } @@ -157,8 +175,8 @@ } } -size_t ircd_send(char const *msg){ - return tcp_send((uint8_t const *)msg,strlen(msg)); +size_t ircd_send(char const *str,size_t len){ + return tcp_send((uint8_t const *)str,len); } size_t ircd_sendf(char const *fmt,...){ @@ -186,7 +204,7 @@ b = buf+n; b = strpcpy(b,"\r\n",buf+sizeof(buf)); - r = ircd_send(buf); + r = ircd_send(buf,b-buf); memset_ffs(buf,0x00,b-buf); return r; } diff -uNr a/src/ircd.h b/src/ircd.h --- a/src/ircd.h 54388690539166f09facba1462c8a7bf04fa8230f2aaf2d5df977a90163480d5321a3930c471241c4da247fea37ba84928317ec2dab38eb50ad5c717b6d731ab +++ b/src/ircd.h e92250adc61344aa6bf843e5a307e7ab709474a89ad32e7224faa856b6d9af75a7f3c06edf3f42f733881daa430dad9e55c954408a5f0a48b82b0df462e1663e @@ -13,7 +13,7 @@ void ircd_tick (void); -size_t ircd_send (char const *msg); +size_t ircd_send (char const *str,size_t len); size_t ircd_sendf (char const *fmt,...); size_t ircd_vsendf (char const *fmt,va_list args); diff -uNr a/src/irclog.c b/src/irclog.c --- a/src/irclog.c 644bec1fb7fe784192c2cc1270fe6cf4b44e6ab657bd9c9328b52dbf4b7219c659ad5a36a47559047903ce1d41246548863daac23d6d766b66e0a99062a19fac +++ b/src/irclog.c 35471ea2fb33bcb3224b02db81dee39c47f2c6d209a3d11698d0cb533bca4c0e6e6b4c42bea3fbb2f107e05e7c9c92c99607cf783f1188b65856be185e2f6a03 @@ -20,33 +20,28 @@ if(!irc_ready) return; if(!chan){ -#ifdef NOTICE_CHAN chan = irc_chan; if(!*chan) chan = self_handle; -#else - chan = self_handle; -#endif } - //TODO: notice to irc_chan knob ?? b += snprintf(b,e-b,":%s NOTICE %s :%s",IRC_SERV,chan,pre); b += vsnprintf(b,e-b,fmt,args); b = strpcpy(b,"\r\n",e); - ircd_send(buf); + ircd_send(buf,b-buf); memset_ffs(buf,0x00,b-buf); } void irc_out(char const *fmt,...){ va_list args; va_start(args,fmt); - _irc_vnotice(0,"",fmt,args); + _irc_vnotice(irc_log_chan,"",fmt,args); va_end(args); } void irc_err(char const *fmt,...){ va_list args; va_start(args,fmt); - _irc_vnotice(0,"error: ",fmt,args); + _irc_vnotice(irc_log_chan,"error: ",fmt,args); va_end(args); } @@ -56,7 +51,7 @@ va_start(args,fmt); va_copy(args2,args); log_vout(fmt,args); - _irc_vnotice(0,"",fmt,args2); + _irc_vnotice(irc_log_chan,"",fmt,args2); va_end(args); va_end(args2); } @@ -67,7 +62,7 @@ va_start(args,fmt); va_copy(args2,args); log_verr(fmt,args); - _irc_vnotice(0,"error: ",fmt,args2); + _irc_vnotice(irc_log_chan,"error: ",fmt,args2); va_end(args); va_end(args2); } diff -uNr a/src/key.c b/src/key.c --- a/src/key.c 4dd5c2b70a6bc7b3b2be1537fcd7e179020e6bda8f6c2df37bf9b05d045af9a868584f973769d58941e64f7c81d207d2d7620b34c37bd6363b873615f641f2be +++ b/src/key.c 9210852bd9d1227857fd9e2c3a46535dfdcf9b859c726a7d3bf1a4840cc2aa07de12be54be7760ca734b8accf0aeb4daea8fcd291dbee49ec57c56ebfe67ef44 @@ -3,15 +3,17 @@ #include "string.h" #include "def.h" #include "log.h" +#include "state.h" #include bool key_gen(uint8_t *key){ - if(!rng_read(key,KEY_SIZE)){ + if(!rng_read(rng_slow_fd,key,KEY_SIZE)){ + irclog_err("could not generate key, rng read failed"); return 0; } if(entropy_missing(key,KEY_SIZE)){ - irclog_err("could not generate key, rng borken"); + irclog_err("could not generate key, entropy missing"); return 0; } diff -uNr a/src/killfile.c b/src/killfile.c --- a/src/killfile.c ac43ab4baa783948ff151c0434a101e59bd0662b0c5528f744adf82086acba2ee76ac8c3f84d9b357683403fb348de06d6184b8c3c0a9e33db276293d3ebb66b +++ b/src/killfile.c 15428faf2b2efbfb12d79031ee08dcb26cfa8c594f1b1538354decd7a9810f9b2686506a0a8cdb968b090b59eeffadabe2f393ad8069278a7705f12cf933af70 @@ -117,13 +117,14 @@ FMT_PATH(path,"%s",FILE_KILLFILE); if(killfile_cnt == 0){ - if(!file_delete(path)){ + if(!file_delete_zero(path)){ disk_unsync(); return 0; } return 1; } - f = file_open(path,"wb"); + + f = file_open_zero(path,"wb"); if(!f){ disk_unsync(); return 0; diff -uNr a/src/knob.c b/src/knob.c --- a/src/knob.c be744fc5044b7007b2deadada7aa49196eec1df7038d27bfcd64b733af4527a5f15426c466f604ac0c9945d8dbce9afd759335b7f68885a9655b5b491566a83a +++ b/src/knob.c 5814f521cdfbc39eee30747d5c2c4248eca748e8f10ec6b7679c5631626c83292517fd0c6c66145b0de149b14b8699ca0cfeea2891c53326142bb9dc198e8009 @@ -333,6 +333,7 @@ k = knob_lookup(name); if(!k) return; + if(val[0] == '\\' && val[1] == 0) val = ""; //hak rc = knob_setval[k->type](k,val); switch(rc){ diff -uNr a/src/knobs.h b/src/knobs.h --- a/src/knobs.h bab63daf3db62c20dfc32d6adfa1a558a8b69971a67db5511e1c73f1ad445f1b4b04ca80e50e6eac8bd07cc64fc7c7e226b072b780905305cbc2865999b94972 +++ b/src/knobs.h a354d299c34ab6e19514c6f81937e945b831ce6e79833b316546ed28ab20c8bc426eab0aa0570fcbd6b5641a5faed67752e8911d10b54f1af6445901ea64fb7b @@ -15,7 +15,7 @@ .name = FILE_CUTOFF, .desc = "bounce cutoff for rejecting packets", .type = KNOB_INT, - .def.i = 3, + .def.i = 5, .min = CUTOFF_MIN, .max = CUTOFF_MAX, }, diff -uNr a/src/log.c b/src/log.c --- a/src/log.c 25f6f816a5b101fbdbad4a2eb03e48d92d7e4cd3ee15ade0d5b662d229bfdb905ad93a498e8d16b604862061f55914cb4cc5eb4187be536ac8529b6e4d0311f8 +++ b/src/log.c 25337c1b41143f539b3d6a87180f010ad2ceda4cc5cd7e574fe98abb8d70617f3522763a5e14e00edddb31b4c375313c510984fe79b0a714d19fdb9d1bb77fbe @@ -22,6 +22,7 @@ timestamp(f,time); vfprintf(f,fmt,args); fputc('\n',f); + fflush(f); } static void _log_verr(FILE *f,char const *fmt,va_list args,uint64_t time){ @@ -29,6 +30,7 @@ fputs("error: ",f); vfprintf(f,fmt,args); fputc('\n',f); + fflush(f); } bool log_open(char const *path){ diff -uNr a/src/log.h b/src/log.h --- a/src/log.h c084f3d98230b1226c621c06a176c0094672ef8641c3523a51cd2cf19c8e5960807874a917af621b48c3b5c46e44b4a405ae2dd5b83e3b70c9b25ad272392187 +++ b/src/log.h 1b0e83df7af65f2d2de15c7c58e5790b8336e4fe5a5cd7597cf42867ca290a7ddd573120d6c0b5727e9a89fe77de0ea2613f0a1d9e0230107ac588db1f2c5b82 @@ -24,10 +24,9 @@ void log_packet (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr,bool outgoing,uint64_t time,uint64_t stamp); void log_martian (addr_t const *addr,bool reason_size); void log_stale (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr,uint64_t time,uint64_t stamp); -void log_dupe (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr); -void log_dedup_full (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr); -void log_ignore (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr); -void log_cutoff (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr,uint8_t bounces); +void log_dupe (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr,uint64_t time,uint64_t stamp); +void log_dedup_full (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr,uint64_t time,uint64_t stamp); +void log_cutoff (hash_t const hash,uint8_t cmd,peer_t *p,addr_t const *addr,uint64_t time,uint64_t stamp,uint8_t bounces); #endif diff -uNr a/src/main.c b/src/main.c --- a/src/main.c d1922183333b9109ee5456760c1b0c6a403c21276d53d128cbfe6c7f080af5db33dfc1da35a7ed2e2c677741f8f089258ca37170c299cee07516e041c757f37f +++ b/src/main.c 61319746c46cbea068b556747f1647dafc2d937c4ab01001970a501db2abd458cea87bb8ed553bcc2dd8aed541952473a0579c8c81747b74fce385efddbb7970 @@ -1,5 +1,6 @@ #define _POSIX_C_SOURCE 200112L #include "ircd.h" +#include "irc.h" #include "pest.h" #include "peer.h" #include "crypto.h" @@ -13,16 +14,8 @@ #include "wipe.h" #include "state.h" #include -#include -#include -#include #include -#include -#include #include -#include - -void irc_ev(unsigned e,ircmsg_t const *msg); static int usage(int code){ fputs( @@ -43,72 +36,11 @@ return code; } -static void sigint_handler(int a){ +static void term(int a){ (void)a; running = 0; } -static void irc_privmsg(char const *speaker, - char const *text, - char const *chan){ - - char buf[IRC_MSG_BUF]; - char *i = buf; - char *e = buf+sizeof(buf); - - i = strpcpy(i,":", e); - i = strpcpy(i,speaker, e); - //i = strpcpy(i,"@", e); - //i = strpcpy(i,IRC_PEERHOST, e); - i = strpcpy(i," PRIVMSG ", e); - i = strpcpy(i,chan, e); - i = strpcpy(i," :", e); - i = strpcpy(i,text, e); - i = strpcpy(i,"\r\n", e); - - ircd_send(buf); - BUF_ZERO(buf); -} - -static void irc_join(char const *speaker){ - char host[IRC_HOST_BUF]; - fmt_peerhost(host,sizeof(host),speaker); - ircd_sendf(":%s JOIN %s",host,irc_chan); -} - -static void irc_part(char const *speaker){ - char host[IRC_HOST_BUF]; - fmt_peerhost(host,sizeof(host),speaker); - ircd_sendf(":%s PART %s",host,irc_chan); -} - -static void pest_ev(unsigned e,char const *speaker,char const *text){ - if(!irc_ready) return; - - switch(e){ - case PEST_BROADCAST: - if(irc_joined){ - irc_privmsg(speaker,text,irc_chan); - } - break; - - case PEST_DIRECT: - irc_privmsg(speaker,text,self_handle); - break; - - case PEST_ACTIVE: - irc_join(speaker); - break; - - case PEST_INACTIVE: - irc_part(speaker); - break; - - default: - assert(0); - } -} - static int _main(int argc,char **argv){ int opt; bool store_user = 0; @@ -122,12 +54,17 @@ uint64_t time; uint64_t ntime; + signal(SIGINT, term); + signal(SIGTERM, term); + signal(SIGHUP, term); + signal(SIGPIPE, SIG_IGN); + while((opt = getopt(argc,argv,"vhu:p:s:i:q:d:l:")) != -1){ switch(opt){ int64_t port; case 'v': - fprintf(stderr,PROG_FULLNAME "\n"); + fprintf(stderr,PROG_FULLERNAME "\n"); return 0; case 'h': @@ -204,29 +141,33 @@ if(!killfile_load()) return 1; if(!peer_load()) return 1; - signal(SIGINT,sigint_handler); - signal(SIGPIPE,SIG_IGN); - - if(!rng_open(RNG_PATH)) return 1; + rng_slow_fd = rng_open(RNG_SLOW_PATH); + rng_fast_fd = rng_open(RNG_FAST_PATH); + if(rng_slow_fd == -1) return 1; + if(rng_fast_fd == -1) return 1; + ircd_init(irc_ev); - if(!pest_init(pest_ev)) return 1; - log_out(PROG_FULLNAME " station running"); + if(!pest_init()) return 1; + log_out(PROG_FULLERNAME " station running"); time_start = mono_sec(); while(running){ bool active; - time = mono_sec(); ntime = mono_nsec(); active = irc_joined || time-time_start > PEST_WAIT_IRC; - ircd_tick(); pest_tick(time,ntime,active); } log_out("station shutting down..."); ircd_exit(); - if(!scram) rng_close(); + rng_close(rng_slow_fd); + rng_slow_fd = -1; + if(!scram){ + rng_close(rng_fast_fd); + rng_fast_fd = -1; + } log_close(); return 0; } @@ -238,19 +179,24 @@ int r = _main(argc,argv); if(scram){ - dir_zero(data_path); - dir_rand(data_path); - rm_rf_nolog(data_path); + dir_zero_nolog(data_path); if(*log_path){ - file_zero(log_path); - file_rand(log_path); - rm_nolog(log_path); + file_zero_nolog(log_path); } state_zero(); stack_zero(); + + dir_rand_nolog(data_path); + rm_rf_nolog(data_path); + if(*log_path){ + file_rand_nolog(log_path); + file_delete_nolog(log_path); + } state_rand(); stack_rand(); - rng_close(); + + rng_close(rng_fast_fd); + rng_fast_fd = -1; }else{ state_zero(); stack_zero(); diff -uNr a/src/message.c b/src/message.c --- a/src/message.c 766126d2b69a1ff4d7b89238bd74ab5bce0af7788b4bef31fd74221d3e3aea42e4e15c30d98699843686c9aff9374044c26ba9d9bc8c402522ed79bec4175f75 +++ b/src/message.c 9c2025b39eed0b91c9031f76316b7e98365dcfbbb868a9b2671aace03d6601580ea06caa6190a8ac5eb6c605c870ff5b732d3df59021eb05d7595a31efb1fa19 @@ -51,7 +51,7 @@ if(p){ problems_display(p,chan,speaker); } - console_send(chan,speaker,m,stampold); + console_send(p,chan,speaker,m,stampold); } } @@ -228,8 +228,8 @@ }else{ //hearsay if(bounces == 0){ - log_out("hearsay message with 0 bounces " - "discarded from peer '%s'",p->handle[0]); + irclog_err("hearsay message with 0 bounces " + "discarded from peer '%s'",p->handle[0]); return 0; } diff -uNr a/src/net.c b/src/net.c --- a/src/net.c 1b24b705d236d1a47b9785ca2dd95314a7846fb2016e949b1e3cf00bca1140b5db2d9f64d3e68365ee5d54b05839387e570bc6792532a6fdbf1b304f6d950401 +++ b/src/net.c 5395ca90226939c2badd640df5f7b9e23b11122a4a41effd21009fc2adea7285f4bdf0302f9fe59b4eeb18e2994856ca89fc75f87e47c29c5c160f35c03ded81 @@ -35,6 +35,10 @@ return 1; } +bool udp_bound(void){ + return udp_fd != -1; +} + size_t udp_recv(uint32_t *src_ip, uint16_t *src_port, uint16_t port, @@ -46,17 +50,17 @@ socklen_t cli_addr_len; ssize_t n; - if(udp_listen_fd == -1){ - udp_listen_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); - if(udp_listen_fd == -1){ + if(udp_fd == -1){ + udp_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); + if(udp_fd == -1){ log_errno("socket"); return 0; } - if(fcntl(udp_listen_fd,F_SETFL,O_NONBLOCK) != 0){ + if(fcntl(udp_fd,F_SETFL,O_NONBLOCK) != 0){ log_errno("fcntl"); - close(udp_listen_fd); - udp_listen_fd = -1; + close(udp_fd); + udp_fd = -1; return 0; } @@ -65,12 +69,12 @@ addr.sin_port = htons(port); addr.sin_addr.s_addr = htonl(INADDR_ANY); - if(bind(udp_listen_fd, + if(bind(udp_fd, (struct sockaddr *)&addr, sizeof(addr)) != 0){ log_errno("bind"); - close(udp_listen_fd); - udp_listen_fd = -1; + close(udp_fd); + udp_fd = -1; sleep_nsec(NET_RETRY_SLEEP*NSEC_MSEC); return 0; } @@ -80,7 +84,7 @@ if(data == 0) return 0; cli_addr_len = sizeof(cli_addr); - n = recvfrom(udp_listen_fd, + n = recvfrom(udp_fd, data, size, 0, @@ -107,22 +111,17 @@ uint8_t const *data, size_t size){ - int fd; struct sockaddr_in addr; ssize_t n; + if(udp_fd == -1) return 0; + VAR_ZERO(addr); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = htonl(ip); - fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); - if(fd == -1){ - log_errno("socket"); - return 0; - } - - n = sendto(fd, + n = sendto(udp_fd, data, size, 0, @@ -131,10 +130,8 @@ if(n == -1){ log_errno("sendto"); - close(fd); return 0; } - close(fd); return n; } diff -uNr a/src/net.h b/src/net.h --- a/src/net.h 592fd4acffa17981edb080ecb9abae7fef094e1c248fbab5f398d31a70727002c7b801a2090c600e45467f1080cafaadf481d108fe695f87795c1a13fb040c72 +++ b/src/net.h a9425e8cc9b1480d5728a6354ab87ef756701c1a6ca8d1ff600e74d036e7aadaac65ba1a7e5e2d8f1f1b7454ae89bf695bd0140b76e27a747fe43e9d25407f42 @@ -9,6 +9,7 @@ bool ip_u32 (uint32_t *ip,char const *str); bool ip_str (char *str,size_t str_size,uint32_t ip); +bool udp_bound (void); size_t udp_recv (uint32_t *src_ip,uint16_t *src_port,uint16_t port,uint8_t *data,size_t size); size_t udp_send (uint32_t ip,uint16_t port,uint8_t const *data,size_t size); diff -uNr a/src/otherpplgode/hmac.c b/src/otherpplgode/hmac.c --- a/src/otherpplgode/hmac.c 753dda1c250612d1189c94b6b919dc049f3587fb607aca9a02b6d8a2c1646dd5e1c70b256cacbf540ba7ecf364c3c57c67ed4104b9c9199b9cbebc3ea88434cd +++ b/src/otherpplgode/hmac.c false @@ -1,118 +0,0 @@ -/* - * HMAC-SHA-224/256/384/512 implementation - * Last update: 06/15/2005 - * Issue date: 06/15/2005 - * - * Copyright (C) 2005 Olivier Gay - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * ********************MODIFIED FOR USE IN SMALPEST******************** - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include "hmac.h" -#include - -static void sha384(uint8_t *sha,uint8_t const *msg,size_t msg_len){ - sha384_ctx ctx; - sha384_init(&ctx); - sha384_update(&ctx,msg,msg_len); - sha384_final(&ctx,sha); -} - -void hmac_sha384_init(hmac_sha384_ctx *ctx, const unsigned char *key, - unsigned int key_size) -{ - unsigned int fill; - unsigned int num; - - const unsigned char *key_used; - unsigned char key_temp[SHA384_DIGEST_SIZE]; - int i; - - if (key_size == SHA384_BLOCK_SIZE) { - key_used = key; - num = SHA384_BLOCK_SIZE; - } else { - if (key_size > SHA384_BLOCK_SIZE){ - num = SHA384_DIGEST_SIZE; - sha384(key_temp,key,key_size); - key_used = key_temp; - } else { /* key_size > SHA384_BLOCK_SIZE */ - key_used = key; - num = key_size; - } - fill = SHA384_BLOCK_SIZE - num; - - memset(ctx->block_ipad + num, 0x36, fill); - memset(ctx->block_opad + num, 0x5c, fill); - } - - for (i = 0; i < (int) num; i++) { - ctx->block_ipad[i] = key_used[i] ^ 0x36; - ctx->block_opad[i] = key_used[i] ^ 0x5c; - } - - sha384_init(&ctx->ctx_inside); - sha384_update(&ctx->ctx_inside, ctx->block_ipad, SHA384_BLOCK_SIZE); - - sha384_init(&ctx->ctx_outside); - sha384_update(&ctx->ctx_outside, ctx->block_opad, - SHA384_BLOCK_SIZE); - - /* for hmac_reinit */ - memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, - sizeof(sha384_ctx)); - memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, - sizeof(sha384_ctx)); -} - -void hmac_sha384_reinit(hmac_sha384_ctx *ctx) -{ - memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, - sizeof(sha384_ctx)); - memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, - sizeof(sha384_ctx)); -} - -void hmac_sha384_update(hmac_sha384_ctx *ctx, const unsigned char *message, - unsigned int message_len) -{ - sha384_update(&ctx->ctx_inside, message, message_len); -} - -void hmac_sha384_final(hmac_sha384_ctx *ctx, unsigned char *mac, - unsigned int mac_size) -{ - unsigned char digest_inside[SHA384_DIGEST_SIZE]; - unsigned char mac_temp[SHA384_DIGEST_SIZE]; - - sha384_final(&ctx->ctx_inside, digest_inside); - sha384_update(&ctx->ctx_outside, digest_inside, SHA384_DIGEST_SIZE); - sha384_final(&ctx->ctx_outside, mac_temp); - memcpy(mac, mac_temp, mac_size); -} - diff -uNr a/src/otherpplgode/hmac.h b/src/otherpplgode/hmac.h --- a/src/otherpplgode/hmac.h edc00426d5a81ff3e6fba03aa372540e04d6dbbde6309bc9a5e7974f03366dba16dee546f50f211ee53aacf208d5ba888260b1f3f1eedee389266d045c4f2851 +++ b/src/otherpplgode/hmac.h false @@ -1,64 +0,0 @@ -/* - * HMAC-SHA-224/256/384/512 implementation - * Last update: 06/15/2005 - * Issue date: 06/15/2005 - * - * Copyright (C) 2005 Olivier Gay - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * ********************MODIFIED FOR USE IN SMALPEST******************** - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef HMAC_SHA2_H -#define HMAC_SHA2_H - -#include "sha.h" - -typedef struct { - sha384_ctx ctx_inside; - sha384_ctx ctx_outside; - - /* for hmac_reinit */ - sha384_ctx ctx_inside_reinit; - sha384_ctx ctx_outside_reinit; - - unsigned char block_ipad[SHA384_BLOCK_SIZE]; - unsigned char block_opad[SHA384_BLOCK_SIZE]; -} hmac_sha384_ctx; - -void hmac_sha384_init(hmac_sha384_ctx *ctx, const unsigned char *key, - unsigned int key_size); -void hmac_sha384_reinit(hmac_sha384_ctx *ctx); -void hmac_sha384_update(hmac_sha384_ctx *ctx, const unsigned char *message, - unsigned int message_len); -void hmac_sha384_final(hmac_sha384_ctx *ctx, unsigned char *mac, - unsigned int mac_size); -void hmac_sha384(const unsigned char *key, unsigned int key_size, - const unsigned char *message, unsigned int message_len, - unsigned char *mac, unsigned mac_size); - -#endif /* !HMAC_SHA2_H */ diff -uNr a/src/otherpplgode/sha.c b/src/otherpplgode/sha.c --- a/src/otherpplgode/sha.c d7d4682b42c1e6787af1f0200b4d3147827a5fcc5652c71cdf3058dbfc99e0946080f95a29deecb1ba46606812521880035cec0d18adbc490747c992ee1f114c +++ b/src/otherpplgode/sha.c false @@ -1,690 +0,0 @@ -/* - * FIPS 180-2 SHA-224/256/384/512 implementation - * Last update: 02/02/2007 - * Issue date: 04/30/2005 - * - * Copyright (C) 2005, 2007 Olivier Gay - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * ********************MODIFIED FOR USE IN SMALPEST******************** - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if 0 -#define UNROLL_LOOPS /* Enable loops unrolling */ -#endif - -#include "sha.h" -#include - -#define SHFR(x, n) (x >> n) -#define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) -#define ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) -#define CH(x, y, z) ((x & y) ^ (~x & z)) -#define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) - -#define SHA256_F1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) -#define SHA256_F2(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) -#define SHA256_F3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHFR(x, 3)) -#define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10)) - -#define SHA512_F1(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39)) -#define SHA512_F2(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41)) -#define SHA512_F3(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHFR(x, 7)) -#define SHA512_F4(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHFR(x, 6)) - -#define UNPACK32(x, str) \ -{ \ - *((str) + 3) = (uint8_t) ((x) ); \ - *((str) + 2) = (uint8_t) ((x) >> 8); \ - *((str) + 1) = (uint8_t) ((x) >> 16); \ - *((str) + 0) = (uint8_t) ((x) >> 24); \ -} - -#define PACK32(str, x) \ -{ \ - *(x) = ((uint32_t) *((str) + 3) ) \ - | ((uint32_t) *((str) + 2) << 8) \ - | ((uint32_t) *((str) + 1) << 16) \ - | ((uint32_t) *((str) + 0) << 24); \ -} - -#define UNPACK64(x, str) \ -{ \ - *((str) + 7) = (uint8_t) ((x) ); \ - *((str) + 6) = (uint8_t) ((x) >> 8); \ - *((str) + 5) = (uint8_t) ((x) >> 16); \ - *((str) + 4) = (uint8_t) ((x) >> 24); \ - *((str) + 3) = (uint8_t) ((x) >> 32); \ - *((str) + 2) = (uint8_t) ((x) >> 40); \ - *((str) + 1) = (uint8_t) ((x) >> 48); \ - *((str) + 0) = (uint8_t) ((x) >> 56); \ -} - -#define PACK64(str, x) \ -{ \ - *(x) = ((uint64_t) *((str) + 7) ) \ - | ((uint64_t) *((str) + 6) << 8) \ - | ((uint64_t) *((str) + 5) << 16) \ - | ((uint64_t) *((str) + 4) << 24) \ - | ((uint64_t) *((str) + 3) << 32) \ - | ((uint64_t) *((str) + 2) << 40) \ - | ((uint64_t) *((str) + 1) << 48) \ - | ((uint64_t) *((str) + 0) << 56); \ -} - -/* Macros used for loops unrolling */ - -#define SHA256_SCR(i) \ -{ \ - w[i] = SHA256_F4(w[i - 2]) + w[i - 7] \ - + SHA256_F3(w[i - 15]) + w[i - 16]; \ -} - -#define SHA512_SCR(i) \ -{ \ - w[i] = SHA512_F4(w[i - 2]) + w[i - 7] \ - + SHA512_F3(w[i - 15]) + w[i - 16]; \ -} - -#define SHA256_EXP(a, b, c, d, e, f, g, h, j) \ -{ \ - t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \ - + sha256_k[j] + w[j]; \ - t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \ - wv[d] += t1; \ - wv[h] = t1 + t2; \ -} - -#define SHA512_EXP(a, b, c, d, e, f, g ,h, j) \ -{ \ - t1 = wv[h] + SHA512_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \ - + sha512_k[j] + w[j]; \ - t2 = SHA512_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \ - wv[d] += t1; \ - wv[h] = t1 + t2; \ -} - -uint32_t sha256_h0[8] = - {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, - 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; - -uint64_t sha384_h0[8] = - {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, - 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, - 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, - 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL}; - -uint64_t sha512_h0[8] = - {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, - 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, - 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, - 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL}; - -uint32_t sha256_k[64] = - {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, - 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, - 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, - 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, - 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, - 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; - -uint64_t sha512_k[80] = - {0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, - 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, - 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, - 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, - 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, - 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, - 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, - 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, - 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, - 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, - 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, - 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, - 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, - 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, - 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, - 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, - 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, - 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, - 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, - 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, - 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, - 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, - 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, - 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, - 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, - 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, - 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, - 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, - 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, - 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, - 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, - 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, - 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, - 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, - 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, - 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, - 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, - 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, - 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, - 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; - -/* SHA-256 functions */ - -static void sha256_transf(sha256_ctx *ctx, const unsigned char *message, - unsigned int block_nb) -{ - uint32_t w[64]; - uint32_t wv[8]; - uint32_t t1, t2; - const unsigned char *sub_block; - int i; - -#ifndef UNROLL_LOOPS - int j; -#endif - - for (i = 0; i < (int) block_nb; i++) { - sub_block = message + (i << 6); - -#ifndef UNROLL_LOOPS - for (j = 0; j < 16; j++) { - PACK32(&sub_block[j << 2], &w[j]); - } - - for (j = 16; j < 64; j++) { - SHA256_SCR(j); - } - - for (j = 0; j < 8; j++) { - wv[j] = ctx->h[j]; - } - - for (j = 0; j < 64; j++) { - t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) - + sha256_k[j] + w[j]; - t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); - wv[7] = wv[6]; - wv[6] = wv[5]; - wv[5] = wv[4]; - wv[4] = wv[3] + t1; - wv[3] = wv[2]; - wv[2] = wv[1]; - wv[1] = wv[0]; - wv[0] = t1 + t2; - } - - for (j = 0; j < 8; j++) { - ctx->h[j] += wv[j]; - } -#else - PACK32(&sub_block[ 0], &w[ 0]); PACK32(&sub_block[ 4], &w[ 1]); - PACK32(&sub_block[ 8], &w[ 2]); PACK32(&sub_block[12], &w[ 3]); - PACK32(&sub_block[16], &w[ 4]); PACK32(&sub_block[20], &w[ 5]); - PACK32(&sub_block[24], &w[ 6]); PACK32(&sub_block[28], &w[ 7]); - PACK32(&sub_block[32], &w[ 8]); PACK32(&sub_block[36], &w[ 9]); - PACK32(&sub_block[40], &w[10]); PACK32(&sub_block[44], &w[11]); - PACK32(&sub_block[48], &w[12]); PACK32(&sub_block[52], &w[13]); - PACK32(&sub_block[56], &w[14]); PACK32(&sub_block[60], &w[15]); - - SHA256_SCR(16); SHA256_SCR(17); SHA256_SCR(18); SHA256_SCR(19); - SHA256_SCR(20); SHA256_SCR(21); SHA256_SCR(22); SHA256_SCR(23); - SHA256_SCR(24); SHA256_SCR(25); SHA256_SCR(26); SHA256_SCR(27); - SHA256_SCR(28); SHA256_SCR(29); SHA256_SCR(30); SHA256_SCR(31); - SHA256_SCR(32); SHA256_SCR(33); SHA256_SCR(34); SHA256_SCR(35); - SHA256_SCR(36); SHA256_SCR(37); SHA256_SCR(38); SHA256_SCR(39); - SHA256_SCR(40); SHA256_SCR(41); SHA256_SCR(42); SHA256_SCR(43); - SHA256_SCR(44); SHA256_SCR(45); SHA256_SCR(46); SHA256_SCR(47); - SHA256_SCR(48); SHA256_SCR(49); SHA256_SCR(50); SHA256_SCR(51); - SHA256_SCR(52); SHA256_SCR(53); SHA256_SCR(54); SHA256_SCR(55); - SHA256_SCR(56); SHA256_SCR(57); SHA256_SCR(58); SHA256_SCR(59); - SHA256_SCR(60); SHA256_SCR(61); SHA256_SCR(62); SHA256_SCR(63); - - wv[0] = ctx->h[0]; wv[1] = ctx->h[1]; - wv[2] = ctx->h[2]; wv[3] = ctx->h[3]; - wv[4] = ctx->h[4]; wv[5] = ctx->h[5]; - wv[6] = ctx->h[6]; wv[7] = ctx->h[7]; - - SHA256_EXP(0,1,2,3,4,5,6,7, 0); SHA256_EXP(7,0,1,2,3,4,5,6, 1); - SHA256_EXP(6,7,0,1,2,3,4,5, 2); SHA256_EXP(5,6,7,0,1,2,3,4, 3); - SHA256_EXP(4,5,6,7,0,1,2,3, 4); SHA256_EXP(3,4,5,6,7,0,1,2, 5); - SHA256_EXP(2,3,4,5,6,7,0,1, 6); SHA256_EXP(1,2,3,4,5,6,7,0, 7); - SHA256_EXP(0,1,2,3,4,5,6,7, 8); SHA256_EXP(7,0,1,2,3,4,5,6, 9); - SHA256_EXP(6,7,0,1,2,3,4,5,10); SHA256_EXP(5,6,7,0,1,2,3,4,11); - SHA256_EXP(4,5,6,7,0,1,2,3,12); SHA256_EXP(3,4,5,6,7,0,1,2,13); - SHA256_EXP(2,3,4,5,6,7,0,1,14); SHA256_EXP(1,2,3,4,5,6,7,0,15); - SHA256_EXP(0,1,2,3,4,5,6,7,16); SHA256_EXP(7,0,1,2,3,4,5,6,17); - SHA256_EXP(6,7,0,1,2,3,4,5,18); SHA256_EXP(5,6,7,0,1,2,3,4,19); - SHA256_EXP(4,5,6,7,0,1,2,3,20); SHA256_EXP(3,4,5,6,7,0,1,2,21); - SHA256_EXP(2,3,4,5,6,7,0,1,22); SHA256_EXP(1,2,3,4,5,6,7,0,23); - SHA256_EXP(0,1,2,3,4,5,6,7,24); SHA256_EXP(7,0,1,2,3,4,5,6,25); - SHA256_EXP(6,7,0,1,2,3,4,5,26); SHA256_EXP(5,6,7,0,1,2,3,4,27); - SHA256_EXP(4,5,6,7,0,1,2,3,28); SHA256_EXP(3,4,5,6,7,0,1,2,29); - SHA256_EXP(2,3,4,5,6,7,0,1,30); SHA256_EXP(1,2,3,4,5,6,7,0,31); - SHA256_EXP(0,1,2,3,4,5,6,7,32); SHA256_EXP(7,0,1,2,3,4,5,6,33); - SHA256_EXP(6,7,0,1,2,3,4,5,34); SHA256_EXP(5,6,7,0,1,2,3,4,35); - SHA256_EXP(4,5,6,7,0,1,2,3,36); SHA256_EXP(3,4,5,6,7,0,1,2,37); - SHA256_EXP(2,3,4,5,6,7,0,1,38); SHA256_EXP(1,2,3,4,5,6,7,0,39); - SHA256_EXP(0,1,2,3,4,5,6,7,40); SHA256_EXP(7,0,1,2,3,4,5,6,41); - SHA256_EXP(6,7,0,1,2,3,4,5,42); SHA256_EXP(5,6,7,0,1,2,3,4,43); - SHA256_EXP(4,5,6,7,0,1,2,3,44); SHA256_EXP(3,4,5,6,7,0,1,2,45); - SHA256_EXP(2,3,4,5,6,7,0,1,46); SHA256_EXP(1,2,3,4,5,6,7,0,47); - SHA256_EXP(0,1,2,3,4,5,6,7,48); SHA256_EXP(7,0,1,2,3,4,5,6,49); - SHA256_EXP(6,7,0,1,2,3,4,5,50); SHA256_EXP(5,6,7,0,1,2,3,4,51); - SHA256_EXP(4,5,6,7,0,1,2,3,52); SHA256_EXP(3,4,5,6,7,0,1,2,53); - SHA256_EXP(2,3,4,5,6,7,0,1,54); SHA256_EXP(1,2,3,4,5,6,7,0,55); - SHA256_EXP(0,1,2,3,4,5,6,7,56); SHA256_EXP(7,0,1,2,3,4,5,6,57); - SHA256_EXP(6,7,0,1,2,3,4,5,58); SHA256_EXP(5,6,7,0,1,2,3,4,59); - SHA256_EXP(4,5,6,7,0,1,2,3,60); SHA256_EXP(3,4,5,6,7,0,1,2,61); - SHA256_EXP(2,3,4,5,6,7,0,1,62); SHA256_EXP(1,2,3,4,5,6,7,0,63); - - ctx->h[0] += wv[0]; ctx->h[1] += wv[1]; - ctx->h[2] += wv[2]; ctx->h[3] += wv[3]; - ctx->h[4] += wv[4]; ctx->h[5] += wv[5]; - ctx->h[6] += wv[6]; ctx->h[7] += wv[7]; -#endif /* !UNROLL_LOOPS */ - } -} - -void sha256_init(sha256_ctx *ctx) -{ -#ifndef UNROLL_LOOPS - int i; - for (i = 0; i < 8; i++) { - ctx->h[i] = sha256_h0[i]; - } -#else - ctx->h[0] = sha256_h0[0]; ctx->h[1] = sha256_h0[1]; - ctx->h[2] = sha256_h0[2]; ctx->h[3] = sha256_h0[3]; - ctx->h[4] = sha256_h0[4]; ctx->h[5] = sha256_h0[5]; - ctx->h[6] = sha256_h0[6]; ctx->h[7] = sha256_h0[7]; -#endif /* !UNROLL_LOOPS */ - - ctx->len = 0; - ctx->tot_len = 0; -} - -void sha256_update(sha256_ctx *ctx, const unsigned char *message, - unsigned int len) -{ - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char *shifted_message; - - tmp_len = SHA256_BLOCK_SIZE - ctx->len; - rem_len = len < tmp_len ? len : tmp_len; - - memcpy(&ctx->block[ctx->len], message, rem_len); - - if (ctx->len + len < SHA256_BLOCK_SIZE) { - ctx->len += len; - return; - } - - new_len = len - rem_len; - block_nb = new_len / SHA256_BLOCK_SIZE; - - shifted_message = message + rem_len; - - sha256_transf(ctx, ctx->block, 1); - sha256_transf(ctx, shifted_message, block_nb); - - rem_len = new_len % SHA256_BLOCK_SIZE; - - memcpy(ctx->block, &shifted_message[block_nb << 6], - rem_len); - - ctx->len = rem_len; - ctx->tot_len += (block_nb + 1) << 6; -} - -void sha256_final(sha256_ctx *ctx, unsigned char *digest) -{ - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; - -#ifndef UNROLL_LOOPS - int i; -#endif - - block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) - < (ctx->len % SHA256_BLOCK_SIZE))); - - len_b = (ctx->tot_len + ctx->len) << 3; - pm_len = block_nb << 6; - - memset(ctx->block + ctx->len, 0, pm_len - ctx->len); - ctx->block[ctx->len] = 0x80; - UNPACK32(len_b, ctx->block + pm_len - 4); - - sha256_transf(ctx, ctx->block, block_nb); - -#ifndef UNROLL_LOOPS - for (i = 0 ; i < 8; i++) { - UNPACK32(ctx->h[i], &digest[i << 2]); - } -#else - UNPACK32(ctx->h[0], &digest[ 0]); - UNPACK32(ctx->h[1], &digest[ 4]); - UNPACK32(ctx->h[2], &digest[ 8]); - UNPACK32(ctx->h[3], &digest[12]); - UNPACK32(ctx->h[4], &digest[16]); - UNPACK32(ctx->h[5], &digest[20]); - UNPACK32(ctx->h[6], &digest[24]); - UNPACK32(ctx->h[7], &digest[28]); -#endif /* !UNROLL_LOOPS */ -} - -/* SHA-512 functions */ - -static void sha512_transf(sha512_ctx *ctx, const unsigned char *message, - unsigned int block_nb) -{ - uint64_t w[80]; - uint64_t wv[8]; - uint64_t t1, t2; - const unsigned char *sub_block; - int i, j; - - for (i = 0; i < (int) block_nb; i++) { - sub_block = message + (i << 7); - -#ifndef UNROLL_LOOPS - for (j = 0; j < 16; j++) { - PACK64(&sub_block[j << 3], &w[j]); - } - - for (j = 16; j < 80; j++) { - SHA512_SCR(j); - } - - for (j = 0; j < 8; j++) { - wv[j] = ctx->h[j]; - } - - for (j = 0; j < 80; j++) { - t1 = wv[7] + SHA512_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) - + sha512_k[j] + w[j]; - t2 = SHA512_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); - wv[7] = wv[6]; - wv[6] = wv[5]; - wv[5] = wv[4]; - wv[4] = wv[3] + t1; - wv[3] = wv[2]; - wv[2] = wv[1]; - wv[1] = wv[0]; - wv[0] = t1 + t2; - } - - for (j = 0; j < 8; j++) { - ctx->h[j] += wv[j]; - } -#else - PACK64(&sub_block[ 0], &w[ 0]); PACK64(&sub_block[ 8], &w[ 1]); - PACK64(&sub_block[ 16], &w[ 2]); PACK64(&sub_block[ 24], &w[ 3]); - PACK64(&sub_block[ 32], &w[ 4]); PACK64(&sub_block[ 40], &w[ 5]); - PACK64(&sub_block[ 48], &w[ 6]); PACK64(&sub_block[ 56], &w[ 7]); - PACK64(&sub_block[ 64], &w[ 8]); PACK64(&sub_block[ 72], &w[ 9]); - PACK64(&sub_block[ 80], &w[10]); PACK64(&sub_block[ 88], &w[11]); - PACK64(&sub_block[ 96], &w[12]); PACK64(&sub_block[104], &w[13]); - PACK64(&sub_block[112], &w[14]); PACK64(&sub_block[120], &w[15]); - - SHA512_SCR(16); SHA512_SCR(17); SHA512_SCR(18); SHA512_SCR(19); - SHA512_SCR(20); SHA512_SCR(21); SHA512_SCR(22); SHA512_SCR(23); - SHA512_SCR(24); SHA512_SCR(25); SHA512_SCR(26); SHA512_SCR(27); - SHA512_SCR(28); SHA512_SCR(29); SHA512_SCR(30); SHA512_SCR(31); - SHA512_SCR(32); SHA512_SCR(33); SHA512_SCR(34); SHA512_SCR(35); - SHA512_SCR(36); SHA512_SCR(37); SHA512_SCR(38); SHA512_SCR(39); - SHA512_SCR(40); SHA512_SCR(41); SHA512_SCR(42); SHA512_SCR(43); - SHA512_SCR(44); SHA512_SCR(45); SHA512_SCR(46); SHA512_SCR(47); - SHA512_SCR(48); SHA512_SCR(49); SHA512_SCR(50); SHA512_SCR(51); - SHA512_SCR(52); SHA512_SCR(53); SHA512_SCR(54); SHA512_SCR(55); - SHA512_SCR(56); SHA512_SCR(57); SHA512_SCR(58); SHA512_SCR(59); - SHA512_SCR(60); SHA512_SCR(61); SHA512_SCR(62); SHA512_SCR(63); - SHA512_SCR(64); SHA512_SCR(65); SHA512_SCR(66); SHA512_SCR(67); - SHA512_SCR(68); SHA512_SCR(69); SHA512_SCR(70); SHA512_SCR(71); - SHA512_SCR(72); SHA512_SCR(73); SHA512_SCR(74); SHA512_SCR(75); - SHA512_SCR(76); SHA512_SCR(77); SHA512_SCR(78); SHA512_SCR(79); - - wv[0] = ctx->h[0]; wv[1] = ctx->h[1]; - wv[2] = ctx->h[2]; wv[3] = ctx->h[3]; - wv[4] = ctx->h[4]; wv[5] = ctx->h[5]; - wv[6] = ctx->h[6]; wv[7] = ctx->h[7]; - - j = 0; - - do { - SHA512_EXP(0,1,2,3,4,5,6,7,j); j++; - SHA512_EXP(7,0,1,2,3,4,5,6,j); j++; - SHA512_EXP(6,7,0,1,2,3,4,5,j); j++; - SHA512_EXP(5,6,7,0,1,2,3,4,j); j++; - SHA512_EXP(4,5,6,7,0,1,2,3,j); j++; - SHA512_EXP(3,4,5,6,7,0,1,2,j); j++; - SHA512_EXP(2,3,4,5,6,7,0,1,j); j++; - SHA512_EXP(1,2,3,4,5,6,7,0,j); j++; - } while (j < 80); - - ctx->h[0] += wv[0]; ctx->h[1] += wv[1]; - ctx->h[2] += wv[2]; ctx->h[3] += wv[3]; - ctx->h[4] += wv[4]; ctx->h[5] += wv[5]; - ctx->h[6] += wv[6]; ctx->h[7] += wv[7]; -#endif /* !UNROLL_LOOPS */ - } -} - -void sha512_init(sha512_ctx *ctx) -{ -#ifndef UNROLL_LOOPS - int i; - for (i = 0; i < 8; i++) { - ctx->h[i] = sha512_h0[i]; - } -#else - ctx->h[0] = sha512_h0[0]; ctx->h[1] = sha512_h0[1]; - ctx->h[2] = sha512_h0[2]; ctx->h[3] = sha512_h0[3]; - ctx->h[4] = sha512_h0[4]; ctx->h[5] = sha512_h0[5]; - ctx->h[6] = sha512_h0[6]; ctx->h[7] = sha512_h0[7]; -#endif /* !UNROLL_LOOPS */ - - ctx->len = 0; - ctx->tot_len = 0; -} - -void sha512_update(sha512_ctx *ctx, const unsigned char *message, - unsigned int len) -{ - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char *shifted_message; - - tmp_len = SHA512_BLOCK_SIZE - ctx->len; - rem_len = len < tmp_len ? len : tmp_len; - - memcpy(&ctx->block[ctx->len], message, rem_len); - - if (ctx->len + len < SHA512_BLOCK_SIZE) { - ctx->len += len; - return; - } - - new_len = len - rem_len; - block_nb = new_len / SHA512_BLOCK_SIZE; - - shifted_message = message + rem_len; - - sha512_transf(ctx, ctx->block, 1); - sha512_transf(ctx, shifted_message, block_nb); - - rem_len = new_len % SHA512_BLOCK_SIZE; - - memcpy(ctx->block, &shifted_message[block_nb << 7], - rem_len); - - ctx->len = rem_len; - ctx->tot_len += (block_nb + 1) << 7; -} - -void sha512_final(sha512_ctx *ctx, unsigned char *digest) -{ - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; - -#ifndef UNROLL_LOOPS - int i; -#endif - - block_nb = 1 + ((SHA512_BLOCK_SIZE - 17) - < (ctx->len % SHA512_BLOCK_SIZE)); - - len_b = (ctx->tot_len + ctx->len) << 3; - pm_len = block_nb << 7; - - memset(ctx->block + ctx->len, 0, pm_len - ctx->len); - ctx->block[ctx->len] = 0x80; - UNPACK32(len_b, ctx->block + pm_len - 4); - - sha512_transf(ctx, ctx->block, block_nb); - -#ifndef UNROLL_LOOPS - for (i = 0 ; i < 8; i++) { - UNPACK64(ctx->h[i], &digest[i << 3]); - } -#else - UNPACK64(ctx->h[0], &digest[ 0]); - UNPACK64(ctx->h[1], &digest[ 8]); - UNPACK64(ctx->h[2], &digest[16]); - UNPACK64(ctx->h[3], &digest[24]); - UNPACK64(ctx->h[4], &digest[32]); - UNPACK64(ctx->h[5], &digest[40]); - UNPACK64(ctx->h[6], &digest[48]); - UNPACK64(ctx->h[7], &digest[56]); -#endif /* !UNROLL_LOOPS */ -} - -/* SHA-384 functions */ - -void sha384_init(sha384_ctx *ctx) -{ -#ifndef UNROLL_LOOPS - int i; - for (i = 0; i < 8; i++) { - ctx->h[i] = sha384_h0[i]; - } -#else - ctx->h[0] = sha384_h0[0]; ctx->h[1] = sha384_h0[1]; - ctx->h[2] = sha384_h0[2]; ctx->h[3] = sha384_h0[3]; - ctx->h[4] = sha384_h0[4]; ctx->h[5] = sha384_h0[5]; - ctx->h[6] = sha384_h0[6]; ctx->h[7] = sha384_h0[7]; -#endif /* !UNROLL_LOOPS */ - - ctx->len = 0; - ctx->tot_len = 0; -} - -void sha384_update(sha384_ctx *ctx, const unsigned char *message, - unsigned int len) -{ - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char *shifted_message; - - tmp_len = SHA384_BLOCK_SIZE - ctx->len; - rem_len = len < tmp_len ? len : tmp_len; - - memcpy(&ctx->block[ctx->len], message, rem_len); - - if (ctx->len + len < SHA384_BLOCK_SIZE) { - ctx->len += len; - return; - } - - new_len = len - rem_len; - block_nb = new_len / SHA384_BLOCK_SIZE; - - shifted_message = message + rem_len; - - sha512_transf(ctx, ctx->block, 1); - sha512_transf(ctx, shifted_message, block_nb); - - rem_len = new_len % SHA384_BLOCK_SIZE; - - memcpy(ctx->block, &shifted_message[block_nb << 7], - rem_len); - - ctx->len = rem_len; - ctx->tot_len += (block_nb + 1) << 7; -} - -void sha384_final(sha384_ctx *ctx, unsigned char *digest) -{ - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; - -#ifndef UNROLL_LOOPS - int i; -#endif - - block_nb = (1 + ((SHA384_BLOCK_SIZE - 17) - < (ctx->len % SHA384_BLOCK_SIZE))); - - len_b = (ctx->tot_len + ctx->len) << 3; - pm_len = block_nb << 7; - - memset(ctx->block + ctx->len, 0, pm_len - ctx->len); - ctx->block[ctx->len] = 0x80; - UNPACK32(len_b, ctx->block + pm_len - 4); - - sha512_transf(ctx, ctx->block, block_nb); - -#ifndef UNROLL_LOOPS - for (i = 0 ; i < 6; i++) { - UNPACK64(ctx->h[i], &digest[i << 3]); - } -#else - UNPACK64(ctx->h[0], &digest[ 0]); - UNPACK64(ctx->h[1], &digest[ 8]); - UNPACK64(ctx->h[2], &digest[16]); - UNPACK64(ctx->h[3], &digest[24]); - UNPACK64(ctx->h[4], &digest[32]); - UNPACK64(ctx->h[5], &digest[40]); -#endif /* !UNROLL_LOOPS */ -} - diff -uNr a/src/otherpplgode/sha.h b/src/otherpplgode/sha.h --- a/src/otherpplgode/sha.h b732edbf39e4671a814c80341bf843c33093d341bf0833b77e30270ce653ad7ececa5a9486a7b5050902afd82d61f08c705d66b50d323eb0d241285dbeffd1f6 +++ b/src/otherpplgode/sha.h false @@ -1,81 +0,0 @@ -/* - * FIPS 180-2 SHA-224/256/384/512 implementation - * Last update: 02/02/2007 - * Issue date: 04/30/2005 - * - * Copyright (C) 2005, 2007 Olivier Gay - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * ********************MODIFIED FOR USE IN SMALPEST******************** - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef SHA_INTERNAL_H -#define SHA_INTERNAL_H - -#include - -#define SHA256_DIGEST_SIZE ( 256 / 8) -#define SHA384_DIGEST_SIZE ( 384 / 8) -#define SHA512_DIGEST_SIZE ( 512 / 8) - -#define SHA256_BLOCK_SIZE ( 512 / 8) -#define SHA512_BLOCK_SIZE (1024 / 8) -#define SHA384_BLOCK_SIZE SHA512_BLOCK_SIZE - -typedef struct { - unsigned int tot_len; - unsigned int len; - unsigned char block[2 * SHA256_BLOCK_SIZE]; - uint32_t h[8]; -} sha256_ctx; - -typedef struct { - unsigned int tot_len; - unsigned int len; - unsigned char block[2 * SHA512_BLOCK_SIZE]; - uint64_t h[8]; -} sha512_ctx; - -typedef sha512_ctx sha384_ctx; - -void sha256_init(sha256_ctx * ctx); -void sha256_update(sha256_ctx *ctx, const unsigned char *message, - unsigned int len); -void sha256_final(sha256_ctx *ctx, unsigned char *digest); - -void sha384_init(sha384_ctx *ctx); -void sha384_update(sha384_ctx *ctx, const unsigned char *message, - unsigned int len); -void sha384_final(sha384_ctx *ctx, unsigned char *digest); - -void sha512_init(sha512_ctx *ctx); -void sha512_update(sha512_ctx *ctx, const unsigned char *message, - unsigned int len); -void sha512_final(sha512_ctx *ctx, unsigned char *digest); - -#endif - diff -uNr a/src/packet.h b/src/packet.h --- a/src/packet.h d05193fac4623a0040118336548a03e67f58d0e37188a5d2d85433e00d9c25477049a3e4e2140f4783f96cae9e763d662757904aca56d95343eab7d90fca894c +++ b/src/packet.h aa379369c8694631dc5e2c09c061382a3bfbc3e99679706e5edb7ced0c5bb5952dcb8631135c784f1a98df24416add82c601b140ef43cfa447e15ca6f4dd20c3 @@ -14,8 +14,8 @@ bool red_rebroadcast (uint8_t *r,hash_t const hash,uint8_t bounces); bool send_message (uint8_t command,uint8_t *r,hash_t hash,peer_t *chan,pkey_t *pk); -bool send_text (uint8_t command,uint64_t stamp,uint8_t const *text,size_t text_size,peer_t *chan); -bool send_str (uint8_t cmd,char const *str,peer_t *chan); +bool send_text (uint8_t command,uint64_t stamp,uint8_t const *text,size_t text_size,peer_t *chan,pkey_t *pk); +bool send_ustr (uint8_t cmd,char const *ustr,peer_t *chan); bool pest_recv (uint64_t time); diff -uNr a/src/packetlog.c b/src/packetlog.c --- a/src/packetlog.c 0e49ce5aa71360b2b86eccd3136be14d823c70e0f1e2085611a5cc9c5cc4496364827fb336eb35821ae8b2d8e007509473c0bede80f0086e098458308a64d794 +++ b/src/packetlog.c 16f0a79bffc5c27b5fc9ed38e099cb91ec43c416890cff2a58cfb198f0ea0c515d2786684fc077fc78b3c58289400668a4bb8f260f2a091f82f73cc15ab24823 @@ -4,6 +4,7 @@ #include "crypto.h" #include "time.h" #include "config.h" +#include #include #define ARROW_DISCARD ANSI_GRAY "<-" ANSI_NORM @@ -35,7 +36,7 @@ char const *dst = p ? p->handle[0] : "*"; #ifndef LOG_IGNORE - if(cmd == CMD_IGNORE && outgoing) return; + if(cmd == CMD_IGNORE) return; #endif hash_str_log(hash_hex,sizeof(hash_hex),hash); @@ -76,15 +77,20 @@ uint8_t cmd, peer_t *p, addr_t const *addr, + uint64_t time, + uint64_t stamp, char const *reason){ char hash_hex [HASH_HEX_BUF]; char at [AT_BUF]; + char tdiff [STAMPDIFF_BUF]; hash_str_log(hash_hex,sizeof(hash_hex),hash); at_from_addr(at,sizeof(at),addr); - log_out("%s %s " ARROW_DISCARD " %s %s [%s]", - cmd_abbrev[cmd],hash_hex,p->handle[0],at,reason); + fmt_stampdiff(tdiff,sizeof(tdiff),time,stamp); + + log_out("%s %s " ARROW_DISCARD " %s %s [%s] [%s]", + cmd_abbrev[cmd],hash_hex,p->handle[0],at,reason,tdiff); } void log_stale(hash_t const hash, @@ -98,63 +104,51 @@ return; #endif - char diff[STAMPDIFF_BUF]; - fmt_stampdiff(diff,sizeof(diff),time,stamp); - log_discard(hash,cmd,p,addr,diff); + log_discard(hash,cmd,p,addr,time,stamp,"stale"); } void log_dupe(hash_t const hash, uint8_t cmd, peer_t *p, - addr_t const *addr){ + addr_t const *addr, + uint64_t time, + uint64_t stamp){ #ifndef LOG_DUPLICATE return; #endif - log_discard(hash,cmd,p,addr,"duplicate"); + log_discard(hash,cmd,p,addr,time,stamp,"duplicate"); } void log_dedup_full(hash_t const hash, uint8_t cmd, peer_t *p, - addr_t const *addr){ + addr_t const *addr, + uint64_t time, + uint64_t stamp){ #ifndef LOG_DUPLICATE return; #endif - log_discard(hash,cmd,p,addr,"dedup buffer full"); -} - -void log_ignore(hash_t const hash, - uint8_t cmd, - peer_t *p, - addr_t const *addr){ - -#ifndef LOG_IGNORE - return; -#endif - - log_discard(hash,cmd,p,addr,"ignore"); + log_discard(hash,cmd,p,addr,time,stamp,"dedup buffer full"); } void log_cutoff(hash_t const hash, uint8_t cmd, peer_t *p, addr_t const *addr, + uint64_t time, + uint64_t stamp, uint8_t bounces){ - char hash_hex [HASH_HEX_BUF]; - char at [AT_BUF]; - #ifndef LOG_CUTOFF return; #endif - hash_str_log(hash_hex,sizeof(hash_hex),hash); - at_from_addr(at,sizeof(at),addr); - log_out("%s %s " ARROW_DISCARD " %s %s [discarded: bounces = %u]", - cmd_abbrev[cmd],hash_hex,p->handle[0],at,bounces); + char reason[21+INT64_LEN+1]; //discarded: bounces = $num\0 + snprintf(reason,sizeof(reason),"discarded: bounces = %u",bounces); + log_discard(hash,cmd,p,addr,time,stamp,reason); } diff -uNr a/src/peer.c b/src/peer.c --- a/src/peer.c f3fcb4725c81aaf279209391dc72553a8ce39d7cb37f92ce2ab1358a3e0b072b33a363a3165ba596b8bc0099bfcb935aa20d9cb20cf4abdd5a1557794d38455a +++ b/src/peer.c c2fc628b8aaa00c26d6fea2117c15937350ea5fb404e06017792ba203fd9fd4b6207644ee04823c2646d55ad80314ac27997c750ef17209c449f5efbc5024a7c @@ -8,6 +8,7 @@ #include "time.h" #include "util.h" #include "string.h" +#include "wipe.h" #include "state.h" #include #include @@ -160,6 +161,7 @@ ARR_DEL(peer,peer_cnt,p); peer_cnt--; FMT_PATH(path,DIR_PEER"/%s",handle); + dir_zero_nolog(path); if(!rm_rf(path)){ disk_unsync(); } diff -uNr a/src/peerio.c b/src/peerio.c --- a/src/peerio.c 6c03825c2db9ce9d1bf96dabcdad757059e3a88a194b41568006fbeea1f5e35d25e860a2fc0d4d54bc41b490b974e939d33cf386261a01bbd8f915bc67fb462e +++ b/src/peerio.c 4b109c47178052af14cf87bb0a5065580f875e7add3e2ff023aa017868eb81a3a4b445269e1e4cb551cc13f6e35cb1befb55380c54b7b2d546fbbfac853eeeb3 @@ -17,13 +17,13 @@ FMT_PATH(path,DIR_PEER"/%s/"FILE_ALIAS,p->handle[0]); if(p->handle_cnt <= 1){ - if(!file_delete(path)){ + if(!file_delete_zero(path)){ disk_unsync(); return 0; } return 1; } - f = file_open(path,"wb"); + f = file_open_zero(path,"wb"); if(!f){ disk_unsync(); return 0; @@ -85,7 +85,7 @@ assert(p->key_cnt > 0); FMT_PATH(path,DIR_PEER"/%s/"FILE_KEY,p->handle[0]); - f = file_open(path,"wb"); + f = file_open_zero(path,"wb"); if(!f){ disk_unsync(); return 0; @@ -279,8 +279,7 @@ } static bool peer_chainmess_load(peer_t *p){ - char path[PATH_BUF]; - size_t cnt; + char path[PATH_BUF]; if(!mess_load(&p->mess)) return 0; if(!chain_load(&p->dchain)) return 0; @@ -292,10 +291,10 @@ FMT_PATH(path,DIR_PEER"/%s/"DIR_DCHAIN"/"FILE_SELF,p->handle[0]); if(file_exists(path)){ - if(!hash_load(path,&p->self,&cnt,1)) return 0; + if(!hash_load(path,p->self)) return 0; }else{ hash_zero(p->self); - if(!hash_store(path,(hash_t const *)&p->self,1)) return 0; + if(!hash_store(path,p->self)) return 0; } return 1; diff -uNr a/src/pest.c b/src/pest.c --- a/src/pest.c a0d4911b59a6ae1cbf0786a01a132d00a1166c0fb1374e2afde87d2f2fc1ac57c4a460c892212974da6022b6b4e4b55304a15d24a079a17f852936bd0e5c5921 +++ b/src/pest.c 5182bfa2a642f8db6e41c730ec6afa50736efcec29fd3037a799796aeadd4c989900afdf3cbecbd4f370b86768df38791edef2c3e743ab3869754f3212b20051 @@ -1,22 +1,23 @@ #include "pest.h" +#include "irc.h" +#include "ircd.h" +#include "addr.h" #include "packet.h" #include "peer.h" #include "chain.h" #include "crypto.h" +#include "wipe.h" #include "net.h" #include "io.h" #include "log.h" #include "time.h" #include "util.h" +#include "string.h" #include "config.h" #include "state.h" -//TODO: brian name for this -bool pest_init(pestev_f func){ - char path[PATH_BUF]; - size_t cnt; - - pest_func = func; +bool pest_init(void){ + char path[PATH_BUF]; if(!knob_load()) return 0; dedup_load(); @@ -34,18 +35,18 @@ FMT_PATH(path,DIR_CHAIN"/%s",FILE_SELF); if(file_exists(path)){ - if(!hash_load(path,&self,&cnt,1)) return 0; + if(!hash_load(path,self)) return 0; }else{ hash_zero(self); - if(!hash_store(path,(hash_t const *)&self,1)) return 0; + if(!hash_store(path,self)) return 0; } FMT_PATH(path,DIR_CHAIN"/%s",FILE_NET); if(file_exists(path)){ - if(!hash_load(path,&net,&cnt,1)) return 0; + if(!hash_load(path,net)) return 0; }else{ hash_zero(self); - if(!hash_store(path,(hash_t const *)&net,1)) return 0; + if(!hash_store(path,net)) return 0; } console_stamp(0,mono_sec()); @@ -55,23 +56,23 @@ static void peer_check_active(uint64_t time){ for(size_t x = 0;x < peer_cnt;x++){ - peer_t *p = peer+x; + peer_t *p = peer+x; bool active = p->active; p->active = time < p->pak_stamp+irctime; - if(pest_func && !active && p->active){ - pest_func(PEST_ACTIVE,p->handle[0],0); + if(!active && p->active){ + irc_join(p->handle[0]); } - if(pest_func && active && !p->active){ - pest_func(PEST_INACTIVE,p->handle[0],0); + if(active && !p->active){ + irc_part(p->handle[0]); } } } void pest_tick(uint64_t time,uint64_t ntime,bool active){ - if(!active){ + if(!active || !udp_bound()){ udp_recv(0,0,pest_port,0,0); - return; + if(!active) return; } dedup_clean (time); @@ -91,32 +92,307 @@ } } -bool pest_broadcast(char const *msg){ - return send_str(CMD_BROADCAST,msg,0); +static void pestop_wot(pestcmd_t const *c){ + if(c->argc > 1 || c->trail){ + irc_out("usage: WOT [handle]"); + return; + } + + if(c->argc > 0){ + char *handle = c->argv[0]; + peer_t *p = peer_lookup(handle); + if(!p) return; + peer_print(p); + }else{ + peer_wot_print(); + } } -bool pest_direct(char const *handle,char const *msg){ - peer_t *p = peer_lookup(handle); - if(!p) return 0; - if(!peer_cansend(p,1)) return 0; - return send_str(CMD_DIRECT,msg,p); +static void pestop_peer(pestcmd_t const *c){ + char host[IRC_HOST_BUF]; + peer_t *p; + + if(c->argc != 1 || c->trail){ + irc_out("usage: PEER handle"); + return; + } + + char *handle = c->argv[0]; + p = peer_add(handle); + if(!p) return; + fmt_peerhost(host,sizeof(host),p->handle[0]); } -bool pest_achtung(char const *msg){ - uint16_t peer_idx[peer_cnt]; +static void pestop_unpeer(pestcmd_t const *c){ + char host[IRC_HOST_BUF]; - if(!index_shuffle(peer_idx,peer_cnt)){ - irclog_err("could not shuffle index, achtung not sent"); - return 0; + if(c->argc != 1 || c->trail){ + irc_out("usage: UNPEER handle"); + return; } - for(size_t x = 0;x < peer_cnt;x++){ - peer_t *p = peer+peer_idx[x]; - if(!peer_cansend(p,0)) continue; - send_str(CMD_DIRECT,msg,p); + char *handle = c->argv[0]; + peer_t *p = peer_lookup(handle); + if(!p) return; + fmt_peerhost(host,sizeof(host),p->handle[0]); + if(p->active){ + ircd_sendf(":%s PART %s",host,irc_chan); } + peer_del(p); +} - return 1; +static void pestop_aka(pestcmd_t const *c){ + if(c->argc != 2 || c->trail){ + irc_out("usage: AKA handle alias"); + return; + } + + char *handle = c->argv[0]; + char *alias = c->argv[1]; + peer_t *p = peer_lookup(handle); + if(!p) return; + if(!peer_alias_add(p,alias)) return; +} + +static void pestop_unaka(pestcmd_t const *c){ + char host[IRC_HOST_BUF]; + + if(c->argc != 1 || c->trail){ + irc_out("usage: UNAKA handle"); + return; + } + + char *handle = c->argv[0]; + peer_t *p = peer_lookup(handle); + if(!p) return; + + fmt_peerhost(host,sizeof(host),p->handle[0]); + unsigned r = peer_alias_del(p,handle); + if(!r) return; + if(r == 2){ //TODO: this is disgusting but works + ircd_sendf(":%s NICK :%s",host,p->handle[0]); + } +} + +static void pestop_pause(pestcmd_t const *c){ + if(c->argc != 1 || c->trail){ + irc_out("usage: PAUSE handle"); + return; + } + + char *handle = c->argv[0]; + peer_t *p = peer_lookup(handle); + if(!p) return; + peer_pause(p,1); +} + +static void pestop_unpause(pestcmd_t const *c){ + if(c->argc != 1 || c->trail){ + irc_out("usage: UNPAUSE handle"); + return; + } + + char *handle = c->argv[0]; + peer_t *p = peer_lookup(handle); + if(!p) return; + peer_pause(p,0); +} + +static void pestop_key(pestcmd_t const *c){ + if(c->argc != 2 || c->trail){ + irc_out("usage: KEY handle key"); + return; + } + + uint8_t key[KEY_SIZE]; + char *handle = c->argv[0]; + char *b64_key = c->argv[1]; + peer_t *p = peer_lookup(handle); + if(!p) return; + + if(!key_from_b64(key,b64_key)){ + irc_err("invalid key: '%s'",b64_key); + key_zero(key); + return; + } + + peer_key_add(p,key,0); + key_zero(key); +} + +static void pestop_unkey(pestcmd_t const *c){ + uint8_t key[KEY_SIZE]; + + if(c->argc != 1 || c->trail){ + irc_out("usage: UNKEY key"); + return; + } + + char *b64_key = c->argv[0]; + if(!key_from_b64(key,b64_key)){ + irc_err("invalid key: '%s'",b64_key); + key_zero(key); + return; + } + + peer_key_del(key,0); + key_zero(key); +} + +static void pestop_genkey(pestcmd_t const *c){ + uint8_t key [KEY_SIZE]; + char b64_key [B64_KEY_BUF]; + + if(c->argc != 0 || c->trail){ + irc_out("usage: GENKEY"); + return; + } + + if(!key_gen(key)){ + return; + } + + b64_from_key(b64_key,sizeof(b64_key),key); + irc_out("key generated from '%s': %s",RNG_SLOW_PATH,b64_key); + key_zero(key); + BUF_ZERO(b64_key); +} + +static void pestop_rekey(pestcmd_t const *c){ + if(c->argc > 1 || c->trail){ + irc_out("usage: REKEY [handle]"); + return; + } + + if(c->argc == 0){ + rekey_initiate(0,1); + }else{ + char *handle = c->argv[0]; + peer_t *p = peer_lookup(handle); + if(!p) return; + rekey_initiate(p,1); + } +} + +static void pestop_rktog(pestcmd_t const *c){ + if(c->argc > 1 || c->trail){ + usage: + irc_out("usage: RKTOG ['ENABLE'/'DISABLE']"); + return; + } + + if(c->argc == 0){ + knob_display(FILE_RKRECV,0); + }else if(strcmpci(c->argv[0],"ENABLE") == 0){ + knob_set(FILE_RKRECV,"1"); + }else if(strcmpci(c->argv[0],"DISABLE") == 0){ + knob_set(FILE_RKRECV,"0"); + }else{ + goto usage; + } +} + +static void pestop_gag(pestcmd_t const *c){ + if(c->argc > 1 || c->trail){ + irc_out("usage: GAG [handle]"); + return; + } + + if(c->argc == 0){ + killfile_print(); + }else{ + char *handle = c->argv[0]; + killfile_add(handle); + } +} + +static void pestop_ungag(pestcmd_t const *c){ + if(c->argc != 1 || c->trail){ + irc_out("usage: UNGAG handle"); + return; + } + + char *handle = c->argv[0]; + killfile_del(handle); +} + +static void pestop_at(pestcmd_t const *c){ + if(c->argc > 2 || c->trail){ + irc_out("usage: AT [handle [ip:port]]"); + return; + } + + if(c->argc == 0){ + peer_at_print(); + }else if(c->argc == 1){ + char *handle = c->argv[0]; + peer_t *p = peer_lookup(handle); + if(!p) return; + if(addr_null(&p->addr)){ + irc_out("no address stored for peer '%s'",handle); + return; + } + peer_addr_print(p); + }else{ + char *handle = c->argv[0]; + char *at = c->argv[1]; + peer_t *p = peer_lookup(handle); + addr_t addr; + + if(!p) return; + if(!addr_from_at(&addr,at)){ + irc_err("invalid address (format is ip:port): '%s'",at); + return; + } + + peer_addr_set(p,&addr,1); + } +} + +static void pestop_knob(pestcmd_t const *c){ + if((!c->trail && c->argc > 2) || + ( c->trail && c->argc != 1)){ + irc_out("usage: KNOB [knob [value]]"); + return; + } + + if(c->argc == 0){ + knob_display(0,0); + }else if(c->trail){ + char *knob = c->argv[0]; + char *value = c->trail; + knob_set(knob,value); + }else if(c->argc == 1){ + char *knob = c->argv[0]; + knob_display(knob,0); + }else{ + char *knob = c->argv[0]; + char *value = c->argv[1]; + knob_set(knob,value); + } +} + +static void pestop_knobinfo(pestcmd_t const *c){ + if(c->argc != 1 || c->trail){ + irc_out("usage: KNOBINFO knob"); + return; + } + + char *knob = c->argv[0]; + knob_display(knob,1); +} + +static void pestop_cut(pestcmd_t const *c){ + if(c->argc > 1 || c->trail){ + irc_out("usage: CUT [cutoff]"); + return; + } + + if(c->argc == 0){ + knob_display(FILE_CUTOFF,0); + }else{ + knob_set(FILE_CUTOFF,c->argv[0]); + } } static bool resolve(char const *handle, @@ -155,7 +431,7 @@ return 1; } -void pest_resolve(char const *handle){ +static void pest_resolve(char const *handle){ size_t n = 0; peer_t *p = peer_lookup(handle); if(!p) return; @@ -176,3 +452,240 @@ } } +static void pestop_resolve(pestcmd_t const *c){ + if(c->argc != 1 || c->trail){ + irc_out("usage: RESOLVE handle"); + return; + } + + char *handle = c->argv[0]; + pest_resolve(handle); +} + +static void pestop_slave(pestcmd_t const *c){ + if(c->argc > 1 || c->trail){ + irc_out("usage: SLAVE [handle]"); + return; + } + + if(!c->argc){ + if(*master){ + irc_out("slave mode is enabled (master = '%s')",master); + }else{ + irc_out("slave mode is disabled"); + } + return; + } + + if(*master){ + irc_err("slave mode already enabled"); + return; + } + + char *handle = c->argv[0]; + knob_set(FILE_MASTER,handle); + irc_out("slave mode enabled"); +} + +static void pestop_unslave(pestcmd_t const *c){ + if(c->argc != 0 || c->trail){ + irc_out("usage: UNSLAVE"); + return; + } + + if(!*master){ + irc_err("slave mode already disabled"); + return; + } + + knob_set(FILE_MASTER,""); + irc_out("slave mode disabled"); +} + +static bool pest_achtung(char const *msg){ + uint16_t peer_idx[peer_cnt]; + + if(!index_shuffle(peer_idx,peer_cnt)){ + irclog_err("could not shuffle index, achtung not sent"); + return 0; + } + + for(size_t x = 0;x < peer_cnt;x++){ + peer_t *p = peer+peer_idx[x]; + if(!peer_cansend(p,0)) continue; + send_ustr(CMD_DIRECT,msg,p); + } + + return 1; +} + +static void pestop_achtung(pestcmd_t const *c){ + if(!c->trail){ + irc_out("usage: ACHTUNG message"); + return; + } + + char *msg = c->trail; + pest_achtung(msg); +} + +static void pestop_scram(pestcmd_t const *c){ + char *pwd; + uint8_t pass[SHA512_SIZE]; + + if(!c->trail){ + irc_out("usage: SCRAM scrampass"); + return; + } + + pwd = c->trail; + sha512(pass,(uint8_t *)pwd,strlen(pwd)); + if(memcmp(pass,scram_pass,SHA512_SIZE) != 0){ + irc_err("SCRAM password mismatch"); + return; + } + + scram_push(); +} + +#define PEST_OP(N,A) { #N, A, pestop_##N }, + +typedef void (*pestcmd_f)(pestcmd_t const *); + +typedef struct pestop{ + char const *cmd; + size_t arg_max; + pestcmd_f func; +}pestop_t; + +static pestop_t const pest_op[] = { + PEST_OP (wot, 1) + PEST_OP (peer, 1) + PEST_OP (unpeer, 1) + PEST_OP (aka, 2) + PEST_OP (unaka, 1) + PEST_OP (pause, 1) + PEST_OP (unpause, 1) + PEST_OP (key, 2) + PEST_OP (unkey, 1) + PEST_OP (genkey, 0) + PEST_OP (rekey, 1) + PEST_OP (rktog, 1) + PEST_OP (gag, 1) + PEST_OP (ungag, 1) + PEST_OP (at, 2) + PEST_OP (knob, 1) + PEST_OP (knobinfo, 1) + PEST_OP (cut, 1) + PEST_OP (resolve, 1) + PEST_OP (slave, 1) + PEST_OP (unslave, 1) + PEST_OP (achtung, 0) + PEST_OP (scram, 0) +}; + +static pestop_t const *get_op(char const *cmd){ + char cmd_buf[PEST_CMD_BUF]; + + for(size_t x = 0;x < ARR_CNT(pest_op);x++){ + pestop_t const *op = pest_op+x; + if(strncmpci(cmd,op->cmd,IRCD_CMD_BUF) != 0) continue; + return op; + } + + strzcpy(cmd_buf,cmd,sizeof(cmd_buf)); + str_toupper(cmd_buf); + irc_err("unknown/unhandled pest command received: %s",cmd_buf); + return 0; +} + +static pestop_t const *parse_cmd(pestcmd_t *c,char *cmd){ + pestop_t const *op; + char *p; + char *l; + + c->cmd = cmd; + c->argc = 0; + c->trail = 0; + + p = scan_white(cmd); + if(!*p){ + return get_op(c->cmd); + } + *p++ = 0; + + op = get_op(c->cmd); + if(!op) return 0; + + p = scan_nowhite(p); + l = p; + while(*p){ + p = scan_white(p); + if(c->argc < op->arg_max){ + c->argv[c->argc] = l; + c->argc++; + }else{ + c->trail = l; + break; + } + if(!*p) break; + *p++ = 0; + p = scan_nowhite(p); + l = p; + } + + return op; +} + +static bool exec_cmd(char const *cmd){ + pestcmd_t c; + pestop_t const *op; + char cmd_buf[IRC_MSG_BUF]; + + strzcpy(cmd_buf,cmd,sizeof(cmd_buf)); + op = parse_cmd(&c,cmd_buf); + if(!op) return 0; + op->func(&c); + return 1; +} + +static char const *msg_cmdp(char const *msg){ + char const *f = scan_nowhite(msg); + return f[0] == '%' && f[1] != '%' ? ++f : 0; +} + +static char const *msg_esc(char const *msg){ + return msg[0] == '%' && msg[1] == '%' ? msg+1 : msg; +} + +bool pest_broadcast(char const *msg){ + char const *cmd = msg_cmdp(msg); + if(cmd){ + return exec_cmd(cmd); + }else{ + return send_ustr(CMD_BROADCAST,msg_esc(msg),0); + } +} + +bool pest_direct(char const *handle,char const *msg){ + char const *cmd = msg_cmdp(msg); + bool r = 1; + + irc_log_chan = handle; + if(cmd){ + r = exec_cmd(cmd); + }else{ + peer_t *p = peer_lookup(handle); + if(!p){ + ircd_sendf(":%s 401 %s %s :No such nick", + handle,self_handle,handle); + return 0; + } + if(!peer_cansend(p,1)) return 0; + r = send_ustr(CMD_DIRECT,msg_esc(msg),p); + } + + irc_log_chan = 0; + return r; +} + diff -uNr a/src/pest.h b/src/pest.h --- a/src/pest.h 65e894912b6909f9dc4f63adc694a5f02df004a72125abb0ee46a7790d373a78ae4575c659e7d7324cd70fe13379550819dabd072880b31b74175aa94f01c216 +++ b/src/pest.h 7baa0f304a8b2b38e14e5ccb0e8674b9eac1e27ce7243d413a60bcbf393fae047fd243aac743cb4368f3983b57ac9178bd502dc9cee60fa1bdb28f05460ae5e8 @@ -5,12 +5,10 @@ #include #include -bool pest_init (pestev_f func); +bool pest_init (void); void pest_tick (uint64_t time,uint64_t ntime,bool active); bool pest_broadcast (char const *msg); bool pest_direct (char const *handle,char const *msg); -bool pest_achtung (char const *msg); -void pest_resolve (char const *handle); void knob_display (char const *name,bool alt); void knob_set (char const *name,char const *val); @@ -71,7 +69,7 @@ bool broadcast_handle (peer_t *p,peer_t *gd_peer,bool immediate,bool pmaster,uint8_t *r,hash_t const hash,uint8_t bounces,char const *speaker); bool direct_handle (peer_t *p,peer_t *gd_peer,bool immediate,uint8_t *r,hash_t const hash,uint8_t bounces,char const *speaker); -void console_send (peer_t *chan,char const *speaker,uint8_t const *m,bool stampold); +void console_send (peer_t *p,peer_t *chan,char const *speaker,uint8_t const *m,bool stampold); void console_stamp (peer_t *p,uint64_t stamp); #endif diff -uNr a/src/recv.c b/src/recv.c --- a/src/recv.c 3e9904e4e4d739d47bc76a32301d4b6c873fe1d66a48d027da55a89c4d4dfb8502fe0f48a9f05c7051496594204da4b8dbb64cefc76332729b0ebd290328d4cb +++ b/src/recv.c 8211afbe36e5b6caa6e02a30f9c3bd2237500e372219fb65ef646f752fdc4f76bdeaae0f722aa0692e88a117111aac68423191a01337b0e95b7470011b4d3011 @@ -82,7 +82,6 @@ bool pmaster; bool gd; peer_t *gd_peer; - bool ig; //TMP xtrem spam test //static int ass = 0; @@ -91,7 +90,7 @@ //if(irc_ready && ++ass > 20){ ////if(irc_ready && ++ass > 40){ //char cunt[45]; - //rng_read(cunt,sizeof(cunt)); + //rng_read(rng_fast_fd,cunt,sizeof(cunt)); //for(size_t x = 0;x < sizeof(cunt);x++){ //if(cunt[x] < 0x20 || cunt[x] > 0x7E) cunt[x] = '.'; //} @@ -131,12 +130,13 @@ handle = p->handle[0]; - if(version > PEST_VERSION){ - log_out("protocol version 0x%02X (current: 0x%02X) " - "packet rejected from peer '%s'", - version,PEST_VERSION,handle); - goto clear; - } + //TODO: vat do with this + //if(version > PEST_VERSION){ + //log_out("protocol version 0x%02X (current: 0x%02X) " + //"packet rejected from peer '%s'", + //version,PEST_VERSION,handle); + //goto clear; + //} if(command >= CMD_UNDEF_MIN && command <= CMD_UNDEF_MAX){ @@ -146,16 +146,8 @@ goto clear; } - ig = command == CMD_IGNORE; - if(ig && - (!rekey_keyexists(&p->rekey) || - !key_equal(pk->key.full,p->rekey.key_new))){ - log_ignore(hash,command,p,&addr); - goto clear; - } - if(bounces > cutoff){ - log_cutoff(hash,command,p,&addr,bounces); + log_cutoff(hash,command,p,&addr,bounces,time,stamp); goto clear; } @@ -166,8 +158,8 @@ assert(gd_peer); } - //skip stamp stale check if expecting getdata or rekey ignore - if(!gd && !ig && stamp_stale(time,stamp)){ + //skip stamp stale check if expecting getdata + if(!gd && stamp_stale(time,stamp)){ log_stale(hash,command,p,&addr,time,stamp); goto clear; } @@ -185,16 +177,22 @@ //data from middle of dedup buffer. instead, make sure //to never send getdata for message that exists somewhere if(!gd && dedup_lookup(hash)){ - log_dupe(hash,command,p,&addr); + log_dupe(hash,command,p,&addr,time,stamp); goto clear; } if(!dedup_push(hash,time)){ - log_dedup_full(hash,command,p,&addr); + log_dedup_full(hash,command,p,&addr,time,stamp); goto clear; } } + peer_stamp(p,time); + peer_key_stamp(p,pk,time); + at_update(p,&addr); + + log_packet(hash,command,p,&addr,0,time,stamp); + cpy_speaker(speaker,sizeof(speaker),m); immediate = peer_handle_lookup(p,speaker); pmaster = *master && peer_handle_lookup(p,master); @@ -206,17 +204,10 @@ }else{ //add peer to message already in hearsay buffer hearsay_peer(h,p,bounces); - log_dupe(hash,command,p,&addr); goto clear; } } - peer_stamp(p,time); - peer_key_stamp(p,pk,time); - at_update(p,&addr); - - log_packet(hash,command,p,&addr,0,time,stamp); - //TODO: should this be handled only for BROADCAST/DIRECT? add to chain? if(killfile_lookup(speaker)){ log_out("speaker '%s' in killfile, ignoring packet",speaker); diff -uNr a/src/rekey.c b/src/rekey.c --- a/src/rekey.c e23a864b5742bd972e12dc4c5881b89305c43691bd5bd1bc8736c56474aaa74e3df60e8e0a6c8d390994f70ad315fe7a832e5f83f1d3f32dd0ba2049d5c9da83 +++ b/src/rekey.c 7eb93bf3f841292b23d794113246b7a3ed2b59a24756114778bdfb41fafa89ddd865ff8a34ae124758e5739c6ed92dd78911474cd2689eca4265ce8f0013cde0 @@ -52,7 +52,8 @@ mono_sec(), rk->offer_self, sizeof(rk->offer_self), - p)){ + p, + 0)){ rekey_abort(p,"could not send data"); return 0; } @@ -67,7 +68,8 @@ mono_sec(), rk->slice_self, sizeof(rk->slice_self), - p)){ + p, + 0)){ rekey_abort(p,"could not send data"); return 0; } @@ -147,9 +149,8 @@ } static bool rkignore_recv(peer_t *p,pkey_t *pk,uint64_t time){ - peer_stamp(p,time); - peer_key_stamp(p,pk,time); - return 1; + rekey_t *rk = &p->rekey; + return key_equal(pk->key.full,rk->key_new); } static bool packet_recv(peer_t *p,pkey_t *pk){ @@ -202,8 +203,9 @@ log_f out = log ? irclog_out : log_out; if(p){ - initiate(p,log); - out("rekeying with peer '%s' initiated",p->handle[0]); + if(initiate(p,log)){ + out("rekeying with peer '%s' initiated",p->handle[0]); + } }else{ uint16_t peer_idx[peer_cnt]; @@ -248,7 +250,6 @@ if(!peer_cansend(p,rk->log)) return 0; if(!keyoffer_recv(p,offer)) return 0; if(!keyoffer_send(p)) return 0; - rk->state = WAIT_SLICE1; rk->log = 0; rk->time = mono_sec(); @@ -259,7 +260,6 @@ if(!peer_cansend(p,rk->log)) return 0; if(!keyoffer_recv(p,offer)) return 0; if(!keyslice_send(p)) return 0; - rk->state = WAIT_SLICE2; return 1; diff -uNr a/src/rng.c b/src/rng.c --- a/src/rng.c e8d59c906b888a13d3fdeb93699ab91204c96430d38195c89dcc517f708cb9dc40fb2f9e661682261cdf2a718ac6e22fcb2a29511526ffef0f8ced10b404831e +++ b/src/rng.c 7aac4653483c841c7c70b4ee60058d54f6e6d1ca6e3d6006d1591251cb490aa685c00a67e36c98e31e68d632ed897a6968a1a65435bda779a70e576cb88bb68b @@ -6,45 +6,57 @@ #include #include -#define RNG_BORKTEST_SIZE 256 +#define RNG_BORKTEST_SIZE 32 -bool rng_open(char const *path){ +int rng_open(char const *path){ uint8_t bork[RNG_BORKTEST_SIZE]; + int fd; - rng_fd = open(path,O_RDONLY); - if(rng_fd == -1){ + fd = open(path,O_RDONLY); + if(fd == -1){ log_errno("open"); irclog_err("could not open rng '%s'",path); - return 0; + return -1; } - if(!rng_read(bork,sizeof(bork))){ - close(rng_fd); - return 0; + if(!rng_read(fd,bork,sizeof(bork))){ + close(fd); + return -1; } if(entropy_missing(bork,sizeof(bork))){ irclog_err("rng '%s' is borken, all bytes were 0x%02X", path,bork[0]); - close(rng_fd); - return 0; + close(fd); + return -1; } - return 1; + return fd; } -void rng_close(void){ - close(rng_fd); - rng_fd = -1; +void rng_close(int fd){ + close(fd); } -static ssize_t _rng_read(void *data,size_t size){ - memset_ffs(data,0x00,size); - return read(rng_fd,data,size); +static ssize_t _rng_read(int fd,void *data,size_t size){ + char *p = data; + size_t p_size = size; + + memset_ffs(p,0x00,p_size); + + do{ + ssize_t n = read(fd,p,p_size); + if(n < 0) return -1; + if(n > (ssize_t)p_size) return 0; + p += n; + p_size -= n; + }while(p_size > 0); + + return size; } -bool rng_read(void *data,size_t size){ - ssize_t r = _rng_read(data,size); +bool rng_read(int fd,void *data,size_t size){ + ssize_t r = _rng_read(fd,data,size); if(r != (ssize_t)size){ if(r == -1) log_errno("read"); irclog_err("could not read %zu bytes from rng",size); @@ -53,8 +65,8 @@ return 1; } -bool rng_read_nolog(void *data,size_t size){ - return _rng_read(data,size) == (ssize_t)size; +bool rng_read_nolog(int fd,void *data,size_t size){ + return _rng_read(fd,data,size) == (ssize_t)size; } bool entropy_missing(uint8_t const *data,size_t size){ diff -uNr a/src/send.c b/src/send.c --- a/src/send.c ba85171fbd8ef4ee91e068d9081e495ca434ac6c32cd5a5ac46a9be8031a04025241abd7af955e6368fbfeb7855a1f15143dc839a02f5057b5bafcaeddee43dc +++ b/src/send.c 264b4f626d3c8ff06c0be821bab29beebce5371a1ff31c0429d3698f53c58a83fd80806756db5df7bb7b58c3ac593b0c8aa806bee1eebf6e5a01c75a4fbbb11c @@ -7,6 +7,7 @@ #include "crypto.h" #include "io.h" #include "serial.h" +#include "utf8.h" #include "time.h" #include "util.h" #include "type.h" @@ -23,7 +24,7 @@ return 1; } - if(!rng_read(r+NONCE_OFF,NONCE_SIZE)){ + if(!rng_read(rng_fast_fd,r+NONCE_OFF,NONCE_SIZE)){ return 0; } @@ -55,7 +56,7 @@ pkey_t *pk){ peer_t *p = chan ? chan : peer; - size_t p_cnt = chan ? 1 : peer_cnt; + size_t p_cnt = chan ? 1 : peer_cnt; uint16_t peer_idx[p_cnt]; bool ret; uint8_t command; @@ -85,7 +86,6 @@ log_packet(hash,command,chan,chan ? &chan->addr : 0,1,0,0); #endif - memset_ffs(r,0x00,RED_SIZE); return ret; } @@ -132,7 +132,7 @@ FMT_PATH(path,DIR_CHAIN"/%s",file); } hash_copy(*dst,src); - if(!hash_store(path,(hash_t const *)dst,1)){ + if(!hash_store(path,*dst)){ disk_unsync(); } } @@ -141,11 +141,13 @@ uint64_t stamp, uint8_t const *text, size_t text_size, - peer_t *chan){ + peer_t *chan, + pkey_t *pk){ uint8_t r[RED_SIZE]; uint8_t *m; hash_t hash; + bool ret; m = r+MESSAGE_OFF; write_u64(m+TIMESTAMP_OFF,&stamp); @@ -191,42 +193,47 @@ break; } - return send_message(command,r,hash,chan,0); + ret = send_message(command,r,hash,chan,pk); + BUF_ZERO(r); + return ret; } -bool send_str(uint8_t cmd,char const *str,peer_t *chan){ - char const *str_orig; +bool send_ustr(uint8_t cmd,char const *ustr,peer_t *chan){ + char const *ustr_orig; uint64_t stamp; - size_t str_len; + size_t ustr_len; bool ret; + assert(utf8_valid(ustr)); + ret = 1; - str_orig = str; - str_len = strlen(str); + ustr_orig = ustr; + ustr_len = strlen(ustr); stamp = mono_sec(); do{ - size_t text_len = str_len; + size_t text_len = ustr_len; if(text_len > TEXT_SIZE){ - text_len = TEXT_SIZE; + text_len = utf8_cram_unsafe(ustr,TEXT_SIZE); } if(!send_text(cmd, stamp, - (uint8_t const *)str, + (uint8_t const *)ustr, text_len, - chan)){ + chan, + 0)){ ret = 0; } - str += text_len; - str_len -= text_len; - }while(str_len > 0); + ustr += text_len; + ustr_len -= text_len; + }while(ustr_len > 0); if(!ret){ irclog_err("pest message (%.10s...) " "was not sent to all peers", - str_orig); + ustr_orig); } console_stamp(chan,stamp); diff -uNr a/src/serial.h b/src/serial.h --- a/src/serial.h ae9f3112613fc53c8bf76c8d18425dfa4d7aa61623ccbd0b9263d0ca19aa2679fc64150a11ef2362fb1ca11dd5273acb717f38064cdfc155caa623a1bd5c7f0c +++ b/src/serial.h 5c3a61d6c1d3e9f8ff6562d3dda83b10f3e7a531281c1c41bec5623d47d37c24a4ff7fc89bb7cf658f461424158137de45781fa78cec4d2f915441f112c1c7b5 @@ -5,14 +5,21 @@ #include #define host8le (uint8_t) +#define host8be (uint8_t) #ifdef BIGENDIAN #define host16le byteswap16 + #define host16be (uint16_t) #define host32le byteswap32 + #define host32be (uint32_t) #define host64le byteswap64 + #define host64be (uint64_t) #else #define host16le (uint16_t) + #define host16be byteswap16 #define host32le (uint32_t) + #define host32be byteswap32 #define host64le (uint64_t) + #define host64be byteswap64 #endif #define __DEF_READ(NAME_PFIX,TYPE,WIDTH)\ diff -uNr a/src/sha.c b/src/sha.c --- a/src/sha.c 9be502a88358aee254f755d25ff1d2a732db0ef5bd98ac5d5071ccc4d04744e4cc2cdd496dd7b7668fef900b48f70dab5b959db34900a0cd426b4abf2d30939c +++ b/src/sha.c false @@ -1,28 +0,0 @@ -#include "crypto.h" -#include "otherpplgode/sha.h" -#include - -void sha256(uint8_t *sha,uint8_t const *msg,size_t msg_len){ - sha256_ctx ctx; - sha256_init(&ctx); - sha256_update(&ctx,msg,msg_len); - sha256_final(&ctx,sha); - VAR_ZERO(ctx); -} - -void sha384(uint8_t *sha,uint8_t const *msg,size_t msg_len){ - sha384_ctx ctx; - sha384_init(&ctx); - sha384_update(&ctx,msg,msg_len); - sha384_final(&ctx,sha); - VAR_ZERO(ctx); -} - -void sha512(uint8_t *sha,uint8_t const *msg,size_t msg_len){ - sha512_ctx ctx; - sha512_init(&ctx); - sha512_update(&ctx,msg,msg_len); - sha512_final(&ctx,sha); - VAR_ZERO(ctx); -} - diff -uNr a/src/sha256.c b/src/sha256.c --- a/src/sha256.c false +++ b/src/sha256.c 084c1efc0d9cadd3e3708b219e67929966988012e8cd8b53d4701333123bc3971aa07ea6951c0bf5bf489464701222c0c51e5473cc5afd0177a8723be5c4a6c5 @@ -0,0 +1,184 @@ +#include "crypto.h" +#include "serial.h" +#include + +#define SHA256_BLOCK_SIZE (512/8) + +#define BYTE_BITS 8 +#define WORD_SIZE 4 +#define BLOCK_BYTES SHA256_BLOCK_SIZE +#define BLOCK_BITS (BLOCK_BYTES*8) +#define BLOCK_WORDS (BLOCK_BYTES/WORD_SIZE) +#define LEN_BITS 64 +#define LEN_BYTES (LEN_BITS/BYTE_BITS) +#define ROUND_N 64 +#define STATE_N (SHA256_SIZE/WORD_SIZE) +#define OUT256_N STATE_N + +static uint32_t const h_init[STATE_N] = { + 0x6A09E667,0xBB67AE85,0x3C6EF372,0xA54FF53A, + 0x510E527F,0x9B05688C,0x1F83D9AB,0x5BE0CD19, +}; + +static uint32_t const k[ROUND_N] = { + 0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5, + 0x3956C25B,0x59F111F1,0x923F82A4,0xAB1C5ED5, + 0xD807AA98,0x12835B01,0x243185BE,0x550C7DC3, + 0x72BE5D74,0x80DEB1FE,0x9BDC06A7,0xC19BF174, + 0xE49B69C1,0xEFBE4786,0x0FC19DC6,0x240CA1CC, + 0x2DE92C6F,0x4A7484AA,0x5CB0A9DC,0x76F988DA, + 0x983E5152,0xA831C66D,0xB00327C8,0xBF597FC7, + 0xC6E00BF3,0xD5A79147,0x06CA6351,0x14292967, + 0x27B70A85,0x2E1B2138,0x4D2C6DFC,0x53380D13, + 0x650A7354,0x766A0ABB,0x81C2C92E,0x92722C85, + 0xA2BFE8A1,0xA81A664B,0xC24B8B70,0xC76C51A3, + 0xD192E819,0xD6990624,0xF40E3585,0x106AA070, + 0x19A4C116,0x1E376C08,0x2748774C,0x34B0BCB5, + 0x391C0CB3,0x4ED8AA4A,0x5B9CCA4F,0x682E6FF3, + 0x748F82EE,0x78A5636F,0x84C87814,0x8CC70208, + 0x90BEFFFA,0xA4506CEB,0xBEF9A3F7,0xC67178F2, +}; + +static inline uint32_t shr(uint32_t x,size_t n){ + return x>>n; +} + +static inline uint32_t ror(uint32_t x,size_t n){ + return x>>n | x<<(32-n); +} + +static inline uint32_t ch(uint32_t x, + uint32_t y, + uint32_t z){ + return ( x & y) ^ + (~x & z); +} + +static inline uint32_t maj(uint32_t x, + uint32_t y, + uint32_t z){ + return (x & y) ^ + (x & z) ^ + (y & z); +} + +static inline uint32_t usigma0(uint32_t x){ + return ror(x, 2) ^ + ror(x,13) ^ + ror(x,22); +} + +static inline uint32_t usigma1(uint32_t x){ + return ror(x, 6) ^ + ror(x,11) ^ + ror(x,25); +} + +static inline uint32_t lsigma0(uint32_t x){ + return ror(x, 7) ^ + ror(x,18) ^ + shr(x, 3); +} + +static inline uint32_t lsigma1(uint32_t x){ + return ror(x,17) ^ + ror(x,19) ^ + shr(x,10); +} + +static inline void expand_block(uint32_t *w,uint8_t const *block){ + for(size_t x = 0;x < BLOCK_WORDS;x++){ + uint32_t b_host; + memcpy(&b_host,block+sizeof(uint32_t)*x,sizeof(uint32_t)); + w[x] = host32be(b_host); + } + for(size_t x = BLOCK_WORDS;x < ROUND_N;x++){ + w[x] = lsigma1(w[x - 2]) + + w[x - 7] + + lsigma0(w[x - 15]) + + w[x - 16]; + } +} + +static void sha256_block(uint32_t *state,uint8_t const *block){ + uint32_t t1; + uint32_t t2; + uint32_t s[STATE_N]; + uint32_t w[ROUND_N]; + + for(size_t x = 0;x < STATE_N;x++){ + s[x] = state[x]; + } + + expand_block(w,block); + + for(size_t x = 0;x < ROUND_N;x++){ + t1 = k[x]+w[x]; + t1 += usigma1(s[4])+ch(s[4],s[5],s[6])+s[7]; + t2 = usigma0(s[0])+maj(s[0],s[1],s[2]); + s[7] = s[6]; + s[6] = s[5]; + s[5] = s[4]; + s[4] = s[3]+t1; + s[3] = s[2]; + s[2] = s[1]; + s[1] = s[0]; + s[0] = t1+t2; + } + + for(size_t x = 0;x < STATE_N;x++){ + state[x] += s[x]; + } +} + +void sha256(uint8_t *out,uint8_t const *data,size_t size){ + uint64_t size_b; + uint64_t spad_b; + size_t block_n; + size_t block_last_off; + size_t block_last_size; + size_t block_last_n; + uint8_t block_last[BLOCK_BYTES*2]; + size_t pad80_off; + size_t pad00_off; + size_t pad00_size; + uint64_t len; + size_t len_off; + uint32_t state[STATE_N]; + + size_b = (uint64_t)size*BYTE_BITS; + spad_b = size_b+1+LEN_BITS; + block_n = (spad_b-1)/BLOCK_BITS+1; + if((block_n-1)*BLOCK_BYTES > size){ + block_last_n = 2; + }else{ + block_last_n = 1; + } + block_last_off = (block_n-block_last_n)*BLOCK_BYTES; + block_last_size = size-block_last_off; + len = host64be(size_b); + len_off = BLOCK_BYTES*block_last_n-LEN_BYTES; + pad80_off = block_last_size; + pad00_off = pad80_off+1; + pad00_size = len_off-pad00_off; + + memcpy(block_last,data+block_last_off,block_last_size); + block_last[pad80_off] = 0x80; + memset(block_last+pad00_off,0x00,pad00_size); + memcpy(block_last+len_off,&len,LEN_BYTES); + memcpy(state,h_init,sizeof(state)); + + for(size_t x = 0;x < block_n-block_last_n;x++){ + sha256_block(state,data+x*BLOCK_BYTES); + } + for(size_t x = 0;x < block_last_n;x++){ + sha256_block(state,block_last+x*BLOCK_BYTES); + memset_ffs(block_last+x*BLOCK_BYTES,0x00,BLOCK_BYTES); + } + + for(size_t x = 0;x < OUT256_N;x++){ + uint32_t state_be = host32be(state[x]); + memcpy(out+x*sizeof(uint32_t),&state_be,sizeof(uint32_t)); + } +} + diff -uNr a/src/sha512.c b/src/sha512.c --- a/src/sha512.c false +++ b/src/sha512.c 532b53f5dfc4a550a6df7edbb32264c9b63fb371c2e286230152de04b4b55e78e3d74f02299af4545820ed8543f2a066cd44c3e6c9fd958916d1a517db52dd1d @@ -0,0 +1,254 @@ +#include "crypto.h" +#include "serial.h" +#include + +#define SHA384_BLOCK_SIZE SHA512_BLOCK_SIZE +#define SHA512_BLOCK_SIZE (1024/8) + +#define BYTE_BITS 8 +#define WORD_SIZE 8 +#define BLOCK_BYTES SHA512_BLOCK_SIZE +#define BLOCK_BITS (BLOCK_BYTES*8) +#define BLOCK_WORDS (BLOCK_BYTES/WORD_SIZE) +#define LEN_BITS 128 +#define LEN_EFF_BITS 64 +#define LEN_EFF_BYTES (LEN_EFF_BITS/BYTE_BITS) +#define ROUND_N 80 +#define STATE_N (SHA512_SIZE/WORD_SIZE) +#define OUT384_N (SHA384_SIZE/WORD_SIZE) +#define OUT512_N STATE_N +#define IPAD_CONST 0x36 +#define OPAD_CONST 0x5C + +static uint64_t const h384_init[STATE_N] = { + 0xCBBB9D5DC1059ED8,0x629A292A367CD507,0x9159015A3070DD17,0x152FECD8F70E5939, + 0x67332667FFC00B31,0x8EB44A8768581511,0xDB0C2E0D64F98FA7,0x47B5481DBEFA4FA4, +}; + +static uint64_t const h512_init[STATE_N] = { + 0x6A09E667F3BCC908,0xBB67AE8584CAA73B,0x3C6EF372FE94F82B,0xA54FF53A5F1D36F1, + 0x510E527FADE682D1,0x9B05688C2B3E6C1F,0x1F83D9ABFB41BD6B,0x5BE0CD19137E2179, +}; + +static uint64_t const k[ROUND_N] = { + 0x428A2F98D728AE22,0x7137449123EF65CD,0xB5C0FBCFEC4D3B2F,0xE9B5DBA58189DBBC, + 0x3956C25BF348B538,0x59F111F1B605D019,0x923F82A4AF194F9B,0xAB1C5ED5DA6D8118, + 0xD807AA98A3030242,0x12835B0145706FBE,0x243185BE4EE4B28C,0x550C7DC3D5FFB4E2, + 0x72BE5D74F27B896F,0x80DEB1FE3B1696B1,0x9BDC06A725C71235,0xC19BF174CF692694, + 0xE49B69C19EF14AD2,0xEFBE4786384F25E3,0x0FC19DC68B8CD5B5,0x240CA1CC77AC9C65, + 0x2DE92C6F592B0275,0x4A7484AA6EA6E483,0x5CB0A9DCBD41FBD4,0x76F988DA831153B5, + 0x983E5152EE66DFAB,0xA831C66D2DB43210,0xB00327C898FB213F,0xBF597FC7BEEF0EE4, + 0xC6E00BF33DA88FC2,0xD5A79147930AA725,0x06CA6351E003826F,0x142929670A0E6E70, + 0x27B70A8546D22FFC,0x2E1B21385C26C926,0x4D2C6DFC5AC42AED,0x53380D139D95B3DF, + 0x650A73548BAF63DE,0x766A0ABB3C77B2A8,0x81C2C92E47EDAEE6,0x92722C851482353B, + 0xA2BFE8A14CF10364,0xA81A664BBC423001,0xC24B8B70D0F89791,0xC76C51A30654BE30, + 0xD192E819D6EF5218,0xD69906245565A910,0xF40E35855771202A,0x106AA07032BBD1B8, + 0x19A4C116B8D2D0C8,0x1E376C085141AB53,0x2748774CDF8EEB99,0x34B0BCB5E19B48A8, + 0x391C0CB3C5C95A63,0x4ED8AA4AE3418ACB,0x5B9CCA4F7763E373,0x682E6FF3D6B2B8A3, + 0x748F82EE5DEFB2FC,0x78A5636F43172F60,0x84C87814A1F0AB72,0x8CC702081A6439EC, + 0x90BEFFFA23631E28,0xA4506CEBDE82BDE9,0xBEF9A3F7B2C67915,0xC67178F2E372532B, + 0xCA273ECEEA26619C,0xD186B8C721C0C207,0xEADA7DD6CDE0EB1E,0xF57D4F7FEE6ED178, + 0x06F067AA72176FBA,0x0A637DC5A2C898A6,0x113F9804BEF90DAE,0x1B710B35131C471B, + 0x28DB77F523047D84,0x32CAAB7B40C72493,0x3C9EBE0A15C9BEBC,0x431D67C49C100D4C, + 0x4CC5D4BECB3E42B6,0x597F299CFC657E2A,0x5FCB6FAB3AD6FAEC,0x6C44198C4A475817, +}; + +static inline uint64_t shr(uint64_t x,size_t n){ + return x>>n; +} + +static inline uint64_t ror(uint64_t x,size_t n){ + return x>>n | x<<(64-n); +} + +static inline uint64_t ch(uint64_t x, + uint64_t y, + uint64_t z){ + return ( x & y) ^ + (~x & z); +} + +static inline uint64_t maj(uint64_t x, + uint64_t y, + uint64_t z){ + return (x & y) ^ + (x & z) ^ + (y & z); +} + +static inline uint64_t usigma0(uint64_t x){ + return ror(x,28) ^ + ror(x,34) ^ + ror(x,39); +} + +static inline uint64_t usigma1(uint64_t x){ + return ror(x,14) ^ + ror(x,18) ^ + ror(x,41); +} + +static inline uint64_t lsigma0(uint64_t x){ + return ror(x, 1) ^ + ror(x, 8) ^ + shr(x, 7); +} + +static inline uint64_t lsigma1(uint64_t x){ + return ror(x,19) ^ + ror(x,61) ^ + shr(x, 6); +} + +static inline void expand_block(uint64_t *w,uint8_t const *block){ + for(size_t x = 0;x < BLOCK_WORDS;x++){ + uint64_t b_host; + memcpy(&b_host,block+sizeof(uint64_t)*x,sizeof(uint64_t)); + w[x] = host64be(b_host); + } + for(size_t x = BLOCK_WORDS;x < ROUND_N;x++){ + w[x] = lsigma1(w[x - 2]) + + w[x - 7] + + lsigma0(w[x - 15]) + + w[x - 16]; + } +} + +static void sha512_block(uint64_t *state,uint8_t const *block){ + uint64_t t1; + uint64_t t2; + uint64_t s[STATE_N]; + uint64_t w[ROUND_N]; + + for(size_t x = 0;x < STATE_N;x++){ + s[x] = state[x]; + } + + expand_block(w,block); + + for(size_t x = 0;x < ROUND_N;x++){ + t1 = k[x]+w[x]; + t1 += usigma1(s[4])+ch(s[4],s[5],s[6])+s[7]; + t2 = usigma0(s[0])+maj(s[0],s[1],s[2]); + s[7] = s[6]; + s[6] = s[5]; + s[5] = s[4]; + s[4] = s[3]+t1; + s[3] = s[2]; + s[2] = s[1]; + s[1] = s[0]; + s[0] = t1+t2; + } + + for(size_t x = 0;x < STATE_N;x++){ + state[x] += s[x]; + } +} + +static void sha512_compute(uint64_t *state, + size_t state_block_n, + uint8_t const *data, + size_t size){ + + uint64_t size_b; + uint64_t spad_b; + size_t block_n; + size_t block_last_off; + size_t block_last_size; + size_t block_last_n; + uint8_t block_last[BLOCK_BYTES*2]; + size_t pad80_off; + size_t pad00_off; + size_t pad00_size; + uint64_t len; + size_t len_off; + + size_b = (uint64_t)size*BYTE_BITS; + spad_b = size_b+1+LEN_BITS; + block_n = (spad_b-1)/BLOCK_BITS+1; + if((block_n-1)*BLOCK_BYTES > size){ + block_last_n = 2; + }else{ + block_last_n = 1; + } + block_last_off = (block_n-block_last_n)*BLOCK_BYTES; + block_last_size = size-block_last_off; + len = host64be(size_b+state_block_n*BLOCK_BITS); + len_off = BLOCK_BYTES*block_last_n-LEN_EFF_BYTES; + pad80_off = block_last_size; + pad00_off = pad80_off+1; + pad00_size = len_off-pad00_off; + + memcpy(block_last,data+block_last_off,block_last_size); + block_last[pad80_off] = 0x80; + memset(block_last+pad00_off,0x00,pad00_size); + memcpy(block_last+len_off,&len,sizeof(len)); + + for(size_t x = 0;x < block_n-block_last_n;x++){ + sha512_block(state,data+x*BLOCK_BYTES); + } + for(size_t x = 0;x < block_last_n;x++){ + sha512_block(state,block_last+x*BLOCK_BYTES); + memset_ffs(block_last+x*BLOCK_BYTES,0x00,BLOCK_BYTES); + } +} + +static void sha512_output(uint8_t *out,uint64_t const *state,size_t n){ + for(size_t x = 0;x < n;x++){ + uint64_t state_be = host64be(state[x]); + memcpy(out+x*sizeof(uint64_t),&state_be,sizeof(uint64_t)); + } +} + +void sha384(uint8_t *out,uint8_t const *data,size_t size){ + uint64_t state[STATE_N]; + memcpy(state,h384_init,sizeof(state)); + sha512_compute(state,0,data,size); + sha512_output(out,state,OUT384_N); +} + +void sha512(uint8_t *out,uint8_t const *data,size_t size){ + uint64_t state[STATE_N]; + memcpy(state,h512_init,sizeof(state)); + sha512_compute(state,0,data,size); + sha512_output(out,state,OUT512_N); +} + +void hmac_sha384(uint8_t *out, + uint8_t const *key, + size_t key_size, + uint8_t const *data, + size_t data_size){ + + uint8_t key_block [SHA384_BLOCK_SIZE]; + uint8_t inner [SHA384_SIZE]; + uint64_t state [STATE_N]; + + if(key_size <= SHA384_BLOCK_SIZE){ + memcpy(key_block,key,key_size); + }else{ + sha384(key_block,key,key_size); + key_size = SHA384_SIZE; + } + + memset(key_block+key_size,IPAD_CONST,SHA384_BLOCK_SIZE-key_size); + for(size_t x = 0;x < key_size;x++){ + key_block[x] ^= IPAD_CONST; + } + + memcpy(state,h384_init,sizeof(state)); + sha512_block(state,key_block); + sha512_compute(state,1,data,data_size); + sha512_output(inner,state,OUT384_N); + + for(size_t x = 0;x < SHA384_BLOCK_SIZE;x++){ + key_block[x] ^= IPAD_CONST ^ OPAD_CONST; + } + + memcpy(state,h384_init,sizeof(state)); + sha512_block(state,key_block); + sha512_compute(state,1,inner,SHA384_SIZE); + sha512_output(out,state,OUT384_N); + BUF_ZERO(key_block); +} + diff -uNr a/src/state.c b/src/state.c --- a/src/state.c 123c548fd3950d12b470fb240b58f8b6fce25e2ad35dd25607e35ca749046a6f699366ece28b0cef8654658281ac0210027328b20ff9bf18f44b4f9141adb893 +++ b/src/state.c fe31e7527ee86a446049dc72b9d9257d705b123feece109d8635d75620f8b04048a01152c68508517df5c31df2224d50b64b2da55b7a86e7bf2a77de1183fefd @@ -7,7 +7,8 @@ uint64_t mono_offset = -1; char log_path [PATH_BUF] = LOG_PATH_DEF; FILE *log_fp = 0; -int rng_fd = -1; +int rng_slow_fd = -1; +int rng_fast_fd = -1; char irc_chan [IRC_CHAN_BUF] = ""; char irc_host [IRC_HOST_BUF] = ""; @@ -17,8 +18,9 @@ bool irc_nicked = 0; bool irc_joined = 0; bool irc_ready = 0; +char const *irc_log_chan = 0; -int udp_listen_fd = -1; +int udp_fd = -1; bool udp_silent = 1; tcpev_f tcp_func = 0; int tcp_listen_fd = -1; @@ -30,8 +32,9 @@ ircev_f ircd_func = 0; uint32_t ircd_src_ip = 0; uint16_t ircd_src_port = 0; +char ircd_buf [IRC_MSG_BUF] = {0}; +char *ircd_buf_p = ircd_buf; -pestev_f pest_func = 0; uint64_t con_stamp = 0; uint64_t rekey_last = 0; uint64_t ignore_next = 0; diff -uNr a/src/state.h b/src/state.h --- a/src/state.h 96ba46ac1b0f48529916a458f1da2c6677f8afe52af9c88ae72ce3ea08254e6e37f80891046c7a82417ead77c781067c3873657153486aaba9191e3b36c9c565 +++ b/src/state.h 3c44c27062cae6ab19d1240accfd981fd70ada90ddfd872344e6c68093860348d6968b0c0070ff38d2efc9182b9e96b50694d844df889190eeda631d3fa91a20 @@ -15,7 +15,8 @@ extern uint64_t mono_offset; extern char log_path [PATH_BUF]; extern FILE *log_fp; -extern int rng_fd; +extern int rng_slow_fd; +extern int rng_fast_fd; extern char irc_chan [IRC_CHAN_BUF]; extern char irc_host [IRC_HOST_BUF]; @@ -25,8 +26,9 @@ extern bool irc_nicked; extern bool irc_joined; extern bool irc_ready; +extern char const *irc_log_chan; -extern int udp_listen_fd; +extern int udp_fd; extern bool udp_silent; extern tcpev_f tcp_func; extern int tcp_listen_fd; @@ -38,8 +40,9 @@ extern ircev_f ircd_func; extern uint32_t ircd_src_ip; extern uint16_t ircd_src_port; +extern char ircd_buf [IRC_MSG_BUF]; +extern char *ircd_buf_p; -extern pestev_f pest_func; extern uint64_t con_stamp; extern uint64_t rekey_last; extern uint64_t ignore_next; diff -uNr a/src/string.c b/src/string.c --- a/src/string.c 384103e5d3401e9434fbb65314d856293b40243aefc8daf8b715f57d2005f52b9c00b435246147cc77975b852db77abfe8d4b893ede03b323e54af97e524016f +++ b/src/string.c 3b965cae910b4f56ecdb2276c9b914807c2db5613cc2ea73a735e7f98039fd7a3a9c6cfb1f4886e1ddc5839b44cac051cf4408b810b9fb50d202542340d106f7 @@ -104,6 +104,28 @@ return (char *)(uintptr_t)s; } +static bool char_white(char c){ + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; +} + +char *scan_white(char const *p){ + while(*p && !char_white(*p)) p++; + return (char *)(uintptr_t)p; +} + +char *scan_nowhite(char const *p){ + while(char_white(*p)) p++; + return (char *)(uintptr_t)p; +} + +char *scan_back_nowhite(char const *str,char const *p){ + while(char_white(*p)){ + if(p <= str) return 0; + p--; + } + return (char *)(uintptr_t)p; +} + char *str_nstr(char const *s, char const *f, size_t s_len){ @@ -141,28 +163,6 @@ } } -static bool char_white(char c){ - return c == ' ' || c == '\t' || c == '\r' || c == '\n'; -} - -static inline char *scan_white(char const *p){ - while(*p && !char_white(*p)) p++; - return (char *)(uintptr_t)p; -} - -static inline char *scan_nowhite(char const *p){ - while(char_white(*p)) p++; - return (char *)(uintptr_t)p; -} - -static inline char *scan_back_nowhite(char const *str,char const *p){ - while(char_white(*p)){ - if(p <= str) return 0; - p--; - } - return (char *)(uintptr_t)p; -} - void str_stripnl(char *str){ char *p = strchr(str,'\n'); if(p) *p = 0; diff -uNr a/src/string.h b/src/string.h --- a/src/string.h dcc4c6a8ffe92fa7be3dcbdaf08f46103ecd5d1f70544bd41423941889535b70947a17eea4e9771bd192df834b7ff2899e37b0e64b602ce6c464cd1afa3b724e +++ b/src/string.h 12843f310906ff533a7be760d1e8081940e0d615102b43b10ce30525098e60f6622c2903aeb9b2d85a3cdb0c7810404157bf4aa8516a0779137718c005383eac @@ -5,22 +5,25 @@ #include #include -char *strzcpy (char *restrict dst,char const *restrict src,size_t dst_size); -char *strzcpy_quote (char *restrict dst,char const *restrict src,size_t dst_size); -inline char *strzzcpy (char *restrict dst,char const *restrict src,size_t dst_size,size_t src_size); -inline char *strpcpy (char *restrict dst,char const *restrict src,char const *restrict dst_end); -inline char *strpzcpy (char *restrict dst,char const *restrict src,char const *restrict dst_end,size_t src_size); -int strcmpci (char const *restrict a,char const *restrict b); -int strncmpci (char const *restrict a,char const *restrict b,size_t n); -char *strchr0 (char const *s,char c); -char *strnotchr0 (char const *s,char n); -char *str_nstr (char const *s,char const *f,size_t s_len); -void str_tolower (char *str); -void str_toupper (char *str); -void str_stripnl (char *str); -char *str_trim (char *str); -size_t str_args (char *str,size_t argc_max,char **argv); -bool str_valid (char const *str,size_t min,size_t max,char const *charset); +char *strzcpy (char *restrict dst,char const *restrict src,size_t dst_size); +char *strzcpy_quote (char *restrict dst,char const *restrict src,size_t dst_size); +inline char *strzzcpy (char *restrict dst,char const *restrict src,size_t dst_size,size_t src_size); +inline char *strpcpy (char *restrict dst,char const *restrict src,char const *restrict dst_end); +inline char *strpzcpy (char *restrict dst,char const *restrict src,char const *restrict dst_end,size_t src_size); +int strcmpci (char const *restrict a,char const *restrict b); +int strncmpci (char const *restrict a,char const *restrict b,size_t n); +char *strchr0 (char const *s,char c); +char *strnotchr0 (char const *s,char n); +char *scan_white (char const *str); +char *scan_nowhite (char const *str); +char *scan_back_nowhite (char const *str,char const *p); +char *str_nstr (char const *s,char const *f,size_t s_len); +void str_tolower (char *str); +void str_toupper (char *str); +void str_stripnl (char *str); +char *str_trim (char *str); +size_t str_args (char *str,size_t argc_max,char **argv); +bool str_valid (char const *str,size_t min,size_t max,char const *charset); inline char *strzzcpy(char *restrict dst, char const *restrict src, diff -uNr a/src/time.c b/src/time.c --- a/src/time.c c293856f555b23e9321eeb64d0099ee39b580397862d2542933658bff49ed3a9eded8d5dcf7734db6f47cea60518ed1ea0ce699ed3413587dc8394952bbb8589 +++ b/src/time.c b143f7b521f07c0d499ed9d946059d2219d10c6ed2dcbff0062d797f66c346ef05e84dfbb8e142e3f22e89721e3e449ccca4d6e785e97bdc85aebd075dfe7433 @@ -7,13 +7,15 @@ #include #include -static void monotonic(struct timespec *ts){ - int r = clock_gettime(CLOCK_MONOTONIC,ts); +static void gettime(clockid_t id,struct timespec *ts){ + int r = clock_gettime(id,ts); assert(r != -1); (void)r; +} + +static void monotonic(struct timespec *ts){ + gettime(CLOCK_MONOTONIC,ts); if(mono_offset == (uint64_t)-1){ - uint64_t tt = time(0); - assert(tt != (uint64_t)-1); - mono_offset = tt-(uint64_t)ts->tv_sec; + mono_offset = time_sec()-(uint64_t)ts->tv_sec; } } @@ -34,7 +36,9 @@ } uint64_t time_sec(void){ - return time(0); + struct timespec ts; + gettime(CLOCK_REALTIME,&ts); + return ts.tv_sec; } size_t time_str(char *buf, diff -uNr a/src/type.h b/src/type.h --- a/src/type.h c7777239d7ecf7609cc4231fb1401c62043b1cab244069d2e40bd3b9ffb8854dbd31a8457d4084c05f28c78c6d3bb3fc430931f930f9b88e44cac9fee8961d45 +++ b/src/type.h 85d4ab2de693185d9bf5eb7b39dff9bec98af8aa8f02338ca6333e97cc6eac778251f2c22c59be63a58a34711a294bc0472399d24535722199d7bdd4986b0a44 @@ -16,19 +16,24 @@ char *pre_host; char *cmd; size_t argc; - char *argv[IRCD_ARG_MAX]; - char *trail; + char *argv [IRCD_ARG_MAX]; }ircmsg_t; +typedef struct pestcmd{ + char *cmd; + size_t argc; + char *argv [PEST_ARG_MAX]; + char *trail; +}pestcmd_t; + typedef void (*tcpev_f) (unsigned); typedef void (*ircev_f) (unsigned,ircmsg_t const *); -typedef void (*pestev_f) (unsigned,char const *,char const *); typedef void (*log_f) (char const *,...); typedef char handle_t [HANDLE_BUF]; typedef uint16_t peerid_t; -typedef uint8_t hash_t[SHA256_SIZE]; +typedef uint8_t hash_t [SHA256_SIZE]; typedef struct addr{ uint32_t ip; @@ -36,23 +41,23 @@ }addr_t; typedef struct mess{ - char path[PATH_BUF]; - size_t idx_cnt; //# of idx files - size_t msg_cnt; //# of msg files - size_t idx_ecnt; //# of entries in last idx file - size_t msg_ecnt; //# of entries in last msg file + char path [PATH_BUF]; + size_t idx_cnt; //# of idx files + size_t msg_cnt; //# of msg files + size_t idx_ecnt; //# of entries in last idx file + size_t msg_ecnt; //# of entries in last msg file }mess_t; typedef struct chain{ - char path[PATH_BUF]; - size_t cha_cnt; //# of cha files - size_t cha_ecnt; //# of entries in last cha file + char path [PATH_BUF]; + size_t cha_cnt; //# of cha files + size_t cha_ecnt; //# of entries in last cha file }chain_t; //TODO: vtf call this typedef struct ht{ - char path[PATH_BUF]; - hash_t hash[HEADTAIL_MAX]; + char path [PATH_BUF]; + hash_t hash [HEADTAIL_MAX]; size_t cnt; size_t last; }ht_t; @@ -62,12 +67,12 @@ bool log; uint64_t time; size_t pak_cnt; - uint8_t offer_self [SHA512_SIZE]; - uint8_t offer_peer [SHA512_SIZE]; - uint8_t slice_self [KEY_SIZE]; - uint8_t slice_peer [KEY_SIZE]; - uint8_t key_old [KEY_SIZE]; - uint8_t key_new [KEY_SIZE]; + uint8_t offer_self [SHA512_SIZE]; + uint8_t offer_peer [SHA512_SIZE]; + uint8_t slice_self [KEY_SIZE]; + uint8_t slice_peer [KEY_SIZE]; + uint8_t key_old [KEY_SIZE]; + uint8_t key_new [KEY_SIZE]; }rekey_t; typedef struct pkey{ diff -uNr a/src/utf8.c b/src/utf8.c --- a/src/utf8.c false +++ b/src/utf8.c 84e42a8a637696ec293214c3964e9ed09d494683de824c6a3aa59a28e0ec79ea6da57bb0ed66887c21a5c490bfd7d3cf23da174b8fb6f3dfb984f5fb4277a9db @@ -0,0 +1,97 @@ +#include "utf8.h" +#include +#include + +static bool utf8_cont(uint8_t c){ + return (c & 0xC0) == 0x80; +} + +static bool unicode_valid(uint32_t u){ + return u <= 0x0010FFFF && + !(u >= 0x0000D800 && + u <= 0x0000DFFF); +} + +static size_t utf8_readchar(uint8_t const *p,uint32_t *u_out){ + uint32_t u; + size_t s; + + if((*p & 0x80) == 0x00){ + s = 1; + u = p[0]; + }else if((*p & 0xE0) == 0xC0){ + if(!utf8_cont(p[1])) return 0; + s = 2; + u = (p[0] & 0x1F) << 6 | + (p[1] & 0x3F) << 0; + if(u < 0x00000080) return 0; + }else if((*p & 0xF0) == 0xE0){ + if(!utf8_cont(p[1]) || + !utf8_cont(p[2])) return 0; + s = 3; + u = (p[0] & 0x0F) << 12 | + (p[1] & 0x3F) << 6 | + (p[2] & 0x3F) << 0; + if(u < 0x00000800 || + (u >= 0x0000D800 && + u <= 0x0000DFFF)) return 0; + }else if((*p & 0xF8) == 0xF0){ + if(!utf8_cont(p[1]) || + !utf8_cont(p[2]) || + !utf8_cont(p[3])) return 0; + s = 4; + u = (p[0] & 0x07) << 18 | + (p[1] & 0x3F) << 12 | + (p[2] & 0x3F) << 6 | + (p[3] & 0x3F) << 0; + if(u < 0x00010000 || + u > 0x0010FFFF) return 0; + }else{ + return 0; + } + + if(u_out) *u_out = u; + return s; +} + +static size_t utf8_charsize_unsafe(uint8_t const *p){ + if((*p & 0x80) == 0x00){ + return 1; + }else if((*p & 0xE0) == 0xC0){ + return 2; + }else if((*p & 0xF0) == 0xE0){ + return 3; + }else if((*p & 0xF8) == 0xF0){ + return 4; + }else{ + assert(0); + return 1; + } +} + +bool utf8_valid(char const *ustr){ + uint8_t const *p = (uint8_t const *)ustr; + + while(*p){ + size_t s = utf8_readchar(p,0); + if(s == 0) return 0; + p += s; + } + + return 1; +} + +size_t utf8_cram_unsafe(char const *ustr,size_t buf_size){ + uint8_t const *p = (uint8_t const *)ustr; + size_t b = buf_size; + + while(*p){ + size_t s = utf8_charsize_unsafe(p); + if(s > b) break; + p += s; + b -= s; + } + + return p-(uint8_t const *)ustr; +} + diff -uNr a/src/utf8.h b/src/utf8.h --- a/src/utf8.h false +++ b/src/utf8.h e8f0034aa38523e65686c929de9827807f2bc9826ce10367f77ec395fe40dc8d4ab53ec87703582a7d683f8d2624c2230f0edf83c7c383bd1062112cf6de65f5 @@ -0,0 +1,11 @@ +#ifndef UTF8_H +#define UTF8_H + +#include +#include + +bool utf8_valid (char const *ustr); +size_t utf8_cram_unsafe (char const *ustr,size_t buf_size); + +#endif + diff -uNr a/src/util.c b/src/util.c --- a/src/util.c efd0b894bfc8679f9264c14e1f096600b98969137a2a39fa0a33a0aec056f2248ac975f34a5a0fd0f7e0ac30e789b7e8e2a683eb0f9dec850b7831765b6d2b34 +++ b/src/util.c fca670c5ef930f7031ac9987d1ae584b61c40c3aa96efa990bf93a46fb3f09d383b41bf497517cd0245469f928e8820c2045e24356215df8bb6542638306cfd7 @@ -4,50 +4,11 @@ #include "crypto.h" #include "net.h" #include "def.h" +#include "state.h" #include #include #include -//TMP -#define HEX_DELIM 4 -#define HEX_GROUPS 4 -void print_hex(uint8_t const *buf,size_t len){ - size_t columns = HEX_DELIM*HEX_GROUPS; - size_t y = 0; - size_t z = 0; - size_t w = columns*3+columns/HEX_DELIM+6; - - for(size_t x = 0;x < len;x++){ - uint8_t c = buf[x]; - fprintf(stderr,"%02X ",c); - z += 3; - - if(x%HEX_DELIM == HEX_DELIM-1){ - fputc(' ',stderr); - fputc(' ',stderr); - z += 2; - } - - if(x%columns == columns-1 || x == len-1){ - for(size_t u = z;u < w;u++){ - fputc(' ',stderr); - } - - for(;y <= x;y++){ - unsigned char c = buf[y]; - bool sym = c < 0x20 || c > 0x7E; - if(sym) fputc('.',stderr); - else fputc(c,stderr); - } - - fputc('\n',stderr); - z = 0; - y = x+1; - } - } - fflush(stderr); -} - bool port_valid(int64_t port){ return port >= PORT_MIN && port <= PORT_MAX; } @@ -106,7 +67,7 @@ uint16_t rngolade[n]; assert(n <= 65536); - if(!rng_read(rngolade,sizeof(*rngolade)*n)) return 0; + if(!rng_read(rng_fast_fd,rngolade,sizeof(*rngolade)*n)) return 0; for(size_t x = 0;x < n;x++){ i[x] = x; diff -uNr a/src/util.h b/src/util.h --- a/src/util.h 1dda9f5412c56df257ba045ce216b1fb362d3e4c0646e0738f358462707e7e0f703c494411a9f06e35a172941ddd0d18fb40b863e46edb147afdcb1f7dae7c02 +++ b/src/util.h 935784b2b1bd0bae964aba8e64ca79437b498f0bb5407f33259ce8179126277d881cc4140c54a6a38cc0e372c526b477d899420eb96c516117c7322c65df7b6a @@ -7,8 +7,6 @@ #include #include -void print_hex (uint8_t const *buf,size_t len); //TMP - bool port_valid (int64_t port); bool check_port (int64_t port,char const *proto); diff -uNr a/src/wipe.c b/src/wipe.c --- a/src/wipe.c f90c578be01219e31e63895b4e254a041a6321d7566c6d321ffcdc2087b3fb20b89c1fc565f3c08fcc057d42c8676aa2cc0f9653b09cbcd51103c04033def86c +++ b/src/wipe.c f1f8a1e2b568db2bc88e510e970aca58f30cc95ac1f4b36c188b21f30df133c2418d045db7c25fd66212b1bbfdfe9ec97e7912f0e4817d9f98b084ea05713386 @@ -9,14 +9,15 @@ #include void state_zero(void){ - BUF_ZERO(data_path); + //BUF_ZERO(data_path); BUF_ZERO(self_handle); VAR_ZERO(running); VAR_ZERO(scram); VAR_ZERO(mono_offset); - BUF_ZERO(log_path); + //BUF_ZERO(log_path); VAR_ZERO(log_fp); - //VAR_ZERO(rng_fd); + VAR_ZERO(rng_slow_fd); + //VAR_ZERO(rng_fast_fd); BUF_ZERO(irc_chan); BUF_ZERO(irc_host); @@ -26,8 +27,9 @@ VAR_ZERO(irc_nicked); VAR_ZERO(irc_joined); VAR_ZERO(irc_ready); + VAR_ZERO(irc_log_chan); - VAR_ZERO(udp_listen_fd); + VAR_ZERO(udp_fd); VAR_ZERO(udp_silent); VAR_ZERO(tcp_func); VAR_ZERO(tcp_listen_fd); @@ -39,8 +41,9 @@ VAR_ZERO(ircd_func); VAR_ZERO(ircd_src_ip); VAR_ZERO(ircd_src_port); + BUF_ZERO(ircd_buf); + VAR_ZERO(ircd_buf_p); - VAR_ZERO(pest_func); VAR_ZERO(con_stamp); VAR_ZERO(rekey_last); VAR_ZERO(ignore_next); @@ -108,7 +111,8 @@ VAR_RAND(mono_offset); BUF_RAND(log_path); VAR_RAND(log_fp); - //VAR_RAND(rng_fd); + VAR_RAND(rng_slow_fd); + //VAR_RAND(rng_fast_fd); BUF_RAND(irc_chan); BUF_RAND(irc_host); @@ -118,8 +122,9 @@ VAR_RAND(irc_nicked); VAR_RAND(irc_joined); VAR_RAND(irc_ready); + VAR_RAND(irc_log_chan); - VAR_RAND(udp_listen_fd); + VAR_RAND(udp_fd); VAR_RAND(udp_silent); VAR_RAND(tcp_func); VAR_RAND(tcp_listen_fd); @@ -131,8 +136,9 @@ VAR_RAND(ircd_func); VAR_RAND(ircd_src_ip); VAR_RAND(ircd_src_port); + BUF_RAND(ircd_buf); + VAR_RAND(ircd_buf_p); - VAR_RAND(pest_func); VAR_RAND(con_stamp); VAR_RAND(rekey_last); VAR_RAND(ignore_next); @@ -215,137 +221,6 @@ } } -bool file_zero(char const *path){ - FILE *f; - struct stat sb; - size_t size; - uint8_t buf[4096]; - - if(stat(path,&sb) != 0){ - return 0; - } - size = (size_t)sb.st_size; - - f = fopen(path,"rb+"); - if(!f) return 0; - - BUF_ZERO(buf); - while(size > 0){ - size_t w_size = size; - if(w_size > sizeof(buf)){ - w_size = sizeof(buf); - } - - if(fwrite(buf,1,w_size,f) != w_size){ - fclose(f); - return 0; - } - - size -= w_size; - } - - if(fclose(f) != 0) return 0; - return 1; -} - -bool file_rand(char const *path){ - FILE *f; - struct stat sb; - size_t size; - uint8_t buf[4096]; - - if(stat(path,&sb) != 0){ - return 0; - } - size = (size_t)sb.st_size; - - f = fopen(path,"rb+"); - if(!f) return 0; - - while(size > 0){ - size_t w_size = size; - if(w_size > sizeof(buf)){ - w_size = sizeof(buf); - } - - if(!rng_read(buf,w_size)){ - fclose(f); - return 0; - } - - if(fwrite(buf,1,w_size,f) != w_size){ - fclose(f); - return 0; - } - - size -= w_size; - } - - if(fclose(f) != 0) return 0; - return 1; -} - -static int ftw_zero(char const *path, - struct stat const *sb, - int flag, - struct FTW *ftw){ - - (void)sb; - (void)ftw; - if(flag == FTW_F){ - file_zero(path); - } - return 0; -} - -static int ftw_rand(char const *path, - struct stat const *sb, - int flag, - struct FTW *ftw){ - - (void)sb; - (void)ftw; - if(flag == FTW_F){ - file_rand(path); - } - return 0; -} - -static bool dir_wipe(char const *path,bool rand){ - return nftw(path, - rand ? ftw_rand : ftw_zero, - 64, - FTW_PHYS) == 0; -} - -bool dir_zero(char const *path){ - return dir_wipe(path,0); -} - -bool dir_rand(char const *path){ - return dir_wipe(path,1); -} - -bool rm_nolog(char const *path){ - return remove(path) == 0; -} - -static int ftw_rm_rf(char const *path, - struct stat const *sb, - int flag, - struct FTW *ftw){ - - (void)sb; - (void)flag; - (void)ftw; - rm_nolog(path); - return 0; -} - -bool rm_rf_nolog(char const *path){ - return nftw(path,ftw_rm_rf,64,FTW_DEPTH | FTW_PHYS) == 0; -} - void scram_push(void){ running = 0; scram = 1; diff -uNr a/src/wipe.h b/src/wipe.h --- a/src/wipe.h 1c6240e818e90a8a027dcd7e7546df714b95a766b3a925456e8812391f4db35da202a25a2e11f52324768b4f918b15d67db42a1e420d78113a368b2960058fe8 +++ b/src/wipe.h b58c912d60b9f8aca4dfd11d03b04272f3ea6859c54236398fdec0246ddb3b7eaf2873c29793f1a692aabe34990292bcd0d8573e66fee62a4a8c1bcf760c7415 @@ -10,13 +10,6 @@ void stack_zero (void); void stack_rand (void); -bool file_zero (char const *path); -bool file_rand (char const *path); -bool dir_zero (char const *path); -bool dir_rand (char const *path); -bool rm_nolog (char const *path); -bool rm_rf_nolog (char const *path); - void scram_push (void); #endif