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 exponenent 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(256))
## [256-bit rsa private key]
## md5: ec:e5:0d:16:2c:58:56:d7:0d:dc:9e:35:de:8b:3b:00
(pubkey <- as.list(key)$pubkey)
## [256-bit rsa public key]
## md5: ec:e5:0d:16:2c:58:56:d7:0d:dc:9e:35:de:8b:3b:00
Usually we would use rsa_encrypt
and rsa_decrypt
to perform the encryption:
msg <- charToRaw("hello world")
ciphertext <- rsa_encrypt(msg, pubkey)
try(rawToChar(ciphertext))
## [1] "*IM\xec\032\034\005sl\xba\xa1\033^\035\xc1\xb1\x8a*\\\xbf_QA\xa1\xc8W\xd9\004!\a\x9a,"
rawToChar(rsa_decrypt(ciphertext, key))
## [1] "hello world"
Let’s look at how this works under the hood.
Using as.list
on the private key we can inspect the underlying bignum integers:
keydata <- as.list(key)$data
print(keydata)
## $e
## [b] 65537
## $n
## [b] 90706973907130807165554162005142876717069546611474220884196610532242122041059
## $p
## [b] 336102297104933393447953113075885901073
## $q
## [b] 269879065654857696452538702435910524083
## $d
## [b] 9576293581693364889733574117118323450893562536519143991130732950868320535521
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\):
as.list(pubkey)$data
## $e
## [b] 65537
## $n
## [b] 90706973907130807165554162005142876717069546611474220884196610532242122041059
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 <- as.list(pubkey)$data$e
n <- as.list(pubkey)$data$n
c <- (m ^ e) %% n
print(c)
## [b] 29892471942183361427220663792671976895123407113668951219040155585865711053933
This is number represents out encrypted message! It is usually exchanged using base64 notation for human readability:
base64_encode(c)
## [1] "QhaKH9854VJTbJugUWEpgIBlaH7Xx6Yf+CESB5NHbG0="
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 <- as.list(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.