Class: Ccrypto::Ruby::ECCEngine

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/ruby/engines/ecc_engine.rb

Constant Summary collapse

NotAbleToFigureOutOnOpenSSLv2 =
[
  "Oakley-EC2N-3",
  "Oakley-EC2N-4"
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ ECCEngine

Returns a new instance of ECCEngine.

Raises:

  • (KeypairEngineException)


242
243
244
245
246
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 242

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)
  teLogger.debug "Config #{@config}"
end

Class Method Details

.supported_curvesObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 225

def self.supported_curves
  if @curves.nil?
    @curves = []
    OpenSSL::PKey::EC.builtin_curves.sort.map { |c| 
      
      next if c[0] =~ /^wap/ or NotAbleToFigureOutOnOpenSSLv2.include?(c[0])

      if c[0] == "prime256v1"
        @curves << Ccrypto::ECCConfig.new(c[0], Ccrypto::KeypairConfig::Algo_Active, true) 
      else
        @curves << Ccrypto::ECCConfig.new(c[0]) 
      end
    }
  end
  @curves
end

.verify(pubKey, val, sign) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 266

def self.verify(pubKey, val, sign)
  if OpenSSL::VERSION > "3.0.0"
    p pubKey.native_pubKey
    p pubKey.native_pubKey.methods.sort
    uPubKey = pubKey.native_pubKey
    if uPubKey.is_a?(OpenSSL::PKey::EC::Point)
      res = uPubKey.dsa_verify_asn1(val, sign)
      res
    else
      raise KeypairEngineException, "Unsupported public key type '#{uPubKey.class}'"
    end

  else
    # OpenSSL v2 - Ruby 2.x
    uPubKey = pubKey.native_pubKey
    if pubKey.native_pubKey.is_a?(OpenSSL::PKey::EC::Point)
      uPubKey = OpenSSL::PKey::EC.new(uPubKey.group)
      uPubKey.public_key = pubKey.native_pubKey
    end

    res = uPubKey.dsa_verify_asn1(val, sign)
    res
  end
end

Instance Method Details

#generate_keypair(&block) ⇒ Object



248
249
250
251
252
253
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 248

def generate_keypair(&block)
  teLogger.debug "Generating keypair of curve #{@config.curve}"
  kp = OpenSSL::PKey::EC.generate(@config.curve.to_s) 
  #teLogger.debug "Generated keypair #{kp.inspect}"
  ECCKeyBundle.new(kp)
end

#sign(val) ⇒ Object

Raises:

  • (KeypairEngineException)


255
256
257
258
259
260
261
262
263
264
# File 'lib/ccrypto/ruby/engines/ecc_engine.rb', line 255

def sign(val)
  raise KeypairEngineException, "Keypair is required" if @config.keypair.nil?
  raise KeypairEngineException, "ECC keypair is required" if not @config.keypair.is_a?(ECCKeyBundle)
  kp = @config.keypair
  
  res = kp.nativeKeypair.dsa_sign_asn1(val)
  teLogger.debug "Data of length #{val.length} signed "

  res
end