Primitive types such as int or double store numbers in exactly 4 or 8 bytes, with finite precision. This suffices for most applications, but cryptography requires arithmetic on very large numbers, without loss of precision. Therefore OpenSSL uses 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 exponent a^b %% m can be calculated using bignum_mod_exp, even 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):

  • Choose two distinct prime numbers pp and qq. Keep these secret.
  • Compute the product n=p*qn = p*q. This nn value is public and used as the modulus.
  • Compute ϕ(n)=(p1)(q1)\phi(n) = (p − 1)(q − 1).
  • Choose an integer ee smaller than ϕ(n)\phi(n) such that ee and ϕ(n)\phi(n) are coprime. OpenSSL always uses 6553765537.
  • Compute a value for dd such that (d*e)(modϕ(n))=1(d * e)\pmod{\phi(n)} = 1.

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

(key <- rsa_keygen(512))
## [512-bit rsa private key]
## md5: f6e6094bbd6ddfd84a8f3e2b858da105
## sha256: 3449f39aae51ee98e751b2f34b35b583d74280904ffb646226a1f0c567a1ce5e
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: f6e6094bbd6ddfd84a8f3e2b858da105
## sha256: 3449f39aae51ee98e751b2f34b35b583d74280904ffb646226a1f0c567a1ce5e

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

The data field of the private key extracts the underlying bignum integers:

key$data
## $e
## [b] 65537
## $n
## [b] 9433710460540421899121249692803365686744678291410638809853425779270749554891912524690658243904133561535348322955888150979662846506259038728428029803265143
## $p
## [b] 99066934911581064738161312766498520059722309035395372160935021098064739220641
## $q
## [b] 95225621636120770000946909964086586791588509885082032926627154531358865574423
## $d
## [b] 8899099467506759587246487018908135508973804812699390010857505561009133004746489712150802081913364561807009268154492951603789851304480281290319487175305793
## $dp
## [b] 19823363663739171505809656463064552726905387196395546187932036356257091263553
## $dq
## [b] 70072643377383161482302602803424626312024466754931811341982714412153633080429
## $qi
## [b] 21242990667545545122849768297079119160199889686500175478246492323456129931601

You can verify that the equations above hold for this key. The public key is simply a subset of the key which only contains nn and ee:

pubkey$data
## $e
## [b] 65537
## $n
## [b] 9433710460540421899121249692803365686744678291410638809853425779270749554891912524690658243904133561535348322955888150979662846506259038728428029803265143

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 mm into ciphertext cc we calculate c=me(modn)c = m^e\pmod n. Using the public key from above:

e <- pubkey$data$e
n <- pubkey$data$n
c <- (m ^ e) %% n
print(c)
## [b] 327703119169570961300955614641375707077527242347977327698315655454633765928754702284925481331283230427274428138668485056430332295019706313031224803710068

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

## [1] "BkHHjAU/+PTFdvOnOueAvpUPFoO3IBVb6cQc6S076Y4uOxuc74n5SAacg5x7nW9g1HpZN3uP8JkFhFMpnK/UdA=="

The ciphertext can be decrypted using dd from the corresponding private key via m=cd(modn)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 <- 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.