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

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] 10017708492427117913598205427929601667592237381418669694238596328165967666342779658184558414655168509099673659657925838273254198485060341024344803876758791
## $p
## [b] 109478776975467897185813518518296652892906742323266722346562949568507665598433
## $q
## [b] 91503657322294484920148691503692982889830163193723508064731590355985821706727
## $d
## [b] 4296317081902574167226219692125323314631658697827708761401410302512486888290444464032723135838365640865883996663142593498985149036626954995462130767602625
## $dp
## [b] 62760236980153636835238321722575114045295120452341894785546638010419045676993
## $dq
## [b] 34123462852386853402634145907659131510863316728788356761860324218384934960593
## $qi
## [b] 75560666988178361963269507735556297196621964683233143360718357085400370035921

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

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

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

base64_encode(c)
## [1] "XkJ7euWdF+hbLawlbmvGpa+uk+XyY8duEoLuFQ+XVUb/3l3hPadkWFuch7R4yKaJkp8/e4w/kMcwiGb3c1qYYw=="

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.