Class: CcipherFactory::SymKeyKeystore

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/ccipher_factory/symkey_keystore/symkey_keystore.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_encoded(bin, &block) ⇒ Object

Raises:



6
7
8
9
10
11
12
# File 'lib/ccipher_factory/symkey_keystore/symkey_keystore.rb', line 6

def self.from_encoded(bin, &block)
  
  raise SymKeyCipherError, "Block is required" if not block

  ts = BinStruct.instance.struct_from_bin(bin)
  from_tspec(ts, &block)
end

.from_tspec(ts, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ccipher_factory/symkey_keystore/symkey_keystore.rb', line 14

def self.from_tspec(ts, &block)
  
  sk = CcipherFactory::SymKey.from_encoded(ts.symkey_derived) do |ops|
    case ops
    when :password
      block.call(:password)
    end
  end

  dec = CcipherFactory::SymKeyCipher.att_decryptor
  decOut = MemBuf.new
  dec.output(decOut)
  dec.key = sk
  dec.att_decrypt_init
  dec.att_decrypt_update(ts.symkey_cipher)
  dec.att_decrypt_final

  CcipherFactory::SymKey.from_encoded(decOut.bytes)

end

Instance Method Details

#to_keystore(key, &block) ⇒ Object

Raises:



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
# File 'lib/ccipher_factory/symkey_keystore/symkey_keystore.rb', line 35

def to_keystore(key, &block)
 
  raise SymKeyCipherError, "Key is required" if is_empty?(key)
  raise SymKeyCipherError, "Block is required" if not block

  # 1. Derive session key from user password
  sk = CcipherFactory::SymKeyGenerator.derive(:aes, 256) do |ops|
    case ops
    when :password
      pass = block.call(:password)
      if is_empty?(pass)
        raise SymKeyCipherError, "Password is required" 
      end
      pass
    end
  end

  # 2. Encrypt the given key with session key
  enc = CcipherFactory::SymKeyCipher.att_encryptor 
  enc.mode = :gcm
  enc.key = sk

  encOut = MemBuf.new
  enc.output(encOut)

  key.attach_mode

  enc.att_encrypt_init
  enc.att_encrypt_update(key.encoded)
  enc.att_encrypt_final

  ts = BinStruct.instance.struct(:symkey_keystore)
  ts.symkey_derived = sk.encoded
  ts.symkey_cipher = encOut.bytes
  ts.symkey = "testing"
  ts.encoded

end