Primitive types such as int or double store numbers in exactly one or two bytes, with finite precision. This suffices for most applications, but cryptographic methods require arithmetic on much larger numbers and without loss of precision. Therefore OpenSSL provides a bignum data type which holds arbitrary sized integers and implements all basic arithmetic and comparison operators such as +, -, *, ^, %%, %/%, ==, !=, <, <=, > and >=.

One special case, the modular exponenent a^b %% m can be calculated using bignum_mod_exp when b is too large for calculating a^b.

# create a bignum
y <- bignum("123456789123456789")
z <- bignum("D41D8CD98F00B204E9800998ECF8427E", hex = TRUE)

# size grows
print(y * z)
## [b] 34808613111804879213872650915812112647840354642904626774
# Basic arithmetic
div <- z %/% y
mod <- z %% y
z2 <- div * y + mod
stopifnot(z2 == z)
stopifnot(div < z)

RSA involves a public key and a private key. The public key should be known by everyone and is used for encrypting messages. Messages encrypted with the public key can only be decrypted in a reasonable amount of time using the private key. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers.

RSA key generation

An RSA key-pair is generated as follows (adapted from wikipedia):

OpenSSL has a key generator that does these things for us.

(key <- rsa_keygen(256))
## [256-bit rsa private key]
## md5: 55:c9:47:96:c2:a2:03:92:d7:14:71:46:72:7e:18:c3
(pubkey <- as.list(key)$pubkey)
## [256-bit rsa public key]
## md5: 55:c9:47:96:c2:a2:03:92:d7:14:71:46:72:7e:18:c3

Usually we would use rsa_encrypt and rsa_decrypt to perform the encryption:

msg <- charToRaw("hello world")
ciphertext <- rsa_encrypt(msg, pubkey)
rawToChar(rsa_decrypt(ciphertext, key))
## [1] "hello world"

Let’s look at how this works under the hood.

How RSA encryption works

Using as.list on the private key we can inspect the underlying bignum integers:

keydata <- as.list(key)$data
print(keydata)
## $e
## [b] 65537
## $n
## [b] 96750873037603376742587788301199610689985029931179594415199836810748932629931
## $p
## [b] 318867163610130322574768205961601889833
## $q
## [b] 303420621747988691201955840375907446707
## $d
## [b] 51965008024835419096679587838964650445787395265772249784550613688561913116673
## $dp
## [b] 218998884618697467833634309146553285369
## $dq
## [b] 83261614988721311969970762062961678465
## $qi
## [b] 6311019195086584815711598220306716290

You can verify that the equations above hold for this key. The public key is simply a subset of the key which only contains \(n\) and \(e\):

as.list(pubkey)$data
## $e
## [b] 65537
## $n
## [b] 96750873037603376742587788301199610689985029931179594415199836810748932629931

In order to encrypt a message into ciphertext we have to treat the message data as an integer. The message cannot be larger than the key size. For example convert the text hello world into an integer:

m <- bignum(charToRaw("hello world"))
print(m)
## [b] 126207244316550804821666916

To encrypt this message \(m\) into ciphertext \(c\) we calculate \(c = m^e\pmod n\). Using the public key from above:

e <- as.list(pubkey)$data$e
n <- as.list(pubkey)$data$n
c <- (m ^ e) %% n
print(c)
## [b] 75125548552654052363908259949236663786498796856601425682038959510222575325302

This is number represents out encrypted message! It is usually exchanged using base64 notation for human readability:

base64_encode(c)
## [1] "pheNu6cX8v0dlCNtrlmUyKTQ9WMDnu4B7RvLP5cNUHY="

The ciphertext can be decrypted using \(d\) from the corresponding private key via \(m = c^d \pmod{n}\). Note that c^d is too large to calculate directly so we need to use bignum_mod_exp instead.

d <- as.list(key)$data$d
out <- bignum_mod_exp(c, d, n)
rawToChar(out)
## [1] "hello world"

The only difference with the actual rsa_encrypt and rsa_decrypt functions is that these add some additional padding to the data.