Class: Ccrypto::Java::ECCEngine

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

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

Constructor Details

#initialize(*args, &block) ⇒ ECCEngine

Returns a new instance of ECCEngine.

Raises:

  • (KeypairEngineException)


184
185
186
187
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 184

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

.supported_curvesObject



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

def self.supported_curves
  if @curves.nil?
    @curves = org.bouncycastle.asn1.x9.ECNamedCurveTable.getNames.sort.to_a.map { |c| Ccrypto::ECCConfig.new(c) }
  end
  @curves
end

.verify(pubKey, val, sign) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 244

def self.verify(pubKey, val, sign)
  ver = java.security.Signature.getInstance("SHA256WithECDSA")
  ver.initVerify(pubKey)
  teLogger.debug "Verifing data : #{val}"
  case val
  when java.io.InputStream
    buf = Java::byte[102400].new
    while((read = val.read(buf, 0 ,buf.length)) != nil)
      ver.update(buf,0, read)
    end
  else
    ver.update(to_java_bytes(val))
  end

  ver.verify(to_java_bytes(sign))
end

Instance Method Details

#generate_keypair(&block) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 189

def generate_keypair(&block)

  algoName = "ECDSA"
  prov = Ccrypto::Java::JCEProvider::BCProv
  randomEngine = java.security.SecureRandom.new
  if block
    # it is the responsibility of caller program to add the 
    # provider into the provider list.
    # Here provider string shall be used
    uprov = block.call(:jce_provider)
    prov = uprov if not is_empty?(uprov) 

    uAlgo = block.call(:jce_algo_name)
    algoName = uAlgo if not is_empty?(uAlgo)

    uRandEng = block.call(:random_engine)
    randomEngine = uRandEng if not uRandEng.nil?
  end

  kpg = java.security.KeyPairGenerator.getInstance(algoName, prov)
  #kpg.java_send :initialize, [java.security.spec.AlgorithmParameterSpec, java.security.SecureRandom], java.security.spec.ECGenParameterSpec.new(curve), java.security.SecureRandom.new
  kpg.java_send :initialize, [java.security.spec.AlgorithmParameterSpec, randomEngine.class], java.security.spec.ECGenParameterSpec.new(@config.curve), randomEngine
  kp = kpg.generate_key_pair

  kb = ECCKeyBundle.new(kp)
  kb

end

#regenerate_keypair(pubKey, privKey, &block) ⇒ Object



218
219
220
221
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 218

def regenerate_keypair(pubKey, privKey, &block)
  kp = java.security.KeyPair.new(pubKey, privKey) 
  ECCKeyBundle.new(kp)
end

#sign(val, &block) ⇒ Object

Raises:

  • (KeypairEngineException)


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

def sign(val, &block)
  raise KeypairEngineException, "Keypair is required" if @config.keypair.nil?
  raise KeypairEngineException, "ECC keypair is required. Given #{@config.keypair}" if not @config.keypair.is_a?(ECCKeyBundle)
  kp = @config.keypair

  sign = java.security.Signature.getInstance("SHA256WithECDSA")
  sign.initSign(kp.private_key)
  teLogger.debug "Signing data : #{val}" 
  case val
  when java.io.InputStream
    buf = Java::byte[102400].new
    while((read = val.read(buf, 0, buf.length)) != nil)
      sign.update(buf,0,read)
    end
  else
    sign.update(to_java_bytes(val))
  end

  sign.sign
end