static void bcipherCrypt(BCipherRandomGen* gen,const char* key,int klen,int nonce, char* dest,const char* src,int n) { char keyx[gen->klen]; assert(klen<=gen->klen); memcpy(keyx,key,klen); memset(keyx+klen,0,gen->klen-klen); resetBCipherRandomGen(gen,keyx); setctrFromIntBCipherRandomGen(gen,nonce); randomizeBuffer(gen,dest,n); memxor(dest, src, n); }
void hashMacCheck(BCipherRandomGen* gen, int* nonce, nnob_key_t* tar, const nnob_key_t* src) { char keyx[gen->klen]; int keylen = 2*NNOB_KEY_BYTES; memcpy(keyx,src,NNOB_KEY_BYTES); memcpy(keyx+NNOB_KEY_BYTES,tar,NNOB_KEY_BYTES); memset(keyx+keylen,0,gen->klen-keylen); resetBCipherRandomGen(gen,keyx); setctrFromIntBCipherRandomGen(gen,*nonce); randomizeBuffer(gen,*tar,NNOB_KEY_BYTES); (*nonce)+=1; debugPrintHex(*tar, NNOB_KEY_BYTES); nl(); }
bool LaOTKeyOfZ(ProtocolDesc* pd, const NnobShareAndMac* x0, const NnobShareAndMac* x1, const NnobKey* c, const NnobKey* r, NnobKey* z, BCipherRandomGen* padder, BCipherRandomGen* gen, int* nonce, int numLaOTs) { NnobProtocolDesc* npd = pd->extra; assert(npd!=NULL); int i, destparty = pd->thisParty==1?1:2; bool success = true; const int XOTBytes = 1+2*NNOB_KEY_BYTES; char (*X0)[XOTBytes] = malloc(numLaOTs*XOTBytes); char (*X1)[XOTBytes] = malloc(numLaOTs*XOTBytes); char (*I0)[NNOB_KEY_BYTES] = malloc(numLaOTs*NNOB_KEY_BYTES); char (*I1)[NNOB_KEY_BYTES] = malloc(numLaOTs*NNOB_KEY_BYTES); char (*T0)[NNOB_KEY_BYTES] = malloc(numLaOTs*NNOB_KEY_BYTES); char (*T1)[NNOB_KEY_BYTES] = malloc(numLaOTs*NNOB_KEY_BYTES); char temp[NNOB_KEY_BYTES]; char (*mc)[XOTBytes] = malloc(numLaOTs*XOTBytes); bool* d = malloc(numLaOTs); const char* globalDelta = npd->globalDelta; for(i=0;i<numLaOTs;i++) { char m0[XOTBytes], m1[XOTBytes]; randomizeBuffer(gen, T0[i], NNOB_KEY_BYTES); randomizeBuffer(gen, T1[i], NNOB_KEY_BYTES); m0[0] = x0[i].share; memcpy(m0+1, x0[i].mac, NNOB_KEY_BYTES); if(x0[i].share) memcpy(m0+1+NNOB_KEY_BYTES, T1[i], NNOB_KEY_BYTES); else memcpy(m0+1+NNOB_KEY_BYTES, T0[i], NNOB_KEY_BYTES); m1[0] = x1[i].share; memcpy(m1+1, x1[i].mac, NNOB_KEY_BYTES); if(x1[i].share) memcpy(m1+1+NNOB_KEY_BYTES, T1[i], NNOB_KEY_BYTES); else memcpy(m1+1+NNOB_KEY_BYTES, T0[i], NNOB_KEY_BYTES); nnobPRG(padder, c[i].key, NNOB_KEY_BYTES, *nonce, X0[i], m0, XOTBytes); memcpy(temp, c[i].key, NNOB_KEY_BYTES); memxor(temp, globalDelta, NNOB_KEY_BYTES); nnobPRG(padder, temp, NNOB_KEY_BYTES, *nonce, X1[i], m1, XOTBytes); *nonce+=1; } osend(pd, destparty, X0, numLaOTs*XOTBytes); osend(pd, destparty, X1, numLaOTs*XOTBytes); orecv(pd, destparty, d, numLaOTs); for(i=0;i<numLaOTs;i++) { z[i]=r[i]; nnobKeyXORConst(&z[i], d[i], globalDelta); nnobHash(padder, z[i].key, NNOB_KEY_BYTES, *nonce, I0[i], T1[i]); memcpy(temp, z[i].key, NNOB_KEY_BYTES); memxor(temp, globalDelta, NNOB_KEY_BYTES); nnobHash(padder, temp, NNOB_KEY_BYTES, *nonce, I1[i], T0[i]); *nonce+=1; } osend(pd, destparty, I0, numLaOTs*NNOB_KEY_BYTES); osend(pd, destparty, I1, numLaOTs*NNOB_KEY_BYTES); success &= ocEqualityCheck(pd, gen, T0, numLaOTs*NNOB_KEY_BYTES, destparty); success &= ocEqualityCheck(pd, gen, T1, numLaOTs*NNOB_KEY_BYTES, destparty); free(X0); free(X1); free(I0); free(I1); free(T0); free(T1); free(mc); free(d); return success; }
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); }
void randomizeSalt(byte_t * salt) { randomizeBuffer(salt, SALT_LEN); }
LONG_INDEX_PROJ getRandomLongIndexProj() { LONG_INDEX_PROJ randNum; randomizeBuffer((byte_t *) &randNum, sizeof(randNum)); return randNum; }
ulong_t getRandomULong() { ulong_t randNum; randomizeBuffer((byte_t *) &randNum, sizeof(randNum)); return randNum; }