|
|
@ -97,6 +97,86 @@ void SuperNET_myipaddr(struct supernet_info *myinfo,struct iguana_info *coin,str |
|
|
|
//printf("myipaddr.%s self.%x your.%x\n",myinfo->ipaddr,myinfo->myaddr.selfipbits,myinfo->myaddr.myipbits);
|
|
|
|
} |
|
|
|
|
|
|
|
int32_t _SuperNET_cipher(uint8_t nonce[crypto_box_NONCEBYTES],uint8_t *cipher,uint8_t *message,int32_t len,bits256 destpub,bits256 srcpriv,uint8_t *buf) |
|
|
|
{ |
|
|
|
memset(cipher,0,len+crypto_box_ZEROBYTES); |
|
|
|
memset(buf,0,crypto_box_ZEROBYTES); |
|
|
|
memcpy(buf+crypto_box_ZEROBYTES,message,len); |
|
|
|
crypto_box(cipher,buf,len+crypto_box_ZEROBYTES,nonce,destpub.bytes,srcpriv.bytes); |
|
|
|
return(len + crypto_box_ZEROBYTES); |
|
|
|
} |
|
|
|
|
|
|
|
uint8_t *_SuperNET_decipher(uint8_t nonce[crypto_box_NONCEBYTES],uint8_t *cipher,uint8_t *message,int32_t len,bits256 srcpub,bits256 mypriv) |
|
|
|
{ |
|
|
|
int32_t err; |
|
|
|
if ( (err= crypto_box_open(message,cipher,len,nonce,srcpub.bytes,mypriv.bytes)) == 0 ) |
|
|
|
{ |
|
|
|
message += crypto_box_ZEROBYTES; |
|
|
|
len -= crypto_box_ZEROBYTES; |
|
|
|
return(message); |
|
|
|
} |
|
|
|
return(0); |
|
|
|
} |
|
|
|
|
|
|
|
void *SuperNET_deciphercalc(int32_t *msglenp,bits256 privkey,bits256 srcpubkey,uint8_t *cipher,int32_t cipherlen,uint8_t *buf,int32_t bufsize) |
|
|
|
{ |
|
|
|
uint8_t *origptr,*nonce,*message; |
|
|
|
if ( bits256_nonz(privkey) == 0 ) |
|
|
|
privkey = GENESIS_PRIVKEY; |
|
|
|
if ( cipherlen > bufsize ) |
|
|
|
message = calloc(1,cipherlen); |
|
|
|
else message = buf; |
|
|
|
origptr = cipher; |
|
|
|
if ( bits256_nonz(srcpubkey) == 0 ) |
|
|
|
{ |
|
|
|
memcpy(srcpubkey.bytes,cipher,sizeof(srcpubkey)); |
|
|
|
//char str[65]; printf("use attached pubkey.(%s)\n",bits256_str(str,srcpubkey));
|
|
|
|
cipher += sizeof(srcpubkey); |
|
|
|
cipherlen -= sizeof(srcpubkey); |
|
|
|
} |
|
|
|
nonce = cipher; |
|
|
|
cipher += crypto_box_NONCEBYTES, cipherlen -= crypto_box_NONCEBYTES; |
|
|
|
*msglenp = cipherlen - crypto_box_ZEROBYTES; |
|
|
|
return(_SuperNET_decipher(nonce,cipher,message,cipherlen,srcpubkey,privkey)); |
|
|
|
} |
|
|
|
|
|
|
|
uint8_t *SuperNET_ciphercalc(int32_t *cipherlenp,bits256 *privkeyp,bits256 *destpubkeyp,uint8_t *data,int32_t datalen,uint8_t *space2,int32_t space2size) |
|
|
|
{ |
|
|
|
bits256 mypubkey; uint8_t *buf,*nonce,*cipher,*origptr,space[8192]; int32_t onetimeflag=0,allocsize; |
|
|
|
allocsize = (datalen + crypto_box_NONCEBYTES + crypto_box_ZEROBYTES); |
|
|
|
if ( bits256_nonz(*destpubkeyp) == 0 ) |
|
|
|
{ |
|
|
|
*destpubkeyp = GENESIS_PUBKEY; |
|
|
|
onetimeflag = 2; // prevent any possible leakage of privkey by using known destpub
|
|
|
|
} |
|
|
|
if ( bits256_nonz(*privkeyp) == 0 ) |
|
|
|
onetimeflag = 1; |
|
|
|
if ( onetimeflag != 0 ) |
|
|
|
{ |
|
|
|
crypto_box_keypair(mypubkey.bytes,privkeyp->bytes); |
|
|
|
allocsize += sizeof(bits256); |
|
|
|
} |
|
|
|
if ( allocsize > sizeof(space) ) |
|
|
|
buf = calloc(1,allocsize); |
|
|
|
else buf = space; |
|
|
|
if ( allocsize > space2size ) |
|
|
|
cipher = calloc(1,allocsize); |
|
|
|
else cipher = space2; |
|
|
|
origptr = nonce = cipher; |
|
|
|
if ( onetimeflag != 0 ) |
|
|
|
{ |
|
|
|
memcpy(cipher,mypubkey.bytes,sizeof(mypubkey)); |
|
|
|
nonce = &cipher[sizeof(mypubkey)]; |
|
|
|
} |
|
|
|
OS_randombytes(nonce,crypto_box_NONCEBYTES); |
|
|
|
cipher = &nonce[crypto_box_NONCEBYTES]; |
|
|
|
_SuperNET_cipher(nonce,cipher,(void *)data,datalen,*destpubkeyp,*privkeyp,buf); |
|
|
|
if ( buf != space ) |
|
|
|
free(buf); |
|
|
|
*cipherlenp = allocsize; |
|
|
|
return(origptr); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t SuperNET_copybits(int32_t reverse,uint8_t *dest,uint8_t *src,int32_t len) |
|
|
|
{ |
|
|
|
int32_t i; uint8_t *tmp; |
|
|
@ -117,6 +197,8 @@ 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); |
|
|
@ -307,6 +389,7 @@ int32_t SuperNET_sendmsg(struct supernet_info *myinfo,struct iguana_info *coin,s |
|
|
|
} |
|
|
|
return(qlen); |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
uint16_t SuperNET_checkc(bits256 privkey,bits256 otherpub,uint64_t tag) |
|
|
|
{ |
|
|
@ -319,9 +402,9 @@ uint16_t SuperNET_checkc(bits256 privkey,bits256 otherpub,uint64_t tag) |
|
|
|
return(check.ushorts[0]); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t SuperNET_json2bits(char *myipaddr,bits256 persistent_priv,bits256 persistent_pub,uint8_t *serialized,int32_t maxsize,char *destip,bits256 destpub,cJSON *json) |
|
|
|
int32_t SuperNET_json2bits(char *myipaddr,bits256 persistent_priv,bits256 persistent_pub,uint8_t *serialized,int32_t maxsize,char *destip,cJSON *json) |
|
|
|
{ |
|
|
|
uint16_t apinum,checkc=0; uint32_t ipbits,crc; char *agent,*method; uint64_t tag; char *hexmsg; |
|
|
|
uint16_t apinum; uint32_t ipbits,crc; char *agent,*method; uint64_t tag; char *hexmsg; |
|
|
|
int32_t n,len = sizeof(uint32_t); |
|
|
|
if ( (tag= j64bits(json,"tag")) == 0 ) |
|
|
|
OS_randombytes((uint8_t *)&tag,sizeof(tag)); |
|
|
@ -331,8 +414,8 @@ int32_t SuperNET_json2bits(char *myipaddr,bits256 persistent_priv,bits256 persis |
|
|
|
len += iguana_rwnum(1,&serialized[len],sizeof(uint32_t),&ipbits); |
|
|
|
len += iguana_rwbignum(1,&serialized[len],sizeof(persistent_pub),persistent_pub.bytes); |
|
|
|
len += iguana_rwnum(1,&serialized[len],sizeof(tag),&tag); |
|
|
|
checkc = SuperNET_checkc(persistent_priv,destpub,tag); |
|
|
|
len += iguana_rwnum(1,&serialized[len],sizeof(checkc),&checkc); |
|
|
|
//checkc = SuperNET_checkc(persistent_priv,destpub,tag);
|
|
|
|
//len += iguana_rwnum(1,&serialized[len],sizeof(checkc),&checkc);
|
|
|
|
agent = jstr(json,"agent"), method = jstr(json,"method"); |
|
|
|
if ( agent != 0 && method != 0 && strcmp(agent,"SuperNET") == 0 && strcmp(method,"json2bits") == 0 ) |
|
|
|
{ |
|
|
@ -363,12 +446,12 @@ int32_t SuperNET_json2bits(char *myipaddr,bits256 persistent_priv,bits256 persis |
|
|
|
return(len); |
|
|
|
} |
|
|
|
|
|
|
|
cJSON *SuperNET_bits2json(bits256 mypriv,bits256 mypub,struct iguana_peer *addr,uint8_t *serialized,uint8_t *space,int32_t datalen,int32_t iscompressed) |
|
|
|
cJSON *SuperNET_bits2json(bits256 mypriv,struct iguana_peer *addr,uint8_t *serialized,int32_t datalen) |
|
|
|
{ |
|
|
|
char destip[64],method[64],checkstr[5],agent[64],myipaddr[64],str[65],*hexmsg; uint64_t tag; |
|
|
|
uint16_t apinum,checkc; uint32_t destipbits,myipbits,timestamp; bits256 senderpub; uint64_t senderbits; |
|
|
|
uint16_t apinum,checkc; uint32_t destipbits,myipbits; bits256 senderpub; |
|
|
|
int32_t len = 0; uint32_t crc; cJSON *json = cJSON_CreateObject(); |
|
|
|
memset(senderpub.bytes,0,sizeof(senderpub)); |
|
|
|
/*memset(senderpub.bytes,0,sizeof(senderpub));
|
|
|
|
if ( iscompressed != 0 ) |
|
|
|
{ |
|
|
|
if ( (len= SuperNET_decrypt(&senderpub,&senderbits,×tamp,mypriv,mypub,space,IGUANA_MAXPACKETSIZE,serialized,datalen)) > 1 && len < IGUANA_MAXPACKETSIZE ) |
|
|
@ -383,13 +466,13 @@ cJSON *SuperNET_bits2json(bits256 mypriv,bits256 mypub,struct iguana_peer *addr, |
|
|
|
datalen = len; |
|
|
|
len = 0; |
|
|
|
} else printf("decrypt error len.%d origlen.%d\n",len,datalen); |
|
|
|
} |
|
|
|
}*/ |
|
|
|
len += iguana_rwnum(0,&serialized[len],sizeof(uint32_t),&crc); |
|
|
|
len += iguana_rwnum(0,&serialized[len],sizeof(uint32_t),&destipbits); |
|
|
|
len += iguana_rwnum(0,&serialized[len],sizeof(uint32_t),&myipbits); |
|
|
|
len += iguana_rwbignum(0,&serialized[len],sizeof(bits256),senderpub.bytes); |
|
|
|
len += iguana_rwnum(0,&serialized[len],sizeof(tag),&tag); |
|
|
|
len += iguana_rwnum(0,&serialized[len],sizeof(checkc),&checkc); |
|
|
|
//len += iguana_rwnum(0,&serialized[len],sizeof(checkc),&checkc);
|
|
|
|
len += iguana_rwnum(0,&serialized[len],sizeof(apinum),&apinum); |
|
|
|
//printf("-> dest.%x myip.%x senderpub.%llx tag.%llu\n",destipbits,myipbits,(long long)senderpub.txid,(long long)tag);
|
|
|
|
//int32_t i; for (i=0; i<len; i++)
|
|
|
@ -422,19 +505,29 @@ cJSON *SuperNET_bits2json(bits256 mypriv,bits256 mypub,struct iguana_peer *addr, |
|
|
|
|
|
|
|
int32_t iguana_send_supernet(struct iguana_info *coin,struct iguana_peer *addr,char *jsonstr,int32_t delaymillis) |
|
|
|
{ |
|
|
|
int32_t datalen,qlen = -1; uint8_t *serialized,*space; cJSON *json; struct supernet_info *myinfo; |
|
|
|
int32_t datalen,cipherlen,qlen = -1; uint8_t *serialized,space2[8192],*cipher; cJSON *json; |
|
|
|
struct supernet_info *myinfo; bits256 destpub,privkey; |
|
|
|
myinfo = SuperNET_MYINFO(0); |
|
|
|
if ( (json= cJSON_Parse(jsonstr)) != 0 ) |
|
|
|
{ |
|
|
|
serialized = malloc(sizeof(struct iguana_msghdr) + IGUANA_MAXPACKETSIZE); |
|
|
|
space = malloc(sizeof(struct iguana_msghdr) + IGUANA_MAXPACKETSIZE); |
|
|
|
datalen = SuperNET_json2bits(myinfo->ipaddr,myinfo->persistent_priv,myinfo->myaddr.persistent,&serialized[sizeof(struct iguana_msghdr)],IGUANA_MAXPACKETSIZE,addr->ipaddr,addr->pubkey,json); |
|
|
|
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 ( 1 ) |
|
|
|
if ( memcmp(destpub.bytes,GENESIS_PUBKEY.bytes,sizeof(destpub)) == 0 ) |
|
|
|
qlen = iguana_queue_send(coin,addr,delaymillis,serialized,"SuperNET",datalen,0,0); |
|
|
|
else qlen = SuperNET_sendmsg(myinfo,coin,addr,addr->pubkey,myinfo->privkey,myinfo->myaddr.pubkey,serialized,datalen,space,delaymillis); |
|
|
|
else |
|
|
|
{ |
|
|
|
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);
|
|
|
|
} |
|
|
|
free(serialized); |
|
|
|
free(space); |
|
|
|
} else printf("cant parse.(%s)\n",jsonstr); |
|
|
|
return(qlen); |
|
|
|
} |
|
|
@ -621,18 +714,19 @@ char *SuperNET_JSON(struct supernet_info *myinfo,cJSON *json,char *remoteaddr) |
|
|
|
|
|
|
|
char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *delaymillisp,char *ipaddr,uint8_t *data,int32_t datalen,int32_t compressed) |
|
|
|
{ |
|
|
|
cJSON *json; char *myipaddr,*method,*retstr; int32_t maxdelay; struct supernet_info *myinfo; uint8_t *space; |
|
|
|
cJSON *json; char *myipaddr,*method,*retstr; int32_t maxdelay,msglen = datalen; |
|
|
|
struct supernet_info *myinfo; uint8_t space[8192],*msgbits = 0; bits256 senderpub; |
|
|
|
myinfo = SuperNET_MYINFO(0); |
|
|
|
retstr = 0; space = 0; |
|
|
|
retstr = 0; |
|
|
|
*delaymillisp = 0; |
|
|
|
if ( compressed != 0 ) |
|
|
|
space = malloc(sizeof(struct iguana_msghdr) + IGUANA_MAXPACKETSIZE); |
|
|
|
if ( (json= SuperNET_bits2json(myinfo->privkey,myinfo->myaddr.pubkey,addr,data,space,datalen,compressed)) != 0 ) |
|
|
|
{ |
|
|
|
/*senderpub = jbits256(json,"mypub");
|
|
|
|
if ( memcmp(senderpub.bytes,addr->pubkey.bytes,sizeof(senderpub)) != 0 ) |
|
|
|
addr->pubkey = senderpub; |
|
|
|
if ( (checkstr= jstr(json,"check")) != 0 ) |
|
|
|
if ( (msgbits= SuperNET_deciphercalc(&msglen,myinfo->privkey,myinfo->myaddr.pubkey,data,datalen,space,sizeof(space))) == 0 ) |
|
|
|
return(clonestr("{\"error\":\"couldnt decrypt p2p packet\"}")); |
|
|
|
} else msgbits = data; |
|
|
|
if ( (json= SuperNET_bits2json(myinfo->privkey,addr,msgbits,msglen)) != 0 ) |
|
|
|
{ |
|
|
|
/*if ( (checkstr= jstr(json,"check")) != 0 )
|
|
|
|
{ |
|
|
|
decode_hex((uint8_t *)&othercheckc,sizeof(othercheckc),checkstr); |
|
|
|
checkc = SuperNET_checkc(myinfo->privkey,senderpub,j64bits(json,"tag")); |
|
|
@ -657,17 +751,20 @@ char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *de |
|
|
|
{ |
|
|
|
addr->dead = (uint32_t)time(NULL); |
|
|
|
free_json(json); |
|
|
|
if ( space != 0 ) |
|
|
|
free(space); |
|
|
|
if ( &msgbits[-crypto_box_ZEROBYTES] != space ) |
|
|
|
free(&msgbits[-crypto_box_ZEROBYTES]); |
|
|
|
return(clonestr("{\"result\":\"peer marked as dead\"}")); |
|
|
|
} |
|
|
|
retstr = SuperNET_JSON(myinfo,json,ipaddr); |
|
|
|
//printf("p2pret.(%s)\n",retstr);
|
|
|
|
*delaymillisp = SuperNET_delaymillis(myinfo,maxdelay); |
|
|
|
senderpub = jbits256(json,"mypub"); |
|
|
|
if ( memcmp(senderpub.bytes,addr->pubkey.bytes,sizeof(senderpub)) != 0 ) |
|
|
|
addr->pubkey = senderpub; |
|
|
|
free_json(json); |
|
|
|
} |
|
|
|
if ( space != 0 ) |
|
|
|
free(space); |
|
|
|
if ( &msgbits[-crypto_box_ZEROBYTES] != space ) |
|
|
|
free(&msgbits[-crypto_box_ZEROBYTES]); |
|
|
|
return(retstr); |
|
|
|
} |
|
|
|
|
|
|
@ -741,27 +838,6 @@ void SuperNET_parsepeers(struct supernet_info *myinfo,cJSON *array,int32_t n,int |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int32_t _SuperNET_cipher(uint8_t nonce[crypto_box_NONCEBYTES],uint8_t *cipher,uint8_t *message,int32_t len,bits256 destpub,bits256 srcpriv,uint8_t *buf) |
|
|
|
{ |
|
|
|
memset(cipher,0,len+crypto_box_ZEROBYTES); |
|
|
|
memset(buf,0,crypto_box_ZEROBYTES); |
|
|
|
memcpy(buf+crypto_box_ZEROBYTES,message,len); |
|
|
|
crypto_box(cipher,buf,len+crypto_box_ZEROBYTES,nonce,destpub.bytes,srcpriv.bytes); |
|
|
|
return(len + crypto_box_ZEROBYTES); |
|
|
|
} |
|
|
|
|
|
|
|
uint8_t *_SuperNET_decipher(uint8_t nonce[crypto_box_NONCEBYTES],uint8_t *cipher,uint8_t *message,int32_t len,bits256 srcpub,bits256 mypriv) |
|
|
|
{ |
|
|
|
int32_t err; |
|
|
|
if ( (err= crypto_box_open(message,cipher,len,nonce,srcpub.bytes,mypriv.bytes)) == 0 ) |
|
|
|
{ |
|
|
|
message += crypto_box_ZEROBYTES; |
|
|
|
len -= crypto_box_ZEROBYTES; |
|
|
|
return(message); |
|
|
|
} |
|
|
|
return(0); |
|
|
|
} |
|
|
|
|
|
|
|
#include "../includes/iguana_apidefs.h" |
|
|
|
|
|
|
|
HASH_ARG(SuperNET,priv2pub,privkey) |
|
|
@ -784,103 +860,54 @@ ZERO_ARGS(SuperNET,keypair) |
|
|
|
|
|
|
|
TWOHASHES_AND_STRING(SuperNET,decipher,privkey,srcpubkey,cipherstr) |
|
|
|
{ |
|
|
|
cJSON *retjson; char *retstr; |
|
|
|
uint8_t *origptr,*cipher,*nonce,*deciphered,*message,space[8192],space2[sizeof(space)]; int len; |
|
|
|
if ( bits256_nonz(privkey) == 0 ) |
|
|
|
privkey = GENESIS_PRIVKEY; |
|
|
|
len = (int32_t)strlen(cipherstr)>>1; |
|
|
|
if ( len < crypto_box_NONCEBYTES ) |
|
|
|
int32_t cipherlen,msglen; char *retstr; cJSON *retjson; uint8_t *cipher,*message,space[8192]; |
|
|
|
cipherlen = (int32_t)strlen(cipherstr) >> 1; |
|
|
|
if ( cipherlen < crypto_box_NONCEBYTES ) |
|
|
|
return(clonestr("{\"error\":\"cipher is too short\"}")); |
|
|
|
if ( len > sizeof(space) ) |
|
|
|
cipher = calloc(1,cipherlen); |
|
|
|
decode_hex(cipher,cipherlen,cipherstr); |
|
|
|
if ( (message= SuperNET_deciphercalc(&msglen,privkey,srcpubkey,cipher,cipherlen,space,sizeof(space))) != 0 ) |
|
|
|
{ |
|
|
|
cipher = calloc(1,len); |
|
|
|
message = calloc(1,len); |
|
|
|
} |
|
|
|
else cipher = space, message = space2; |
|
|
|
decode_hex(cipher,len,cipherstr); |
|
|
|
origptr = cipher; |
|
|
|
if ( bits256_nonz(srcpubkey) == 0 ) |
|
|
|
{ |
|
|
|
memcpy(srcpubkey.bytes,cipher,sizeof(srcpubkey)); |
|
|
|
//char str[65]; printf("use attached pubkey.(%s)\n",bits256_str(str,srcpubkey));
|
|
|
|
cipher += sizeof(srcpubkey); |
|
|
|
len -= sizeof(srcpubkey); |
|
|
|
} |
|
|
|
nonce = cipher; |
|
|
|
cipher += crypto_box_NONCEBYTES, len -= crypto_box_NONCEBYTES; |
|
|
|
if ( (deciphered= _SuperNET_decipher(nonce,cipher,message,len,srcpubkey,privkey)) != 0 ) |
|
|
|
{ |
|
|
|
len -= crypto_box_ZEROBYTES; |
|
|
|
deciphered[len] = 0; |
|
|
|
message[msglen] = 0; |
|
|
|
retjson = cJSON_CreateObject(); |
|
|
|
jaddstr(retjson,"result","deciphered message"); |
|
|
|
jaddstr(retjson,"message",(char *)deciphered); |
|
|
|
jaddstr(retjson,"message",(char *)message); |
|
|
|
retstr = jprint(retjson,1); |
|
|
|
if ( &message[-crypto_box_ZEROBYTES] != space ) |
|
|
|
free(&message[-crypto_box_ZEROBYTES]); |
|
|
|
} else retstr = clonestr("{\"error\":\"couldnt decipher message\"}"); |
|
|
|
if ( origptr != space ) |
|
|
|
free(origptr); |
|
|
|
if ( message != space2 ) |
|
|
|
free(message); |
|
|
|
return(retstr); |
|
|
|
} |
|
|
|
|
|
|
|
TWOHASHES_AND_STRING(SuperNET,cipher,privkey,destpubkey,message) |
|
|
|
{ |
|
|
|
bits256 mypubkey; uint8_t *buf,*nonce,*cipher,*origptr,space[8192],space2[sizeof(space)]; |
|
|
|
cJSON *retjson; char *retstr,*hexstr; int32_t onetimeflag=0,allocsize,len; |
|
|
|
len = (int32_t)strlen(message); |
|
|
|
allocsize = (len + crypto_box_NONCEBYTES + crypto_box_ZEROBYTES); |
|
|
|
if ( bits256_nonz(destpubkey) == 0 ) |
|
|
|
{ |
|
|
|
destpubkey = GENESIS_PUBKEY; |
|
|
|
onetimeflag = 2; // prevent any possible leakage of privkey by using known destpub
|
|
|
|
} |
|
|
|
if ( bits256_nonz(privkey) == 0 ) |
|
|
|
onetimeflag = 1; |
|
|
|
if ( onetimeflag != 0 ) |
|
|
|
{ |
|
|
|
crypto_box_keypair(mypubkey.bytes,privkey.bytes); |
|
|
|
allocsize += sizeof(bits256); |
|
|
|
} |
|
|
|
if ( allocsize > sizeof(space) ) |
|
|
|
{ |
|
|
|
buf = calloc(1,allocsize); |
|
|
|
cipher = calloc(1,allocsize); |
|
|
|
} else buf = space, cipher = space2; |
|
|
|
origptr = nonce = cipher; |
|
|
|
if ( onetimeflag != 0 ) |
|
|
|
cJSON *retjson; char *retstr,*hexstr,space[8129]; uint8_t space2[8129]; |
|
|
|
uint8_t *cipher; int32_t cipherlen,onetimeflag; bits256 origprivkey; |
|
|
|
if ( (cipher= SuperNET_ciphercalc(&cipherlen,&privkey,&destpubkey,(uint8_t *)message,(int32_t)strlen(message)+1,space2,sizeof(space2))) != 0 ) |
|
|
|
{ |
|
|
|
memcpy(cipher,mypubkey.bytes,sizeof(mypubkey)); |
|
|
|
nonce = &cipher[sizeof(mypubkey)]; |
|
|
|
} |
|
|
|
OS_randombytes(nonce,crypto_box_NONCEBYTES); |
|
|
|
cipher = &nonce[crypto_box_NONCEBYTES]; |
|
|
|
_SuperNET_cipher(nonce,cipher,(void *)message,len,destpubkey,privkey,buf); |
|
|
|
if ( buf != space ) |
|
|
|
free(buf); |
|
|
|
if ( allocsize > sizeof(space)/2 ) |
|
|
|
hexstr = calloc(1,(allocsize<<1)+1); |
|
|
|
else hexstr = (void *)space; |
|
|
|
init_hexbytes_noT(hexstr,origptr,allocsize); |
|
|
|
retjson = cJSON_CreateObject(); |
|
|
|
jaddstr(retjson,"result","ciphered message"); |
|
|
|
jaddstr(retjson,"message",message); |
|
|
|
jaddstr(retjson,"cipher",hexstr); |
|
|
|
init_hexbytes_noT(hexstr,nonce,crypto_box_NONCEBYTES); |
|
|
|
jaddstr(retjson,"nonce",hexstr); |
|
|
|
if ( onetimeflag != 0 ) |
|
|
|
{ |
|
|
|
jaddbits256(retjson,"onetime_privkey",privkey); |
|
|
|
jaddbits256(retjson,"onetime_pubkey",mypubkey); |
|
|
|
if ( onetimeflag == 2 ) |
|
|
|
jaddstr(retjson,"warning","onetime keypair was used to broadcast"); |
|
|
|
} |
|
|
|
retstr = jprint(retjson,1); |
|
|
|
if ( hexstr != (void *)space ) |
|
|
|
free(hexstr); |
|
|
|
if ( origptr != space2 ) |
|
|
|
free(origptr); |
|
|
|
return(retstr); |
|
|
|
if ( cipherlen > sizeof(space)/2 ) |
|
|
|
hexstr = calloc(1,(cipherlen<<1)+1); |
|
|
|
else hexstr = (void *)space; |
|
|
|
init_hexbytes_noT(hexstr,cipher,cipherlen); |
|
|
|
retjson = cJSON_CreateObject(); |
|
|
|
jaddstr(retjson,"result",hexstr); |
|
|
|
//jaddstr(retjson,"message",message);
|
|
|
|
//jaddstr(retjson,"cipher",hexstr);
|
|
|
|
onetimeflag = memcmp(origprivkey.bytes,privkey.bytes,sizeof(privkey)); |
|
|
|
if ( onetimeflag != 0 ) |
|
|
|
{ |
|
|
|
jaddbits256(retjson,"onetime_privkey",privkey); |
|
|
|
jaddbits256(retjson,"onetime_pubkey",destpubkey); |
|
|
|
if ( onetimeflag == 2 ) |
|
|
|
jaddstr(retjson,"warning","onetime keypair was used to broadcast"); |
|
|
|
} |
|
|
|
retstr = jprint(retjson,1); |
|
|
|
if ( hexstr != (void *)space ) |
|
|
|
free(hexstr); |
|
|
|
if ( cipher != space2 ) |
|
|
|
free(cipher); |
|
|
|
return(retstr); |
|
|
|
} else return(clonestr("{\"error\":\"cant encrypt message\"}")); |
|
|
|
} |
|
|
|
|
|
|
|
bits256 SuperNET_pindecipher(IGUANA_ARGS,char *pin,char *privcipher) |
|
|
|