Module: CcipherFactory::ShamirSharingHelper

Included in:
SymKey
Defined in:
lib/ccipher_factory/shamir/shamir_sharing_helper.rb

Defined Under Namespace

Classes: InvalidShare, NotEnoughShare, ShamirSharingError

Instance Method Summary collapse

Instance Method Details

#shamir_recover(shares) ⇒ Object

Raises:



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
# File 'lib/ccipher_factory/shamir/shamir_sharing_helper.rb', line 47

def shamir_recover(shares)

  shares = [shares] if not shares.is_a?(Array)
  shares = [] if is_empty?(shares)

  reqShare = nil
  res = { }
  foundSerial = nil
  shares.each do |s|
    ts = BinStruct.instance.struct_from_bin(s)

    raise ShamirSharingError, "Not a shared secret envelope [#{ts.id}]" if ts.oid != BTag.constant_value(:shared_secret)

    serial = ts.serial
    raise InvalidShare, "Given share not in same batch. Cannot proceed" if not_empty?(foundSerial) and serial != foundSerial

    rs = ts.req_share
    raise ShamirSharingError, "Inconsistancy required shares value in given shares" if not_empty?(reqShare) and rs != reqShare
    reqShare = rs  

    sid = ts.share_id
    if not res.keys.include?(sid)
      val = ts.shared_value
      #res[sid.to_i] = val.chars.map(&:ord)
      res[sid.to_i] = val
    end

    foundSerial = serial
  end

  raise NotEnoughShare, "Required #{reqShare} share(s) but only #{res.size} is/are given" if res.size < reqShare or res.size == 0

  #ssc = Ccrypto::SecretSharingConfig.new
  #ssc.required_parts = reqShare
  #ss = Ccrypto::AlgoFactory.engine(ssc)
  ss = Ccrypto::AlgoFactory.engine(Ccrypto::SecretSharingConfig)
  ss.combine(reqShare, res)

end

#shamir_split(data, totalShare, reqShare) ⇒ Object



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
# File 'lib/ccipher_factory/shamir/shamir_sharing_helper.rb', line 11

def shamir_split(data, totalShare, reqShare)

  rand = Ccrypto::AlgoFactory.engine(Ccrypto::SecureRandomConfig)

  ssc = Ccrypto::SecretSharingConfig.new
  ssc.split_into = totalShare
  ssc.required_parts = reqShare
  ss = Ccrypto::AlgoFactory.engine(ssc)

  serial = rand.random_bytes(8)
  shares = ss.split(data)
  shares = shares.map { |s| 
    ts = BinStruct.instance.struct(:shared_secret)
    ts.req_share = reqShare
    ts.share_id = s[0]
    ts.serial = serial
    ts.shared_value = s[1]
    ts.encoded
  }

  #shares = []
  #(1..totalShare).each do |i|
  #  share = ss.compute_share(i)
  #  ts = Encoding::ASN1Encoder.instance(:shared_secret)
  #  ts.set(:req_share, reqShare)
  #  ts.set(:share_id, share[0])
  #  ts.set(:serial, serial)
  #  sbin = share[1].map { |v| v.chr }.join
  #  ts.set(:shared_value, sbin) 
  #  shares << ts.to_asn1
  #end

  shares

end