Fun with bignum: how RSA encryption works

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):

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

(key <- rsa_keygen(512))
## [512-bit rsa private key]
## md5: f1d3267b59452b95256f9680caa1aba6
## sha256: 2f1f794599dae7d4d17779b842e4f7ff4128e8eb78ce8ec65c756be531325b7b
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: f1d3267b59452b95256f9680caa1aba6
## sha256: 2f1f794599dae7d4d17779b842e4f7ff4128e8eb78ce8ec65c756be531325b7b

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] 11379759874390300482470285055538106566852341752580679813114717359512562881727440070332215612817876473183164720011904958427582279133270623329750684023729161
## $p
## [b] 109420700090006208638903577264436059845001647061872399638391259809441540255453
## $q
## [b] 104000064567578611474258172419629361326314631040226311606512430379011260055837
## $d
## [b] 3854084718128188801881539391377753228830348925649339596440091345526051630724896206933511115697062033058845180726855423976530236120775788838667062639958849
## $dp
## [b] 35529128552044373710054902180252366655502007255087121233882631318872026132353
## $dq
## [b] 103939762715898369973487282532207814479031417649629723422112053914504150669045
## $qi
## [b] 10325444322390945651845068926919281513298778624394263688075301506905920257024

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:

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

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 = me (mod  n). Using the public key from above:

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

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

base64_encode(c)
## [1] "qFRplpOc2wGG5BVzz1wZjBFKybWbs4TswIenWJhAfzxKyHS0Px5Mir38QqGmRQZL3jpv+OfkeTQV7pbKBVJrTg=="

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