

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


The openssl utility has 46 commands which can be used to perform many cryptographic operations. The commands can be classify into 7 categories:
Version version ciphers engine errstr Benchmarking speed s time Symmetric encryption and hashing enc rand dgst passwd Asymmetric encryption and signature gendh gendsa genpkey genrsa pkey dh dsa rsa ec prime dhparam dsaparam rsautl ecparam pkeyutl pkeyparam PKI and certificat ca crl crl2pkcs7 nseq ocsp pkcs12 pkcs7 pkcs8 req spkac x509 verify Server and session asn1parse s client s server srp ts sess id S/Mine smime cms
Most of the commands are associated to asymmetric cryptography (16 commands) and PKI (12 commands). All the commands are used with options and flags. For instance, to determine the version of openssl you are using:
$ openssl version
If you want to have all the details on the openssl you are using, you just need to add the following flag:
$ openssl version -a
The following command gives all the cipher suites available in openssl:
$ openssl ciphers -v
To know if openssl has been setup with special cryptographic engines (hardware modules or CPU instruction set extension) use:
$ openssl engine
If you need help to understand a command, you can find help in the man or you can use the command with -help.
$ man enc $ openssl enc - help
Actually, there is no -help flag in openssl but this is an invalid command that will display all the options and flags for the command.
The rand command is very useful to produce symmetric keys, initialization vectors or nonces. It produces pseudo-random bytes with
The following command produces a 256-bit random value written in hexadecimal stored in the file mykey.key:
$ openssl rand 32 - out mykey. key - hex
The enc command is used to encrypt file using symmetric ciphers. enc has 2 main options:
$ openssl enc -e -aes -128 - cfb
Note that it is now even possible to use authenticated mode like CCM or GCM. By default the input and output of the enc command are the standard input and the standard output of the terminal. It can be changed using the following options:
$ openssl enc -e - in file. in - out file. out
The enc command has 2 modes:
$ openssl enc -e -aes -128 - cfb - in input. clear
The password mode has 4 options:
$ openssl enc -e -aes -128 - cfb - in input. clear
It is important to notice that even when it is not needed, the enc command always add some extra padding to the plaintext. To get rid of this padding, you can use -nopad option.
The command dgst can be used to compute digests of files and authentication tags. The main option of this command is the choice of the message digest algorithm:
$ openssl dgst - alg myfile
is the command to compute the digest of myfile using algorithm alg. For SHA256, we have for instance:
$ openssl dgst - sha256 myfile
There is 4 options to format the output of dgst:
$ openssl dgst - sha256 - hmac KEY myfile
with KEY the hexadecimal value of the key.
The passwd command produces password digests for authentication schemes using Unix format (-crypt), MD5 (-1) or Apache (apr1). The following command produces the digests of each line of file passfile:
$ openssl passwd - crypt - salt AA - in passfile
The option -salt XX is used to create all the digests using a ASCII character salt XX. If this option is not used the salt is chosen randomly for each line of the file.
The speed command can be used to measure the speed of different ciphers. The list of available ciphers for benchmark is short compared to the list of all the cipher suites available in openssl. Use speed -help to get this list. The following command benchmark all the ciphers:
$ openssl speed
This command only benchmarks AES modes of operation:
$ openssl speed aes
We use the example of RSA but the same approach works for DSA and elliptic curves.
To produce an RSA keys-pair, the command is genrsa. This main argument of this command is the key size given in bits.
openssl genrsa 2048
The main options of this command are related to the format of the output:
The command
openssl genrsa 2048 - out mykey. pem - aes
will produce a 2048-bit public and private key and store it into mykey.pem which is encrypted with AES-128 in CBC mode.
To extract the public key from mykey.pem, we use the command rsa. The format of the input and output of this command can be specified by the arguments:
openssl rsa - in mykey. pem - outform PEM \
To encrypt a file using our newly generated RSA keys, we use the rsautl command with -encrypt and -decrypt option. The following command use the standard input and output with instead of the usual -in and -out option available in most openssl commands.
echo ’ Mr Lauradoux is a genius! ’ |
openssl rsautl - encrypt - pubin - inkey
public. pem > file. out It is important to notice the usage of -pubin to tell rsautl that the input key file contains only a public key. Without this option, rsautl expects a input key file with a private and a public key. Padding is necessary with RSA. By default it uses PKCS# v1.5 but you can use any of these 4 padding schemes: -pkcs, -oaep, -ssl, -raw. To decrypt with the private key: openssl rsautl - decrypt - inkey mykey. pem \
To sign and verify a signature, we still use rsautl but this time with the -sign and -verify option. We sign with the private key: echo ’ Mr Lauradoux is stronger than Chuck
Norris! ’ | openssl rsautl - sign - inkey
mykey. pem - out file. out
and verify with the
openssl rsautl - verify - pubin - inkey
public. pem - in file. out
It is not possible to encrypt or sign large files with asymmetric cryptosystems. To circumvent this problem, we use hybrid encryption and hashing for signature. To sign a large file, we first compute its cryptographic digest and then sign the digest. Because the output of the dgst is not convenient for chaining rsautl, two options have been added to dgst: