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):

• Choose two distinct prime numbers $$p$$ and $$q$$. Keep these secret.
• Compute the product $$n = p*q$$. This $$n$$ value is public and used as the modulus.
• Compute $$\phi(n) = (p − 1)(q − 1)$$.
• Choose an integer $$e$$ smaller than $$\phi(n)$$ such that $$e$$ and $$\phi(n)$$ are coprime. OpenSSL always uses $$65537$$.
• Compute a value for $$d$$ such that $$(d * e)\pmod{\phi(n)} = 1$$.

OpenSSL has a key generator that does these things for us.

(key <- rsa_keygen(512))
## [512-bit rsa private key]
## md5: 1ec18b404283cef67bb51426e1b06ffd
## sha256: 572e547707042cb3e830795c0b1959759402192fce75932e5514b2686b7aa3cd
(pubkey <- key$pubkey) ## [512-bit rsa public key] ## md5: 1ec18b404283cef67bb51426e1b06ffd ## sha256: 572e547707042cb3e830795c0b1959759402192fce75932e5514b2686b7aa3cd 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] 10074793365724745624308351310937533186753080423470317386886216111136399698081594769054364190338684321635150692109531476957543858011100747783115382959778771
## $p ## [b] 105505409790558911948227379954402463145687166791014886956713752151642986719227 ##$q
## [b] 95490775171855523571814627965145008763232726036957824684553213380758393457673
## $d ## [b] 5552458330051910938926001205422170089755931944021306951751579103683958995605564210785814467306199408209957149597422664122869113037386163352766829602722737 ##$dp
## [b] 42149360500731089806193284129059390727076024244661058185462860045994120546909
## $dq ## [b] 53425702324281344626817324009307292618237855953081962215367085387373056638425 ##$qi
## [b] 98148163204215208375676199677102107010237052913191303204266598108800743562498

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] 10074793365724745624308351310937533186753080423470317386886216111136399698081594769054364190338684321635150692109531476957543858011100747783115382959778771 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] 7544902785656248227403241919347546941168566466920571237095839197851790779143861206252062571592391697042846027830848856495876481437732820218003486695510841 This number represents our encrypted message! It is usually exchanged using base64 notation for human readability: base64_encode(c) ## [1] "kA63c4OTAW2dvuTJbSuVG5UKbHztimlxuZtegXDs3IXd+fYRyy3DT2W0xorCiyD6oM5YOSRYh9Wc/wG9mxhTOQ==" 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.