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

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] 10125725304238799169062273502523461459887324411752261765332720932411608001315182098399137754501022863496561125826477710030843446043268517680847444653676647
## $p
## [b] 110637597936892378527977489590386596683996818544875945954030132246450633414863
## $q
## [b] 91521557707846359692470056355358333575765031966292170493183503087718456769769
## $d
## [b] 677963328119991008954411342128461005019844965725756818686848336839070081172011718165318024217879625268036380843990484102343684262979566995006061509873857
## $dp
## [b] 27010720157930299776426138417171758654560768675984789283374004241012102089473
## $dq
## [b] 88798408379523421483514556257811165556588966349399884723897331443898156928441
## $qi
## [b] 68999039943496954862728081504956185762132526692123363559408038049224256376478

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

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

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

base64_encode(c)
## [1] "neLTr1lb6FfZtVMfwl9PnUte12iMPcITk8+7wHrUq0ILORUPo0j9pzXbOHCDn3WAQWaA/61zGVkuMEwhlOH52Q=="

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.