/* * This file is Copyright Daniel Silverstone 2006 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * */ #include //#include "config.h" #include "OS_portable.h" #include "../includes/libgfshare.h" //#include "../includes/libgfshare_tables.h" #include #include #include #define XMALLOC malloc #define XFREE free struct _gfshare_ctx { uint32_t sharecount,threshold,size,buffersize; uint8_t sharenrs[255],buffer[]; }; uint8_t ctx_logs[256] = { 0x00, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b, 0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71, 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45, 0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6, 0x06, 0x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x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce, 0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc, 0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54, 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73, 0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, 0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41, 0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6, 0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09, 0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16, 0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e }; /*void _gfshare_fill_rand_using_random(uint8_t *buffer,unsigned long long count) { uint32_t i; for (i=0; i> 8; // apparently the bottom 8 aren't very random but the middles ones are }*/ //void randombytes(uint8_t *x,long xlen); //gfshare_rand_func_t gfshare_fill_rand = _gfshare_fill_rand_using_random; //gfshare_rand_func_t gfshare_fill_rand = OS_randombytes; // ------------------------------------------------------[ Preparation ]---- gfshare_ctx *_gfshare_ctx_init_core(uint8_t *sharenrs,uint32_t sharecount,uint8_t threshold,uint32_t size) { gfshare_ctx *ctx; ctx = XMALLOC(sizeof(struct _gfshare_ctx) + threshold * size); if ( ctx == NULL ) return(NULL); // errno should still be set from XMALLOC() ctx->sharecount = sharecount; ctx->threshold = threshold; ctx->size = size; memcpy(ctx->sharenrs,sharenrs,sharecount); ctx->buffersize = threshold * size; return(ctx); } // Initialise a gfshare context for producing shares gfshare_ctx *gfshare_ctx_init_enc(uint8_t *sharenrs,uint32_t sharecount,uint8_t threshold,uint32_t size) { uint32_t i; // can't have x[i] = 0 - that would just be a copy of the secret, in theory // in fact, due to the way we use exp/log for multiplication and treat log(0) as 0, it ends up as a copy of x[i] = 1 for (i=0; ithreshold = 0; return(ctx); } // Free a share context's memory. void gfshare_ctx_free(gfshare_ctx *ctx) { long len = sizeof(struct _gfshare_ctx) + ctx->buffersize; //gfshare_fill_rand((uint8_t*)ctx,len); OS_randombytes((uint8_t *)ctx,len); XFREE(ctx); } // --------------------------------------------------------[ Splitting ]---- // Provide a secret to the encoder. (this re-scrambles the coefficients) void gfshare_ctx_enc_setsecret(gfshare_ctx *ctx,uint8_t *secret) { memcpy(ctx->buffer + ((ctx->threshold-1) * ctx->size),secret,ctx->size); //gfshare_fill_rand(ctx->buffer,(ctx->threshold-1) * ctx->size); OS_randombytes(ctx->buffer,(ctx->threshold-1) * ctx->size); } // Extract a share from the context. 'share' must be preallocated and at least 'size' bytes long. // 'sharenr' is the index into the 'sharenrs' array of the share you want. void calc_share(uint8_t *buffer,int32_t size,int32_t M,uint32_t ilog,uint8_t *share) { uint32_t pos,coefficient;//,ilog = ctx_logs[ctx->sharenrs[sharenr]]; //uint8_t *coefficient_ptr = buffer; uint8_t *share_ptr,share_byte; for (pos=0; posbuffer,ctx->size,ctx->threshold,ctx_logs[ctx->sharenrs[sharenr]],share); } #ifdef notnow void calc_shares(uint8_t *shares,uint8_t *secret,int32_t size,int32_t width,int32_t M,int32_t N,uint8_t *sharenrs) { int32_t i; uint8_t *buffer = calloc(M,width); memset(shares,0,N*width); memcpy(buffer + ((M - 1) * size),secret,size); //gfshare_fill_rand(buffer,(M - 1) * size); OS_randombytes(buffer,(M - 1) * size); for (i=0; isharenrs,sharenrs,ctx->sharecount); } // Provide a share context with one of the shares. The 'sharenr' is the index into the 'sharenrs' array void gfshare_ctx_dec_giveshare(gfshare_ctx *ctx,uint8_t sharenr,uint8_t *share) { memcpy(ctx->buffer + (sharenr * ctx->size),share,ctx->size); } // Extract the secret by interpolation of the shares. secretbuf must be allocated and at least 'size' bytes long void gfshare_extract(uint8_t *secretbuf,uint8_t *sharenrs,int32_t N,uint8_t *buffer,int32_t size,int32_t width) { uint32_t i,j,Li_top,Li_bottom; uint8_t *secret_ptr,*share_ptr,sharei,sharej; memset(secretbuf,0,width); for (i=0; i= 0xff ) Li_top -= 0xff; Li_bottom += ctx_logs[sharei ^ sharej]; if ( Li_bottom >= 0xff ) Li_bottom -= 0xff; } if ( Li_bottom > Li_top ) Li_top += 0xff; Li_top -= Li_bottom; // Li_top is now log(L(i)) secret_ptr = secretbuf; share_ptr = buffer + (width * i); for (j=0; jsharenrs,ctx->sharecount,ctx->buffer,ctx->size,ctx->size); } int32_t init_sharenrs(uint8_t sharenrs[255],uint8_t *orig,int32_t m,int32_t n) { uint8_t *randvals,valid[255]; int32_t i,j,r,remains,orign; if ( m > n || n >= 0xff ) // reserve 255 for illegal sharei { printf("illegal M.%d of N.%d\n",m,n); return(-1); } randvals = calloc(1,65536); OS_randombytes(randvals,65536); memset(sharenrs,0,n); if ( orig == 0 && n == m ) { for (i=0; i<255; i++) valid[i] = (i + 1); remains = orign = 255; for (i=0; i> 8) % remains; sharenrs[i] = valid[r]; valid[r] = valid[--remains]; } /*while ( i < m ) { if ( j >= 65536 ) { gfshare_fill_rand(randvals,65536); printf("refill j.%d\n",j); j = 0; } r = (randvals[j++] % n); if ( valid[r] != 0 ) { remains--; i++; sharenrs[r] = valid[r]; //printf("%d ",sharenrs[i]); valid[r] = 0; } }*/ for (i=0; i> 8) % n) + 1; } err = 0; printf("err.%d\n",err); //#ifdef hardcoded_m_of_n_test int32_t ok; // Stage 3, attempt a recombination with shares 1 and 2 sharenrs[2] = 0; gfshare_ctx_dec_newshares(G,sharenrs); gfshare_ctx_dec_extract( G, recomb ); for( i = 0; i < 512; ++i ) if( secret[i] != recomb[i] ) ok = 0; printf("shares 1 + 2: ok.%d\n",ok); err += (ok == 0), ok = 1; // Stage 4, attempt a recombination with shares 1 and 3 sharenrs[2] = '2'; sharenrs[1] = 0; gfshare_ctx_dec_newshares( G, sharenrs ); gfshare_ctx_dec_extract( G, recomb ); for( i = 0; i < 512; ++i ) if( secret[i] != recomb[i] ) ok = 0; printf("shares 1 + 3: ok.%d\n",ok); err += (ok == 0), ok = 1; // Stage 5, attempt a recombination with shares 2 and 3 sharenrs[0] = 0; sharenrs[1] = '1'; gfshare_ctx_dec_newshares( G, sharenrs ); gfshare_ctx_dec_extract( G, recomb ); for( i = 0; i < 512; ++i ) if( secret[i] != recomb[i] ) ok = 0; printf("shares 2 + 3: ok.%d\n",ok); err += (ok == 0), ok = 1; // Stage 6, attempt a recombination with shares 1, 2 and 3 sharenrs[0] = '0'; gfshare_ctx_dec_newshares( G, sharenrs ); gfshare_ctx_dec_extract( G, recomb ); for( i = 0; i < 512; ++i ) if( secret[i] != recomb[i] ) ok = 0; printf("shares 1 + 2 + 3: ok.%d\n",ok); err += (ok == 0), ok = 1; gfshare_ctx_free( G ); printf("total error test_m_of_n %d\n",err); //#endif cleanup: for (i=0; i