Class: Oqs::SIG

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SIG

Returns a new instance of SIG.

Raises:



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

def initialize(name)
  @algo = name
  oqsSig = SIGWrapper.OQS_SIG_new(@algo) 
  raise Error, "Unable to create object '#{@algo}'. It is either the algorithm not supported or it is disabled at compile time." if oqsSig.null?
  @struct = OQS_SIG.new(oqsSig)
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/sig.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_signature_algoObject



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

def self.supported_signature_algo
  ttl = SIGWrapper.OQS_SIG_alg_count
  supported = []
  (0...ttl).each do |i|
     pName = SIGWrapper.OQS_SIG_alg_identifier(i)
     name = pName.to_s
     st = SIGWrapper.OQS_SIG_alg_is_enabled(name)
     if st
       supported << name
     end
  end

  supported
end

Instance Method Details

#algo_versionObject



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

def algo_version
  @struct.algo_version.to_s
end

#cleanupObject



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

def cleanup
  SIGWrapper.OQS_SIG_free(@struct) if not @struct.nil?
end

#free(obj) ⇒ Object



35
36
37
# File 'lib/oqs/sig.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
# File 'lib/oqs/sig.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 = SIGWrapper.OQS_SIG_keypair(@struct, pubKey, privKey)
  raise Error, "Error in generation of keypair" if rv != Oqs::OQS_SUCCESS

  [pubKey, privKey]
end

#intrinsic_nameObject



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

def intrinsic_name
  @struct.intrinsic_name.to_s
end

#sign(message, privKey) ⇒ Object

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/oqs/sig.rb', line 74

def sign(message, privKey)

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

  signature = Fiddle::Pointer.malloc(@struct.length_signature, Fiddle::RUBY_FREE)
  raise Error, "Unable to allocate memory for signature size #{@struct.length_signature}" if signature.null?
  signLen = Fiddle::Pointer.malloc(Fiddle::SIZEOF_SIZE_T, Fiddle::RUBY_FREE)
  raise Error, "Unable to allocate memory for signature length size #{Fiddle::SIZEOF_SIZE_T}" if signLen.null?

  pMessage = Fiddle::Pointer.to_ptr(message)

  rv = SIGWrapper.OQS_SIG_sign(@struct, signature, signLen, pMessage, message.length, privKey)
  raise Error, "Error in signing" if rv != Oqs::OQS_SUCCESS

  signBin = signature[0, signLen.ptr.to_i]

  signLen.free
  signature.free

  signBin
  
end

#verify(message, signature, pubKey) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/oqs/sig.rb', line 63

def verify(message,signature,pubKey)

  pMessage = Fiddle::Pointer.to_ptr(message)
  pSignature = Fiddle::Pointer.to_ptr(signature)
  
  rv = SIGWrapper.OQS_SIG_verify(@struct, pMessage, message.length, pSignature, signature.length, pubKey)

  rv == Oqs::OQS_SUCCESS

end