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: 1559589d0c5161dbfcf6ae0ba5a7a928
## sha256: 049abf3bade6aeb2d3a122b056916fb58ace2a1ab5db0c3f7999f4188b8c82c8
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: 1559589d0c5161dbfcf6ae0ba5a7a928
## sha256: 049abf3bade6aeb2d3a122b056916fb58ace2a1ab5db0c3f7999f4188b8c82c8

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] 9401544007003192261553164210936928737812906362818792226636556835908857226323591216935994959655991913480734226114758384464484232400344647174092478429382981
## $p
## [b] 103540578610199263635435149764660236973727913572290293198721791801367260461939
## $q
## [b] 90800574356430081466079440124527586132992125747341419167748473412208530418279
## $d
## [b] 8622732306528340944160361237026365451156723311355600090920703698564316190241340607741676715112486078040357562317997440304984875996047915797776722764165649
## $dp
## [b] 26772642097264701185072311029066517780136308091551815135656796677693052715077
## $dq
## [b] 90409867397483024338450247422464373302232344563266007107304677243597779154443
## $qi
## [b] 43824255519631034807164669324136789397469015284017649431124299026079106001197

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

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

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

base64_encode(c)
## [1] "bdmgTNKUM+X9dB7uRClH34qx+cP4mZF2G9+uFcHSgcpMwBHYRyFQe3Sm5Ses9xvd3rvp2t26mu3T23NGjud2rA=="

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.