Class: Roqs::KEM

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ KEM

Returns a new instance of KEM.

Raises:



26
27
28
29
30
31
# File 'lib/roqs/kem.rb', line 26

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



49
50
51
# File 'lib/roqs/kem.rb', line 49

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/roqs/kem.rb', line 11

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



45
46
47
# File 'lib/roqs/kem.rb', line 45

def algo_version
  @struct.algo_version.to_s
end

#cleanupObject



33
34
35
# File 'lib/roqs/kem.rb', line 33

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

#derive_decapsulation_key(cipherBin, privKey) ⇒ Object

Raises:



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

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 != Roqs::OQS_SUCCESS

  encpKeyBin = encpKey[0,encpKey.size]

  encpKey.free

  encpKeyBin
  
end

#derive_encapsulation_key(pubKey) ⇒ Object

Raises:



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

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 != Roqs::OQS_SUCCESS

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

  cipher.free
  encpKey.free

  [encpKeyBin, cipherBin]

end

#free(obj) ⇒ Object



37
38
39
# File 'lib/roqs/kem.rb', line 37

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

#genkeypairObject

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/roqs/kem.rb', line 53

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 != Roqs::OQS_SUCCESS

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

  [KEMPublicKey.new(pubKey), privKey]
end

#intrinsic_nameObject



41
42
43
# File 'lib/roqs/kem.rb', line 41

def intrinsic_name
  @struct.intrinsic_name.to_s
end