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: f8811416887560740cfb0afb7b6e86f0
## sha256: 4180e247b340a8651df675aa7f7ca371384a62fb4976191ef6a8173e636e1029
## [512-bit rsa public key]
## md5: f8811416887560740cfb0afb7b6e86f0
## sha256: 4180e247b340a8651df675aa7f7ca371384a62fb4976191ef6a8173e636e1029
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] 10667589892699112641054348019437648416486848510351648726313149234935979203407595541886289858793394098653890548613821252172768686462693731394813401990797767
## $p
## [b] 103333041407710489601707980343281837249612666627799880001732939970951320820811
## $q
## [b] 103235032545002783553839334864964050986630756541745369935740655482259219518197
## $d
## [b] 8064051428416000708960045616658986188711589619601772757358530897007770903386008888728669406035730325013671645660175664181647628880821070904922449987619633
## $dp
## [b] 739478407925541596704167764484172019928717223071519046047465536206664471443
## $dq
## [b] 64768219984937965597802949961742632168352179939987249578831019599066974822309
## $qi
## [b] 71615015438758818046964200947208148842972641169424040412594437283262551268156
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] 10667589892699112641054348019437648416486848510351648726313149234935979203407595541886289858793394098653890548613821252172768686462693731394813401990797767
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] 185343940577057893650195741396482662254376153133038689981891144481625846663783950743284588260983396709384985772520826819260570782703144474289533347094769
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
## [1] "A4nxOzxfeS8byoIGt8+l3o+cxWcS+JV/zeBnR9TZ0d2KveHSQEqX2b6NT94MoJjlBziO1ZfeJqHeR4FDM3bg8Q=="
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.