ar_doc_04 openssl_ECC_enc_dec_signature - JohnHau/mis GitHub Wiki

Command Line Elliptic Curve Operations

OpenSSL provides two command line tools for working with keys suitable for Elliptic Curve (EC) algorithms:

openssl ecparam openssl ec The only Elliptic Curve algorithms that OpenSSL currently supports are Elliptic Curve Diffie Hellman (ECDH) for key agreement and Elliptic Curve Digital Signature Algorithm (ECDSA) for signing/verifying.

x25519, ed25519 and ed448 aren't standard EC curves so you can't use ecparams or ec subcommands to work with them. If you need to generate x25519 or ed25519 keys then see the genpkey subcommand.

Contents 1 EC Private Key File Formats 2 EC Public Key File Formats 3 Generating EC Keys and Parameters 4 See also EC Private Key File Formats By default OpenSSL will work with PEM files for storing EC private keys. These are text files containing base-64 encoded data. A typical traditional format private key file in PEM format will look something like the following, in a file with a ".pem" extension:

-----BEGIN EC PRIVATE KEY----- MIIBIAIBAQQYd8yhaE899FaH3sw8aD4F/vtpMVBLfVqmoIHKMIHHAgEBMCQGByqG SM49AQECGQD////////////////////+//////////8wSwQY//////////////// /////v/////////8BBgiEj3COVoFyqdCPa7MyUdgp9RiJWvVaRYDFQDEaWhENd6z eMS2XKlZHipXYwWaLgQxBH0pd4EAxlodoXg3FliNziuLSu6OIo8YljipDyJjczcz S0nctmptyPmXisp2SKlDsAIZAP///////////////3pi0DHIP0KU9kDsEwIBAaE0 AzIABBsl8ZSGJqcUpVoP8zekF92DGqDBMERcHhCXmgPXchP+ljybXbzYKINgxbp5 0g9/pw== -----END EC PRIVATE KEY----- Or, in an encrypted form like this:

-----BEGIN EC PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,258248872DB25390

JIzhns0nRb+pj6RONAijJli8Rhu2bIrw8D+ruHEWL1IEH6Q5tvzqAI2PDYXbSzCn 24JPWx9khmTu6ijerANNYYk0p2Pjxr12MAYpqgtXbRrXLF4AIomzYWq16BH7Y63o zvqWMBJO6tQ5RHPLM2FmweyPB/XSL7KvLTe+g6pz/W9wf52CyQ/VeK+yBXqEi7QF 0f9EKRlePRLAUcQPD4nkckcywX6Nz+TW/SOKt38YytM9MyQsAfcxu7u0nl/dLylk n57qUm3nk0z0moYJbfLx59eP0/go8VjeP2fRKkgz1DOM7VkmtPrC7vnyRpKsnP2S 6n6uacerkNXTmUcz7mTCGGfrsBeACJeX1gwinDZVwkzDxNKhLXOlFFAMWE+SeiFp kDny2v3D8sU= -----END EC PRIVATE KEY----- You may also encounter PKCS8 format private keys in PEM files. These look like this:

-----BEGIN PRIVATE KEY----- MIIBMAIBADCB0wYHKoZIzj0CATCBxwIBATAkBgcqhkjOPQEBAhkA//////////// /////////v//////////MEsEGP////////////////////7//////////AQYIhI9 wjlaBcqnQj2uzMlHYKfUYiVr1WkWAxUAxGloRDXes3jEtlypWR4qV2MFmi4EMQR9 KXeBAMZaHaF4NxZYjc4ri0rujiKPGJY4qQ8iY3M3M0tJ3LZqbcj5l4rKdkipQ7AC GQD///////////////96YtAxyD9ClPZA7BMCAQEEVTBTAgEBBBiKtwssqrxHY/gu KDD4QgmyLDKaqBv2wEWhNAMyAAT5j6o+ojeB6jaFAfx4rtGf5hYbT1N6NnlAWiP1 +bEWtTJiEVqnpeZN0m0SLybIGZY= -----END PRIVATE KEY----- Or, in an encrypted form like this:

-----BEGIN ENCRYPTED PRIVATE KEY----- MIIBWTAbBgkqhkiG9w0BBQMwDgQIGIcvnv17Q8oCAggABIIBOK+i1pk7em94F0Bn +yKxU5p7e2+cnnW/8b2mjvga0Uj8JVxRHi5eR2/u+3fjHQItq0df+qzyVC0TTCPz YZVrgHO9hPilgbGQKQQSpy9bpbGGiZ7I+aFpriEaJzugHUi8XTXY6XtnxgHAqTOX nma2HHoGRic2wNgIGKQ+B1pULy2kFDMvQ/AwvYS13uH2Trfja9M9wRqYjM2MS0Ky ii03OsNhJjZQcPmy2ALciR+umG4IQ7qszfrCA7L95F3qVXa7DgAPDZyUSdF3ucSh IlrEvaP7FeLfJ1/ilUaXK6XC9EDYPDWMErUQJZJAywczQMqjY4/pdhb8Y+TpbN/r q1I5j+JbRwfvvJV7CAHv1EEjvWiWvjHamlb7iqh3gneOYPbvSfjuaOyVd5YhwQ7P nGOah+eEf9uyDSZabg== -----END ENCRYPTED PRIVATE KEY----- PKCS8 private key files, like the above, are capable of holding many different types of private key - not just EC keys.

You can convert between these formats if you like. All of the conversion commands can read either the encrypted or unencrypted forms of the files however you must specify whether you want the output to be encrypted or not. To convert a PKCS8 file to a traditional encrypted EC format use:

openssl ec -aes-128-cbc -in p8file.pem -out tradfile.pem You can replace the first argument "aes-128-cbc" with any other valid openssl cipher name (see Manual:enc(1) for a list of valid cipher names). To convert a PKCS8 file to a traditional unencrypted EC format, just drop the first argument:

openssl ec -in p8file.pem -out tradfile.pem Or to convert from a traditional EC format to an encrypted PKCS8 format use:

openssl pkcs8 -topk8 -in tradfile.pem -out p8file.pem Or to a non-encrypted PKCS8 format use:

openssl pkcs8 -topk8 -nocrypt -in tradfile.pem -out p8file.pem Note that by default in the above traditional format EC Private Key files are not encrypted (you have to explicitly state that the file should be encrypted, and what cipher to use), whilst for PKCS8 files the opposite is true. The default is to encrypt - you have to explicitly state that you do not want encryption applied if appropriate using the "-nocrypt" option.

As well as PEM format all of the above types of key file can also be stored in DER format. This is a binary format and so is not directly human readable - unlike a PEM file. A PEM file is essentially just DER data encoded using base 64 encoding rules with a header and footer added. Often it is more convenient to work with PEM files for this reason.

The openssl commands typically have options "-inform DER" or "-outform DER" to specify that the input or output file is DER respectively. So for example the command to convert a PKCS8 file to a traditional encrypted EC format in DER is the same as above, but with the addition of "-outform DER":

openssl ec -in p8file.pem -outform DER -out tradfile.der Note that you cannot encrypt a traditional format EC Private Key in DER format (and in fact if you attempt to do so the argument is silently ignored!). The same is not true for PKCS8 files - these can still be encrypted even in DER format. So for example the following will convert a traditional format key file to an ecrypted PKCS8 format DER encoded key:

openssl pkcs8 -topk8 -in tradfile.pem -outform DER -out p8file.der EC Public Key File Formats EC Public Keys are also stored in PEM files. A typical EC public key looks as follows:

-----BEGIN PUBLIC KEY----- MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQMDMgAE+Y+qPqI3geo2hQH8eK7Rn+YWG09T ejZ5QFoj9fmxFrUyYhFap6XmTdJtEi8myBmW -----END PUBLIC KEY----- This format is used to store all types of public keys in OpenSSL not just EC keys.

It is possible to create a public key file from a private key file (although obviously not the other way around!):

openssl ec -in ecprivkey.pem -pubout -out ecpubkey.pem As above a DER encoded version can be created using "-outform DER":

openssl ec -in ecprivkey.pem -pubout -outform DER -out ecpubkey.der Generating EC Keys and Parameters An EC Parameters file contains all of the information necessary to define an Elliptic Curve that can then be used for cryptographic operations (for OpenSSL this means ECDH and ECDSA). OpenSSL contains a large set of pre-defined curves that can be used. The full list of built-in curves can be obtained through the following command:

openssl ecparam -list_curves An EC parameters file can then be generated for any of the built-in named curves as follows:

openssl ecparam -name secp256k1 -out secp256k1.pem Replace secp256k1 in the above with whichever curve you are interested in.

Keys can be generated from the ecparam command, either through a pre-existing parameters file or directly by selecting the name of the curve. To generate a private/public key pair from a pre-eixsting parameters file use the following:

openssl ecparam -in secp256k1.pem -genkey -noout -out secp256k1-key.pem Or to do the equivalent operation without a parameters file use the following:

openssl ecparam -name secp256k1 -genkey -noout -out secp256k1-key.pem Information on the parameters that have been used to generate the key are embedded in the key file itself.

By default, when creating a parameters file, or generating a key, openssl will only store the name of the curve in the generated parameters or key file, not the full set of explicit parameters associated with that name. For example:

openssl ecparam -in secp256k1.pem -text -noout This will simply confirm the name of the curve in the parameters file by printing out the following:

ASN1 OID: secp256k1 If you wish to examine the specific details of the parameters associated with a particular named curve then this can be achieved as follows:

openssl ecparam -in secp256k1.pem -text -param_enc explicit -noout The above command shows the details for a built-in named curve from a file, but this can also be done directly using the "-name" argument instead of "-in". The output will look similar to the following:

Field Type: prime-field Prime: 00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff: ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:fe:ff: ff:fc:2f A: 0 B: 7 (0x7) Generator (uncompressed): 04:79:be:66:7e:f9:dc:bb:ac:55:a0:62:95:ce:87: 0b:07:02:9b:fc:db:2d:ce:28:d9:59:f2:81:5b:16: f8:17:98:48:3a:da:77:26:a3:c4:65:5d:a4:fb:fc: 0e:11:08:a8:fd:17:b4:48:a6:85:54:19:9c:47:d0: 8f:fb:10:d4:b8 Order: 00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff: ff:fe:ba:ae:dc:e6:af:48:a0:3b:bf:d2:5e:8c:d0: 36:41:41 Cofactor: 1 (0x1) The meaning of each of these parameters is discussed further on this page.

Parameters and key files can be generated to include the full explicit parameters instead of just the name of the curve if desired. This might be important if, for example, not all the target systems know the details of the named curve. In OpenSSL version 1.0.2 new named curves have been added such as brainpool512t1. Attempting to use a parameters file or key file in versions of OpenSSL less than 1.0.2 with this curve will result in an error:

bash$ openssl ecparam -in brainpoolP512t1.pem -text -noout unable to load elliptic curve parameters 140138321110720:error:1009E077:elliptic curve routines:EC_ASN1_PKPARAMETERS2GROUP:ec group new by name failure:ec_asn1.c:1035: 140138321110720:error:1009107F:elliptic curve routines:d2i_ECPKParameters:pkparameters2group failure:ec_asn1.c:1080: 140138321110720:error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib:pem_oth.c:83: This problem can be avoided if explicit parameters are used instead. So under OpenSSL 1.0.2 you could create a parameters file like this:

openssl ecparam -name brainpoolP512t1 -out brainpoolP512t1.pem -param_enc explicit Looking at the parameters file you will notice that it is now much longer:

-----BEGIN EC PARAMETERS----- MIIBogIBATBMBgcqhkjOPQEBAkEAqt2duNvpxIs/1OauM8n8B8swjbOzydIO1mOc ynAzCHF9TZsAm8ZoQq7NoSrmo4DmKIH/Ly2CxoUoqmBWWDpI8zCBhARAqt2duNvp xIs/1OauM8n8B8swjbOzydIO1mOcynAzCHF9TZsAm8ZoQq7NoSrmo4DmKIH/Ly2C xoUoqmBWWDpI8ARAfLu8+UQc+rduGJDkaITq4yH3DAvLSYFSeJdQS+w+NqYrzfoj BJdlQPZFAIXy2uFFwiVTtGV2NokYDqJXGGdCPgSBgQRkDs5cEniHF7nBugbLwqb+ uoWEJFjFbd6dsXWNOcAxPYK6UXNc2z6kmap3p9aUOmT3o/Jf4m8GtRuqJpb6kDXa W1NL1ZX1rw+iyJI3bISs4btOMBm3FjTAETEVnK4DzunZkyGEvu8ha9cd8trfhqYn MG7P+W27i6zhmLYeAPizMgJBAKrdnbjb6cSLP9TmrjPJ/AfLMI2zs8nSDtZjnMpw MwhwVT5cQUypJhlBhmEZf6wQRx2x04EIXdrdtYeWgpypAGkCAQE= -----END EC PARAMETERS----- The full parameters are included rather than just the name. This can now be processed by versions of OpenSSL less than 1.0.2. So under 1.0.1:

openssl ecparam -in brainpoolP512t1.pem -text -noout This will correctly display the parameters, even though this version of OpenSSL does not know about this curve.

The same is true of key files. So to generate a key with explicit parameters:

openssl ecparam -name brainpoolP512t1 -genkey -noout -out brainpoolP512t1-key.pem -param_enc explicit This key file can now be processed by versions of openssl that do not know about the brainpool curve.

It should be noted however that once the parameters have been converted from the curve name format into explicit parameters it is not possible to change them back again, i.e. there is no utility to take a set of explicit parameters and work out which named curve they are associated with.

See also Elliptic Curve Cryptography Elliptic Curve Diffie Hellman Manual:ec(3) Command Line Utilities

//////////////////////////////////////////////////////////////////////////////////////////////////

Command Line Utilities

The openssl program provides a rich variety of commands, each of which often has a wealth of options and arguments. Many commands use an external configuration file for some or all of their arguments and have a -config option to specify that file. The environment variable OPENSSL_CONF can be used to specify the location of the configuration file. If the environment variable is not specified, a default file is created in the default certificate storage area called openssl.cnf. The settings in this default configuration file depend on the flags set when the version of OpenSSL being used was built.

This article is an overview of the available tools provided by OpenSSL. For all of the details on usage and implementation, you can find the manpages, which are automatically generated from the source code at the official OpenSSL project home. Likewise, the source code itself may be found on the OpenSSL project home page, as well as on the OpenSSL Github. The main OpenSSL site also includes an overview of the command-line utilities, as well as links to all of their respective documentation.

Getting Started[edit]

The entry point for the OpenSSL library is the openssl binary, usually /usr/bin/openssl on Linux. The general syntax for calling openssl is as follows:

$ openssl command [ command_options ] [ command_arguments ]

Before OpenSSL 3.0, you could call openssl without arguments to enter the interactive mode prompt and then enter commands directly, exiting with either a quit command or by issuing a termination signal with either Ctrl+C or Ctrl+D. The following is a sample interactive session in which the user invokes the prime command twice before using the quit command to terminate the session.

OpenSSL> prime -generate -bits 24
13467269
OpenSSL> prime -generate -bits 24
16651079
OpenSSL> quit

Basic Tasks[edit]

This section is a brief tutorial on performing the most basic tasks using OpenSSL. For a detailed explanation of the rationale behind the syntax and semantics of the commands shown here, see the section on Commands.

Getting Help[edit]

As mentioned previously, the general syntax of a command is openssl command [ command_options ] [ command_arguments ]. The help command is no different, but it does have its idiosyncrasies. To view the top-level help menu, you can call openssl as follows.

$ openssl help

Since OpenSSL 3.0, there are equivalent invocations such as:

$ openssl --help
$ openssl -h

This query will print all of the available commands, like so:

Standard commands
asn1parse         ca                ciphers           cmp               
cms               crl               crl2pkcs7         dgst              
dhparam           dsa               dsaparam          ec 
...

Note the above output was truncated, so only the first four lines of output are shown.

The same output is obtained also with

$ openssl list -standard-commands

A help menu for each command may be requested in two different ways. First, the same command used above may be repeated, followed by the name of the command to print help for.

$ openssl help genpkey

The program will then display the valid options for the given command.

$ openssl help genpkey
General options:
 -help               Display this summary
 -engine val         Use engine, possibly a hardware device
 -paramfile infile   Parameters file
 -algorithm val      The public key algorithm
 -quiet              Do not output status while generating keys
 -pkeyopt val        Set the public key algorithm option as opt:value
 -config infile      Load a configuration file (this may load modules)
Output options:
 -out outfile        Output file
 -outform PEM|DER    output format (DER or PEM)
 -pass val           Output file pass phrase source
 -genparam           Generate parameters, not key
 -text               Print the in text
 -*                  Cipher to use to encrypt the key
Provider options:
 -provider-path val  Provider load path (must be before 'provider' argument if required)
 -provider val       Provider to load (can be specified multiple times)
 -propquery val      Property query used when fetching algorithms
Order of options may be important!  See the documentation.

The second way of requesting the help menu for a particular command is by using the first option in the output shown above, namely openssl command -help. Both commands will yield the same output; the help menu displayed will be exactly the same.

For additional information on the usage of a particular command, the project manpages are the definite source of information. The manpages may be views in a shell as usual, e.g.

$ man openssl
$ man openssl-genpkey
$ man genpkey

Another way of accessing the manpages is via the project perldocs. perldoc is a utility included with most if not all Perl distributions, and it's capable of displaying documentation information in a variety of formats, one of which is as manpages. Not surprisingly, the project documentation is generated from the pod files located in the doc directory of the source code.

Getting Library Version Information[edit]

$ openssl version
OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022)

As mentioned above, the version command's help menu may be queried for additional options like so:

$ openssl version -help
Usage: version [options]
General options:
 -help  Display this summary
Output options:
 -a     Show all data
 -b     Show build date
 -d     Show configuration directory
 -e     Show engines directory
 -m     Show modules directory
 -f     Show compiler flags used
 -o     Show some internal datatype options
 -p     Show target build platform
 -r     Show random seeding options
 -v     Show library version
 -c     Show CPU settings info

Using the -a option to show all version information yields the following output on my current machine:

$ openssl version -a
OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022)
built on: Fri Jun 24 08:58:53 2022 UTC
platform: linux-x86_64
options:  bn(64,64)
compiler: gcc -fPIC -pthread -m64 -Wa,--noexecstack -Wall -O3 -DOPENSSL_USE_NODELETE -DL_ENDIAN -DOPENSSL_PIC -DOPENSSL_BUILDING_OPENSSL -DNDEBUG
OPENSSLDIR: "/usr/local/ssl"
ENGINESDIR: "/usr/local/lib64/engines-3"
MODULESDIR: "/usr/local/lib64/ossl-modules"
Seeding source: os-specific
CPUINFO: OPENSSL_ia32cap=0x7ffaf3ffffebffff:0x29c67af


Commands[edit]

There are three different kinds of commands. These are standard commands, cipher commands, and message digest commands. Calling the OpenSSL top-level help command with no arguments will result in openssl printing all available commands by group, sorted alphabetically.

Standard Commands[edit]

Command Description
asn1parse Parse an ASN.1 sequence.
ca Certificate Authority (CA) Management.
ciphers Cipher Suite Description Determination.
cmp Certificate Management Protocol (CMP, RFC 4210) application.
cms CMS (Cryptographic Message Syntax) utility.
crl Certificate Revocation List (CRL) Management.
crl2pkcs7 CRL to PKCS#7 Conversion.
dgst Message Digest calculation. MAC calculations are superseded by mac(1).
dhparam Generation and Management of Diffie-Hellman Parameters. Superseded by genpkey(1) and pkeyparam(1).
dsa DSA Data Management.
dsaparam DSA Parameter Generation and Management. Superseded by genpkey(1) and pkeyparam(1).
ec EC (Elliptic curve) key processing.
ecparam EC parameter manipulation and generation.
enc Symmetric cipher routines.
engine Engine (loadable module) information and manipulation.
errstr Error Number to Error String Conversion.
fipsinstall IPS configuration installation.
gendsa Generation of DSA Private Key from Parameters. Superseded by genpkey(1) and pkey(1).
genpkey Generation of Private Key or Parameters.
genrsa Generation of RSA Private Key. Superseded by genpkey(1).
help Display information about a command's options.
info Display diverse information built into the OpenSSL libraries.
kdf Key Derivation Functions.
list List algorithms and features.
mac Message Authentication Code Calculation.
nseq Create or examine a Netscape certificate sequence.
ocsp Online Certificate Status Protocol utility.
passwd Generation of hashed passwords.
pkcs12 PKCS#12 Data Management.
pkcs7 PKCS#7 Data Management.
pkcs8 PKCS#8 format private key conversion tool.
pkey Public and private key management.
pkeyparam Public key algorithm parameter management.
pkeyutl Public key algorithm cryptographic operation utility.
prime Compute prime numbers.
rand Generate pseudo-random bytes -- see Random Numbers
rehash Create symbolic links to certificate and CRL files named by the hash values.
req PKCS#10 X.509 Certificate Signing Request (CSR) Management.
rsa RSA key management.
rsautl RSA utility for signing, verification, encryption, and decryption. Superseded by pkeyutl(1).
s_client This implements a generic SSL/TLS client which can establish a transparent connection to a remote server speaking SSL/TLS.
s_server This implements a generic SSL/TLS server which accepts connections from remote clients speaking SSL/TLS.
s_time SSL Connection Timer.
sess_id SSL Session Data Management.
smime S/MIME mail processing.
speed Algorithm Speed Measurement.
spkac SPKAC printing and generating utility.
srp Maintain SRP password file.
storeutl Utility to list and display certificates, keys, CRLs, etc.
ts Time Stamping Authority tool (client/server).
verify X.509 Certificate Verification.
version OpenSSL Version Information.
x509 X.509 Certificate Data Management.

Generating an RSA Private Key[edit]

Generating a private key can be done in a variety of different ways depending on the type of key, algorithm, bits, and other options your specific use case may require. In this example, we are generating a private key using RSA and a key size of 2048 bits.

$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem

To generate a password protected private key, the previous command may be slightly amended as follows:

$ openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem

The addition of the -aes256 option specifies the cipher to use to encrypt the private key file. For a list of available ciphers in the library, you can run the following command:

$ openssl list -cipher-algorithms

With your private key in hand, you can use the following command to see the key's details, such as its modulus and its constituent primes. Remember to change the name of the input file to the file name of your private key.

$ openssl pkey -in private-key.pem -text

The above command yields the following output in my specific case. Your output will differ but should be structurally similar.

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDZD6IMLRFk4CaF
w0rhRienwuE5EZ6xFE8e3C5TVi1+d9Enhi38RgkwD7UlWxPE6AWhp5T3kfrFWdak
1lZFVPp7/btOKLjKUru15nLoA4AKYtz9W9PhsM0dyzLc6FQ6K4ReQam5pHCqI2zF
82MwE+eIAduvuqyoQLKiI608EArWZqDtMUpBJzv0UVEYvRdnMWpCwfzpI+hPJywV
CcTlNCT/ctGgBKyIx+dDuZ7bR9MNmSW7GreJEbTH+R13xT3dd/JCka1+LYCl4h0q
oWhFPhOkvQzmmSzUmZlAlTDQLv2eAdJIrQcsnKZ3SsIOCC/3IpqwSzpid38Ill4O
xH6XIrVFAgMBAAECggEBAJ2MC0JrM8TULSHJrf/0u7O4b2DMuTIuW386sSUr17mD
nfviGF6TNvf7bq++e4rgHbZHvIg1HJ9Bpdne+J86HtUARYNlazru8fAFZEGiyLzB
JUV/8TpO6ZJGepR8zSWrkFgZsOddw6i6LalADy5GRDcjoiDajZdR3lZxLrv5qOQU
I1vKTf4Zs2Tl3gnaJ/Il1gBHIQ9W9xUH8jPBIwj51iXwCh8H0BiDPvFkU7cHIFCP
sJhGsGp6OS3uSwwQuSE+NqbuPfVilysCcwgZduknyio0QO1YfMBL6+XoKE/bFHsn
N+FzzczQg9sWyiwVR+3EeI9kp4JSElNh2nqG96i4QAECgYEA76OLUGrShHb4saoP
aYnBAKLEdWj5K483JdY6BSbdd5RkDbJG8ExmcbfTas/BGdKc4iVCkxV3ysxKnX18
PfxATHDLL8NMa+gGgZY5oTKUsrXEpS132HhCJ9T9LoesQjRb4kOZH8POVqm6O4Xf
lCt0y1+M1eQHI1NPO9CmPBgouEUCgYEA5+F4SS8RMyYRkU/kx195fwh0hhaOElzr
E8mZou3NFL/XT6/9t+2+7sMTuiQCP9zIa6s+/rrXdjWtrTcDp4WlDITas0UUgZhv
YVBQBF4vhHxIVwJxnT9Gwi4XM1JlFmVHofWD71P6DRe7jSWRS3CujP3AE9vmpWMx
tE1D9qLiWQECgYB445LzFYBvrKjWz4iI4CJKFNJwvGz+iXfzkXehg7KzkVtMAYSB
0rjXYzm3J2ktgq778nn8Qxc0agy2GEil6GvzY+9MgAQ8Z0do9gTKif6zjLjP7vkH
bdtJxsuWPoEqwMkdgqZrfNbJp0O4pVddovJ/agtdF3R2YJ+W+DH0HOfl1QKBgFnM
c2zEEYEhaQRBUHP1gXO0rouPCI4L9e2/0QPL2/QBJzzxBuzH4X1NhsI7V7OrqOIp
e0fiy7Y3q369I2ko1HY4rQln4z0c72VcWOCYKQbBqrInfCBNdPWWK93wNr2pk0gh
cGqqtteDLVrIBbCVfsOTMWN/cZ7y/zi4A23sPoQBAoGAEPzcIjOyoB97Pzd7iNim
Gin8RkwXIiFGSHo8vAh74CKBNokThM50OUNm5T2eJ4huzPpowQ+ID1mB5EjEai9n
JY9ll3cUpawiIIW/6uGTHyXfvZWNtqEYXrVJ6fcDaKcW4y3cplNj/SJaBW8HXsW7
YGHW3zHsgy7EOAOzPwlm9oE=
-----END PRIVATE KEY-----
RSA Private-Key: (2048 bit, 2 primes)
modulus:
    00:d9:0f:a2:0c:2d:11:64:e0:26:85:c3:4a:e1:46:
    27:a7:c2:e1:39:11:9e:b1:14:4f:1e:dc:2e:53:56:
    2d:7e:77:d1:27:86:2d:fc:46:09:30:0f:b5:25:5b:
    13:c4:e8:05:a1:a7:94:f7:91:fa:c5:59:d6:a4:d6:
    56:45:54:fa:7b:fd:bb:4e:28:b8:ca:52:bb:b5:e6:
    72:e8:03:80:0a:62:dc:fd:5b:d3:e1:b0:cd:1d:cb:
    32:dc:e8:54:3a:2b:84:5e:41:a9:b9:a4:70:aa:23:
    6c:c5:f3:63:30:13:e7:88:01:db:af:ba:ac:a8:40:
    b2:a2:23:ad:3c:10:0a:d6:66:a0:ed:31:4a:41:27:
    3b:f4:51:51:18:bd:17:67:31:6a:42:c1:fc:e9:23:
    e8:4f:27:2c:15:09:c4:e5:34:24:ff:72:d1:a0:04:
    ac:88:c7:e7:43:b9:9e:db:47:d3:0d:99:25:bb:1a:
    b7:89:11:b4:c7:f9:1d:77:c5:3d:dd:77:f2:42:91:
    ad:7e:2d:80:a5:e2:1d:2a:a1:68:45:3e:13:a4:bd:
    0c:e6:99:2c:d4:99:99:40:95:30:d0:2e:fd:9e:01:
    d2:48:ad:07:2c:9c:a6:77:4a:c2:0e:08:2f:f7:22:
    9a:b0:4b:3a:62:77:7f:08:96:5e:0e:c4:7e:97:22:
    b5:45
publicExponent: 65537 (0x10001)
privateExponent:
    00:9d:8c:0b:42:6b:33:c4:d4:2d:21:c9:ad:ff:f4:
    bb:b3:b8:6f:60:cc:b9:32:2e:5b:7f:3a:b1:25:2b:
    d7:b9:83:9d:fb:e2:18:5e:93:36:f7:fb:6e:af:be:
    7b:8a:e0:1d:b6:47:bc:88:35:1c:9f:41:a5:d9:de:
    f8:9f:3a:1e:d5:00:45:83:65:6b:3a:ee:f1:f0:05:
    64:41:a2:c8:bc:c1:25:45:7f:f1:3a:4e:e9:92:46:
    7a:94:7c:cd:25:ab:90:58:19:b0:e7:5d:c3:a8:ba:
    2d:a9:40:0f:2e:46:44:37:23:a2:20:da:8d:97:51:
    de:56:71:2e:bb:f9:a8:e4:14:23:5b:ca:4d:fe:19:
    b3:64:e5:de:09:da:27:f2:25:d6:00:47:21:0f:56:
    f7:15:07:f2:33:c1:23:08:f9:d6:25:f0:0a:1f:07:
    d0:18:83:3e:f1:64:53:b7:07:20:50:8f:b0:98:46:
    b0:6a:7a:39:2d:ee:4b:0c:10:b9:21:3e:36:a6:ee:
    3d:f5:62:97:2b:02:73:08:19:76:e9:27:ca:2a:34:
    40:ed:58:7c:c0:4b:eb:e5:e8:28:4f:db:14:7b:27:
    37:e1:73:cd:cc:d0:83:db:16:ca:2c:15:47:ed:c4:
    78:8f:64:a7:82:52:12:53:61:da:7a:86:f7:a8:b8:
    40:01
prime1:
    00:ef:a3:8b:50:6a:d2:84:76:f8:b1:aa:0f:69:89:
    c1:00:a2:c4:75:68:f9:2b:8f:37:25:d6:3a:05:26:
    dd:77:94:64:0d:b2:46:f0:4c:66:71:b7:d3:6a:cf:
    c1:19:d2:9c:e2:25:42:93:15:77:ca:cc:4a:9d:7d:
    7c:3d:fc:40:4c:70:cb:2f:c3:4c:6b:e8:06:81:96:
    39:a1:32:94:b2:b5:c4:a5:2d:77:d8:78:42:27:d4:
    fd:2e:87:ac:42:34:5b:e2:43:99:1f:c3:ce:56:a9:
    ba:3b:85:df:94:2b:74:cb:5f:8c:d5:e4:07:23:53:
    4f:3b:d0:a6:3c:18:28:b8:45
prime2:
    00:e7:e1:78:49:2f:11:33:26:11:91:4f:e4:c7:5f:
    79:7f:08:74:86:16:8e:12:5c:eb:13:c9:99:a2:ed:
    cd:14:bf:d7:4f:af:fd:b7:ed:be:ee:c3:13:ba:24:
    02:3f:dc:c8:6b:ab:3e:fe:ba:d7:76:35:ad:ad:37:
    03:a7:85:a5:0c:84:da:b3:45:14:81:98:6f:61:50:
    50:04:5e:2f:84:7c:48:57:02:71:9d:3f:46:c2:2e:
    17:33:52:65:16:65:47:a1:f5:83:ef:53:fa:0d:17:
    bb:8d:25:91:4b:70:ae:8c:fd:c0:13:db:e6:a5:63:
    31:b4:4d:43:f6:a2:e2:59:01
exponent1:
    78:e3:92:f3:15:80:6f:ac:a8:d6:cf:88:88:e0:22:
    4a:14:d2:70:bc:6c:fe:89:77:f3:91:77:a1:83:b2:
    b3:91:5b:4c:01:84:81:d2:b8:d7:63:39:b7:27:69:
    2d:82:ae:fb:f2:79:fc:43:17:34:6a:0c:b6:18:48:
    a5:e8:6b:f3:63:ef:4c:80:04:3c:67:47:68:f6:04:
    ca:89:fe:b3:8c:b8:cf:ee:f9:07:6d:db:49:c6:cb:
    96:3e:81:2a:c0:c9:1d:82:a6:6b:7c:d6:c9:a7:43:
    b8:a5:57:5d:a2:f2:7f:6a:0b:5d:17:74:76:60:9f:
    96:f8:31:f4:1c:e7:e5:d5
exponent2:
    59:cc:73:6c:c4:11:81:21:69:04:41:50:73:f5:81:
    73:b4:ae:8b:8f:08:8e:0b:f5:ed:bf:d1:03:cb:db:
    f4:01:27:3c:f1:06:ec:c7:e1:7d:4d:86:c2:3b:57:
    b3:ab:a8:e2:29:7b:47:e2:cb:b6:37:ab:7e:bd:23:
    69:28:d4:76:38:ad:09:67:e3:3d:1c:ef:65:5c:58:
    e0:98:29:06:c1:aa:b2:27:7c:20:4d:74:f5:96:2b:
    dd:f0:36:bd:a9:93:48:21:70:6a:aa:b6:d7:83:2d:
    5a:c8:05:b0:95:7e:c3:93:31:63:7f:71:9e:f2:ff:
    38:b8:03:6d:ec:3e:84:01
coefficient:
    10:fc:dc:22:33:b2:a0:1f:7b:3f:37:7b:88:d8:a6:
    1a:29:fc:46:4c:17:22:21:46:48:7a:3c:bc:08:7b:
    e0:22:81:36:89:13:84:ce:74:39:43:66:e5:3d:9e:
    27:88:6e:cc:fa:68:c1:0f:88:0f:59:81:e4:48:c4:
    6a:2f:67:25:8f:65:97:77:14:a5:ac:22:20:85:bf:
    ea:e1:93:1f:25:df:bd:95:8d:b6:a1:18:5e:b5:49:
    e9:f7:03:68:a7:16:e3:2d:dc:a6:53:63:fd:22:5a:
    05:6f:07:5e:c5:bb:60:61:d6:df:31:ec:83:2e:c4:
    38:03:b3:3f:09:66:f6:81

Keep in mind the above key was generated solely for pedagogical purposes; never give anyone access to your private keys.

Generating a Public Key[edit]

Having previously generated your private key, you may generate the corresponding public key using the following command.

$ openssl pkey -in private-key.pem -out public-key.pem -pubout

You may once again view the key details, using a slightly different command this time.

$ openssl pkey -in public-key.pem -pubin -text

The output for the public key will be shorter, as it carries much less information, and it will look something like this.

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2Q+iDC0RZOAmhcNK4UYn
p8LhORGesRRPHtwuU1YtfnfRJ4Yt/EYJMA+1JVsTxOgFoaeU95H6xVnWpNZWRVT6
e/27Tii4ylK7teZy6AOACmLc/VvT4bDNHcsy3OhUOiuEXkGpuaRwqiNsxfNjMBPn
iAHbr7qsqECyoiOtPBAK1mag7TFKQSc79FFRGL0XZzFqQsH86SPoTycsFQnE5TQk
/3LRoASsiMfnQ7me20fTDZkluxq3iRG0x/kdd8U93XfyQpGtfi2ApeIdKqFoRT4T
pL0M5pks1JmZQJUw0C79ngHSSK0HLJymd0rCDggv9yKasEs6Ynd/CJZeDsR+lyK1
RQIDAQAB
-----END PUBLIC KEY-----
RSA Public-Key: (2048 bit)
Modulus:
    00:d9:0f:a2:0c:2d:11:64:e0:26:85:c3:4a:e1:46:
    27:a7:c2:e1:39:11:9e:b1:14:4f:1e:dc:2e:53:56:
    2d:7e:77:d1:27:86:2d:fc:46:09:30:0f:b5:25:5b:
    13:c4:e8:05:a1:a7:94:f7:91:fa:c5:59:d6:a4:d6:
    56:45:54:fa:7b:fd:bb:4e:28:b8:ca:52:bb:b5:e6:
    72:e8:03:80:0a:62:dc:fd:5b:d3:e1:b0:cd:1d:cb:
    32:dc:e8:54:3a:2b:84:5e:41:a9:b9:a4:70:aa:23:
    6c:c5:f3:63:30:13:e7:88:01:db:af:ba:ac:a8:40:
    b2:a2:23:ad:3c:10:0a:d6:66:a0:ed:31:4a:41:27:
    3b:f4:51:51:18:bd:17:67:31:6a:42:c1:fc:e9:23:
    e8:4f:27:2c:15:09:c4:e5:34:24:ff:72:d1:a0:04:
    ac:88:c7:e7:43:b9:9e:db:47:d3:0d:99:25:bb:1a:
    b7:89:11:b4:c7:f9:1d:77:c5:3d:dd:77:f2:42:91:
    ad:7e:2d:80:a5:e2:1d:2a:a1:68:45:3e:13:a4:bd:
    0c:e6:99:2c:d4:99:99:40:95:30:d0:2e:fd:9e:01:
    d2:48:ad:07:2c:9c:a6:77:4a:c2:0e:08:2f:f7:22:
    9a:b0:4b:3a:62:77:7f:08:96:5e:0e:c4:7e:97:22:
    b5:45
Exponent: 65537 (0x10001)

For more information on generating keys, see the source code documentation, located in the doc/HOWTO/keys.txt file.

Generating Keys Based on Elliptic Curves[edit]

There are essentially two steps to generating a key:

  1. Generate the parameters for the specific curve you are using
  2. Use those parameters to generate the key

To see the list of curves instrinsically supported by openssl, you can use the -list_curves</t> option when calling the ecparam command.

$ openssl ecparam -list_curves
  secp112r1 : SECG/WTLS curve over a 112 bit prime field
  secp112r2 : SECG curve over a 112 bit prime field
  secp128r1 : SECG curve over a 128 bit prime field
  secp128r2 : SECG curve over a 128 bit prime field
  secp160k1 : SECG curve over a 160 bit prime field
  ...

For this example I will use the prime256v1 curve, which is an X9.62/SECG curve over a 256 bit prime field.

Generating the Curve Parameters[edit]

Having selected our curve, we now call ecparam to generate our parameters file.

$ openssl ecparam -name prime256v1 -out prime256v1.pem

Printing Parameters to Standard Out[edit]

You can print the generated curve parameters to the terminal output with the following command:

$ openssl ecparam -in prime256v1.pem -noout -text
ASN1 OID: prime256v1
NIST CURVE: P-256

Printing Parameters as C Code[edit]

Analogously, you may also output the generated curve parameters as C code. The parameters can then be loaded by calling the get_ec_group_XXX() function. To print the C code to the current terminal's output, the following command may be used:

$ openssl ecparam -in prime256v1.pem -noout -C

And here are the first few lines of the corresponding output:

EC_GROUP *get_ec_group_256(void)
{
    static unsigned char ec_p_256[] = {
        0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        ...

Generating the Key[edit]

With the curve parameters in hand, we are now free to generate the key. Just as with the [#Generating an RSA Private Key|RSA] example above, we may optionally specify a cipher algorithm with which to encrypt the private key. The call to generate the key using the elliptic curve parameters generated in the example above looks like this:

$ openssl genpkey -aes256 -paramfile prime256v1.pem -out private-key.pem
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

Putting it All Together[edit]

The process of generation a curve based on elliptic-curves can be streamlined by calling the genpkey command directly and specifying both the algorithm and the name of the curve to use for parameter generation. In it's simplest form, the command to generate a key based on the same curve as in the example above looks like this:

$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256

This command will result in the generated key being printed to the terminal's output.

$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256

-----BEGIN PRIVATE KEY----- MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgqqYoJGowXJ5/GTkB SRLnBMNWLoQ2RM/QxrY+bfDDGRahRANCAASPY4eTANkwIIAWhh32eoFl2YFLJSWy bdITdZ82O5JDpDijmGmJ2hepe5afek9WVqxMPYjmbTwMPO3xMGbqUiJD -----END PRIVATE KEY-----

Remember that you can specify a cipher algorithm to encrypt the key with, which something you may or may not want to do, depending on your specific use case. Here is a slightly more complete example showing a key generated with a password and written to a specific output file.

$ openssl genpkey -aes256 -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private-key.pem
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

Just as with the previous example, you can use the pkey command to inspect your newly-generated key.

$ openssl pkey -in private-key.pem -text
Enter pass phrase for private-key.pem:
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgEO7CxgTwi0hsjdbp
sXWuU2x2flLthxqXabYDOqOZCvuhRANCAAQVTLkeCBJdvMnqwZKYJxrPvTTuanrD
NkyAPQCARKsQ7bVrP6ky/5uAcAvjuZB0xKCcSp7roXLWRzD/y/ik8P5R
-----END PRIVATE KEY-----
Private-Key: (256 bit)
priv:
10:ee:c2:c6:04:f0:8b:48:6c:8d:d6:e9:b1:75:ae:
53:6c:76:7e:52:ed:87:1a:97:69:b6:03:3a:a3:99:
0a:fb
pub:
04:15:4c:b9:1e:08:12:5d:bc:c9:ea:c1:92:98:27:
1a:cf:bd:34:ee:6a:7a:c3:36:4c:80:3d:00:80:44:
ab:10:ed:b5:6b:3f:a9:32:ff:9b:80:70:0b:e3:b9:
90:74:c4:a0:9c:4a:9e:eb:a1:72:d6:47:30:ff:cb:
f8:a4:f0:fe:51
ASN1 OID: prime256v1
NIST CURVE: P-256

For more details on elliptic curve cryptography or key generation, check out the manpages.

Base64 Encoding Strings[edit]

For simple string encoding, you can use "here string" syntax with the base64 command as below. Intuitively, the -e flag specifies the action to be encoding.

$ openssl base64 -e <<< 'Welcome to openssl wiki'
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK

Similarly, the base64 command's -d flag may be used to indicate decoding mode.

$ openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'
Welcome to openssl wiki


Note: base64 line length is limited to 76 characters by default in openssl (and generated with 64 characters per line).

openssl base64 -e <<< 'Welcome to openssl wiki with a very long line that splits...'
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRo
YXQgc3BsaXRzLi4uCg==
openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='

=> NOTHING!

To be able to decode a base64 line without line feeds that exceeds the default 76 character length restriction use the -A option.

openssl base64 -d -A <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
Welcome to openssl wiki with a very long line that splits...

It is recommended to actually split base64 strings into multiple lines of 64 characters, however, since the -A option is buggy, particularly with its handling of long files.

Generating a File Hash[edit]

One of the most basic uses of the dgst command (short for digest) is viewing the hash of a given file. To do this, simply invoke the command with the specified digest algorithm to use. For this example, I will be hashing an arbitrary file on my system using the MD5, SHA1, and SHA384 algorithms.

$ openssl dgst -md5 primes.dat
MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664

$ openssl dgst -sha1 primes.dat SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32

$ openssl dgst -sha384 primes.dat SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271

For a list of the available digest algorithms, you can use the following command.

$ openssl list -digest-algorithms
RSA-MD4 => MD4
RSA-MD5 => MD5
RSA-MDC2 => MDC2
RSA-RIPEMD160 => RIPEMD160
RSA-SHA1 => SHA1
RSA-SHA1-2 => RSA-SHA1
...

You can also use a similar command to see the available digest commands:

$ openssl list -digest-commands
blake2b512        blake2s256        md5               sha1
sha224 sha256 sha3-224 sha3-256
sha3-384 sha3-512 sha384 sha512
sha512-224 sha512-256 shake128 shake256
sm3

Below are three sample invocations of the md5, sha1, and sha384 digest commands using the same file as the dgst command invocation above.

$ openssl md5 primes.dat
MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664

$ openssl sha1 primes.dat SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32

$ openssl sha384 primes.dat SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271

File Encryption and Decryption[edit]

The following example demonstrates a simple file encryption and decryption using the enc command. The first argument is the cipher algorithm to use for encrypting the file. For this example I carefully selected the AES-256 algorithm in CBC Mode by looking up the available ciphers and picking out the first one I saw. To see the list of available ciphers, you can use the following command.

$ openssl enc -ciphers
Supported ciphers:
-aes-128-cbc               -aes-128-cfb               -aes-128-cfb1
-aes-128-cfb8 -aes-128-ctr -aes-128-ecb
-aes-128-ofb -aes-192-cbc -aes-192-cfb
-aes-192-cfb1 -aes-192-cfb8 -aes-192-ctr ...

You can also use the following command:

$ openssl list -cipher-algorithms
AES-128-CBC
AES-128-CBC-HMAC-SHA1
AES-128-CBC-HMAC-SHA256
id-aes128-CCM
AES-128-CFB
AES-128-CFB1
AES-128-CFB8
AES-128-CTR
...

Having selected an encryption algorithm, you must then specify whether the action you are taking is either encryption or decryption via the -e or -d flags, respectively. The -iter flag specifies the number of iterations on the password used for deriving the encryption key. A higher iteration count increases the time required to brute-force the resulting file. Using this option implies enabling use of the Password-Based Key Derivation Function 2, usually set using the -pbkdf2 flag. We then use the -salt flag to enable the use of a randomly generated salt in the key-derivation function.

Putting it all together, you can see the command to encrypt a file and the corresponding output below. Note that the passwords entered by the user are blank, just as they would usually be in a terminal session.

$ openssl enc -aes-256-cbc -e -iter 1000 -salt -in primes.dat -out primes.enc
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:

The analogous decryption command is as follows:

$ openssl enc -aes-256-cbc -d -iter 1000 -in primes.enc -out primes.dec
enter aes-256-cbc decryption password:

Further reading[edit]

  • Paul Heinlein. "OpenSSL Command-Line HOWTO". Has many quick cookbook-style recipes for doing common tasks using the "openssl" command-line application.

Command Line Utilities The openssl program provides a rich variety of commands, each of which often has a wealth of options and arguments. Many commands use an external configuration file for some or all of their arguments and have a -config option to specify that file. The environment variable OPENSSL_CONF can be used to specify the location of the configuration file. If the environment variable is not specified, a default file is created in the default certificate storage area called openssl.cnf. The settings in this default configuration file depend on the flags set when the version of OpenSSL being used was built.

This article is an overview of the available tools provided by OpenSSL. For all of the details on usage and implementation, you can find the manpages, which are automatically generated from the source code at the official OpenSSL project home. Likewise, the source code itself may be found on the OpenSSL project home page, as well as on the OpenSSL Github. The main OpenSSL site also includes an overview of the command-line utilities, as well as links to all of their respective documentation.

Contents 1 Getting Started 2 Basic Tasks 2.1 Getting Help 2.2 Getting Library Version Information 3 Commands 3.1 Standard Commands 3.2 Generating an RSA Private Key 3.3 Generating a Public Key 3.4 Generating Keys Based on Elliptic Curves 3.4.1 Generating the Curve Parameters 3.4.1.1 Printing Parameters to Standard Out 3.4.1.2 Printing Parameters as C Code 3.4.2 Generating the Key 3.4.3 Putting it All Together 3.5 Base64 Encoding Strings 3.6 Generating a File Hash 3.7 File Encryption and Decryption 4 Further reading Getting Started The entry point for the OpenSSL library is the openssl binary, usually /usr/bin/openssl on Linux. The general syntax for calling openssl is as follows:

$ openssl command [ command_options ] [ command_arguments ] Before OpenSSL 3.0, you could call openssl without arguments to enter the interactive mode prompt and then enter commands directly, exiting with either a quit command or by issuing a termination signal with either Ctrl+C or Ctrl+D. The following is a sample interactive session in which the user invokes the prime command twice before using the quit command to terminate the session.

OpenSSL> prime -generate -bits 24 13467269 OpenSSL> prime -generate -bits 24 16651079 OpenSSL> quit Basic Tasks This section is a brief tutorial on performing the most basic tasks using OpenSSL. For a detailed explanation of the rationale behind the syntax and semantics of the commands shown here, see the section on Commands.

Getting Help As mentioned previously, the general syntax of a command is openssl command [ command_options ] [ command_arguments ]. The help command is no different, but it does have its idiosyncrasies. To view the top-level help menu, you can call openssl as follows.

$ openssl help Since OpenSSL 3.0, there are equivalent invocations such as:

$ openssl --help $ openssl -h This query will print all of the available commands, like so:

Standard commands asn1parse ca ciphers cmp
cms crl crl2pkcs7 dgst
dhparam dsa dsaparam ec ... Note the above output was truncated, so only the first four lines of output are shown.

The same output is obtained also with

$ openssl list -standard-commands A help menu for each command may be requested in two different ways. First, the same command used above may be repeated, followed by the name of the command to print help for.

$ openssl help genpkey The program will then display the valid options for the given command.

$ openssl help genpkey General options: -help Display this summary -engine val Use engine, possibly a hardware device -paramfile infile Parameters file -algorithm val The public key algorithm -quiet Do not output status while generating keys -pkeyopt val Set the public key algorithm option as opt:value -config infile Load a configuration file (this may load modules) Output options: -out outfile Output file -outform PEM|DER output format (DER or PEM) -pass val Output file pass phrase source -genparam Generate parameters, not key -text Print the in text -* Cipher to use to encrypt the key Provider options: -provider-path val Provider load path (must be before 'provider' argument if required) -provider val Provider to load (can be specified multiple times) -propquery val Property query used when fetching algorithms Order of options may be important! See the documentation. The second way of requesting the help menu for a particular command is by using the first option in the output shown above, namely openssl command -help. Both commands will yield the same output; the help menu displayed will be exactly the same.

For additional information on the usage of a particular command, the project manpages are the definite source of information. The manpages may be views in a shell as usual, e.g.

$ man openssl $ man openssl-genpkey $ man genpkey Another way of accessing the manpages is via the project perldocs. perldoc is a utility included with most if not all Perl distributions, and it's capable of displaying documentation information in a variety of formats, one of which is as manpages. Not surprisingly, the project documentation is generated from the pod files located in the doc directory of the source code.

Getting Library Version Information $ openssl version OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022) As mentioned above, the version command's help menu may be queried for additional options like so:

$ openssl version -help Usage: version [options] General options: -help Display this summary Output options: -a Show all data -b Show build date -d Show configuration directory -e Show engines directory -m Show modules directory -f Show compiler flags used -o Show some internal datatype options -p Show target build platform -r Show random seeding options -v Show library version -c Show CPU settings info Using the -a option to show all version information yields the following output on my current machine:

$ openssl version -a OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022) built on: Fri Jun 24 08:58:53 2022 UTC platform: linux-x86_64 options: bn(64,64) compiler: gcc -fPIC -pthread -m64 -Wa,--noexecstack -Wall -O3 -DOPENSSL_USE_NODELETE -DL_ENDIAN -DOPENSSL_PIC -DOPENSSL_BUILDING_OPENSSL -DNDEBUG OPENSSLDIR: "/usr/local/ssl" ENGINESDIR: "/usr/local/lib64/engines-3" MODULESDIR: "/usr/local/lib64/ossl-modules" Seeding source: os-specific CPUINFO: OPENSSL_ia32cap=0x7ffaf3ffffebffff:0x29c67af

Commands There are three different kinds of commands. These are standard commands, cipher commands, and message digest commands. Calling the OpenSSL top-level help command with no arguments will result in openssl printing all available commands by group, sorted alphabetically.

Standard Commands Overview of OpenSSL's command line utilities Command Description asn1parse Parse an ASN.1 sequence. ca Certificate Authority (CA) Management. ciphers Cipher Suite Description Determination. cmp Certificate Management Protocol (CMP, RFC 4210) application. cms CMS (Cryptographic Message Syntax) utility. crl Certificate Revocation List (CRL) Management. crl2pkcs7 CRL to PKCS#7 Conversion. dgst Message Digest calculation. MAC calculations are superseded by mac(1). dhparam Generation and Management of Diffie-Hellman Parameters. Superseded by genpkey(1) and pkeyparam(1). dsa DSA Data Management. dsaparam DSA Parameter Generation and Management. Superseded by genpkey(1) and pkeyparam(1). ec EC (Elliptic curve) key processing. ecparam EC parameter manipulation and generation. enc Symmetric cipher routines. engine Engine (loadable module) information and manipulation. errstr Error Number to Error String Conversion. fipsinstall IPS configuration installation. gendsa Generation of DSA Private Key from Parameters. Superseded by genpkey(1) and pkey(1). genpkey Generation of Private Key or Parameters. genrsa Generation of RSA Private Key. Superseded by genpkey(1). help Display information about a command's options. info Display diverse information built into the OpenSSL libraries. kdf Key Derivation Functions. list List algorithms and features. mac Message Authentication Code Calculation. nseq Create or examine a Netscape certificate sequence. ocsp Online Certificate Status Protocol utility. passwd Generation of hashed passwords. pkcs12 PKCS#12 Data Management. pkcs7 PKCS#7 Data Management. pkcs8 PKCS#8 format private key conversion tool. pkey Public and private key management. pkeyparam Public key algorithm parameter management. pkeyutl Public key algorithm cryptographic operation utility. prime Compute prime numbers. rand Generate pseudo-random bytes -- see Random Numbers rehash Create symbolic links to certificate and CRL files named by the hash values. req PKCS#10 X.509 Certificate Signing Request (CSR) Management. rsa RSA key management. rsautl RSA utility for signing, verification, encryption, and decryption. Superseded by pkeyutl(1). s_client This implements a generic SSL/TLS client which can establish a transparent connection to a remote server speaking SSL/TLS. s_server This implements a generic SSL/TLS server which accepts connections from remote clients speaking SSL/TLS. s_time SSL Connection Timer. sess_id SSL Session Data Management. smime S/MIME mail processing. speed Algorithm Speed Measurement. spkac SPKAC printing and generating utility. srp Maintain SRP password file. storeutl Utility to list and display certificates, keys, CRLs, etc. ts Time Stamping Authority tool (client/server). verify X.509 Certificate Verification. version OpenSSL Version Information. x509 X.509 Certificate Data Management. Generating an RSA Private Key Generating a private key can be done in a variety of different ways depending on the type of key, algorithm, bits, and other options your specific use case may require. In this example, we are generating a private key using RSA and a key size of 2048 bits.

$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem To generate a password protected private key, the previous command may be slightly amended as follows:

$ openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem The addition of the -aes256 option specifies the cipher to use to encrypt the private key file. For a list of available ciphers in the library, you can run the following command:

$ openssl list -cipher-algorithms With your private key in hand, you can use the following command to see the key's details, such as its modulus and its constituent primes. Remember to change the name of the input file to the file name of your private key.

$ openssl pkey -in private-key.pem -text The above command yields the following output in my specific case. Your output will differ but should be structurally similar.

-----BEGIN PRIVATE KEY----- MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDZD6IMLRFk4CaF w0rhRienwuE5EZ6xFE8e3C5TVi1+d9Enhi38RgkwD7UlWxPE6AWhp5T3kfrFWdak 1lZFVPp7/btOKLjKUru15nLoA4AKYtz9W9PhsM0dyzLc6FQ6K4ReQam5pHCqI2zF 82MwE+eIAduvuqyoQLKiI608EArWZqDtMUpBJzv0UVEYvRdnMWpCwfzpI+hPJywV CcTlNCT/ctGgBKyIx+dDuZ7bR9MNmSW7GreJEbTH+R13xT3dd/JCka1+LYCl4h0q oWhFPhOkvQzmmSzUmZlAlTDQLv2eAdJIrQcsnKZ3SsIOCC/3IpqwSzpid38Ill4O xH6XIrVFAgMBAAECggEBAJ2MC0JrM8TULSHJrf/0u7O4b2DMuTIuW386sSUr17mD nfviGF6TNvf7bq++e4rgHbZHvIg1HJ9Bpdne+J86HtUARYNlazru8fAFZEGiyLzB JUV/8TpO6ZJGepR8zSWrkFgZsOddw6i6LalADy5GRDcjoiDajZdR3lZxLrv5qOQU I1vKTf4Zs2Tl3gnaJ/Il1gBHIQ9W9xUH8jPBIwj51iXwCh8H0BiDPvFkU7cHIFCP sJhGsGp6OS3uSwwQuSE+NqbuPfVilysCcwgZduknyio0QO1YfMBL6+XoKE/bFHsn N+FzzczQg9sWyiwVR+3EeI9kp4JSElNh2nqG96i4QAECgYEA76OLUGrShHb4saoP aYnBAKLEdWj5K483JdY6BSbdd5RkDbJG8ExmcbfTas/BGdKc4iVCkxV3ysxKnX18 PfxATHDLL8NMa+gGgZY5oTKUsrXEpS132HhCJ9T9LoesQjRb4kOZH8POVqm6O4Xf lCt0y1+M1eQHI1NPO9CmPBgouEUCgYEA5+F4SS8RMyYRkU/kx195fwh0hhaOElzr E8mZou3NFL/XT6/9t+2+7sMTuiQCP9zIa6s+/rrXdjWtrTcDp4WlDITas0UUgZhv YVBQBF4vhHxIVwJxnT9Gwi4XM1JlFmVHofWD71P6DRe7jSWRS3CujP3AE9vmpWMx tE1D9qLiWQECgYB445LzFYBvrKjWz4iI4CJKFNJwvGz+iXfzkXehg7KzkVtMAYSB 0rjXYzm3J2ktgq778nn8Qxc0agy2GEil6GvzY+9MgAQ8Z0do9gTKif6zjLjP7vkH bdtJxsuWPoEqwMkdgqZrfNbJp0O4pVddovJ/agtdF3R2YJ+W+DH0HOfl1QKBgFnM c2zEEYEhaQRBUHP1gXO0rouPCI4L9e2/0QPL2/QBJzzxBuzH4X1NhsI7V7OrqOIp e0fiy7Y3q369I2ko1HY4rQln4z0c72VcWOCYKQbBqrInfCBNdPWWK93wNr2pk0gh cGqqtteDLVrIBbCVfsOTMWN/cZ7y/zi4A23sPoQBAoGAEPzcIjOyoB97Pzd7iNim Gin8RkwXIiFGSHo8vAh74CKBNokThM50OUNm5T2eJ4huzPpowQ+ID1mB5EjEai9n JY9ll3cUpawiIIW/6uGTHyXfvZWNtqEYXrVJ6fcDaKcW4y3cplNj/SJaBW8HXsW7 YGHW3zHsgy7EOAOzPwlm9oE= -----END PRIVATE KEY----- RSA Private-Key: (2048 bit, 2 primes) modulus: 00:d9:0f:a2:0c:2d:11:64:e0:26:85:c3:4a:e1:46: 27:a7:c2:e1:39:11:9e:b1:14:4f:1e:dc:2e:53:56: 2d:7e:77:d1:27:86:2d:fc:46:09:30:0f:b5:25:5b: 13:c4:e8:05:a1:a7:94:f7:91:fa:c5:59:d6:a4:d6: 56:45:54:fa:7b:fd:bb:4e:28:b8:ca:52:bb:b5:e6: 72:e8:03:80:0a:62:dc:fd:5b:d3:e1:b0:cd:1d:cb: 32:dc:e8:54:3a:2b:84:5e:41:a9:b9:a4:70:aa:23: 6c:c5:f3:63:30:13:e7:88:01:db:af:ba:ac:a8:40: b2:a2:23:ad:3c:10:0a:d6:66:a0:ed:31:4a:41:27: 3b:f4:51:51:18:bd:17:67:31:6a:42:c1:fc:e9:23: e8:4f:27:2c:15:09:c4:e5:34:24:ff:72:d1:a0:04: ac:88:c7:e7:43:b9:9e:db:47:d3:0d:99:25:bb:1a: b7:89:11:b4:c7:f9:1d:77:c5:3d:dd:77:f2:42:91: ad:7e:2d:80:a5:e2:1d:2a:a1:68:45:3e:13:a4:bd: 0c:e6:99:2c:d4:99:99:40:95:30:d0:2e:fd:9e:01: d2:48:ad:07:2c:9c:a6:77:4a:c2:0e:08:2f:f7:22: 9a:b0:4b:3a:62:77:7f:08:96:5e:0e:c4:7e:97:22: b5:45 publicExponent: 65537 (0x10001) privateExponent: 00:9d:8c:0b:42:6b:33:c4:d4:2d:21:c9:ad:ff:f4: bb:b3:b8:6f:60:cc:b9:32:2e:5b:7f:3a:b1:25:2b: d7:b9:83:9d:fb:e2:18:5e:93:36:f7:fb:6e:af:be: 7b:8a:e0:1d:b6:47:bc:88:35:1c:9f:41:a5:d9:de: f8:9f:3a:1e:d5:00:45:83:65:6b:3a:ee:f1:f0:05: 64:41:a2:c8:bc:c1:25:45:7f:f1:3a:4e:e9:92:46: 7a:94:7c:cd:25:ab:90:58:19:b0:e7:5d:c3:a8:ba: 2d:a9:40:0f:2e:46:44:37:23:a2:20:da:8d:97:51: de:56:71:2e:bb:f9:a8:e4:14:23:5b:ca:4d:fe:19: b3:64:e5:de:09:da:27:f2:25:d6:00:47:21:0f:56: f7:15:07:f2:33:c1:23:08:f9:d6:25:f0:0a:1f:07: d0:18:83:3e:f1:64:53:b7:07:20:50:8f:b0:98:46: b0:6a:7a:39:2d:ee:4b:0c:10:b9:21:3e:36:a6:ee: 3d:f5:62:97:2b:02:73:08:19:76:e9:27:ca:2a:34: 40:ed:58:7c:c0:4b:eb:e5:e8:28:4f:db:14:7b:27: 37:e1:73:cd:cc:d0:83:db:16:ca:2c:15:47:ed:c4: 78:8f:64:a7:82:52:12:53:61:da:7a:86:f7:a8:b8: 40:01 prime1: 00:ef:a3:8b:50:6a:d2:84:76:f8:b1:aa:0f:69:89: c1:00:a2:c4:75:68:f9:2b:8f:37:25:d6:3a:05:26: dd:77:94:64:0d:b2:46:f0:4c:66:71:b7:d3:6a:cf: c1:19:d2:9c:e2:25:42:93:15:77:ca:cc:4a:9d:7d: 7c:3d:fc:40:4c:70:cb:2f:c3:4c:6b:e8:06:81:96: 39:a1:32:94:b2:b5:c4:a5:2d:77:d8:78:42:27:d4: fd:2e:87:ac:42:34:5b:e2:43:99:1f:c3:ce:56:a9: ba:3b:85:df:94:2b:74:cb:5f:8c:d5:e4:07:23:53: 4f:3b:d0:a6:3c:18:28:b8:45 prime2: 00:e7:e1:78:49:2f:11:33:26:11:91:4f:e4:c7:5f: 79:7f:08:74:86:16:8e:12:5c:eb:13:c9:99:a2:ed: cd:14:bf:d7:4f:af:fd:b7:ed:be:ee:c3:13:ba:24: 02:3f:dc:c8:6b:ab:3e:fe:ba:d7:76:35:ad:ad:37: 03:a7:85:a5:0c:84:da:b3:45:14:81:98:6f:61:50: 50:04:5e:2f:84:7c:48:57:02:71:9d:3f:46:c2:2e: 17:33:52:65:16:65:47:a1:f5:83:ef:53:fa:0d:17: bb:8d:25:91:4b:70:ae:8c:fd:c0:13:db:e6:a5:63: 31:b4:4d:43:f6:a2:e2:59:01 exponent1: 78:e3:92:f3:15:80:6f:ac:a8:d6:cf:88:88:e0:22: 4a:14:d2:70:bc:6c:fe:89:77:f3:91:77:a1:83:b2: b3:91:5b:4c:01:84:81:d2:b8:d7:63:39:b7:27:69: 2d:82:ae:fb:f2:79:fc:43:17:34:6a:0c:b6:18:48: a5:e8:6b:f3:63:ef:4c:80:04:3c:67:47:68:f6:04: ca:89:fe:b3:8c:b8:cf:ee:f9:07:6d:db:49:c6:cb: 96:3e:81:2a:c0:c9:1d:82:a6:6b:7c:d6:c9:a7:43: b8:a5:57:5d:a2:f2:7f:6a:0b:5d:17:74:76:60:9f: 96:f8:31:f4:1c:e7:e5:d5 exponent2: 59:cc:73:6c:c4:11:81:21:69:04:41:50:73:f5:81: 73:b4:ae:8b:8f:08:8e:0b:f5:ed:bf:d1:03:cb:db: f4:01:27:3c:f1:06:ec:c7:e1:7d:4d:86:c2:3b:57: b3:ab:a8:e2:29:7b:47:e2:cb:b6:37:ab:7e:bd:23: 69:28:d4:76:38:ad:09:67:e3:3d:1c:ef:65:5c:58: e0:98:29:06:c1:aa:b2:27:7c:20:4d:74:f5:96:2b: dd:f0:36:bd:a9:93:48:21:70:6a:aa:b6:d7:83:2d: 5a:c8:05:b0:95:7e:c3:93:31:63:7f:71:9e:f2:ff: 38:b8:03:6d:ec:3e:84:01 coefficient: 10:fc:dc:22:33:b2:a0:1f:7b:3f:37:7b:88:d8:a6: 1a:29:fc:46:4c:17:22:21:46:48:7a:3c:bc:08:7b: e0:22:81:36:89:13:84:ce:74:39:43:66:e5:3d:9e: 27:88:6e:cc:fa:68:c1:0f:88:0f:59:81:e4:48:c4: 6a:2f:67:25:8f:65:97:77:14:a5:ac:22:20:85:bf: ea:e1:93:1f:25:df:bd:95:8d:b6:a1:18:5e:b5:49: e9:f7:03:68:a7:16:e3:2d:dc:a6:53:63:fd:22:5a: 05:6f:07:5e:c5:bb:60:61:d6:df:31:ec:83:2e:c4: 38:03:b3:3f:09:66:f6:81 Keep in mind the above key was generated solely for pedagogical purposes; never give anyone access to your private keys.

Generating a Public Key Having previously generated your private key, you may generate the corresponding public key using the following command.

$ openssl pkey -in private-key.pem -out public-key.pem -pubout You may once again view the key details, using a slightly different command this time.

$ openssl pkey -in public-key.pem -pubin -text The output for the public key will be shorter, as it carries much less information, and it will look something like this.

-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2Q+iDC0RZOAmhcNK4UYn p8LhORGesRRPHtwuU1YtfnfRJ4Yt/EYJMA+1JVsTxOgFoaeU95H6xVnWpNZWRVT6 e/27Tii4ylK7teZy6AOACmLc/VvT4bDNHcsy3OhUOiuEXkGpuaRwqiNsxfNjMBPn iAHbr7qsqECyoiOtPBAK1mag7TFKQSc79FFRGL0XZzFqQsH86SPoTycsFQnE5TQk /3LRoASsiMfnQ7me20fTDZkluxq3iRG0x/kdd8U93XfyQpGtfi2ApeIdKqFoRT4T pL0M5pks1JmZQJUw0C79ngHSSK0HLJymd0rCDggv9yKasEs6Ynd/CJZeDsR+lyK1 RQIDAQAB -----END PUBLIC KEY----- RSA Public-Key: (2048 bit) Modulus: 00:d9:0f:a2:0c:2d:11:64:e0:26:85:c3:4a:e1:46: 27:a7:c2:e1:39:11:9e:b1:14:4f:1e:dc:2e:53:56: 2d:7e:77:d1:27:86:2d:fc:46:09:30:0f:b5:25:5b: 13:c4:e8:05:a1:a7:94:f7:91:fa:c5:59:d6:a4:d6: 56:45:54:fa:7b:fd:bb:4e:28:b8:ca:52:bb:b5:e6: 72:e8:03:80:0a:62:dc:fd:5b:d3:e1:b0:cd:1d:cb: 32:dc:e8:54:3a:2b:84:5e:41:a9:b9:a4:70:aa:23: 6c:c5:f3:63:30:13:e7:88:01:db:af:ba:ac:a8:40: b2:a2:23:ad:3c:10:0a:d6:66:a0:ed:31:4a:41:27: 3b:f4:51:51:18:bd:17:67:31:6a:42:c1:fc:e9:23: e8:4f:27:2c:15:09:c4:e5:34:24:ff:72:d1:a0:04: ac:88:c7:e7:43:b9:9e:db:47:d3:0d:99:25:bb:1a: b7:89:11:b4:c7:f9:1d:77:c5:3d:dd:77:f2:42:91: ad:7e:2d:80:a5:e2:1d:2a:a1:68:45:3e:13:a4:bd: 0c:e6:99:2c:d4:99:99:40:95:30:d0:2e:fd:9e:01: d2:48:ad:07:2c:9c:a6:77:4a:c2:0e:08:2f:f7:22: 9a:b0:4b:3a:62:77:7f:08:96:5e:0e:c4:7e:97:22: b5:45 Exponent: 65537 (0x10001) For more information on generating keys, see the source code documentation, located in the doc/HOWTO/keys.txt file.

Generating Keys Based on Elliptic Curves There are essentially two steps to generating a key:

Generate the parameters for the specific curve you are using Use those parameters to generate the key To see the list of curves instrinsically supported by openssl, you can use the -list_curves option when calling the ecparam command.

$ openssl ecparam -list_curves secp112r1 : SECG/WTLS curve over a 112 bit prime field secp112r2 : SECG curve over a 112 bit prime field secp128r1 : SECG curve over a 128 bit prime field secp128r2 : SECG curve over a 128 bit prime field secp160k1 : SECG curve over a 160 bit prime field ... For this example I will use the prime256v1 curve, which is an X9.62/SECG curve over a 256 bit prime field.

Generating the Curve Parameters Having selected our curve, we now call ecparam to generate our parameters file.

$ openssl ecparam -name prime256v1 -out prime256v1.pem Printing Parameters to Standard Out You can print the generated curve parameters to the terminal output with the following command:

$ openssl ecparam -in prime256v1.pem -noout -text ASN1 OID: prime256v1 NIST CURVE: P-256 Printing Parameters as C Code Analogously, you may also output the generated curve parameters as C code. The parameters can then be loaded by calling the get_ec_group_XXX() function. To print the C code to the current terminal's output, the following command may be used:

$ openssl ecparam -in prime256v1.pem -noout -C And here are the first few lines of the corresponding output:

EC_GROUP *get_ec_group_256(void) { static unsigned char ec_p_256[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ... Generating the Key With the curve parameters in hand, we are now free to generate the key. Just as with the [#Generating an RSA Private Key|RSA] example above, we may optionally specify a cipher algorithm with which to encrypt the private key. The call to generate the key using the elliptic curve parameters generated in the example above looks like this:

$ openssl genpkey -aes256 -paramfile prime256v1.pem -out private-key.pem Enter PEM pass phrase: Verifying - Enter PEM pass phrase: Putting it All Together The process of generation a curve based on elliptic-curves can be streamlined by calling the genpkey command directly and specifying both the algorithm and the name of the curve to use for parameter generation. In it's simplest form, the command to generate a key based on the same curve as in the example above looks like this:

$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 This command will result in the generated key being printed to the terminal's output.

$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256

-----BEGIN PRIVATE KEY----- MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgqqYoJGowXJ5/GTkB SRLnBMNWLoQ2RM/QxrY+bfDDGRahRANCAASPY4eTANkwIIAWhh32eoFl2YFLJSWy bdITdZ82O5JDpDijmGmJ2hepe5afek9WVqxMPYjmbTwMPO3xMGbqUiJD -----END PRIVATE KEY----- Remember that you can specify a cipher algorithm to encrypt the key with, which something you may or may not want to do, depending on your specific use case. Here is a slightly more complete example showing a key generated with a password and written to a specific output file.

$ openssl genpkey -aes256 -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private-key.pem Enter PEM pass phrase: Verifying - Enter PEM pass phrase: Just as with the previous example, you can use the pkey command to inspect your newly-generated key.

$ openssl pkey -in private-key.pem -text Enter pass phrase for private-key.pem: -----BEGIN PRIVATE KEY----- MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgEO7CxgTwi0hsjdbp sXWuU2x2flLthxqXabYDOqOZCvuhRANCAAQVTLkeCBJdvMnqwZKYJxrPvTTuanrD NkyAPQCARKsQ7bVrP6ky/5uAcAvjuZB0xKCcSp7roXLWRzD/y/ik8P5R -----END PRIVATE KEY----- Private-Key: (256 bit) priv: 10:ee:c2:c6:04:f0:8b:48:6c:8d:d6:e9:b1:75:ae: 53:6c:76:7e:52:ed:87:1a:97:69:b6:03:3a:a3:99: 0a:fb pub: 04:15:4c:b9:1e:08:12:5d:bc:c9:ea:c1:92:98:27: 1a:cf:bd:34:ee:6a:7a:c3:36:4c:80:3d:00:80:44: ab:10:ed:b5:6b:3f:a9:32:ff:9b:80:70:0b:e3:b9: 90:74:c4:a0:9c:4a:9e:eb:a1:72:d6:47:30:ff:cb: f8:a4:f0:fe:51 ASN1 OID: prime256v1 NIST CURVE: P-256 For more details on elliptic curve cryptography or key generation, check out the manpages.

Base64 Encoding Strings For simple string encoding, you can use "here string" syntax with the base64 command as below. Intuitively, the -e flag specifies the action to be encoding.

$ openssl base64 -e <<< 'Welcome to openssl wiki' V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK Similarly, the base64 command's -d flag may be used to indicate decoding mode.

$ openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK' Welcome to openssl wiki

Note: base64 line length is limited to 76 characters by default in openssl (and generated with 64 characters per line).

openssl base64 -e <<< 'Welcome to openssl wiki with a very long line that splits...' V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRo YXQgc3BsaXRzLi4uCg== openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg==' => NOTHING!

To be able to decode a base64 line without line feeds that exceeds the default 76 character length restriction use the -A option.

openssl base64 -d -A <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg==' Welcome to openssl wiki with a very long line that splits... It is recommended to actually split base64 strings into multiple lines of 64 characters, however, since the -A option is buggy, particularly with its handling of long files.

Generating a File Hash One of the most basic uses of the dgst command (short for digest) is viewing the hash of a given file. To do this, simply invoke the command with the specified digest algorithm to use. For this example, I will be hashing an arbitrary file on my system using the MD5, SHA1, and SHA384 algorithms.

$ openssl dgst -md5 primes.dat MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664

$ openssl dgst -sha1 primes.dat SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32

$ openssl dgst -sha384 primes.dat SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271 For a list of the available digest algorithms, you can use the following command.

$ openssl list -digest-algorithms RSA-MD4 => MD4 RSA-MD5 => MD5 RSA-MDC2 => MDC2 RSA-RIPEMD160 => RIPEMD160 RSA-SHA1 => SHA1 RSA-SHA1-2 => RSA-SHA1 ... You can also use a similar command to see the available digest commands:

$ openssl list -digest-commands blake2b512 blake2s256 md5 sha1
sha224 sha256 sha3-224 sha3-256
sha3-384 sha3-512 sha384 sha512
sha512-224 sha512-256 shake128 shake256
sm3
Below are three sample invocations of the md5, sha1, and sha384 digest commands using the same file as the dgst command invocation above.

$ openssl md5 primes.dat MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664

$ openssl sha1 primes.dat SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32

$ openssl sha384 primes.dat SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271 File Encryption and Decryption The following example demonstrates a simple file encryption and decryption using the enc command. The first argument is the cipher algorithm to use for encrypting the file. For this example I carefully selected the AES-256 algorithm in CBC Mode by looking up the available ciphers and picking out the first one I saw. To see the list of available ciphers, you can use the following command.

$ openssl enc -ciphers Supported ciphers: -aes-128-cbc -aes-128-cfb -aes-128-cfb1
-aes-128-cfb8 -aes-128-ctr -aes-128-ecb
-aes-128-ofb -aes-192-cbc -aes-192-cfb
-aes-192-cfb1 -aes-192-cfb8 -aes-192-ctr ... You can also use the following command:

$ openssl list -cipher-algorithms AES-128-CBC AES-128-CBC-HMAC-SHA1 AES-128-CBC-HMAC-SHA256 id-aes128-CCM AES-128-CFB AES-128-CFB1 AES-128-CFB8 AES-128-CTR ... Having selected an encryption algorithm, you must then specify whether the action you are taking is either encryption or decryption via the -e or -d flags, respectively. The -iter flag specifies the number of iterations on the password used for deriving the encryption key. A higher iteration count increases the time required to brute-force the resulting file. Using this option implies enabling use of the Password-Based Key Derivation Function 2, usually set using the -pbkdf2 flag. We then use the -salt flag to enable the use of a randomly generated salt in the key-derivation function.

Putting it all together, you can see the command to encrypt a file and the corresponding output below. Note that the passwords entered by the user are blank, just as they would usually be in a terminal session.

$ openssl enc -aes-256-cbc -e -iter 1000 -salt -in primes.dat -out primes.enc enter aes-256-cbc encryption password: Verifying - enter aes-256-cbc encryption password: The analogous decryption command is as follows:

$ openssl enc -aes-256-cbc -d -iter 1000 -in primes.enc -out primes.dec enter aes-256-cbc decryption password: Further reading Paul Heinlein. "OpenSSL Command-Line HOWTO". Has many quick cookbook-style recipes for doing common tasks using the "openssl" command-line application.

Categories: ExamplesShell level

⚠️ **GitHub.com Fallback** ⚠️