From ab4275e66463b72074c0848b87a7fd3b7e0df9b4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 20 Jan 2016 00:36:57 -0300 Subject: [PATCH] test --- deprecated/obsolete.h | 194 +++++++++++++++++++++++++++++++++++++ iguana/SuperNET.c | 220 +++--------------------------------------- 2 files changed, 209 insertions(+), 205 deletions(-) diff --git a/deprecated/obsolete.h b/deprecated/obsolete.h index 00d17ff6e..361f51ba7 100644 --- a/deprecated/obsolete.h +++ b/deprecated/obsolete.h @@ -12108,5 +12108,199 @@ void iguana_dedicatedrecv(void *arg) } else printf("cant decode apinum.%d (%d.%d)\n",apinum,apinum>>5,apinum%0x1f); return(0); } + +#ifdef notyet + + int32_t SuperNET_serialize(int32_t reverse,bits256 *senderpubp,uint64_t *senderbitsp,bits256 *sigp,uint32_t *timestampp,uint64_t *destbitsp,uint8_t *origbuf) + { + uint8_t *buf = origbuf; long extra = sizeof(bits256) + sizeof(uint64_t) + sizeof(uint64_t); + buf += SuperNET_copybits(reverse,buf,(void *)destbitsp,sizeof(uint64_t)); + buf += SuperNET_copybits(reverse,buf,senderpubp->bytes,sizeof(bits256)); + buf += SuperNET_copybits(reverse,buf,(void *)senderbitsp,sizeof(uint64_t)); + buf += SuperNET_copybits(reverse,buf,(void *)timestampp,sizeof(uint32_t)), extra += sizeof(uint32_t); + if ( *senderbitsp != 0 ) + buf += SuperNET_copybits(reverse,buf,sigp->bytes,sizeof(bits256)), extra += sizeof(bits256); + else memset(sigp,0,sizeof(*sigp)); + if ( ((long)buf - (long)origbuf) != extra ) + { + printf("SuperNET_serialize: extrasize mismatch %ld vs %ld\n",((long)buf - (long)origbuf),extra); + } + return((int32_t)extra); + } + + int32_t SuperNET_decode(uint64_t *senderbitsp,bits256 *sigp,uint32_t *timestampp,uint64_t *destbitsp,uint8_t *str,uint8_t *cipher,int32_t *lenp,uint8_t *myprivkey) + { + bits256 srcpubkey; uint8_t *nonce; int i,hdrlen,err=0,len = *lenp; + hdrlen = SuperNET_serialize(1,&srcpubkey,senderbitsp,sigp,timestampp,destbitsp,cipher); + cipher += hdrlen, len -= hdrlen; + if ( *destbitsp != 0 && *senderbitsp != 0 ) + { + nonce = cipher; + cipher += crypto_box_NONCEBYTES, len -= crypto_box_NONCEBYTES; + printf("decode ptr.%p[%d]\n",cipher,len); + err = crypto_box_open((uint8_t *)str,cipher,len,nonce,srcpubkey.bytes,myprivkey); + for (i=0; i %d %d\n",len,len+crypto_box_ZEROBYTES,len + crypto_box_ZEROBYTES + crypto_box_NONCEBYTES); + memset(cipher,0,len+crypto_box_ZEROBYTES); + memset(buf,0,crypto_box_ZEROBYTES); + memcpy(buf+crypto_box_ZEROBYTES,str,len); + printf("cryptobox.%p[%d]\n",cipher,len+crypto_box_ZEROBYTES); + crypto_box(cipher,buf,len+crypto_box_ZEROBYTES,nonce,destpubkey.bytes,myprivkey.bytes); + hdrlen += crypto_box_NONCEBYTES + crypto_box_ZEROBYTES; + } + else memcpy(&cipher[hdrlen],str,len); + if ( totalsize != len+hdrlen ) + printf("unexpected totalsize.%d != len.%d + hdrlen.%d %d\n",totalsize,len,hdrlen,len+hdrlen); + *cipherlenp = totalsize; + { + bits256 checksig; uint32_t checkstamp; uint64_t checksender,checkbits; int32_t checklen; + checklen = totalsize; + if ( SuperNET_decode(&checksender,&checksig,&checkstamp,&checkbits,(void *)buf,ptr,&checklen,myprivkey.bytes) == 0 ) + { + printf("decoded %u %llx checklen.%d\n",checkstamp,(long long)checkbits,checklen); + } else printf("encrypt/decrypt error\n"); + printf("decoded %u %llx checklen.%d\n",checkstamp,(long long)checkbits,checklen); + } + free(buf); + return(origcipher); + } + + int32_t SuperNET_decrypt(bits256 *senderpubp,uint64_t *senderbitsp,uint32_t *timestampp,bits256 mypriv,bits256 mypub,uint8_t *dest,int32_t maxlen,uint8_t *src,int32_t len) + { + bits256 seed,sig,msgpriv; uint64_t my64bits,destbits,senderbits,sendertmp,desttmp; + uint8_t *buf; int32_t hdrlen,diff,newlen = -1; HUFF H,*hp = &H; struct acct777_sig checksig; + *senderbitsp = 0; + my64bits = acct777_nxt64bits(mypub); + if ( (buf = calloc(1,maxlen)) == 0 ) + { + printf("SuperNET_decrypt cant allocate maxlen.%d\n",maxlen); + return(-1); + } + hdrlen = SuperNET_serialize(1,senderpubp,&senderbits,&sig,timestampp,&destbits,src); + if ( destbits != 0 && my64bits != destbits && destbits != acct777_nxt64bits(GENESIS_PUBKEY) ) + { + free(buf); + printf("SuperNET_decrypt received destination packet.%llu when my64bits.%llu len.%d\n",(long long)destbits,(long long)my64bits,len); + return(-1); + } + if ( memcmp(mypub.bytes,senderpubp->bytes,sizeof(mypub)) == 0 ) + { + if ( destbits != 0 ) + printf("SuperNET: got my own msg?\n"); + } + printf("decrypt(%d) destbits.%llu my64.%llu mypriv.%llx mypub.%llx senderpub.%llx shared.%llx\n",len,(long long)destbits,(long long)my64bits,(long long)mypriv.txid,(long long)mypub.txid,(long long)senderpubp->txid,(long long)seed.txid); + if ( SuperNET_decode(&sendertmp,&sig,timestampp,&desttmp,(void *)buf,src,&len,mypriv.bytes) == 0 ) + { + if ( (diff= (*timestampp - (uint32_t)time(NULL))) < 0 ) + diff = -diff; + if ( 1 && diff > SUPERNET_MAXTIMEDIFF ) + printf("diff.%d > %d %u vs %u\n",diff,SUPERNET_MAXTIMEDIFF,*timestampp,(uint32_t)time(NULL)); + else + { + if ( 0 ) + { + memset(seed.bytes,0,sizeof(seed)); + //for (i='0'; i<='9'; i++) + // SETBIT(seed.bytes,i); + //for (i='a'; i<='f'; i++) + // SETBIT(seed.bytes,i); + _init_HUFF(hp,len,buf), hp->endpos = (len << 3); + newlen = ramcoder_decoder(0,1,dest,maxlen,hp,&seed); + } + else memcpy(dest,buf,len), newlen = len; + //printf("T%d decrypted newlen.%d\n",threadid,newlen); + if ( senderbits != 0 && senderpubp->txid != 0 ) + { + *senderbitsp = senderbits; + if ( destbits == 0 ) + msgpriv = GENESIS_PRIVKEY; + else msgpriv = mypriv; + acct777_sign(&checksig,msgpriv,*senderpubp,*timestampp,dest,newlen); + if ( memcmp(checksig.sigbits.bytes,&sig,sizeof(checksig.sigbits)) != 0 ) + { + printf("sender.%llu sig %llx compare error vs %llx using sig->pub from %llu, broadcast.%d len.%d -> newlen.%d\n",(long long)senderbits,(long long)sig.txid,(long long)checksig.sigbits.txid,(long long)senderbits,destbits == 0,len,newlen); + //free(buf); + //return(0); + } //else printf("SIG VERIFIED newlen.%d (%llu -> %llu)\n",newlen,(long long)senderbits,(long long)destbits); + } + } + } else printf("%llu: SuperNET_decrypt skip: decode_cipher error len.%d -> newlen.%d\n",(long long)acct777_nxt64bits(mypub),len,newlen); + free(buf); + return(newlen); + } + + int32_t SuperNET_sendmsg(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr,bits256 destpub,bits256 mypriv,bits256 mypub,uint8_t *msg,int32_t len,uint8_t *data,int32_t delaymillis) + { + int32_t cipherlen,datalen,qlen=-1; bits256 seed; uint8_t *cipher; uint64_t destbits; struct acct777_sig sig; HUFF H,*hp = &H; + if ( destpub.txid != 0 ) + destbits = acct777_nxt64bits(destpub); + else + { + destbits = 0; + destpub = GENESIS_PUBKEY; + } + printf("SuperNET_sendmsg dest.%llu destpub.%llx priv.%llx pub.%llx\n",(long long)destbits,(long long)destpub.txid,(long long)mypriv.txid,(long long)mypub.txid); + memset(&sig,0,sizeof(sig)); + if ( mypub.txid == 0 || mypriv.txid == 0 ) + mypriv = curve25519_keypair(&mypub), sig.timestamp = (uint32_t)time(NULL); + else acct777_sign(&sig,mypriv,destpub,(uint32_t)time(NULL),msg,len); + if ( 0 ) + { + memset(seed.bytes,0,sizeof(seed)); + //seed = addr->sharedseed; + data = calloc(1,len*2); + _init_HUFF(hp,len*2,data); + /*for (i='0'; i<='9'; i++) + SETBIT(seed.bytes,i); + for (i='a'; i<='f'; i++) + SETBIT(seed.bytes,i);*/ + ramcoder_encoder(0,1,msg,len,hp,0,&seed); + datalen = (int32_t)hconv_bitlen(hp->bitoffset); + } + else data = msg, datalen = len; + if ( (cipher= SuperNET_encode(&cipherlen,data,datalen,destpub,mypriv,mypub,sig.signer64bits,sig.sigbits,sig.timestamp)) != 0 ) + { + qlen = iguana_queue_send(coin,addr,delaymillis,cipher,"SuperNETb",cipherlen,0,0); + free(cipher); + } + return(qlen); + } +#endif #endif diff --git a/iguana/SuperNET.c b/iguana/SuperNET.c index 823d3ea29..b573dbfb6 100644 --- a/iguana/SuperNET.c +++ b/iguana/SuperNET.c @@ -206,200 +206,6 @@ int32_t SuperNET_copybits(int32_t reverse,uint8_t *dest,uint8_t *src,int32_t len return(len >> 3); } -#ifdef notyet - -int32_t SuperNET_serialize(int32_t reverse,bits256 *senderpubp,uint64_t *senderbitsp,bits256 *sigp,uint32_t *timestampp,uint64_t *destbitsp,uint8_t *origbuf) -{ - uint8_t *buf = origbuf; long extra = sizeof(bits256) + sizeof(uint64_t) + sizeof(uint64_t); - buf += SuperNET_copybits(reverse,buf,(void *)destbitsp,sizeof(uint64_t)); - buf += SuperNET_copybits(reverse,buf,senderpubp->bytes,sizeof(bits256)); - buf += SuperNET_copybits(reverse,buf,(void *)senderbitsp,sizeof(uint64_t)); - buf += SuperNET_copybits(reverse,buf,(void *)timestampp,sizeof(uint32_t)), extra += sizeof(uint32_t); - if ( *senderbitsp != 0 ) - buf += SuperNET_copybits(reverse,buf,sigp->bytes,sizeof(bits256)), extra += sizeof(bits256); - else memset(sigp,0,sizeof(*sigp)); - if ( ((long)buf - (long)origbuf) != extra ) - { - printf("SuperNET_serialize: extrasize mismatch %ld vs %ld\n",((long)buf - (long)origbuf),extra); - } - return((int32_t)extra); -} - -int32_t SuperNET_decode(uint64_t *senderbitsp,bits256 *sigp,uint32_t *timestampp,uint64_t *destbitsp,uint8_t *str,uint8_t *cipher,int32_t *lenp,uint8_t *myprivkey) -{ - bits256 srcpubkey; uint8_t *nonce; int i,hdrlen,err=0,len = *lenp; - hdrlen = SuperNET_serialize(1,&srcpubkey,senderbitsp,sigp,timestampp,destbitsp,cipher); - cipher += hdrlen, len -= hdrlen; - if ( *destbitsp != 0 && *senderbitsp != 0 ) - { - nonce = cipher; - cipher += crypto_box_NONCEBYTES, len -= crypto_box_NONCEBYTES; - printf("decode ptr.%p[%d]\n",cipher,len); - err = crypto_box_open((uint8_t *)str,cipher,len,nonce,srcpubkey.bytes,myprivkey); - for (i=0; i %d %d\n",len,len+crypto_box_ZEROBYTES,len + crypto_box_ZEROBYTES + crypto_box_NONCEBYTES); - memset(cipher,0,len+crypto_box_ZEROBYTES); - memset(buf,0,crypto_box_ZEROBYTES); - memcpy(buf+crypto_box_ZEROBYTES,str,len); - printf("cryptobox.%p[%d]\n",cipher,len+crypto_box_ZEROBYTES); - crypto_box(cipher,buf,len+crypto_box_ZEROBYTES,nonce,destpubkey.bytes,myprivkey.bytes); - hdrlen += crypto_box_NONCEBYTES + crypto_box_ZEROBYTES; - } - else memcpy(&cipher[hdrlen],str,len); - if ( totalsize != len+hdrlen ) - printf("unexpected totalsize.%d != len.%d + hdrlen.%d %d\n",totalsize,len,hdrlen,len+hdrlen); - *cipherlenp = totalsize; - { - bits256 checksig; uint32_t checkstamp; uint64_t checksender,checkbits; int32_t checklen; - checklen = totalsize; - if ( SuperNET_decode(&checksender,&checksig,&checkstamp,&checkbits,(void *)buf,ptr,&checklen,myprivkey.bytes) == 0 ) - { - printf("decoded %u %llx checklen.%d\n",checkstamp,(long long)checkbits,checklen); - } else printf("encrypt/decrypt error\n"); - printf("decoded %u %llx checklen.%d\n",checkstamp,(long long)checkbits,checklen); - } - free(buf); - return(origcipher); -} - -int32_t SuperNET_decrypt(bits256 *senderpubp,uint64_t *senderbitsp,uint32_t *timestampp,bits256 mypriv,bits256 mypub,uint8_t *dest,int32_t maxlen,uint8_t *src,int32_t len) -{ - bits256 seed,sig,msgpriv; uint64_t my64bits,destbits,senderbits,sendertmp,desttmp; - uint8_t *buf; int32_t hdrlen,diff,newlen = -1; HUFF H,*hp = &H; struct acct777_sig checksig; - *senderbitsp = 0; - my64bits = acct777_nxt64bits(mypub); - if ( (buf = calloc(1,maxlen)) == 0 ) - { - printf("SuperNET_decrypt cant allocate maxlen.%d\n",maxlen); - return(-1); - } - hdrlen = SuperNET_serialize(1,senderpubp,&senderbits,&sig,timestampp,&destbits,src); - if ( destbits != 0 && my64bits != destbits && destbits != acct777_nxt64bits(GENESIS_PUBKEY) ) - { - free(buf); - printf("SuperNET_decrypt received destination packet.%llu when my64bits.%llu len.%d\n",(long long)destbits,(long long)my64bits,len); - return(-1); - } - if ( memcmp(mypub.bytes,senderpubp->bytes,sizeof(mypub)) == 0 ) - { - if ( destbits != 0 ) - printf("SuperNET: got my own msg?\n"); - } - printf("decrypt(%d) destbits.%llu my64.%llu mypriv.%llx mypub.%llx senderpub.%llx shared.%llx\n",len,(long long)destbits,(long long)my64bits,(long long)mypriv.txid,(long long)mypub.txid,(long long)senderpubp->txid,(long long)seed.txid); - if ( SuperNET_decode(&sendertmp,&sig,timestampp,&desttmp,(void *)buf,src,&len,mypriv.bytes) == 0 ) - { - if ( (diff= (*timestampp - (uint32_t)time(NULL))) < 0 ) - diff = -diff; - if ( 1 && diff > SUPERNET_MAXTIMEDIFF ) - printf("diff.%d > %d %u vs %u\n",diff,SUPERNET_MAXTIMEDIFF,*timestampp,(uint32_t)time(NULL)); - else - { - if ( 0 ) - { - memset(seed.bytes,0,sizeof(seed)); - //for (i='0'; i<='9'; i++) - // SETBIT(seed.bytes,i); - //for (i='a'; i<='f'; i++) - // SETBIT(seed.bytes,i); - _init_HUFF(hp,len,buf), hp->endpos = (len << 3); - newlen = ramcoder_decoder(0,1,dest,maxlen,hp,&seed); - } - else memcpy(dest,buf,len), newlen = len; - //printf("T%d decrypted newlen.%d\n",threadid,newlen); - if ( senderbits != 0 && senderpubp->txid != 0 ) - { - *senderbitsp = senderbits; - if ( destbits == 0 ) - msgpriv = GENESIS_PRIVKEY; - else msgpriv = mypriv; - acct777_sign(&checksig,msgpriv,*senderpubp,*timestampp,dest,newlen); - if ( memcmp(checksig.sigbits.bytes,&sig,sizeof(checksig.sigbits)) != 0 ) - { - printf("sender.%llu sig %llx compare error vs %llx using sig->pub from %llu, broadcast.%d len.%d -> newlen.%d\n",(long long)senderbits,(long long)sig.txid,(long long)checksig.sigbits.txid,(long long)senderbits,destbits == 0,len,newlen); - //free(buf); - //return(0); - } //else printf("SIG VERIFIED newlen.%d (%llu -> %llu)\n",newlen,(long long)senderbits,(long long)destbits); - } - } - } else printf("%llu: SuperNET_decrypt skip: decode_cipher error len.%d -> newlen.%d\n",(long long)acct777_nxt64bits(mypub),len,newlen); - free(buf); - return(newlen); -} - -int32_t SuperNET_sendmsg(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr,bits256 destpub,bits256 mypriv,bits256 mypub,uint8_t *msg,int32_t len,uint8_t *data,int32_t delaymillis) -{ - int32_t cipherlen,datalen,qlen=-1; bits256 seed; uint8_t *cipher; uint64_t destbits; struct acct777_sig sig; HUFF H,*hp = &H; - if ( destpub.txid != 0 ) - destbits = acct777_nxt64bits(destpub); - else - { - destbits = 0; - destpub = GENESIS_PUBKEY; - } - printf("SuperNET_sendmsg dest.%llu destpub.%llx priv.%llx pub.%llx\n",(long long)destbits,(long long)destpub.txid,(long long)mypriv.txid,(long long)mypub.txid); - memset(&sig,0,sizeof(sig)); - if ( mypub.txid == 0 || mypriv.txid == 0 ) - mypriv = curve25519_keypair(&mypub), sig.timestamp = (uint32_t)time(NULL); - else acct777_sign(&sig,mypriv,destpub,(uint32_t)time(NULL),msg,len); - if ( 0 ) - { - memset(seed.bytes,0,sizeof(seed)); - //seed = addr->sharedseed; - data = calloc(1,len*2); - _init_HUFF(hp,len*2,data); - /*for (i='0'; i<='9'; i++) - SETBIT(seed.bytes,i); - for (i='a'; i<='f'; i++) - SETBIT(seed.bytes,i);*/ - ramcoder_encoder(0,1,msg,len,hp,0,&seed); - datalen = (int32_t)hconv_bitlen(hp->bitoffset); - } - else data = msg, datalen = len; - if ( (cipher= SuperNET_encode(&cipherlen,data,datalen,destpub,mypriv,mypub,sig.signer64bits,sig.sigbits,sig.timestamp)) != 0 ) - { - qlen = iguana_queue_send(coin,addr,delaymillis,cipher,"SuperNETb",cipherlen,0,0); - free(cipher); - } - return(qlen); -} -#endif - uint16_t SuperNET_checkc(bits256 privkey,bits256 otherpub,uint64_t tag) { uint8_t buf[40]; bits256 check,seed,seed2; @@ -521,20 +327,23 @@ int32_t iguana_send_supernet(struct iguana_info *coin,struct iguana_peer *addr,c { serialized = malloc(sizeof(struct iguana_msghdr) + IGUANA_MAXPACKETSIZE); destpub = addr->pubkey; - datalen = SuperNET_json2bits(myinfo->ipaddr,myinfo->persistent_priv,myinfo->myaddr.persistent,&serialized[sizeof(struct iguana_msghdr)],IGUANA_MAXPACKETSIZE,addr->ipaddr,json); - printf("SUPERSEND.(%s) -> (%s) delaymillis.%d datalen.%d\n",jsonstr,addr->ipaddr,delaymillis,datalen); - if ( memcmp(destpub.bytes,GENESIS_PUBKEY.bytes,sizeof(destpub)) == 0 ) - qlen = iguana_queue_send(coin,addr,delaymillis,serialized,"SuperNET",datalen,0,0); - else + if ( (datalen= SuperNET_json2bits(myinfo->ipaddr,myinfo->persistent_priv,myinfo->myaddr.persistent,&serialized[sizeof(struct iguana_msghdr)],IGUANA_MAXPACKETSIZE,addr->ipaddr,json)) > 0 ) { - privkey = myinfo->privkey; - if ( (cipher= SuperNET_ciphercalc(&cipherlen,&privkey,&destpub,serialized,datalen,space2,sizeof(space2))) != 0 ) + printf("SUPERSEND.(%s) -> (%s) delaymillis.%d datalen.%d\n",jsonstr,addr->ipaddr,delaymillis,datalen); + if ( memcmp(destpub.bytes,GENESIS_PUBKEY.bytes,sizeof(destpub)) == 0 ) + qlen = iguana_queue_send(coin,addr,delaymillis,serialized,"SuperNET",datalen,0,0); + else { - qlen = iguana_queue_send(coin,addr,delaymillis,cipher,"SuperNETb",cipherlen,0,0); - if ( cipher != space2 ) - free(cipher); + printf("ENCRYPT\n"); + privkey = myinfo->privkey; + if ( (cipher= SuperNET_ciphercalc(&cipherlen,&privkey,&destpub,serialized,datalen,space2,sizeof(space2))) != 0 ) + { + qlen = iguana_queue_send(coin,addr,delaymillis,cipher,"SuperNETb",cipherlen,0,0); + if ( cipher != space2 ) + free(cipher); + } + //qlen = SuperNET_sendmsg(myinfo,coin,addr,addr->pubkey,myinfo->privkey,myinfo->myaddr.pubkey,serialized,datalen,space,delaymillis); } - //qlen = SuperNET_sendmsg(myinfo,coin,addr,addr->pubkey,myinfo->privkey,myinfo->myaddr.pubkey,serialized,datalen,space,delaymillis); } free(serialized); } else printf("cant parse.(%s)\n",jsonstr); @@ -730,6 +539,7 @@ char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *de *delaymillisp = 0; if ( compressed != 0 ) { + printf("DECRYPT\n"); if ( (msgbits= SuperNET_deciphercalc(&ptr,&msglen,myinfo->privkey,myinfo->myaddr.pubkey,data,datalen,space,sizeof(space))) == 0 ) return(clonestr("{\"error\":\"couldnt decrypt p2p packet\"}")); } else msgbits = data;