Primitive types such as int
or double
store numbers in exactly one or two bytes, with finite precision. This suffices for most applications, but cryptographic methods require arithmetic on much larger numbers and without loss of precision. Therefore OpenSSL provides 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
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.
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: 2756efaab62e064be71223d66efeff15
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: 2756efaab62e064be71223d66efeff15
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.
The data
field of the private key extracts the underlying bignum integers:
key$data
## $e
## [b] 65537
## $n
## [b] 9743775270520008918335575690198532996589564244544663052593899703298728030000357595773660474015684932349756730125127873077819768320547755099920256254436951
## $p
## [b] 102128324288674135667481150134689439540961528693264436583643389811621285278723
## $q
## [b] 95407178550961279984030497768811001570265064106565696834534083378150993745437
## $d
## [b] 7868674278533827791978734812453382956871731810161073776012514179731833635774577279854634677859938184394244200696195752250174408785172236994288888176125273
## $dp
## [b] 27348705178238721347701209772552903916014996544956144804353899189678403903773
## $dq
## [b] 76783438752031703487765759407940056255581740714965608951289587465053267835105
## $qi
## [b] 89533428055978566910029142754726935646663178885599917629403501346828141532427
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] 9743775270520008918335575690198532996589564244544663052593899703298728030000357595773660474015684932349756730125127873077819768320547755099920256254436951
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] 3343323364498274343180627475429598574804835151908480827157941384540584860230355856034931115243295601284150320357851036296017096770254800076916077853226180
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
base64_encode(c)
## [1] "P9XS/l3oiELYS+g/iEpBALZZJe/X6Viy6m1+G3N5XuBVVUoQ+XaTPvXsGfVa92dpTxB8WVviQeyHSgX1ZzU0xA=="
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.