Primitive types such as int or double store numbers in exactly one or two bytes, with finite precision. This suffices for most applications, but cryptographic methods require arithmetic on much larger numbers and without loss of precision. Therefore OpenSSL provides 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 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: d28ad60fcc5e2a58a3be0025769049d5
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: d28ad60fcc5e2a58a3be0025769049d5

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] 11476849050946563058096024789439430445028415133816919662956794592907650069089228429786142664218213790657244906522172949864497365440872918175001203667327191
## $p
## [b] 112916628482552786418689518956086815763632843496873321607360984278556030076859
## $q
## [b] 101640025965882414736928834178323877842082727413802176371634006105580681132949
## $d
## [b] 5608923177026221343481660405281833433385951574240842880889317088304913622272182512817721813039769232658320568089677280729805882054121327886233731137181801
## $dp
## [b] 94865336592296815847735552645408549002023656300072403187532169528316752613513
## $dq
## [b] 32261406841055983510957071708767464285380851971587238855649947281814689853481
## $qi
## [b] 41954473356570542099071379391742958747801376424784977278765992668106875477416

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

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

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

base64_encode(c)
## [1] "QJspkYpju4Cv9O0//0yyBoD/4z/nC4/Ounz9BIhiuDK7Rlnf1kez80JqlnR6lBFzE0OjojKkxYY8djECsJvbhQ=="

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.