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: f7700249154cd193f57386f6b3827f38
## sha256: eb694ca0e3cf8f34fc373bcd73c387be3a73af0a5f2212c4aca6ec72efdfdb35
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: f7700249154cd193f57386f6b3827f38
## sha256: eb694ca0e3cf8f34fc373bcd73c387be3a73af0a5f2212c4aca6ec72efdfdb35

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] 9574259662021334681449456150250338165218399163292536715306316898614113050874245562743175486720414081515263449891445625095625273227528171064962208016958881
## $p
## [b] 100765931633674501719842787222138602331680579686535592600601062017229346078087
## $q
## [b] 95014847843889303399441851233813706606587140590405886678232973474201987706263
## $d
## [b] 2783294857574049290492012734403000484058485143644802317774935525770115840597714214046605319550516448267344194026915027735708628964823631980186417403418545
## $dp
## [b] 62936232655769998304748841255531980549043764107434905655132265308928526222047
## $dq
## [b] 89995676944465393724466376479530430712481449443054235249573703837696085979923
## $qi
## [b] 74858103881789745232009802745350002087589323422296423494795601141451151731195

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] 9574259662021334681449456150250338165218399163292536715306316898614113050874245562743175486720414081515263449891445625095625273227528171064962208016958881

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] 5764024382981896069571536242111935849208701503711339639415227102810224576599385076378465930284236762834360120069262492738523781912255115667044459219811038

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

base64_encode(c)
## [1] "bg323GHyKFRfFBPSz1kwgnXlxU/SKi6spAg4b+z9PIkb5dpajw9V5bu4CtCbDbNHRCkaG3x+5go77XK11Hx63g=="

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.