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: 5388968930d69bcc988c85f9d3929ede
## sha256: a478cb291a24b6d1efb5dc1ff2b4b5c9ab8728db3826c548a9a4265b415564ab
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: 5388968930d69bcc988c85f9d3929ede
## sha256: a478cb291a24b6d1efb5dc1ff2b4b5c9ab8728db3826c548a9a4265b415564ab

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] 9740405958289491963343818114869397975361236170877954263370507531376852325576946881892018879965864504796085270170472070584311635006884951705500737280220669
## $p
## [b] 103954401191849747858968692229424948143806963565108769755136479362914648134331
## $q
## [b] 93698831859109021238012325033698695797759754016135791243066938901610475704999
## $d
## [b] 648597457954672062011267257477303702709560014186053563717425192897578216104138633655305186506899819548816391465001130947013311929228523880029583744303953
## $dp
## [b] 30930784491830112505148076635698712007022533675932999835591518494542558228473
## $dq
## [b] 51279368941066317725007065657318325864446787269736826899538915384959075516291
## $qi
## [b] 103567042814249132304265311220060857921509589126191597481387744897099859276597

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

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

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

base64_encode(c)
## [1] "c5dSiHEA3/3YACMcL+zvNKiFFcve/LWbb6/jltwLO9uDG7l1IvgcfcySrAerjFhI5sJFB3k71RXoWxlDC6l+Qw=="

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.