RSA (Rivest-Shamir-Adleman) is one of the first practical public-key cryptosystems and is widely used for secure data transmission.
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.
Primitive types such as int or double store numbers in exactly one or two bytes, with finite precision. This suffices for most applications, but cryptographic methods require arithmetic on much larger numbers and without loss of precision. Therefore OpenSSL provides a bignum data type which holds arbitrary sized integers and implements all basic arithmetic and comparison operators such as +, -, *, ^, %%, %/%, ==, !=, <, <=, > and >=.
library(openssl)
## Warning: package 'openssl' was built under R version 3.4.3
# 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: 1. Choose two distinct prime numbers p and q Keep these secret. 2. Compute the product n = p ??? q This n value is public and used as the modulus. 3. Compute ??(n)=(p-1)(q-1) 4. Choose an integer e smaller than ??(n) such that e and ??(n) are coprime. OpenSSL always uses 65537 5. Compute a value for d such that (d???e)(mod ??(n))=1
(key <- rsa_keygen(512))
## [512-bit rsa private key]
## md5: f87c9cfd01a89d6b706593167032121f
(pubkey <- key$pubkey)
## [512-bit rsa public key]
## md5: f87c9cfd01a89d6b706593167032121f
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"
The data field of the private key extracts the underlying bignum integers:
key$data
## $e
## [b] 65537
## $n
## [b] 10208942481852106727770952858250462439855824780219453131701788440570862216075782901025716867000327123045587038441102361972217830829986776273234092689180791
## $p
## [b] 106807332593738242680825574167075432176490666402636578186897929110968341429277
## $q
## [b] 95582786630237622295418420959041783617108921273195215936155235542671772920483
## $d
## [b] 9681492655898046679550368358227024293139438270220169241625308655719969751305574016713451022734534575642235633536386543341344122893601572469313268962240009
## $dp
## [b] 70770838120192916801422868886358097597755576064429153116041969752106447145373
## $dq
## [b] 3570298635439853813558513427647501202293096102610462618241726301302477991201
## $qi
## [b] 2214344230841519314833870248780645179813485190785148482508884462265745458630
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] 10208942481852106727770952858250462439855824780219453131701788440570862216075782901025716867000327123045587038441102361972217830829986776273234092689180791
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 (mod n) Using the public key from above:
e <- pubkey$data$e
n <- pubkey$data$n
c <- (m ^ e) %% n
print(c)
## [b] 8159399555844842903645429272323307202328533460845056492904520552093694195283906048093370767123572008298684800992089900753268507677545964436553043212789634
This is number represents out encrypted message! It is usually exchanged using base64 notation for human readability:
base64_encode(c)
## [1] "m8pQctD0tNdBmhLdFyutqtIsYi/0RpChzqj7jkYrEF8bgY8d5QgYSw7UZtX2mNdo+c5yfNG2ch9d62rejoBngg=="
The ciphertext can be decrypted using d from the corresponding private key via m = c^d (mod 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"