File: test_gfshare_blockwise_simple.c

package info (click to toggle)
libgfshare 2.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,628 kB
  • sloc: sh: 11,402; ansic: 845; makefile: 63
file content (94 lines) | stat: -rw-r--r-- 3,337 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * This file is Copyright Daniel Silverstone <dsilvers@digital-scurf.org> 2006,2015
 *
 * 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 "libgfshare.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int
main( int argc, char **argv )
{
  int ok = 1, i;
  unsigned char* secret = malloc(512);
  unsigned char* share1 = malloc(512);
  unsigned char* share2 = malloc(512);
  unsigned char* share3 = malloc(512);
  unsigned char* recomb = malloc(512);
  unsigned char* sharenrs = (unsigned char*)strdup("012");
  gfshare_ctx *G;
  
  gfshare_fill_rand = gfshare_bad_idea_but_fill_rand_using_random;

  /* Stage 1, make a secret */
  for( i = 0; i < 512; ++i )
    secret[i] = (random() & 0xff00) >> 8;
  /* Stage 2, split it three ways with a threshold of 2 */
  G = gfshare_ctx_init_enc( sharenrs, 3, 2, 512 );
  gfshare_ctx_enc_setsecret( G, secret );
  gfshare_ctx_enc_getshare( G, 0, share1 );
  gfshare_ctx_enc_getshare( G, 1, share2 );
  gfshare_ctx_enc_getshare( G, 2, share3 );
  gfshare_ctx_free( G );
  /* Prep the decode shape */
  G = gfshare_ctx_init_dec( sharenrs, 3, 512 );
  gfshare_ctx_dec_giveshare( G, 0, share1 );
  gfshare_ctx_dec_giveshare( G, 1, share2 );
  gfshare_ctx_dec_giveshare( G, 2, share3 );
  /* 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;
  /* 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;
  /* 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;
  /* 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;
  gfshare_ctx_free( G );
  return ok!=1;
}