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.
## [512-bit rsa private key]
## md5: 3fd03a9b8948a1ba4e0dbb875d9fefb2
## sha256: 0e5ac62d41ccf2a4782551ff7e49770900c33968c6e3e54e1fec1689bacbcbb3
## [512-bit rsa public key]
## md5: 3fd03a9b8948a1ba4e0dbb875d9fefb2
## sha256: 0e5ac62d41ccf2a4782551ff7e49770900c33968c6e3e54e1fec1689bacbcbb3
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:
## $e
## [b] 65537
## $n
## [b] 9404187792769008099099823534256767535321396446620310427643542477085530422415522557562672674304740866674447879828182488542921443797452949951905958174692617
## $p
## [b] 101099998444545253050366905250309525186810944151174717801171233243519520073471
## $q
## [b] 93018673960982651500202956053332146038279881945646310263802580753198788135927
## $d
## [b] 3693830998694780146894542890563158366941474088360773777078597898059656745560757811219818680785414325046370126472624583548629010167227448338222067727283993
## $dp
## [b] 73059430952494975120395755567918261636134798892223242367875697795338274115073
## $dq
## [b] 67653815600717137897808476177397959519975721391293117247271218001769587184141
## $qi
## [b] 17960432844958791056551207269354606964474618879150448526482170782078539927644
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:
## $e
## [b] 65537
## $n
## [b] 9404187792769008099099823534256767535321396446620310427643542477085530422415522557562672674304740866674447879828182488542921443797452949951905958174692617
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 m into ciphertext c we calculate c = me (mod n). Using the public key from above:
## [b] 5616314083511044694847684150431882042204758011927555632844154261350952667053568400063453232727852436238961424983558923334070687606838857817944143396006889
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
## [1] "azv4rWnunrIOKGIC+baCBMqdeaiMt9dXZ9lcPhrX7IDWdlv25jYXk7UpWEgB1rA5TQLL3GmJuOBwWPm+tL7r6Q=="
The ciphertext can be decrypted using d from the corresponding private key
via m = cd (mod n).
Note that c^d
is too large to calculate directly so we need
to use bignum_mod_exp
instead.
## [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.