Module: Tem::Toolkit

Included in:
Session
Defined in:
lib/tem/toolkit.rb

Instance Method Summary collapse

Instance Method Details

#tk_gen_key(type = :asymmetric, authz = nil) ⇒ Object



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
# File 'lib/tem/toolkit.rb', line 2

def tk_gen_key(type = :asymmetric, authz = nil)
  gen_sec = assemble do |s|
    s.ldbc authz.nil? ? 24 : 4
    s.outnew
    if authz.nil?
      # No authorization given, must generate one.
      s.ldbc 20
      s.ldwc :key_auth
      s.dupn :n => 2
      s.rnd
      s.outvb
    end
    s.genkp :type => (type == :asymmetric) ? 0x00 : 0x80
    s.authk :auth => :key_auth
    s.outw
    if type == :asymmetric
      s.authk :auth => :key_auth 
      s.outw
    end
    s.halt
    s.label :key_auth
    if authz.nil?
      s.zeros :tem_ubyte, 20
    else
      s.data :tem_ubyte, authz
    end
    s.stack 4
  end
  
  kp_buffer = execute gen_sec
  keys_offset = authz.nil? ? 20 : 0
  k1id = read_tem_ushort kp_buffer, keys_offset    
  k2id = read_tem_ushort kp_buffer, keys_offset + 2 if type == :asymmetric
  if type == :asymmetric 
    return_val = { :pubk_id => k1id, :privk_id => k2id }
  else
    return_val = { :key_id => k1id }
  end
  return { :authz => authz || kp_buffer[0...20] }.merge!(return_val)
end

#tk_post_key(key, authz) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tem/toolkit.rb', line 62

def tk_post_key(key, authz)
  post_sec = assemble do |s|
    s.ldbc :const => 1
    s.outnew
    s.ldwc :const => :key_data
    s.rdk
    s.authk :auth => :key_auth
    s.outb
    s.halt
    s.label :key_data
    s.data :tem_ubyte, key.to_tem_key
    s.label :key_auth
    s.data :tem_ubyte, authz
    s.stack 4
  end
  id_string = execute post_sec
  return read_tem_ubyte(id_string, 0)
end

#tk_read_key(key_id, authz) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tem/toolkit.rb', line 43

def tk_read_key(key_id, authz)
  read_sec = assemble do |s|
    s.ldbc :const => key_id
    s.authk :auth => :key_auth
    s.ldkl
    s.outnew
    s.ldbc :const => key_id
    s.ldbc(-1)
    s.stk
    s.halt
    s.label :key_auth
    s.data :tem_ubyte, authz
    s.stack 4
  end
  
  key_string = execute read_sec
  return read_tem_key(key_string, 0)
end