AdSense

Wednesday, September 6, 2017

20 - Linux Security: asymmetric cryptography with GPG2 and RSA algorithm


ASYMMETRIC CRYPTOGRAPHY WITH GPG2 AND RSA ALGORITHM

- Layout for this exercise:





1 - Introduction to asymmetric cryptography and RSA algorithm


Public key cryptography, or asymmetric cryptography, is any cryptographic system that uses pairs of keys: public keys which may be disseminated widely, and private keys which are known only to the owner. 

- This accomplishes two functions: authentication, which is when the public key is used to verify that a holder of the paired private key sent the message, and encryption, whereby only the holder of the paired private key can decrypt the message encrypted with the public key.

- In a public key encryption system, any person can encrypt a message using the public key of the receiver, but such a message can be decrypted only with the receiver's private key. 

- For this to work it must be computationally easy for a user to generate a public and private key-pair to be used for encryption and decryption. 

- The strength of a public key cryptography system relies on the degree of difficulty (computational impracticality) for a properly generated private key to be determined from its corresponding public key. 

- Security then depends only on keeping the private key private, and the public key may be published without compromising security.

- Public key cryptography systems often rely on cryptographic algorithms based on mathematical problems that currently admit no efficient solution, particularly those inherent in certain integer factorization, discrete logarithm, and elliptic curve relationships. 

- Public key algorithms, unlike symmetric key algorithms, do not require a secure channel for the initial exchange of one (or more) secret keys between the parties.

- Because of the computational complexity of asymmetric encryption, it is usually used only for small blocks of data, typically the transfer of a symmetric encryption key (e.g. a session key). 

- This symmetric key is then used to encrypt the rest of the potentially long message sequence. The symmetric encryption/decryption is based on simpler algorithms and is much faster.

- RSA (Rivest - Shamir - Adleman) is one of the public-key cryptosystems algorithm and is widely used for secure data transmission. 

- With RSA the encryption key is public and different from the decryption key which is kept secret (private). 

- In RSA the asymmetry is based on the practical difficulty of factoring the product of two large prime numbers.

- RSA is a relatively slow algorithm, and because of this it is less commonly used to directly encrypt user data. More often, RSA passes encrypted shared keys for symmetric key cryptography which in turn can perform bulk encryption-decryption operations at much higher speed.

https://en.wikipedia.org/wiki/Public-key_cryptography

https://en.wikipedia.org/wiki/RSA_(cryptosystem)


2 - Randomness to generate entropy

The Random Number Generator or RNG is a mechanism in charge of producing pseudo random numbers. 

- These numbers are then used for generating SSH keys, random PIDs for processes, TCP sequence numbers, and UUIDs for example.

- Using encryption (file systems, mails, etc) consumes a lot of pseudo random numbers.

- To get good pseudo random numbers, you need some entropy, what is defined as randomness collected by an operating system.

- Such random number generators are needed for cryptography.

Entropy doesn’t come from a single source in the Linux kernel. This entropy may be provided by:

i) some internal sources like keyboard timings, network traffic, mouse movements, etc ...
ii) a specific processor instruction like RDRAND available in some processors.
iii) in case of virtual machines through the virtio-rng paravirtualized device.
iv) a dedicated, external, physical device.

- To allow applications to get pseudo random numbers, at least two kernel devices exist:

a) /dev/urandom gives standard quality numbers but when asking for a lot of pseudo random numbers its quality decreases.
b/dev/random provides high quality numbers but stops when entropy is poor.

Because of the blocking behaviour, lack of entropy may cause performance problems. 

- At the CentOS device, checking the entropy generated by the kernel it yields a low value:




- Installing the rng-tools package:





- Starting the rngd service:





- Feeding random data to /dev/urandom:




- Now the entropy reaches a higher value:






3 - Generating a pair of asymmetric keys

- Working at the CentOS device a pair of keys are generated:




- gpg2 offers different asymmetric algorithms. The default is RSA:




- Taking a key size of 2048 bits:




- Specifying never expiration for the keys:




- About the key identification, name, email and comment must be provided:





- A passphrase must be entered:








- As mentioned before random bytes must be generated:





- Finally the public and private keys are generated:




- Listing the keys:



- Exporting the whitelist public key and giving a name with .pub extension:



- Listing the exported public key:




- Checking that the key file type is public:




- As expected the content is unreadable:





4 - Sending the public key

- CentOS connects via SFTP with a remote user Debian:




- CentOS sends the public key to Debian:




- The remote user Debian has successfully received the public key:




- Debian imports the public key:




- Listing the key at Debian:





5 - Encrypting a text file with the public key

- Now, the remote user Debian creates a text file:




- Debian encrypts the text file with the public key previously sent by CentOS:




- Automatically an encrypted file is created:




- Debian connects to CentOS via SFTP:




- Debian sends the encrypted file to CentOS:





6 - Decrypting the text file with the private key

 - According to mentioned at the introduction, in a public key encryption system Debian  has encrypted a message using the public key of CentOS, and the message can be decrypted only with CentOS' private key. 

- CentOS has received successfully the encrypted file:




- Now, CentOS can decrypt the file with its own private key (entering the passphrase):









- The decrypted_secret.txt decrypted by CentOS matches the original secret.txt written by Debian so the whole process has been sucessful:













Tuesday, September 5, 2017

19 - Linux Security: symmetric cryptography with GPG2 and AES algorithm


SYMMETRIC CRYPTOGRAPHY WITH GPG2 AND AES ALGORITHM

- Layout for this exercise:





1 - Introduction

- Symmetric key algorithms use the same cryptographic keys for both encryption of plaintext and decryption of ciphertext. The symmetric key is a shared secret between two or more parties that can be used to maintain a private information link.

https://en.wikipedia.org/wiki/Symmetric-key_algorithm


- The Advanced Encryption Standard (AES), also known by its original name Rijndael, is a specification for the encryption of electronic data and adopted by the U.S. government and now used worldwide. The algorithm described by AES is a symmetric-key algorithm, meaning the same key is used for both encrypting and decrypting the data.

https://es.wikipedia.org/wiki/Advanced_Encryption_Standard


gpg2 is the OpenPGP part of the GNU Privacy Guard (GnuPG). It is a tool to provide digital encryption and signing services using the OpenPGP standard:

https://en.wikipedia.org/wiki/GNU_Privacy_Guard






2 - Encrypting and decrypting (locally) a text file

- The goal of this exercise is to implement a symmetric cryptography example uusing the gpg2 Linux command and the AES algorithm.

- gpg2 supports, among others, the AES algorithm with a key of 256 bits:





















- Creating a text file and writing some plaintext:








- Encrypting the text file with the symmetric algorithm AES of 256 bits, a passphrase is required to be entered and reentered:









- As a consequence a new encrypted message is created with the extension .gpg:




- The content of message.txt.gpg is encrypted and the chipertext unreadable:




- Decrypting the file message.txt.gpg the passphrase is required:






- Let's confirm that the decryption has been successful because decrypted_message.txt and the original message.txt match their contents of the plaintext file:




3 - Decrypting a text file (remotely)

- Now, let's repeat the process implementing a potential real example where a remote user receives an encrypted message and decrypts it (using a previously shared key with the sender). 

- The sender (user lic at Ubuntu) uses the secure file transfer protocol SFTP to send the ciphertext or encrypted file message.txt.gpg:






- The receiver (user marie at Debian) successfully receives the encrypted message:




- As expected, the content of the encrypted file is unreadable:




- Decrypting, the remote user or receiver marie is invited to enter the same passphrase than the sender lic:







- Once the passphrase is entered, the receiver can read the plaintext:




- It is important to distinguish two concepts. On the one hand, the original plain text has been encrypted with the symmetric algorithm AES. On the other hand, the encrypted file has been sent over the network with SFTP. However, there are two levels of different encryption, while the first level refers to the content of the text file, the second level refers to the transport, communication or transfer of the file.