Module: RSA::PKCS1

Defined in:
lib/rsa/pkcs1.rb

Overview

Support for the PKCS #1 (aka RFC 3447) padding schemes.

See Also:

Class Method Summary (collapse)

Class Method Details

+ (String) i2osp(x, len = nil)

Converts a nonnegative integer into an octet string of a specified length.

This is the PKCS #1 I2OSP (Integer-to-Octet-String) primitive. Refer to PKCS #1 v2.1 pp. 8-9, section 4.1.

Examples:

RSA::PKCS1.i2osp(9_202_000, 2)    #=> ArgumentError: integer too large
RSA::PKCS1.i2osp(9_202_000, 3)    #=> "\x8C\x69\x50"
RSA::PKCS1.i2osp(9_202_000, 4)    #=> "\x00\x8C\x69\x50"

Parameters:

  • (Integer) x

    nonnegative integer to be converted

  • (Integer) len (defaults to: nil)

    intended length of the resulting octet string

Returns:

  • (String)

    octet string of length len

Raises:

  • (ArgumentError)

    if n is greater than 256len

See Also:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rsa/pkcs1.rb', line 26

def self.i2osp(x, len = nil)
  raise ArgumentError, "integer too large" if len && x >= 256**len

  StringIO.open do |buffer|
    while x > 0
      b = (x & 0xFF).chr
      x >>= 8
      buffer << b
    end
    s = buffer.string
    s.force_encoding(Encoding::BINARY) if s.respond_to?(:force_encoding)
    s.reverse!
    s = len ? s.rjust(len, "\0") : s
  end
end

+ (Integer) os2ip(x)

Converts an octet string into a nonnegative integer.

This is the PKCS #1 OS2IP (Octet-String-to-Integer) primitive. Refer to PKCS #1 v2.1 p. 9, section 4.2.

Examples:

RSA::PKCS1.os2ip("\x8C\x69\x50")  #=> 9_202_000

Parameters:

  • (String) x

    octet string to be converted

Returns:

  • (Integer)

    nonnegative integer

See Also:



54
55
56
# File 'lib/rsa/pkcs1.rb', line 54

def self.os2ip(x)
  x.bytes.inject(0) { |n, b| (n << 8) + b }
end

+ (Integer) rsadp(k, c)

Recovers the message representative from a ciphertext representative under the control of a private key.

This is the PKCS #1 RSADP decryption primitive. Refer to PKCS #1 v2.1 pp. 10-11, section 5.1.2.

Parameters:

  • (Key, #to_a) k

    RSA private key (n, d)

  • (Integer) c

    ciphertext representative, an integer between 0 and n - 1

Returns:

  • (Integer)

    message representative, an integer between 0 and n - 1

Raises:

  • (ArgumentError)

    if c is out of range

See Also:



88
89
90
91
92
# File 'lib/rsa/pkcs1.rb', line 88

def self.rsadp(k, c)
  n, d = k.to_a
  raise ArgumentError, "ciphertext representative out of range" unless c >= 0 && c < n
  m = Math.modpow(c, d, n)
end

+ (Integer) rsaep(k, m)

Produces a ciphertext representative from a message representative under the control of a public key.

This is the PKCS #1 RSAEP encryption primitive. Refer to PKCS #1 v2.1 p. 10, section 5.1.1.

Parameters:

  • (Key, #to_a) k

    RSA public key (n, e)

  • (Integer) m

    message representative, an integer between 0 and n - 1

Returns:

  • (Integer)

    ciphertext representative, an integer between 0 and n - 1

Raises:

  • (ArgumentError)

    if m is out of range

See Also:



70
71
72
73
74
# File 'lib/rsa/pkcs1.rb', line 70

def self.rsaep(k, m)
  n, e = k.to_a
  raise ArgumentError, "message representative out of range" unless m >= 0 && m < n
  c = Math.modpow(m, e, n)
end

+ (Integer) rsasp1(k, m)

Produces a signature representative from a message representative under the control of a private key.

This is the PKCS #1 RSASP1 signature primitive. Refer to PKCS #1 v2.1 pp. 12-13, section 5.2.1.

Parameters:

  • (Key, #to_a) k

    RSA private key (n, d)

  • (Integer) m

    message representative, an integer between 0 and n - 1

Returns:

  • (Integer)

    signature representative, an integer between 0 and n - 1

Raises:

  • (ArgumentError)

    if m is out of range

See Also:



106
107
108
109
110
# File 'lib/rsa/pkcs1.rb', line 106

def self.rsasp1(k, m)
  n, d = k.to_a
  raise ArgumentError, "message representative out of range" unless m >= 0 && m < n
  s = Math.modpow(m, d, n)
end

+ (Integer) rsavp1(k, s)

Recovers the message representative from a signature representative under the control of a public key.

This is the PKCS #1 RSAVP1 verification primitive. Refer to PKCS #1 v2.1 p. 13, section 5.2.2.

Parameters:

  • (Key, #to_a) k

    RSA public key (n, e)

  • (Integer) s

    signature representative, an integer between 0 and n - 1

Returns:

  • (Integer)

    message representative, an integer between 0 and n - 1

Raises:

  • (ArgumentError)

    if s is out of range

See Also:



124
125
126
127
128
# File 'lib/rsa/pkcs1.rb', line 124

def self.rsavp1(k, s)
  n, e = k.to_a
  raise ArgumentError, "signature representative out of range" unless s >= 0 && s < n
  m = Math.modpow(s, e, n)
end