Class: Oqs::KEM

Inherits:
Object
  • Object
show all
Defined in:
lib/oqs/kem.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ KEM

Returns a new instance of KEM.

Raises:



24
25
26
27
28
29
# File 'lib/oqs/kem.rb', line 24

def initialize(name)
  @algo = name
  oqsKem = KEMWrapper.OQS_KEM_new(@algo) 
  raise Error, "Unable to create object '#{@algo}'. It is either the algorithm not supported or it is disabled at compile time." if oqsKem.null?
  @struct = OQS_KEM.new(oqsKem)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object



47
48
49
# File 'lib/oqs/kem.rb', line 47

def method_missing(mtd, *args, &block)
  @struct.send(mtd) if not @struct.nil? and @struct.respond_to?(mtd)
end

Class Method Details

.supported_kem_algoObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/oqs/kem.rb', line 9

def self.supported_kem_algo
  ttl = KEMWrapper.OQS_KEM_alg_count
  supported = []
  (0...ttl).each do |i|
     pName = KEMWrapper.OQS_KEM_alg_identifier(i)
     name = pName.to_s
     st = KEMWrapper.OQS_KEM_alg_is_enabled(name)
     if st
       supported << name
     end
  end

  supported
end

Instance Method Details

#algo_versionObject



43
44
45
# File 'lib/oqs/kem.rb', line 43

def algo_version
  @struct.algo_version.to_s
end

#cleanupObject



31
32
33
# File 'lib/oqs/kem.rb', line 31

def cleanup
  KEMWrapper.OQS_KEM_free(@struct) if not @struct.nil?
end

#derive_decapsulation_key(cipherBin, privKey) ⇒ Object

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/oqs/kem.rb', line 87

def derive_decapsulation_key(cipherBin, privKey)

  raise Error, "Cipher cannot be empty" if cipherBin.nil?
  raise Error, "Private key cannot be nil" if privKey.nil?

  encpKey = Fiddle::Pointer.malloc(@struct.length_shared_secret, Fiddle::RUBY_FREE)
  raise Error, "Unable to allocate memory for shared secret size #{@struct.length_shared_secret}" if encpKey.null?
  
  rv = KEMWrapper.OQS_KEM_decaps(@struct, encpKey , cipherBin, privKey)
  raise Error, "Error in decapsulation" if rv != Oqs::OQS_SUCCESS

  encpKeyBin = encpKey[0,encpKey.size]

  encpKey.free

  encpKeyBin
  
end

#derive_encapsulation_key(pubKey) ⇒ Object

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/oqs/kem.rb', line 66

def derive_encapsulation_key(pubKey)

  cipher = Fiddle::Pointer.malloc(@struct.length_ciphertext, Fiddle::RUBY_FREE)
  raise Error, "Unable to allocate memory for ciphertext size #{@struct.length_ciphertext}" if cipher.null?

  encpKey = Fiddle::Pointer.malloc(@struct.length_shared_secret, Fiddle::RUBY_FREE)
  raise Error, "Unable to allocate memory for shared secret size #{@struct.length_shared_secret}" if encpKey.null?
  
  rv = KEMWrapper.OQS_KEM_encaps(@struct, cipher, encpKey, pubKey)
  raise Error, "Error in encapsulation" if rv != Oqs::OQS_SUCCESS

  encpKeyBin = encpKey[0,encpKey.size]
  cipherBin = cipher[0,cipher.size]

  cipher.free
  encpKey.free

  [encpKeyBin, cipherBin]

end

#free(obj) ⇒ Object



35
36
37
# File 'lib/oqs/kem.rb', line 35

def free(obj)
  obj.free if not (obj.nil? and obj.null?)
end

#genkeypairObject

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/oqs/kem.rb', line 51

def genkeypair
  pubKey = Fiddle::Pointer.malloc(@struct.length_public_key, Fiddle::RUBY_FREE)
  raise Error, "Unable to allocate memory for public key size #{@struct.length_public_key}" if pubKey.null?
  privKey = Fiddle::Pointer.malloc(@struct.length_secret_key, Fiddle::RUBY_FREE)
  raise Error, "Unable to allocate memory for secret key size #{@struct.length_secret_key}" if privKey.null?

  rv = KEMWrapper.OQS_KEM_keypair(@struct, pubKey, privKey)
  raise Error, "Error in generation of keypair" if rv != Oqs::OQS_SUCCESS

  #pubKeyBin = pubKey[0, pubKey.size]
  #privKeyBin = privKey[0, privKey.size]

  [pubKey, privKey]
end

#intrinsic_nameObject



39
40
41
# File 'lib/oqs/kem.rb', line 39

def intrinsic_name
  @struct.intrinsic_name.to_s
end