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: 2b5bf428fcd187b776542d638f9a3531
## sha256: 975eb9687209dd6b19a39fb1a8bb0a3826cc8a0480e99bed2e3aa0c6f6c41160
## [512-bit rsa public key]
## md5: 2b5bf428fcd187b776542d638f9a3531
## sha256: 975eb9687209dd6b19a39fb1a8bb0a3826cc8a0480e99bed2e3aa0c6f6c41160
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] 11336288321808428148013257085039472877932878125625536322895578742456070330232093448723242611393923229344029171074815477938839889028922702927782801368068631
## $p
## [b] 115326266168574152134509988246719958055492838208446223489270849336709097084691
## $q
## [b] 98297540520717141729237340139377555999066943641692978263182088646753127679341
## $d
## [b] 7393314241584690112491915014858128229070709322146065177124415621863334575198300310917242504351995702890163056748615944639914184157238061325114108879917073
## $dp
## [b] 35164331245656915667545250700126721116665599370117351785801903997672427591183
## $dq
## [b] 35040162681309700857201927771123769218154659770163897617902253001593703844313
## $qi
## [b] 90829363159745896203263911062067115871127433334792293991949836223787429685081
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] 11336288321808428148013257085039472877932878125625536322895578742456070330232093448723242611393923229344029171074815477938839889028922702927782801368068631
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] 11022178534216810110519528319360621355947954005889801362798610374334582397871469224391026971990167458010889298739942562920972440833914501582687783271347
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
## [1] "NeASxT2bJ76odnXOuOnp3/yhQM2mDkl+QmXB7PpRW+8qvCf+Do0VR1YqAnVFBEYr+9eTqXGhZvJPvuisdruz"
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.