diff --git a/iguana/SuperNET.c b/iguana/SuperNET.c index d1230eb30..e6785243b 100644 --- a/iguana/SuperNET.c +++ b/iguana/SuperNET.c @@ -16,6 +16,19 @@ #include "iguana777.h" #include "../includes/tweetnacl.h" +bits256 testprivkey(int32_t selector) +{ + bits256 privkey; + memset(privkey.bytes,0,sizeof(privkey.bytes)); + privkey.bytes[15] = selector; + return(privkey); +} + +bits256 testpubkey(int32_t selector) +{ + return(acct777_pubkey(testprivkey(selector))); +} + bits256 SuperNET_sharedseed(bits256 privkey,bits256 otherpub) { bits256 seed2,seed; @@ -154,10 +167,10 @@ uint8_t *SuperNET_ciphercalc(void **ptrp,int32_t *cipherlenp,bits256 *privkeyp,b bits256 mypubkey; uint8_t *buf,*nonce,*cipher,*origptr,space[8192]; int32_t onetimeflag=0,allocsize; *ptrp = 0; allocsize = (datalen + crypto_box_NONCEBYTES + crypto_box_ZEROBYTES); - if ( bits256_nonz(*destpubkeyp) == 0 ) + if ( bits256_nonz(*destpubkeyp) == 0 || memcmp(destpubkeyp->bytes,GENESIS_PUBKEY.bytes,sizeof(*destpubkeyp)) == 0 ) { *destpubkeyp = GENESIS_PUBKEY; - onetimeflag = 2; // prevent any possible leakage of privkey by using known destpub + onetimeflag = 2; // prevent any possible leakage of privkey by encrypting to known destpub } if ( bits256_nonz(*privkeyp) == 0 ) onetimeflag = 1; @@ -307,19 +320,51 @@ cJSON *SuperNET_bits2json(struct iguana_peer *addr,uint8_t *serialized,int32_t d return(0); } +bits256 iguana_setkeys(struct supernet_info *myinfo,struct iguana_peer *addr,bits256 *myprivp,bits256 *mypubp,bits256 *destpubp,bits256 *nextprivp,bits256 *nextpubp,bits256 *nextdestpubp) +{ + bits256 testpriv; +#ifdef __APPLE__ + *nextprivp = testprivkey(0); // myinfo->privkey; + *nextpubp = testpubkey(0); // myinfo->myaddr.pubkey; + testpriv = testprivkey(1); + *nextdestpubp = testpubkey(1); // addr->pubkey; +#else + *nextprivp = testprivkey(1); + *nextpubp = testpubkey(1); + testpriv = testprivkey(0); + *nextdestpubp = testpubkey(0); +#endif + if ( addr->validpub < 3 || addr->othervalid < 3 ) + *myprivp = testpriv = GENESIS_PRIVKEY, *destpubp = *mypubp = GENESIS_PUBKEY; + else *myprivp = *nextprivp, *mypubp = *nextpubp, *destpubp = *nextdestpubp; + char str[65]; printf("(priv.%llx pub.%llx) -> destpub.%s\n",(long long)myprivp->txid,(long long)mypubp->txid,bits256_str(str,*destpubp)); + return(testpriv); +} + +bits256 iguana_actualpubkey(uint8_t *cipher,int32_t cipherlen,bits256 destpubkey) +{ + int32_t i; + if ( cipherlen < 56+16 ) + return(destpubkey); + for (i=56; i<56+16; i++) + if ( cipher[i] != 0 ) + break; + if ( i == 56+16 ) + memcpy(destpubkey.bytes,cipher,sizeof(destpubkey)); + return(destpubkey); +} + int32_t iguana_send_supernet(struct iguana_info *coin,struct iguana_peer *addr,char *jsonstr,int32_t delaymillis) { int32_t datalen,cipherlen,qlen = -1; uint8_t *serialized,space2[8192],*cipher; cJSON *json; - struct supernet_info *myinfo; bits256 destpub,privkey; void *ptr = 0; + struct supernet_info *myinfo; bits256 destpub,privkey,pubkey,nextprivkey,nextpubkey,nextdestpub; void *ptr = 0; myinfo = SuperNET_MYINFO(0); if ( (json= cJSON_Parse(jsonstr)) != 0 ) { + bits256 testpriv; + testpriv = iguana_setkeys(myinfo,addr,&privkey,&pubkey,&destpub,&nextprivkey,&nextpubkey,&nextdestpub); serialized = malloc(sizeof(struct iguana_msghdr) + IGUANA_MAXPACKETSIZE); - privkey = myinfo->privkey; - //if ( addr->validpub > 1 && addr->othervalid > 3 ) - destpub = addr->pubkey; - //else privkey = GENESIS_PRIVKEY, destpub = GENESIS_PUBKEY; - if ( (datalen= SuperNET_json2bits(myinfo->ipaddr,myinfo->privkey,myinfo->myaddr.pubkey,&serialized[sizeof(struct iguana_msghdr)],IGUANA_MAXPACKETSIZE,addr->ipaddr,json,addr->pubkey,addr->validpub)) > 0 ) + if ( (datalen= SuperNET_json2bits(myinfo->ipaddr,nextprivkey,nextpubkey,&serialized[sizeof(struct iguana_msghdr)],IGUANA_MAXPACKETSIZE,addr->ipaddr,json,nextdestpub,addr->validpub)) > 0 ) { printf("SUPERSEND.(%s) -> (%s) delaymillis.%d datalen.%d\n",jsonstr,addr->ipaddr,delaymillis,datalen); if ( 0 && memcmp(destpub.bytes,GENESIS_PUBKEY.bytes,sizeof(destpub)) == 0 ) @@ -329,12 +374,17 @@ int32_t iguana_send_supernet(struct iguana_info *coin,struct iguana_peer *addr,c //int32_t i; for (i=0; ivalidpub > 3 && addr->othervalid > 3 ) - { - privkey = myinfo->privkey; - memcpy(senderpub.bytes,data,sizeof(senderpub));// = addr->pubkey; - printf("decrypt with priv.%llx pub.%llx\n",(long long)privkey.txid,(long long)senderpub.txid); - }// else privkey = GENESIS_PRIVKEY, senderpub = GENESIS_PUBKEY; - if ( (msgbits= SuperNET_deciphercalc(&ptr,&msglen,privkey,senderpub,data,datalen,space,sizeof(space))) == 0 ) + iguana_setkeys(myinfo,addr,&privkey,&pubkey,&senderpub,&nextprivkey,&nextpubkey,&nextdestpub); + if ( (msgbits= SuperNET_deciphercalc(&ptr,&msglen,privkey,iguana_actualpubkey(data,datalen,senderpub),data,datalen,space,sizeof(space))) == 0 ) { int32_t i; for (i=0; ipubkey.bytes,0,sizeof(addr->pubkey)); - addr->validpub = addr->othervalid = 0; + addr->validpub = 0; return(clonestr("{\"error\":\"couldnt decrypt p2p packet\"}")); } //for (i=0; ivalidpub++; else if ( addr->validpub > 0 ) - addr->validpub = 0; + addr->validpub >>= 1; else addr->validpub--; printf("validpub.%d: %x vs %x shared.%llx\n",addr->validpub,checkc,othercheckc,(long long)addr->sharedseed.txid); } @@ -593,7 +637,7 @@ char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *de retstr = SuperNET_JSON(myinfo,json,ipaddr); //printf("p2pret.(%s)\n",retstr); *delaymillisp = SuperNET_delaymillis(myinfo,maxdelay); - //senderpub = jbits256(json,"mypub"); + senderpub = jbits256(json,"mypub"); //if ( memcmp(senderpub.bytes,addr->pubkey.bytes,sizeof(senderpub)) != 0 ) // addr->pubkey = senderpub; addr->othervalid = (int32_t)jdouble(json,"ov");