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 and . Keep these secret.
- Compute the product . This value is public and used as the modulus.
- Compute .
- Choose an integer smaller than such that and are coprime. OpenSSL always uses .
- Compute a value for such that .
OpenSSL has a key generator that does these things for us.
(key <- rsa_keygen(512))## [512-bit rsa private key]
## md5: 2e6cba7e300526966f473827d7d28a69
## sha256: b9920fa2bcc338aa5a8afa178b0c669d2dab4c12c4d52f3e03978c2c83709029
(pubkey <- key$pubkey)## [512-bit rsa public key]
## md5: 2e6cba7e300526966f473827d7d28a69
## sha256: b9920fa2bcc338aa5a8afa178b0c669d2dab4c12c4d52f3e03978c2c83709029
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] 10499520480911408498617561734917783374585657575647247023137050212926232331593356521981438428888100997772053925674234947275416952632834807970375046398434931
## $p
## [b] 110993146672067611714069747672786666743538124005401443443173303930023666369021
## $q
## [b] 94596115127112653674216741208266638208777047496432380896815860641069556241711
## $d
## [b] 3145354312857373438719480439166285319639901356221407766685226770074625316480283385905437804113855053795909684654868637809117987206725647765603384541571273
## $dp
## [b] 16444809103037620119071932647249012528491618232333613315122950106970563200073
## $dq
## [b] 55517499673987732094410768223958370163788265832358494689018851150303772551763
## $qi
## [b] 43276340264974147314127683087467503814414334769284315013641210328716982052224
You can verify that the equations above hold for this key. The public key is simply a subset of the key which only contains and :
pubkey$data## $e
## [b] 65537
## $n
## [b] 10499520480911408498617561734917783374585657575647247023137050212926232331593356521981438428888100997772053925674234947275416952632834807970375046398434931
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:
## [b] 126207244316550804821666916
To encrypt this message into ciphertext we calculate . Using the public key from above:
## [b] 359489899867135158788510130762390104206974655189814950621312480222659329126382129239758417640654353856474298218201486692266202085428173573607902362079690
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
## [1] "Bt0maDng+fL3ioNd8fG9X8rbn25psvttX8ApGXUGjnADCxPGyyyDdxJ5bfsFDzxfgf5CcTjJ5aQmra3Vc7oRyg=="
The ciphertext can be decrypted using
from the corresponding private key via
.
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.