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: 8b047f6d941dd1048fa28f80f5436c9d
## sha256: 86f752a7ff61937ff1cefd5cb427cf62f0380b0d17fe53428edf22a6da9d5997
## [512-bit rsa public key]
## md5: 8b047f6d941dd1048fa28f80f5436c9d
## sha256: 86f752a7ff61937ff1cefd5cb427cf62f0380b0d17fe53428edf22a6da9d5997
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] 9747806934377760309162623432011721894618683786891402262033597037481301466906318100737744313647839705385483556790656468623629990920568158082642593849109237
## $p
## [b] 106424094753134893686117054745555248271077811682090048102701477773600636648943
## $q
## [b] 91593984961667930034424748306482867333899505136429823867845045450857518631259
## $d
## [b] 2697800054560688107291936826675444584350728390476162385046086684862533317160431604425764671366218676305875198524926132556005713747645810434788381299337337
## $dp
## [b] 96287848061204106399717880909069662118094493853693782172661002557115060959443
## $dq
## [b] 43644002050490045609579566353584188795688141758097418858737279389343097667225
## $qi
## [b] 100700903707800065784130235069227141869871721980327088854164221421951973728558
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] 9747806934377760309162623432011721894618683786891402262033597037481301466906318100737744313647839705385483556790656468623629990920568158082642593849109237
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] 6701868674182196183595102732291271425696838380681461395602023544587801630999890028667022608330995371198402312753201210496048503028512446241468850475999191
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
## [1] "f/YNPJhDTkcz32p92jmzD9pYkvLbVBQ1zSN5sOxiWGGKRdWw0zUPZtlY2/mVdO8aLxOXgJp5l3Z1ASH0NSmf1w=="
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.