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)=(p1)(q1)\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: 67ff75c0058a66ded85161bebece10d4
## sha256: fe6b6393a13c609787b51db825a2fe1914047666dedc1e6ccb66992a01e4e3ef
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: 67ff75c0058a66ded85161bebece10d4
## sha256: fe6b6393a13c609787b51db825a2fe1914047666dedc1e6ccb66992a01e4e3ef

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] 9846770048766340382227951455132535958853128975810850840129494555179149437215714975026365396688913171021886573218413847407855097845327979588632829072006741
## $p
## [b] 101287449496767235296692124267255598130416917512582487296467599451073714287019
## $q
## [b] 97216092395342792537363010923811824739860301270723234421226824288640608727039
## $d
## [b] 7073351547306434113775844158333911040647078839788565785001088616639821737419047018394891944846884700515950547352527582851841277897909959335838351712056969
## $dp
## [b] 80194462165460046573844662344990245683952628786950771065602747576434135841419
## $dq
## [b] 86807234493117784141576260726940018367893324844938335266035884422101231711953
## $qi
## [b] 2209109860533828624385479571342749974412070386339023427167186498202384936759

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

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

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

## [1] "q4uPh/bTIoQ5OA3ihJ3A+C9MhMo2qU+/dnodo81D4EwiDd/U1MGzCqnkb2nkO9KsQ5JzXTCZ3YTdRhgjY3R12Q=="

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.