Beispiel #1
0
int32_t iguana_alloctxbits(struct iguana_info *coin,struct iguana_ramchain *ramchain)
{
    static int64_t total; struct iguana_ramchaindata *rdata;
    if ( 0 && ramchain->txbits == 0 && (rdata= ramchain->H.data) != 0 )
    {
        int32_t tlen; uint8_t *TXbits;
        TXbits = RAMCHAIN_PTR(rdata,TXoffset);
        //TXbits = (uint8_t *)((long)rdata + rdata->TXoffset);
        tlen = (int32_t)hconv_bitlen(rdata->numtxsparse * rdata->txsparsebits);
        ramchain->txbits = calloc(1,tlen);
        memcpy(ramchain->txbits,TXbits,tlen);
        total += tlen;
        char str[65]; printf("%s alloc.[%d] txbits.%p[%d] total %s\n",coin->symbol,rdata->height/coin->chain->bundlesize,ramchain->txbits,tlen,mbstr(str,total));
        return(tlen);
    }
    return(-1);
}
Beispiel #2
0
struct crypto777_packet *crypto777_packet(struct crypto777_link *conn,uint32_t *timestamp,uint8_t *msg,int32_t len,int32_t leverage,int32_t maxmillis,uint64_t dest64bits)
{
    static uint32_t rseed = 1234;
    uint64_t hit = SAMHIT_LIMIT;
    int32_t defnumrounds=NUMROUNDS_NONCE,numrounds,codersize,netsize,allocsize = MAX_UDPSIZE;
    struct crypto777_packet *bufs;
    struct SaMhdr *hdr;
    bits384 checksig;
    if ( *timestamp == 0 )
        *timestamp = (uint32_t)time(NULL);
    netsize = (allocsize - 2*sizeof(uint32_t));
    if ( conn->my64bits == conn->send.nxt64bits && conn->recv.nxt64bits == dest64bits && memcmp(conn->send.prevmsg,msg,len) == 0 && *timestamp == conn->send.prevtimestamp )
        while ( conn->send.prevtimestamp == time(NULL) )
            msleep(250), fprintf(stderr,". "), *timestamp = (uint32_t)time(NULL);
    if ( Debuglevel > 0 )
        printf("(%llu %llu) packet.(%s) timestamp.%u [%llx] len.%d -> %llu\n",(long long)conn->send.nxt64bits,(long long)conn->recv.nxt64bits,(conn->send.nxt64bits != dest64bits) ? (char *)&msg[sizeof(struct SaMhdr)] : "ENCRYPTED",*timestamp,*(long long *)msg,len,(long long)dest64bits);
    codersize = (sizeof(*bufs->coder) + sizeof(uint16_t)*(0x100 + 2));
    bufs = calloc(1,sizeof(struct crypto777_packet) + codersize + (2 * sizeof(struct crypto777_bits)) + (sizeof(struct SaMhdr) + len) + MAX_CRYPTO777_MSG);
    //bufs->DL.ptr = bufs;
    bufs->coder = (struct ramcoder *)bufs->space, memset(bufs->coder,0,codersize);
    bufs->plaintext = (struct crypto777_bits *)&bufs->space[codersize], _init_HUFF(&bufs->plainH,netsize,bufs->plaintext->bits);
    bufs->encrypted = &bufs->plaintext[1], _init_HUFF(&bufs->encryptedH,netsize,&bufs->encrypted->bits);
    bufs->origmsg = (uint8_t *)&bufs->plaintext[2];
    bufs->recvbuf = &bufs->origmsg[sizeof(struct SaMhdr) + len]; // must be last!
    if ( dest64bits != 0 && conn->send.nxt64bits != 0 )
        calc_sha256cat(bufs->coderseed.bytes,(uint8_t *)timestamp,sizeof(*timestamp),conn->shared_curve25519.bytes,sizeof(conn->shared_curve25519));
    bufs->encrypted->plaintext_timestamp = bufs->plaintext->plaintext_timestamp = *timestamp;
    bufs->origlen = len; //printf("hdr.%ld len.%d\n",sizeof(struct SaMhdr),len);
    if ( conn->recv.nxt64bits == dest64bits && maxmillis > 0 )
    {
        hdr = (struct SaMhdr *)bufs->origmsg;
        memcpy(&bufs->origmsg[sizeof(*hdr)],msg,len);
        hdr->timestamp = *timestamp, hdr->leverage = leverage, hdr->numrounds = defnumrounds;
        bufs->threshold = SaM_threshold(leverage);
        len += (int32_t)(sizeof(*hdr) - sizeof(hdr->sig));
        //printf("+= %d | %ld = (%ld - %ld)\n",len,sizeof(*hdr) - sizeof(hdr->sig),sizeof(*hdr),sizeof(hdr->sig));
        /*if ( (hit = SaMnonce(&hdr->sig,&hdr->nonce,&bufs->origmsg[sizeof(hdr->sig)],len,bufs->threshold,rseed,maxmillis)) == 0 )
        {
            printf("crypto777_packet cant find nonce: numrounds.%d leverage.%d\n",hdr->numrounds,leverage);
            free(bufs);
            return(0);
         }*/printf("deprecated SaMnonce\n"); while ( 1 ) sleep(60);
        if ( Debuglevel > 0 )
            printf(">>>>>>>>>>>> outbound timestamp.%u nonce.%u hit.%llu (%s) | sig.%llx\n",hdr->timestamp,hdr->nonce,(long long)hit,&bufs->origmsg[sizeof(*hdr)],(long long)hdr->sig.txid);
        rseed = (uint32_t)(hdr->sig.txid ^ hit);
        len += sizeof(hdr->sig);
        if ( dest64bits != 0 )
        {
            ramcoder_encoder(bufs->coder,1,bufs->origmsg,len,&bufs->plainH,&bufs->coderseed);
            if ( (bufs->newlen= hconv_bitlen(bufs->plainH.bitoffset)) >= bufs->plainH.allocsize )
            {
                printf("crypto777_packet overflow: newlen.%d doesnt fit into %d\n",bufs->newlen,bufs->plainH.allocsize);
                free(bufs);
                return(0);
            }
            else _randombytes(&bufs->plainH.buf[bufs->newlen],bufs->plainH.allocsize - bufs->newlen,(uint32_t)hdr->sig.txid);
            if ( dest64bits != 0 )
                crypto777_encrypt(&bufs->encryptedH,&bufs->plainH,netsize,&conn->send,*timestamp);
        }
        conn->send.prevlen = bufs->origlen, memcpy(conn->send.prevmsg,msg,bufs->origlen), conn->send.prevtimestamp = *timestamp;
    }
    else if ( conn->send.nxt64bits == dest64bits && milliseconds() > conn->recv.blacklist )
    {
        if ( dest64bits != 0 )
        {
            if ( dest64bits != 0 )
                memcpy(bufs->encryptedH.buf,msg,len), crypto777_encrypt(&bufs->plainH,&bufs->encryptedH,len,&conn->recv,*timestamp);
            else memcpy(bufs->plainH.buf,msg,len);
            hseek(&bufs->plainH,len << 3,SEEK_SET), hrewind(&bufs->plainH);
            bufs->recvlen = ramcoder_decoder(bufs->coder,1,bufs->recvbuf,len,&bufs->plainH,&bufs->coderseed);
        } else memcpy(bufs->recvbuf,msg,len), bufs->recvlen = len;
        hdr = (struct SaMhdr *)bufs->recvbuf;
        leverage = hdr->leverage, numrounds = hdr->numrounds;
        if ( Debuglevel > 0 )
            printf("<<<<<<<< nonce.%u len.%d leverage.%d numrounds.%d\n",hdr->nonce,len,leverage,(int)numrounds);
        if ( leverage < CRYPTO777_MAXLEVERAGE && numrounds == defnumrounds )
        {
            bufs->threshold = SaM_threshold(leverage);
            len = bufs->recvlen - sizeof(hdr->sig);
            hit = SaM(&checksig,&bufs->recvbuf[sizeof(hdr->sig)],len,0,0);
            len -= (sizeof(*hdr) - sizeof(hdr->sig));
            if ( hit >= bufs->threshold || memcmp(checksig.bytes,hdr->sig.bytes,sizeof(checksig)) != 0 )
            {
                conn->recv.blacklist = (milliseconds() + 10000);
                printf("crypto777_packet invalid AUTH: hit %llu >= threshold.%llu || sig mismatch.%d (%llx v %llx) from %llu leverage.%d numrounds.%d\n",(long long)hit,(long long)bufs->threshold,memcmp(checksig.bytes,hdr->sig.bytes,sizeof(checksig)),(long long)checksig.txid,(long long)hdr->sig.txid,(long long)conn->recv.nxt64bits,hdr->leverage,hdr->numrounds);
                free(bufs);
                return(0);
            } else if ( Debuglevel > 0 ) printf("AUTHENTICATED packet from %llu len.%d leverage.%d numrounds.%d | hit %.2f%%\n",(long long)conn->recv.nxt64bits,len,leverage,numrounds,100.*(double)hit/bufs->threshold);
        } else printf("AUTH failure from %llu: hit.%llu >= threshold.%llu leverage.%d numrounds.%d\n",(long long)conn->recv.nxt64bits,(long long)hit,(long long)bufs->threshold,leverage,numrounds);
    } else printf("crypto777_packet warning invalid dest64bits %llu\n",(long long)dest64bits);
    return(bufs);
}