int32_t test(int32_t M,int32_t N,int32_t datasize) { int ok = 1, i; uint8_t * secret = malloc(datasize); uint8_t *shares[255]; uint8_t *recomb = malloc(datasize); uint8_t sharenrs[255],newsharenrs[255];// = (uint8_t *)strdup("0124z89abehtr"); gfshare_ctx *G; gfshare_fill_rand = gfshare_bad_idea_but_fill_rand_using_random; for (i=0; i<N; i++) { sharenrs[i] = i+1; shares[i] = malloc(datasize); } /* Stage 1, make a secret */ for( i = 0; i < datasize; ++i ) secret[i] = (rand() & 0xff00) >> 8; /* Stage 2, split it N ways with a threshold of M */ G = gfshare_ctx_init_enc( sharenrs, N, M, datasize ); gfshare_ctx_enc_setsecret( G, secret ); for (i=0; i<N; i++) gfshare_ctx_enc_getshare( G, i, shares[i] ); gfshare_ctx_free( G ); /* Prep the decode shape */ G = gfshare_ctx_init_dec( sharenrs, N, datasize ); memset(newsharenrs,0,N); int32_t j,r; for (i=0; i<M; i++) { r = rand() % N; while ( (j= sharenrs[r]) == 0 || newsharenrs[r] != 0 ) r = rand() % N; newsharenrs[r] = j; sharenrs[r] = 0; } for (i=0; i<N; i++) { if ( newsharenrs[i] != 0 ) gfshare_ctx_dec_giveshare( G, i, shares[i] ); //newsharenrs[i] = sharenrs[i]; } /* Stage 3, attempt a recombination with shares 1 and 2 */ //sharenrs[2] = 0; gfshare_ctx_dec_newshares( G, newsharenrs ); gfshare_ctx_dec_extract( G, recomb ); for( i = 0; i < datasize; ++i ) if( secret[i] != recomb[i] ) ok = 0; printf("M.%-3d N.%-3d ok.%d datalen.%d\n",M,N,ok,datasize); free(recomb), free(secret); for (i=0; i<N; i++) free(shares[i]); return ok!=1; }
PPH_ERROR pph_destroy_context(pph_context *context){ pph_account_node *current,*next; // check that we are given a valid pointer if(context == NULL){ return PPH_BAD_PTR; } // do child freeing. if(context->secret !=NULL){ free(context->secret); } if(context->account_data != NULL){ next = context->account_data; while(next!=NULL){ current=next; next=next->next; // free their entry list _destroy_entry_list(current->account.entries); free(current); } } if(context->share_context!=NULL){ gfshare_ctx_free(context->share_context); } // now it is safe to free the context free(context); return PPH_ERROR_OK; }
uint8_t *recoverdata(uint8_t *shares[],uint8_t *sharenrs,int32_t M,int32_t datasize,int32_t N) { void *G; int32_t i; uint8_t *recover,recovernrs[255]; if ( (recover= calloc(1,datasize)) == 0 ) { printf("cards777_recover: unexpected out of memory error\n"); return(0); } memset(recovernrs,0,sizeof(recovernrs)); for (i=0; i<N; i++) if ( shares[i] != 0 ) recovernrs[i] = sharenrs[i]; G = gfshare_ctx_init_dec(recovernrs,N,datasize); for (i=0; i<N; i++) if ( shares[i] != 0 ) gfshare_ctx_dec_giveshare(G,i,shares[i]); gfshare_ctx_dec_newshares(G,recovernrs); gfshare_ctx_dec_extract(G,recover); gfshare_ctx_free(G); return(recover); }
int sharefarm_read(const char *path, size_t *len, char **buf) { sharefarm_content *c = NULL, *cc = NULL; int ret, i; gfshare_ctx *gfc = NULL; unsigned char *sharenrs = NULL; unsigned char *sparebuf = NULL; if ((ret = find_all_shares(&c)) < 0) return ret; cc = find_content(c, path); if (cc == NULL) { free_content(c); return -ENOENT; } sharenrs = malloc(cc->sharecount); if (sharenrs == NULL) { free_content(c); return -ENOMEM; } for (i = 0; i < cc->sharecount; ++i) sharenrs[i] = (unsigned char)(cc->shares[i].sharenum); *len = cc->statinfo.st_size; *buf = calloc(1, *len); if (*buf == NULL) { ret = -ENOMEM; goto out; } mlock(*buf, *len); sparebuf = malloc(*len); if (sparebuf == NULL) { ret = -ENOMEM; goto out; } gfc = gfshare_ctx_init_dec(sharenrs, cc->sharecount, cc->statinfo.st_size); if (gfc == NULL) { ret = -ENOMEM; goto out; } for (i = 0; i < cc->sharecount; ++i) { int fd = open(cc->shares[i].fname, O_RDONLY); if (fd == -1) { ret = -errno; goto out; } if (read(fd, sparebuf, *len) != *len) { ret = -errno; close(fd); goto out; } close(fd); gfshare_ctx_dec_giveshare(gfc, i, sparebuf); } gfshare_ctx_dec_extract(gfc, (unsigned char *)*buf); out: if (ret < 0 ) { if (*buf != NULL) { munlock(*buf, *len); free(*buf); } } if (gfc != NULL) { gfshare_ctx_free(gfc); } free(sharenrs); free_content(c); return ret; }