Class: Ccrypto::Ruby::RSAEngine

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/ccrypto/ruby/engines/rsa_engine.rb

Overview

RSAKeyBundle

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ RSAEngine

Returns a new instance of RSAEngine.

Raises:

  • (KeypairEngineException)


121
122
123
124
# File 'lib/ccrypto/ruby/engines/rsa_engine.rb', line 121

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

.encrypt(pubKey, val, &block) ⇒ Object

Raises:

  • (KeypairEngineException)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ccrypto/ruby/engines/rsa_engine.rb', line 161

def self.encrypt(pubKey, val, &block)
  raise KeypairEngineException, "Public key is required" if is_empty?(pubKey)

  padding = :oaep
  if block
    padding = block.call(:padding)
  end

  case padding
  when :pkcs1
    padVal = OpenSSL::PKey::RSA::PKCS1_PADDING
  when :oaep
    padVal = OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING
  when :no_padding
    padVal = OpenSSL::PKey::RSA::NO_PADDING
  else
    raise KeypairEngineException, "Padding requires either :pkcs1 or :oaep. Default is :oaep"
  end

  pubKey.public_encrypt(val, padVal)
end

.supported_keysizesObject



207
208
209
210
211
212
213
214
# File 'lib/ccrypto/ruby/engines/rsa_engine.rb', line 207

def self.supported_keysizes
  [
    Ccrypto::RSAConfig.new(1024, Ccrypto::KeypairConfig::Algo_NotRecommended), 
    Ccrypto::RSAConfig.new(2048, Ccrypto::KeypairConfig::Algo_Active, true), 
    Ccrypto::RSAConfig.new(4096), 
    Ccrypto::RSAConfig.new(8192)
  ] 
end

.verify(pubKey, val, sign, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ccrypto/ruby/engines/rsa_engine.rb', line 146

def self.verify(pubKey, val, sign, &block)
  if block
    pss = block.call(:pss_mode)
    pss = false if is_empty?(pss) or not is_bool?(pss)

    if pss
      verify_pss(pubKey, val, sign, &block)
    else
      verify_typical(pubKey, val, sign, &block)
    end
  else
    verify_typical(pubKey, val, sign, &block)
  end
end

Instance Method Details

#decrypt(enc, &block) ⇒ Object

Raises:

  • (KeypairEngineException)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ccrypto/ruby/engines/rsa_engine.rb', line 183

def decrypt(enc, &block)

  raise KeypairEngineException, "Private key is required" if not @config.has_private_key? 
  raise KeypairEngineException, "RSA private key is required" if not @config.private_key.is_a?(RSAPrivateKey)

  padding = :oaep
  if block
    padding = block.call(:padding)
  end

  case padding
  when :pkcs1
    padVal = OpenSSL::PKey::RSA::PKCS1_PADDING
  when :oaep
    padVal = OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING
  when :no_padding
    padVal = OpenSSL::PKey::RSA::NO_PADDING
  else
    raise KeypairEngineException, "Padding requires either :pkcs1 or :oaep. Default is :oaep"
  end

  @config.private_key.private_decrypt(enc, padVal)
end

#generate_keypair(&block) ⇒ Object



126
127
128
129
# File 'lib/ccrypto/ruby/engines/rsa_engine.rb', line 126

def generate_keypair(&block)
  kp = OpenSSL::PKey::RSA.generate(@config.keysize)
  RSAKeyBundle.new(kp)
end

#sign(val, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ccrypto/ruby/engines/rsa_engine.rb', line 131

def sign(val, &block)
  if block
    pss = block.call(:pss_mode)
    pss = false if is_empty?(pss) or not is_bool?(pss)

    if pss
      sign_pss(val, &block)
    else
      sign_typical(val, &block)
    end
  else
    sign_typical(val, &block)
  end
end