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.
RSA key generation
An RSA key-pair is generated as follows (adapted from wikipedia):
- Choose two distinct prime numbers and . Keep these secret.
- Compute the product . This value is public and used as the modulus.
- Compute .
- Choose an integer smaller than such that and are coprime. OpenSSL always uses .
- Compute a value for such that .
OpenSSL has a key generator that does these things for us.
(key <- rsa_keygen(512))## [512-bit rsa private key]
## md5: cf016640300cef0d337937519fb48cd7
## sha256: 42a0df721effc60d590340f5153a86f4489e6523680469e147571517f663da41
(pubkey <- key$pubkey)## [512-bit rsa public key]
## md5: cf016640300cef0d337937519fb48cd7
## sha256: 42a0df721effc60d590340f5153a86f4489e6523680469e147571517f663da41
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.
How RSA encryption works
The data field of the private key extracts the
underlying bignum integers:
key$data## $e
## [b] 65537
## $n
## [b] 10402943095111123146665573615374810856834699288095880185468907917996672257813966357374762585268987766954777640565622980857615406573465961778932261516373909
## $p
## [b] 110967817522429161235422456767875369622217059191792801762978211864809701684173
## $q
## [b] 93747388453489660239938237126498855058905105384857358416858504577365098099433
## $d
## [b] 5613624098732088287254955382573663517584862601637407918566732205013233965509310473096100530370878682994585298596936216681863918405722932280009184380307553
## $dp
## [b] 29050381391157927877628562350526232763299786144522774918708777956948594714365
## $dq
## [b] 25956942046126972164028247415924565114345973149726438894552915514302868152529
## $qi
## [b] 37206818602742785959507527277528359023654634737991769927077819635895822484488
You can verify that the equations above hold for this key. The public key is simply a subset of the key which only contains and :
pubkey$data## $e
## [b] 65537
## $n
## [b] 10402943095111123146665573615374810856834699288095880185468907917996672257813966357374762585268987766954777640565622980857615406573465961778932261516373909
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 into ciphertext we calculate . Using the public key from above:
## [b] 5944052080631208556529277343994167049569758206339065714899362157152075000871757654726160234287744776419088263957890572713842820280024722666661588094420820
This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:
## [1] "cX3r3gDOXs/1pwNxPjdHtDNsj6r1PlDBdvlpQR+u9PcomluOyeCcTApXkF1+vV5FdLT6/SHE7359hdV5VswHVA=="
The ciphertext can be decrypted using
from the corresponding private key via
.
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.