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

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] 11137628556743838121744660761276634530978236811394768897551111112457807654521722020292218046595148214398419507241294223182682566006504216629460475401409007
## $p
## [b] 114121117105614600694400242937663442203165533327491605702187173611913461004609
## $q
## [b] 97594808386220066564784186840877581762803505192513728582428613639637465567023
## $d
## [b] 5035274677018526629372301962187243915930759395224310048759355343561841143107219262524707126252520317819565484963504872698435883701682935644808401716052865
## $dp
## [b] 103462476066148848968651064808950838182145708961140138309674734994361203580417
## $dq
## [b] 24370035846018148364018695052427813686135760905679038836862295530962160672663
## $qi
## [b] 1644937802591369851081274970131048975580654449743183107500219631924161466102

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

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

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

base64_encode(c)
## [1] "PcIaM/uv+u+z2rst7AW79FC03MeLNHznuWxcCwm8QgoE8JoTc/xcn34CNLdUmSIGSGydf1mlk0SbdjS1zXu4Yw=="

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.