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: 2ba2daa57a0487cb92a5a0c3c1cd8784
## sha256: 5bd7d61ed8b525595a4f89acf7576181e8af6db7bbcc662767583ffd569986d0
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: 2ba2daa57a0487cb92a5a0c3c1cd8784
## sha256: 5bd7d61ed8b525595a4f89acf7576181e8af6db7bbcc662767583ffd569986d0
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] 8964667079256419481496367705284098558563731664727693813066989598394224986131965832593787697584639818134825265929409772625253418759492473537565947794751487
## $p
## [b] 97856257005032756542372846273637718873312255179188851762906999886936414012751
## $q
## [b] 91610565881294292777893652725386866171409347871103756810696956000949278854737
## $d
## [b] 3203572073732171815259241827635588877146689588902796727070645534497959155518351503522506524527170665415125052600592482032134476928369011788804800561913473
## $dp
## [b] 47213251239744507100871712149967570544488357855348146737615687877457457788473
## $dq
## [b] 5851379049683353061145045246326036007042121705119860933664608661061288757281
## $qi
## [b] 97498026424225359733840311939880626509300968701180248570423086149969077652291
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] 8964667079256419481496367705284098558563731664727693813066989598394224986131965832593787697584639818134825265929409772625253418759492473537565947794751487
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] 5809734447294522536902411457570406985470563036097229259835451197514463894703370198821903507493857069847642941757101201502830274636918812697168632828697808
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
base64_encode(c)
## [1] "bu1j8+i0vwnzJXnhv9OcbjoYbllyzeEflnMResMwlXOS2zNywrCew2hlQd7s9DHoW9FbPsq3YXq4ohdEkzyc0A=="
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.