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: 194367d60e41691b0eb50068e0101b66
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: 194367d60e41691b0eb50068e0101b66
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] 12441156965473727028437150908174799884762281246930125051667265046199424861283512901946856012239141385087037624832802426483975646633678115251609717281692517
## $p
## [b] 112273996634325512985388088472129726000712314455985884576316390399167106649573
## $q
## [b] 110810671557318499188233584697100088730645607963011109750006395781286529171329
## $d
## [b] 6640016911642889116082131688452906760596534376842423578394152872209034328668796423833454724382301271475506793428782059541116140364759555622866240197221377
## $dp
## [b] 102819181988784177462456662555812470743408329478300209355041535605493254297481
## $dq
## [b] 94839272141256876847692295943437158810910520717354412881084101282758483112577
## $qi
## [b] 66380546274724613590710358892686696486793919205074701138290166156595746488803
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] 12441156965473727028437150908174799884762281246930125051667265046199424861283512901946856012239141385087037624832802426483975646633678115251609717281692517
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] 2962860836515914994162103228792170547379304936589802917811220818049492303515936138935247094937710117560696270072916418679947814200243266286573875407120922
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
base64_encode(c)
## [1] "OJIpfxElEtB+V5NRg45WlnMr5pNfxNdkv5GHgmEc5TyBTt3T8yBS6PPewu2aL5zkXe9sMK45JCr/LG1qVVRqGg=="
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.