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: 97c0ac8b84b5795df9487a9298f46dee
## sha256: 27c6654f39658e8314cc8907df8b13cafc1e5727fef1cf5a39c0c71b171b7374
## [512-bit rsa public key]
## md5: 97c0ac8b84b5795df9487a9298f46dee
## sha256: 27c6654f39658e8314cc8907df8b13cafc1e5727fef1cf5a39c0c71b171b7374
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] 8973520310317846220104292200920663306475635231910390307160537017549006179783411366912965806676759956823090589638992187178637123267097580379917054272093569
## $p
## [b] 100728018172655890083117921022458616168247931683075633260091727188961134576137
## $q
## [b] 89086636202218470655298173180363546553571415711930647355717710006951671684537
## $d
## [b] 2320433475730603169081701022765803760545075152588391054449389212756198906400754621757577254527360377355271998953573613739354260289148215087146118748515649
## $dp
## [b] 93184598223750766129809976557832116814390403131257039060776375600645812719785
## $dq
## [b] 36325467739321058201039612461645407244016188444236426587833504196343584882217
## $qi
## [b] 26455592126457829512014406140829783175330819432211595242618305380995204872587
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] 8973520310317846220104292200920663306475635231910390307160537017549006179783411366912965806676759956823090589638992187178637123267097580379917054272093569
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 = m^e\pmod n\). Using the public key from above:
## [b] 830066319405283565649964480499615236869982116864359217671300133868550358514046718455364925298091981181882618093528813984253200500235950220177574420731144
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
## [1] "D9lHl1qcVPNYrsSYq9Y8YnXWo6nu4p4C56v8bWDnl3GU37sz7p+8b7TvfFdiK9Y+uXLY+EeTIpIuyt4d3NLVCA=="
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.
## [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.