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.
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: e020f94a93f1e9fe4ce5f6d59c2f036a
## sha256: a045461c7d9215139bed66c8772164da3cfe27ba5fb89cd126abd50e71ac1ebb
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: e020f94a93f1e9fe4ce5f6d59c2f036a
## sha256: a045461c7d9215139bed66c8772164da3cfe27ba5fb89cd126abd50e71ac1ebb
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] 10233830949767391512118597272803556274386681286353818042780580180053845558954337207311088110510976330873929137121157228825407365411440499226780550273661723
## $p
## [b] 113901548592372787568781548298973315915043543300225732183806017802822573566311
## $q
## [b] 89848040489703066280016137416345911780945028458157078339258283252474174286893
## $d
## [b] 8866551034663506987649817229153893660313260450134369017305096711836632813265402015089388050320679287455330100459414112376862579627732317202764936149792233
## $dp
## [b] 917649841414358787193747921050061961993118251713065697133673762910879638113
## $dq
## [b] 7510071651167941728823846083384086923966871628145695944923583253079230461321
## $qi
## [b] 67701285569612286206819166643770397634991961469122258547556787921682886169026
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] 10233830949767391512118597272803556274386681286353818042780580180053845558954337207311088110510976330873929137121157228825407365411440499226780550273661723
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] 1284313849763694830823852014322852785705851212998288167411823013583525565463000036017122570344270864795969068394757287590688377649330363456537542083819381
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
base64_encode(c)
## [1] "GIWYZ5p6Om4uw0uC/vCamiBIbYnElIRFhw01LuMN7RoKbFTgWlsicd4hJUdqefiP73EG9F8iQZgeoR5v7JU3dQ=="
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.