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

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] 10491317805032876187358626329033951778671427563012171758342625663332465539425877812751457940262321074469824237959711924941253600694010882054788383253370959
## $p
## [b] 106791299072753374262880468836693910772500958630043465854840757432458537146427
## $q
## [b] 98241316438013259155746167525858982168275894774468504482194514246668006757117
## $d
## [b] 10026278541355556424106161164533842986431311337512830028358260685503440524670167044043435668076436487979391370662404604224150695407802315817319971408934849
## $dp
## [b] 8434193875224246840482007212700118744502570484903260437076090765070195283121
## $dq
## [b] 64666251885491462635148601872194800368604815976864928435195220258586922555125
## $qi
## [b] 100530512549326869517059292171531661766712475400190570442774994270951781201143

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

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

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

base64_encode(c)
## [1] "nhdo9aV57wHakrib7P0LxLIGDmDjQLFhuG6YxgRH1ghOle1s55lN5+BG160RMfeGdV7oAbLuDJ3Eq6nx0A+A3g=="

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.