Class: Ccrypto::Java::CrystalDilithiumEngine

Inherits:
Object
  • Object
show all
Includes:
DataConversion, TR::CondUtils
Defined in:
lib/ccrypto/java/engines/crystal_dilithium_engine.rb

Overview

class CrystalDilithiumKeyBundle

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_b64_mime, #from_hex, included, #logger, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(*args, &block) ⇒ CrystalDilithiumEngine

Returns a new instance of CrystalDilithiumEngine.

Raises:

  • (KeypairEngineException)


177
178
179
180
# File 'lib/ccrypto/java/engines/crystal_dilithium_engine.rb', line 177

def initialize(*args, &block)
  @config = args.first
  raise KeypairEngineException, "1st parameter must be a #{Ccrypto::KeypairConfig.class} object" if not @config.is_a?(Ccrypto::KeypairConfig)
end

Class Method Details

.find_config(conf) ⇒ Object



169
170
171
# File 'lib/ccrypto/java/engines/crystal_dilithium_engine.rb', line 169

def self.find_config(conf)
  supported_configs.select { |k,v| k == conf }.first[1]
end

.loggerObject



173
174
175
# File 'lib/ccrypto/java/engines/crystal_dilithium_engine.rb', line 173

def self.logger
  Ccrypto::Java.logger(:dilithium_engine)
end

.supported_configsObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ccrypto/java/engines/crystal_dilithium_engine.rb', line 150

def self.supported_configs
  if @supportedConfig.nil?
    @supportedConfig = {}
    {
      dilithium2: DilithiumParameters::dilithium2,
      dilithium3: DilithiumParameters::dilithium3,
      dilithium5: DilithiumParameters::dilithium5,
      dilithium2_aes: DilithiumParameters::dilithium2_aes,
      dilithium3_aes: DilithiumParameters::dilithium3_aes,
      dilithium5_aes: DilithiumParameters::dilithium5_aes,
    }.each do |k,kp|
      conf = Ccrypto::CrystalDilithiumConfig.new(k)
      conf.provider_config = { params: kp.dup }
      @supportedConfig[k] = conf
    end
  end 
  @supportedConfig.freeze
end

.supported_paramsObject



146
147
148
# File 'lib/ccrypto/java/engines/crystal_dilithium_engine.rb', line 146

def self.supported_params
  supported_configs.keys
end

.verify(pubKey, data, sign) ⇒ Object

Raises:

  • (KeypairEngineException)


211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ccrypto/java/engines/crystal_dilithium_engine.rb', line 211

def self.verify(pubKey, data, sign)
  raise KeypairEngineException, "Given public key to verify is empty" if is_empty?(pubKey)
  raise KeypairEngineException, "Given data to verify is empty" if is_empty?(data)
  raise KeypairEngineException, "Given signature to verify is empty" if is_empty?(sign)

  ver = DilithiumSigner.new
  puts "verifying public key #{pubKey.inspect}"
  ver.init(false, pubKey.native)

  ver.verifySignature(to_java_bytes(data), to_java_bytes(sign))
end

Instance Method Details

#generate_keypair(&block) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ccrypto/java/engines/crystal_dilithium_engine.rb', line 182

def generate_keypair(&block)

  rand = java.security.SecureRandom.getInstanceStrong
  kpg = DilithiumKeyPairGenerator.new
  #logger.debug "CD config  : #{@config.inspect}"
  #logger.debug "CD config  : #{DilithiumKeyGenerationParameters}"
  #logger.debug "CD provider config  : #{@config.provider_config}"
  #logger.debug "CD provider config params  : #{@config.provider_config[:params]}"
  #logger.debug "CD provider config params  : #{DilithiumParameters::dilithium2}"
  param = DilithiumKeyGenerationParameters.new(rand, @config.provider_config[:params])
  logger.debug "CD param  : #{param.inspect}"
  kpg.init(param)

  CrystalDilithiumKeyBundle.new(kpg.generateKeyPair(), @config)

end

#sign(data, &block) ⇒ Object

Raises:

  • (KeypairEngineException)


199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ccrypto/java/engines/crystal_dilithium_engine.rb', line 199

def sign(data, &block)

  raise KeypairEngineException, "Keypair is required" if @config.keypair.nil?
  raise KeypairEngineException, "Crystal Dilithium keypair is required. Given #{@config.keypair}" if not @config.keypair.is_a?(CrystalDilithiumKeyBundle)
  kp = @config.keypair
  
  signer = DilithiumSigner.new
  signer.init(true, kp.private_key.native)
  signer.generateSignature(to_java_bytes(data))

end