Skip to contents

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 pp and qq. Keep these secret.
  • Compute the product n=p*qn = p*q. This nn value is public and used as the modulus.
  • Compute ϕ(n)=(p−1)(q−1)\phi(n) = (p − 1)(q − 1).
  • Choose an integer ee smaller than ϕ(n)\phi(n) such that ee and ϕ(n)\phi(n) are coprime. OpenSSL always uses 6553765537.
  • Compute a value for dd such that (d*e)(modϕ(n))=1(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: 2d5e2319a7b786b56d971afc6e8bf054
## sha256: 1c2945691cabf2316d5de4bba5fa826e71cac5047af76ad8c420d509677d0de6
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: 2d5e2319a7b786b56d971afc6e8bf054
## sha256: 1c2945691cabf2316d5de4bba5fa826e71cac5047af76ad8c420d509677d0de6

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] 9844003626983341789765648415815132196488326776007695442302487152542663683835325401955304845206911997133590486189812728558792676014256734217773796316523197
## $p
## [b] 112615380255843435499649834737364196760772269056934237956589975761391513264939
## $q
## [b] 87412603896727080287407580088986303589251129976539027826853728107659001386423
## $d
## [b] 3530424969843240694976147830467047120653396288253732604114891710535464962156656343589391688120301517969236190934054525261683938865070360563327847556757185
## $dp
## [b] 90501958394719960646589827058722515754403678329655193931636798196453426623705
## $dq
## [b] 28028265838927977557098782183956515916574198473030366522018161381423413569957
## $qi
## [b] 41691263898927500982354360848249669029867731644341409590848815959834366568926

You can verify that the equations above hold for this key. The public key is simply a subset of the key which only contains nn and ee:

pubkey$data
## $e
## [b] 65537
## $n
## [b] 9844003626983341789765648415815132196488326776007695442302487152542663683835325401955304845206911997133590486189812728558792676014256734217773796316523197

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 mm into ciphertext cc we calculate c=me(modn)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] 6572253667264123888040392205742202511929242936491296662244270395783712644106568893368261548139938637894452046289125985401330553050937088270212466504429769

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

## [1] "fXyBulFKfaS8hAYConh7OEmFlxy4LaFJV+swVBThdeOf6dNxMiPAioGfoJtG4GnT9F0W5NBSyVhhaI12++DwyQ=="

The ciphertext can be decrypted using dd from the corresponding private key via m=cd(modn)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.