Class: Ronin::Support::Crypto::Key::RSA

Inherits:
OpenSSL::PKey::RSA
  • Object
show all
Includes:
Methods
Defined in:
lib/ronin/support/crypto/key/rsa.rb

Overview

Represents an RSA key.

Constant Summary collapse

PADDINGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Mapping of padding names to pdding constants.

Since:

  • 1.0.0

{
  pkcs1_oaep: PKCS1_OAEP_PADDING,
  pkcs1:      PKCS1_PADDING,
  sslv23:     SSLV23_PADDING,

  nil      => NO_PADDING,
  false    => NO_PADDING
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

included, #save

Class Method Details

.generate(key_size = 4096, *arguments, &block) ⇒ RSA

Generates a new random RSA key.

Parameters:

Returns:

  • (RSA)

    The newly generated key.

Since:

  • 1.0.0



51
52
53
54
55
56
57
# File 'lib/ronin/support/crypto/key/rsa.rb', line 51

def self.generate(key_size=4096,*arguments,&block)
  # HACK: openssl-3.0.0 will return an OpenSSL::PKey::RSA instance,
  # even though we subclassed OpenSSL::PKey::RSA.
  new_key = allocate
  new_key.send(:initialize_copy,super(key_size,*arguments,&block))
  new_key
end

Instance Method Details

#dOpenSSL::BN

The d variable for the RSA key.

Returns:

  • (OpenSSL::BN)

See Also:

Since:

  • 1.0.0



88
89
90
# File 'lib/ronin/support/crypto/key/rsa.rb', line 88

def d
  super()
end

#eOpenSSL::BN

The e variable for the RSA key.

Returns:

  • (OpenSSL::BN)

See Also:

Since:

  • 1.0.0



77
78
79
# File 'lib/ronin/support/crypto/key/rsa.rb', line 77

def e
  super()
end

#nOpenSSL::BN

The n variable for the RSA key.

Returns:

  • (OpenSSL::BN)

See Also:

Since:

  • 1.0.0



66
67
68
# File 'lib/ronin/support/crypto/key/rsa.rb', line 66

def n
  super()
end

#private_decrypt(data, padding: :pkcs1) ⇒ String

Decrypts data using the private key.

Examples:

encrypted = "i;k\x89\xE9\x92\xA5\xAB\xBAc\xC6;\r\xB7\x18W\x11\x02\xCBf.\xC2\x87\xDF\xDD[|\xF0\x97\x15\xC6\xCF\xCD\x93\x1C\x11S&L\x89\xE6\xCA\xC9\xAD\xAD\x1F\xE6\x8D\x86\xF3$\x8BfS(3\x9F\x7F\xEFZ \xB7\xDC{f\xF1\xB7-\x18\x94\xB8}\x93%,{X\x85\xBD(\xBD\xAD\x00,O\xAC\xECJ}\x99\xC7\xE2\xB6\x11\x9D\xDF\x12\xA5\x8F|\xF8\xC3Q\xDA\x95\x12\xEFH\xFFt\xCD\x854jJ\xE9\xE7\xC4\xDD|\xD4}w\xDAJ8\xAE\x17"
rsa = Crypto::Key::RSA.load_file('key.pem')
rsa.private_decrypt(encrypted, padding: pkcs1_oaep)
# => "top secret"

Parameters:

  • data (String)

    The data to decrypt.

  • padding (:pkcs1_oaep, :pkcs1, :sslv23, nil, false) (defaults to: :pkcs1)

    Optional padding mode. nil and false will disable padding.

Returns:

  • (String)

    The decrypted data.

Since:

  • 1.0.0



157
158
159
160
161
162
163
# File 'lib/ronin/support/crypto/key/rsa.rb', line 157

def private_decrypt(data, padding: :pkcs1)
  padding_const = PADDINGS.fetch(padding) do
    raise(ArgumentError,"padding must be #{PADDINGS.keys.map(&:inspect).join(', ')}: #{padding.inspect}")
  end

  super(data,padding_const)
end

#public_encrypt(data, padding: :pkcs1) ⇒ String

Encrypts data using the public key.

Examples:

rsa = Crypto::Key::RSA.load_file('key.pem')
rsa.public_encrypt("top secret", padding: :pkcs1_oaep)
# => "i;k\x89\xE9\x92\xA5\xAB\xBAc\xC6;\r\xB7\x18W\x11\x02\xCBf.\xC2\x87\xDF\xDD[|\xF0\x97\x15\xC6\xCF\xCD\x93\x1C\x11S&L\x89\xE6\xCA\xC9\xAD\xAD\x1F\xE6\x8D\x86\xF3$\x8BfS(3\x9F\x7F\xEFZ \xB7\xDC{f\xF1\xB7-\x18\x94\xB8}\x93%,{X\x85\xBD(\xBD\xAD\x00,O\xAC\xECJ}\x99\xC7\xE2\xB6\x11\x9D\xDF\x12\xA5\x8F|\xF8\xC3Q\xDA\x95\x12\xEFH\xFFt\xCD\x854jJ\xE9\xE7\xC4\xDD|\xD4}w\xDAJ8\xAE\x17"

Parameters:

  • data (String)

    The data to encrypt.

  • padding (:pkcs1_oaep, :pkcs1, :sslv23, nil, false) (defaults to: :pkcs1)

    Optional padding mode. nil and false will disable padding.

Returns:

  • (String)

    The encrypted data.

Since:

  • 1.0.0



131
132
133
134
135
136
137
# File 'lib/ronin/support/crypto/key/rsa.rb', line 131

def public_encrypt(data, padding: :pkcs1)
  padding_const = PADDINGS.fetch(padding) do
    raise(ArgumentError,"padding must be #{PADDINGS.keys.map(&:inspect).join(', ')}: #{padding.inspect}")
  end

  super(data,padding_const)
end

#sizeInteger

The size of the RSA key in bits.

Returns:

  • (Integer)

    The key size in bits.

Since:

  • 1.0.0



98
99
100
# File 'lib/ronin/support/crypto/key/rsa.rb', line 98

def size
  n.num_bits
end