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: 97cf7869eff7f22df7ff5a79485ed240
## sha256: 26e558b717f0fd9d5d645645bbcb849143c42bce8a758622d48ff21095375c85
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: 97cf7869eff7f22df7ff5a79485ed240
## sha256: 26e558b717f0fd9d5d645645bbcb849143c42bce8a758622d48ff21095375c85

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] 9280202740042032396046738734805869796725820580724895265493603220095458567844146821651698832754597829580627679486601672949598264983797078123024787119883831
## $p
## [b] 98616976542050940783809067852758809237748785338301175514070166480573241767377
## $q
## [b] 94103500892515110240339421364763155177387170743186046793520611499885677172103
## $d
## [b] 1668643805615992641637773612630306081825794127336652056221319565216669724941226408414140563633289779177886953877068620045647522912879627063704508346734337
## $dp
## [b] 59854547445155565124396498038957949784853341385806580380901918185113170236369
## $dq
## [b] 44159163013998347503416368839768772972906374247157234586385292065054612138957
## $qi
## [b] 20341873797960227328699678947144546163222657506248370084923506618492384365740

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

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 = m^e\pmod n\). Using the public key from above:

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

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

base64_encode(c)
## [1] "d7q16Oh8QPHe99gDQ6j58PYkL71UpQHQCcBVkXpKf/LMwmjgd+CcUVrMN9U61xClzUwOnR4PQWT9hsh8gfNB6g=="

The ciphertext can be decrypted using \(d\) from the corresponding private key via \(m = c^d \pmod{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.