void VeritazzExtra::drawPackedImage(int16_t x, int16_t y, const uint8_t *img, uint8_t w, uint8_t h, uint16_t flags) { // no need to dar at all of we're offscreen if (x + w <= 0 || x > WIDTH - 1 || y + h <= 0 || y > HEIGHT - 1) return; int yOffset = abs(y) % 8; int sRow = y / 8; int sCol = 0; int eCol = w - (x + w > WIDTH ? (x + w) % WIDTH : 0); uint8_t bCol; if (y < 0) { sRow--; yOffset = 8 - yOffset; } if (x < 0) sCol = abs(x); uint8_t buf[w]; if (flags & __flag_unpack) setStartByte(img, 0); int rows = h / 8; if (h % 8 != 0) rows++; for (int a = 0; a < rows; a++) { int bRow = sRow + a; if (flags & __flag_unpack) unpackBytes(buf, w); else { #ifndef HOST_TEST memcpy_P(buf, img, w); #else memcpy(buf, img, w); #endif img += w; } if (bRow > (HEIGHT / 8) - 1) break; if (bRow >= 0) { for (int iCol = sCol; iCol < eCol; iCol++) { if (iCol + x > (WIDTH - 1)) break; if (flags & __flag_h_mirror) bCol = w - iCol - 1; else bCol = iCol; if (flags & __flag_white) this->sBuffer[ (bRow * WIDTH) + x + iCol ] |= buf[bCol] << yOffset; else if (flags & __flag_black) this->sBuffer[ (bRow * WIDTH) + x + iCol ] &= ~(buf[bCol] << yOffset); else this->sBuffer[ (bRow * WIDTH) + x + iCol ] ^= buf[bCol] << yOffset; } } if (yOffset && bRow < (HEIGHT / 8) - 1 && bRow > -2) { for (int iCol = sCol; iCol < eCol; iCol++) { if (iCol + x > (WIDTH - 1)) break; if (flags & __flag_h_mirror) bCol = w - iCol - 1; else bCol = iCol; if (flags & __flag_white) this->sBuffer[ ((bRow + 1)*WIDTH) + x + iCol ] |= buf[bCol] >> (8 - yOffset); else if (flags & __flag_black) this->sBuffer[ ((bRow + 1)*WIDTH) + x + iCol ] &= ~(buf[bCol] >> (8 - yOffset)); else this->sBuffer[ ((bRow + 1)*WIDTH) + x + iCol ] ^= buf[bCol] >> (8 - yOffset); } }
bool WaBitBoxGetBitAndMac(ProtocolDesc* pd, bool* b, char* mat, char (*aBitFullMac)[A_BIT_PARAMETER_BYTES], int n, OTExtValidation validation, int destparty){ assert(n%8==0); int i; bool success = true; int k = 8*A_BIT_PARAMETER_BYTES; k*=validation==OTExtValidation_hhash?1:2; int rowBytes = (n+7)/8; int *rows,rc; char *box = malloc(k*rowBytes); char *mask = malloc(rowBytes); BCipherRandomGen* gen = newBCipherRandomGen(); RecverExtensionBox* r =recverExtensionBoxNew(pd, destparty, k/8); randomizeBuffer(gen,mask,rowBytes); unpackBytes(b,mask,n); recverExtensionBox(r,box,mask,rowBytes); if(validation==OTExtValidation_hhash) { rows = allRows(k); rc=k; if(!recverExtensionBoxValidate_hhash(r, gen, box, rowBytes)) success = false; } else { rows = malloc(sizeof(int[k/2])); rc=k/2; if(!recverExtensionBoxValidate_byPair(r, gen, rows, box,mask,rowBytes, true)) success = false; } if(!success) r->pd->error = OC_ERROR_OT_EXTENSION; success&=ocRandomBytes(pd, gen, mat,8*A_BIT_PARAMETER_BYTES*NNOB_KEY_BYTES, destparty); assert(rc==8*A_BIT_PARAMETER_BYTES); int tc=OT_THREAD_COUNT, done=0; TransposeThread args[OT_THREAD_COUNT]; pthread_t transpt[OT_THREAD_COUNT]; for(i=0;i<tc;++i) { int c = (n-done)/(tc-i); args[i] = (TransposeThread){.dest=aBitFullMac,.src=box, .rc=rc,.rows=rows,.rowBytes=rowBytes,.from=done,.to=done+c}; if(i==tc-1) transpose_thread(&args[i]); // no thread if tc==1 else pthread_create(&transpt[i],NULL,transpose_thread,&args[i]); done+=c; } for(i=0;i<tc-1;++i) pthread_join(transpt[i],NULL); free(mask); free(rows); free(box); recverExtensionBoxRelease(r); releaseBCipherRandomGen(gen); return success; } bool WaBitBoxGetKey(ProtocolDesc* pd, nnob_key_t globalDelta, char* mat, char (*aBitFullKey)[A_BIT_PARAMETER_BYTES], int n, OTExtValidation validation, int destparty){ assert(n%8==0); int i; bool success = true; int k = 8*A_BIT_PARAMETER_BYTES; k*=validation==OTExtValidation_hhash?1:2; int rowBytes = (n+7)/8; int *rows, rc; char *box = malloc(k*rowBytes); BCipherRandomGen* gen = newBCipherRandomGen(); SenderExtensionBox* s = senderExtensionBoxNew(pd, destparty, k/8); senderExtensionBox(s,box,rowBytes); if(validation==OTExtValidation_hhash) { rows = allRows(k); rc = k; if(!senderExtensionBoxValidate_hhash(s,gen,box,rowBytes)) success = false; } else { rows = malloc(sizeof(int[k/2])); rc = k/2; if(!senderExtensionBoxValidate_byPair(s,gen,rows,box,rowBytes, true)) success = false; for(i=0;i<rc;++i) setBit(s->spack,i,s->S[rows[i]]); } if(!success) s->pd->error = OC_ERROR_OT_EXTENSION; success&=ocRandomBytes(pd, gen, mat,8*A_BIT_PARAMETER_BYTES*NNOB_KEY_BYTES, destparty); bitmatMul(globalDelta, mat, s->spack, 8*NNOB_KEY_BYTES, rc); assert(rc==8*A_BIT_PARAMETER_BYTES); int tc=OT_THREAD_COUNT, done=0; TransposeThread args[OT_THREAD_COUNT]; pthread_t transpt[OT_THREAD_COUNT]; for(i=0;i<tc;++i) { int c = (n-done)/(tc-i); args[i] = (TransposeThread){.dest=aBitFullKey,.src=box, .rc=rc,.rows=rows,.rowBytes=rowBytes,.from=done,.to=done+c}; if(i==tc-1) transpose_thread(&args[i]); // no thread if tc==1 else pthread_create(&transpt[i],NULL,transpose_thread,&args[i]); done+=c; } for(i=0;i<tc-1;++i) pthread_join(transpt[i],NULL); free(rows); free(box); senderExtensionBoxRelease(s); releaseBCipherRandomGen(gen); return success; } void WaBitToaBit(char (*aBit)[NNOB_KEY_BYTES], char (*WaBit)[A_BIT_PARAMETER_BYTES], char* mat, int n){ int rowBytes = (n+7)/8, i,done=0,tc; BitMatMulThread args[OT_THREAD_COUNT]; pthread_t hasht[OT_THREAD_COUNT]; if(8*rowBytes<=OT_THREAD_THRESHOLD) tc=1; else tc=OT_THREAD_COUNT; for(i=0;i<tc;++i) { int c = (n-done)/(tc-i); args[i] = (BitMatMulThread){.dest=aBit,.hashmat=mat,.src=WaBit, .from=done,.to=done+c}; if(i==tc-1) bitmatMul_thread_nnob(&args[i]); // no thread if tc==1 else pthread_create(&hasht[i],NULL,bitmatMul_thread_nnob,&args[i]); done+=c; } for(i=0;i<tc-1;++i) pthread_join(hasht[i],NULL); } void randomOblivAuthenticationShareAndMac(NnobProtocolDesc* npd, NnobShareAndMac* sm) { int counter = npd->aBitsShareAndMac.counter; npd->aBitsShareAndMac.counter+=1; assert(counter<npd->aBitsShareAndMac.n); sm->share = npd->aBitsShareAndMac.share[counter]; memcpy(sm->mac, npd->aBitsShareAndMac.mac[counter], NNOB_KEY_BYTES); }